]> andersk Git - gssapi-openssh.git/blob - openssh/misc.c
Merge from OPENSSH_3_8_1P1_GSSAPI_20040713 to OPENSSH_3_9P1_GSSAPI_20040818.
[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.25 2004/08/11 21:43:05 avsm 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 char *
297 cleanhostname(char *host)
298 {
299         if (*host == '[' && host[strlen(host) - 1] == ']') {
300                 host[strlen(host) - 1] = '\0';
301                 return (host + 1);
302         } else
303                 return host;
304 }
305
306 char *
307 colon(char *cp)
308 {
309         int flag = 0;
310
311         if (*cp == ':')         /* Leading colon is part of file name. */
312                 return (0);
313         if (*cp == '[')
314                 flag = 1;
315
316         for (; *cp; ++cp) {
317                 if (*cp == '@' && *(cp+1) == '[')
318                         flag = 1;
319                 if (*cp == ']' && *(cp+1) == ':' && flag)
320                         return (cp+1);
321                 if (*cp == ':' && !flag)
322                         return (cp);
323                 if (*cp == '/')
324                         return (0);
325         }
326         return (0);
327 }
328
329 /* function to assist building execv() arguments */
330 void
331 addargs(arglist *args, char *fmt, ...)
332 {
333         va_list ap;
334         char buf[1024];
335         u_int nalloc;
336
337         va_start(ap, fmt);
338         vsnprintf(buf, sizeof(buf), fmt, ap);
339         va_end(ap);
340
341         nalloc = args->nalloc;
342         if (args->list == NULL) {
343                 nalloc = 32;
344                 args->num = 0;
345         } else if (args->num+2 >= nalloc)
346                 nalloc *= 2;
347
348         args->list = xrealloc(args->list, nalloc * sizeof(char *));
349         args->nalloc = nalloc;
350         args->list[args->num++] = xstrdup(buf);
351         args->list[args->num] = NULL;
352 }
This page took 0.073212 seconds and 5 git commands to generate.