Damien Miller | 398e1cf | 2002-02-05 11:52:13 +1100 | [diff] [blame] | 1 | /* $OpenBSD: misc.c,v 1.15 2002/01/24 21:09:25 stevesk 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" |
Damien Miller | 398e1cf | 2002-02-05 11:52:13 +1100 | [diff] [blame] | 28 | RCSID("$OpenBSD: misc.c,v 1.15 2002/01/24 21:09:25 stevesk 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) { |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 40 | if (*t == '\n' || *t == '\r') { |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 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 | 398e1cf | 2002-02-05 11:52:13 +1100 | [diff] [blame] | 95 | /* disable nagle on socket */ |
| 96 | void |
| 97 | set_nodelay(int fd) |
| 98 | { |
| 99 | int on = 1; |
| 100 | |
| 101 | debug("fd %d setting TCP_NODELAY", fd); |
| 102 | if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof on) == -1) |
| 103 | error("setsockopt TCP_NODELAY: %.100s", strerror(errno)); |
| 104 | } |
| 105 | |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 106 | /* Characters considered whitespace in strsep calls. */ |
| 107 | #define WHITESPACE " \t\r\n" |
| 108 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 109 | /* return next token in configuration line */ |
Damien Miller | 61c5150 | 2000-08-18 14:01:04 +1000 | [diff] [blame] | 110 | char * |
| 111 | strdelim(char **s) |
| 112 | { |
| 113 | char *old; |
| 114 | int wspace = 0; |
| 115 | |
| 116 | if (*s == NULL) |
| 117 | return NULL; |
| 118 | |
| 119 | old = *s; |
| 120 | |
| 121 | *s = strpbrk(*s, WHITESPACE "="); |
| 122 | if (*s == NULL) |
| 123 | return (old); |
| 124 | |
| 125 | /* Allow only one '=' to be skipped */ |
| 126 | if (*s[0] == '=') |
| 127 | wspace = 1; |
| 128 | *s[0] = '\0'; |
| 129 | |
| 130 | *s += strspn(*s + 1, WHITESPACE) + 1; |
| 131 | if (*s[0] == '=' && !wspace) |
| 132 | *s += strspn(*s + 1, WHITESPACE) + 1; |
| 133 | |
| 134 | return (old); |
| 135 | } |
Kevin Steves | b6e773a | 2001-02-04 13:20:36 +0000 | [diff] [blame] | 136 | |
Ben Lindstrom | 086cf21 | 2001-03-05 05:56:40 +0000 | [diff] [blame] | 137 | struct passwd * |
| 138 | pwcopy(struct passwd *pw) |
| 139 | { |
| 140 | struct passwd *copy = xmalloc(sizeof(*copy)); |
Ben Lindstrom | 4030442 | 2001-03-05 06:22:01 +0000 | [diff] [blame] | 141 | |
Ben Lindstrom | 086cf21 | 2001-03-05 05:56:40 +0000 | [diff] [blame] | 142 | memset(copy, 0, sizeof(*copy)); |
| 143 | copy->pw_name = xstrdup(pw->pw_name); |
| 144 | copy->pw_passwd = xstrdup(pw->pw_passwd); |
Ben Lindstrom | 4030442 | 2001-03-05 06:22:01 +0000 | [diff] [blame] | 145 | copy->pw_gecos = xstrdup(pw->pw_gecos); |
Ben Lindstrom | 086cf21 | 2001-03-05 05:56:40 +0000 | [diff] [blame] | 146 | copy->pw_uid = pw->pw_uid; |
| 147 | copy->pw_gid = pw->pw_gid; |
Kevin Steves | 8245695 | 2001-06-22 21:14:18 +0000 | [diff] [blame] | 148 | #ifdef HAVE_PW_EXPIRE_IN_PASSWD |
Ben Lindstrom | 3af4d46 | 2001-06-21 03:11:27 +0000 | [diff] [blame] | 149 | copy->pw_expire = pw->pw_expire; |
Kevin Steves | 8245695 | 2001-06-22 21:14:18 +0000 | [diff] [blame] | 150 | #endif |
| 151 | #ifdef HAVE_PW_CHANGE_IN_PASSWD |
Ben Lindstrom | 3af4d46 | 2001-06-21 03:11:27 +0000 | [diff] [blame] | 152 | copy->pw_change = pw->pw_change; |
Kevin Steves | 8245695 | 2001-06-22 21:14:18 +0000 | [diff] [blame] | 153 | #endif |
Ben Lindstrom | 0f68db4 | 2001-03-05 07:57:09 +0000 | [diff] [blame] | 154 | #ifdef HAVE_PW_CLASS_IN_PASSWD |
Ben Lindstrom | 086cf21 | 2001-03-05 05:56:40 +0000 | [diff] [blame] | 155 | copy->pw_class = xstrdup(pw->pw_class); |
Ben Lindstrom | 0f68db4 | 2001-03-05 07:57:09 +0000 | [diff] [blame] | 156 | #endif |
Ben Lindstrom | 086cf21 | 2001-03-05 05:56:40 +0000 | [diff] [blame] | 157 | copy->pw_dir = xstrdup(pw->pw_dir); |
| 158 | copy->pw_shell = xstrdup(pw->pw_shell); |
| 159 | return copy; |
| 160 | } |
| 161 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 162 | /* |
| 163 | * Convert ASCII string to TCP/IP port number. |
| 164 | * Port must be >0 and <=65535. |
| 165 | * Return 0 if invalid. |
| 166 | */ |
| 167 | int |
| 168 | a2port(const char *s) |
Ben Lindstrom | 19066a1 | 2001-04-12 23:39:26 +0000 | [diff] [blame] | 169 | { |
| 170 | long port; |
| 171 | char *endp; |
| 172 | |
| 173 | errno = 0; |
| 174 | port = strtol(s, &endp, 0); |
| 175 | if (s == endp || *endp != '\0' || |
| 176 | (errno == ERANGE && (port == LONG_MIN || port == LONG_MAX)) || |
| 177 | port <= 0 || port > 65535) |
| 178 | return 0; |
| 179 | |
| 180 | return port; |
| 181 | } |
| 182 | |
Ben Lindstrom | 1bda4c8 | 2001-06-05 19:59:08 +0000 | [diff] [blame] | 183 | #define SECONDS 1 |
| 184 | #define MINUTES (SECONDS * 60) |
| 185 | #define HOURS (MINUTES * 60) |
| 186 | #define DAYS (HOURS * 24) |
| 187 | #define WEEKS (DAYS * 7) |
| 188 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 189 | /* |
| 190 | * Convert a time string into seconds; format is |
| 191 | * a sequence of: |
| 192 | * time[qualifier] |
| 193 | * |
| 194 | * Valid time qualifiers are: |
| 195 | * <none> seconds |
| 196 | * s|S seconds |
| 197 | * m|M minutes |
| 198 | * h|H hours |
| 199 | * d|D days |
| 200 | * w|W weeks |
| 201 | * |
| 202 | * Examples: |
| 203 | * 90m 90 minutes |
| 204 | * 1h30m 90 minutes |
| 205 | * 2d 2 days |
| 206 | * 1w 1 week |
| 207 | * |
| 208 | * Return -1 if time string is invalid. |
| 209 | */ |
| 210 | long |
| 211 | convtime(const char *s) |
Ben Lindstrom | 1bda4c8 | 2001-06-05 19:59:08 +0000 | [diff] [blame] | 212 | { |
| 213 | long total, secs; |
| 214 | const char *p; |
| 215 | char *endp; |
| 216 | |
| 217 | errno = 0; |
| 218 | total = 0; |
| 219 | p = s; |
| 220 | |
| 221 | if (p == NULL || *p == '\0') |
| 222 | return -1; |
| 223 | |
| 224 | while (*p) { |
| 225 | secs = strtol(p, &endp, 10); |
| 226 | if (p == endp || |
| 227 | (errno == ERANGE && (secs == LONG_MIN || secs == LONG_MAX)) || |
| 228 | secs < 0) |
| 229 | return -1; |
| 230 | |
| 231 | switch (*endp++) { |
| 232 | case '\0': |
| 233 | endp--; |
| 234 | case 's': |
| 235 | case 'S': |
| 236 | break; |
| 237 | case 'm': |
| 238 | case 'M': |
| 239 | secs *= MINUTES; |
| 240 | break; |
| 241 | case 'h': |
| 242 | case 'H': |
| 243 | secs *= HOURS; |
| 244 | break; |
| 245 | case 'd': |
| 246 | case 'D': |
| 247 | secs *= DAYS; |
| 248 | break; |
| 249 | case 'w': |
| 250 | case 'W': |
| 251 | secs *= WEEKS; |
| 252 | break; |
| 253 | default: |
| 254 | return -1; |
| 255 | } |
| 256 | total += secs; |
| 257 | if (total < 0) |
| 258 | return -1; |
| 259 | p = endp; |
| 260 | } |
| 261 | |
| 262 | return total; |
| 263 | } |
| 264 | |
Ben Lindstrom | 4529b70 | 2001-05-03 23:39:53 +0000 | [diff] [blame] | 265 | char * |
| 266 | cleanhostname(char *host) |
| 267 | { |
| 268 | if (*host == '[' && host[strlen(host) - 1] == ']') { |
| 269 | host[strlen(host) - 1] = '\0'; |
| 270 | return (host + 1); |
| 271 | } else |
| 272 | return host; |
| 273 | } |
| 274 | |
| 275 | char * |
| 276 | colon(char *cp) |
| 277 | { |
| 278 | int flag = 0; |
| 279 | |
| 280 | if (*cp == ':') /* Leading colon is part of file name. */ |
| 281 | return (0); |
| 282 | if (*cp == '[') |
| 283 | flag = 1; |
| 284 | |
| 285 | for (; *cp; ++cp) { |
| 286 | if (*cp == '@' && *(cp+1) == '[') |
| 287 | flag = 1; |
| 288 | if (*cp == ']' && *(cp+1) == ':' && flag) |
| 289 | return (cp+1); |
| 290 | if (*cp == ':' && !flag) |
| 291 | return (cp); |
| 292 | if (*cp == '/') |
| 293 | return (0); |
| 294 | } |
| 295 | return (0); |
| 296 | } |
| 297 | |
Ben Lindstrom | 4cc240d | 2001-07-04 04:46:56 +0000 | [diff] [blame] | 298 | /* function to assist building execv() arguments */ |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 299 | void |
| 300 | addargs(arglist *args, char *fmt, ...) |
| 301 | { |
| 302 | va_list ap; |
| 303 | char buf[1024]; |
| 304 | |
| 305 | va_start(ap, fmt); |
| 306 | vsnprintf(buf, sizeof(buf), fmt, ap); |
| 307 | va_end(ap); |
| 308 | |
| 309 | if (args->list == NULL) { |
| 310 | args->nalloc = 32; |
| 311 | args->num = 0; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 312 | } else if (args->num+2 >= args->nalloc) |
Ben Lindstrom | 387c472 | 2001-05-08 20:27:25 +0000 | [diff] [blame] | 313 | args->nalloc *= 2; |
| 314 | |
| 315 | args->list = xrealloc(args->list, args->nalloc * sizeof(char *)); |
| 316 | args->list[args->num++] = xstrdup(buf); |
| 317 | args->list[args->num] = NULL; |
| 318 | } |
| 319 | |
Kevin Steves | b6e773a | 2001-02-04 13:20:36 +0000 | [diff] [blame] | 320 | mysig_t |
| 321 | mysignal(int sig, mysig_t act) |
| 322 | { |
| 323 | #ifdef HAVE_SIGACTION |
| 324 | struct sigaction sa, osa; |
| 325 | |
Kevin Steves | e74ebd0 | 2001-02-17 17:10:16 +0000 | [diff] [blame] | 326 | if (sigaction(sig, NULL, &osa) == -1) |
Kevin Steves | b6e773a | 2001-02-04 13:20:36 +0000 | [diff] [blame] | 327 | return (mysig_t) -1; |
| 328 | if (osa.sa_handler != act) { |
Kevin Steves | e74ebd0 | 2001-02-17 17:10:16 +0000 | [diff] [blame] | 329 | memset(&sa, 0, sizeof(sa)); |
Kevin Steves | b6e773a | 2001-02-04 13:20:36 +0000 | [diff] [blame] | 330 | sigemptyset(&sa.sa_mask); |
| 331 | sa.sa_flags = 0; |
Damien Miller | 722ccb1 | 2001-02-18 15:18:43 +1100 | [diff] [blame] | 332 | #if defined(SA_INTERRUPT) |
| 333 | if (sig == SIGALRM) |
Damien Miller | 0318e2e | 2001-02-18 13:04:23 +1100 | [diff] [blame] | 334 | sa.sa_flags |= SA_INTERRUPT; |
| 335 | #endif |
Kevin Steves | b6e773a | 2001-02-04 13:20:36 +0000 | [diff] [blame] | 336 | sa.sa_handler = act; |
Kevin Steves | e74ebd0 | 2001-02-17 17:10:16 +0000 | [diff] [blame] | 337 | if (sigaction(sig, &sa, NULL) == -1) |
Kevin Steves | b6e773a | 2001-02-04 13:20:36 +0000 | [diff] [blame] | 338 | return (mysig_t) -1; |
| 339 | } |
| 340 | return (osa.sa_handler); |
| 341 | #else |
| 342 | return (signal(sig, act)); |
| 343 | #endif |
| 344 | } |