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