]> diplodocus.org Git - nmh/blob - sbr/ruserpass.c
6b1d1a137a24ff35c8d9c3902b2ba2bf2f416c85
[nmh] / sbr / ruserpass.c
1 /* Portions of this code are
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 * Portions of this code are Copyright (c) 2013, by the authors of
18 * nmh. See the COPYRIGHT file in the root directory of the nmh
19 * distribution for complete copyright information.
20 */
21
22 #include <h/mh.h>
23 #include <h/utils.h>
24 #include <pwd.h>
25
26 static FILE *cfile;
27
28 #define TOK_EOF 0
29 #define DEFAULT 1
30 #define LOGIN 2
31 #define PASSWD 3
32 #define ACCOUNT 4
33 #define MACDEF 5
34 #define ID 10
35 #define MACH 11
36
37 #define MAX_TOKVAL_SIZE 1024 /* Including terminating NUL. */
38
39 struct toktab {
40 char *tokstr;
41 int tval;
42 };
43
44 static struct toktab toktabs[] = {
45 { "", TOK_EOF },
46 { "default", DEFAULT },
47 { "login", LOGIN },
48 { "password", PASSWD },
49 { "passwd", PASSWD },
50 { "account", ACCOUNT },
51 { "machine", MACH },
52 { "macdef", MACDEF },
53 { 0, 0 }
54 };
55
56 /*
57 * prototypes
58 */
59 static int token(char *);
60
61
62 void
63 ruserpass(const char *host, char **aname, char **apass, int flags)
64 {
65 int t, usedefault = 0;
66 struct stat stb;
67
68 init_credentials_file ();
69
70 cfile = fopen (credentials_file, "r");
71 if (cfile == NULL) {
72 if (errno != ENOENT)
73 perror (credentials_file);
74 } else {
75 char tokval[MAX_TOKVAL_SIZE];
76 tokval[0] = '\0';
77
78 while ((t = token(tokval))) {
79 switch(t) {
80 case DEFAULT:
81 usedefault = 1;
82 /* FALLTHRU */
83
84 case MACH:
85 if (!usedefault) {
86 if (token(tokval) != ID)
87 continue;
88 /*
89 * Allow match either for user's host name.
90 */
91 if (strcasecmp(host, tokval) == 0)
92 goto match;
93 continue;
94 }
95 match:
96 while ((t = token(tokval)) && t != MACH && t != DEFAULT) {
97 switch(t) {
98 case LOGIN:
99 if (token(tokval) && *aname == 0)
100 *aname = mh_xstrdup(tokval);
101 break;
102
103 case PASSWD:
104 if (!credentials_no_perm_check &&
105 fstat(fileno(cfile), &stb) >= 0 &&
106 (stb.st_mode & 077) != 0) {
107 /* We make this a fatal error to force the
108 user to correct it. */
109 inform("group or other permissions, %#o, "
110 "forbidden: %s", stb.st_mode, credentials_file);
111 adios(NULL, "Remove password or correct file "
112 "permissions.");
113 }
114 if (token(tokval) && *apass == 0)
115 *apass = mh_xstrdup(tokval);
116 break;
117
118 case ACCOUNT:
119 break;
120
121 case MACDEF:
122 fclose(cfile);
123 return;
124
125 default:
126 fprintf(stderr,
127 "Unknown keyword %s in credentials file %s\n",
128 tokval, credentials_file);
129 break;
130 }
131 }
132 return;
133 }
134 }
135 }
136
137 if (!*aname && ! (flags & RUSERPASS_NO_PROMPT_USER)) {
138 char tmp[80];
139 char *myname;
140
141 if ((myname = getlogin()) == NULL) {
142 struct passwd *pp;
143
144 if ((pp = getpwuid (getuid())) != NULL)
145 myname = pp->pw_name;
146 }
147 printf("Name (%s:%s): ", host, myname);
148
149 if (fgets(tmp, sizeof tmp, stdin) == NULL) {
150 advise ("tmp", "fgets");
151 }
152 trim_suffix_c(tmp, '\n');
153 if (*tmp != '\0' || myname == NULL) {
154 myname = tmp;
155 }
156
157 *aname = mh_xstrdup(myname);
158 }
159
160 if (!*apass && ! (flags & RUSERPASS_NO_PROMPT_PASSWORD)) {
161 char prompt[256];
162 char *mypass;
163
164 snprintf(prompt, sizeof(prompt), "Password (%s:%s): ", host, *aname);
165 mypass = nmh_getpass(prompt);
166
167 if (*mypass == '\0') {
168 mypass = *aname;
169 }
170
171 *aname = mh_xstrdup(mypass);
172 }
173
174 }
175
176 static int
177 token(char *tokval)
178 {
179 int c;
180 const char normalStop[] = "\t\n ,"; /* Each breaks a word. */
181 const char *stop;
182 char *cp;
183 struct toktab *t;
184
185 if (feof(cfile) || ferror(cfile))
186 return TOK_EOF;
187
188 stop = normalStop;
189 while ((c = getc(cfile)) != EOF && c && strchr(stop, c))
190 ;
191 if (c == EOF)
192 return TOK_EOF;
193
194 cp = tokval;
195 if (c == '"')
196 /* FIXME: Where is the quoted-string syntax of netrc documented?
197 * This code treats «"foo""bar"» as two tokens without further
198 * separators. */
199 stop = "\"";
200 else
201 /* Might be backslash. Get it again later. It's handled then. */
202 if (ungetc(c, cfile) == EOF)
203 return TOK_EOF;
204
205 while ((c = getc(cfile)) != EOF && c && !strchr(stop, c)) {
206 if (c == '\\' && (c = getc(cfile)) == EOF)
207 return TOK_EOF; /* Discard whole token. */
208
209 *cp++ = c;
210 if (cp - tokval > MAX_TOKVAL_SIZE-1) {
211 adios(NULL, "credential tokens restricted to length %d",
212 MAX_TOKVAL_SIZE - 1);
213 }
214 }
215 *cp = '\0';
216
217 for (t = toktabs; t->tokstr; t++)
218 if (!strcmp(t->tokstr, tokval))
219 return (t->tval);
220
221 return (ID);
222 }