Annotation of 43BSDReno/usr.bin/ftp/ruserpass.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * Copyright (c) 1985 Regents of the University of California.
                      3:  * All rights reserved.
                      4:  *
                      5:  * Redistribution and use in source and binary forms are permitted
                      6:  * provided that: (1) source distributions retain this entire copyright
                      7:  * notice and comment, and (2) distributions including binaries display
                      8:  * the following acknowledgement:  ``This product includes software
                      9:  * developed by the University of California, Berkeley and its contributors''
                     10:  * in the documentation or other materials provided with the distribution
                     11:  * and in all advertising materials mentioning features or use of this
                     12:  * software. Neither the name of the University nor the names of its
                     13:  * contributors may be used to endorse or promote products derived
                     14:  * from this software without specific prior written permission.
                     15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
                     16:  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
                     17:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
                     18:  */
                     19: 
                     20: #ifndef lint
                     21: static char sccsid[] = "@(#)ruserpass.c        5.2 (Berkeley) 6/1/90";
                     22: #endif /* not lint */
                     23: 
                     24: #include <sys/types.h>
                     25: #include <stdio.h>
                     26: #include <utmp.h>
                     27: #include <ctype.h>
                     28: #include <sys/stat.h>
                     29: #include <errno.h>
                     30: #include "ftp_var.h"
                     31: 
                     32: char   *renvlook(), *malloc(), *index(), *getenv(), *getpass(), *getlogin();
                     33: char   *strcpy();
                     34: struct utmp *getutmp();
                     35: static FILE *cfile;
                     36: 
                     37: #define        DEFAULT 1
                     38: #define        LOGIN   2
                     39: #define        PASSWD  3
                     40: #define        ACCOUNT 4
                     41: #define MACDEF  5
                     42: #define        ID      10
                     43: #define        MACH    11
                     44: 
                     45: static char tokval[100];
                     46: 
                     47: static struct toktab {
                     48:        char *tokstr;
                     49:        int tval;
                     50: } toktab[]= {
                     51:        "default",      DEFAULT,
                     52:        "login",        LOGIN,
                     53:        "password",     PASSWD,
                     54:        "passwd",       PASSWD,
                     55:        "account",      ACCOUNT,
                     56:        "machine",      MACH,
                     57:        "macdef",       MACDEF,
                     58:        0,              0
                     59: };
                     60: 
                     61: ruserpass(host, aname, apass, aacct)
                     62:        char *host, **aname, **apass, **aacct;
                     63: {
                     64:        char *hdir, buf[BUFSIZ], *tmp;
                     65:        char myname[MAXHOSTNAMELEN], *mydomain;
                     66:        int t, i, c, usedefault = 0;
                     67:        struct stat stb;
                     68:        extern int errno;
                     69: 
                     70:        hdir = getenv("HOME");
                     71:        if (hdir == NULL)
                     72:                hdir = ".";
                     73:        (void) sprintf(buf, "%s/.netrc", hdir);
                     74:        cfile = fopen(buf, "r");
                     75:        if (cfile == NULL) {
                     76:                if (errno != ENOENT)
                     77:                        perror(buf);
                     78:                return(0);
                     79:        }
                     80:        if (gethostname(myname, sizeof(myname)) < 0)
                     81:                myname[0] = '\0';
                     82:        if ((mydomain = index(myname, '.')) == NULL)
                     83:                mydomain = "";
                     84: next:
                     85:        while ((t = token())) switch(t) {
                     86: 
                     87:        case DEFAULT:
                     88:                usedefault = 1;
                     89:                /* FALL THROUGH */
                     90: 
                     91:        case MACH:
                     92:                if (!usedefault) {
                     93:                        if (token() != ID)
                     94:                                continue;
                     95:                        /*
                     96:                         * Allow match either for user's input host name
                     97:                         * or official hostname.  Also allow match of 
                     98:                         * incompletely-specified host in local domain.
                     99:                         */
                    100:                        if (strcasecmp(host, tokval) == 0)
                    101:                                goto match;
                    102:                        if (strcasecmp(hostname, tokval) == 0)
                    103:                                goto match;
                    104:                        if ((tmp = index(hostname, '.')) != NULL &&
                    105:                            strcasecmp(tmp, mydomain) == 0 &&
                    106:                            strncasecmp(hostname, tokval, tmp-hostname) == 0 &&
                    107:                            tokval[tmp - hostname] == '\0')
                    108:                                goto match;
                    109:                        if ((tmp = index(host, '.')) != NULL &&
                    110:                            strcasecmp(tmp, mydomain) == 0 &&
                    111:                            strncasecmp(host, tokval, tmp - host) == 0 &&
                    112:                            tokval[tmp - host] == '\0')
                    113:                                goto match;
                    114:                        continue;
                    115:                }
                    116:        match:
                    117:                while ((t = token()) && t != MACH && t != DEFAULT) switch(t) {
                    118: 
                    119:                case LOGIN:
                    120:                        if (token())
                    121:                                if (*aname == 0) { 
                    122:                                        *aname = malloc((unsigned) strlen(tokval) + 1);
                    123:                                        (void) strcpy(*aname, tokval);
                    124:                                } else {
                    125:                                        if (strcmp(*aname, tokval))
                    126:                                                goto next;
                    127:                                }
                    128:                        break;
                    129:                case PASSWD:
                    130:                        if (strcmp(*aname, "anonymous") &&
                    131:                            fstat(fileno(cfile), &stb) >= 0 &&
                    132:                            (stb.st_mode & 077) != 0) {
                    133:        fprintf(stderr, "Error - .netrc file not correct mode.\n");
                    134:        fprintf(stderr, "Remove password or correct mode.\n");
                    135:                                goto bad;
                    136:                        }
                    137:                        if (token() && *apass == 0) {
                    138:                                *apass = malloc((unsigned) strlen(tokval) + 1);
                    139:                                (void) strcpy(*apass, tokval);
                    140:                        }
                    141:                        break;
                    142:                case ACCOUNT:
                    143:                        if (fstat(fileno(cfile), &stb) >= 0
                    144:                            && (stb.st_mode & 077) != 0) {
                    145:        fprintf(stderr, "Error - .netrc file not correct mode.\n");
                    146:        fprintf(stderr, "Remove account or correct mode.\n");
                    147:                                goto bad;
                    148:                        }
                    149:                        if (token() && *aacct == 0) {
                    150:                                *aacct = malloc((unsigned) strlen(tokval) + 1);
                    151:                                (void) strcpy(*aacct, tokval);
                    152:                        }
                    153:                        break;
                    154:                case MACDEF:
                    155:                        if (proxy) {
                    156:                                (void) fclose(cfile);
                    157:                                return(0);
                    158:                        }
                    159:                        while ((c=getc(cfile)) != EOF && c == ' ' || c == '\t');
                    160:                        if (c == EOF || c == '\n') {
                    161:                                printf("Missing macdef name argument.\n");
                    162:                                goto bad;
                    163:                        }
                    164:                        if (macnum == 16) {
                    165:                                printf("Limit of 16 macros have already been defined\n");
                    166:                                goto bad;
                    167:                        }
                    168:                        tmp = macros[macnum].mac_name;
                    169:                        *tmp++ = c;
                    170:                        for (i=0; i < 8 && (c=getc(cfile)) != EOF &&
                    171:                            !isspace(c); ++i) {
                    172:                                *tmp++ = c;
                    173:                        }
                    174:                        if (c == EOF) {
                    175:                                printf("Macro definition missing null line terminator.\n");
                    176:                                goto bad;
                    177:                        }
                    178:                        *tmp = '\0';
                    179:                        if (c != '\n') {
                    180:                                while ((c=getc(cfile)) != EOF && c != '\n');
                    181:                        }
                    182:                        if (c == EOF) {
                    183:                                printf("Macro definition missing null line terminator.\n");
                    184:                                goto bad;
                    185:                        }
                    186:                        if (macnum == 0) {
                    187:                                macros[macnum].mac_start = macbuf;
                    188:                        }
                    189:                        else {
                    190:                                macros[macnum].mac_start = macros[macnum-1].mac_end + 1;
                    191:                        }
                    192:                        tmp = macros[macnum].mac_start;
                    193:                        while (tmp != macbuf + 4096) {
                    194:                                if ((c=getc(cfile)) == EOF) {
                    195:                                printf("Macro definition missing null line terminator.\n");
                    196:                                        goto bad;
                    197:                                }
                    198:                                *tmp = c;
                    199:                                if (*tmp == '\n') {
                    200:                                        if (*(tmp-1) == '\0') {
                    201:                                           macros[macnum++].mac_end = tmp - 1;
                    202:                                           break;
                    203:                                        }
                    204:                                        *tmp = '\0';
                    205:                                }
                    206:                                tmp++;
                    207:                        }
                    208:                        if (tmp == macbuf + 4096) {
                    209:                                printf("4K macro buffer exceeded\n");
                    210:                                goto bad;
                    211:                        }
                    212:                        break;
                    213:                default:
                    214:        fprintf(stderr, "Unknown .netrc keyword %s\n", tokval);
                    215:                        break;
                    216:                }
                    217:                goto done;
                    218:        }
                    219: done:
                    220:        (void) fclose(cfile);
                    221:        return(0);
                    222: bad:
                    223:        (void) fclose(cfile);
                    224:        return(-1);
                    225: }
                    226: 
                    227: static
                    228: token()
                    229: {
                    230:        char *cp;
                    231:        int c;
                    232:        struct toktab *t;
                    233: 
                    234:        if (feof(cfile))
                    235:                return (0);
                    236:        while ((c = getc(cfile)) != EOF &&
                    237:            (c == '\n' || c == '\t' || c == ' ' || c == ','))
                    238:                continue;
                    239:        if (c == EOF)
                    240:                return (0);
                    241:        cp = tokval;
                    242:        if (c == '"') {
                    243:                while ((c = getc(cfile)) != EOF && c != '"') {
                    244:                        if (c == '\\')
                    245:                                c = getc(cfile);
                    246:                        *cp++ = c;
                    247:                }
                    248:        } else {
                    249:                *cp++ = c;
                    250:                while ((c = getc(cfile)) != EOF
                    251:                    && c != '\n' && c != '\t' && c != ' ' && c != ',') {
                    252:                        if (c == '\\')
                    253:                                c = getc(cfile);
                    254:                        *cp++ = c;
                    255:                }
                    256:        }
                    257:        *cp = 0;
                    258:        if (tokval[0] == 0)
                    259:                return (0);
                    260:        for (t = toktab; t->tokstr; t++)
                    261:                if (!strcmp(t->tokstr, tokval))
                    262:                        return (t->tval);
                    263:        return (ID);
                    264: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.