0
|
1 /*
|
|
2 * Copyright (c) 1985 Regents of the University of California.
|
|
3 * All rights reserved.
|
|
4 *
|
|
5 * Redistribution and use in source and binary forms are permitted
|
|
6 * provided that the above copyright notice and this paragraph are
|
|
7 * duplicated in all such forms and that any documentation,
|
|
8 * advertising materials, and other materials related to such
|
|
9 * distribution and use acknowledge that the software was developed
|
|
10 * by the University of California, Berkeley. The name of the
|
|
11 * University may not be used to endorse or promote products derived
|
|
12 * from this software without specific prior written permission.
|
|
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
|
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
|
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
|
16 */
|
|
17
|
|
18 #ifndef lint
|
|
19 static char sccsid[] = "@(#)ruserpass.c 5.1 (Berkeley) 3/1/89";
|
|
20 #endif /* not lint */
|
|
21
|
|
22 #include <sys/types.h>
|
|
23 #include <sys/stat.h>
|
|
24 #include <stdio.h>
|
|
25 #include <ctype.h>
|
|
26 #include <pwd.h>
|
|
27 #include <errno.h>
|
|
28
|
10
|
29 // char *malloc(), *index(), *getenv(), *getpass(), *getlogin();
|
|
30 // char *strcpy();
|
12
|
31 #include <unistd.h>
|
|
32
|
10
|
33 #include <stdlib.h>
|
|
34 #include <string.h>
|
0
|
35 static FILE *cfile;
|
|
36
|
|
37 #ifndef MAXHOSTNAMELEN
|
|
38 #define MAXHOSTNAMELEN 64
|
|
39 #endif
|
|
40
|
|
41 #define DEFAULT 1
|
|
42 #define LOGIN 2
|
|
43 #define PASSWD 3
|
|
44 #define ACCOUNT 4
|
|
45 #define MACDEF 5
|
|
46 #define ID 10
|
|
47 #define MACH 11
|
|
48
|
|
49 static int token();
|
|
50 static char tokval[256];
|
|
51
|
|
52 static struct toktab {
|
|
53 char *tokstr;
|
|
54 int tval;
|
|
55 } toktab[]= {
|
|
56 "default", DEFAULT,
|
|
57 "login", LOGIN,
|
|
58 "password", PASSWD,
|
|
59 "passwd", PASSWD,
|
|
60 "account", ACCOUNT,
|
|
61 "machine", MACH,
|
|
62 "macdef", MACDEF,
|
|
63 0, 0
|
|
64 };
|
|
65
|
|
66 ruserpass(host, aname, apass)
|
|
67 char *host, **aname, **apass;
|
|
68 {
|
|
69 char *hdir, buf[BUFSIZ], *tmp;
|
|
70 char myname[MAXHOSTNAMELEN], *mydomain;
|
|
71 int t, i, c, usedefault = 0;
|
|
72 struct stat stb;
|
|
73
|
|
74 hdir = getenv("HOME");
|
|
75 if (hdir == NULL)
|
|
76 hdir = ".";
|
|
77 (void) sprintf(buf, "%s/.netrc", hdir);
|
|
78 cfile = fopen(buf, "r");
|
|
79 if (cfile == NULL) {
|
|
80 if (errno != ENOENT)
|
|
81 perror(buf);
|
|
82 goto done;
|
|
83 }
|
|
84
|
|
85 while ((t = token())) switch(t) {
|
|
86 case DEFAULT:
|
|
87 usedefault = 1;
|
|
88 /* FALL THROUGH */
|
|
89
|
|
90 case MACH:
|
|
91 if (!usedefault) {
|
|
92 if (token() != ID)
|
|
93 continue;
|
|
94 /*
|
|
95 * Allow match either for user's host name.
|
|
96 */
|
|
97 if (uleq(host, tokval))
|
|
98 goto match;
|
|
99 continue;
|
|
100 }
|
|
101 match:
|
|
102 while ((t = token()) && t != MACH && t != DEFAULT) switch(t) {
|
|
103
|
|
104 case LOGIN:
|
|
105 if (token() && *aname == 0) {
|
|
106 *aname = malloc((unsigned) strlen(tokval) + 1);
|
|
107 (void) strcpy(*aname, tokval);
|
|
108 }
|
|
109 break;
|
|
110 case PASSWD:
|
|
111 if (fstat(fileno(cfile), &stb) >= 0 &&
|
|
112 (stb.st_mode & 077) != 0) {
|
|
113 fprintf(stderr, "Error - .netrc file not correct mode.\n");
|
|
114 fprintf(stderr, "Remove password or correct mode.\n");
|
|
115 goto bad;
|
|
116 }
|
|
117 if (token() && *apass == 0) {
|
|
118 *apass = malloc((unsigned) strlen(tokval) + 1);
|
|
119 (void) strcpy(*apass, tokval);
|
|
120 }
|
|
121 break;
|
|
122 case ACCOUNT:
|
|
123 break;
|
|
124
|
|
125 case MACDEF:
|
|
126 goto done_close;
|
|
127 break;
|
|
128 default:
|
|
129 fprintf(stderr, "Unknown .netrc keyword %s\n", tokval);
|
|
130 break;
|
|
131 }
|
|
132 goto done;
|
|
133 }
|
|
134
|
|
135 done_close:
|
|
136 (void) fclose(cfile);
|
|
137
|
|
138 done:
|
|
139 if (!*aname) {
|
|
140 char tmp[80];
|
|
141 char *myname;
|
|
142
|
|
143 if ((myname = getlogin()) == NULL) {
|
|
144 struct passwd *pp;
|
|
145
|
|
146 if ((pp = getpwuid (getuid())) != NULL)
|
|
147 myname = pp->pw_name;
|
|
148 }
|
|
149 printf("Name (%s:%s): ", host, myname ? myname : "");
|
|
150
|
|
151 (void) fgets(tmp, sizeof(tmp) - 1, stdin);
|
|
152 tmp[strlen(tmp) - 1] = '\0';
|
|
153 if (*tmp != '\0') {
|
|
154 myname = tmp;
|
|
155 }
|
|
156
|
|
157 *aname = malloc((unsigned) strlen(myname) + 1);
|
|
158 strcpy (*aname, myname);
|
|
159 }
|
|
160
|
|
161 if (!*apass) {
|
|
162 char prompt[BUFSIZ];
|
|
163 char *mypass;
|
|
164
|
|
165 sprintf(prompt, "Password (%s:%s): ", host, *aname);
|
|
166 mypass = getpass (prompt);
|
|
167
|
|
168 if (*mypass == '\0') {
|
|
169 mypass = *aname;
|
|
170 }
|
|
171
|
|
172 *apass = malloc((unsigned) strlen(mypass) + 1);
|
|
173 strcpy (*apass, mypass);
|
|
174 }
|
|
175
|
|
176 return(0);
|
|
177 bad:
|
|
178 (void) fclose(cfile);
|
|
179 return(-1);
|
|
180 }
|
|
181
|
|
182 static int
|
|
183 token()
|
|
184 {
|
|
185 char *cp;
|
|
186 int c;
|
|
187 struct toktab *t;
|
|
188
|
|
189 if (feof(cfile))
|
|
190 return (0);
|
|
191 while ((c = getc(cfile)) != EOF &&
|
|
192 (c == '\n' || c == '\t' || c == ' ' || c == ','))
|
|
193 continue;
|
|
194 if (c == EOF)
|
|
195 return (0);
|
|
196 cp = tokval;
|
|
197 if (c == '"') {
|
|
198 while ((c = getc(cfile)) != EOF && c != '"') {
|
|
199 if (c == '\\')
|
|
200 c = getc(cfile);
|
|
201 if (cp > tokval + sizeof(tokval) - 2) {
|
|
202 fprintf(stderr, ".netrc keyword too long.\n");
|
|
203 tokval[0] = 0;
|
|
204 return 0;
|
|
205 }
|
|
206 *cp++ = c;
|
|
207 }
|
|
208 } else {
|
|
209 *cp++ = c;
|
|
210 while ((c = getc(cfile)) != EOF
|
|
211 && c != '\n' && c != '\t' && c != ' ' && c != ',') {
|
|
212 if (c == '\\')
|
|
213 c = getc(cfile);
|
|
214 if (cp > tokval + sizeof(tokval) - 2) {
|
|
215 fprintf(stderr, ".netrc keyword too long.\n");
|
|
216 tokval[0] = 0;
|
|
217 return 0;
|
|
218 }
|
|
219 *cp++ = c;
|
|
220 }
|
|
221 }
|
|
222 *cp = 0;
|
|
223 if (tokval[0] == 0)
|
|
224 return (0);
|
|
225 for (t = toktab; t->tokstr; t++)
|
|
226 if (!strcmp(t->tokstr, tokval))
|
|
227 return (t->tval);
|
|
228 return (ID);
|
|
229 }
|