* Brought over the newer string tokenizer from Citadel
[citadel.git] / webcit / auth.c
1 /*
2  * auth.c
3  *
4  * This file contains code which relates to authentication of users to Citadel.
5  *
6  * $Id$
7  */
8
9
10 #include <ctype.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 #include <stdio.h>
14 #include <fcntl.h>
15 #include <signal.h>
16 #include <sys/types.h>
17 #include <sys/wait.h>
18 #include <sys/socket.h>
19 #include <sys/time.h>
20 #include <limits.h>
21 #include <netinet/in.h>
22 #include <netdb.h>
23 #include <string.h>
24 #include <pwd.h>
25 #include <errno.h>
26 #include <stdarg.h>
27 #include <pthread.h>
28 #include <signal.h>
29 #include "webcit.h"
30
31 char *axdefs[] =
32 {
33         "Deleted",
34         "New User",
35         "Problem User",
36         "Local User",
37         "Network User",
38         "Preferred User",
39         "Aide"
40 };
41
42 /*
43  * Display the login screen
44  */
45 void display_login(char *mesg)
46 {
47         char buf[SIZ];
48
49         output_headers(3);
50
51         if (mesg != NULL) if (strlen(mesg) > 0) {
52                 stresc(buf, mesg, 0);
53                 svprintf("mesg", WCS_STRING, "%s", buf);
54         }
55
56         stresc(buf, serv_info.serv_humannode, 1);
57         svprintf("humannode", WCS_STRING, "%s", buf);
58
59         svprintf("hello", WCS_SERVCMD, "MESG hello");
60
61         do_template("login.html");
62
63         clear_local_substs();
64         wDumpContent(0);        /* No menu here; not logged in yet! */
65 }
66
67
68
69
70 /*
71  * This function needs to get called whenever a PASS or NEWU succeeds.
72  */
73 void become_logged_in(char *user, char *pass, char *serv_response)
74 {
75         WC->logged_in = 1;
76         extract(WC->wc_username, &serv_response[4], 0);
77         strcpy(WC->wc_password, pass);
78         WC->axlevel = extract_int(&serv_response[4], 1);
79         if (WC->axlevel >= 6)
80                 WC->is_aide = 1;
81 }
82
83
84 void do_login(void)
85 {
86         char buf[SIZ];
87         int need_regi = 0;
88
89
90         if (!strcasecmp(bstr("action"), "Exit")) {
91                 do_logout();
92                 return;
93         }
94         if (!strcasecmp(bstr("action"), "Login")) {
95                 serv_printf("USER %s", bstr("name"));
96                 serv_gets(buf);
97                 if (buf[0] == '3') {
98                         serv_printf("PASS %s", bstr("pass"));
99                         serv_gets(buf);
100                         if (buf[0] == '2') {
101                                 become_logged_in(bstr("name"),
102                                                  bstr("pass"), buf);
103                         } else {
104                                 display_login(&buf[4]);
105                                 return;
106                         }
107                 } else {
108                         display_login(&buf[4]);
109                         return;
110                 }
111         }
112         if (!strcasecmp(bstr("action"), "New User")) {
113                 serv_printf("NEWU %s", bstr("name"));
114                 serv_gets(buf);
115                 if (buf[0] == '2') {
116                         become_logged_in(bstr("name"), bstr("pass"), buf);
117                         serv_printf("SETP %s", bstr("pass"));
118                         serv_gets(buf);
119                 } else {
120                         display_login(&buf[4]);
121                         return;
122                 }
123         }
124         if (WC->logged_in) {
125                 serv_puts("CHEK");
126                 serv_gets(buf);
127                 if (buf[0] == '2') {
128                         WC->new_mail = extract_int(&buf[4], 0);
129                         need_regi = extract_int(&buf[4], 1);
130                         WC->need_vali = extract_int(&buf[4], 2);
131                 }
132                 if (need_regi) {
133                         display_reg(1);
134                 } else {
135                         do_welcome();
136                 }
137         } else {
138                 display_login("Your password was not accepted.");
139         }
140
141 }
142
143 void do_welcome(void)
144 {
145         http_redirect("/static/mainframeset.html");
146 }
147
148
149 /*
150  * Disconnect from the Citadel server, and end this WebCit session
151  */
152 void end_webcit_session(void) {
153         serv_puts("QUIT");
154         WC->killthis = 1;
155         /* close() of citadel socket will be done by do_housekeeping() */
156 }
157
158
159 void do_logout(void)
160 {
161         char buf[SIZ];
162
163         strcpy(WC->wc_username, "");
164         strcpy(WC->wc_password, "");
165         strcpy(WC->wc_roomname, "");
166
167         output_headers(2);      /* note "2" causes cookies to be unset */
168
169         wprintf("<CENTER>");
170         serv_puts("MESG goodbye");
171         serv_gets(buf);
172
173         if (buf[0] == '1')
174                 fmout(NULL);
175         else
176                 wprintf("Goodbye\n");
177
178         wprintf("<HR><A HREF=\"/\">Log in again</A>&nbsp;&nbsp;&nbsp;"
179                 "<A HREF=\"javascript:window.close();\">Close window</A>"
180                 "</CENTER>\n");
181         wDumpContent(2);
182         end_webcit_session();
183 }
184
185
186
187 /* 
188  * validate new users
189  */
190 void validate(void)
191 {
192         char cmd[SIZ];
193         char user[SIZ];
194         char buf[SIZ];
195         int a;
196
197         output_headers(1);
198
199         strcpy(buf, bstr("user"));
200         if (strlen(buf) > 0)
201                 if (strlen(bstr("WC->axlevel")) > 0) {
202                         serv_printf("VALI %s|%s", buf, bstr("WC->axlevel"));
203                         serv_gets(buf);
204                         if (buf[0] != '2') {
205                                 wprintf("<EM>%s</EM><BR>\n", &buf[4]);
206                         }
207                 }
208         serv_puts("GNUR");
209         serv_gets(buf);
210
211         if (buf[0] != '3') {
212                 wprintf("<EM>%s</EM><BR>\n", &buf[4]);
213                 wDumpContent(1);
214                 return;
215         }
216         strcpy(user, &buf[4]);
217         serv_printf("GREG %s", user);
218         serv_gets(cmd);
219         if (cmd[0] == '1') {
220                 a = 0;
221                 do {
222                         serv_gets(buf);
223                         ++a;
224                         if (a == 1)
225                                 wprintf("User #%s<BR><H1>%s</H1>",
226                                         buf, &cmd[4]);
227                         if (a == 2)
228                                 wprintf("PW: %s<BR>\n", buf);
229                         if (a == 3)
230                                 wprintf("%s<BR>\n", buf);
231                         if (a == 4)
232                                 wprintf("%s<BR>\n", buf);
233                         if (a == 5)
234                                 wprintf("%s, ", buf);
235                         if (a == 6)
236                                 wprintf("%s ", buf);
237                         if (a == 7)
238                                 wprintf("%s<BR>\n", buf);
239                         if (a == 8)
240                                 wprintf("%s<BR>\n", buf);
241                         if (a == 9)
242                                 wprintf("Current access level: %d (%s)\n",
243                                         atoi(buf), axdefs[atoi(buf)]);
244                 } while (strcmp(buf, "000"));
245         } else {
246                 wprintf("<H1>%s</H1>%s<BR>\n", user, &cmd[4]);
247         }
248
249         wprintf("<CENTER><TABLE border><CAPTION>Select access level:");
250         wprintf("</CAPTION><TR>");
251         for (a = 0; a <= 6; ++a) {
252                 wprintf(
253                                "<TD><A HREF=\"/validate&user=%s&WC->axlevel=%d\">%s</A></TD>\n",
254                                urlesc(user), a, axdefs[a]);
255         }
256         wprintf("</TR></TABLE><CENTER><BR>\n");
257         wDumpContent(1);
258 }
259
260
261
262
263
264
265 /* 
266  * Display form for registration.
267  * (Set during_login to 1 if this registration is being performed during
268  * new user login and will require chaining to the proper screen.)
269  */
270 void display_reg(int during_login)
271 {
272         char buf[SIZ];
273         int a;
274
275         output_headers(1);
276
277         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=007700><TR><TD>");
278         wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
279         wprintf("<B>Enter registration info</B>\n");
280         wprintf("</FONT></TD></TR></TABLE>\n");
281
282         wprintf("<CENTER>");
283         serv_puts("MESG register");
284         serv_gets(buf);
285         if (buf[0] == '1')
286                 fmout(NULL);
287
288         wprintf("<FORM ACTION=\"/register\" METHOD=\"POST\">\n");
289         wprintf("<INPUT TYPE=\"hidden\" NAME=\"during_login\" VALUE=\"%d\">\n", during_login);
290
291         serv_puts("GREG _SELF_");
292         serv_gets(buf);
293         if (buf[0] != '1') {
294                 wprintf("<EM>%s</EM><BR>\n", &buf[4]);
295         } else {
296
297                 wprintf("<H1>%s</H1><TABLE border>\n", &buf[4]);
298                 a = 0;
299                 while (serv_gets(buf), strcmp(buf, "000")) {
300                         ++a;
301                         wprintf("<TR><TD>");
302                         switch (a) {
303                         case 3:
304                                 wprintf("Real Name:</TD><TD><INPUT TYPE=\"text\" NAME=\"realname\" VALUE=\"%s\" MAXLENGTH=\"29\"><BR>\n", buf);
305                                 break;
306                         case 4:
307                                 wprintf("Street Address:</TD><TD><INPUT TYPE=\"text\" NAME=\"address\" VALUE=\"%s\" MAXLENGTH=\"24\"><BR>\n", buf);
308                                 break;
309                         case 5:
310                                 wprintf("City/town:</TD><TD><INPUT TYPE=\"text\" NAME=\"city\" VALUE=\"%s\" MAXLENGTH=\"14\"><BR>\n", buf);
311                                 break;
312                         case 6:
313                                 wprintf("State/province:</TD><TD><INPUT TYPE=\"text\" NAME=\"state\" VALUE=\"%s\" MAXLENGTH=\"2\"><BR>\n", buf);
314                                 break;
315                         case 7:
316                                 wprintf("ZIP/postal code:</TD><TD><INPUT TYPE=\"text\" NAME=\"zip\" VALUE=\"%s\" MAXLENGTH=\"10\"><BR>\n", buf);
317                                 break;
318                         case 8:
319                                 wprintf("Telephone:</TD><TD><INPUT TYPE=\"text\" NAME=\"phone\" VALUE=\"%s\" MAXLENGTH=\"14\"><BR>\n", buf);
320                                 break;
321                         case 10:
322                                 wprintf("E-Mail:</TD><TD><INPUT TYPE=\"text\" NAME=\"email\" VALUE=\"%s\" MAXLENGTH=\"31\"><BR>\n", buf);
323                                 break;
324                         case 11:
325                                 wprintf("Country:</TD><TD><INPUT TYPE=\"text\" NAME=\"country\" VALUE=\"%s\" MAXLENGTH=\"31\"><BR>\n", buf);
326                                 break;
327                         }
328                         wprintf("</TD></TR>\n");
329                 }
330                 wprintf("</TABLE><P>");
331         }
332         wprintf("<INPUT type=\"submit\" NAME=\"action\" VALUE=\"Register\">\n");
333         wprintf("<INPUT type=\"submit\" NAME=\"action\" VALUE=\"Cancel\">\n");
334         wprintf("</CENTER>\n");
335         wDumpContent(1);
336 }
337
338 /*
339  * register
340  */
341 void register_user(void)
342 {
343         char buf[SIZ];
344
345         if (strcmp(bstr("action"), "Register")) {
346                 display_error("Cancelled.  Registration was not saved.");
347                 return;
348         }
349         serv_puts("REGI");
350         serv_gets(buf);
351         if (buf[0] != '4') {
352                 display_error(&buf[4]);
353         }
354         serv_puts(bstr("realname"));
355         serv_puts(bstr("address"));
356         serv_puts(bstr("city"));
357         serv_puts(bstr("state"));
358         serv_puts(bstr("zip"));
359         serv_puts(bstr("phone"));
360         serv_puts(bstr("email"));
361         serv_puts(bstr("country"));
362         serv_puts("000");
363
364         if (atoi(bstr("during_login"))) {
365                 do_welcome();
366         } else {
367                 display_success("Registration information has been saved.");
368         }
369 }
370
371
372
373
374
375 /* 
376  * display form for changing your password
377  */
378 void display_changepw(void)
379 {
380         char buf[SIZ];
381
382         output_headers(1);
383
384         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=770000><TR><TD>");
385         wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
386         wprintf("<B>Change your password</B>\n");
387         wprintf("</FONT></TD></TR></TABLE>\n");
388
389         wprintf("<CENTER>");
390         serv_puts("MESG changepw");
391         serv_gets(buf);
392         if (buf[0] == '1')
393                 fmout(NULL);
394
395         wprintf("<FORM ACTION=\"changepw\" METHOD=\"POST\">\n");
396         wprintf("<CENTER><TABLE border><TR><TD>Enter new password:</TD>\n");
397         wprintf("<TD><INPUT TYPE=\"password\" NAME=\"newpass1\" VALUE=\"\" MAXLENGTH=\"20\"></TD></TR>\n");
398         wprintf("<TR><TD>Enter it again to confirm:</TD>\n");
399         wprintf("<TD><INPUT TYPE=\"password\" NAME=\"newpass2\" VALUE=\"\" MAXLENGTH=\"20\"></TD></TR>\n");
400         wprintf("</TABLE>\n");
401         wprintf("<INPUT type=\"submit\" NAME=\"action\" VALUE=\"Change\">\n");
402         wprintf("<INPUT type=\"submit\" NAME=\"action\" VALUE=\"Cancel\">\n");
403         wprintf("</CENTER>\n");
404         wDumpContent(1);
405 }
406
407 /*
408  * change password
409  */
410 void changepw(void)
411 {
412         char buf[SIZ];
413         char newpass1[32], newpass2[32];
414
415         if (strcmp(bstr("action"), "Change")) {
416                 display_error("Cancelled.  Password was not changed.");
417                 return;
418         }
419         strcpy(newpass1, bstr("newpass1"));
420         strcpy(newpass2, bstr("newpass2"));
421
422         if (strcasecmp(newpass1, newpass2)) {
423                 display_error("They don't match.  Password was not changed.");
424                 return;
425         }
426         serv_printf("SETP %s", newpass1);
427         serv_gets(buf);
428         if (buf[0] == '2')
429                 display_success(&buf[4]);
430         else
431                 display_error(&buf[4]);
432 }