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