[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.10

1.10    ! markus      1: /*     $OpenBSD: misc.c,v 1.9 2001/05/19 19:43:57 stevesk Exp $        */
1.1       markus      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:
                     27: #include "includes.h"
1.10    ! markus     28: RCSID("$OpenBSD: misc.c,v 1.9 2001/05/19 19:43:57 stevesk Exp $");
1.1       markus     29:
                     30: #include "misc.h"
                     31: #include "log.h"
1.3       deraadt    32: #include "xmalloc.h"
1.1       markus     33:
                     34: char *
                     35: chop(char *s)
                     36: {
                     37:        char *t = s;
                     38:        while (*t) {
                     39:                if(*t == '\n' || *t == '\r') {
                     40:                        *t = '\0';
                     41:                        return s;
                     42:                }
                     43:                t++;
                     44:        }
                     45:        return s;
                     46:
                     47: }
                     48:
                     49: void
                     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));
                     57:                return;
                     58:        }
                     59:        if (val & O_NONBLOCK) {
1.8       markus     60:                debug2("fd %d is O_NONBLOCK", fd);
1.1       markus     61:                return;
                     62:        }
                     63:        debug("fd %d setting O_NONBLOCK", fd);
                     64:        val |= O_NONBLOCK;
1.8       markus     65:        if (fcntl(fd, F_SETFL, val) == -1)
                     66:                if (errno != ENODEV)
                     67:                        error("fcntl(%d, F_SETFL, O_NONBLOCK): %s",
                     68:                            fd, strerror(errno));
                     69: }
                     70:
                     71: void
                     72: unset_nonblock(int fd)
                     73: {
                     74:        int val;
                     75:
                     76:        val = fcntl(fd, F_GETFL, 0);
                     77:        if (val < 0) {
                     78:                error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno));
                     79:                return;
                     80:        }
                     81:        if (!(val & O_NONBLOCK)) {
                     82:                debug2("fd %d is not O_NONBLOCK", fd);
                     83:                return;
                     84:        }
1.10    ! markus     85:        debug("fd %d clearing O_NONBLOCK", fd);
1.8       markus     86:        val &= ~O_NONBLOCK;
1.1       markus     87:        if (fcntl(fd, F_SETFL, val) == -1)
                     88:                if (errno != ENODEV)
                     89:                        error("fcntl(%d, F_SETFL, O_NONBLOCK): %s",
                     90:                            fd, strerror(errno));
                     91: }
                     92:
                     93: /* Characters considered whitespace in strsep calls. */
                     94: #define WHITESPACE " \t\r\n"
                     95:
                     96: char *
                     97: strdelim(char **s)
                     98: {
                     99:        char *old;
                    100:        int wspace = 0;
                    101:
                    102:        if (*s == NULL)
                    103:                return NULL;
                    104:
                    105:        old = *s;
                    106:
                    107:        *s = strpbrk(*s, WHITESPACE "=");
                    108:        if (*s == NULL)
                    109:                return (old);
                    110:
                    111:        /* Allow only one '=' to be skipped */
                    112:        if (*s[0] == '=')
                    113:                wspace = 1;
                    114:        *s[0] = '\0';
                    115:
                    116:        *s += strspn(*s + 1, WHITESPACE) + 1;
                    117:        if (*s[0] == '=' && !wspace)
                    118:                *s += strspn(*s + 1, WHITESPACE) + 1;
                    119:
                    120:        return (old);
1.2       markus    121: }
                    122:
                    123: struct passwd *
                    124: pwcopy(struct passwd *pw)
                    125: {
                    126:        struct passwd *copy = xmalloc(sizeof(*copy));
1.4       deraadt   127:
1.2       markus    128:        memset(copy, 0, sizeof(*copy));
                    129:        copy->pw_name = xstrdup(pw->pw_name);
                    130:        copy->pw_passwd = xstrdup(pw->pw_passwd);
1.4       deraadt   131:        copy->pw_gecos = xstrdup(pw->pw_gecos);
1.2       markus    132:        copy->pw_uid = pw->pw_uid;
                    133:        copy->pw_gid = pw->pw_gid;
                    134:        copy->pw_class = xstrdup(pw->pw_class);
                    135:        copy->pw_dir = xstrdup(pw->pw_dir);
                    136:        copy->pw_shell = xstrdup(pw->pw_shell);
                    137:        return copy;
1.5       stevesk   138: }
                    139:
                    140: int a2port(const char *s)
                    141: {
                    142:        long port;
                    143:        char *endp;
                    144:
                    145:        errno = 0;
                    146:        port = strtol(s, &endp, 0);
                    147:        if (s == endp || *endp != '\0' ||
                    148:            (errno == ERANGE && (port == LONG_MIN || port == LONG_MAX)) ||
                    149:            port <= 0 || port > 65535)
                    150:                return 0;
                    151:
                    152:        return port;
1.9       stevesk   153: }
                    154:
                    155: #define SECONDS                1
                    156: #define MINUTES                (SECONDS * 60)
                    157: #define HOURS          (MINUTES * 60)
                    158: #define DAYS           (HOURS * 24)
                    159: #define WEEKS          (DAYS * 7)
                    160:
                    161: long convtime(const char *s)
                    162: {
                    163:        long total, secs;
                    164:        const char *p;
                    165:        char *endp;
                    166:
                    167:        errno = 0;
                    168:        total = 0;
                    169:        p = s;
                    170:
                    171:        if (p == NULL || *p == '\0')
                    172:                return -1;
                    173:
                    174:        while (*p) {
                    175:                secs = strtol(p, &endp, 10);
                    176:                if (p == endp ||
                    177:                    (errno == ERANGE && (secs == LONG_MIN || secs == LONG_MAX)) ||
                    178:                    secs < 0)
                    179:                        return -1;
                    180:
                    181:                switch (*endp++) {
                    182:                case '\0':
                    183:                        endp--;
                    184:                case 's':
                    185:                case 'S':
                    186:                        break;
                    187:                case 'm':
                    188:                case 'M':
                    189:                        secs *= MINUTES;
                    190:                        break;
                    191:                case 'h':
                    192:                case 'H':
                    193:                        secs *= HOURS;
                    194:                        break;
                    195:                case 'd':
                    196:                case 'D':
                    197:                        secs *= DAYS;
                    198:                        break;
                    199:                case 'w':
                    200:                case 'W':
                    201:                        secs *= WEEKS;
                    202:                        break;
                    203:                default:
                    204:                        return -1;
                    205:                }
                    206:                total += secs;
                    207:                if (total < 0)
                    208:                        return -1;
                    209:                p = endp;
                    210:        }
                    211:
                    212:        return total;
1.6       mouring   213: }
                    214:
                    215: char *
                    216: cleanhostname(char *host)
                    217: {
                    218:        if (*host == '[' && host[strlen(host) - 1] == ']') {
                    219:                host[strlen(host) - 1] = '\0';
                    220:                return (host + 1);
                    221:        } else
                    222:                return host;
                    223: }
                    224:
                    225: char *
                    226: colon(char *cp)
                    227: {
                    228:        int flag = 0;
                    229:
                    230:        if (*cp == ':')         /* Leading colon is part of file name. */
                    231:                return (0);
                    232:        if (*cp == '[')
                    233:                flag = 1;
                    234:
                    235:        for (; *cp; ++cp) {
                    236:                if (*cp == '@' && *(cp+1) == '[')
                    237:                        flag = 1;
                    238:                if (*cp == ']' && *(cp+1) == ':' && flag)
                    239:                        return (cp+1);
                    240:                if (*cp == ':' && !flag)
                    241:                        return (cp);
                    242:                if (*cp == '/')
                    243:                        return (0);
                    244:        }
                    245:        return (0);
1.7       mouring   246: }
                    247:
                    248: void
                    249: addargs(arglist *args, char *fmt, ...)
                    250: {
                    251:        va_list ap;
                    252:        char buf[1024];
                    253:
                    254:        va_start(ap, fmt);
                    255:        vsnprintf(buf, sizeof(buf), fmt, ap);
                    256:        va_end(ap);
                    257:
                    258:        if (args->list == NULL) {
                    259:                args->nalloc = 32;
                    260:                args->num = 0;
                    261:        } else if (args->num+2 >= args->nalloc)
                    262:                args->nalloc *= 2;
                    263:
                    264:        args->list = xrealloc(args->list, args->nalloc * sizeof(char *));
                    265:        args->list[args->num++] = xstrdup(buf);
                    266:        args->list[args->num] = NULL;
1.1       markus    267: }