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