50e42586d968b18f488814460138b05bc42cd99e
[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, 0);
53                 svprintf("mesg", WCS_STRING, "%s", buf);
54         }
55
56         svprintf("hello", WCS_SERVCMD, "MESG hello");
57         svprintf("BOXTITLE", WCS_STRING, "%s - powered by Citadel",
58                 serv_info.serv_humannode);
59
60         do_template("login");
61
62         wDumpContent(0);        /* No menu here; not logged in yet! */
63 }
64
65
66
67
68 /*
69  * This function needs to get called whenever the session changes from
70  * not-logged-in to logged-in, either by an explicit login by the user or
71  * by a timed-out session automatically re-establishing with a little help
72  * from the browser cookie.  Either way, we need to load access controls and
73  * preferences from the server.
74  */
75 void become_logged_in(char *user, char *pass, char *serv_response)
76 {
77         char buf[SIZ];
78
79         WC->logged_in = 1;
80         extract(WC->wc_username, &serv_response[4], 0);
81         strcpy(WC->wc_password, pass);
82         WC->axlevel = extract_int(&serv_response[4], 1);
83         if (WC->axlevel >= 6) {
84                 WC->is_aide = 1;
85         }
86
87         load_preferences();
88
89         serv_puts("CHEK");
90         serv_gets(buf);
91         if (buf[0] == '2') {
92                 WC->new_mail = extract_int(&buf[4], 0);
93                 WC->need_regi = extract_int(&buf[4], 1);
94                 WC->need_vali = extract_int(&buf[4], 2);
95                 extract(WC->cs_inet_email, &buf[4], 3);
96         }
97 }
98
99
100 void do_login(void)
101 {
102         char buf[SIZ];
103
104         if (!strcasecmp(bstr("action"), "Exit")) {
105                 do_logout();
106                 return;
107         }
108         if (!strcasecmp(bstr("action"), "Login")) {
109                 serv_printf("USER %s", bstr("name"));
110                 serv_gets(buf);
111                 if (buf[0] == '3') {
112                         serv_printf("PASS %s", bstr("pass"));
113                         serv_gets(buf);
114                         if (buf[0] == '2') {
115                                 become_logged_in(bstr("name"),
116                                                  bstr("pass"), buf);
117                         } else {
118                                 display_login(&buf[4]);
119                                 return;
120                         }
121                 } else {
122                         display_login(&buf[4]);
123                         return;
124                 }
125         }
126         if (!strcasecmp(bstr("action"), "New User")) {
127                 serv_printf("NEWU %s", bstr("name"));
128                 serv_gets(buf);
129                 if (buf[0] == '2') {
130                         become_logged_in(bstr("name"), bstr("pass"), buf);
131                         serv_printf("SETP %s", bstr("pass"));
132                         serv_gets(buf);
133                 } else {
134                         display_login(&buf[4]);
135                         return;
136                 }
137         }
138         if (WC->logged_in) {
139                 if (WC->need_regi) {
140                         display_reg(1);
141                 } else {
142                         do_welcome();
143                 }
144         } else {
145                 display_login("Your password was not accepted.");
146         }
147
148 }
149
150 void do_welcome(void)
151 {
152         char startpage[SIZ];
153
154         get_preference("startpage", startpage);
155         if (strlen(startpage)==0) {
156                 strcpy(startpage, "/dotskip&room=_BASEROOM_");
157                 set_preference("startpage", startpage);
158         }
159
160         svprintf("STARTPAGE", WCS_STRING, startpage);
161
162         do_template("mainframeset");
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, "CENTER");
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("axlevel")) > 0) {
228                         serv_printf("VALI %s|%s", buf, bstr("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
243         wprintf("<CENTER>");
244         do_template("beginbox_nt");
245         wprintf("<CENTER>");
246
247         strcpy(user, &buf[4]);
248         serv_printf("GREG %s", user);
249         serv_gets(cmd);
250         if (cmd[0] == '1') {
251                 a = 0;
252                 do {
253                         serv_gets(buf);
254                         ++a;
255                         if (a == 1)
256                                 wprintf("User #%s<BR><H1>%s</H1>",
257                                         buf, &cmd[4]);
258                         if (a == 2)
259                                 wprintf("PW: %s<BR>\n", buf);
260                         if (a == 3)
261                                 wprintf("%s<BR>\n", buf);
262                         if (a == 4)
263                                 wprintf("%s<BR>\n", buf);
264                         if (a == 5)
265                                 wprintf("%s, ", buf);
266                         if (a == 6)
267                                 wprintf("%s ", buf);
268                         if (a == 7)
269                                 wprintf("%s<BR>\n", buf);
270                         if (a == 8)
271                                 wprintf("%s<BR>\n", buf);
272                         if (a == 9)
273                                 wprintf("Current access level: %d (%s)\n",
274                                         atoi(buf), axdefs[atoi(buf)]);
275                 } while (strcmp(buf, "000"));
276         } else {
277                 wprintf("<H1>%s</H1>%s<BR>\n", user, &cmd[4]);
278         }
279
280         wprintf("<HR>Select access level for this user:<BR>\n");
281         for (a = 0; a <= 6; ++a) {
282                 wprintf("<A HREF=\"/validate&user=");
283                 urlescputs(user);
284                 wprintf("&axlevel=%d\">%s</A>&nbsp;&nbsp;&nbsp;\n",
285                         a, axdefs[a]);
286         }
287         wprintf("<BR>\n");
288
289         wprintf("</CENTER>\n");
290         do_template("endbox");
291         wprintf("</CENTER>\n");
292         wDumpContent(1);
293 }
294
295
296
297 /* 
298  * Display form for registration.
299  * (Set during_login to 1 if this registration is being performed during
300  * new user login and will require chaining to the proper screen.)
301  */
302 void display_reg(int during_login)
303 {
304         long vcard_msgnum;
305
306         if (goto_config_room() != 0) {
307                 if (during_login) do_welcome();
308                 else display_main_menu();
309                 return;
310         }
311
312         vcard_msgnum = locate_user_vcard(WC->wc_username, -1);
313         if (vcard_msgnum < 0L) {
314                 if (during_login) do_welcome();
315                 else display_main_menu();
316                 return;
317         }
318
319         if (during_login) {
320                 do_edit_vcard(vcard_msgnum, "1", "/do_welcome");
321         }
322         else {
323                 do_edit_vcard(vcard_msgnum, "1", "/display_main_menu");
324         }
325
326 }
327
328
329
330
331 /* 
332  * display form for changing your password
333  */
334 void display_changepw(void)
335 {
336         char buf[SIZ];
337
338         output_headers(3);
339
340         svprintf("BOXTITLE", WCS_STRING, "Change your password");
341         do_template("beginbox");
342         wprintf("<CENTER><BR>");
343         serv_puts("MESG changepw");
344         serv_gets(buf);
345         if (buf[0] == '1') {
346                 fmout(NULL, "CENTER");
347         }
348
349         wprintf("<FORM ACTION=\"changepw\" METHOD=\"POST\">\n");
350         wprintf("<CENTER>"
351                 "<table border=\"0\" cellspacing=\"5\" cellpadding=\"5\" "
352                 "BGCOLOR=\"#EEEEEE\">"
353                 "<TR><TD>Enter new password:</TD>\n");
354         wprintf("<TD><INPUT TYPE=\"password\" NAME=\"newpass1\" VALUE=\"\" MAXLENGTH=\"20\"></TD></TR>\n");
355         wprintf("<TR><TD>Enter it again to confirm:</TD>\n");
356         wprintf("<TD><INPUT TYPE=\"password\" NAME=\"newpass2\" VALUE=\"\" MAXLENGTH=\"20\"></TD></TR>\n");
357         wprintf("</TABLE><BR>\n");
358         wprintf("<INPUT type=\"submit\" NAME=\"action\" VALUE=\"Change\">\n"
359                 "&nbsp;"
360                 "<INPUT type=\"submit\" NAME=\"action\" VALUE=\"Cancel\">\n");
361         wprintf("</CENTER>\n");
362         do_template("endbox");
363         wDumpContent(1);
364 }
365
366 /*
367  * change password
368  */
369 void changepw(void)
370 {
371         char buf[SIZ];
372         char newpass1[32], newpass2[32];
373
374         if (strcmp(bstr("action"), "Change")) {
375                 strcpy(WC->ImportantMessage, 
376                         "Cancelled.  Password was not changed.");
377                 display_main_menu();
378                 return;
379         }
380         strcpy(newpass1, bstr("newpass1"));
381         strcpy(newpass2, bstr("newpass2"));
382
383         if (strcasecmp(newpass1, newpass2)) {
384                 strcpy(WC->ImportantMessage, 
385                         "They don't match.  Password was not changed.");
386                 display_main_menu();
387                 return;
388         }
389         serv_printf("SETP %s", newpass1);
390         serv_gets(buf);
391         strcpy(WC->ImportantMessage, &buf[4]);
392         display_main_menu();
393 }