10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * Copyright (c) 2000 Markus Friedl. All rights reserved. 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 50Sstevel@tonic-gate * modification, are permitted provided that the following conditions 60Sstevel@tonic-gate * are met: 70Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright 80Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 90Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 100Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 110Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 140Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 150Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 160Sstevel@tonic-gate * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 170Sstevel@tonic-gate * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 180Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 190Sstevel@tonic-gate * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 200Sstevel@tonic-gate * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 210Sstevel@tonic-gate * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 220Sstevel@tonic-gate * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 230Sstevel@tonic-gate */ 240Sstevel@tonic-gate /* 25*2780Sjp161948 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 260Sstevel@tonic-gate * Use is subject to license terms. 270Sstevel@tonic-gate */ 280Sstevel@tonic-gate 290Sstevel@tonic-gate #include "includes.h" 300Sstevel@tonic-gate RCSID("$OpenBSD: misc.c,v 1.19 2002/03/04 17:27:39 stevesk Exp $"); 310Sstevel@tonic-gate 320Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 330Sstevel@tonic-gate 340Sstevel@tonic-gate #include "misc.h" 350Sstevel@tonic-gate #include "log.h" 360Sstevel@tonic-gate #include "xmalloc.h" 370Sstevel@tonic-gate 380Sstevel@tonic-gate /* remove newline at end of string */ 390Sstevel@tonic-gate char * 400Sstevel@tonic-gate chop(char *s) 410Sstevel@tonic-gate { 420Sstevel@tonic-gate char *t = s; 430Sstevel@tonic-gate while (*t) { 440Sstevel@tonic-gate if (*t == '\n' || *t == '\r') { 450Sstevel@tonic-gate *t = '\0'; 460Sstevel@tonic-gate return s; 470Sstevel@tonic-gate } 480Sstevel@tonic-gate t++; 490Sstevel@tonic-gate } 500Sstevel@tonic-gate return s; 510Sstevel@tonic-gate 520Sstevel@tonic-gate } 530Sstevel@tonic-gate 540Sstevel@tonic-gate /* set/unset filedescriptor to non-blocking */ 550Sstevel@tonic-gate void 560Sstevel@tonic-gate set_nonblock(int fd) 570Sstevel@tonic-gate { 580Sstevel@tonic-gate int val; 590Sstevel@tonic-gate 600Sstevel@tonic-gate val = fcntl(fd, F_GETFL, 0); 610Sstevel@tonic-gate if (val < 0) { 620Sstevel@tonic-gate error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno)); 630Sstevel@tonic-gate return; 640Sstevel@tonic-gate } 650Sstevel@tonic-gate if (val & O_NONBLOCK) { 660Sstevel@tonic-gate debug2("fd %d is O_NONBLOCK", fd); 670Sstevel@tonic-gate return; 680Sstevel@tonic-gate } 690Sstevel@tonic-gate debug("fd %d setting O_NONBLOCK", fd); 700Sstevel@tonic-gate val |= O_NONBLOCK; 710Sstevel@tonic-gate if (fcntl(fd, F_SETFL, val) == -1) 720Sstevel@tonic-gate debug("fcntl(%d, F_SETFL, O_NONBLOCK): %s", 730Sstevel@tonic-gate fd, strerror(errno)); 740Sstevel@tonic-gate } 750Sstevel@tonic-gate 760Sstevel@tonic-gate void 770Sstevel@tonic-gate unset_nonblock(int fd) 780Sstevel@tonic-gate { 790Sstevel@tonic-gate int val; 800Sstevel@tonic-gate 810Sstevel@tonic-gate val = fcntl(fd, F_GETFL, 0); 820Sstevel@tonic-gate if (val < 0) { 830Sstevel@tonic-gate error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno)); 840Sstevel@tonic-gate return; 850Sstevel@tonic-gate } 860Sstevel@tonic-gate if (!(val & O_NONBLOCK)) { 870Sstevel@tonic-gate debug2("fd %d is not O_NONBLOCK", fd); 880Sstevel@tonic-gate return; 890Sstevel@tonic-gate } 900Sstevel@tonic-gate debug("fd %d clearing O_NONBLOCK", fd); 910Sstevel@tonic-gate val &= ~O_NONBLOCK; 920Sstevel@tonic-gate if (fcntl(fd, F_SETFL, val) == -1) 930Sstevel@tonic-gate debug("fcntl(%d, F_SETFL, O_NONBLOCK): %s", 940Sstevel@tonic-gate fd, strerror(errno)); 950Sstevel@tonic-gate } 960Sstevel@tonic-gate 970Sstevel@tonic-gate /* disable nagle on socket */ 980Sstevel@tonic-gate void 990Sstevel@tonic-gate set_nodelay(int fd) 1000Sstevel@tonic-gate { 1010Sstevel@tonic-gate int opt; 1020Sstevel@tonic-gate socklen_t optlen; 1030Sstevel@tonic-gate 1040Sstevel@tonic-gate optlen = sizeof opt; 1050Sstevel@tonic-gate if (getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, &optlen) == -1) { 1060Sstevel@tonic-gate error("getsockopt TCP_NODELAY: %.100s", strerror(errno)); 1070Sstevel@tonic-gate return; 1080Sstevel@tonic-gate } 1090Sstevel@tonic-gate if (opt == 1) { 1100Sstevel@tonic-gate debug2("fd %d is TCP_NODELAY", fd); 1110Sstevel@tonic-gate return; 1120Sstevel@tonic-gate } 1130Sstevel@tonic-gate opt = 1; 1140Sstevel@tonic-gate debug("fd %d setting TCP_NODELAY", fd); 1150Sstevel@tonic-gate if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1) 1160Sstevel@tonic-gate error("setsockopt TCP_NODELAY: %.100s", strerror(errno)); 1170Sstevel@tonic-gate } 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate /* Characters considered whitespace in strsep calls. */ 1200Sstevel@tonic-gate #define WHITESPACE " \t\r\n" 1210Sstevel@tonic-gate 1220Sstevel@tonic-gate /* return next token in configuration line */ 1230Sstevel@tonic-gate char * 1240Sstevel@tonic-gate strdelim(char **s) 1250Sstevel@tonic-gate { 1260Sstevel@tonic-gate char *old; 1270Sstevel@tonic-gate int wspace = 0; 1280Sstevel@tonic-gate 1290Sstevel@tonic-gate if (*s == NULL) 1300Sstevel@tonic-gate return NULL; 1310Sstevel@tonic-gate 1320Sstevel@tonic-gate old = *s; 1330Sstevel@tonic-gate 1340Sstevel@tonic-gate *s = strpbrk(*s, WHITESPACE "="); 1350Sstevel@tonic-gate if (*s == NULL) 1360Sstevel@tonic-gate return (old); 1370Sstevel@tonic-gate 1380Sstevel@tonic-gate /* Allow only one '=' to be skipped */ 1390Sstevel@tonic-gate if (*s[0] == '=') 1400Sstevel@tonic-gate wspace = 1; 1410Sstevel@tonic-gate *s[0] = '\0'; 1420Sstevel@tonic-gate 1430Sstevel@tonic-gate *s += strspn(*s + 1, WHITESPACE) + 1; 1440Sstevel@tonic-gate if (*s[0] == '=' && !wspace) 1450Sstevel@tonic-gate *s += strspn(*s + 1, WHITESPACE) + 1; 1460Sstevel@tonic-gate 1470Sstevel@tonic-gate return (old); 1480Sstevel@tonic-gate } 1490Sstevel@tonic-gate 1500Sstevel@tonic-gate struct passwd * 1510Sstevel@tonic-gate pwcopy(struct passwd *pw) 1520Sstevel@tonic-gate { 1530Sstevel@tonic-gate struct passwd *copy = xmalloc(sizeof(*copy)); 1540Sstevel@tonic-gate 1550Sstevel@tonic-gate memset(copy, 0, sizeof(*copy)); 1560Sstevel@tonic-gate copy->pw_name = xstrdup(pw->pw_name); 1570Sstevel@tonic-gate copy->pw_passwd = xstrdup(pw->pw_passwd); 1580Sstevel@tonic-gate copy->pw_gecos = xstrdup(pw->pw_gecos); 1590Sstevel@tonic-gate copy->pw_uid = pw->pw_uid; 1600Sstevel@tonic-gate copy->pw_gid = pw->pw_gid; 1610Sstevel@tonic-gate #ifdef HAVE_PW_EXPIRE_IN_PASSWD 1620Sstevel@tonic-gate copy->pw_expire = pw->pw_expire; 1630Sstevel@tonic-gate #endif 1640Sstevel@tonic-gate #ifdef HAVE_PW_CHANGE_IN_PASSWD 1650Sstevel@tonic-gate copy->pw_change = pw->pw_change; 1660Sstevel@tonic-gate #endif 1670Sstevel@tonic-gate #ifdef HAVE_PW_CLASS_IN_PASSWD 1680Sstevel@tonic-gate copy->pw_class = xstrdup(pw->pw_class); 1690Sstevel@tonic-gate #endif 1700Sstevel@tonic-gate copy->pw_dir = xstrdup(pw->pw_dir); 1710Sstevel@tonic-gate copy->pw_shell = xstrdup(pw->pw_shell); 1720Sstevel@tonic-gate return copy; 1730Sstevel@tonic-gate } 1740Sstevel@tonic-gate 1750Sstevel@tonic-gate void 1760Sstevel@tonic-gate pwfree(struct passwd **pw) 1770Sstevel@tonic-gate { 1780Sstevel@tonic-gate struct passwd *p; 1790Sstevel@tonic-gate 1800Sstevel@tonic-gate if (pw == NULL || *pw == NULL) 1810Sstevel@tonic-gate return; 1820Sstevel@tonic-gate 1830Sstevel@tonic-gate p = *pw; 1840Sstevel@tonic-gate *pw = NULL; 1850Sstevel@tonic-gate 1860Sstevel@tonic-gate xfree(p->pw_name); 1870Sstevel@tonic-gate xfree(p->pw_passwd); 1880Sstevel@tonic-gate xfree(p->pw_gecos); 1890Sstevel@tonic-gate #ifdef HAVE_PW_CLASS_IN_PASSWD 1900Sstevel@tonic-gate xfree(p->pw_class); 1910Sstevel@tonic-gate #endif 1920Sstevel@tonic-gate xfree(p->pw_dir); 1930Sstevel@tonic-gate xfree(p->pw_shell); 1940Sstevel@tonic-gate xfree(p); 1950Sstevel@tonic-gate } 1960Sstevel@tonic-gate 1970Sstevel@tonic-gate /* 1980Sstevel@tonic-gate * Convert ASCII string to TCP/IP port number. 1990Sstevel@tonic-gate * Port must be >0 and <=65535. 2000Sstevel@tonic-gate * Return 0 if invalid. 2010Sstevel@tonic-gate */ 2020Sstevel@tonic-gate int 2030Sstevel@tonic-gate a2port(const char *s) 2040Sstevel@tonic-gate { 2050Sstevel@tonic-gate long port; 2060Sstevel@tonic-gate char *endp; 2070Sstevel@tonic-gate 2080Sstevel@tonic-gate errno = 0; 2090Sstevel@tonic-gate port = strtol(s, &endp, 0); 2100Sstevel@tonic-gate if (s == endp || *endp != '\0' || 2110Sstevel@tonic-gate (errno == ERANGE && (port == LONG_MIN || port == LONG_MAX)) || 2120Sstevel@tonic-gate port <= 0 || port > 65535) 2130Sstevel@tonic-gate return 0; 2140Sstevel@tonic-gate 2150Sstevel@tonic-gate return port; 2160Sstevel@tonic-gate } 2170Sstevel@tonic-gate 2180Sstevel@tonic-gate #define SECONDS 1 2190Sstevel@tonic-gate #define MINUTES (SECONDS * 60) 2200Sstevel@tonic-gate #define HOURS (MINUTES * 60) 2210Sstevel@tonic-gate #define DAYS (HOURS * 24) 2220Sstevel@tonic-gate #define WEEKS (DAYS * 7) 2230Sstevel@tonic-gate 2240Sstevel@tonic-gate /* 2250Sstevel@tonic-gate * Convert a time string into seconds; format is 2260Sstevel@tonic-gate * a sequence of: 2270Sstevel@tonic-gate * time[qualifier] 2280Sstevel@tonic-gate * 2290Sstevel@tonic-gate * Valid time qualifiers are: 2300Sstevel@tonic-gate * <none> seconds 2310Sstevel@tonic-gate * s|S seconds 2320Sstevel@tonic-gate * m|M minutes 2330Sstevel@tonic-gate * h|H hours 2340Sstevel@tonic-gate * d|D days 2350Sstevel@tonic-gate * w|W weeks 2360Sstevel@tonic-gate * 2370Sstevel@tonic-gate * Examples: 2380Sstevel@tonic-gate * 90m 90 minutes 2390Sstevel@tonic-gate * 1h30m 90 minutes 2400Sstevel@tonic-gate * 2d 2 days 2410Sstevel@tonic-gate * 1w 1 week 2420Sstevel@tonic-gate * 2430Sstevel@tonic-gate * Return -1 if time string is invalid. 2440Sstevel@tonic-gate */ 2450Sstevel@tonic-gate long 2460Sstevel@tonic-gate convtime(const char *s) 2470Sstevel@tonic-gate { 2480Sstevel@tonic-gate long total, secs; 2490Sstevel@tonic-gate const char *p; 2500Sstevel@tonic-gate char *endp; 2510Sstevel@tonic-gate 2520Sstevel@tonic-gate errno = 0; 2530Sstevel@tonic-gate total = 0; 2540Sstevel@tonic-gate p = s; 2550Sstevel@tonic-gate 2560Sstevel@tonic-gate if (p == NULL || *p == '\0') 2570Sstevel@tonic-gate return -1; 2580Sstevel@tonic-gate 2590Sstevel@tonic-gate while (*p) { 2600Sstevel@tonic-gate secs = strtol(p, &endp, 10); 2610Sstevel@tonic-gate if (p == endp || 2620Sstevel@tonic-gate (errno == ERANGE && (secs == LONG_MIN || secs == LONG_MAX)) || 2630Sstevel@tonic-gate secs < 0) 2640Sstevel@tonic-gate return -1; 2650Sstevel@tonic-gate 2660Sstevel@tonic-gate switch (*endp++) { 2670Sstevel@tonic-gate case '\0': 2680Sstevel@tonic-gate endp--; 2690Sstevel@tonic-gate break; 2700Sstevel@tonic-gate case 's': 2710Sstevel@tonic-gate case 'S': 2720Sstevel@tonic-gate break; 2730Sstevel@tonic-gate case 'm': 2740Sstevel@tonic-gate case 'M': 2750Sstevel@tonic-gate secs *= MINUTES; 2760Sstevel@tonic-gate break; 2770Sstevel@tonic-gate case 'h': 2780Sstevel@tonic-gate case 'H': 2790Sstevel@tonic-gate secs *= HOURS; 2800Sstevel@tonic-gate break; 2810Sstevel@tonic-gate case 'd': 2820Sstevel@tonic-gate case 'D': 2830Sstevel@tonic-gate secs *= DAYS; 2840Sstevel@tonic-gate break; 2850Sstevel@tonic-gate case 'w': 2860Sstevel@tonic-gate case 'W': 2870Sstevel@tonic-gate secs *= WEEKS; 2880Sstevel@tonic-gate break; 2890Sstevel@tonic-gate default: 2900Sstevel@tonic-gate return -1; 2910Sstevel@tonic-gate } 2920Sstevel@tonic-gate total += secs; 2930Sstevel@tonic-gate if (total < 0) 2940Sstevel@tonic-gate return -1; 2950Sstevel@tonic-gate p = endp; 2960Sstevel@tonic-gate } 2970Sstevel@tonic-gate 2980Sstevel@tonic-gate return total; 2990Sstevel@tonic-gate } 3000Sstevel@tonic-gate 3010Sstevel@tonic-gate char * 3020Sstevel@tonic-gate cleanhostname(char *host) 3030Sstevel@tonic-gate { 3040Sstevel@tonic-gate if (*host == '[' && host[strlen(host) - 1] == ']') { 3050Sstevel@tonic-gate host[strlen(host) - 1] = '\0'; 3060Sstevel@tonic-gate return (host + 1); 3070Sstevel@tonic-gate } else 3080Sstevel@tonic-gate return host; 3090Sstevel@tonic-gate } 3100Sstevel@tonic-gate 3110Sstevel@tonic-gate char * 3120Sstevel@tonic-gate colon(char *cp) 3130Sstevel@tonic-gate { 3140Sstevel@tonic-gate int flag = 0; 3150Sstevel@tonic-gate 3160Sstevel@tonic-gate if (*cp == ':') /* Leading colon is part of file name. */ 3170Sstevel@tonic-gate return (0); 3180Sstevel@tonic-gate if (*cp == '[') 3190Sstevel@tonic-gate flag = 1; 3200Sstevel@tonic-gate 3210Sstevel@tonic-gate for (; *cp; ++cp) { 3220Sstevel@tonic-gate if (*cp == '@' && *(cp+1) == '[') 3230Sstevel@tonic-gate flag = 1; 3240Sstevel@tonic-gate if (*cp == ']' && *(cp+1) == ':' && flag) 3250Sstevel@tonic-gate return (cp+1); 3260Sstevel@tonic-gate if (*cp == ':' && !flag) 3270Sstevel@tonic-gate return (cp); 3280Sstevel@tonic-gate if (*cp == '/') 3290Sstevel@tonic-gate return (0); 3300Sstevel@tonic-gate } 3310Sstevel@tonic-gate return (0); 3320Sstevel@tonic-gate } 3330Sstevel@tonic-gate 3340Sstevel@tonic-gate /* function to assist building execv() arguments */ 3350Sstevel@tonic-gate /* PRINTFLIKE2 */ 3360Sstevel@tonic-gate void 3370Sstevel@tonic-gate addargs(arglist *args, char *fmt, ...) 3380Sstevel@tonic-gate { 3390Sstevel@tonic-gate va_list ap; 3400Sstevel@tonic-gate char buf[1024]; 3410Sstevel@tonic-gate 3420Sstevel@tonic-gate va_start(ap, fmt); 3430Sstevel@tonic-gate vsnprintf(buf, sizeof(buf), fmt, ap); 3440Sstevel@tonic-gate va_end(ap); 3450Sstevel@tonic-gate 3460Sstevel@tonic-gate if (args->list == NULL) { 3470Sstevel@tonic-gate args->nalloc = 32; 3480Sstevel@tonic-gate args->num = 0; 3490Sstevel@tonic-gate } else if (args->num+2 >= args->nalloc) 3500Sstevel@tonic-gate args->nalloc *= 2; 3510Sstevel@tonic-gate 3520Sstevel@tonic-gate args->list = xrealloc(args->list, args->nalloc * sizeof(char *)); 3530Sstevel@tonic-gate args->list[args->num++] = xstrdup(buf); 3540Sstevel@tonic-gate args->list[args->num] = NULL; 3550Sstevel@tonic-gate } 3560Sstevel@tonic-gate 357*2780Sjp161948 void 358*2780Sjp161948 freeargs(arglist *args) 359*2780Sjp161948 { 360*2780Sjp161948 u_int i; 361*2780Sjp161948 362*2780Sjp161948 if (args->list != NULL) { 363*2780Sjp161948 for (i = 0; i < args->num; i++) 364*2780Sjp161948 xfree(args->list[i]); 365*2780Sjp161948 xfree(args->list); 366*2780Sjp161948 args->nalloc = args->num = 0; 367*2780Sjp161948 args->list = NULL; 368*2780Sjp161948 } 369*2780Sjp161948 } 370*2780Sjp161948 3710Sstevel@tonic-gate mysig_t 3720Sstevel@tonic-gate mysignal(int sig, mysig_t act) 3730Sstevel@tonic-gate { 3740Sstevel@tonic-gate #ifdef HAVE_SIGACTION 3750Sstevel@tonic-gate struct sigaction sa, osa; 3760Sstevel@tonic-gate 3770Sstevel@tonic-gate if (sigaction(sig, NULL, &osa) == -1) 3780Sstevel@tonic-gate return (mysig_t) -1; 3790Sstevel@tonic-gate if (osa.sa_handler != act) { 3800Sstevel@tonic-gate memset(&sa, 0, sizeof(sa)); 3810Sstevel@tonic-gate sigemptyset(&sa.sa_mask); 3820Sstevel@tonic-gate sa.sa_flags = 0; 3830Sstevel@tonic-gate #if defined(SA_INTERRUPT) 3840Sstevel@tonic-gate if (sig == SIGALRM) 3850Sstevel@tonic-gate sa.sa_flags |= SA_INTERRUPT; 3860Sstevel@tonic-gate #endif 3870Sstevel@tonic-gate sa.sa_handler = act; 3880Sstevel@tonic-gate if (sigaction(sig, &sa, NULL) == -1) 3890Sstevel@tonic-gate return (mysig_t) -1; 3900Sstevel@tonic-gate } 3910Sstevel@tonic-gate return (osa.sa_handler); 3920Sstevel@tonic-gate #else 3930Sstevel@tonic-gate return (signal(sig, act)); 3940Sstevel@tonic-gate #endif 3950Sstevel@tonic-gate } 396