Home | History | Annotate | Line # | Download | only in 4.3
ruserpass.c revision 1.6
      1 /*	$NetBSD: ruserpass.c,v 1.6 1999/09/16 11:45:47 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.6 1999/09/16 11:45:47 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 #ifdef _DIAGNOSTIC
    110 	if (host == NULL || aname == NULL || apass == NULL)
    111 		return (-1);
    112 #endif
    113 
    114 	hdir = getenv("HOME");
    115 	if (hdir == NULL)
    116 		hdir = ".";
    117 	if (strlen(hdir) + sizeof(".netrc") < sizeof(buf)) {
    118 		(void)snprintf(buf, sizeof buf, "%s/.netrc", hdir);
    119 	} else {
    120 		warnx("%s/.netrc: %s", hdir, strerror(ENAMETOOLONG));
    121 		return (0);
    122 	}
    123 	cfile = fopen(buf, "r");
    124 	if (cfile == NULL) {
    125 		if (errno != ENOENT)
    126 			warn("%s", buf);
    127 		return (0);
    128 	}
    129 	if (gethostname(myname, sizeof(myname)) < 0)
    130 		myname[0] = '\0';
    131 	else
    132 		myname[sizeof(myname) - 1] = '\0';
    133 	if ((mydomain = strchr(myname, '.')) == NULL)
    134 		mydomain = "";
    135 next:
    136 	while ((t = token()) != NULL) switch(t) {
    137 
    138 	case DEFAULT:
    139 		usedefault = 1;
    140 		/* FALLTHROUGH */
    141 
    142 	case MACH:
    143 		if (!usedefault) {
    144 			if (token() != ID)
    145 				continue;
    146 			/*
    147 			 * Allow match either for user's input host name
    148 			 * or official hostname.  Also allow match of
    149 			 * incompletely-specified host in local domain.
    150 			 */
    151 			if (strcasecmp(host, tokval) == 0)
    152 				goto match;
    153 			if ((tmp = strchr(host, '.')) != NULL &&
    154 			    strcasecmp(tmp, mydomain) == 0 &&
    155 			    strncasecmp(host, tokval,
    156 			    (size_t)(tmp - host)) == 0 &&
    157 			    tokval[tmp - host] == '\0')
    158 				goto match;
    159 			continue;
    160 		}
    161 	match:
    162 		while ((t = token()) && t != MACH && t != DEFAULT) switch(t) {
    163 
    164 		case LOGIN:
    165 			if (token()) {
    166 				if (*aname == NULL) {
    167 					*aname = strdup(tokval);
    168 					if (*aname == NULL)
    169 						err(1, "can't strdup *aname");
    170 				} else {
    171 					if (strcmp(*aname, tokval))
    172 						goto next;
    173 				}
    174 			}
    175 			break;
    176 		case PASSWD:
    177 			if ((*aname == NULL || strcmp(*aname, "anonymous")) &&
    178 			    fstat(fileno(cfile), &stb) >= 0 &&
    179 			    (stb.st_mode & 077) != 0) {
    180 	warnx("Error: .netrc file is readable by others.");
    181 	warnx("Remove password or make file unreadable by others.");
    182 				goto bad;
    183 			}
    184 			if (token() && *apass == NULL) {
    185 				*apass = strdup(tokval);
    186 				if (*apass == NULL)
    187 					err(1, "can't strdup *apass");
    188 			}
    189 			break;
    190 		case ACCOUNT:
    191 			if (fstat(fileno(cfile), &stb) >= 0
    192 			    && (stb.st_mode & 077) != 0) {
    193 	warnx("Error: .netrc file is readable by others.");
    194 	warnx("Remove account or make file unreadable by others.");
    195 				goto bad;
    196 			}
    197 			break;
    198 		case MACDEF:
    199 			while ((c=getc(cfile)) != EOF)
    200 				if (c != ' ' && c != '\t')
    201 					break;
    202 			if (c == EOF || c == '\n') {
    203 				puts("Missing macdef name argument.");
    204 				goto bad;
    205 			}
    206 			if (macnum == 16) {
    207 				puts(
    208 "Limit of 16 macros have already been defined.");
    209 				goto bad;
    210 			}
    211 			tmp = macros[macnum].mac_name;
    212 			*tmp++ = c;
    213 			for (i=0; i < 8 && (c=getc(cfile)) != EOF &&
    214 			    !isspace(c); ++i) {
    215 				*tmp++ = c;
    216 			}
    217 			if (c == EOF) {
    218 				puts(
    219 "Macro definition missing null line terminator.");
    220 				goto bad;
    221 			}
    222 			*tmp = '\0';
    223 			if (c != '\n') {
    224 				while ((c=getc(cfile)) != EOF && c != '\n');
    225 			}
    226 			if (c == EOF) {
    227 				puts(
    228 "Macro definition missing null line terminator.");
    229 				goto bad;
    230 			}
    231 			if (macnum == 0) {
    232 				macros[macnum].mac_start = macbuf;
    233 			}
    234 			else {
    235 				macros[macnum].mac_start =
    236 				    macros[macnum-1].mac_end + 1;
    237 			}
    238 			tmp = macros[macnum].mac_start;
    239 			while (tmp != macbuf + 4096) {
    240 				if ((c=getc(cfile)) == EOF) {
    241 				puts(
    242 "Macro definition missing null line terminator.");
    243 					goto bad;
    244 				}
    245 				*tmp = c;
    246 				if (*tmp == '\n') {
    247 					if (*(tmp-1) == '\0') {
    248 					   macros[macnum++].mac_end = tmp - 1;
    249 					   break;
    250 					}
    251 					*tmp = '\0';
    252 				}
    253 				tmp++;
    254 			}
    255 			if (tmp == macbuf + 4096) {
    256 				puts("4K macro buffer exceeded.");
    257 				goto bad;
    258 			}
    259 			break;
    260 		default:
    261 			warnx("Unknown .netrc keyword %s", tokval);
    262 			break;
    263 		}
    264 		goto done;
    265 	}
    266 done:
    267 	(void)fclose(cfile);
    268 	return (0);
    269 bad:
    270 	(void)fclose(cfile);
    271 	return (-1);
    272 }
    273 
    274 static int
    275 token()
    276 {
    277 	char *cp;
    278 	int c;
    279 	struct toktab *t;
    280 
    281 	if (feof(cfile) || ferror(cfile))
    282 		return (0);
    283 	while ((c = getc(cfile)) != EOF &&
    284 	    (c == '\n' || c == '\t' || c == ' ' || c == ','))
    285 		continue;
    286 	if (c == EOF)
    287 		return (0);
    288 	cp = tokval;
    289 	if (c == '"') {
    290 		while ((c = getc(cfile)) != EOF && c != '"') {
    291 			if (c == '\\')
    292 				c = getc(cfile);
    293 			*cp++ = c;
    294 		}
    295 	} else {
    296 		*cp++ = c;
    297 		while ((c = getc(cfile)) != EOF
    298 		    && c != '\n' && c != '\t' && c != ' ' && c != ',') {
    299 			if (c == '\\')
    300 				c = getc(cfile);
    301 			*cp++ = c;
    302 		}
    303 	}
    304 	*cp = 0;
    305 	if (tokval[0] == 0)
    306 		return (0);
    307 	for (t = toktab; t->tokstr; t++)
    308 		if (!strcmp(t->tokstr, tokval))
    309 			return (t->tval);
    310 	return (ID);
    311 }
    312