]> code.citadel.org Git - citadel.git/blob - webcit/auth.c
* If you paid for this software, someone is ripping you off.
[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         stresc(buf, serv_info.serv_humannode, 1);
57         svprintf("humannode", WCS_STRING, "%s", buf);
58
59         svprintf("hello", WCS_SERVCMD, "MESG hello");
60
61         do_template("login.html");
62
63         clear_local_substs();
64         wDumpContent(0);        /* No menu here; not logged in yet! */
65 }
66
67
68
69
70 /*
71  * This function needs to get called whenever a PASS or NEWU succeeds.
72  */
73 void become_logged_in(char *user, char *pass, char *serv_response)
74 {
75         WC->logged_in = 1;
76         extract(WC->wc_username, &serv_response[4], 0);
77         strcpy(WC->wc_password, pass);
78         WC->axlevel = extract_int(&serv_response[4], 1);
79         if (WC->axlevel >= 6)
80                 WC->is_aide = 1;
81 }
82
83
84 void do_login(void)
85 {
86         char buf[SIZ];
87         int need_regi = 0;
88
89
90         if (!strcasecmp(bstr("action"), "Exit")) {
91                 do_logout();
92                 return;
93         }
94         if (!strcasecmp(bstr("action"), "Login")) {
95                 serv_printf("USER %s", bstr("name"));
96                 serv_gets(buf);
97                 if (buf[0] == '3') {
98                         serv_printf("PASS %s", bstr("pass"));
99                         serv_gets(buf);
100                         if (buf[0] == '2') {
101                                 become_logged_in(bstr("name"),
102                                                  bstr("pass"), buf);
103                         } else {
104                                 display_login(&buf[4]);
105                                 return;
106                         }
107                 } else {
108                         display_login(&buf[4]);
109                         return;
110                 }
111         }
112         if (!strcasecmp(bstr("action"), "New User")) {
113                 serv_printf("NEWU %s", bstr("name"));
114                 serv_gets(buf);
115                 if (buf[0] == '2') {
116                         become_logged_in(bstr("name"), bstr("pass"), buf);
117                         serv_printf("SETP %s", bstr("pass"));
118                         serv_gets(buf);
119                 } else {
120                         display_login(&buf[4]);
121                         return;
122                 }
123         }
124         if (WC->logged_in) {
125                 serv_puts("CHEK");
126                 serv_gets(buf);
127                 if (buf[0] == '2') {
128                         WC->new_mail = extract_int(&buf[4], 0);
129                         need_regi = extract_int(&buf[4], 1);
130                         WC->need_vali = extract_int(&buf[4], 2);
131                 }
132                 if (need_regi) {
133                         display_reg(1);
134                 } else {
135                         do_welcome();
136                 }
137         } else {
138                 display_login("Your password was not accepted.");
139         }
140
141 }
142
143 void do_welcome(void)
144 {
145         http_redirect("/static/mainframeset.html");
146 }
147
148
149 /*
150  * Disconnect from the Citadel server, and end this WebCit session
151  */
152 void end_webcit_session(void) {
153         serv_puts("QUIT");
154         WC->killthis = 1;
155         /* close() of citadel socket will be done by do_housekeeping() */
156 }
157
158
159 void do_logout(void)
160 {
161         char buf[SIZ];
162
163         strcpy(WC->wc_username, "");
164         strcpy(WC->wc_password, "");
165         strcpy(WC->wc_roomname, "");
166
167         output_headers(2);      /* note "2" causes cookies to be unset */
168
169         wprintf("<CENTER>");
170         serv_puts("MESG goodbye");
171         serv_gets(buf);
172
173         if (buf[0] == '1')
174                 fmout(NULL);
175         else
176                 wprintf("Goodbye\n");
177
178         wprintf("<HR><A HREF=\"/\">Log in again</A>&nbsp;&nbsp;&nbsp;"
179                 "<A HREF=\"javascript:window.close();\">Close window</A>"
180                 "</CENTER>\n");
181         wDumpContent(2);
182         end_webcit_session();
183 }
184
185
186
187 /* 
188  * validate new users
189  */
190 void validate(void)
191 {
192         char cmd[SIZ];
193         char user[SIZ];
194         char buf[SIZ];
195         int a;
196
197         output_headers(1);
198
199         strcpy(buf, bstr("user"));
200         if (strlen(buf) > 0)
201                 if (strlen(bstr("WC->axlevel")) > 0) {
202                         serv_printf("VALI %s|%s", buf, bstr("WC->axlevel"));
203                         serv_gets(buf);
204                         if (buf[0] != '2') {
205                                 wprintf("<EM>%s</EM><BR>\n", &buf[4]);
206                         }
207                 }
208         serv_puts("GNUR");
209         serv_gets(buf);
210
211         if (buf[0] != '3') {
212                 wprintf("<EM>%s</EM><BR>\n", &buf[4]);
213                 wDumpContent(1);
214                 return;
215         }
216         strcpy(user, &buf[4]);
217         serv_printf("GREG %s", user);
218         serv_gets(cmd);
219         if (cmd[0] == '1') {
220                 a = 0;
221                 do {
222                         serv_gets(buf);
223                         ++a;
224                         if (a == 1)
225                                 wprintf("User #%s<BR><H1>%s</H1>",
226                                         buf, &cmd[4]);
227                         if (a == 2)
228                                 wprintf("PW: %s<BR>\n", buf);
229                         if (a == 3)
230                                 wprintf("%s<BR>\n", buf);
231                         if (a == 4)
232                                 wprintf("%s<BR>\n", buf);
233                         if (a == 5)
234                                 wprintf("%s, ", buf);
235                         if (a == 6)
236                                 wprintf("%s ", buf);
237                         if (a == 7)
238                                 wprintf("%s<BR>\n", buf);
239                         if (a == 8)
240                                 wprintf("%s<BR>\n", buf);
241                         if (a == 9)
242                                 wprintf("Current access level: %d (%s)\n",
243                                         atoi(buf), axdefs[atoi(buf)]);
244                 } while (strcmp(buf, "000"));
245         } else {
246                 wprintf("<H1>%s</H1>%s<BR>\n", user, &cmd[4]);
247         }
248
249         wprintf("<CENTER><TABLE border><CAPTION>Select access level:");
250         wprintf("</CAPTION><TR>");
251         for (a = 0; a <= 6; ++a) {
252                 wprintf("<TD><A HREF=\"/validate&user=");
253                 urlescputs(user);
254                 wprintf("&WC->axlevel=%d\">%s</A></TD>\n",
255                         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[SIZ];
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[SIZ];
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_success("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[SIZ];
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[SIZ];
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 }