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
5004388ebScasper  * Common Development and Distribution License (the "License").
6004388ebScasper  * 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  */
217257d1b4Sraf 
227c478bd9Sstevel@tonic-gate /*
237257d1b4Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*00f1a4f4SToomas Soome /*	  All Rights Reserved	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
327c478bd9Sstevel@tonic-gate  * The Regents of the University of California
337c478bd9Sstevel@tonic-gate  * All Rights Reserved
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
367c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
377c478bd9Sstevel@tonic-gate  * contributors.
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include <stdio.h>
417c478bd9Sstevel@tonic-gate #include <ctype.h>
427c478bd9Sstevel@tonic-gate #include <sys/types.h>
437c478bd9Sstevel@tonic-gate #include <sys/stat.h>
447c478bd9Sstevel@tonic-gate #include <errno.h>
457c478bd9Sstevel@tonic-gate #include <unistd.h>
467c478bd9Sstevel@tonic-gate #include <strings.h>
477c478bd9Sstevel@tonic-gate #include <stdlib.h>
487c478bd9Sstevel@tonic-gate #include <libintl.h>
49*00f1a4f4SToomas Soome #include <limits.h>
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #ifdef SYSV
527c478bd9Sstevel@tonic-gate #define	index	strchr
537c478bd9Sstevel@tonic-gate #endif /* SYSV */
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate static void rnetrc(const char *host, char **aname, char **apass);
567c478bd9Sstevel@tonic-gate static int token();
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #define	DEFAULT	1
597c478bd9Sstevel@tonic-gate #define	LOGIN	2
607c478bd9Sstevel@tonic-gate #define	PASSWD	3
617c478bd9Sstevel@tonic-gate #define	NOTIFY	4
627c478bd9Sstevel@tonic-gate #define	WRITE	5
637c478bd9Sstevel@tonic-gate #define	YES	6
647c478bd9Sstevel@tonic-gate #define	NO	7
657c478bd9Sstevel@tonic-gate #define	COMMAND	8
667c478bd9Sstevel@tonic-gate #define	FORCE	9
677c478bd9Sstevel@tonic-gate #define	ID	10
687c478bd9Sstevel@tonic-gate #define	MACHINE	11
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #define	MAXTOKEN  11
717c478bd9Sstevel@tonic-gate #define	NTOKENS	(MAXTOKEN - 1 + 2 + 1)	/* two duplicates and null, minus id */
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate static struct ruserdata {
747c478bd9Sstevel@tonic-gate 	char tokval[100];
757c478bd9Sstevel@tonic-gate 	struct toktab {
767c478bd9Sstevel@tonic-gate 		char *tokstr;
777c478bd9Sstevel@tonic-gate 		int tval;
787c478bd9Sstevel@tonic-gate 	} toktab[NTOKENS];
797c478bd9Sstevel@tonic-gate 	FILE *cfile;
807c478bd9Sstevel@tonic-gate } *ruserdata, *_ruserdata();
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate static struct ruserdata *
_ruserdata()847c478bd9Sstevel@tonic-gate _ruserdata()
857c478bd9Sstevel@tonic-gate {
867c478bd9Sstevel@tonic-gate 	struct ruserdata *d = ruserdata;
877c478bd9Sstevel@tonic-gate 	struct toktab *t;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	if (d == 0) {
907c478bd9Sstevel@tonic-gate 		if ((d = (struct ruserdata *)
917c478bd9Sstevel@tonic-gate 			calloc(1, sizeof (struct ruserdata))) == NULL) {
927c478bd9Sstevel@tonic-gate 				return (NULL);
937c478bd9Sstevel@tonic-gate 		}
947c478bd9Sstevel@tonic-gate 		ruserdata = d;
957c478bd9Sstevel@tonic-gate 		t = d->toktab;
967c478bd9Sstevel@tonic-gate 		t->tokstr = "default";  t++->tval = DEFAULT;
977c478bd9Sstevel@tonic-gate 		t->tokstr = "login";    t++->tval = LOGIN;
987c478bd9Sstevel@tonic-gate 		t->tokstr = "password"; t++->tval = PASSWD;
997c478bd9Sstevel@tonic-gate 		t->tokstr = "notify";   t++->tval = NOTIFY;
1007c478bd9Sstevel@tonic-gate 		t->tokstr = "write";    t++->tval = WRITE;
1017c478bd9Sstevel@tonic-gate 		t->tokstr = "yes";	t++->tval = YES;
1027c478bd9Sstevel@tonic-gate 		t->tokstr = "y";	t++->tval = YES;
1037c478bd9Sstevel@tonic-gate 		t->tokstr = "no";	t++->tval = NO;
1047c478bd9Sstevel@tonic-gate 		t->tokstr = "n";	t++->tval = NO;
1057c478bd9Sstevel@tonic-gate 		t->tokstr = "command";  t++->tval = COMMAND;
1067c478bd9Sstevel@tonic-gate 		t->tokstr = "force";    t++->tval = FORCE;
1077c478bd9Sstevel@tonic-gate 		t->tokstr = "machine";  t++->tval = MACHINE;
1087c478bd9Sstevel@tonic-gate 		t->tokstr = 0;		t->tval = 0;
1097c478bd9Sstevel@tonic-gate 	}
1107c478bd9Sstevel@tonic-gate 	return (d);
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate #define	MAXANAME	16
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate void
_ruserpass(const char * host,char ** aname,char ** apass)1177c478bd9Sstevel@tonic-gate _ruserpass(const char *host, char **aname, char **apass)
1187c478bd9Sstevel@tonic-gate {
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	if (*aname == 0 || *apass == 0)
1217c478bd9Sstevel@tonic-gate 		rnetrc(host, aname, apass);
1227c478bd9Sstevel@tonic-gate 	if (*aname == 0) {
1237c478bd9Sstevel@tonic-gate 		char myname[L_cuserid];
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 		*aname = malloc(MAXANAME + 1);
1267c478bd9Sstevel@tonic-gate 		(void) cuserid(myname);
1277257d1b4Sraf 		(void) printf(dgettext(TEXT_DOMAIN, "Name (%s:%s): "),
128004388ebScasper 		    host, myname);
1297c478bd9Sstevel@tonic-gate 		(void) fflush(stdout);
1307c478bd9Sstevel@tonic-gate 		if (read(2, *aname, MAXANAME) <= 0)
1317c478bd9Sstevel@tonic-gate 			exit(1);
1327c478bd9Sstevel@tonic-gate 		aname[0][MAXANAME] = '\0';
1337c478bd9Sstevel@tonic-gate 		if ((*aname)[0] == '\n')
1347c478bd9Sstevel@tonic-gate 			(void) strcpy(*aname, myname);
1357c478bd9Sstevel@tonic-gate 		else
1367c478bd9Sstevel@tonic-gate 			if (index(*aname, '\n'))
1377c478bd9Sstevel@tonic-gate 				*index(*aname, '\n') = 0;
1387c478bd9Sstevel@tonic-gate 	}
1397c478bd9Sstevel@tonic-gate 	if (*aname && *apass == 0) {
1407257d1b4Sraf 		(void) printf(dgettext(TEXT_DOMAIN, "Password (%s:%s): "),
1417257d1b4Sraf 		    host, *aname);
1427c478bd9Sstevel@tonic-gate 		(void) fflush(stdout);
1437c478bd9Sstevel@tonic-gate 		*apass = getpass("");
1447c478bd9Sstevel@tonic-gate 	}
1457c478bd9Sstevel@tonic-gate }
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate static void
rnetrc(const char * host,char ** aname,char ** apass)1497c478bd9Sstevel@tonic-gate rnetrc(const char *host, char **aname, char **apass)
1507c478bd9Sstevel@tonic-gate {
1517c478bd9Sstevel@tonic-gate 	struct ruserdata *d = _ruserdata();
152*00f1a4f4SToomas Soome 	char *hdir, buf[PATH_MAX];
1537c478bd9Sstevel@tonic-gate 	int t;
1547c478bd9Sstevel@tonic-gate 	struct stat64 stb;
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	if (d == 0)
1577c478bd9Sstevel@tonic-gate 		return;
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	hdir = getenv("HOME");
1607c478bd9Sstevel@tonic-gate 	if (hdir == NULL)
1617c478bd9Sstevel@tonic-gate 		hdir = ".";
162*00f1a4f4SToomas Soome 	t = snprintf(buf, sizeof (buf), "%s/.netrc", hdir);
163*00f1a4f4SToomas Soome 	if (t < 0 || t >= sizeof (buf)) {
164*00f1a4f4SToomas Soome 		if (t < 0) {
165*00f1a4f4SToomas Soome 			perror(buf);
166*00f1a4f4SToomas Soome 		} else {
167*00f1a4f4SToomas Soome 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
168*00f1a4f4SToomas Soome 			    "HOME directory name is too long: %s\n"), hdir);
169*00f1a4f4SToomas Soome 		}
170*00f1a4f4SToomas Soome 		return;
171*00f1a4f4SToomas Soome 	}
172*00f1a4f4SToomas Soome 
173004388ebScasper 	d->cfile = fopen(buf, "rF");
1747c478bd9Sstevel@tonic-gate 	if (d->cfile == NULL) {
1757c478bd9Sstevel@tonic-gate 		if (errno != ENOENT)
1767c478bd9Sstevel@tonic-gate 			perror(buf);
1777c478bd9Sstevel@tonic-gate 		return;
1787c478bd9Sstevel@tonic-gate 	}
1797c478bd9Sstevel@tonic-gate next:
1807c478bd9Sstevel@tonic-gate 	while ((t = token()))
1817c478bd9Sstevel@tonic-gate 	switch (t) {
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	case DEFAULT:
1847c478bd9Sstevel@tonic-gate 		(void) token();
1857c478bd9Sstevel@tonic-gate 		continue;
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	case MACHINE:
1887c478bd9Sstevel@tonic-gate 		if (token() != ID || strcmp(host, d->tokval))
1897c478bd9Sstevel@tonic-gate 			continue;
1907c478bd9Sstevel@tonic-gate 		while ((t = token()) != 0 && t != MACHINE)
1917c478bd9Sstevel@tonic-gate 		switch (t) {
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 		case LOGIN:
1947c478bd9Sstevel@tonic-gate 			if (token())
1957c478bd9Sstevel@tonic-gate 				if (*aname == 0) {
1967c478bd9Sstevel@tonic-gate 					*aname = malloc(strlen(d->tokval) + 1);
1977c478bd9Sstevel@tonic-gate 					(void) strcpy(*aname, d->tokval);
1987c478bd9Sstevel@tonic-gate 				} else {
1997c478bd9Sstevel@tonic-gate 					if (strcmp(*aname, d->tokval))
2007c478bd9Sstevel@tonic-gate 						goto next;
2017c478bd9Sstevel@tonic-gate 				}
2027c478bd9Sstevel@tonic-gate 			break;
2037c478bd9Sstevel@tonic-gate 		case PASSWD:
2047c478bd9Sstevel@tonic-gate 			if (fstat64(fileno(d->cfile), &stb) >= 0 &&
2057257d1b4Sraf 			    (stb.st_mode & 077) != 0) {
2067c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
2077257d1b4Sraf 				    dgettext(TEXT_DOMAIN,
208004388ebScasper 				    "Error - .netrc file not correct mode.\n"));
2097c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
2107257d1b4Sraf 				    dgettext(TEXT_DOMAIN,
211004388ebScasper 				    "Remove password or correct mode.\n"));
2127c478bd9Sstevel@tonic-gate 				exit(1);
2137c478bd9Sstevel@tonic-gate 			}
2147c478bd9Sstevel@tonic-gate 			if (token() && *apass == 0) {
2157c478bd9Sstevel@tonic-gate 				*apass = malloc(strlen(d->tokval) + 1);
2167c478bd9Sstevel@tonic-gate 				(void) strcpy(*apass, d->tokval);
2177c478bd9Sstevel@tonic-gate 			}
2187c478bd9Sstevel@tonic-gate 			break;
2197c478bd9Sstevel@tonic-gate 		case COMMAND:
2207c478bd9Sstevel@tonic-gate 		case NOTIFY:
2217c478bd9Sstevel@tonic-gate 		case WRITE:
2227c478bd9Sstevel@tonic-gate 		case FORCE:
2237c478bd9Sstevel@tonic-gate 			(void) token();
2247c478bd9Sstevel@tonic-gate 			break;
2257c478bd9Sstevel@tonic-gate 		default:
2267257d1b4Sraf 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
227004388ebScasper 			    "Unknown .netrc option %s\n"), d->tokval);
2287c478bd9Sstevel@tonic-gate 			break;
2297c478bd9Sstevel@tonic-gate 		}
2307c478bd9Sstevel@tonic-gate 		goto done;
2317c478bd9Sstevel@tonic-gate 	}
2327c478bd9Sstevel@tonic-gate done:
2337c478bd9Sstevel@tonic-gate 	(void) fclose(d->cfile);
2347c478bd9Sstevel@tonic-gate }
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate static int
token()2377c478bd9Sstevel@tonic-gate token()
2387c478bd9Sstevel@tonic-gate {
2397c478bd9Sstevel@tonic-gate 	struct ruserdata *d = _ruserdata();
2407c478bd9Sstevel@tonic-gate 	char *cp;
2417c478bd9Sstevel@tonic-gate 	int c;
2427c478bd9Sstevel@tonic-gate 	struct toktab *t;
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	if (d == 0)
2457c478bd9Sstevel@tonic-gate 		return (0);
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	if (feof(d->cfile))
2487c478bd9Sstevel@tonic-gate 		return (0);
2497c478bd9Sstevel@tonic-gate 	while ((c = getc(d->cfile)) != EOF &&
2507c478bd9Sstevel@tonic-gate 	    (c == '\n' || c == '\t' || c == ' ' || c == ','))
2517c478bd9Sstevel@tonic-gate 		continue;
2527c478bd9Sstevel@tonic-gate 	if (c == EOF)
2537c478bd9Sstevel@tonic-gate 		return (0);
2547c478bd9Sstevel@tonic-gate 	cp = d->tokval;
2557c478bd9Sstevel@tonic-gate 	if (c == '"') {
2567c478bd9Sstevel@tonic-gate 		while ((c = getc(d->cfile)) != EOF && c != '"') {
2577c478bd9Sstevel@tonic-gate 			if (c == '\\')
2587c478bd9Sstevel@tonic-gate 				c = getc(d->cfile);
2597c478bd9Sstevel@tonic-gate 			*cp++ = (char)c;
2607c478bd9Sstevel@tonic-gate 		}
2617c478bd9Sstevel@tonic-gate 	} else {
2627c478bd9Sstevel@tonic-gate 		*cp++ = (char)c;
2637c478bd9Sstevel@tonic-gate 		while ((c = getc(d->cfile)) != EOF &&
2647257d1b4Sraf 		    c != '\n' && c != '\t' && c != ' ' && c != ',') {
2657c478bd9Sstevel@tonic-gate 			if (c == '\\')
2667c478bd9Sstevel@tonic-gate 				c = getc(d->cfile);
2677c478bd9Sstevel@tonic-gate 			*cp++ = (char)c;
2687c478bd9Sstevel@tonic-gate 		}
2697c478bd9Sstevel@tonic-gate 	}
2707c478bd9Sstevel@tonic-gate 	*cp = 0;
2717c478bd9Sstevel@tonic-gate 	if (d->tokval[0] == 0)
2727c478bd9Sstevel@tonic-gate 		return (0);
2737c478bd9Sstevel@tonic-gate 	for (t = d->toktab; t->tokstr; t++)
2747c478bd9Sstevel@tonic-gate 		if ((strcmp(t->tokstr, d->tokval) == 0))
2757c478bd9Sstevel@tonic-gate 			return (t->tval);
2767c478bd9Sstevel@tonic-gate 	return (ID);
2777c478bd9Sstevel@tonic-gate }
278