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