blob: 9e5a5aba63121aaec458d21468742fffa2a76e31 [file] [log] [blame]
Ben Lindstrom84fcb312002-03-05 01:48:09 +00001/* $OpenBSD: misc.c,v 1.18 2002/03/04 13:10:46 markus 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 Lindstrom84fcb312002-03-05 01:48:09 +000028RCSID("$OpenBSD: misc.c,v 1.18 2002/03/04 13:10:46 markus 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)
Ben Lindstrom84fcb312002-03-05 01:48:09 +000068 debug("fcntl(%d, F_SETFL, O_NONBLOCK): %s",
69 fd, strerror(errno));
Damien Miller61c51502000-08-18 14:01:04 +100070}
71
Ben Lindstromc93e84c2001-05-12 00:08:37 +000072void
73unset_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 }
Ben Lindstrom352b1c22001-06-21 03:04:37 +000086 debug("fd %d clearing O_NONBLOCK", fd);
Ben Lindstromc93e84c2001-05-12 00:08:37 +000087 val &= ~O_NONBLOCK;
88 if (fcntl(fd, F_SETFL, val) == -1)
Ben Lindstrom84fcb312002-03-05 01:48:09 +000089 debug("fcntl(%d, F_SETFL, O_NONBLOCK): %s",
90 fd, strerror(errno));
Ben Lindstromc93e84c2001-05-12 00:08:37 +000091}
92
Damien Miller398e1cf2002-02-05 11:52:13 +110093/* disable nagle on socket */
94void
95set_nodelay(int fd)
96{
Ben Lindstrome86de512002-03-05 01:28:14 +000097 int opt;
98 socklen_t optlen;
Damien Miller398e1cf2002-02-05 11:52:13 +110099
Ben Lindstrom1ebd7a52002-02-26 18:12:51 +0000100 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;
Damien Miller398e1cf2002-02-05 11:52:13 +1100110 debug("fd %d setting TCP_NODELAY", fd);
Ben Lindstrom1ebd7a52002-02-26 18:12:51 +0000111 if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1)
Damien Miller398e1cf2002-02-05 11:52:13 +1100112 error("setsockopt TCP_NODELAY: %.100s", strerror(errno));
113}
114
Damien Miller61c51502000-08-18 14:01:04 +1000115/* Characters considered whitespace in strsep calls. */
116#define WHITESPACE " \t\r\n"
117
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000118/* return next token in configuration line */
Damien Miller61c51502000-08-18 14:01:04 +1000119char *
120strdelim(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}
Kevin Stevesb6e773a2001-02-04 13:20:36 +0000145
Ben Lindstrom086cf212001-03-05 05:56:40 +0000146struct passwd *
147pwcopy(struct passwd *pw)
148{
149 struct passwd *copy = xmalloc(sizeof(*copy));
Ben Lindstrom40304422001-03-05 06:22:01 +0000150
Ben Lindstrom086cf212001-03-05 05:56:40 +0000151 memset(copy, 0, sizeof(*copy));
152 copy->pw_name = xstrdup(pw->pw_name);
153 copy->pw_passwd = xstrdup(pw->pw_passwd);
Ben Lindstrom40304422001-03-05 06:22:01 +0000154 copy->pw_gecos = xstrdup(pw->pw_gecos);
Ben Lindstrom086cf212001-03-05 05:56:40 +0000155 copy->pw_uid = pw->pw_uid;
156 copy->pw_gid = pw->pw_gid;
Kevin Steves82456952001-06-22 21:14:18 +0000157#ifdef HAVE_PW_EXPIRE_IN_PASSWD
Ben Lindstrom3af4d462001-06-21 03:11:27 +0000158 copy->pw_expire = pw->pw_expire;
Kevin Steves82456952001-06-22 21:14:18 +0000159#endif
160#ifdef HAVE_PW_CHANGE_IN_PASSWD
Ben Lindstrom3af4d462001-06-21 03:11:27 +0000161 copy->pw_change = pw->pw_change;
Kevin Steves82456952001-06-22 21:14:18 +0000162#endif
Ben Lindstrom0f68db42001-03-05 07:57:09 +0000163#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom086cf212001-03-05 05:56:40 +0000164 copy->pw_class = xstrdup(pw->pw_class);
Ben Lindstrom0f68db42001-03-05 07:57:09 +0000165#endif
Ben Lindstrom086cf212001-03-05 05:56:40 +0000166 copy->pw_dir = xstrdup(pw->pw_dir);
167 copy->pw_shell = xstrdup(pw->pw_shell);
168 return copy;
169}
170
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000171/*
172 * Convert ASCII string to TCP/IP port number.
173 * Port must be >0 and <=65535.
174 * Return 0 if invalid.
175 */
176int
177a2port(const char *s)
Ben Lindstrom19066a12001-04-12 23:39:26 +0000178{
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
Ben Lindstrom1bda4c82001-06-05 19:59:08 +0000192#define SECONDS 1
193#define MINUTES (SECONDS * 60)
194#define HOURS (MINUTES * 60)
195#define DAYS (HOURS * 24)
196#define WEEKS (DAYS * 7)
197
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000198/*
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 */
219long
220convtime(const char *s)
Ben Lindstrom1bda4c82001-06-05 19:59:08 +0000221{
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
Ben Lindstrom4529b702001-05-03 23:39:53 +0000274char *
275cleanhostname(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
284char *
285colon(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
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000307/* function to assist building execv() arguments */
Ben Lindstrom387c4722001-05-08 20:27:25 +0000308void
309addargs(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;
Damien Miller9f0f5c62001-12-21 14:45:46 +1100321 } else if (args->num+2 >= args->nalloc)
Ben Lindstrom387c4722001-05-08 20:27:25 +0000322 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
Kevin Stevesb6e773a2001-02-04 13:20:36 +0000329mysig_t
330mysignal(int sig, mysig_t act)
331{
332#ifdef HAVE_SIGACTION
333 struct sigaction sa, osa;
334
Kevin Stevese74ebd02001-02-17 17:10:16 +0000335 if (sigaction(sig, NULL, &osa) == -1)
Kevin Stevesb6e773a2001-02-04 13:20:36 +0000336 return (mysig_t) -1;
337 if (osa.sa_handler != act) {
Kevin Stevese74ebd02001-02-17 17:10:16 +0000338 memset(&sa, 0, sizeof(sa));
Kevin Stevesb6e773a2001-02-04 13:20:36 +0000339 sigemptyset(&sa.sa_mask);
340 sa.sa_flags = 0;
Damien Miller722ccb12001-02-18 15:18:43 +1100341#if defined(SA_INTERRUPT)
342 if (sig == SIGALRM)
Damien Miller0318e2e2001-02-18 13:04:23 +1100343 sa.sa_flags |= SA_INTERRUPT;
344#endif
Kevin Stevesb6e773a2001-02-04 13:20:36 +0000345 sa.sa_handler = act;
Kevin Stevese74ebd02001-02-17 17:10:16 +0000346 if (sigaction(sig, &sa, NULL) == -1)
Kevin Stevesb6e773a2001-02-04 13:20:36 +0000347 return (mysig_t) -1;
348 }
349 return (osa.sa_handler);
350#else
351 return (signal(sig, act));
352#endif
353}