]> andersk Git - openssh.git/blob - misc.c
- markus@cvs.openbsd.org 2002/02/11 16:19:39
[openssh.git] / misc.c
1 /*      $OpenBSD: misc.c,v 1.15 2002/01/24 21:09:25 stevesk Exp $       */
2
3 /*
4  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "includes.h"
28 RCSID("$OpenBSD: misc.c,v 1.15 2002/01/24 21:09:25 stevesk Exp $");
29
30 #include "misc.h"
31 #include "log.h"
32 #include "xmalloc.h"
33
34 /* remove newline at end of string */
35 char *
36 chop(char *s)
37 {
38         char *t = s;
39         while (*t) {
40                 if (*t == '\n' || *t == '\r') {
41                         *t = '\0';
42                         return s;
43                 }
44                 t++;
45         }
46         return s;
47
48 }
49
50 /* set/unset filedescriptor to non-blocking */
51 void
52 set_nonblock(int fd)
53 {
54         int val;
55
56         val = fcntl(fd, F_GETFL, 0);
57         if (val < 0) {
58                 error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno));
59                 return;
60         }
61         if (val & O_NONBLOCK) {
62                 debug2("fd %d is O_NONBLOCK", fd);
63                 return;
64         }
65         debug("fd %d setting O_NONBLOCK", fd);
66         val |= O_NONBLOCK;
67         if (fcntl(fd, F_SETFL, val) == -1)
68                 if (errno != ENODEV)
69                         error("fcntl(%d, F_SETFL, O_NONBLOCK): %s",
70                             fd, strerror(errno));
71 }
72
73 void
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;
82         }
83         if (!(val & O_NONBLOCK)) {
84                 debug2("fd %d is not O_NONBLOCK", fd);
85                 return;
86         }
87         debug("fd %d clearing O_NONBLOCK", fd);
88         val &= ~O_NONBLOCK;
89         if (fcntl(fd, F_SETFL, val) == -1)
90                 if (errno != ENODEV)
91                         error("fcntl(%d, F_SETFL, O_NONBLOCK): %s",
92                             fd, strerror(errno));
93 }
94
95 /* disable nagle on socket */
96 void
97 set_nodelay(int fd)
98 {
99         int on = 1;
100
101         debug("fd %d setting TCP_NODELAY", fd);
102         if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof on) == -1)
103                 error("setsockopt TCP_NODELAY: %.100s", strerror(errno));
104 }
105
106 /* Characters considered whitespace in strsep calls. */
107 #define WHITESPACE " \t\r\n"
108
109 /* return next token in configuration line */
110 char *
111 strdelim(char **s)
112 {
113         char *old;
114         int wspace = 0;
115
116         if (*s == NULL)
117                 return NULL;
118
119         old = *s;
120
121         *s = strpbrk(*s, WHITESPACE "=");
122         if (*s == NULL)
123                 return (old);
124
125         /* Allow only one '=' to be skipped */
126         if (*s[0] == '=')
127                 wspace = 1;
128         *s[0] = '\0';
129
130         *s += strspn(*s + 1, WHITESPACE) + 1;
131         if (*s[0] == '=' && !wspace)
132                 *s += strspn(*s + 1, WHITESPACE) + 1;
133
134         return (old);
135 }
136
137 struct passwd *
138 pwcopy(struct passwd *pw)
139 {
140         struct passwd *copy = xmalloc(sizeof(*copy));
141
142         memset(copy, 0, sizeof(*copy));
143         copy->pw_name = xstrdup(pw->pw_name);
144         copy->pw_passwd = xstrdup(pw->pw_passwd);
145         copy->pw_gecos = xstrdup(pw->pw_gecos);
146         copy->pw_uid = pw->pw_uid;
147         copy->pw_gid = pw->pw_gid;
148 #ifdef HAVE_PW_EXPIRE_IN_PASSWD
149         copy->pw_expire = pw->pw_expire;
150 #endif
151 #ifdef HAVE_PW_CHANGE_IN_PASSWD
152         copy->pw_change = pw->pw_change;
153 #endif
154 #ifdef HAVE_PW_CLASS_IN_PASSWD
155         copy->pw_class = xstrdup(pw->pw_class);
156 #endif
157         copy->pw_dir = xstrdup(pw->pw_dir);
158         copy->pw_shell = xstrdup(pw->pw_shell);
159         return copy;
160 }
161
162 /*
163  * Convert ASCII string to TCP/IP port number.
164  * Port must be >0 and <=65535.
165  * Return 0 if invalid.
166  */
167 int
168 a2port(const char *s)
169 {
170         long port;
171         char *endp;
172
173         errno = 0;
174         port = strtol(s, &endp, 0);
175         if (s == endp || *endp != '\0' ||
176             (errno == ERANGE && (port == LONG_MIN || port == LONG_MAX)) ||
177             port <= 0 || port > 65535)
178                 return 0;
179
180         return port;
181 }
182
183 #define SECONDS         1
184 #define MINUTES         (SECONDS * 60)
185 #define HOURS           (MINUTES * 60)
186 #define DAYS            (HOURS * 24)
187 #define WEEKS           (DAYS * 7)
188
189 /*
190  * Convert a time string into seconds; format is
191  * a sequence of:
192  *      time[qualifier]
193  *
194  * Valid time qualifiers are:
195  *      <none>  seconds
196  *      s|S     seconds
197  *      m|M     minutes
198  *      h|H     hours
199  *      d|D     days
200  *      w|W     weeks
201  *
202  * Examples:
203  *      90m     90 minutes
204  *      1h30m   90 minutes
205  *      2d      2 days
206  *      1w      1 week
207  *
208  * Return -1 if time string is invalid.
209  */
210 long
211 convtime(const char *s)
212 {
213         long total, secs;
214         const char *p;
215         char *endp;
216
217         errno = 0;
218         total = 0;
219         p = s;
220
221         if (p == NULL || *p == '\0')
222                 return -1;
223
224         while (*p) {
225                 secs = strtol(p, &endp, 10);
226                 if (p == endp ||
227                     (errno == ERANGE && (secs == LONG_MIN || secs == LONG_MAX)) ||
228                     secs < 0)
229                         return -1;
230
231                 switch (*endp++) {
232                 case '\0':
233                         endp--;
234                 case 's':
235                 case 'S':
236                         break;
237                 case 'm':
238                 case 'M':
239                         secs *= MINUTES;
240                         break;
241                 case 'h':
242                 case 'H':
243                         secs *= HOURS;
244                         break;
245                 case 'd':
246                 case 'D':
247                         secs *= DAYS;
248                         break;
249                 case 'w':
250                 case 'W':
251                         secs *= WEEKS;
252                         break;
253                 default:
254                         return -1;
255                 }
256                 total += secs;
257                 if (total < 0)
258                         return -1;
259                 p = endp;
260         }
261
262         return total;
263 }
264
265 char *
266 cleanhostname(char *host)
267 {
268         if (*host == '[' && host[strlen(host) - 1] == ']') {
269                 host[strlen(host) - 1] = '\0';
270                 return (host + 1);
271         } else
272                 return host;
273 }
274
275 char *
276 colon(char *cp)
277 {
278         int flag = 0;
279
280         if (*cp == ':')         /* Leading colon is part of file name. */
281                 return (0);
282         if (*cp == '[')
283                 flag = 1;
284
285         for (; *cp; ++cp) {
286                 if (*cp == '@' && *(cp+1) == '[')
287                         flag = 1;
288                 if (*cp == ']' && *(cp+1) == ':' && flag)
289                         return (cp+1);
290                 if (*cp == ':' && !flag)
291                         return (cp);
292                 if (*cp == '/')
293                         return (0);
294         }
295         return (0);
296 }
297
298 /* function to assist building execv() arguments */
299 void
300 addargs(arglist *args, char *fmt, ...)
301 {
302         va_list ap;
303         char buf[1024];
304
305         va_start(ap, fmt);
306         vsnprintf(buf, sizeof(buf), fmt, ap);
307         va_end(ap);
308
309         if (args->list == NULL) {
310                 args->nalloc = 32;
311                 args->num = 0;
312         } else if (args->num+2 >= args->nalloc)
313                 args->nalloc *= 2;
314
315         args->list = xrealloc(args->list, args->nalloc * sizeof(char *));
316         args->list[args->num++] = xstrdup(buf);
317         args->list[args->num] = NULL;
318 }
319
320 mysig_t
321 mysignal(int sig, mysig_t act)
322 {
323 #ifdef HAVE_SIGACTION
324         struct sigaction sa, osa;
325
326         if (sigaction(sig, NULL, &osa) == -1)
327                 return (mysig_t) -1;
328         if (osa.sa_handler != act) {
329                 memset(&sa, 0, sizeof(sa));
330                 sigemptyset(&sa.sa_mask);
331                 sa.sa_flags = 0;
332 #if defined(SA_INTERRUPT)
333                 if (sig == SIGALRM)
334                         sa.sa_flags |= SA_INTERRUPT;
335 #endif
336                 sa.sa_handler = act;
337                 if (sigaction(sig, &sa, NULL) == -1)
338                         return (mysig_t) -1;
339         }
340         return (osa.sa_handler);
341 #else
342         return (signal(sig, act));
343 #endif
344 }
This page took 0.149782 seconds and 5 git commands to generate.