* *** HUGE CHANGES *** *** WARNING: NOT FULLY FUNCTIONAL ***
[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(1, 1, 2, 0, 0, 0, 0);
50         wprintf("<div id=\"text\">\n");
51
52         if (mesg != NULL) if (strlen(mesg) > 0) {
53                 stresc(buf, mesg, 0, 0);
54                 svprintf("mesg", WCS_STRING, "%s", buf);
55         }
56
57         svprintf("hello", WCS_SERVCMD, "MESG hello");
58         svprintf("BOXTITLE", WCS_STRING, "%s - powered by Citadel",
59                 serv_info.serv_humannode);
60
61         do_template("login");
62
63         wDumpContent(2);
64 }
65
66
67
68
69 /*
70  * This function needs to get called whenever the session changes from
71  * not-logged-in to logged-in, either by an explicit login by the user or
72  * by a timed-out session automatically re-establishing with a little help
73  * from the browser cookie.  Either way, we need to load access controls and
74  * preferences from the server.
75  */
76 void become_logged_in(char *user, char *pass, char *serv_response)
77 {
78         char buf[SIZ];
79
80         WC->logged_in = 1;
81         extract(WC->wc_username, &serv_response[4], 0);
82         strcpy(WC->wc_password, pass);
83         WC->axlevel = extract_int(&serv_response[4], 1);
84         if (WC->axlevel >= 6) {
85                 WC->is_aide = 1;
86         }
87
88         load_preferences();
89
90         serv_puts("CHEK");
91         serv_gets(buf);
92         if (buf[0] == '2') {
93                 WC->new_mail = extract_int(&buf[4], 0);
94                 WC->need_regi = extract_int(&buf[4], 1);
95                 WC->need_vali = extract_int(&buf[4], 2);
96                 extract(WC->cs_inet_email, &buf[4], 3);
97         }
98 }
99
100
101 void do_login(void)
102 {
103         char buf[SIZ];
104
105         if (!strcasecmp(bstr("action"), "Exit")) {
106                 do_logout();
107                 return;
108         }
109         if (!strcasecmp(bstr("action"), "Login")) {
110                 serv_printf("USER %s", bstr("name"));
111                 serv_gets(buf);
112                 if (buf[0] == '3') {
113                         serv_printf("PASS %s", bstr("pass"));
114                         serv_gets(buf);
115                         if (buf[0] == '2') {
116                                 become_logged_in(bstr("name"),
117                                                  bstr("pass"), buf);
118                         } else {
119                                 display_login(&buf[4]);
120                                 return;
121                         }
122                 } else {
123                         display_login(&buf[4]);
124                         return;
125                 }
126         }
127         if (!strcasecmp(bstr("action"), "New User")) {
128                 serv_printf("NEWU %s", bstr("name"));
129                 serv_gets(buf);
130                 if (buf[0] == '2') {
131                         become_logged_in(bstr("name"), bstr("pass"), buf);
132                         serv_printf("SETP %s", bstr("pass"));
133                         serv_gets(buf);
134                 } else {
135                         display_login(&buf[4]);
136                         return;
137                 }
138         }
139         if (WC->logged_in) {
140                 if (WC->need_regi) {
141                         display_reg(1);
142                 } else {
143                         do_welcome();
144                 }
145         } else {
146                 display_login("Your password was not accepted.");
147         }
148
149 }
150
151 void do_welcome(void)
152 {
153         char startpage[SIZ];
154
155         get_preference("startpage", startpage);
156         if (strlen(startpage)==0) {
157                 strcpy(startpage, "/dotskip&room=_BASEROOM_");
158                 set_preference("startpage", startpage);
159         }
160
161         svprintf("STARTPAGE", WCS_STRING, startpage);
162
163         /* FIXME ... go to my start page, not to the summary */
164         summary();
165 }
166
167
168 /*
169  * Disconnect from the Citadel server, and end this WebCit session
170  */
171 void end_webcit_session(void) {
172         serv_puts("QUIT");
173         WC->killthis = 1;
174         /* close() of citadel socket will be done by do_housekeeping() */
175 }
176
177
178 void do_logout(void)
179 {
180         char buf[SIZ];
181
182         strcpy(WC->wc_username, "");
183         strcpy(WC->wc_password, "");
184         strcpy(WC->wc_roomname, "");
185
186         /* Calling output_headers() this way causes the cookies to be un-set */
187         output_headers(1, 1, 0, 1, 0, 0, 0);
188
189         wprintf("<CENTER>");
190         serv_puts("MESG goodbye");
191         serv_gets(buf);
192
193         if (WC->serv_sock >= 0) {
194                 if (buf[0] == '1') {
195                         fmout(NULL, "CENTER");
196                 } else {
197                         wprintf("Goodbye\n");
198                 }
199         }
200         else {
201                 wprintf("This program was unable to connect or stay "
202                         "connected to the Citadel server.  Please report "
203                         "this problem to your system administrator."
204                 );
205         }
206
207         wprintf("<hr /><A HREF=\"/\">Log in again</A>&nbsp;&nbsp;&nbsp;"
208                 "<A HREF=\"javascript:window.close();\">Close window</A>"
209                 "</CENTER>\n");
210         wDumpContent(2);
211         end_webcit_session();
212 }
213
214
215 /* 
216  * validate new users
217  */
218 void validate(void)
219 {
220         char cmd[SIZ];
221         char user[SIZ];
222         char buf[SIZ];
223         int a;
224
225         output_headers(1, 1, 0, 0, 0, 0, 0);
226
227         strcpy(buf, bstr("user"));
228         if (strlen(buf) > 0)
229                 if (strlen(bstr("axlevel")) > 0) {
230                         serv_printf("VALI %s|%s", buf, bstr("axlevel"));
231                         serv_gets(buf);
232                         if (buf[0] != '2') {
233                                 wprintf("<EM>%s</EM><br />\n", &buf[4]);
234                         }
235                 }
236         serv_puts("GNUR");
237         serv_gets(buf);
238
239         if (buf[0] != '3') {
240                 wprintf("<EM>%s</EM><br />\n", &buf[4]);
241                 wDumpContent(1);
242                 return;
243         }
244
245         wprintf("<CENTER>");
246         do_template("beginbox_nt");
247         wprintf("<CENTER>");
248
249         strcpy(user, &buf[4]);
250         serv_printf("GREG %s", user);
251         serv_gets(cmd);
252         if (cmd[0] == '1') {
253                 a = 0;
254                 do {
255                         serv_gets(buf);
256                         ++a;
257                         if (a == 1)
258                                 wprintf("User #%s<br /><H1>%s</H1>",
259                                         buf, &cmd[4]);
260                         if (a == 2)
261                                 wprintf("PW: %s<br />\n", buf);
262                         if (a == 3)
263                                 wprintf("%s<br />\n", buf);
264                         if (a == 4)
265                                 wprintf("%s<br />\n", buf);
266                         if (a == 5)
267                                 wprintf("%s, ", buf);
268                         if (a == 6)
269                                 wprintf("%s ", buf);
270                         if (a == 7)
271                                 wprintf("%s<br />\n", buf);
272                         if (a == 8)
273                                 wprintf("%s<br />\n", buf);
274                         if (a == 9)
275                                 wprintf("Current access level: %d (%s)\n",
276                                         atoi(buf), axdefs[atoi(buf)]);
277                 } while (strcmp(buf, "000"));
278         } else {
279                 wprintf("<H1>%s</H1>%s<br />\n", user, &cmd[4]);
280         }
281
282         wprintf("<hr />Select access level for this user:<br />\n");
283         for (a = 0; a <= 6; ++a) {
284                 wprintf("<A HREF=\"/validate&user=");
285                 urlescputs(user);
286                 wprintf("&axlevel=%d\">%s</A>&nbsp;&nbsp;&nbsp;\n",
287                         a, axdefs[a]);
288         }
289         wprintf("<br />\n");
290
291         wprintf("</CENTER>\n");
292         do_template("endbox");
293         wprintf("</CENTER>\n");
294         wDumpContent(1);
295 }
296
297
298
299 /* 
300  * Display form for registration.
301  * (Set during_login to 1 if this registration is being performed during
302  * new user login and will require chaining to the proper screen.)
303  */
304 void display_reg(int during_login)
305 {
306         long vcard_msgnum;
307
308         if (goto_config_room() != 0) {
309                 if (during_login) do_welcome();
310                 else display_main_menu();
311                 return;
312         }
313
314         vcard_msgnum = locate_user_vcard(WC->wc_username, -1);
315         if (vcard_msgnum < 0L) {
316                 if (during_login) do_welcome();
317                 else display_main_menu();
318                 return;
319         }
320
321         if (during_login) {
322                 do_edit_vcard(vcard_msgnum, "1", "/do_welcome");
323         }
324         else {
325                 do_edit_vcard(vcard_msgnum, "1", "/display_main_menu");
326         }
327
328 }
329
330
331
332
333 /* 
334  * display form for changing your password
335  */
336 void display_changepw(void)
337 {
338         char buf[SIZ];
339
340         output_headers(1, 1, 0, 0, 0, 0, 0);
341
342         svprintf("BOXTITLE", WCS_STRING, "Change your password");
343         do_template("beginbox");
344         wprintf("<CENTER><br />");
345         serv_puts("MESG changepw");
346         serv_gets(buf);
347         if (buf[0] == '1') {
348                 fmout(NULL, "CENTER");
349         }
350
351         wprintf("<FORM ACTION=\"changepw\" METHOD=\"POST\">\n");
352         wprintf("<CENTER>"
353                 "<table border=\"0\" cellspacing=\"5\" cellpadding=\"5\" "
354                 "BGCOLOR=\"#EEEEEE\">"
355                 "<TR><TD>Enter new password:</TD>\n");
356         wprintf("<TD><INPUT TYPE=\"password\" NAME=\"newpass1\" VALUE=\"\" MAXLENGTH=\"20\"></TD></TR>\n");
357         wprintf("<TR><TD>Enter it again to confirm:</TD>\n");
358         wprintf("<TD><INPUT TYPE=\"password\" NAME=\"newpass2\" VALUE=\"\" MAXLENGTH=\"20\"></TD></TR>\n");
359         wprintf("</TABLE><br />\n");
360         wprintf("<INPUT type=\"submit\" NAME=\"action\" VALUE=\"Change\">\n"
361                 "&nbsp;"
362                 "<INPUT type=\"submit\" NAME=\"action\" VALUE=\"Cancel\">\n");
363         wprintf("</CENTER>\n");
364         do_template("endbox");
365         wDumpContent(1);
366 }
367
368 /*
369  * change password
370  */
371 void changepw(void)
372 {
373         char buf[SIZ];
374         char newpass1[32], newpass2[32];
375
376         if (strcmp(bstr("action"), "Change")) {
377                 strcpy(WC->ImportantMessage, 
378                         "Cancelled.  Password was not changed.");
379                 display_main_menu();
380                 return;
381         }
382         strcpy(newpass1, bstr("newpass1"));
383         strcpy(newpass2, bstr("newpass2"));
384
385         if (strcasecmp(newpass1, newpass2)) {
386                 strcpy(WC->ImportantMessage, 
387                         "They don't match.  Password was not changed.");
388                 display_main_menu();
389                 return;
390         }
391         serv_printf("SETP %s", newpass1);
392         serv_gets(buf);
393         strcpy(WC->ImportantMessage, &buf[4]);
394         display_main_menu();
395 }