17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0b6880ccSsp  * Common Development and Distribution License (the "License").
6*0b6880ccSsp  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*0b6880ccSsp  *	Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  *	Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  *	University Copyright- Copyright (c) 1982, 1986, 1988
317c478bd9Sstevel@tonic-gate  *	The Regents of the University of California
327c478bd9Sstevel@tonic-gate  *	All Rights Reserved
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  *	University Acknowledgment- Portions of this document are derived from
357c478bd9Sstevel@tonic-gate  *	software developed by the University of California, Berkeley, and its
367c478bd9Sstevel@tonic-gate  *	contributors.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include "ftp_var.h"
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate static	FILE *cfile;
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate static int rnetrc(char *host, char **aname, char **apass, char **aacct);
447c478bd9Sstevel@tonic-gate static int token(void);
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate int
ruserpass(char * host,char ** aname,char ** apass,char ** aacct)477c478bd9Sstevel@tonic-gate ruserpass(char *host, char **aname, char **apass, char **aacct)
487c478bd9Sstevel@tonic-gate {
497c478bd9Sstevel@tonic-gate #if 0
507c478bd9Sstevel@tonic-gate 	renv(host, aname, apass, aacct);
517c478bd9Sstevel@tonic-gate 	if (*aname == 0 || *apass == 0)
527c478bd9Sstevel@tonic-gate #endif
537c478bd9Sstevel@tonic-gate 		return (rnetrc(host, aname, apass, aacct));
547c478bd9Sstevel@tonic-gate }
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #define	DEFAULT	1
577c478bd9Sstevel@tonic-gate #define	LOGIN	2
587c478bd9Sstevel@tonic-gate #define	PASSWD	3
597c478bd9Sstevel@tonic-gate #define	ACCOUNT 4
607c478bd9Sstevel@tonic-gate #define	MACDEF	5
61*0b6880ccSsp #define	SKIPSYST	6
627c478bd9Sstevel@tonic-gate #define	ID	10
637c478bd9Sstevel@tonic-gate #define	MACHINE	11
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate static char tokval[100];
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate static struct toktab {
687c478bd9Sstevel@tonic-gate 	char *tokstr;
697c478bd9Sstevel@tonic-gate 	int tval;
707c478bd9Sstevel@tonic-gate } toktab[] = {
717c478bd9Sstevel@tonic-gate 	"default",	DEFAULT,
727c478bd9Sstevel@tonic-gate 	"login",	LOGIN,
737c478bd9Sstevel@tonic-gate 	"password",	PASSWD,
747c478bd9Sstevel@tonic-gate 	"account",	ACCOUNT,
757c478bd9Sstevel@tonic-gate 	"machine",	MACHINE,
767c478bd9Sstevel@tonic-gate 	"macdef",	MACDEF,
77*0b6880ccSsp 	"skipsyst",	SKIPSYST,
787c478bd9Sstevel@tonic-gate 	0,		0
797c478bd9Sstevel@tonic-gate };
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate static int
rnetrc(char * host,char ** aname,char ** apass,char ** aacct)827c478bd9Sstevel@tonic-gate rnetrc(char *host, char **aname, char **apass, char **aacct)
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate 	char *hdir, buf[PATH_MAX+1], *tmp;
857c478bd9Sstevel@tonic-gate 	int t, i, c;
867c478bd9Sstevel@tonic-gate 	struct stat stb;
877c478bd9Sstevel@tonic-gate 	extern int errno;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	hdir = getenv("HOME");
907c478bd9Sstevel@tonic-gate 	if (hdir == NULL)
917c478bd9Sstevel@tonic-gate 		hdir = ".";
927c478bd9Sstevel@tonic-gate 	if (snprintf(buf, sizeof (buf), "%s/.netrc", hdir) >= sizeof (buf)) {
937c478bd9Sstevel@tonic-gate 		fprintf(stderr, ".netrc: %s\n", strerror(ENAMETOOLONG));
947c478bd9Sstevel@tonic-gate 		exit(1);
957c478bd9Sstevel@tonic-gate 	}
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	cfile = fopen(buf, "r");
987c478bd9Sstevel@tonic-gate 	if (cfile == NULL) {
997c478bd9Sstevel@tonic-gate 		if (errno != ENOENT)
1007c478bd9Sstevel@tonic-gate 			perror(buf);
1017c478bd9Sstevel@tonic-gate 		return (0);
1027c478bd9Sstevel@tonic-gate 	}
1037c478bd9Sstevel@tonic-gate next:
1047c478bd9Sstevel@tonic-gate 	while ((t = token()))
1057c478bd9Sstevel@tonic-gate 		switch (t) {
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	case MACHINE:
1087c478bd9Sstevel@tonic-gate 		if (token() != ID || strcmp(host, tokval))
1097c478bd9Sstevel@tonic-gate 			continue;
1107c478bd9Sstevel@tonic-gate 		/* "machine name" matches host */
1117c478bd9Sstevel@tonic-gate 		/* FALLTHROUGH */
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	case DEFAULT:
1147c478bd9Sstevel@tonic-gate 		/* "default" matches any host */
1157c478bd9Sstevel@tonic-gate 		while (((t = token()) != 0) && t != MACHINE && t != DEFAULT)
1167c478bd9Sstevel@tonic-gate 			switch (t) {
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 		case LOGIN:
1197c478bd9Sstevel@tonic-gate 			if (token())
1207c478bd9Sstevel@tonic-gate 				if (*aname == 0) {
1217c478bd9Sstevel@tonic-gate 					*aname = malloc((unsigned)
1227c478bd9Sstevel@tonic-gate 					    strlen(tokval) + 1);
1237c478bd9Sstevel@tonic-gate 					if (*aname == NULL) {
1247c478bd9Sstevel@tonic-gate 						fprintf(stderr,
1257c478bd9Sstevel@tonic-gate 						    "Error - out of VM\n");
1267c478bd9Sstevel@tonic-gate 						exit(1);
1277c478bd9Sstevel@tonic-gate 					}
1287c478bd9Sstevel@tonic-gate 					(void) strcpy(*aname, tokval);
1297c478bd9Sstevel@tonic-gate 				} else {
1307c478bd9Sstevel@tonic-gate 					if (strcmp(*aname, tokval))
1317c478bd9Sstevel@tonic-gate 						goto next;
1327c478bd9Sstevel@tonic-gate 				}
1337c478bd9Sstevel@tonic-gate 			break;
1347c478bd9Sstevel@tonic-gate 		case PASSWD:
1357c478bd9Sstevel@tonic-gate 			if (fstat(fileno(cfile), &stb) >= 0 &&
1367c478bd9Sstevel@tonic-gate 			    (stb.st_mode & 077) != 0) {
1377c478bd9Sstevel@tonic-gate 				fprintf(stderr, "Error - .netrc file not "
1387c478bd9Sstevel@tonic-gate 				    "correct mode.\n");
1397c478bd9Sstevel@tonic-gate 				fprintf(stderr, "Remove password or correct "
1407c478bd9Sstevel@tonic-gate 				    "mode.\n");
1417c478bd9Sstevel@tonic-gate 				return (-1);
1427c478bd9Sstevel@tonic-gate 			}
1437c478bd9Sstevel@tonic-gate 			if (token() && *apass == 0) {
1447c478bd9Sstevel@tonic-gate 				*apass = malloc((unsigned)strlen(tokval) + 1);
1457c478bd9Sstevel@tonic-gate 				if (*apass == NULL) {
1467c478bd9Sstevel@tonic-gate 					fprintf(stderr, "Error - out of VM\n");
1477c478bd9Sstevel@tonic-gate 					exit(1);
1487c478bd9Sstevel@tonic-gate 				}
1497c478bd9Sstevel@tonic-gate 				(void) strcpy(*apass, tokval);
1507c478bd9Sstevel@tonic-gate 			}
1517c478bd9Sstevel@tonic-gate 			break;
1527c478bd9Sstevel@tonic-gate 		case ACCOUNT:
1537c478bd9Sstevel@tonic-gate 			if (fstat(fileno(cfile), &stb) >= 0 &&
1547c478bd9Sstevel@tonic-gate 			    (stb.st_mode & 077) != 0) {
1557c478bd9Sstevel@tonic-gate 				fprintf(stderr, "Error - .netrc file not "
1567c478bd9Sstevel@tonic-gate 				    "correct mode.\n");
1577c478bd9Sstevel@tonic-gate 				fprintf(stderr, "Remove account or correct "
1587c478bd9Sstevel@tonic-gate 				    "mode.\n");
1597c478bd9Sstevel@tonic-gate 				return (-1);
1607c478bd9Sstevel@tonic-gate 			}
1617c478bd9Sstevel@tonic-gate 			if (token() && *aacct == 0) {
1627c478bd9Sstevel@tonic-gate 				*aacct = malloc((unsigned)strlen(tokval) + 1);
1637c478bd9Sstevel@tonic-gate 				if (*aacct == NULL) {
1647c478bd9Sstevel@tonic-gate 					fprintf(stderr, "Error - out of VM\n");
1657c478bd9Sstevel@tonic-gate 					exit(1);
1667c478bd9Sstevel@tonic-gate 				}
1677c478bd9Sstevel@tonic-gate 				(void) strcpy(*aacct, tokval);
1687c478bd9Sstevel@tonic-gate 			}
1697c478bd9Sstevel@tonic-gate 			break;
1707c478bd9Sstevel@tonic-gate 		case MACDEF:
1717c478bd9Sstevel@tonic-gate 			if (proxy) {
1727c478bd9Sstevel@tonic-gate 				return (0);
1737c478bd9Sstevel@tonic-gate 			}
1747c478bd9Sstevel@tonic-gate 			while ((c = getc(cfile)) != EOF && c == ' ' ||
1757c478bd9Sstevel@tonic-gate 			    c == '\t');
1767c478bd9Sstevel@tonic-gate 			if (c == EOF || c == '\n') {
1777c478bd9Sstevel@tonic-gate 				printf("Missing macdef name argument.\n");
1787c478bd9Sstevel@tonic-gate 				return (-1);
1797c478bd9Sstevel@tonic-gate 			}
1807c478bd9Sstevel@tonic-gate 			if (macnum == 16) {
1817c478bd9Sstevel@tonic-gate 				printf("Limit of 16 macros have already "
1827c478bd9Sstevel@tonic-gate 				    "been defined\n");
1837c478bd9Sstevel@tonic-gate 				return (-1);
1847c478bd9Sstevel@tonic-gate 			}
1857c478bd9Sstevel@tonic-gate 			tmp = macros[macnum].mac_name;
1867c478bd9Sstevel@tonic-gate 			*tmp++ = c;
1877c478bd9Sstevel@tonic-gate 			for (i = 0; i < 8 && (c = getc(cfile)) != EOF &&
1887c478bd9Sstevel@tonic-gate 			    !isspace(c); ++i) {
1897c478bd9Sstevel@tonic-gate 				*tmp++ = c;
1907c478bd9Sstevel@tonic-gate 			}
1917c478bd9Sstevel@tonic-gate 			if (c == EOF) {
1927c478bd9Sstevel@tonic-gate 				printf("Macro definition for `%s` missing "
1937c478bd9Sstevel@tonic-gate 				    "null line terminator.\n",
1947c478bd9Sstevel@tonic-gate 				    macros[macnum].mac_name);
1957c478bd9Sstevel@tonic-gate 				return (-1);
1967c478bd9Sstevel@tonic-gate 			}
1977c478bd9Sstevel@tonic-gate 			*tmp = '\0';
1987c478bd9Sstevel@tonic-gate 			if (c != '\n') {
1997c478bd9Sstevel@tonic-gate 				while ((c = getc(cfile)) != EOF && c != '\n');
2007c478bd9Sstevel@tonic-gate 			}
2017c478bd9Sstevel@tonic-gate 			if (c == EOF) {
2027c478bd9Sstevel@tonic-gate 				printf("Macro definition for `%s` missing "
2037c478bd9Sstevel@tonic-gate 				    "null line terminator.\n",
2047c478bd9Sstevel@tonic-gate 				    macros[macnum].mac_name);
2057c478bd9Sstevel@tonic-gate 				return (-1);
2067c478bd9Sstevel@tonic-gate 			}
2077c478bd9Sstevel@tonic-gate 			if (macnum == 0) {
2087c478bd9Sstevel@tonic-gate 				macros[macnum].mac_start = macbuf;
2097c478bd9Sstevel@tonic-gate 			} else {
2107c478bd9Sstevel@tonic-gate 				macros[macnum].mac_start =
2117c478bd9Sstevel@tonic-gate 				    macros[macnum-1].mac_end + 1;
2127c478bd9Sstevel@tonic-gate 			}
2137c478bd9Sstevel@tonic-gate 			tmp = macros[macnum].mac_start;
2147c478bd9Sstevel@tonic-gate 			while (tmp != macbuf + 4096) {
2157c478bd9Sstevel@tonic-gate 				if ((c = getc(cfile)) == EOF) {
2167c478bd9Sstevel@tonic-gate 				printf("Macro definition for `%s` missing "
2177c478bd9Sstevel@tonic-gate 				    "null line terminator.\n",
2187c478bd9Sstevel@tonic-gate 				    macros[macnum].mac_name);
2197c478bd9Sstevel@tonic-gate 					return (-1);
2207c478bd9Sstevel@tonic-gate 				}
2217c478bd9Sstevel@tonic-gate 				*tmp = c;
2227c478bd9Sstevel@tonic-gate 				if (*tmp == '\n') {
2237c478bd9Sstevel@tonic-gate 					if (*(tmp-1) == '\0') {
2247c478bd9Sstevel@tonic-gate 						macros[macnum++].mac_end =
2257c478bd9Sstevel@tonic-gate 						    tmp - 1;
2267c478bd9Sstevel@tonic-gate 						break;
2277c478bd9Sstevel@tonic-gate 					}
2287c478bd9Sstevel@tonic-gate 					*tmp = '\0';
2297c478bd9Sstevel@tonic-gate 				}
2307c478bd9Sstevel@tonic-gate 				tmp++;
2317c478bd9Sstevel@tonic-gate 			}
2327c478bd9Sstevel@tonic-gate 			if (tmp == macbuf + 4096) {
2337c478bd9Sstevel@tonic-gate 				printf("4K macro buffer exceeded\n");
2347c478bd9Sstevel@tonic-gate 				return (-1);
2357c478bd9Sstevel@tonic-gate 			}
2367c478bd9Sstevel@tonic-gate 			if (*macros[macnum - 1].mac_start == '\n') {
2377c478bd9Sstevel@tonic-gate 				printf("Macro definition for `%s` is empty, "
2387c478bd9Sstevel@tonic-gate 				    "macro not stored.\n",
2397c478bd9Sstevel@tonic-gate 					macros[--macnum].mac_name);
2407c478bd9Sstevel@tonic-gate 			}
2417c478bd9Sstevel@tonic-gate 			break;
242*0b6880ccSsp 		case SKIPSYST:
243*0b6880ccSsp 			skipsyst = 1;
244*0b6880ccSsp 			break;
2457c478bd9Sstevel@tonic-gate 		default:
2467c478bd9Sstevel@tonic-gate 			fprintf(stderr, "Unknown .netrc keyword %s\n", tokval);
2477c478bd9Sstevel@tonic-gate 			break;
2487c478bd9Sstevel@tonic-gate 		}
2497c478bd9Sstevel@tonic-gate 		goto done;
2507c478bd9Sstevel@tonic-gate 	}
2517c478bd9Sstevel@tonic-gate done:
2527c478bd9Sstevel@tonic-gate 	(void) fclose(cfile);
2537c478bd9Sstevel@tonic-gate 	return (0);
2547c478bd9Sstevel@tonic-gate }
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate static int
token(void)2577c478bd9Sstevel@tonic-gate token(void)
2587c478bd9Sstevel@tonic-gate {
2597c478bd9Sstevel@tonic-gate 	char *cp;
2607c478bd9Sstevel@tonic-gate 	int c;
2617c478bd9Sstevel@tonic-gate 	struct toktab *t;
2627c478bd9Sstevel@tonic-gate 	int	len;
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	if (feof(cfile))
2657c478bd9Sstevel@tonic-gate 		return (0);
2667c478bd9Sstevel@tonic-gate 	while ((c = fgetwc(cfile)) != EOF &&
2677c478bd9Sstevel@tonic-gate 	    (c == '\n' || c == '\t' || c == ' ' || c == ','))
2687c478bd9Sstevel@tonic-gate 		continue;
2697c478bd9Sstevel@tonic-gate 	if (c == EOF)
2707c478bd9Sstevel@tonic-gate 		return (0);
2717c478bd9Sstevel@tonic-gate 	cp = tokval;
2727c478bd9Sstevel@tonic-gate 	if (c == '"') {
2737c478bd9Sstevel@tonic-gate 		while ((c = fgetwc(cfile)) != EOF && c != '"') {
2747c478bd9Sstevel@tonic-gate 			if (c == '\\')
2757c478bd9Sstevel@tonic-gate 				c = fgetwc(cfile);
2767c478bd9Sstevel@tonic-gate 			if ((len = wctomb(cp, c)) <= 0) {
2777c478bd9Sstevel@tonic-gate 				len = 1;
2787c478bd9Sstevel@tonic-gate 				*cp = (unsigned char)c;
2797c478bd9Sstevel@tonic-gate 			}
2807c478bd9Sstevel@tonic-gate 			cp += len;
2817c478bd9Sstevel@tonic-gate 		}
2827c478bd9Sstevel@tonic-gate 	} else {
2837c478bd9Sstevel@tonic-gate 		if ((len = wctomb(cp, c)) <= 0) {
2847c478bd9Sstevel@tonic-gate 			*cp = (unsigned char)c;
2857c478bd9Sstevel@tonic-gate 			len = 1;
2867c478bd9Sstevel@tonic-gate 		}
2877c478bd9Sstevel@tonic-gate 		cp += len;
2887c478bd9Sstevel@tonic-gate 		while ((c = fgetwc(cfile)) != EOF && c != '\n' && c != '\t' &&
2897c478bd9Sstevel@tonic-gate 		    c != ' ' && c != ',') {
2907c478bd9Sstevel@tonic-gate 			if (c == '\\')
2917c478bd9Sstevel@tonic-gate 				c = fgetwc(cfile);
2927c478bd9Sstevel@tonic-gate 			if ((len = wctomb(cp, c)) <= 0) {
2937c478bd9Sstevel@tonic-gate 				len = 1;
2947c478bd9Sstevel@tonic-gate 				*cp = (unsigned char)c;
2957c478bd9Sstevel@tonic-gate 			}
2967c478bd9Sstevel@tonic-gate 			cp += len;
2977c478bd9Sstevel@tonic-gate 		}
2987c478bd9Sstevel@tonic-gate 	}
2997c478bd9Sstevel@tonic-gate 	*cp = 0;
3007c478bd9Sstevel@tonic-gate 	if (tokval[0] == 0)
3017c478bd9Sstevel@tonic-gate 		return (0);
3027c478bd9Sstevel@tonic-gate 	for (t = toktab; t->tokstr; t++)
3037c478bd9Sstevel@tonic-gate 		if (strcmp(t->tokstr, tokval) == 0)
3047c478bd9Sstevel@tonic-gate 			return (t->tval);
3057c478bd9Sstevel@tonic-gate 	return (ID);
3067c478bd9Sstevel@tonic-gate }
307