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