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