3987ba394ce3c06e902c0fe39e08830efc9fd996
[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=\"content\">\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         http_redirect(startpage);
162 }
163
164
165 /*
166  * Disconnect from the Citadel server, and end this WebCit session
167  */
168 void end_webcit_session(void) {
169         serv_puts("QUIT");
170         WC->killthis = 1;
171         /* close() of citadel socket will be done by do_housekeeping() */
172 }
173
174
175 void do_logout(void)
176 {
177         char buf[SIZ];
178
179         strcpy(WC->wc_username, "");
180         strcpy(WC->wc_password, "");
181         strcpy(WC->wc_roomname, "");
182
183         /* Calling output_headers() this way causes the cookies to be un-set */
184         output_headers(1, 1, 0, 1, 0, 0, 0);
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  * validate new users
214  */
215 void validate(void)
216 {
217         char cmd[SIZ];
218         char user[SIZ];
219         char buf[SIZ];
220         int a;
221
222         output_headers(1, 1, 0, 0, 0, 0, 0);
223
224         strcpy(buf, bstr("user"));
225         if (strlen(buf) > 0)
226                 if (strlen(bstr("axlevel")) > 0) {
227                         serv_printf("VALI %s|%s", buf, bstr("axlevel"));
228                         serv_gets(buf);
229                         if (buf[0] != '2') {
230                                 wprintf("<b>%s</b><br />\n", &buf[4]);
231                         }
232                 }
233         serv_puts("GNUR");
234         serv_gets(buf);
235
236         if (buf[0] != '3') {
237                 wprintf("<b>%s</b><br />\n", &buf[4]);
238                 wDumpContent(1);
239                 return;
240         }
241
242         wprintf("<center>");
243         do_template("beginbox_nt");
244         wprintf("<center>");
245
246         strcpy(user, &buf[4]);
247         serv_printf("GREG %s", user);
248         serv_gets(cmd);
249         if (cmd[0] == '1') {
250                 a = 0;
251                 do {
252                         serv_gets(buf);
253                         ++a;
254                         if (a == 1)
255                                 wprintf("User #%s<br /><H1>%s</H1>",
256                                         buf, &cmd[4]);
257                         if (a == 2)
258                                 wprintf("PW: %s<br />\n", buf);
259                         if (a == 3)
260                                 wprintf("%s<br />\n", buf);
261                         if (a == 4)
262                                 wprintf("%s<br />\n", buf);
263                         if (a == 5)
264                                 wprintf("%s, ", buf);
265                         if (a == 6)
266                                 wprintf("%s ", buf);
267                         if (a == 7)
268                                 wprintf("%s<br />\n", buf);
269                         if (a == 8)
270                                 wprintf("%s<br />\n", buf);
271                         if (a == 9)
272                                 wprintf("Current access level: %d (%s)\n",
273                                         atoi(buf), axdefs[atoi(buf)]);
274                 } while (strcmp(buf, "000"));
275         } else {
276                 wprintf("<H1>%s</H1>%s<br />\n", user, &cmd[4]);
277         }
278
279         wprintf("<hr />Select access level for this user:<br />\n");
280         for (a = 0; a <= 6; ++a) {
281                 wprintf("<A HREF=\"/validate&user=");
282                 urlescputs(user);
283                 wprintf("&axlevel=%d\">%s</A>&nbsp;&nbsp;&nbsp;\n",
284                         a, axdefs[a]);
285         }
286         wprintf("<br />\n");
287
288         wprintf("</CENTER>\n");
289         do_template("endbox");
290         wprintf("</CENTER>\n");
291         wDumpContent(1);
292 }
293
294
295
296 /* 
297  * Display form for registration.
298  * (Set during_login to 1 if this registration is being performed during
299  * new user login and will require chaining to the proper screen.)
300  */
301 void display_reg(int during_login)
302 {
303         long vcard_msgnum;
304
305         if (goto_config_room() != 0) {
306                 if (during_login) do_welcome();
307                 else display_main_menu();
308                 return;
309         }
310
311         vcard_msgnum = locate_user_vcard(WC->wc_username, -1);
312         if (vcard_msgnum < 0L) {
313                 if (during_login) do_welcome();
314                 else display_main_menu();
315                 return;
316         }
317
318         if (during_login) {
319                 do_edit_vcard(vcard_msgnum, "1", "/do_welcome");
320         }
321         else {
322                 do_edit_vcard(vcard_msgnum, "1", "/display_main_menu");
323         }
324
325 }
326
327
328
329
330 /* 
331  * display form for changing your password
332  */
333 void display_changepw(void)
334 {
335         char buf[SIZ];
336
337         output_headers(1, 1, 0, 0, 0, 0, 0);
338
339         svprintf("BOXTITLE", WCS_STRING, "Change your password");
340         do_template("beginbox");
341         wprintf("<CENTER><br />");
342         serv_puts("MESG changepw");
343         serv_gets(buf);
344         if (buf[0] == '1') {
345                 fmout(NULL, "CENTER");
346         }
347
348         wprintf("<FORM ACTION=\"changepw\" METHOD=\"POST\">\n");
349         wprintf("<CENTER>"
350                 "<table border=\"0\" cellspacing=\"5\" cellpadding=\"5\" "
351                 "BGCOLOR=\"#EEEEEE\">"
352                 "<TR><TD>Enter new password:</TD>\n");
353         wprintf("<TD><INPUT TYPE=\"password\" NAME=\"newpass1\" VALUE=\"\" MAXLENGTH=\"20\"></TD></TR>\n");
354         wprintf("<TR><TD>Enter it again to confirm:</TD>\n");
355         wprintf("<TD><INPUT TYPE=\"password\" NAME=\"newpass2\" VALUE=\"\" MAXLENGTH=\"20\"></TD></TR>\n");
356         wprintf("</TABLE><br />\n");
357         wprintf("<INPUT type=\"submit\" NAME=\"action\" VALUE=\"Change\">\n"
358                 "&nbsp;"
359                 "<INPUT type=\"submit\" NAME=\"action\" VALUE=\"Cancel\">\n");
360         wprintf("</CENTER>\n");
361         do_template("endbox");
362         wDumpContent(1);
363 }
364
365 /*
366  * change password
367  */
368 void changepw(void)
369 {
370         char buf[SIZ];
371         char newpass1[32], newpass2[32];
372
373         if (strcmp(bstr("action"), "Change")) {
374                 strcpy(WC->ImportantMessage, 
375                         "Cancelled.  Password was not changed.");
376                 display_main_menu();
377                 return;
378         }
379         strcpy(newpass1, bstr("newpass1"));
380         strcpy(newpass2, bstr("newpass2"));
381
382         if (strcasecmp(newpass1, newpass2)) {
383                 strcpy(WC->ImportantMessage, 
384                         "They don't match.  Password was not changed.");
385                 display_main_menu();
386                 return;
387         }
388         serv_printf("SETP %s", newpass1);
389         serv_gets(buf);
390         strcpy(WC->ImportantMessage, &buf[4]);
391         display_main_menu();
392 }