xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.bin/ftp/ruserpass.c (revision 2321:cd490d070379)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*2321Ssp149894  * Common Development and Distribution License (the "License").
6*2321Ssp149894  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*2321Ssp149894  *	Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  *	Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	All Rights Reserved  	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  *	University Copyright- Copyright (c) 1982, 1986, 1988
310Sstevel@tonic-gate  *	The Regents of the University of California
320Sstevel@tonic-gate  *	All Rights Reserved
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  *	University Acknowledgment- Portions of this document are derived from
350Sstevel@tonic-gate  *	software developed by the University of California, Berkeley, and its
360Sstevel@tonic-gate  *	contributors.
370Sstevel@tonic-gate  */
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include "ftp_var.h"
420Sstevel@tonic-gate 
430Sstevel@tonic-gate static	FILE *cfile;
440Sstevel@tonic-gate 
450Sstevel@tonic-gate static int rnetrc(char *host, char **aname, char **apass, char **aacct);
460Sstevel@tonic-gate static int token(void);
470Sstevel@tonic-gate 
480Sstevel@tonic-gate int
ruserpass(char * host,char ** aname,char ** apass,char ** aacct)490Sstevel@tonic-gate ruserpass(char *host, char **aname, char **apass, char **aacct)
500Sstevel@tonic-gate {
510Sstevel@tonic-gate #if 0
520Sstevel@tonic-gate 	renv(host, aname, apass, aacct);
530Sstevel@tonic-gate 	if (*aname == 0 || *apass == 0)
540Sstevel@tonic-gate #endif
550Sstevel@tonic-gate 		return (rnetrc(host, aname, apass, aacct));
560Sstevel@tonic-gate }
570Sstevel@tonic-gate 
580Sstevel@tonic-gate #define	DEFAULT	1
590Sstevel@tonic-gate #define	LOGIN	2
600Sstevel@tonic-gate #define	PASSWD	3
610Sstevel@tonic-gate #define	ACCOUNT 4
620Sstevel@tonic-gate #define	MACDEF	5
63*2321Ssp149894 #define	SKIPSYST	6
640Sstevel@tonic-gate #define	ID	10
650Sstevel@tonic-gate #define	MACHINE	11
660Sstevel@tonic-gate 
670Sstevel@tonic-gate static char tokval[100];
680Sstevel@tonic-gate 
690Sstevel@tonic-gate static struct toktab {
700Sstevel@tonic-gate 	char *tokstr;
710Sstevel@tonic-gate 	int tval;
720Sstevel@tonic-gate } toktab[] = {
730Sstevel@tonic-gate 	"default",	DEFAULT,
740Sstevel@tonic-gate 	"login",	LOGIN,
750Sstevel@tonic-gate 	"password",	PASSWD,
760Sstevel@tonic-gate 	"account",	ACCOUNT,
770Sstevel@tonic-gate 	"machine",	MACHINE,
780Sstevel@tonic-gate 	"macdef",	MACDEF,
79*2321Ssp149894 	"skipsyst",	SKIPSYST,
800Sstevel@tonic-gate 	0,		0
810Sstevel@tonic-gate };
820Sstevel@tonic-gate 
830Sstevel@tonic-gate static int
rnetrc(char * host,char ** aname,char ** apass,char ** aacct)840Sstevel@tonic-gate rnetrc(char *host, char **aname, char **apass, char **aacct)
850Sstevel@tonic-gate {
860Sstevel@tonic-gate 	char *hdir, buf[PATH_MAX+1], *tmp;
870Sstevel@tonic-gate 	int t, i, c;
880Sstevel@tonic-gate 	struct stat stb;
890Sstevel@tonic-gate 	extern int errno;
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	hdir = getenv("HOME");
920Sstevel@tonic-gate 	if (hdir == NULL)
930Sstevel@tonic-gate 		hdir = ".";
940Sstevel@tonic-gate 	if (snprintf(buf, sizeof (buf), "%s/.netrc", hdir) >= sizeof (buf)) {
950Sstevel@tonic-gate 		fprintf(stderr, ".netrc: %s\n", strerror(ENAMETOOLONG));
960Sstevel@tonic-gate 		exit(1);
970Sstevel@tonic-gate 	}
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	cfile = fopen(buf, "r");
1000Sstevel@tonic-gate 	if (cfile == NULL) {
1010Sstevel@tonic-gate 		if (errno != ENOENT)
1020Sstevel@tonic-gate 			perror(buf);
1030Sstevel@tonic-gate 		return (0);
1040Sstevel@tonic-gate 	}
1050Sstevel@tonic-gate next:
1060Sstevel@tonic-gate 	while ((t = token()))
1070Sstevel@tonic-gate 		switch (t) {
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	case MACHINE:
1100Sstevel@tonic-gate 		if (token() != ID || strcmp(host, tokval))
1110Sstevel@tonic-gate 			continue;
1120Sstevel@tonic-gate 		/* "machine name" matches host */
1130Sstevel@tonic-gate 		/* FALLTHROUGH */
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	case DEFAULT:
1160Sstevel@tonic-gate 		/* "default" matches any host */
1170Sstevel@tonic-gate 		while (((t = token()) != 0) && t != MACHINE && t != DEFAULT)
1180Sstevel@tonic-gate 			switch (t) {
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 		case LOGIN:
1210Sstevel@tonic-gate 			if (token())
1220Sstevel@tonic-gate 				if (*aname == 0) {
1230Sstevel@tonic-gate 					*aname = malloc((unsigned)
1240Sstevel@tonic-gate 					    strlen(tokval) + 1);
1250Sstevel@tonic-gate 					if (*aname == NULL) {
1260Sstevel@tonic-gate 						fprintf(stderr,
1270Sstevel@tonic-gate 						    "Error - out of VM\n");
1280Sstevel@tonic-gate 						exit(1);
1290Sstevel@tonic-gate 					}
1300Sstevel@tonic-gate 					(void) strcpy(*aname, tokval);
1310Sstevel@tonic-gate 				} else {
1320Sstevel@tonic-gate 					if (strcmp(*aname, tokval))
1330Sstevel@tonic-gate 						goto next;
1340Sstevel@tonic-gate 				}
1350Sstevel@tonic-gate 			break;
1360Sstevel@tonic-gate 		case PASSWD:
1370Sstevel@tonic-gate 			if (fstat(fileno(cfile), &stb) >= 0 &&
1380Sstevel@tonic-gate 			    (stb.st_mode & 077) != 0) {
1390Sstevel@tonic-gate 				fprintf(stderr, "Error - .netrc file not "
1400Sstevel@tonic-gate 				    "correct mode.\n");
1410Sstevel@tonic-gate 				fprintf(stderr, "Remove password or correct "
1420Sstevel@tonic-gate 				    "mode.\n");
1430Sstevel@tonic-gate 				return (-1);
1440Sstevel@tonic-gate 			}
1450Sstevel@tonic-gate 			if (token() && *apass == 0) {
1460Sstevel@tonic-gate 				*apass = malloc((unsigned)strlen(tokval) + 1);
1470Sstevel@tonic-gate 				if (*apass == NULL) {
1480Sstevel@tonic-gate 					fprintf(stderr, "Error - out of VM\n");
1490Sstevel@tonic-gate 					exit(1);
1500Sstevel@tonic-gate 				}
1510Sstevel@tonic-gate 				(void) strcpy(*apass, tokval);
1520Sstevel@tonic-gate 			}
1530Sstevel@tonic-gate 			break;
1540Sstevel@tonic-gate 		case ACCOUNT:
1550Sstevel@tonic-gate 			if (fstat(fileno(cfile), &stb) >= 0 &&
1560Sstevel@tonic-gate 			    (stb.st_mode & 077) != 0) {
1570Sstevel@tonic-gate 				fprintf(stderr, "Error - .netrc file not "
1580Sstevel@tonic-gate 				    "correct mode.\n");
1590Sstevel@tonic-gate 				fprintf(stderr, "Remove account or correct "
1600Sstevel@tonic-gate 				    "mode.\n");
1610Sstevel@tonic-gate 				return (-1);
1620Sstevel@tonic-gate 			}
1630Sstevel@tonic-gate 			if (token() && *aacct == 0) {
1640Sstevel@tonic-gate 				*aacct = malloc((unsigned)strlen(tokval) + 1);
1650Sstevel@tonic-gate 				if (*aacct == NULL) {
1660Sstevel@tonic-gate 					fprintf(stderr, "Error - out of VM\n");
1670Sstevel@tonic-gate 					exit(1);
1680Sstevel@tonic-gate 				}
1690Sstevel@tonic-gate 				(void) strcpy(*aacct, tokval);
1700Sstevel@tonic-gate 			}
1710Sstevel@tonic-gate 			break;
1720Sstevel@tonic-gate 		case MACDEF:
1730Sstevel@tonic-gate 			if (proxy) {
1740Sstevel@tonic-gate 				return (0);
1750Sstevel@tonic-gate 			}
1760Sstevel@tonic-gate 			while ((c = getc(cfile)) != EOF && c == ' ' ||
1770Sstevel@tonic-gate 			    c == '\t');
1780Sstevel@tonic-gate 			if (c == EOF || c == '\n') {
1790Sstevel@tonic-gate 				printf("Missing macdef name argument.\n");
1800Sstevel@tonic-gate 				return (-1);
1810Sstevel@tonic-gate 			}
1820Sstevel@tonic-gate 			if (macnum == 16) {
1830Sstevel@tonic-gate 				printf("Limit of 16 macros have already "
1840Sstevel@tonic-gate 				    "been defined\n");
1850Sstevel@tonic-gate 				return (-1);
1860Sstevel@tonic-gate 			}
1870Sstevel@tonic-gate 			tmp = macros[macnum].mac_name;
1880Sstevel@tonic-gate 			*tmp++ = c;
1890Sstevel@tonic-gate 			for (i = 0; i < 8 && (c = getc(cfile)) != EOF &&
1900Sstevel@tonic-gate 			    !isspace(c); ++i) {
1910Sstevel@tonic-gate 				*tmp++ = c;
1920Sstevel@tonic-gate 			}
1930Sstevel@tonic-gate 			if (c == EOF) {
1940Sstevel@tonic-gate 				printf("Macro definition for `%s` missing "
1950Sstevel@tonic-gate 				    "null line terminator.\n",
1960Sstevel@tonic-gate 				    macros[macnum].mac_name);
1970Sstevel@tonic-gate 				return (-1);
1980Sstevel@tonic-gate 			}
1990Sstevel@tonic-gate 			*tmp = '\0';
2000Sstevel@tonic-gate 			if (c != '\n') {
2010Sstevel@tonic-gate 				while ((c = getc(cfile)) != EOF && c != '\n');
2020Sstevel@tonic-gate 			}
2030Sstevel@tonic-gate 			if (c == EOF) {
2040Sstevel@tonic-gate 				printf("Macro definition for `%s` missing "
2050Sstevel@tonic-gate 				    "null line terminator.\n",
2060Sstevel@tonic-gate 				    macros[macnum].mac_name);
2070Sstevel@tonic-gate 				return (-1);
2080Sstevel@tonic-gate 			}
2090Sstevel@tonic-gate 			if (macnum == 0) {
2100Sstevel@tonic-gate 				macros[macnum].mac_start = macbuf;
2110Sstevel@tonic-gate 			} else {
2120Sstevel@tonic-gate 				macros[macnum].mac_start =
2130Sstevel@tonic-gate 				    macros[macnum-1].mac_end + 1;
2140Sstevel@tonic-gate 			}
2150Sstevel@tonic-gate 			tmp = macros[macnum].mac_start;
2160Sstevel@tonic-gate 			while (tmp != macbuf + 4096) {
2170Sstevel@tonic-gate 				if ((c = getc(cfile)) == EOF) {
2180Sstevel@tonic-gate 				printf("Macro definition for `%s` missing "
2190Sstevel@tonic-gate 				    "null line terminator.\n",
2200Sstevel@tonic-gate 				    macros[macnum].mac_name);
2210Sstevel@tonic-gate 					return (-1);
2220Sstevel@tonic-gate 				}
2230Sstevel@tonic-gate 				*tmp = c;
2240Sstevel@tonic-gate 				if (*tmp == '\n') {
2250Sstevel@tonic-gate 					if (*(tmp-1) == '\0') {
2260Sstevel@tonic-gate 						macros[macnum++].mac_end =
2270Sstevel@tonic-gate 						    tmp - 1;
2280Sstevel@tonic-gate 						break;
2290Sstevel@tonic-gate 					}
2300Sstevel@tonic-gate 					*tmp = '\0';
2310Sstevel@tonic-gate 				}
2320Sstevel@tonic-gate 				tmp++;
2330Sstevel@tonic-gate 			}
2340Sstevel@tonic-gate 			if (tmp == macbuf + 4096) {
2350Sstevel@tonic-gate 				printf("4K macro buffer exceeded\n");
2360Sstevel@tonic-gate 				return (-1);
2370Sstevel@tonic-gate 			}
2380Sstevel@tonic-gate 			if (*macros[macnum - 1].mac_start == '\n') {
2390Sstevel@tonic-gate 				printf("Macro definition for `%s` is empty, "
2400Sstevel@tonic-gate 				    "macro not stored.\n",
2410Sstevel@tonic-gate 					macros[--macnum].mac_name);
2420Sstevel@tonic-gate 			}
2430Sstevel@tonic-gate 			break;
244*2321Ssp149894 		case SKIPSYST:
245*2321Ssp149894 			skipsyst = 1;
246*2321Ssp149894 			break;
2470Sstevel@tonic-gate 		default:
2480Sstevel@tonic-gate 			fprintf(stderr, "Unknown .netrc keyword %s\n", tokval);
2490Sstevel@tonic-gate 			break;
2500Sstevel@tonic-gate 		}
2510Sstevel@tonic-gate 		goto done;
2520Sstevel@tonic-gate 	}
2530Sstevel@tonic-gate done:
2540Sstevel@tonic-gate 	(void) fclose(cfile);
2550Sstevel@tonic-gate 	return (0);
2560Sstevel@tonic-gate }
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate static int
token(void)2590Sstevel@tonic-gate token(void)
2600Sstevel@tonic-gate {
2610Sstevel@tonic-gate 	char *cp;
2620Sstevel@tonic-gate 	int c;
2630Sstevel@tonic-gate 	struct toktab *t;
2640Sstevel@tonic-gate 	int	len;
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 	if (feof(cfile))
2670Sstevel@tonic-gate 		return (0);
2680Sstevel@tonic-gate 	while ((c = fgetwc(cfile)) != EOF &&
2690Sstevel@tonic-gate 	    (c == '\n' || c == '\t' || c == ' ' || c == ','))
2700Sstevel@tonic-gate 		continue;
2710Sstevel@tonic-gate 	if (c == EOF)
2720Sstevel@tonic-gate 		return (0);
2730Sstevel@tonic-gate 	cp = tokval;
2740Sstevel@tonic-gate 	if (c == '"') {
2750Sstevel@tonic-gate 		while ((c = fgetwc(cfile)) != EOF && c != '"') {
2760Sstevel@tonic-gate 			if (c == '\\')
2770Sstevel@tonic-gate 				c = fgetwc(cfile);
2780Sstevel@tonic-gate 			if ((len = wctomb(cp, c)) <= 0) {
2790Sstevel@tonic-gate 				len = 1;
2800Sstevel@tonic-gate 				*cp = (unsigned char)c;
2810Sstevel@tonic-gate 			}
2820Sstevel@tonic-gate 			cp += len;
2830Sstevel@tonic-gate 		}
2840Sstevel@tonic-gate 	} else {
2850Sstevel@tonic-gate 		if ((len = wctomb(cp, c)) <= 0) {
2860Sstevel@tonic-gate 			*cp = (unsigned char)c;
2870Sstevel@tonic-gate 			len = 1;
2880Sstevel@tonic-gate 		}
2890Sstevel@tonic-gate 		cp += len;
2900Sstevel@tonic-gate 		while ((c = fgetwc(cfile)) != EOF && c != '\n' && c != '\t' &&
2910Sstevel@tonic-gate 		    c != ' ' && c != ',') {
2920Sstevel@tonic-gate 			if (c == '\\')
2930Sstevel@tonic-gate 				c = fgetwc(cfile);
2940Sstevel@tonic-gate 			if ((len = wctomb(cp, c)) <= 0) {
2950Sstevel@tonic-gate 				len = 1;
2960Sstevel@tonic-gate 				*cp = (unsigned char)c;
2970Sstevel@tonic-gate 			}
2980Sstevel@tonic-gate 			cp += len;
2990Sstevel@tonic-gate 		}
3000Sstevel@tonic-gate 	}
3010Sstevel@tonic-gate 	*cp = 0;
3020Sstevel@tonic-gate 	if (tokval[0] == 0)
3030Sstevel@tonic-gate 		return (0);
3040Sstevel@tonic-gate 	for (t = toktab; t->tokstr; t++)
3050Sstevel@tonic-gate 		if (strcmp(t->tokstr, tokval) == 0)
3060Sstevel@tonic-gate 			return (t->tval);
3070Sstevel@tonic-gate 	return (ID);
3080Sstevel@tonic-gate }
309