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