* Cleaned up the rcs/cvs Id tags and leading comments at the top of some files
[citadel.git] / webcit / auth.c
1 /*
2  * $Id$
3  *
4  * Handles authentication of users to a Citadel server.
5  *
6  */
7
8
9 #include <ctype.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <fcntl.h>
14 #include <signal.h>
15 #include <sys/types.h>
16 #include <sys/wait.h>
17 #include <sys/socket.h>
18 #include <sys/time.h>
19 #include <limits.h>
20 #include <netinet/in.h>
21 #include <netdb.h>
22 #include <string.h>
23 #include <pwd.h>
24 #include <errno.h>
25 #include <stdarg.h>
26 #include <pthread.h>
27 #include <signal.h>
28 #include "webcit.h"
29
30 char *axdefs[] =
31 {
32         "Deleted",
33         "New User",
34         "Problem User",
35         "Local User",
36         "Network User",
37         "Preferred User",
38         "Aide"
39 };
40
41 /*
42  * Display the login screen
43  */
44 void display_login(char *mesg)
45 {
46         char buf[SIZ];
47
48         output_headers(1, 1, 2, 0, 0, 0, 0);
49         wprintf("<div id=\"content\">\n");
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(2);
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         http_redirect(startpage);
161 }
162
163
164 /*
165  * Disconnect from the Citadel server, and end this WebCit session
166  */
167 void end_webcit_session(void) {
168         serv_puts("QUIT");
169         WC->killthis = 1;
170         /* close() of citadel socket will be done by do_housekeeping() */
171 }
172
173
174 void do_logout(void)
175 {
176         char buf[SIZ];
177
178         strcpy(WC->wc_username, "");
179         strcpy(WC->wc_password, "");
180         strcpy(WC->wc_roomname, "");
181
182         /* Calling output_headers() this way causes the cookies to be un-set */
183         output_headers(1, 1, 0, 1, 0, 0, 0);
184
185         wprintf("<center>");
186         serv_puts("MESG goodbye");
187         serv_gets(buf);
188
189         if (WC->serv_sock >= 0) {
190                 if (buf[0] == '1') {
191                         fmout(NULL, "CENTER");
192                 } else {
193                         wprintf("Goodbye\n");
194                 }
195         }
196         else {
197                 wprintf("This program was unable to connect or stay "
198                         "connected to the Citadel server.  Please report "
199                         "this problem to your system administrator."
200                 );
201         }
202
203         wprintf("<hr /><a href=\"/\">Log in again</A>&nbsp;&nbsp;&nbsp;"
204                 "<a href=\"javascript:window.close();\">Close window</A>"
205                 "</center>\n");
206         wDumpContent(2);
207         end_webcit_session();
208 }
209
210
211 /* 
212  * validate new users
213  */
214 void validate(void)
215 {
216         char cmd[SIZ];
217         char user[SIZ];
218         char buf[SIZ];
219         int a;
220
221         output_headers(1, 1, 0, 0, 0, 0, 0);
222
223         strcpy(buf, bstr("user"));
224         if (strlen(buf) > 0)
225                 if (strlen(bstr("axlevel")) > 0) {
226                         serv_printf("VALI %s|%s", buf, bstr("axlevel"));
227                         serv_gets(buf);
228                         if (buf[0] != '2') {
229                                 wprintf("<b>%s</b><br />\n", &buf[4]);
230                         }
231                 }
232         serv_puts("GNUR");
233         serv_gets(buf);
234
235         if (buf[0] != '3') {
236                 wprintf("<b>%s</b><br />\n", &buf[4]);
237                 wDumpContent(1);
238                 return;
239         }
240
241         wprintf("<center>");
242         do_template("beginbox_nt");
243         wprintf("<center>");
244
245         strcpy(user, &buf[4]);
246         serv_printf("GREG %s", user);
247         serv_gets(cmd);
248         if (cmd[0] == '1') {
249                 a = 0;
250                 do {
251                         serv_gets(buf);
252                         ++a;
253                         if (a == 1)
254                                 wprintf("User #%s<br /><H1>%s</H1>",
255                                         buf, &cmd[4]);
256                         if (a == 2)
257                                 wprintf("PW: %s<br />\n", buf);
258                         if (a == 3)
259                                 wprintf("%s<br />\n", buf);
260                         if (a == 4)
261                                 wprintf("%s<br />\n", buf);
262                         if (a == 5)
263                                 wprintf("%s, ", buf);
264                         if (a == 6)
265                                 wprintf("%s ", buf);
266                         if (a == 7)
267                                 wprintf("%s<br />\n", buf);
268                         if (a == 8)
269                                 wprintf("%s<br />\n", buf);
270                         if (a == 9)
271                                 wprintf("Current access level: %d (%s)\n",
272                                         atoi(buf), axdefs[atoi(buf)]);
273                 } while (strcmp(buf, "000"));
274         } else {
275                 wprintf("<H1>%s</H1>%s<br />\n", user, &cmd[4]);
276         }
277
278         wprintf("<hr />Select access level for this user:<br />\n");
279         for (a = 0; a <= 6; ++a) {
280                 wprintf("<A HREF=\"/validate&user=");
281                 urlescputs(user);
282                 wprintf("&axlevel=%d\">%s</A>&nbsp;&nbsp;&nbsp;\n",
283                         a, axdefs[a]);
284         }
285         wprintf("<br />\n");
286
287         wprintf("</CENTER>\n");
288         do_template("endbox");
289         wprintf("</CENTER>\n");
290         wDumpContent(1);
291 }
292
293
294
295 /* 
296  * Display form for registration.
297  * (Set during_login to 1 if this registration is being performed during
298  * new user login and will require chaining to the proper screen.)
299  */
300 void display_reg(int during_login)
301 {
302         long vcard_msgnum;
303
304         if (goto_config_room() != 0) {
305                 if (during_login) do_welcome();
306                 else display_main_menu();
307                 return;
308         }
309
310         vcard_msgnum = locate_user_vcard(WC->wc_username, -1);
311         if (vcard_msgnum < 0L) {
312                 if (during_login) do_welcome();
313                 else display_main_menu();
314                 return;
315         }
316
317         if (during_login) {
318                 do_edit_vcard(vcard_msgnum, "1", "/do_welcome");
319         }
320         else {
321                 do_edit_vcard(vcard_msgnum, "1", "/display_main_menu");
322         }
323
324 }
325
326
327
328
329 /* 
330  * display form for changing your password
331  */
332 void display_changepw(void)
333 {
334         char buf[SIZ];
335
336         output_headers(1, 1, 0, 0, 0, 0, 0);
337
338         svprintf("BOXTITLE", WCS_STRING, "Change your password");
339         do_template("beginbox");
340         wprintf("<CENTER><br />");
341         serv_puts("MESG changepw");
342         serv_gets(buf);
343         if (buf[0] == '1') {
344                 fmout(NULL, "CENTER");
345         }
346
347         wprintf("<FORM ACTION=\"changepw\" METHOD=\"POST\">\n");
348         wprintf("<CENTER>"
349                 "<table border=\"0\" cellspacing=\"5\" cellpadding=\"5\" "
350                 "BGCOLOR=\"#EEEEEE\">"
351                 "<TR><TD>Enter new password:</TD>\n");
352         wprintf("<TD><INPUT TYPE=\"password\" NAME=\"newpass1\" VALUE=\"\" MAXLENGTH=\"20\"></TD></TR>\n");
353         wprintf("<TR><TD>Enter it again to confirm:</TD>\n");
354         wprintf("<TD><INPUT TYPE=\"password\" NAME=\"newpass2\" VALUE=\"\" MAXLENGTH=\"20\"></TD></TR>\n");
355         wprintf("</TABLE><br />\n");
356         wprintf("<INPUT type=\"submit\" NAME=\"action\" VALUE=\"Change\">\n"
357                 "&nbsp;"
358                 "<INPUT type=\"submit\" NAME=\"action\" VALUE=\"Cancel\">\n");
359         wprintf("</CENTER>\n");
360         do_template("endbox");
361         wDumpContent(1);
362 }
363
364 /*
365  * change password
366  */
367 void changepw(void)
368 {
369         char buf[SIZ];
370         char newpass1[32], newpass2[32];
371
372         if (strcmp(bstr("action"), "Change")) {
373                 strcpy(WC->ImportantMessage, 
374                         "Cancelled.  Password was not changed.");
375                 display_main_menu();
376                 return;
377         }
378         strcpy(newpass1, bstr("newpass1"));
379         strcpy(newpass2, bstr("newpass2"));
380
381         if (strcasecmp(newpass1, newpass2)) {
382                 strcpy(WC->ImportantMessage, 
383                         "They don't match.  Password was not changed.");
384                 display_main_menu();
385                 return;
386         }
387         serv_printf("SETP %s", newpass1);
388         serv_gets(buf);
389         strcpy(WC->ImportantMessage, &buf[4]);
390         display_main_menu();
391 }