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