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