]> diplodocus.org Git - nmh/blob - sbr/ruserpass.c
f5ef74a418187eba33e72b681cb332f2113ececd
[nmh] / sbr / ruserpass.c
1 /*
2 * Portions of this code are
3 * Copyright (c) 1985 Regents of the University of California.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms are permitted
7 * provided that the above copyright notice and this paragraph are
8 * duplicated in all such forms and that any documentation,
9 * advertising materials, and other materials related to such
10 * distribution and use acknowledge that the software was developed
11 * by the University of California, Berkeley. The name of the
12 * University may not be used to endorse or promote products derived
13 * from this software without specific prior written permission.
14 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17 *
18 * Portions of this code are Copyright (c) 2013, by the authors of
19 * nmh. See the COPYRIGHT file in the root directory of the nmh
20 * distribution for complete copyright information.
21 */
22
23 #include <h/mh.h>
24 #include <h/utils.h>
25 #include <pwd.h>
26 #include <errno.h>
27
28 static FILE *cfile;
29
30 #define DEFAULT 1
31 #define LOGIN 2
32 #define PASSWD 3
33 #define ACCOUNT 4
34 #define MACDEF 5
35 #define ID 10
36 #define MACH 11
37
38 static char tokval[100];
39
40 struct toktab {
41 char *tokstr;
42 int tval;
43 };
44
45 static struct toktab toktabs[] = {
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(void);
60
61
62 void
63 ruserpass(char *host, char **aname, char **apass)
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 while ((t = token())) {
76 switch(t) {
77 case DEFAULT:
78 usedefault = 1;
79 /* FALL THROUGH */
80
81 case MACH:
82 if (!usedefault) {
83 if (token() != ID)
84 continue;
85 /*
86 * Allow match either for user's host name.
87 */
88 if (strcasecmp(host, tokval) == 0)
89 goto match;
90 continue;
91 }
92 match:
93 while ((t = token()) && t != MACH && t != DEFAULT) {
94 switch(t) {
95 case LOGIN:
96 if (token() && *aname == 0) {
97 *aname = mh_xmalloc((size_t) strlen(tokval) + 1);
98 strcpy(*aname, tokval);
99 }
100 break;
101
102 case PASSWD:
103 if (fstat(fileno(cfile), &stb) >= 0 &&
104 (stb.st_mode & 077) != 0) {
105 /* We make this a fatal error to force the
106 user to correct it. */
107 advise(NULL, "Error - file %s must not be world or "
108 "group readable.", credentials_file);
109 adios(NULL, "Remove password or correct file "
110 "permissions.");
111 }
112 if (token() && *apass == 0) {
113 *apass = mh_xmalloc((size_t) strlen(tokval) + 1);
114 strcpy(*apass, tokval);
115 }
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) {
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 fgets(tmp, sizeof(tmp) - 1, stdin);
150 tmp[strlen(tmp) - 1] = '\0';
151 if (*tmp != '\0') {
152 myname = tmp;
153 }
154
155 *aname = mh_xmalloc((size_t) strlen(myname) + 1);
156 strcpy (*aname, myname);
157 }
158
159 if (!*apass) {
160 char prompt[256];
161 char *mypass;
162
163 snprintf(prompt, sizeof(prompt), "Password (%s:%s): ", host, *aname);
164 mypass = nmh_getpass(prompt);
165
166 if (*mypass == '\0') {
167 mypass = *aname;
168 }
169
170 *apass = mh_xmalloc((size_t) strlen(mypass) + 1);
171 strcpy (*apass, mypass);
172 }
173
174 }
175
176 static int
177 token(void)
178 {
179 char *cp;
180 int c;
181 struct toktab *t;
182
183 if (feof(cfile))
184 return (0);
185 while ((c = getc(cfile)) != EOF &&
186 (c == '\n' || c == '\t' || c == ' ' || c == ','))
187 continue;
188 if (c == EOF)
189 return (0);
190 cp = tokval;
191 if (c == '"') {
192 while ((c = getc(cfile)) != EOF && c != '"') {
193 if (c == '\\')
194 c = getc(cfile);
195 *cp++ = c;
196 }
197 } else {
198 *cp++ = c;
199 while ((c = getc(cfile)) != EOF
200 && c != '\n' && c != '\t' && c != ' ' && c != ',') {
201 if (c == '\\')
202 c = getc(cfile);
203 *cp++ = c;
204 }
205 }
206 *cp = 0;
207 if (tokval[0] == 0)
208 return (0);
209 for (t = toktabs; t->tokstr; t++)
210 if (!strcmp(t->tokstr, tokval))
211 return (t->tval);
212 return (ID);
213 }