]> andersk Git - gssapi-openssh.git/blob - openssh/misc.c
merged OpenSSH 4.0p1 to trunk and removed GSSAPI compat flags needed for compat with...
[gssapi-openssh.git] / openssh / misc.c
1 /*
2  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24
25 #include "includes.h"
26 RCSID("$OpenBSD: misc.c,v 1.28 2005/03/01 10:09:52 djm Exp $");
27
28 #include "misc.h"
29 #include "log.h"
30 #include "xmalloc.h"
31
32 /* remove newline at end of string */
33 char *
34 chop(char *s)
35 {
36         char *t = s;
37         while (*t) {
38                 if (*t == '\n' || *t == '\r') {
39                         *t = '\0';
40                         return s;
41                 }
42                 t++;
43         }
44         return s;
45
46 }
47
48 /* set/unset filedescriptor to non-blocking */
49 int
50 set_nonblock(int fd)
51 {
52         int val;
53
54         val = fcntl(fd, F_GETFL, 0);
55         if (val < 0) {
56                 error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno));
57                 return (-1);
58         }
59         if (val & O_NONBLOCK) {
60                 debug3("fd %d is O_NONBLOCK", fd);
61                 return (0);
62         }
63         debug2("fd %d setting O_NONBLOCK", fd);
64         val |= O_NONBLOCK;
65         if (fcntl(fd, F_SETFL, val) == -1) {
66                 debug("fcntl(%d, F_SETFL, O_NONBLOCK): %s", fd,
67                     strerror(errno));
68                 return (-1);
69         }
70         return (0);
71 }
72
73 int
74 unset_nonblock(int fd)
75 {
76         int val;
77
78         val = fcntl(fd, F_GETFL, 0);
79         if (val < 0) {
80                 error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno));
81                 return (-1);
82         }
83         if (!(val & O_NONBLOCK)) {
84                 debug3("fd %d is not O_NONBLOCK", fd);
85                 return (0);
86         }
87         debug("fd %d clearing O_NONBLOCK", fd);
88         val &= ~O_NONBLOCK;
89         if (fcntl(fd, F_SETFL, val) == -1) {
90                 debug("fcntl(%d, F_SETFL, ~O_NONBLOCK): %s",
91                     fd, strerror(errno));
92                 return (-1);
93         }
94         return (0);
95 }
96
97 /* disable nagle on socket */
98 void
99 set_nodelay(int fd)
100 {
101         int opt;
102         socklen_t optlen;
103
104         optlen = sizeof opt;
105         if (getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, &optlen) == -1) {
106                 debug("getsockopt TCP_NODELAY: %.100s", strerror(errno));
107                 return;
108         }
109         if (opt == 1) {
110                 debug2("fd %d is TCP_NODELAY", fd);
111                 return;
112         }
113         opt = 1;
114         debug2("fd %d setting TCP_NODELAY", fd);
115         if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1)
116                 error("setsockopt TCP_NODELAY: %.100s", strerror(errno));
117 }
118
119 /* Characters considered whitespace in strsep calls. */
120 #define WHITESPACE " \t\r\n"
121
122 /* Characters considered as quotations. */
123 #define QUOTES "'\""
124
125 /* return next token in configuration line */
126 char *
127 strdelim(char **s)
128 {
129         char *old, *p, *q;
130         int wspace = 0;
131
132         if (*s == NULL)
133                 return NULL;
134
135         old = *s;
136
137         if ((q=strchr(QUOTES, (int) *old)) && *q)
138         {
139             /* find next quote character, point old to start of quoted
140              * string */
141             for (p = ++old;*p && *p!=*q; p++)
142                  ;
143             
144             /* find start of next token */
145             *s = (*p) ? p + strspn(p + 1, WHITESPACE) + 1 : NULL;
146             
147             /* terminate 'old' token */
148             *p = '\0';
149             return (old);
150         }
151
152         *s = strpbrk(*s, WHITESPACE "=");
153         if (*s == NULL)
154                 return (old);
155
156         /* Allow only one '=' to be skipped */
157         if (*s[0] == '=')
158                 wspace = 1;
159         *s[0] = '\0';
160
161         *s += strspn(*s + 1, WHITESPACE) + 1;
162         if (*s[0] == '=' && !wspace)
163                 *s += strspn(*s + 1, WHITESPACE) + 1;
164
165         return (old);
166 }
167
168 struct passwd *
169 pwcopy(struct passwd *pw)
170 {
171         struct passwd *copy = xmalloc(sizeof(*copy));
172
173         memset(copy, 0, sizeof(*copy));
174         copy->pw_name = xstrdup(pw->pw_name);
175         copy->pw_passwd = xstrdup(pw->pw_passwd);
176         copy->pw_gecos = xstrdup(pw->pw_gecos);
177         copy->pw_uid = pw->pw_uid;
178         copy->pw_gid = pw->pw_gid;
179 #ifdef HAVE_PW_EXPIRE_IN_PASSWD
180         copy->pw_expire = pw->pw_expire;
181 #endif
182 #ifdef HAVE_PW_CHANGE_IN_PASSWD
183         copy->pw_change = pw->pw_change;
184 #endif
185 #ifdef HAVE_PW_CLASS_IN_PASSWD
186         copy->pw_class = xstrdup(pw->pw_class);
187 #endif
188         copy->pw_dir = xstrdup(pw->pw_dir);
189         copy->pw_shell = xstrdup(pw->pw_shell);
190         return copy;
191 }
192
193 /*
194  * Convert ASCII string to TCP/IP port number.
195  * Port must be >0 and <=65535.
196  * Return 0 if invalid.
197  */
198 int
199 a2port(const char *s)
200 {
201         long port;
202         char *endp;
203
204         errno = 0;
205         port = strtol(s, &endp, 0);
206         if (s == endp || *endp != '\0' ||
207             (errno == ERANGE && (port == LONG_MIN || port == LONG_MAX)) ||
208             port <= 0 || port > 65535)
209                 return 0;
210
211         return port;
212 }
213
214 #define SECONDS         1
215 #define MINUTES         (SECONDS * 60)
216 #define HOURS           (MINUTES * 60)
217 #define DAYS            (HOURS * 24)
218 #define WEEKS           (DAYS * 7)
219
220 /*
221  * Convert a time string into seconds; format is
222  * a sequence of:
223  *      time[qualifier]
224  *
225  * Valid time qualifiers are:
226  *      <none>  seconds
227  *      s|S     seconds
228  *      m|M     minutes
229  *      h|H     hours
230  *      d|D     days
231  *      w|W     weeks
232  *
233  * Examples:
234  *      90m     90 minutes
235  *      1h30m   90 minutes
236  *      2d      2 days
237  *      1w      1 week
238  *
239  * Return -1 if time string is invalid.
240  */
241 long
242 convtime(const char *s)
243 {
244         long total, secs;
245         const char *p;
246         char *endp;
247
248         errno = 0;
249         total = 0;
250         p = s;
251
252         if (p == NULL || *p == '\0')
253                 return -1;
254
255         while (*p) {
256                 secs = strtol(p, &endp, 10);
257                 if (p == endp ||
258                     (errno == ERANGE && (secs == LONG_MIN || secs == LONG_MAX)) ||
259                     secs < 0)
260                         return -1;
261
262                 switch (*endp++) {
263                 case '\0':
264                         endp--;
265                 case 's':
266                 case 'S':
267                         break;
268                 case 'm':
269                 case 'M':
270                         secs *= MINUTES;
271                         break;
272                 case 'h':
273                 case 'H':
274                         secs *= HOURS;
275                         break;
276                 case 'd':
277                 case 'D':
278                         secs *= DAYS;
279                         break;
280                 case 'w':
281                 case 'W':
282                         secs *= WEEKS;
283                         break;
284                 default:
285                         return -1;
286                 }
287                 total += secs;
288                 if (total < 0)
289                         return -1;
290                 p = endp;
291         }
292
293         return total;
294 }
295
296 /*
297  * Search for next delimiter between hostnames/addresses and ports.
298  * Argument may be modified (for termination).
299  * Returns *cp if parsing succeeds.
300  * *cp is set to the start of the next delimiter, if one was found.
301  * If this is the last field, *cp is set to NULL.
302  */
303 char *
304 hpdelim(char **cp)
305 {
306         char *s, *old;
307
308         if (cp == NULL || *cp == NULL)
309                 return NULL;
310
311         old = s = *cp;
312         if (*s == '[') {
313                 if ((s = strchr(s, ']')) == NULL)
314                         return NULL;
315                 else
316                         s++;
317         } else if ((s = strpbrk(s, ":/")) == NULL)
318                 s = *cp + strlen(*cp); /* skip to end (see first case below) */
319
320         switch (*s) {
321         case '\0':
322                 *cp = NULL;     /* no more fields*/
323                 break;
324         
325         case ':':
326         case '/':
327                 *s = '\0';      /* terminate */
328                 *cp = s + 1;
329                 break;
330         
331         default:
332                 return NULL;
333         }
334
335         return old;
336 }
337
338 char *
339 cleanhostname(char *host)
340 {
341         if (*host == '[' && host[strlen(host) - 1] == ']') {
342                 host[strlen(host) - 1] = '\0';
343                 return (host + 1);
344         } else
345                 return host;
346 }
347
348 char *
349 colon(char *cp)
350 {
351         int flag = 0;
352
353         if (*cp == ':')         /* Leading colon is part of file name. */
354                 return (0);
355         if (*cp == '[')
356                 flag = 1;
357
358         for (; *cp; ++cp) {
359                 if (*cp == '@' && *(cp+1) == '[')
360                         flag = 1;
361                 if (*cp == ']' && *(cp+1) == ':' && flag)
362                         return (cp+1);
363                 if (*cp == ':' && !flag)
364                         return (cp);
365                 if (*cp == '/')
366                         return (0);
367         }
368         return (0);
369 }
370
371 /* function to assist building execv() arguments */
372 void
373 addargs(arglist *args, char *fmt, ...)
374 {
375         va_list ap;
376         char buf[1024];
377         u_int nalloc;
378
379         va_start(ap, fmt);
380         vsnprintf(buf, sizeof(buf), fmt, ap);
381         va_end(ap);
382
383         nalloc = args->nalloc;
384         if (args->list == NULL) {
385                 nalloc = 32;
386                 args->num = 0;
387         } else if (args->num+2 >= nalloc)
388                 nalloc *= 2;
389
390         args->list = xrealloc(args->list, nalloc * sizeof(char *));
391         args->nalloc = nalloc;
392         args->list[args->num++] = xstrdup(buf);
393         args->list[args->num] = NULL;
394 }
395
396 /*
397  * Read an entire line from a public key file into a static buffer, discarding
398  * lines that exceed the buffer size.  Returns 0 on success, -1 on failure.
399  */
400 int
401 read_keyfile_line(FILE *f, const char *filename, char *buf, size_t bufsz,
402    u_long *lineno)
403 {
404         while (fgets(buf, bufsz, f) != NULL) {
405                 (*lineno)++;
406                 if (buf[strlen(buf) - 1] == '\n' || feof(f)) {
407                         return 0;
408                 } else {
409                         debug("%s: %s line %lu exceeds size limit", __func__,
410                             filename, *lineno);
411                         /* discard remainder of line */
412                         while(fgetc(f) != '\n' && !feof(f))
413                                 ;       /* nothing */
414                 }
415         }
416         return -1;
417 }
This page took 0.072947 seconds and 5 git commands to generate.