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