blob: 46cc4319898851df7896271a8f0e1d4495c1b356 [file] [log] [blame]
Ben Lindstrome86de512002-03-05 01:28:14 +00001/* $OpenBSD: misc.c,v 1.17 2002/02/26 20:03:51 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"
Ben Lindstrome86de512002-03-05 01:28:14 +000028RCSID("$OpenBSD: misc.c,v 1.17 2002/02/26 20:03:51 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{
Ben Lindstrome86de512002-03-05 01:28:14 +000099 int opt;
100 socklen_t optlen;
Damien Miller398e1cf2002-02-05 11:52:13 +1100101
Ben Lindstrom1ebd7a52002-02-26 18:12:51 +0000102 optlen = sizeof opt;
103 if (getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, &optlen) == -1) {
104 error("getsockopt TCP_NODELAY: %.100s", strerror(errno));
105 return;
106 }
107 if (opt == 1) {
108 debug2("fd %d is TCP_NODELAY", fd);
109 return;
110 }
111 opt = 1;
Damien Miller398e1cf2002-02-05 11:52:13 +1100112 debug("fd %d setting TCP_NODELAY", fd);
Ben Lindstrom1ebd7a52002-02-26 18:12:51 +0000113 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1)
Damien Miller398e1cf2002-02-05 11:52:13 +1100114 error("setsockopt TCP_NODELAY: %.100s", strerror(errno));
115}
116
Damien Miller61c51502000-08-18 14:01:04 +1000117/* Characters considered whitespace in strsep calls. */
118#define WHITESPACE " \t\r\n"
119
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000120/* return next token in configuration line */
Damien Miller61c51502000-08-18 14:01:04 +1000121char *
122strdelim(char **s)
123{
124 char *old;
125 int wspace = 0;
126
127 if (*s == NULL)
128 return NULL;
129
130 old = *s;
131
132 *s = strpbrk(*s, WHITESPACE "=");
133 if (*s == NULL)
134 return (old);
135
136 /* Allow only one '=' to be skipped */
137 if (*s[0] == '=')
138 wspace = 1;
139 *s[0] = '\0';
140
141 *s += strspn(*s + 1, WHITESPACE) + 1;
142 if (*s[0] == '=' && !wspace)
143 *s += strspn(*s + 1, WHITESPACE) + 1;
144
145 return (old);
146}
Kevin Stevesb6e773a2001-02-04 13:20:36 +0000147
Ben Lindstrom086cf212001-03-05 05:56:40 +0000148struct passwd *
149pwcopy(struct passwd *pw)
150{
151 struct passwd *copy = xmalloc(sizeof(*copy));
Ben Lindstrom40304422001-03-05 06:22:01 +0000152
Ben Lindstrom086cf212001-03-05 05:56:40 +0000153 memset(copy, 0, sizeof(*copy));
154 copy->pw_name = xstrdup(pw->pw_name);
155 copy->pw_passwd = xstrdup(pw->pw_passwd);
Ben Lindstrom40304422001-03-05 06:22:01 +0000156 copy->pw_gecos = xstrdup(pw->pw_gecos);
Ben Lindstrom086cf212001-03-05 05:56:40 +0000157 copy->pw_uid = pw->pw_uid;
158 copy->pw_gid = pw->pw_gid;
Kevin Steves82456952001-06-22 21:14:18 +0000159#ifdef HAVE_PW_EXPIRE_IN_PASSWD
Ben Lindstrom3af4d462001-06-21 03:11:27 +0000160 copy->pw_expire = pw->pw_expire;
Kevin Steves82456952001-06-22 21:14:18 +0000161#endif
162#ifdef HAVE_PW_CHANGE_IN_PASSWD
Ben Lindstrom3af4d462001-06-21 03:11:27 +0000163 copy->pw_change = pw->pw_change;
Kevin Steves82456952001-06-22 21:14:18 +0000164#endif
Ben Lindstrom0f68db42001-03-05 07:57:09 +0000165#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom086cf212001-03-05 05:56:40 +0000166 copy->pw_class = xstrdup(pw->pw_class);
Ben Lindstrom0f68db42001-03-05 07:57:09 +0000167#endif
Ben Lindstrom086cf212001-03-05 05:56:40 +0000168 copy->pw_dir = xstrdup(pw->pw_dir);
169 copy->pw_shell = xstrdup(pw->pw_shell);
170 return copy;
171}
172
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000173/*
174 * Convert ASCII string to TCP/IP port number.
175 * Port must be >0 and <=65535.
176 * Return 0 if invalid.
177 */
178int
179a2port(const char *s)
Ben Lindstrom19066a12001-04-12 23:39:26 +0000180{
181 long port;
182 char *endp;
183
184 errno = 0;
185 port = strtol(s, &endp, 0);
186 if (s == endp || *endp != '\0' ||
187 (errno == ERANGE && (port == LONG_MIN || port == LONG_MAX)) ||
188 port <= 0 || port > 65535)
189 return 0;
190
191 return port;
192}
193
Ben Lindstrom1bda4c82001-06-05 19:59:08 +0000194#define SECONDS 1
195#define MINUTES (SECONDS * 60)
196#define HOURS (MINUTES * 60)
197#define DAYS (HOURS * 24)
198#define WEEKS (DAYS * 7)
199
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000200/*
201 * Convert a time string into seconds; format is
202 * a sequence of:
203 * time[qualifier]
204 *
205 * Valid time qualifiers are:
206 * <none> seconds
207 * s|S seconds
208 * m|M minutes
209 * h|H hours
210 * d|D days
211 * w|W weeks
212 *
213 * Examples:
214 * 90m 90 minutes
215 * 1h30m 90 minutes
216 * 2d 2 days
217 * 1w 1 week
218 *
219 * Return -1 if time string is invalid.
220 */
221long
222convtime(const char *s)
Ben Lindstrom1bda4c82001-06-05 19:59:08 +0000223{
224 long total, secs;
225 const char *p;
226 char *endp;
227
228 errno = 0;
229 total = 0;
230 p = s;
231
232 if (p == NULL || *p == '\0')
233 return -1;
234
235 while (*p) {
236 secs = strtol(p, &endp, 10);
237 if (p == endp ||
238 (errno == ERANGE && (secs == LONG_MIN || secs == LONG_MAX)) ||
239 secs < 0)
240 return -1;
241
242 switch (*endp++) {
243 case '\0':
244 endp--;
245 case 's':
246 case 'S':
247 break;
248 case 'm':
249 case 'M':
250 secs *= MINUTES;
251 break;
252 case 'h':
253 case 'H':
254 secs *= HOURS;
255 break;
256 case 'd':
257 case 'D':
258 secs *= DAYS;
259 break;
260 case 'w':
261 case 'W':
262 secs *= WEEKS;
263 break;
264 default:
265 return -1;
266 }
267 total += secs;
268 if (total < 0)
269 return -1;
270 p = endp;
271 }
272
273 return total;
274}
275
Ben Lindstrom4529b702001-05-03 23:39:53 +0000276char *
277cleanhostname(char *host)
278{
279 if (*host == '[' && host[strlen(host) - 1] == ']') {
280 host[strlen(host) - 1] = '\0';
281 return (host + 1);
282 } else
283 return host;
284}
285
286char *
287colon(char *cp)
288{
289 int flag = 0;
290
291 if (*cp == ':') /* Leading colon is part of file name. */
292 return (0);
293 if (*cp == '[')
294 flag = 1;
295
296 for (; *cp; ++cp) {
297 if (*cp == '@' && *(cp+1) == '[')
298 flag = 1;
299 if (*cp == ']' && *(cp+1) == ':' && flag)
300 return (cp+1);
301 if (*cp == ':' && !flag)
302 return (cp);
303 if (*cp == '/')
304 return (0);
305 }
306 return (0);
307}
308
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000309/* function to assist building execv() arguments */
Ben Lindstrom387c4722001-05-08 20:27:25 +0000310void
311addargs(arglist *args, char *fmt, ...)
312{
313 va_list ap;
314 char buf[1024];
315
316 va_start(ap, fmt);
317 vsnprintf(buf, sizeof(buf), fmt, ap);
318 va_end(ap);
319
320 if (args->list == NULL) {
321 args->nalloc = 32;
322 args->num = 0;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100323 } else if (args->num+2 >= args->nalloc)
Ben Lindstrom387c4722001-05-08 20:27:25 +0000324 args->nalloc *= 2;
325
326 args->list = xrealloc(args->list, args->nalloc * sizeof(char *));
327 args->list[args->num++] = xstrdup(buf);
328 args->list[args->num] = NULL;
329}
330
Kevin Stevesb6e773a2001-02-04 13:20:36 +0000331mysig_t
332mysignal(int sig, mysig_t act)
333{
334#ifdef HAVE_SIGACTION
335 struct sigaction sa, osa;
336
Kevin Stevese74ebd02001-02-17 17:10:16 +0000337 if (sigaction(sig, NULL, &osa) == -1)
Kevin Stevesb6e773a2001-02-04 13:20:36 +0000338 return (mysig_t) -1;
339 if (osa.sa_handler != act) {
Kevin Stevese74ebd02001-02-17 17:10:16 +0000340 memset(&sa, 0, sizeof(sa));
Kevin Stevesb6e773a2001-02-04 13:20:36 +0000341 sigemptyset(&sa.sa_mask);
342 sa.sa_flags = 0;
Damien Miller722ccb12001-02-18 15:18:43 +1100343#if defined(SA_INTERRUPT)
344 if (sig == SIGALRM)
Damien Miller0318e2e2001-02-18 13:04:23 +1100345 sa.sa_flags |= SA_INTERRUPT;
346#endif
Kevin Stevesb6e773a2001-02-04 13:20:36 +0000347 sa.sa_handler = act;
Kevin Stevese74ebd02001-02-17 17:10:16 +0000348 if (sigaction(sig, &sa, NULL) == -1)
Kevin Stevesb6e773a2001-02-04 13:20:36 +0000349 return (mysig_t) -1;
350 }
351 return (osa.sa_handler);
352#else
353 return (signal(sig, act));
354#endif
355}