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

Annotation of src/usr.bin/ftp/ruserpass.c, Revision 1.24

1.24    ! pyr         1: /*     $OpenBSD: ruserpass.c,v 1.23 2007/03/24 14:29:02 moritz Exp $   */
1.9       millert     2: /*     $NetBSD: ruserpass.c,v 1.14 1997/07/20 09:46:01 lukem Exp $     */
1.1       deraadt     3:
                      4: /*
                      5:  * Copyright (c) 1985, 1993, 1994
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
1.16      millert    16:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    17:  *    may be used to endorse or promote products derived from this software
                     18:  *    without specific prior written permission.
                     19:  *
                     20:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     21:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     22:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     23:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     24:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     25:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     26:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     27:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     28:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     29:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     30:  * SUCH DAMAGE.
                     31:  */
                     32:
1.24    ! pyr        33: #ifndef SMALL
1.1       deraadt    34: #ifndef lint
1.4       millert    35: #if 0
1.1       deraadt    36: static char sccsid[] = "@(#)ruserpass.c        8.4 (Berkeley) 4/27/95";
1.4       millert    37: #else
1.24    ! pyr        38: static const char rcsid[] = "$OpenBSD: ruserpass.c,v 1.23 2007/03/24 14:29:02 moritz Exp $";
1.4       millert    39: #endif
1.1       deraadt    40: #endif /* not lint */
                     41:
                     42: #include <sys/types.h>
                     43: #include <sys/stat.h>
                     44:
                     45: #include <ctype.h>
                     46: #include <err.h>
                     47: #include <errno.h>
                     48: #include <stdio.h>
                     49: #include <stdlib.h>
                     50: #include <string.h>
                     51: #include <unistd.h>
                     52:
                     53: #include "ftp_var.h"
                     54:
1.13      millert    55: static int token(void);
1.1       deraadt    56: static FILE *cfile;
                     57:
                     58: #define        DEFAULT 1
                     59: #define        LOGIN   2
                     60: #define        PASSWD  3
                     61: #define        ACCOUNT 4
                     62: #define MACDEF  5
                     63: #define        ID      10
                     64: #define        MACH    11
                     65:
                     66: static char tokval[100];
                     67:
                     68: static struct toktab {
                     69:        char *tokstr;
                     70:        int tval;
                     71: } toktab[]= {
                     72:        { "default",    DEFAULT },
                     73:        { "login",      LOGIN },
                     74:        { "password",   PASSWD },
                     75:        { "passwd",     PASSWD },
                     76:        { "account",    ACCOUNT },
                     77:        { "machine",    MACH },
                     78:        { "macdef",     MACDEF },
                     79:        { NULL,         0 }
                     80: };
                     81:
                     82: int
1.18      deraadt    83: ruserpass(const char *host, char **aname, char **apass, char **aacct)
1.1       deraadt    84: {
1.23      moritz     85:        char *hdir, buf[MAXPATHLEN], *tmp;
1.1       deraadt    86:        char myname[MAXHOSTNAMELEN], *mydomain;
                     87:        int t, i, c, usedefault = 0;
                     88:        struct stat stb;
                     89:
                     90:        hdir = getenv("HOME");
1.14      millert    91:        if (hdir == NULL || *hdir == '\0')
1.12      millert    92:                return (0);
1.22      moritz     93:        i = snprintf(buf, sizeof(buf), "%s/.netrc", hdir);
                     94:        if (i < 0 || i >= sizeof(buf)) {
1.3       millert    95:                warnx("%s/.netrc: %s", hdir, strerror(ENAMETOOLONG));
                     96:                return (0);
                     97:        }
1.1       deraadt    98:        cfile = fopen(buf, "r");
                     99:        if (cfile == NULL) {
                    100:                if (errno != ENOENT)
                    101:                        warn("%s", buf);
                    102:                return (0);
                    103:        }
                    104:        if (gethostname(myname, sizeof(myname)) < 0)
                    105:                myname[0] = '\0';
                    106:        if ((mydomain = strchr(myname, '.')) == NULL)
                    107:                mydomain = "";
                    108: next:
1.21      moritz    109:        while ((t = token()) > 0) switch(t) {
1.1       deraadt   110:
                    111:        case DEFAULT:
                    112:                usedefault = 1;
1.20      ray       113:                /* FALLTHROUGH */
1.1       deraadt   114:
                    115:        case MACH:
                    116:                if (!usedefault) {
1.21      moritz    117:                        if ((t = token()) == -1)
                    118:                                goto bad;
                    119:                        if (t != ID)
1.1       deraadt   120:                                continue;
                    121:                        /*
                    122:                         * Allow match either for user's input host name
1.4       millert   123:                         * or official hostname.  Also allow match of
1.1       deraadt   124:                         * incompletely-specified host in local domain.
                    125:                         */
                    126:                        if (strcasecmp(host, tokval) == 0)
                    127:                                goto match;
                    128:                        if (strcasecmp(hostname, tokval) == 0)
                    129:                                goto match;
                    130:                        if ((tmp = strchr(hostname, '.')) != NULL &&
                    131:                            strcasecmp(tmp, mydomain) == 0 &&
1.9       millert   132:                            strncasecmp(hostname, tokval,
                    133:                            (size_t)(tmp - hostname)) == 0 &&
1.1       deraadt   134:                            tokval[tmp - hostname] == '\0')
                    135:                                goto match;
                    136:                        if ((tmp = strchr(host, '.')) != NULL &&
                    137:                            strcasecmp(tmp, mydomain) == 0 &&
1.9       millert   138:                            strncasecmp(host, tokval,
                    139:                            (size_t)(tmp - host)) == 0 &&
1.1       deraadt   140:                            tokval[tmp - host] == '\0')
                    141:                                goto match;
                    142:                        continue;
                    143:                }
                    144:        match:
1.21      moritz    145:                while ((t = token()) > 0 &&
                    146:                    t != MACH && t != DEFAULT) switch(t) {
1.1       deraadt   147:
                    148:                case LOGIN:
1.21      moritz    149:                        if ((t = token()) == -1)
                    150:                                goto bad;
                    151:                        if (t) {
1.15      deraadt   152:                                if (*aname == 0)
                    153:                                        *aname = strdup(tokval);
                    154:                                else {
1.1       deraadt   155:                                        if (strcmp(*aname, tokval))
                    156:                                                goto next;
                    157:                                }
1.11      deraadt   158:                        }
1.1       deraadt   159:                        break;
                    160:                case PASSWD:
                    161:                        if ((*aname == NULL || strcmp(*aname, "anonymous")) &&
                    162:                            fstat(fileno(cfile), &stb) >= 0 &&
                    163:                            (stb.st_mode & 077) != 0) {
                    164:        warnx("Error: .netrc file is readable by others.");
                    165:        warnx("Remove password or make file unreadable by others.");
                    166:                                goto bad;
                    167:                        }
1.21      moritz    168:                        if ((t = token()) == -1)
                    169:                                goto bad;
                    170:                        if (t && *apass == 0)
1.15      deraadt   171:                                *apass = strdup(tokval);
1.1       deraadt   172:                        break;
                    173:                case ACCOUNT:
                    174:                        if (fstat(fileno(cfile), &stb) >= 0
                    175:                            && (stb.st_mode & 077) != 0) {
                    176:        warnx("Error: .netrc file is readable by others.");
                    177:        warnx("Remove account or make file unreadable by others.");
                    178:                                goto bad;
                    179:                        }
1.21      moritz    180:                        if ((t = token()) == -1)
                    181:                                goto bad;
                    182:                        if (t && *aacct == 0)
1.15      deraadt   183:                                *aacct = strdup(tokval);
1.1       deraadt   184:                        break;
                    185:                case MACDEF:
                    186:                        if (proxy) {
1.5       millert   187:                                (void)fclose(cfile);
1.1       deraadt   188:                                return (0);
                    189:                        }
1.10      millert   190:                        while ((c = fgetc(cfile)) != EOF)
1.4       millert   191:                                if (c != ' ' && c != '\t')
                    192:                                        break;
1.1       deraadt   193:                        if (c == EOF || c == '\n') {
1.8       deraadt   194:                                fputs("Missing macdef name argument.\n", ttyout);
1.1       deraadt   195:                                goto bad;
                    196:                        }
                    197:                        if (macnum == 16) {
1.8       deraadt   198:                                fputs(
                    199: "Limit of 16 macros have already been defined.\n", ttyout);
1.1       deraadt   200:                                goto bad;
                    201:                        }
                    202:                        tmp = macros[macnum].mac_name;
                    203:                        *tmp++ = c;
1.10      millert   204:                        for (i=0; i < 8 && (c = fgetc(cfile)) != EOF &&
1.1       deraadt   205:                            !isspace(c); ++i) {
                    206:                                *tmp++ = c;
                    207:                        }
                    208:                        if (c == EOF) {
1.8       deraadt   209:                                fputs(
                    210: "Macro definition missing null line terminator.\n", ttyout);
1.1       deraadt   211:                                goto bad;
                    212:                        }
                    213:                        *tmp = '\0';
                    214:                        if (c != '\n') {
1.10      millert   215:                                while ((c = fgetc(cfile)) != EOF && c != '\n');
1.1       deraadt   216:                        }
                    217:                        if (c == EOF) {
1.8       deraadt   218:                                fputs(
                    219: "Macro definition missing null line terminator.\n", ttyout);
1.1       deraadt   220:                                goto bad;
                    221:                        }
                    222:                        if (macnum == 0) {
                    223:                                macros[macnum].mac_start = macbuf;
                    224:                        }
                    225:                        else {
1.4       millert   226:                                macros[macnum].mac_start =
                    227:                                    macros[macnum-1].mac_end + 1;
1.1       deraadt   228:                        }
                    229:                        tmp = macros[macnum].mac_start;
                    230:                        while (tmp != macbuf + 4096) {
1.10      millert   231:                                if ((c = fgetc(cfile)) == EOF) {
1.8       deraadt   232:                                fputs(
                    233: "Macro definition missing null line terminator.\n", ttyout);
1.1       deraadt   234:                                        goto bad;
                    235:                                }
                    236:                                *tmp = c;
                    237:                                if (*tmp == '\n') {
1.23      moritz    238:                                        if (tmp == macros[macnum].mac_start) {
                    239:                                                macros[macnum++].mac_end = tmp;
                    240:                                                break;
                    241:                                        } else if (*(tmp-1) == '\0') {
                    242:                                                macros[macnum++].mac_end =
                    243:                                                    tmp - 1;
                    244:                                                break;
1.1       deraadt   245:                                        }
                    246:                                        *tmp = '\0';
                    247:                                }
                    248:                                tmp++;
                    249:                        }
                    250:                        if (tmp == macbuf + 4096) {
1.8       deraadt   251:                                fputs("4K macro buffer exceeded.\n", ttyout);
1.1       deraadt   252:                                goto bad;
                    253:                        }
                    254:                        break;
                    255:                default:
                    256:                        warnx("Unknown .netrc keyword %s", tokval);
                    257:                        break;
                    258:                }
                    259:                goto done;
                    260:        }
                    261: done:
1.21      moritz    262:        if (t == -1)
                    263:                goto bad;
1.5       millert   264:        (void)fclose(cfile);
1.1       deraadt   265:        return (0);
                    266: bad:
1.5       millert   267:        (void)fclose(cfile);
1.1       deraadt   268:        return (-1);
                    269: }
                    270:
                    271: static int
1.18      deraadt   272: token(void)
1.1       deraadt   273: {
                    274:        char *cp;
                    275:        int c;
                    276:        struct toktab *t;
                    277:
                    278:        if (feof(cfile) || ferror(cfile))
                    279:                return (0);
1.10      millert   280:        while ((c = fgetc(cfile)) != EOF &&
1.1       deraadt   281:            (c == '\n' || c == '\t' || c == ' ' || c == ','))
                    282:                continue;
                    283:        if (c == EOF)
                    284:                return (0);
                    285:        cp = tokval;
                    286:        if (c == '"') {
1.10      millert   287:                while ((c = fgetc(cfile)) != EOF && c != '"') {
1.1       deraadt   288:                        if (c == '\\')
1.10      millert   289:                                c = fgetc(cfile);
1.1       deraadt   290:                        *cp++ = c;
1.21      moritz    291:                        if (cp == tokval + sizeof(tokval)) {
                    292:                                warnx("Token in .netrc too long");
                    293:                                return (-1);
                    294:                        }
1.1       deraadt   295:                }
                    296:        } else {
                    297:                *cp++ = c;
1.10      millert   298:                while ((c = fgetc(cfile)) != EOF
1.1       deraadt   299:                    && c != '\n' && c != '\t' && c != ' ' && c != ',') {
                    300:                        if (c == '\\')
1.10      millert   301:                                c = fgetc(cfile);
1.1       deraadt   302:                        *cp++ = c;
1.21      moritz    303:                        if (cp == tokval + sizeof(tokval)) {
                    304:                                warnx("Token in .netrc too long");
                    305:                                return (-1);
                    306:                        }
1.1       deraadt   307:                }
                    308:        }
                    309:        *cp = 0;
                    310:        if (tokval[0] == 0)
                    311:                return (0);
                    312:        for (t = toktab; t->tokstr; t++)
                    313:                if (!strcmp(t->tokstr, tokval))
                    314:                        return (t->tval);
                    315:        return (ID);
                    316: }
1.24    ! pyr       317: #endif