Home | History | Annotate | Line # | Download | only in 4.3
ruserpass.c revision 1.7
      1 /*	$NetBSD: ruserpass.c,v 1.7 1999/09/20 04:48:04 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 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "@(#)ruserpass.c	8.4 (Berkeley) 4/27/95";
     40 #else
     41 __RCSID("$NetBSD: ruserpass.c,v 1.7 1999/09/20 04:48:04 lukem Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #include <sys/types.h>
     46 #include <sys/stat.h>
     47 #include <sys/param.h>
     48 
     49 #include <assert.h>
     50 #include <ctype.h>
     51 #include <err.h>
     52 #include <errno.h>
     53 #include <stdio.h>
     54 #include <stdlib.h>
     55 #include <string.h>
     56 #include <unistd.h>
     57 
     58 struct macel {
     59 	char mac_name[9];	/* macro name */
     60 	char *mac_start;	/* start of macro in macbuf */
     61 	char *mac_end;		/* end of macro in macbuf */
     62 };
     63 
     64 static	int token __P((void));
     65 static	FILE *cfile;
     66 static	int macnum;		/* number of defined macros */
     67 static	struct macel macros[16];
     68 static	char macbuf[4096];
     69 
     70 #define	DEFAULT	1
     71 #define	LOGIN	2
     72 #define	PASSWD	3
     73 #define	ACCOUNT 4
     74 #define MACDEF  5
     75 #define	ID	10
     76 #define	MACH	11
     77 
     78 static char tokval[100];
     79 
     80 static struct toktab {
     81 	char *tokstr;
     82 	int tval;
     83 } toktab[]= {
     84 	{ "default",	DEFAULT },
     85 	{ "login",	LOGIN },
     86 	{ "password",	PASSWD },
     87 	{ "passwd",	PASSWD },
     88 	{ "account",	ACCOUNT },
     89 	{ "machine",	MACH },
     90 	{ "macdef",	MACDEF },
     91 	{ NULL,		0 }
     92 };
     93 
     94 int ruserpass __P((const char *, char **, char **));
     95 
     96 int
     97 ruserpass(host, aname, apass)
     98 	const char *host;
     99 	char **aname, **apass;
    100 {
    101 	char *hdir, buf[BUFSIZ], *tmp;
    102 	char myname[MAXHOSTNAMELEN + 1], *mydomain;
    103 	int t, i, c, usedefault = 0;
    104 	struct stat stb;
    105 
    106 	_DIAGASSERT(host != NULL);
    107 	_DIAGASSERT(aname != NULL);
    108 	_DIAGASSERT(apass != NULL);
    109 
    110 	hdir = getenv("HOME");
    111 	if (hdir == NULL)
    112 		hdir = ".";
    113 	if (strlen(hdir) + sizeof(".netrc") < sizeof(buf)) {
    114 		(void)snprintf(buf, sizeof buf, "%s/.netrc", hdir);
    115 	} else {
    116 		warnx("%s/.netrc: %s", hdir, strerror(ENAMETOOLONG));
    117 		return (0);
    118 	}
    119 	cfile = fopen(buf, "r");
    120 	if (cfile == NULL) {
    121 		if (errno != ENOENT)
    122 			warn("%s", buf);
    123 		return (0);
    124 	}
    125 	if (gethostname(myname, sizeof(myname)) < 0)
    126 		myname[0] = '\0';
    127 	else
    128 		myname[sizeof(myname) - 1] = '\0';
    129 	if ((mydomain = strchr(myname, '.')) == NULL)
    130 		mydomain = "";
    131 next:
    132 	while ((t = token()) != NULL) switch(t) {
    133 
    134 	case DEFAULT:
    135 		usedefault = 1;
    136 		/* FALLTHROUGH */
    137 
    138 	case MACH:
    139 		if (!usedefault) {
    140 			if (token() != ID)
    141 				continue;
    142 			/*
    143 			 * Allow match either for user's input host name
    144 			 * or official hostname.  Also allow match of
    145 			 * incompletely-specified host in local domain.
    146 			 */
    147 			if (strcasecmp(host, tokval) == 0)
    148 				goto match;
    149 			if ((tmp = strchr(host, '.')) != NULL &&
    150 			    strcasecmp(tmp, mydomain) == 0 &&
    151 			    strncasecmp(host, tokval,
    152 			    (size_t)(tmp - host)) == 0 &&
    153 			    tokval[tmp - host] == '\0')
    154 				goto match;
    155 			continue;
    156 		}
    157 	match:
    158 		while ((t = token()) && t != MACH && t != DEFAULT) switch(t) {
    159 
    160 		case LOGIN:
    161 			if (token()) {
    162 				if (*aname == NULL) {
    163 					*aname = strdup(tokval);
    164 					if (*aname == NULL)
    165 						err(1, "can't strdup *aname");
    166 				} else {
    167 					if (strcmp(*aname, tokval))
    168 						goto next;
    169 				}
    170 			}
    171 			break;
    172 		case PASSWD:
    173 			if ((*aname == NULL || strcmp(*aname, "anonymous")) &&
    174 			    fstat(fileno(cfile), &stb) >= 0 &&
    175 			    (stb.st_mode & 077) != 0) {
    176 	warnx("Error: .netrc file is readable by others.");
    177 	warnx("Remove password or make file unreadable by others.");
    178 				goto bad;
    179 			}
    180 			if (token() && *apass == NULL) {
    181 				*apass = strdup(tokval);
    182 				if (*apass == NULL)
    183 					err(1, "can't strdup *apass");
    184 			}
    185 			break;
    186 		case ACCOUNT:
    187 			if (fstat(fileno(cfile), &stb) >= 0
    188 			    && (stb.st_mode & 077) != 0) {
    189 	warnx("Error: .netrc file is readable by others.");
    190 	warnx("Remove account or make file unreadable by others.");
    191 				goto bad;
    192 			}
    193 			break;
    194 		case MACDEF:
    195 			while ((c=getc(cfile)) != EOF)
    196 				if (c != ' ' && c != '\t')
    197 					break;
    198 			if (c == EOF || c == '\n') {
    199 				puts("Missing macdef name argument.");
    200 				goto bad;
    201 			}
    202 			if (macnum == 16) {
    203 				puts(
    204 "Limit of 16 macros have already been defined.");
    205 				goto bad;
    206 			}
    207 			tmp = macros[macnum].mac_name;
    208 			*tmp++ = c;
    209 			for (i=0; i < 8 && (c=getc(cfile)) != EOF &&
    210 			    !isspace(c); ++i) {
    211 				*tmp++ = c;
    212 			}
    213 			if (c == EOF) {
    214 				puts(
    215 "Macro definition missing null line terminator.");
    216 				goto bad;
    217 			}
    218 			*tmp = '\0';
    219 			if (c != '\n') {
    220 				while ((c=getc(cfile)) != EOF && c != '\n');
    221 			}
    222 			if (c == EOF) {
    223 				puts(
    224 "Macro definition missing null line terminator.");
    225 				goto bad;
    226 			}
    227 			if (macnum == 0) {
    228 				macros[macnum].mac_start = macbuf;
    229 			}
    230 			else {
    231 				macros[macnum].mac_start =
    232 				    macros[macnum-1].mac_end + 1;
    233 			}
    234 			tmp = macros[macnum].mac_start;
    235 			while (tmp != macbuf + 4096) {
    236 				if ((c=getc(cfile)) == EOF) {
    237 				puts(
    238 "Macro definition missing null line terminator.");
    239 					goto bad;
    240 				}
    241 				*tmp = c;
    242 				if (*tmp == '\n') {
    243 					if (*(tmp-1) == '\0') {
    244 					   macros[macnum++].mac_end = tmp - 1;
    245 					   break;
    246 					}
    247 					*tmp = '\0';
    248 				}
    249 				tmp++;
    250 			}
    251 			if (tmp == macbuf + 4096) {
    252 				puts("4K macro buffer exceeded.");
    253 				goto bad;
    254 			}
    255 			break;
    256 		default:
    257 			warnx("Unknown .netrc keyword %s", tokval);
    258 			break;
    259 		}
    260 		goto done;
    261 	}
    262 done:
    263 	(void)fclose(cfile);
    264 	return (0);
    265 bad:
    266 	(void)fclose(cfile);
    267 	return (-1);
    268 }
    269 
    270 static int
    271 token()
    272 {
    273 	char *cp;
    274 	int c;
    275 	struct toktab *t;
    276 
    277 	if (feof(cfile) || ferror(cfile))
    278 		return (0);
    279 	while ((c = getc(cfile)) != EOF &&
    280 	    (c == '\n' || c == '\t' || c == ' ' || c == ','))
    281 		continue;
    282 	if (c == EOF)
    283 		return (0);
    284 	cp = tokval;
    285 	if (c == '"') {
    286 		while ((c = getc(cfile)) != EOF && c != '"') {
    287 			if (c == '\\')
    288 				c = getc(cfile);
    289 			*cp++ = c;
    290 		}
    291 	} else {
    292 		*cp++ = c;
    293 		while ((c = getc(cfile)) != EOF
    294 		    && c != '\n' && c != '\t' && c != ' ' && c != ',') {
    295 			if (c == '\\')
    296 				c = getc(cfile);
    297 			*cp++ = c;
    298 		}
    299 	}
    300 	*cp = 0;
    301 	if (tokval[0] == 0)
    302 		return (0);
    303 	for (t = toktab; t->tokstr; t++)
    304 		if (!strcmp(t->tokstr, tokval))
    305 			return (t->tval);
    306 	return (ID);
    307 }
    308