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