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