]>
Commit | Line | Data |
---|---|---|
255cabd9 | 1 | /* $OpenBSD: misc.c,v 1.12 2001/06/26 17:27:24 markus Exp $ */ |
bcbf86ec | 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 | ||
6d24277f | 27 | #include "includes.h" |
255cabd9 | 28 | RCSID("$OpenBSD: misc.c,v 1.12 2001/06/26 17:27:24 markus Exp $"); |
6d24277f | 29 | |
1aa00dcb | 30 | #include "misc.h" |
42f11eb2 | 31 | #include "log.h" |
4ab21f86 | 32 | #include "xmalloc.h" |
6d24277f | 33 | |
255cabd9 | 34 | /* remove newline at end of string */ |
6d24277f | 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 | ||
255cabd9 | 50 | /* set/unset filedescriptor to non-blocking */ |
6d24277f | 51 | void |
52 | set_nonblock(int fd) | |
53 | { | |
54 | int val; | |
89aa792b | 55 | |
6d24277f | 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 | } | |
a22aff1f | 61 | if (val & O_NONBLOCK) { |
89aa792b | 62 | debug2("fd %d is O_NONBLOCK", fd); |
6d24277f | 63 | return; |
a22aff1f | 64 | } |
6d24277f | 65 | debug("fd %d setting O_NONBLOCK", fd); |
66 | val |= O_NONBLOCK; | |
67 | if (fcntl(fd, F_SETFL, val) == -1) | |
14a9a859 | 68 | if (errno != ENODEV) |
69 | error("fcntl(%d, F_SETFL, O_NONBLOCK): %s", | |
70 | fd, strerror(errno)); | |
6d24277f | 71 | } |
72 | ||
89aa792b | 73 | void |
74 | unset_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 | } | |
e04e7a19 | 87 | debug("fd %d clearing O_NONBLOCK", fd); |
89aa792b | 88 | 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 | ||
6d24277f | 95 | /* Characters considered whitespace in strsep calls. */ |
96 | #define WHITESPACE " \t\r\n" | |
97 | ||
255cabd9 | 98 | /* return next token in configuration line */ |
6d24277f | 99 | char * |
100 | strdelim(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 | } | |
1aa00dcb | 125 | |
3b1a83df | 126 | struct passwd * |
127 | pwcopy(struct passwd *pw) | |
128 | { | |
129 | struct passwd *copy = xmalloc(sizeof(*copy)); | |
5649fbbe | 130 | |
3b1a83df | 131 | memset(copy, 0, sizeof(*copy)); |
132 | copy->pw_name = xstrdup(pw->pw_name); | |
133 | copy->pw_passwd = xstrdup(pw->pw_passwd); | |
5649fbbe | 134 | copy->pw_gecos = xstrdup(pw->pw_gecos); |
3b1a83df | 135 | copy->pw_uid = pw->pw_uid; |
136 | copy->pw_gid = pw->pw_gid; | |
7751d4eb | 137 | #ifdef HAVE_PW_EXPIRE_IN_PASSWD |
c4d49b85 | 138 | copy->pw_expire = pw->pw_expire; |
7751d4eb | 139 | #endif |
140 | #ifdef HAVE_PW_CHANGE_IN_PASSWD | |
c4d49b85 | 141 | copy->pw_change = pw->pw_change; |
7751d4eb | 142 | #endif |
2605addd | 143 | #ifdef HAVE_PW_CLASS_IN_PASSWD |
3b1a83df | 144 | copy->pw_class = xstrdup(pw->pw_class); |
2605addd | 145 | #endif |
3b1a83df | 146 | copy->pw_dir = xstrdup(pw->pw_dir); |
147 | copy->pw_shell = xstrdup(pw->pw_shell); | |
148 | return copy; | |
149 | } | |
150 | ||
255cabd9 | 151 | /* |
152 | * Convert ASCII string to TCP/IP port number. | |
153 | * Port must be >0 and <=65535. | |
154 | * Return 0 if invalid. | |
155 | */ | |
156 | int | |
157 | a2port(const char *s) | |
2d2a2c65 | 158 | { |
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 | ||
e2b1fb42 | 172 | #define SECONDS 1 |
173 | #define MINUTES (SECONDS * 60) | |
174 | #define HOURS (MINUTES * 60) | |
175 | #define DAYS (HOURS * 24) | |
176 | #define WEEKS (DAYS * 7) | |
177 | ||
255cabd9 | 178 | /* |
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 | */ | |
199 | long | |
200 | convtime(const char *s) | |
e2b1fb42 | 201 | { |
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 | ||
1fcde3fe | 254 | char * |
255 | cleanhostname(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 | ||
264 | char * | |
265 | colon(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 | ||
255cabd9 | 287 | /* function to assist building execv() arguments */ |
8a624ebf | 288 | void |
289 | addargs(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 | ||
1aa00dcb | 309 | mysig_t |
310 | mysignal(int sig, mysig_t act) | |
311 | { | |
312 | #ifdef HAVE_SIGACTION | |
313 | struct sigaction sa, osa; | |
314 | ||
120bf679 | 315 | if (sigaction(sig, NULL, &osa) == -1) |
1aa00dcb | 316 | return (mysig_t) -1; |
317 | if (osa.sa_handler != act) { | |
120bf679 | 318 | memset(&sa, 0, sizeof(sa)); |
1aa00dcb | 319 | sigemptyset(&sa.sa_mask); |
320 | sa.sa_flags = 0; | |
d54d99a3 | 321 | #if defined(SA_INTERRUPT) |
322 | if (sig == SIGALRM) | |
df538d55 | 323 | sa.sa_flags |= SA_INTERRUPT; |
25cd3375 | 324 | #endif |
1aa00dcb | 325 | sa.sa_handler = act; |
120bf679 | 326 | if (sigaction(sig, &sa, NULL) == -1) |
1aa00dcb | 327 | return (mysig_t) -1; |
328 | } | |
329 | return (osa.sa_handler); | |
330 | #else | |
331 | return (signal(sig, act)); | |
332 | #endif | |
333 | } |