a48e2277190519f8a0ed549312c3faf6567e9096
[citadel.git] / webcit / auth.c
1 /*
2  * $Id$
3  *
4  * Handles authentication of users to a Citadel server.
5  *
6  */
7
8
9 #include <ctype.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <fcntl.h>
14 #include <signal.h>
15 #include <sys/types.h>
16 #include <sys/wait.h>
17 #include <sys/socket.h>
18 #include <sys/time.h>
19 #include <limits.h>
20 #include <netinet/in.h>
21 #include <netdb.h>
22 #include <string.h>
23 #include <pwd.h>
24 #include <errno.h>
25 #include <stdarg.h>
26 #include <pthread.h>
27 #include <signal.h>
28 #include "webcit.h"
29
30 char *axdefs[] =
31 {
32         "Deleted",
33         "New User",
34         "Problem User",
35         "Local User",
36         "Network User",
37         "Preferred User",
38         "Aide"
39 };
40
41 /*
42  * Display the login screen
43  */
44 void display_login(char *mesg)
45 {
46         char buf[SIZ];
47
48         output_headers(1, 1, 2, 0, 0, 0, 0);
49         wprintf("<div style=\"position:absolute; top:20px; left:20px; right:20px\">\n");
50
51         if (mesg != NULL) if (strlen(mesg) > 0) {
52                 stresc(buf, mesg, 0, 0);
53                 svprintf("mesg", WCS_STRING, "%s", buf);
54         }
55
56         svprintf("hello", WCS_SERVCMD, "MESG hello");
57         svprintf("BOXTITLE", WCS_STRING, "%s - powered by Citadel",
58                 serv_info.serv_humannode);
59
60         do_template("login");
61
62         wDumpContent(2);
63 }
64
65
66
67
68 /*
69  * This function needs to get called whenever the session changes from
70  * not-logged-in to logged-in, either by an explicit login by the user or
71  * by a timed-out session automatically re-establishing with a little help
72  * from the browser cookie.  Either way, we need to load access controls and
73  * preferences from the server.
74  */
75 void become_logged_in(char *user, char *pass, char *serv_response)
76 {
77         char buf[SIZ];
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
87         load_preferences();
88
89         serv_puts("CHEK");
90         serv_gets(buf);
91         if (buf[0] == '2') {
92                 WC->new_mail = extract_int(&buf[4], 0);
93                 WC->need_regi = extract_int(&buf[4], 1);
94                 WC->need_vali = extract_int(&buf[4], 2);
95                 extract(WC->cs_inet_email, &buf[4], 3);
96         }
97 }
98
99
100 void do_login(void)
101 {
102         char buf[SIZ];
103
104         if (!strcasecmp(bstr("action"), "Exit")) {
105                 do_logout();
106                 return;
107         }
108         if (!strcasecmp(bstr("action"), "Login")) {
109                 serv_printf("USER %s", bstr("name"));
110                 serv_gets(buf);
111                 if (buf[0] == '3') {
112                         serv_printf("PASS %s", bstr("pass"));
113                         serv_gets(buf);
114                         if (buf[0] == '2') {
115                                 become_logged_in(bstr("name"),
116                                                  bstr("pass"), buf);
117                         } else {
118                                 display_login(&buf[4]);
119                                 return;
120                         }
121                 } else {
122                         display_login(&buf[4]);
123                         return;
124                 }
125         }
126         if (!strcasecmp(bstr("action"), "New User")) {
127                 if (strlen(bstr("pass")) == 0) {
128                         display_login("Blank passwords are not allowed.");
129                         return;
130                 }
131                 serv_printf("NEWU %s", bstr("name"));
132                 serv_gets(buf);
133                 if (buf[0] == '2') {
134                         become_logged_in(bstr("name"), bstr("pass"), buf);
135                         serv_printf("SETP %s", bstr("pass"));
136                         serv_gets(buf);
137                 } else {
138                         display_login(&buf[4]);
139                         return;
140                 }
141         }
142         if (WC->logged_in) {
143                 if (WC->need_regi) {
144                         display_reg(1);
145                 } else {
146                         do_welcome();
147                 }
148         } else {
149                 display_login("Your password was not accepted.");
150         }
151
152 }
153
154 void do_welcome(void)
155 {
156         char buf[SIZ];
157         FILE *fp;
158         int i;
159
160 #ifdef XXX_NOT_FINISHED_YET_XXX
161         /*
162          * See if we have to run the first-time setup wizard
163          */
164         if (WC->is_aide) {
165                 if (!setup_wizard) {
166                         sprintf(wizard_filename, "setupwiz.%s.%s",
167                                 ctdlhost, ctdlport);
168                         for (i=0; i<strlen(wizard_filename); ++i) {
169                                 if (    (wizard_filename[i]==' ')
170                                         || (wizard_filename[i] == '/')
171                                 ) {
172                                         wizard_filename[i] = '_';
173                                 }
174                         }
175         
176                         fp = fopen(wizard_filename, "r");
177                         if (fp != NULL) {
178                                 fgets(buf, sizeof buf, fp);
179                                 buf[strlen(buf)-1] = 0;
180                                 fclose(fp);
181                                 if (atoi(buf) == serv_info.serv_rev_level) {
182                                         setup_wizard = 1; /* already run */
183                                 }
184                         }
185                 }
186
187                 if (!setup_wizard) {
188                         http_redirect("/setup_wizard");
189                 }
190         }
191 #endif
192
193         /*
194          * Go to the user's preferred start page
195          */
196         get_preference("startpage", buf);
197         if (strlen(buf)==0) {
198                 strcpy(buf, "/dotskip&room=_BASEROOM_");
199                 set_preference("startpage", buf);
200         }
201         http_redirect(buf);
202 }
203
204
205 /*
206  * Disconnect from the Citadel server, and end this WebCit session
207  */
208 void end_webcit_session(void) {
209         serv_puts("QUIT");
210         WC->killthis = 1;
211         /* close() of citadel socket will be done by do_housekeeping() */
212 }
213
214
215 void do_logout(void)
216 {
217         char buf[SIZ];
218
219         strcpy(WC->wc_username, "");
220         strcpy(WC->wc_password, "");
221         strcpy(WC->wc_roomname, "");
222
223         /* Calling output_headers() this way causes the cookies to be un-set */
224         output_headers(1, 1, 0, 1, 0, 0, 0);
225
226         wprintf("<center>");
227         serv_puts("MESG goodbye");
228         serv_gets(buf);
229
230         if (WC->serv_sock >= 0) {
231                 if (buf[0] == '1') {
232                         fmout(NULL, "CENTER");
233                 } else {
234                         wprintf("Goodbye\n");
235                 }
236         }
237         else {
238                 wprintf("This program was unable to connect or stay "
239                         "connected to the Citadel server.  Please report "
240                         "this problem to your system administrator."
241                 );
242         }
243
244         wprintf("<hr /><a href=\"/\">Log in again</A>&nbsp;&nbsp;&nbsp;"
245                 "<a href=\"javascript:window.close();\">Close window</A>"
246                 "</center>\n");
247         wDumpContent(2);
248         end_webcit_session();
249 }
250
251
252 /* 
253  * validate new users
254  */
255 void validate(void)
256 {
257         char cmd[SIZ];
258         char user[SIZ];
259         char buf[SIZ];
260         int a;
261
262         output_headers(1, 1, 2, 0, 0, 0, 0);
263         wprintf("<div id=\"banner\">\n"
264                 "<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#444455\"><TR><TD>"
265                 "<SPAN CLASS=\"titlebar\">Validate new users</SPAN>"
266                 "</TD></TR></TABLE>\n"
267                 "</div>\n<div id=\"content\">\n"
268         );
269                                                                                                                              
270         strcpy(buf, bstr("user"));
271         if (strlen(buf) > 0)
272                 if (strlen(bstr("axlevel")) > 0) {
273                         serv_printf("VALI %s|%s", buf, bstr("axlevel"));
274                         serv_gets(buf);
275                         if (buf[0] != '2') {
276                                 wprintf("<b>%s</b><br />\n", &buf[4]);
277                         }
278                 }
279         serv_puts("GNUR");
280         serv_gets(buf);
281
282         if (buf[0] != '3') {
283                 wprintf("<b>%s</b><br />\n", &buf[4]);
284                 wDumpContent(1);
285                 return;
286         }
287
288         wprintf("<div id=\"fix_scrollbar_bug\">"
289                 "<table border=0 width=100%% bgcolor=\"#ffffff\"><tr><td>\n");
290         wprintf("<center>");
291
292         strcpy(user, &buf[4]);
293         serv_printf("GREG %s", user);
294         serv_gets(cmd);
295         if (cmd[0] == '1') {
296                 a = 0;
297                 do {
298                         serv_gets(buf);
299                         ++a;
300                         if (a == 1)
301                                 wprintf("User #%s<br /><H1>%s</H1>",
302                                         buf, &cmd[4]);
303                         if (a == 2)
304                                 wprintf("PW: %s<br />\n", buf);
305                         if (a == 3)
306                                 wprintf("%s<br />\n", buf);
307                         if (a == 4)
308                                 wprintf("%s<br />\n", buf);
309                         if (a == 5)
310                                 wprintf("%s, ", buf);
311                         if (a == 6)
312                                 wprintf("%s ", buf);
313                         if (a == 7)
314                                 wprintf("%s<br />\n", buf);
315                         if (a == 8)
316                                 wprintf("%s<br />\n", buf);
317                         if (a == 9)
318                                 wprintf("Current access level: %d (%s)\n",
319                                         atoi(buf), axdefs[atoi(buf)]);
320                 } while (strcmp(buf, "000"));
321         } else {
322                 wprintf("<H1>%s</H1>%s<br />\n", user, &cmd[4]);
323         }
324
325         wprintf("<hr />Select access level for this user:<br />\n");
326         for (a = 0; a <= 6; ++a) {
327                 wprintf("<A HREF=\"/validate&user=");
328                 urlescputs(user);
329                 wprintf("&axlevel=%d\">%s</A>&nbsp;&nbsp;&nbsp;\n",
330                         a, axdefs[a]);
331         }
332         wprintf("<br />\n");
333
334         wprintf("</CENTER>\n");
335         wprintf("</td></tr></table></div>\n");
336         wDumpContent(1);
337 }
338
339
340
341 /* 
342  * Display form for registration.
343  * (Set during_login to 1 if this registration is being performed during
344  * new user login and will require chaining to the proper screen.)
345  */
346 void display_reg(int during_login)
347 {
348         long vcard_msgnum;
349
350         if (goto_config_room() != 0) {
351                 if (during_login) do_welcome();
352                 else display_main_menu();
353                 return;
354         }
355
356         vcard_msgnum = locate_user_vcard(WC->wc_username, -1);
357         if (vcard_msgnum < 0L) {
358                 if (during_login) do_welcome();
359                 else display_main_menu();
360                 return;
361         }
362
363         if (during_login) {
364                 do_edit_vcard(vcard_msgnum, "1", "/do_welcome");
365         }
366         else {
367                 do_edit_vcard(vcard_msgnum, "1", "/display_main_menu");
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, 1, 2, 0, 0, 0, 0);
383         wprintf("<div id=\"banner\">\n"
384                 "<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#444455\"><TR><TD>"
385                 "<SPAN CLASS=\"titlebar\">Change your password</SPAN>"
386                 "</TD></TR></TABLE>\n"
387                 "</div>\n<div id=\"content\">\n"
388         );
389
390         if (strlen(WC->ImportantMessage) > 0) {
391                 do_template("beginbox_nt");
392                 wprintf("<SPAN CLASS=\"errormsg\">"
393                         "%s</SPAN><br />\n", WC->ImportantMessage);
394                 do_template("endbox");
395                 strcpy(WC->ImportantMessage, "");
396         }
397
398         wprintf("<div id=\"fix_scrollbar_bug\">"
399                 "<table border=0 width=100%% bgcolor=\"#ffffff\"><tr><td>\n");
400
401         wprintf("<CENTER><br />");
402         serv_puts("MESG changepw");
403         serv_gets(buf);
404         if (buf[0] == '1') {
405                 fmout(NULL, "CENTER");
406         }
407
408         wprintf("<form name=\"changepwform\" action=\"changepw\" method=\"post\">\n");
409         wprintf("<CENTER>"
410                 "<table border=\"0\" cellspacing=\"5\" cellpadding=\"5\" "
411                 "BGCOLOR=\"#EEEEEE\">"
412                 "<TR><TD>Enter new password:</TD>\n");
413         wprintf("<TD><INPUT TYPE=\"password\" NAME=\"newpass1\" VALUE=\"\" MAXLENGTH=\"20\"></TD></TR>\n");
414         wprintf("<TR><TD>Enter it again to confirm:</TD>\n");
415         wprintf("<TD><INPUT TYPE=\"password\" NAME=\"newpass2\" VALUE=\"\" MAXLENGTH=\"20\"></TD></TR>\n");
416
417         wprintf("</TABLE><br />\n");
418         wprintf("<INPUT type=\"submit\" name=\"action\" value=\"Change\">"
419                 "&nbsp;"
420                 "<INPUT type=\"submit\" name=\"action\" value=\"Cancel\">\n");
421         wprintf("</form></center>\n");
422         wprintf("</td></tr></table></div>\n");
423         wDumpContent(1);
424 }
425
426 /*
427  * change password
428  */
429 void changepw(void)
430 {
431         char buf[SIZ];
432         char newpass1[32], newpass2[32];
433
434         if (strcmp(bstr("action"), "Change")) {
435                 strcpy(WC->ImportantMessage, 
436                         "Cancelled.  Password was not changed.");
437                 display_main_menu();
438                 return;
439         }
440
441         strcpy(newpass1, bstr("newpass1"));
442         strcpy(newpass2, bstr("newpass2"));
443
444         if (strcasecmp(newpass1, newpass2)) {
445                 strcpy(WC->ImportantMessage, 
446                         "They don't match.  Password was not changed.");
447                 display_changepw();
448                 return;
449         }
450
451         if (strlen(newpass1) == 0) {
452                 strcpy(WC->ImportantMessage, 
453                         "Blank passwords are not allowed.");
454                 display_changepw();
455                 return;
456         }
457
458         serv_printf("SETP %s", newpass1);
459         serv_gets(buf);
460         sprintf(WC->ImportantMessage, "%s", &buf[4]);
461         if (buf[0] == '2') {
462                 safestrncpy(WC->wc_password, buf, sizeof WC->wc_password);
463                 display_main_menu();
464         }
465         else {
466                 display_changepw();
467         }
468 }