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