xref: /titanic_52/usr/src/lib/libsocket/inet/ruserpass.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 1998 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate /*
31*7c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
32*7c478bd9Sstevel@tonic-gate  * The Regents of the University of California
33*7c478bd9Sstevel@tonic-gate  * All Rights Reserved
34*7c478bd9Sstevel@tonic-gate  *
35*7c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
36*7c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
37*7c478bd9Sstevel@tonic-gate  * contributors.
38*7c478bd9Sstevel@tonic-gate  */
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate #include <stdio.h>
43*7c478bd9Sstevel@tonic-gate #include <ctype.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
46*7c478bd9Sstevel@tonic-gate #include <errno.h>
47*7c478bd9Sstevel@tonic-gate #include <unistd.h>
48*7c478bd9Sstevel@tonic-gate #include <strings.h>
49*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
50*7c478bd9Sstevel@tonic-gate #include <libintl.h>
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate extern char *_dgettext();
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate #ifdef SYSV
55*7c478bd9Sstevel@tonic-gate #define	index	strchr
56*7c478bd9Sstevel@tonic-gate #endif /* SYSV */
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate static void rnetrc(const char *host, char **aname, char **apass);
59*7c478bd9Sstevel@tonic-gate static int token();
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate #define	DEFAULT	1
62*7c478bd9Sstevel@tonic-gate #define	LOGIN	2
63*7c478bd9Sstevel@tonic-gate #define	PASSWD	3
64*7c478bd9Sstevel@tonic-gate #define	NOTIFY	4
65*7c478bd9Sstevel@tonic-gate #define	WRITE	5
66*7c478bd9Sstevel@tonic-gate #define	YES	6
67*7c478bd9Sstevel@tonic-gate #define	NO	7
68*7c478bd9Sstevel@tonic-gate #define	COMMAND	8
69*7c478bd9Sstevel@tonic-gate #define	FORCE	9
70*7c478bd9Sstevel@tonic-gate #define	ID	10
71*7c478bd9Sstevel@tonic-gate #define	MACHINE	11
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate #define	MAXTOKEN  11
74*7c478bd9Sstevel@tonic-gate #define	NTOKENS	(MAXTOKEN - 1 + 2 + 1)	/* two duplicates and null, minus id */
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate static struct ruserdata {
77*7c478bd9Sstevel@tonic-gate 	char tokval[100];
78*7c478bd9Sstevel@tonic-gate 	struct toktab {
79*7c478bd9Sstevel@tonic-gate 		char *tokstr;
80*7c478bd9Sstevel@tonic-gate 		int tval;
81*7c478bd9Sstevel@tonic-gate 	} toktab[NTOKENS];
82*7c478bd9Sstevel@tonic-gate 	FILE *cfile;
83*7c478bd9Sstevel@tonic-gate } *ruserdata, *_ruserdata();
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate static struct ruserdata *
87*7c478bd9Sstevel@tonic-gate _ruserdata()
88*7c478bd9Sstevel@tonic-gate {
89*7c478bd9Sstevel@tonic-gate 	struct ruserdata *d = ruserdata;
90*7c478bd9Sstevel@tonic-gate 	struct toktab *t;
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 	if (d == 0) {
93*7c478bd9Sstevel@tonic-gate 		if ((d = (struct ruserdata *)
94*7c478bd9Sstevel@tonic-gate 			calloc(1, sizeof (struct ruserdata))) == NULL) {
95*7c478bd9Sstevel@tonic-gate 				return (NULL);
96*7c478bd9Sstevel@tonic-gate 		}
97*7c478bd9Sstevel@tonic-gate 		ruserdata = d;
98*7c478bd9Sstevel@tonic-gate 		t = d->toktab;
99*7c478bd9Sstevel@tonic-gate 		t->tokstr = "default";  t++->tval = DEFAULT;
100*7c478bd9Sstevel@tonic-gate 		t->tokstr = "login";    t++->tval = LOGIN;
101*7c478bd9Sstevel@tonic-gate 		t->tokstr = "password"; t++->tval = PASSWD;
102*7c478bd9Sstevel@tonic-gate 		t->tokstr = "notify";   t++->tval = NOTIFY;
103*7c478bd9Sstevel@tonic-gate 		t->tokstr = "write";    t++->tval = WRITE;
104*7c478bd9Sstevel@tonic-gate 		t->tokstr = "yes";	t++->tval = YES;
105*7c478bd9Sstevel@tonic-gate 		t->tokstr = "y";	t++->tval = YES;
106*7c478bd9Sstevel@tonic-gate 		t->tokstr = "no";	t++->tval = NO;
107*7c478bd9Sstevel@tonic-gate 		t->tokstr = "n";	t++->tval = NO;
108*7c478bd9Sstevel@tonic-gate 		t->tokstr = "command";  t++->tval = COMMAND;
109*7c478bd9Sstevel@tonic-gate 		t->tokstr = "force";    t++->tval = FORCE;
110*7c478bd9Sstevel@tonic-gate 		t->tokstr = "machine";  t++->tval = MACHINE;
111*7c478bd9Sstevel@tonic-gate 		t->tokstr = 0;		t->tval = 0;
112*7c478bd9Sstevel@tonic-gate 	}
113*7c478bd9Sstevel@tonic-gate 	return (d);
114*7c478bd9Sstevel@tonic-gate }
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate #define	MAXANAME	16
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate void
120*7c478bd9Sstevel@tonic-gate _ruserpass(const char *host, char **aname, char **apass)
121*7c478bd9Sstevel@tonic-gate {
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate 	if (*aname == 0 || *apass == 0)
124*7c478bd9Sstevel@tonic-gate 		rnetrc(host, aname, apass);
125*7c478bd9Sstevel@tonic-gate 	if (*aname == 0) {
126*7c478bd9Sstevel@tonic-gate 		char myname[L_cuserid];
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate 		*aname = malloc(MAXANAME + 1);
129*7c478bd9Sstevel@tonic-gate 		(void) cuserid(myname);
130*7c478bd9Sstevel@tonic-gate 		(void) printf(_dgettext(TEXT_DOMAIN, "Name (%s:%s): "), host, myname);
131*7c478bd9Sstevel@tonic-gate 		(void) fflush(stdout);
132*7c478bd9Sstevel@tonic-gate 		if (read(2, *aname, MAXANAME) <= 0)
133*7c478bd9Sstevel@tonic-gate 			exit(1);
134*7c478bd9Sstevel@tonic-gate 		aname[0][MAXANAME] = '\0';
135*7c478bd9Sstevel@tonic-gate 		if ((*aname)[0] == '\n')
136*7c478bd9Sstevel@tonic-gate 			(void) strcpy(*aname, myname);
137*7c478bd9Sstevel@tonic-gate 		else
138*7c478bd9Sstevel@tonic-gate 			if (index(*aname, '\n'))
139*7c478bd9Sstevel@tonic-gate 				*index(*aname, '\n') = 0;
140*7c478bd9Sstevel@tonic-gate 	}
141*7c478bd9Sstevel@tonic-gate 	if (*aname && *apass == 0) {
142*7c478bd9Sstevel@tonic-gate 		(void) printf(_dgettext(TEXT_DOMAIN, "Password (%s:%s): "),
143*7c478bd9Sstevel@tonic-gate 			host, *aname);
144*7c478bd9Sstevel@tonic-gate 		(void) fflush(stdout);
145*7c478bd9Sstevel@tonic-gate 		*apass = getpass("");
146*7c478bd9Sstevel@tonic-gate 	}
147*7c478bd9Sstevel@tonic-gate }
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate static void
151*7c478bd9Sstevel@tonic-gate rnetrc(const char *host, char **aname, char **apass)
152*7c478bd9Sstevel@tonic-gate {
153*7c478bd9Sstevel@tonic-gate 	struct ruserdata *d = _ruserdata();
154*7c478bd9Sstevel@tonic-gate 	char *hdir, buf[BUFSIZ];
155*7c478bd9Sstevel@tonic-gate 	int t;
156*7c478bd9Sstevel@tonic-gate 	struct stat64 stb;
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate 	if (d == 0)
159*7c478bd9Sstevel@tonic-gate 		return;
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 	hdir = getenv("HOME");
162*7c478bd9Sstevel@tonic-gate 	if (hdir == NULL)
163*7c478bd9Sstevel@tonic-gate 		hdir = ".";
164*7c478bd9Sstevel@tonic-gate 	(void) sprintf(buf, "%s/.netrc", hdir);
165*7c478bd9Sstevel@tonic-gate 	d->cfile = fopen(buf, "r");
166*7c478bd9Sstevel@tonic-gate 	if (d->cfile == NULL) {
167*7c478bd9Sstevel@tonic-gate 		if (errno != ENOENT)
168*7c478bd9Sstevel@tonic-gate 			perror(buf);
169*7c478bd9Sstevel@tonic-gate 		return;
170*7c478bd9Sstevel@tonic-gate 	}
171*7c478bd9Sstevel@tonic-gate next:
172*7c478bd9Sstevel@tonic-gate 	while ((t = token()))
173*7c478bd9Sstevel@tonic-gate 	switch (t) {
174*7c478bd9Sstevel@tonic-gate 
175*7c478bd9Sstevel@tonic-gate 	case DEFAULT:
176*7c478bd9Sstevel@tonic-gate 		(void) token();
177*7c478bd9Sstevel@tonic-gate 		continue;
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate 	case MACHINE:
180*7c478bd9Sstevel@tonic-gate 		if (token() != ID || strcmp(host, d->tokval))
181*7c478bd9Sstevel@tonic-gate 			continue;
182*7c478bd9Sstevel@tonic-gate 		while ((t = token()) != 0 && t != MACHINE)
183*7c478bd9Sstevel@tonic-gate 		switch (t) {
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate 		case LOGIN:
186*7c478bd9Sstevel@tonic-gate 			if (token())
187*7c478bd9Sstevel@tonic-gate 				if (*aname == 0) {
188*7c478bd9Sstevel@tonic-gate 					*aname = malloc(strlen(d->tokval) + 1);
189*7c478bd9Sstevel@tonic-gate 					(void) strcpy(*aname, d->tokval);
190*7c478bd9Sstevel@tonic-gate 				} else {
191*7c478bd9Sstevel@tonic-gate 					if (strcmp(*aname, d->tokval))
192*7c478bd9Sstevel@tonic-gate 						goto next;
193*7c478bd9Sstevel@tonic-gate 				}
194*7c478bd9Sstevel@tonic-gate 			break;
195*7c478bd9Sstevel@tonic-gate 		case PASSWD:
196*7c478bd9Sstevel@tonic-gate 			if (fstat64(fileno(d->cfile), &stb) >= 0 &&
197*7c478bd9Sstevel@tonic-gate 				    (stb.st_mode & 077) != 0) {
198*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
199*7c478bd9Sstevel@tonic-gate 				     _dgettext(TEXT_DOMAIN,
200*7c478bd9Sstevel@tonic-gate 				     "Error - .netrc file not correct mode.\n"));
201*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
202*7c478bd9Sstevel@tonic-gate 				     _dgettext(TEXT_DOMAIN,
203*7c478bd9Sstevel@tonic-gate 				     "Remove password or correct mode.\n"));
204*7c478bd9Sstevel@tonic-gate 				exit(1);
205*7c478bd9Sstevel@tonic-gate 			}
206*7c478bd9Sstevel@tonic-gate 			if (token() && *apass == 0) {
207*7c478bd9Sstevel@tonic-gate 				*apass = malloc(strlen(d->tokval) + 1);
208*7c478bd9Sstevel@tonic-gate 				(void) strcpy(*apass, d->tokval);
209*7c478bd9Sstevel@tonic-gate 			}
210*7c478bd9Sstevel@tonic-gate 			break;
211*7c478bd9Sstevel@tonic-gate 		case COMMAND:
212*7c478bd9Sstevel@tonic-gate 		case NOTIFY:
213*7c478bd9Sstevel@tonic-gate 		case WRITE:
214*7c478bd9Sstevel@tonic-gate 		case FORCE:
215*7c478bd9Sstevel@tonic-gate 			(void) token();
216*7c478bd9Sstevel@tonic-gate 			break;
217*7c478bd9Sstevel@tonic-gate 		default:
218*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
219*7c478bd9Sstevel@tonic-gate 			    _dgettext(TEXT_DOMAIN, "Unknown .netrc option %s\n"),
220*7c478bd9Sstevel@tonic-gate 			    d->tokval);
221*7c478bd9Sstevel@tonic-gate 			break;
222*7c478bd9Sstevel@tonic-gate 		}
223*7c478bd9Sstevel@tonic-gate 		goto done;
224*7c478bd9Sstevel@tonic-gate 	}
225*7c478bd9Sstevel@tonic-gate done:
226*7c478bd9Sstevel@tonic-gate 	(void) fclose(d->cfile);
227*7c478bd9Sstevel@tonic-gate }
228*7c478bd9Sstevel@tonic-gate 
229*7c478bd9Sstevel@tonic-gate static int
230*7c478bd9Sstevel@tonic-gate token()
231*7c478bd9Sstevel@tonic-gate {
232*7c478bd9Sstevel@tonic-gate 	struct ruserdata *d = _ruserdata();
233*7c478bd9Sstevel@tonic-gate 	char *cp;
234*7c478bd9Sstevel@tonic-gate 	int c;
235*7c478bd9Sstevel@tonic-gate 	struct toktab *t;
236*7c478bd9Sstevel@tonic-gate 
237*7c478bd9Sstevel@tonic-gate 	if (d == 0)
238*7c478bd9Sstevel@tonic-gate 		return (0);
239*7c478bd9Sstevel@tonic-gate 
240*7c478bd9Sstevel@tonic-gate 	if (feof(d->cfile))
241*7c478bd9Sstevel@tonic-gate 		return (0);
242*7c478bd9Sstevel@tonic-gate 	while ((c = getc(d->cfile)) != EOF &&
243*7c478bd9Sstevel@tonic-gate 	    (c == '\n' || c == '\t' || c == ' ' || c == ','))
244*7c478bd9Sstevel@tonic-gate 		continue;
245*7c478bd9Sstevel@tonic-gate 	if (c == EOF)
246*7c478bd9Sstevel@tonic-gate 		return (0);
247*7c478bd9Sstevel@tonic-gate 	cp = d->tokval;
248*7c478bd9Sstevel@tonic-gate 	if (c == '"') {
249*7c478bd9Sstevel@tonic-gate 		while ((c = getc(d->cfile)) != EOF && c != '"') {
250*7c478bd9Sstevel@tonic-gate 			if (c == '\\')
251*7c478bd9Sstevel@tonic-gate 				c = getc(d->cfile);
252*7c478bd9Sstevel@tonic-gate 			*cp++ = (char)c;
253*7c478bd9Sstevel@tonic-gate 		}
254*7c478bd9Sstevel@tonic-gate 	} else {
255*7c478bd9Sstevel@tonic-gate 		*cp++ = (char)c;
256*7c478bd9Sstevel@tonic-gate 		while ((c = getc(d->cfile)) != EOF &&
257*7c478bd9Sstevel@tonic-gate 			    c != '\n' && c != '\t' && c != ' ' && c != ',') {
258*7c478bd9Sstevel@tonic-gate 			if (c == '\\')
259*7c478bd9Sstevel@tonic-gate 				c = getc(d->cfile);
260*7c478bd9Sstevel@tonic-gate 			*cp++ = (char)c;
261*7c478bd9Sstevel@tonic-gate 		}
262*7c478bd9Sstevel@tonic-gate 	}
263*7c478bd9Sstevel@tonic-gate 	*cp = 0;
264*7c478bd9Sstevel@tonic-gate 	if (d->tokval[0] == 0)
265*7c478bd9Sstevel@tonic-gate 		return (0);
266*7c478bd9Sstevel@tonic-gate 	for (t = d->toktab; t->tokstr; t++)
267*7c478bd9Sstevel@tonic-gate 		if ((strcmp(t->tokstr, d->tokval) == 0))
268*7c478bd9Sstevel@tonic-gate 			return (t->tval);
269*7c478bd9Sstevel@tonic-gate 	return (ID);
270*7c478bd9Sstevel@tonic-gate }
271