blob: 620121fdca29f7e2fbf915c764a2e4bfb73e7949 [file] [log] [blame]
Ben Lindstrom4cc240d2001-07-04 04:46:56 +00001/* $OpenBSD: misc.c,v 1.12 2001/06/26 17:27:24 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 Lindstrom4cc240d2001-07-04 04:46:56 +000028RCSID("$OpenBSD: misc.c,v 1.12 2001/06/26 17:27:24 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) {
40 if(*t == '\n' || *t == '\r') {
41 *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 Miller61c51502000-08-18 14:01:04 +100095/* Characters considered whitespace in strsep calls. */
96#define WHITESPACE " \t\r\n"
97
Ben Lindstrom4cc240d2001-07-04 04:46:56 +000098/* return next token in configuration line */
Damien Miller61c51502000-08-18 14:01:04 +100099char *
100strdelim(char **s)
101{
102 char *old;
103 int wspace = 0;
104
105 if (*s == NULL)
106 return NULL;
107
108 old = *s;
109
110 *s = strpbrk(*s, WHITESPACE "=");
111 if (*s == NULL)
112 return (old);
113
114 /* Allow only one '=' to be skipped */
115 if (*s[0] == '=')
116 wspace = 1;
117 *s[0] = '\0';
118
119 *s += strspn(*s + 1, WHITESPACE) + 1;
120 if (*s[0] == '=' && !wspace)
121 *s += strspn(*s + 1, WHITESPACE) + 1;
122
123 return (old);
124}
Kevin Stevesb6e773a2001-02-04 13:20:36 +0000125
Ben Lindstrom086cf212001-03-05 05:56:40 +0000126struct passwd *
127pwcopy(struct passwd *pw)
128{
129 struct passwd *copy = xmalloc(sizeof(*copy));
Ben Lindstrom40304422001-03-05 06:22:01 +0000130
Ben Lindstrom086cf212001-03-05 05:56:40 +0000131 memset(copy, 0, sizeof(*copy));
132 copy->pw_name = xstrdup(pw->pw_name);
133 copy->pw_passwd = xstrdup(pw->pw_passwd);
Ben Lindstrom40304422001-03-05 06:22:01 +0000134 copy->pw_gecos = xstrdup(pw->pw_gecos);
Ben Lindstrom086cf212001-03-05 05:56:40 +0000135 copy->pw_uid = pw->pw_uid;
136 copy->pw_gid = pw->pw_gid;
Kevin Steves82456952001-06-22 21:14:18 +0000137#ifdef HAVE_PW_EXPIRE_IN_PASSWD
Ben Lindstrom3af4d462001-06-21 03:11:27 +0000138 copy->pw_expire = pw->pw_expire;
Kevin Steves82456952001-06-22 21:14:18 +0000139#endif
140#ifdef HAVE_PW_CHANGE_IN_PASSWD
Ben Lindstrom3af4d462001-06-21 03:11:27 +0000141 copy->pw_change = pw->pw_change;
Kevin Steves82456952001-06-22 21:14:18 +0000142#endif
Ben Lindstrom0f68db42001-03-05 07:57:09 +0000143#ifdef HAVE_PW_CLASS_IN_PASSWD
Ben Lindstrom086cf212001-03-05 05:56:40 +0000144 copy->pw_class = xstrdup(pw->pw_class);
Ben Lindstrom0f68db42001-03-05 07:57:09 +0000145#endif
Ben Lindstrom086cf212001-03-05 05:56:40 +0000146 copy->pw_dir = xstrdup(pw->pw_dir);
147 copy->pw_shell = xstrdup(pw->pw_shell);
148 return copy;
149}
150
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000151/*
152 * Convert ASCII string to TCP/IP port number.
153 * Port must be >0 and <=65535.
154 * Return 0 if invalid.
155 */
156int
157a2port(const char *s)
Ben Lindstrom19066a12001-04-12 23:39:26 +0000158{
159 long port;
160 char *endp;
161
162 errno = 0;
163 port = strtol(s, &endp, 0);
164 if (s == endp || *endp != '\0' ||
165 (errno == ERANGE && (port == LONG_MIN || port == LONG_MAX)) ||
166 port <= 0 || port > 65535)
167 return 0;
168
169 return port;
170}
171
Ben Lindstrom1bda4c82001-06-05 19:59:08 +0000172#define SECONDS 1
173#define MINUTES (SECONDS * 60)
174#define HOURS (MINUTES * 60)
175#define DAYS (HOURS * 24)
176#define WEEKS (DAYS * 7)
177
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000178/*
179 * Convert a time string into seconds; format is
180 * a sequence of:
181 * time[qualifier]
182 *
183 * Valid time qualifiers are:
184 * <none> seconds
185 * s|S seconds
186 * m|M minutes
187 * h|H hours
188 * d|D days
189 * w|W weeks
190 *
191 * Examples:
192 * 90m 90 minutes
193 * 1h30m 90 minutes
194 * 2d 2 days
195 * 1w 1 week
196 *
197 * Return -1 if time string is invalid.
198 */
199long
200convtime(const char *s)
Ben Lindstrom1bda4c82001-06-05 19:59:08 +0000201{
202 long total, secs;
203 const char *p;
204 char *endp;
205
206 errno = 0;
207 total = 0;
208 p = s;
209
210 if (p == NULL || *p == '\0')
211 return -1;
212
213 while (*p) {
214 secs = strtol(p, &endp, 10);
215 if (p == endp ||
216 (errno == ERANGE && (secs == LONG_MIN || secs == LONG_MAX)) ||
217 secs < 0)
218 return -1;
219
220 switch (*endp++) {
221 case '\0':
222 endp--;
223 case 's':
224 case 'S':
225 break;
226 case 'm':
227 case 'M':
228 secs *= MINUTES;
229 break;
230 case 'h':
231 case 'H':
232 secs *= HOURS;
233 break;
234 case 'd':
235 case 'D':
236 secs *= DAYS;
237 break;
238 case 'w':
239 case 'W':
240 secs *= WEEKS;
241 break;
242 default:
243 return -1;
244 }
245 total += secs;
246 if (total < 0)
247 return -1;
248 p = endp;
249 }
250
251 return total;
252}
253
Ben Lindstrom4529b702001-05-03 23:39:53 +0000254char *
255cleanhostname(char *host)
256{
257 if (*host == '[' && host[strlen(host) - 1] == ']') {
258 host[strlen(host) - 1] = '\0';
259 return (host + 1);
260 } else
261 return host;
262}
263
264char *
265colon(char *cp)
266{
267 int flag = 0;
268
269 if (*cp == ':') /* Leading colon is part of file name. */
270 return (0);
271 if (*cp == '[')
272 flag = 1;
273
274 for (; *cp; ++cp) {
275 if (*cp == '@' && *(cp+1) == '[')
276 flag = 1;
277 if (*cp == ']' && *(cp+1) == ':' && flag)
278 return (cp+1);
279 if (*cp == ':' && !flag)
280 return (cp);
281 if (*cp == '/')
282 return (0);
283 }
284 return (0);
285}
286
Ben Lindstrom4cc240d2001-07-04 04:46:56 +0000287/* function to assist building execv() arguments */
Ben Lindstrom387c4722001-05-08 20:27:25 +0000288void
289addargs(arglist *args, char *fmt, ...)
290{
291 va_list ap;
292 char buf[1024];
293
294 va_start(ap, fmt);
295 vsnprintf(buf, sizeof(buf), fmt, ap);
296 va_end(ap);
297
298 if (args->list == NULL) {
299 args->nalloc = 32;
300 args->num = 0;
301 } else if (args->num+2 >= args->nalloc)
302 args->nalloc *= 2;
303
304 args->list = xrealloc(args->list, args->nalloc * sizeof(char *));
305 args->list[args->num++] = xstrdup(buf);
306 args->list[args->num] = NULL;
307}
308
Kevin Stevesb6e773a2001-02-04 13:20:36 +0000309mysig_t
310mysignal(int sig, mysig_t act)
311{
312#ifdef HAVE_SIGACTION
313 struct sigaction sa, osa;
314
Kevin Stevese74ebd02001-02-17 17:10:16 +0000315 if (sigaction(sig, NULL, &osa) == -1)
Kevin Stevesb6e773a2001-02-04 13:20:36 +0000316 return (mysig_t) -1;
317 if (osa.sa_handler != act) {
Kevin Stevese74ebd02001-02-17 17:10:16 +0000318 memset(&sa, 0, sizeof(sa));
Kevin Stevesb6e773a2001-02-04 13:20:36 +0000319 sigemptyset(&sa.sa_mask);
320 sa.sa_flags = 0;
Damien Miller722ccb12001-02-18 15:18:43 +1100321#if defined(SA_INTERRUPT)
322 if (sig == SIGALRM)
Damien Miller0318e2e2001-02-18 13:04:23 +1100323 sa.sa_flags |= SA_INTERRUPT;
324#endif
Kevin Stevesb6e773a2001-02-04 13:20:36 +0000325 sa.sa_handler = act;
Kevin Stevese74ebd02001-02-17 17:10:16 +0000326 if (sigaction(sig, &sa, NULL) == -1)
Kevin Stevesb6e773a2001-02-04 13:20:36 +0000327 return (mysig_t) -1;
328 }
329 return (osa.sa_handler);
330#else
331 return (signal(sig, act));
332#endif
333}