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