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