Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 1 | /* $OpenBSD: misc.c,v 1.12 2001/06/26 17:27:24 markus Exp $ */ |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 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 | |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 27 | #include "includes.h" |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 28 | RCSID("$OpenBSD: misc.c,v 1.12 2001/06/26 17:27:24 markus Exp $"); |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 29 | |
Kevin Steves | b6e773a | 2001-02-04 13:20:36 +0000 | [diff] [blame] | 30 | #include "misc.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 31 | #include "log.h" |
Ben Lindstrom | 0690901 | 2001-03-05 06:09:31 +0000 | [diff] [blame] | 32 | #include "xmalloc.h" |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 33 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 34 | /* remove newline at end of string */ |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 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 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 50 | /* set/unset filedescriptor to non-blocking */ |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 51 | void |
| 52 | set_nonblock(int fd) |
| 53 | { |
| 54 | int val; |
Ben Lindstrom | c93e84c | 2001-05-12 00:08:37 +0000 | [diff] [blame] | 55 | |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 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 | } |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 61 | if (val & O_NONBLOCK) { |
Ben Lindstrom | c93e84c | 2001-05-12 00:08:37 +0000 | [diff] [blame] | 62 | debug2("fd %d is O_NONBLOCK", fd); |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 63 | return; |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 64 | } |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 65 | debug("fd %d setting O_NONBLOCK", fd); |
| 66 | val |= O_NONBLOCK; |
| 67 | if (fcntl(fd, F_SETFL, val) == -1) |
Damien Miller | caf6dd6 | 2000-08-29 11:33:50 +1100 | [diff] [blame] | 68 | if (errno != ENODEV) |
| 69 | error("fcntl(%d, F_SETFL, O_NONBLOCK): %s", |
| 70 | fd, strerror(errno)); |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 71 | } |
| 72 | |
Ben Lindstrom | c93e84c | 2001-05-12 00:08:37 +0000 | [diff] [blame] | 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 | } |
Ben Lindstrom | 352b1c2 | 2001-06-21 03:04:37 +0000 | [diff] [blame] | 87 | debug("fd %d clearing O_NONBLOCK", fd); |
Ben Lindstrom | c93e84c | 2001-05-12 00:08:37 +0000 | [diff] [blame] | 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 | |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 95 | /* Characters considered whitespace in strsep calls. */ |
| 96 | #define WHITESPACE " \t\r\n" |
| 97 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 98 | /* return next token in configuration line */ |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 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 | } |
Kevin Steves | b6e773a | 2001-02-04 13:20:36 +0000 | [diff] [blame] | 125 | |
Ben Lindstrom | 086cf21 | 2001-03-05 05:56:40 +0000 | [diff] [blame] | 126 | struct passwd * |
| 127 | pwcopy(struct passwd *pw) |
| 128 | { |
| 129 | struct passwd *copy = xmalloc(sizeof(*copy)); |
Ben Lindstrom | 4030442 | 2001-03-05 06:22:01 +0000 | [diff] [blame] | 130 | |
Ben Lindstrom | 086cf21 | 2001-03-05 05:56:40 +0000 | [diff] [blame] | 131 | memset(copy, 0, sizeof(*copy)); |
| 132 | copy->pw_name = xstrdup(pw->pw_name); |
| 133 | copy->pw_passwd = xstrdup(pw->pw_passwd); |
Ben Lindstrom | 4030442 | 2001-03-05 06:22:01 +0000 | [diff] [blame] | 134 | copy->pw_gecos = xstrdup(pw->pw_gecos); |
Ben Lindstrom | 086cf21 | 2001-03-05 05:56:40 +0000 | [diff] [blame] | 135 | copy->pw_uid = pw->pw_uid; |
| 136 | copy->pw_gid = pw->pw_gid; |
Kevin Steves | 8245695 | 2001-06-22 21:14:18 +0000 | [diff] [blame] | 137 | #ifdef HAVE_PW_EXPIRE_IN_PASSWD |
Ben Lindstrom | 3af4d46 | 2001-06-21 03:11:27 +0000 | [diff] [blame] | 138 | copy->pw_expire = pw->pw_expire; |
Kevin Steves | 8245695 | 2001-06-22 21:14:18 +0000 | [diff] [blame] | 139 | #endif |
| 140 | #ifdef HAVE_PW_CHANGE_IN_PASSWD |
Ben Lindstrom | 3af4d46 | 2001-06-21 03:11:27 +0000 | [diff] [blame] | 141 | copy->pw_change = pw->pw_change; |
Kevin Steves | 8245695 | 2001-06-22 21:14:18 +0000 | [diff] [blame] | 142 | #endif |
Ben Lindstrom | 0f68db4 | 2001-03-05 07:57:09 +0000 | [diff] [blame] | 143 | #ifdef HAVE_PW_CLASS_IN_PASSWD |
Ben Lindstrom | 086cf21 | 2001-03-05 05:56:40 +0000 | [diff] [blame] | 144 | copy->pw_class = xstrdup(pw->pw_class); |
Ben Lindstrom | 0f68db4 | 2001-03-05 07:57:09 +0000 | [diff] [blame] | 145 | #endif |
Ben Lindstrom | 086cf21 | 2001-03-05 05:56:40 +0000 | [diff] [blame] | 146 | copy->pw_dir = xstrdup(pw->pw_dir); |
| 147 | copy->pw_shell = xstrdup(pw->pw_shell); |
| 148 | return copy; |
| 149 | } |
| 150 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 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) |
Ben Lindstrom | 19066a1 | 2001-04-12 23:39:26 +0000 | [diff] [blame] | 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 | |
Ben Lindstrom | 1bda4c8 | 2001-06-05 19:59:08 +0000 | [diff] [blame] | 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 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 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) |
Ben Lindstrom | 1bda4c8 | 2001-06-05 19:59:08 +0000 | [diff] [blame] | 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 | |
Ben Lindstrom | 4529b70 | 2001-05-03 23:39:53 +0000 | [diff] [blame] | 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 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 287 | /* function to assist building execv() arguments */ |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 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 | |
Kevin Steves | b6e773a | 2001-02-04 13:20:36 +0000 | [diff] [blame] | 309 | mysig_t |
| 310 | mysignal(int sig, mysig_t act) |
| 311 | { |
| 312 | #ifdef HAVE_SIGACTION |
| 313 | struct sigaction sa, osa; |
| 314 | |
Kevin Steves | e74ebd0 | 2001-02-17 17:10:16 +0000 | [diff] [blame] | 315 | if (sigaction(sig, NULL, &osa) == -1) |
Kevin Steves | b6e773a | 2001-02-04 13:20:36 +0000 | [diff] [blame] | 316 | return (mysig_t) -1; |
| 317 | if (osa.sa_handler != act) { |
Kevin Steves | e74ebd0 | 2001-02-17 17:10:16 +0000 | [diff] [blame] | 318 | memset(&sa, 0, sizeof(sa)); |
Kevin Steves | b6e773a | 2001-02-04 13:20:36 +0000 | [diff] [blame] | 319 | sigemptyset(&sa.sa_mask); |
| 320 | sa.sa_flags = 0; |
Damien Miller | 722ccb1 | 2001-02-18 15:18:43 +1100 | [diff] [blame] | 321 | #if defined(SA_INTERRUPT) |
| 322 | if (sig == SIGALRM) |
Damien Miller | 0318e2e | 2001-02-18 13:04:23 +1100 | [diff] [blame] | 323 | sa.sa_flags |= SA_INTERRUPT; |
| 324 | #endif |
Kevin Steves | b6e773a | 2001-02-04 13:20:36 +0000 | [diff] [blame] | 325 | sa.sa_handler = act; |
Kevin Steves | e74ebd0 | 2001-02-17 17:10:16 +0000 | [diff] [blame] | 326 | if (sigaction(sig, &sa, NULL) == -1) |
Kevin Steves | b6e773a | 2001-02-04 13:20:36 +0000 | [diff] [blame] | 327 | return (mysig_t) -1; |
| 328 | } |
| 329 | return (osa.sa_handler); |
| 330 | #else |
| 331 | return (signal(sig, act)); |
| 332 | #endif |
| 333 | } |