[BACK]Return to misc.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/misc.c, Revision 1.25.2.1

1.1       markus      1: /*
                      2:  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
                      3:  *
                      4:  * Redistribution and use in source and binary forms, with or without
                      5:  * modification, are permitted provided that the following conditions
                      6:  * are met:
                      7:  * 1. Redistributions of source code must retain the above copyright
                      8:  *    notice, this list of conditions and the following disclaimer.
                      9:  * 2. Redistributions in binary form must reproduce the above copyright
                     10:  *    notice, this list of conditions and the following disclaimer in the
                     11:  *    documentation and/or other materials provided with the distribution.
                     12:  *
                     13:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     14:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     15:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     16:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     17:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     18:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     19:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     20:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     21:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     22:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     23:  */
                     24:
                     25: #include "includes.h"
1.25.2.1! brad       26: RCSID("$OpenBSD: misc.c,v 1.28 2005/03/01 10:09:52 djm Exp $");
1.1       markus     27:
                     28: #include "misc.h"
                     29: #include "log.h"
1.3       deraadt    30: #include "xmalloc.h"
1.1       markus     31:
1.12      markus     32: /* remove newline at end of string */
1.1       markus     33: char *
                     34: chop(char *s)
                     35: {
                     36:        char *t = s;
                     37:        while (*t) {
1.13      deraadt    38:                if (*t == '\n' || *t == '\r') {
1.1       markus     39:                        *t = '\0';
                     40:                        return s;
                     41:                }
                     42:                t++;
                     43:        }
                     44:        return s;
                     45:
                     46: }
                     47:
1.12      markus     48: /* set/unset filedescriptor to non-blocking */
1.24      djm        49: int
1.1       markus     50: set_nonblock(int fd)
                     51: {
                     52:        int val;
1.8       markus     53:
1.1       markus     54:        val = fcntl(fd, F_GETFL, 0);
                     55:        if (val < 0) {
                     56:                error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno));
1.24      djm        57:                return (-1);
1.1       markus     58:        }
                     59:        if (val & O_NONBLOCK) {
1.24      djm        60:                debug3("fd %d is O_NONBLOCK", fd);
                     61:                return (0);
1.1       markus     62:        }
1.21      markus     63:        debug2("fd %d setting O_NONBLOCK", fd);
1.1       markus     64:        val |= O_NONBLOCK;
1.24      djm        65:        if (fcntl(fd, F_SETFL, val) == -1) {
                     66:                debug("fcntl(%d, F_SETFL, O_NONBLOCK): %s", fd,
                     67:                    strerror(errno));
                     68:                return (-1);
                     69:        }
                     70:        return (0);
1.8       markus     71: }
                     72:
1.24      djm        73: int
1.8       markus     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));
1.24      djm        81:                return (-1);
1.8       markus     82:        }
                     83:        if (!(val & O_NONBLOCK)) {
1.24      djm        84:                debug3("fd %d is not O_NONBLOCK", fd);
                     85:                return (0);
1.8       markus     86:        }
1.10      markus     87:        debug("fd %d clearing O_NONBLOCK", fd);
1.8       markus     88:        val &= ~O_NONBLOCK;
1.24      djm        89:        if (fcntl(fd, F_SETFL, val) == -1) {
                     90:                debug("fcntl(%d, F_SETFL, ~O_NONBLOCK): %s",
1.18      markus     91:                    fd, strerror(errno));
1.24      djm        92:                return (-1);
                     93:        }
                     94:        return (0);
1.15      stevesk    95: }
                     96:
                     97: /* disable nagle on socket */
                     98: void
                     99: set_nodelay(int fd)
                    100: {
1.17      stevesk   101:        int opt;
                    102:        socklen_t optlen;
1.15      stevesk   103:
1.16      stevesk   104:        optlen = sizeof opt;
                    105:        if (getsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, &optlen) == -1) {
1.23      markus    106:                debug("getsockopt TCP_NODELAY: %.100s", strerror(errno));
1.16      stevesk   107:                return;
                    108:        }
                    109:        if (opt == 1) {
                    110:                debug2("fd %d is TCP_NODELAY", fd);
                    111:                return;
                    112:        }
                    113:        opt = 1;
1.20      markus    114:        debug2("fd %d setting TCP_NODELAY", fd);
1.16      stevesk   115:        if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &opt, sizeof opt) == -1)
1.15      stevesk   116:                error("setsockopt TCP_NODELAY: %.100s", strerror(errno));
1.1       markus    117: }
                    118:
                    119: /* Characters considered whitespace in strsep calls. */
                    120: #define WHITESPACE " \t\r\n"
                    121:
1.12      markus    122: /* return next token in configuration line */
1.1       markus    123: char *
                    124: strdelim(char **s)
                    125: {
                    126:        char *old;
                    127:        int wspace = 0;
                    128:
                    129:        if (*s == NULL)
                    130:                return NULL;
                    131:
                    132:        old = *s;
                    133:
                    134:        *s = strpbrk(*s, WHITESPACE "=");
                    135:        if (*s == NULL)
                    136:                return (old);
                    137:
                    138:        /* Allow only one '=' to be skipped */
                    139:        if (*s[0] == '=')
                    140:                wspace = 1;
                    141:        *s[0] = '\0';
                    142:
                    143:        *s += strspn(*s + 1, WHITESPACE) + 1;
                    144:        if (*s[0] == '=' && !wspace)
                    145:                *s += strspn(*s + 1, WHITESPACE) + 1;
                    146:
                    147:        return (old);
1.2       markus    148: }
                    149:
                    150: struct passwd *
                    151: pwcopy(struct passwd *pw)
                    152: {
                    153:        struct passwd *copy = xmalloc(sizeof(*copy));
1.4       deraadt   154:
1.2       markus    155:        memset(copy, 0, sizeof(*copy));
                    156:        copy->pw_name = xstrdup(pw->pw_name);
                    157:        copy->pw_passwd = xstrdup(pw->pw_passwd);
1.4       deraadt   158:        copy->pw_gecos = xstrdup(pw->pw_gecos);
1.2       markus    159:        copy->pw_uid = pw->pw_uid;
                    160:        copy->pw_gid = pw->pw_gid;
1.11      markus    161:        copy->pw_expire = pw->pw_expire;
                    162:        copy->pw_change = pw->pw_change;
1.2       markus    163:        copy->pw_class = xstrdup(pw->pw_class);
                    164:        copy->pw_dir = xstrdup(pw->pw_dir);
                    165:        copy->pw_shell = xstrdup(pw->pw_shell);
                    166:        return copy;
1.5       stevesk   167: }
                    168:
1.12      markus    169: /*
                    170:  * Convert ASCII string to TCP/IP port number.
                    171:  * Port must be >0 and <=65535.
                    172:  * Return 0 if invalid.
                    173:  */
                    174: int
                    175: a2port(const char *s)
1.5       stevesk   176: {
                    177:        long port;
                    178:        char *endp;
                    179:
                    180:        errno = 0;
                    181:        port = strtol(s, &endp, 0);
                    182:        if (s == endp || *endp != '\0' ||
                    183:            (errno == ERANGE && (port == LONG_MIN || port == LONG_MAX)) ||
                    184:            port <= 0 || port > 65535)
                    185:                return 0;
                    186:
                    187:        return port;
1.9       stevesk   188: }
                    189:
                    190: #define SECONDS                1
                    191: #define MINUTES                (SECONDS * 60)
                    192: #define HOURS          (MINUTES * 60)
                    193: #define DAYS           (HOURS * 24)
                    194: #define WEEKS          (DAYS * 7)
                    195:
1.12      markus    196: /*
                    197:  * Convert a time string into seconds; format is
                    198:  * a sequence of:
                    199:  *      time[qualifier]
                    200:  *
                    201:  * Valid time qualifiers are:
                    202:  *      <none>  seconds
                    203:  *      s|S     seconds
                    204:  *      m|M     minutes
                    205:  *      h|H     hours
                    206:  *      d|D     days
                    207:  *      w|W     weeks
                    208:  *
                    209:  * Examples:
                    210:  *      90m     90 minutes
                    211:  *      1h30m   90 minutes
                    212:  *      2d      2 days
                    213:  *      1w      1 week
                    214:  *
                    215:  * Return -1 if time string is invalid.
                    216:  */
                    217: long
                    218: convtime(const char *s)
1.9       stevesk   219: {
                    220:        long total, secs;
                    221:        const char *p;
                    222:        char *endp;
                    223:
                    224:        errno = 0;
                    225:        total = 0;
                    226:        p = s;
                    227:
                    228:        if (p == NULL || *p == '\0')
                    229:                return -1;
                    230:
                    231:        while (*p) {
                    232:                secs = strtol(p, &endp, 10);
                    233:                if (p == endp ||
                    234:                    (errno == ERANGE && (secs == LONG_MIN || secs == LONG_MAX)) ||
                    235:                    secs < 0)
                    236:                        return -1;
                    237:
                    238:                switch (*endp++) {
                    239:                case '\0':
                    240:                        endp--;
                    241:                case 's':
                    242:                case 'S':
                    243:                        break;
                    244:                case 'm':
                    245:                case 'M':
                    246:                        secs *= MINUTES;
                    247:                        break;
                    248:                case 'h':
                    249:                case 'H':
                    250:                        secs *= HOURS;
                    251:                        break;
                    252:                case 'd':
                    253:                case 'D':
                    254:                        secs *= DAYS;
                    255:                        break;
                    256:                case 'w':
                    257:                case 'W':
                    258:                        secs *= WEEKS;
                    259:                        break;
                    260:                default:
                    261:                        return -1;
                    262:                }
                    263:                total += secs;
                    264:                if (total < 0)
                    265:                        return -1;
                    266:                p = endp;
                    267:        }
                    268:
                    269:        return total;
1.6       mouring   270: }
                    271:
1.25.2.1! brad      272: /*
        !           273:  * Search for next delimiter between hostnames/addresses and ports.
        !           274:  * Argument may be modified (for termination).
        !           275:  * Returns *cp if parsing succeeds.
        !           276:  * *cp is set to the start of the next delimiter, if one was found.
        !           277:  * If this is the last field, *cp is set to NULL.
        !           278:  */
        !           279: char *
        !           280: hpdelim(char **cp)
        !           281: {
        !           282:        char *s, *old;
        !           283:
        !           284:        if (cp == NULL || *cp == NULL)
        !           285:                return NULL;
        !           286:
        !           287:        old = s = *cp;
        !           288:        if (*s == '[') {
        !           289:                if ((s = strchr(s, ']')) == NULL)
        !           290:                        return NULL;
        !           291:                else
        !           292:                        s++;
        !           293:        } else if ((s = strpbrk(s, ":/")) == NULL)
        !           294:                s = *cp + strlen(*cp); /* skip to end (see first case below) */
        !           295:
        !           296:        switch (*s) {
        !           297:        case '\0':
        !           298:                *cp = NULL;     /* no more fields*/
        !           299:                break;
        !           300:
        !           301:        case ':':
        !           302:        case '/':
        !           303:                *s = '\0';      /* terminate */
        !           304:                *cp = s + 1;
        !           305:                break;
        !           306:
        !           307:        default:
        !           308:                return NULL;
        !           309:        }
        !           310:
        !           311:        return old;
        !           312: }
        !           313:
1.6       mouring   314: char *
                    315: cleanhostname(char *host)
                    316: {
                    317:        if (*host == '[' && host[strlen(host) - 1] == ']') {
                    318:                host[strlen(host) - 1] = '\0';
                    319:                return (host + 1);
                    320:        } else
                    321:                return host;
                    322: }
                    323:
                    324: char *
                    325: colon(char *cp)
                    326: {
                    327:        int flag = 0;
                    328:
                    329:        if (*cp == ':')         /* Leading colon is part of file name. */
                    330:                return (0);
                    331:        if (*cp == '[')
                    332:                flag = 1;
                    333:
                    334:        for (; *cp; ++cp) {
                    335:                if (*cp == '@' && *(cp+1) == '[')
                    336:                        flag = 1;
                    337:                if (*cp == ']' && *(cp+1) == ':' && flag)
                    338:                        return (cp+1);
                    339:                if (*cp == ':' && !flag)
                    340:                        return (cp);
                    341:                if (*cp == '/')
                    342:                        return (0);
                    343:        }
                    344:        return (0);
1.7       mouring   345: }
                    346:
1.12      markus    347: /* function to assist building execv() arguments */
1.7       mouring   348: void
                    349: addargs(arglist *args, char *fmt, ...)
                    350: {
                    351:        va_list ap;
                    352:        char buf[1024];
1.25      avsm      353:        u_int nalloc;
1.7       mouring   354:
                    355:        va_start(ap, fmt);
                    356:        vsnprintf(buf, sizeof(buf), fmt, ap);
                    357:        va_end(ap);
                    358:
1.22      markus    359:        nalloc = args->nalloc;
1.7       mouring   360:        if (args->list == NULL) {
1.22      markus    361:                nalloc = 32;
1.7       mouring   362:                args->num = 0;
1.22      markus    363:        } else if (args->num+2 >= nalloc)
                    364:                nalloc *= 2;
1.7       mouring   365:
1.22      markus    366:        args->list = xrealloc(args->list, nalloc * sizeof(char *));
                    367:        args->nalloc = nalloc;
1.7       mouring   368:        args->list[args->num++] = xstrdup(buf);
                    369:        args->list[args->num] = NULL;
1.25.2.1! brad      370: }
        !           371:
        !           372: /*
        !           373:  * Read an entire line from a public key file into a static buffer, discarding
        !           374:  * lines that exceed the buffer size.  Returns 0 on success, -1 on failure.
        !           375:  */
        !           376: int
        !           377: read_keyfile_line(FILE *f, const char *filename, char *buf, size_t bufsz,
        !           378:    u_long *lineno)
        !           379: {
        !           380:        while (fgets(buf, bufsz, f) != NULL) {
        !           381:                (*lineno)++;
        !           382:                if (buf[strlen(buf) - 1] == '\n' || feof(f)) {
        !           383:                        return 0;
        !           384:                } else {
        !           385:                        debug("%s: %s line %lu exceeds size limit", __func__,
        !           386:                            filename, *lineno);
        !           387:                        /* discard remainder of line */
        !           388:                        while(fgetc(f) != '\n' && !feof(f))
        !           389:                                ;       /* nothing */
        !           390:                }
        !           391:        }
        !           392:        return -1;
1.1       markus    393: }