blob: fbdf408e852c5c8bff06a4fc85f3131fc9683624 [file] [log] [blame]
Damien Miller398e1cf2002-02-05 11:52:13 +11001/* $OpenBSD: misc.c,v 1.15 2002/01/24 21:09:25 stevesk Exp $ */
Damien Millere4340be2000-09-16 13:29:08 +11002
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
Damien Miller61c51502000-08-18 14:01:04 +100027#include "includes.h"
Damien Miller398e1cf2002-02-05 11:52:13 +110028RCSID("$OpenBSD: misc.c,v 1.15 2002/01/24 21:09:25 stevesk Exp $");
Damien Miller61c51502000-08-18 14:01:04 +100029
Kevin Stevesb6e773a2001-02-04 13:20:36 +000030#include "misc.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000031#include "log.h"
Ben Lindstrom06909012001-03-05 06:09:31 +000032#include "xmalloc.h"
Damien Miller61c51502000-08-18 14:01:04 +100033
Ben Lindstrom4cc240d2001-07-04 04:46:56 +000034/* remove newline at end of string */
Damien Miller61c51502000-08-18 14:01:04 +100035char *
36chop(char *s)
37{
38 char *t = s;
39 while (*t) {
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +000040 if (*t == '\n' || *t == '\r') {
Damien Miller61c51502000-08-18 14:01:04 +100041 *t = '\0';
42 return s;
43 }
44 t++;
45 }
46 return s;
47
48}
49
Ben Lindstrom4cc240d2001-07-04 04:46:56 +000050/* set/unset filedescriptor to non-blocking */
Damien Miller61c51502000-08-18 14:01:04 +100051void
52set_nonblock(int fd)
53{
54 int val;
Ben Lindstromc93e84c2001-05-12 00:08:37 +000055
Damien Miller61c51502000-08-18 14:01:04 +100056 val = fcntl(fd, F_GETFL, 0);
57 if (val < 0) {
58 error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno));
59 return;
60 }
Damien Miller69b69aa2000-10-28 14:19:58 +110061 if (val & O_NONBLOCK) {
Ben Lindstromc93e84c2001-05-12 00:08:37 +000062 debug2("fd %d is O_NONBLOCK", fd);
Damien Miller61c51502000-08-18 14:01:04 +100063 return;
Damien Miller69b69aa2000-10-28 14:19:58 +110064 }
Damien Miller61c51502000-08-18 14:01:04 +100065 debug("fd %d setting O_NONBLOCK", fd);
66 val |= O_NONBLOCK;
67 if (fcntl(fd, F_SETFL, val) == -1)
Damien Millercaf6dd62000-08-29 11:33:50 +110068 if (errno != ENODEV)
69 error("fcntl(%d, F_SETFL, O_NONBLOCK): %s",
70 fd, strerror(errno));
Damien Miller61c51502000-08-18 14:01:04 +100071}
72
Ben Lindstromc93e84c2001-05-12 00:08:37 +000073void
74unset_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 }
Ben Lindstrom352b1c22001-06-21 03:04:37 +000087 debug("fd %d clearing O_NONBLOCK", fd);
Ben Lindstromc93e84c2001-05-12 00:08:37 +000088 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
Damien Miller398e1cf2002-02-05 11:52:13 +110095/* disable nagle on socket */
96void
97set_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
Damien Miller61c51502000-08-18 14:01:04 +1000106/* Characters considered whitespace in strsep calls. */
107#define WHITESPACE " \t\r\n"
108
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000109/* return next token in configuration line */
Damien Miller61c51502000-08-18 14:01:04 +1000110char *
111strdelim(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}
Kevin Stevesb6e773a2001-02-04 13:20:36 +0000136
Ben Lindstrom086cf212001-03-05 05:56:40 +0000137struct passwd *
138pwcopy(struct passwd *pw)
139{
140 struct passwd *copy = xmalloc(sizeof(*copy));
Ben Lindstrom40304422001-03-05 06:22:01 +0000141
Ben Lindstrom086cf212001-03-05 05:56:40 +0000142 memset(copy, 0, sizeof(*copy));
143 copy->pw_name = xstrdup(pw->pw_name);
144 copy->pw_passwd = xstrdup(pw->pw_passwd);
Ben Lindstrom40304422001-03-05 06:22:01 +0000145 copy->pw_gecos = xstrdup(pw->pw_gecos);
Ben Lindstrom086cf212001-03-05 05:56:40 +0000146 copy->pw_uid = pw->pw_uid;
147 copy->pw_gid = pw->pw_gid;
Kevin Steves82456952001-06-22 21:14:18 +0000148#ifdef HAVE_PW_EXPIRE_IN_PASSWD
Ben Lindstrom3af4d462001-06-21 03:11:27 +0000149 copy->pw_expire = pw->pw_expire;
Kevin Steves82456952001-06-22 21:14:18 +0000150#endif
151#ifdef HAVE_PW_CHANGE_IN_PASSWD
Ben Lindstrom3af4d462001-06-21 03:11:27 +0000152 copy->pw_change = pw->pw_change;
Kevin Steves82456952001-06-22 21:14:18 +0000153#endif
Ben Lindstrom0f68db42001-03-05 07:57:09 +0000154#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom086cf212001-03-05 05:56:40 +0000155 copy->pw_class = xstrdup(pw->pw_class);
Ben Lindstrom0f68db42001-03-05 07:57:09 +0000156#endif
Ben Lindstrom086cf212001-03-05 05:56:40 +0000157 copy->pw_dir = xstrdup(pw->pw_dir);
158 copy->pw_shell = xstrdup(pw->pw_shell);
159 return copy;
160}
161
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000162/*
163 * Convert ASCII string to TCP/IP port number.
164 * Port must be >0 and <=65535.
165 * Return 0 if invalid.
166 */
167int
168a2port(const char *s)
Ben Lindstrom19066a12001-04-12 23:39:26 +0000169{
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
Ben Lindstrom1bda4c82001-06-05 19:59:08 +0000183#define SECONDS 1
184#define MINUTES (SECONDS * 60)
185#define HOURS (MINUTES * 60)
186#define DAYS (HOURS * 24)
187#define WEEKS (DAYS * 7)
188
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000189/*
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 */
210long
211convtime(const char *s)
Ben Lindstrom1bda4c82001-06-05 19:59:08 +0000212{
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
Ben Lindstrom4529b702001-05-03 23:39:53 +0000265char *
266cleanhostname(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
275char *
276colon(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
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000298/* function to assist building execv() arguments */
Ben Lindstrom387c4722001-05-08 20:27:25 +0000299void
300addargs(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;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100312 } else if (args->num+2 >= args->nalloc)
Ben Lindstrom387c4722001-05-08 20:27:25 +0000313 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
Kevin Stevesb6e773a2001-02-04 13:20:36 +0000320mysig_t
321mysignal(int sig, mysig_t act)
322{
323#ifdef HAVE_SIGACTION
324 struct sigaction sa, osa;
325
Kevin Stevese74ebd02001-02-17 17:10:16 +0000326 if (sigaction(sig, NULL, &osa) == -1)
Kevin Stevesb6e773a2001-02-04 13:20:36 +0000327 return (mysig_t) -1;
328 if (osa.sa_handler != act) {
Kevin Stevese74ebd02001-02-17 17:10:16 +0000329 memset(&sa, 0, sizeof(sa));
Kevin Stevesb6e773a2001-02-04 13:20:36 +0000330 sigemptyset(&sa.sa_mask);
331 sa.sa_flags = 0;
Damien Miller722ccb12001-02-18 15:18:43 +1100332#if defined(SA_INTERRUPT)
333 if (sig == SIGALRM)
Damien Miller0318e2e2001-02-18 13:04:23 +1100334 sa.sa_flags |= SA_INTERRUPT;
335#endif
Kevin Stevesb6e773a2001-02-04 13:20:36 +0000336 sa.sa_handler = act;
Kevin Stevese74ebd02001-02-17 17:10:16 +0000337 if (sigaction(sig, &sa, NULL) == -1)
Kevin Stevesb6e773a2001-02-04 13:20:36 +0000338 return (mysig_t) -1;
339 }
340 return (osa.sa_handler);
341#else
342 return (signal(sig, act));
343#endif
344}