]> andersk Git - openssh.git/blob - misc.c
- markus@cvs.openbsd.org 2002/03/04 13:10:46
[openssh.git] / misc.c
1 /*      $OpenBSD: misc.c,v 1.18 2002/03/04 13:10:46 markus 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.18 2002/03/04 13:10:46 markus 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                 debug("fcntl(%d, F_SETFL, O_NONBLOCK): %s",
69                     fd, strerror(errno));
70 }
71
72 void
73 unset_nonblock(int fd)
74 {
75         int val;
76
77         val = fcntl(fd, F_GETFL, 0);
78         if (val < 0) {
79                 error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno));
80                 return;
81         }
82         if (!(val & O_NONBLOCK)) {
83                 debug2("fd %d is not O_NONBLOCK", fd);
84                 return;
85         }
86         debug("fd %d clearing O_NONBLOCK", fd);
87         val &= ~O_NONBLOCK;
88         if (fcntl(fd, F_SETFL, val) == -1)
89                 debug("fcntl(%d, F_SETFL, O_NONBLOCK): %s",
90                     fd, strerror(errno));
91 }
92
93 /* disable nagle on socket */
94 void
95 set_nodelay(int fd)
96 {
97         int opt;
98         socklen_t optlen;
99
100         optlen = sizeof opt;
101         if (getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, &optlen) == -1) {
102                 error("getsockopt TCP_NODELAY: %.100s", strerror(errno));
103                 return;
104         }
105         if (opt == 1) {
106                 debug2("fd %d is TCP_NODELAY", fd);
107                 return;
108         }
109         opt = 1;
110         debug("fd %d setting TCP_NODELAY", fd);
111         if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1)
112                 error("setsockopt TCP_NODELAY: %.100s", strerror(errno));
113 }
114
115 /* Characters considered whitespace in strsep calls. */
116 #define WHITESPACE " \t\r\n"
117
118 /* return next token in configuration line */
119 char *
120 strdelim(char **s)
121 {
122         char *old;
123         int wspace = 0;
124
125         if (*s == NULL)
126                 return NULL;
127
128         old = *s;
129
130         *s = strpbrk(*s, WHITESPACE "=");
131         if (*s == NULL)
132                 return (old);
133
134         /* Allow only one '=' to be skipped */
135         if (*s[0] == '=')
136                 wspace = 1;
137         *s[0] = '\0';
138
139         *s += strspn(*s + 1, WHITESPACE) + 1;
140         if (*s[0] == '=' && !wspace)
141                 *s += strspn(*s + 1, WHITESPACE) + 1;
142
143         return (old);
144 }
145
146 struct passwd *
147 pwcopy(struct passwd *pw)
148 {
149         struct passwd *copy = xmalloc(sizeof(*copy));
150
151         memset(copy, 0, sizeof(*copy));
152         copy->pw_name = xstrdup(pw->pw_name);
153         copy->pw_passwd = xstrdup(pw->pw_passwd);
154         copy->pw_gecos = xstrdup(pw->pw_gecos);
155         copy->pw_uid = pw->pw_uid;
156         copy->pw_gid = pw->pw_gid;
157 #ifdef HAVE_PW_EXPIRE_IN_PASSWD
158         copy->pw_expire = pw->pw_expire;
159 #endif
160 #ifdef HAVE_PW_CHANGE_IN_PASSWD
161         copy->pw_change = pw->pw_change;
162 #endif
163 #ifdef HAVE_PW_CLASS_IN_PASSWD
164         copy->pw_class = xstrdup(pw->pw_class);
165 #endif
166         copy->pw_dir = xstrdup(pw->pw_dir);
167         copy->pw_shell = xstrdup(pw->pw_shell);
168         return copy;
169 }
170
171 /*
172  * Convert ASCII string to TCP/IP port number.
173  * Port must be >0 and <=65535.
174  * Return 0 if invalid.
175  */
176 int
177 a2port(const char *s)
178 {
179         long port;
180         char *endp;
181
182         errno = 0;
183         port = strtol(s, &endp, 0);
184         if (s == endp || *endp != '\0' ||
185             (errno == ERANGE && (port == LONG_MIN || port == LONG_MAX)) ||
186             port <= 0 || port > 65535)
187                 return 0;
188
189         return port;
190 }
191
192 #define SECONDS         1
193 #define MINUTES         (SECONDS * 60)
194 #define HOURS           (MINUTES * 60)
195 #define DAYS            (HOURS * 24)
196 #define WEEKS           (DAYS * 7)
197
198 /*
199  * Convert a time string into seconds; format is
200  * a sequence of:
201  *      time[qualifier]
202  *
203  * Valid time qualifiers are:
204  *      <none>  seconds
205  *      s|S     seconds
206  *      m|M     minutes
207  *      h|H     hours
208  *      d|D     days
209  *      w|W     weeks
210  *
211  * Examples:
212  *      90m     90 minutes
213  *      1h30m   90 minutes
214  *      2d      2 days
215  *      1w      1 week
216  *
217  * Return -1 if time string is invalid.
218  */
219 long
220 convtime(const char *s)
221 {
222         long total, secs;
223         const char *p;
224         char *endp;
225
226         errno = 0;
227         total = 0;
228         p = s;
229
230         if (p == NULL || *p == '\0')
231                 return -1;
232
233         while (*p) {
234                 secs = strtol(p, &endp, 10);
235                 if (p == endp ||
236                     (errno == ERANGE && (secs == LONG_MIN || secs == LONG_MAX)) ||
237                     secs < 0)
238                         return -1;
239
240                 switch (*endp++) {
241                 case '\0':
242                         endp--;
243                 case 's':
244                 case 'S':
245                         break;
246                 case 'm':
247                 case 'M':
248                         secs *= MINUTES;
249                         break;
250                 case 'h':
251                 case 'H':
252                         secs *= HOURS;
253                         break;
254                 case 'd':
255                 case 'D':
256                         secs *= DAYS;
257                         break;
258                 case 'w':
259                 case 'W':
260                         secs *= WEEKS;
261                         break;
262                 default:
263                         return -1;
264                 }
265                 total += secs;
266                 if (total < 0)
267                         return -1;
268                 p = endp;
269         }
270
271         return total;
272 }
273
274 char *
275 cleanhostname(char *host)
276 {
277         if (*host == '[' && host[strlen(host) - 1] == ']') {
278                 host[strlen(host) - 1] = '\0';
279                 return (host + 1);
280         } else
281                 return host;
282 }
283
284 char *
285 colon(char *cp)
286 {
287         int flag = 0;
288
289         if (*cp == ':')         /* Leading colon is part of file name. */
290                 return (0);
291         if (*cp == '[')
292                 flag = 1;
293
294         for (; *cp; ++cp) {
295                 if (*cp == '@' && *(cp+1) == '[')
296                         flag = 1;
297                 if (*cp == ']' && *(cp+1) == ':' && flag)
298                         return (cp+1);
299                 if (*cp == ':' && !flag)
300                         return (cp);
301                 if (*cp == '/')
302                         return (0);
303         }
304         return (0);
305 }
306
307 /* function to assist building execv() arguments */
308 void
309 addargs(arglist *args, char *fmt, ...)
310 {
311         va_list ap;
312         char buf[1024];
313
314         va_start(ap, fmt);
315         vsnprintf(buf, sizeof(buf), fmt, ap);
316         va_end(ap);
317
318         if (args->list == NULL) {
319                 args->nalloc = 32;
320                 args->num = 0;
321         } else if (args->num+2 >= args->nalloc)
322                 args->nalloc *= 2;
323
324         args->list = xrealloc(args->list, args->nalloc * sizeof(char *));
325         args->list[args->num++] = xstrdup(buf);
326         args->list[args->num] = NULL;
327 }
328
329 mysig_t
330 mysignal(int sig, mysig_t act)
331 {
332 #ifdef HAVE_SIGACTION
333         struct sigaction sa, osa;
334
335         if (sigaction(sig, NULL, &osa) == -1)
336                 return (mysig_t) -1;
337         if (osa.sa_handler != act) {
338                 memset(&sa, 0, sizeof(sa));
339                 sigemptyset(&sa.sa_mask);
340                 sa.sa_flags = 0;
341 #if defined(SA_INTERRUPT)
342                 if (sig == SIGALRM)
343                         sa.sa_flags |= SA_INTERRUPT;
344 #endif
345                 sa.sa_handler = act;
346                 if (sigaction(sig, &sa, NULL) == -1)
347                         return (mysig_t) -1;
348         }
349         return (osa.sa_handler);
350 #else
351         return (signal(sig, act));
352 #endif
353 }
This page took 0.060628 seconds and 5 git commands to generate.