]> diplodocus.org Git - nmh/blob - sbr/ruserpass.c
More cleaned and conversion to the new parameter API.
[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
27 static FILE *cfile;
28
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 static char tokval[100];
38
39 struct toktab {
40 char *tokstr;
41 int tval;
42 };
43
44 static struct toktab toktabs[] = {
45 { "default", DEFAULT },
46 { "login", LOGIN },
47 { "password", PASSWD },
48 { "passwd", PASSWD },
49 { "account", ACCOUNT },
50 { "machine", MACH },
51 { "macdef", MACDEF },
52 { 0, 0 }
53 };
54
55 /*
56 * prototypes
57 */
58 static int token(void);
59
60
61 void
62 ruserpass(char *host, char **aname, char **apass)
63 {
64 int t, usedefault = 0;
65 struct stat stb;
66
67 init_credentials_file ();
68
69 cfile = fopen (credentials_file, "r");
70 if (cfile == NULL) {
71 if (errno != ENOENT)
72 perror (credentials_file);
73 } else {
74 while ((t = token())) {
75 switch(t) {
76 case DEFAULT:
77 usedefault = 1;
78 /* FALL THROUGH */
79
80 case MACH:
81 if (!usedefault) {
82 if (token() != ID)
83 continue;
84 /*
85 * Allow match either for user's host name.
86 */
87 if (strcasecmp(host, tokval) == 0)
88 goto match;
89 continue;
90 }
91 match:
92 while ((t = token()) && t != MACH && t != DEFAULT) {
93 switch(t) {
94 case LOGIN:
95 if (token() && *aname == 0) {
96 *aname = mh_xmalloc((size_t) strlen(tokval) + 1);
97 strcpy(*aname, tokval);
98 }
99 break;
100
101 case PASSWD:
102 if (fstat(fileno(cfile), &stb) >= 0 &&
103 (stb.st_mode & 077) != 0) {
104 /* We make this a fatal error to force the
105 user to correct it. */
106 advise(NULL, "Error - file %s must not be world or "
107 "group readable.", credentials_file);
108 adios(NULL, "Remove password or correct file "
109 "permissions.");
110 }
111 if (token() && *apass == 0) {
112 *apass = mh_xmalloc((size_t) strlen(tokval) + 1);
113 strcpy(*apass, tokval);
114 }
115 break;
116
117 case ACCOUNT:
118 break;
119
120 case MACDEF:
121 fclose(cfile);
122 return;
123
124 default:
125 fprintf(stderr,
126 "Unknown keyword %s in credentials file %s\n",
127 tokval, credentials_file);
128 break;
129 }
130 }
131 return;
132 }
133 }
134 }
135
136 if (!*aname) {
137 char tmp[80];
138 char *myname;
139
140 if ((myname = getlogin()) == NULL) {
141 struct passwd *pp;
142
143 if ((pp = getpwuid (getuid())) != NULL)
144 myname = pp->pw_name;
145 }
146 printf("Name (%s:%s): ", host, myname);
147
148 fgets(tmp, sizeof(tmp) - 1, stdin);
149 tmp[strlen(tmp) - 1] = '\0';
150 if (*tmp != '\0') {
151 myname = tmp;
152 }
153
154 *aname = mh_xmalloc((size_t) strlen(myname) + 1);
155 strcpy (*aname, myname);
156 }
157
158 if (!*apass) {
159 char prompt[256];
160 char *mypass;
161
162 snprintf(prompt, sizeof(prompt), "Password (%s:%s): ", host, *aname);
163 mypass = nmh_getpass(prompt);
164
165 if (*mypass == '\0') {
166 mypass = *aname;
167 }
168
169 *apass = mh_xmalloc((size_t) strlen(mypass) + 1);
170 strcpy (*apass, mypass);
171 }
172
173 }
174
175 static int
176 token(void)
177 {
178 char *cp;
179 int c;
180 struct toktab *t;
181
182 if (feof(cfile))
183 return (0);
184 while ((c = getc(cfile)) != EOF &&
185 (c == '\n' || c == '\t' || c == ' ' || c == ','))
186 continue;
187 if (c == EOF)
188 return (0);
189 cp = tokval;
190 if (c == '"') {
191 while ((c = getc(cfile)) != EOF && c != '"') {
192 if (c == '\\')
193 c = getc(cfile);
194 *cp++ = c;
195 }
196 } else {
197 *cp++ = c;
198 while ((c = getc(cfile)) != EOF
199 && c != '\n' && c != '\t' && c != ' ' && c != ',') {
200 if (c == '\\')
201 c = getc(cfile);
202 *cp++ = c;
203 }
204 }
205 *cp = 0;
206 if (tokval[0] == 0)
207 return (0);
208 for (t = toktabs; t->tokstr; t++)
209 if (!strcmp(t->tokstr, tokval))
210 return (t->tval);
211 return (ID);
212 }