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