* Removed all of the absolute URL's.
[citadel.git] / webcit / auth.c
1 /*
2  * $Id$
3  *
4  * Handles authentication of users to a Citadel server.
5  *
6  */
7
8 #include "webcit.h"
9
10 char *axdefs[] =
11 {
12         "Deleted",
13         "New User",
14         "Problem User",
15         "Local User",
16         "Network User",
17         "Preferred User",
18         "Aide"
19 };
20
21 /*
22  * Display the login screen
23  */
24 void display_login(char *mesg)
25 {
26         char buf[SIZ];
27
28         output_headers(1, 1, 2, 0, 0, 0);
29         wprintf("<div style=\"position:absolute; top:20px; left:20px; right:20px\">\n");
30
31         if (mesg != NULL) if (strlen(mesg) > 0) {
32                 stresc(buf, mesg, 0, 0);
33                 svprintf("mesg", WCS_STRING, "%s", buf);
34         }
35
36         svprintf("LOGIN_INSTRUCTIONS", WCS_STRING,
37                 _("<ul>"
38                 "<li><b>If you already have an account on %s</b>, "
39                 "enter your user name and password and click &quot;Login.&quot; "
40                 "<li><b>If you are a new user</b>, enter the name and password "
41                 "you wish to use, "
42                 "and click &quot;New User.&quot; "
43                 "<li>Please log off properly when finished. "
44                 "<li>You must use a browser that supports <i>frames</i> and "
45                 "<i>cookies</i>. "
46                 "<li>Also keep in mind that if your browser is "
47                 "configured to block pop-up windows, you will not be able "
48                 "to receive any instant messages.<br />"
49                 "</ul>"),
50                 serv_info.serv_humannode
51         );
52
53         svprintf("LOGIN_BUTTON", WCS_STRING, "%s", _("Login"));
54         svprintf("NEWUSER_BUTTON", WCS_STRING, "%s", _("New User"));
55         svprintf("EXIT_BUTTON", WCS_STRING, "%s", _("Exit"));
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_token(WC->wc_username, &serv_response[4], 0, '|', sizeof WC->wc_username);
81         safestrncpy(WC->wc_password, pass, sizeof WC->wc_password);
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_getln(buf, sizeof 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_token(WC->cs_inet_email, &buf[4], 3, '|', sizeof WC->cs_inet_email);
96         }
97 }
98
99
100 void do_login(void)
101 {
102         char buf[SIZ];
103
104         if (strlen(bstr("exit_action")) > 0) {
105                 do_logout();
106                 return;
107         }
108         if (strlen(bstr("login_action")) > 0) {
109                 serv_printf("USER %s", bstr("name"));
110                 serv_getln(buf, sizeof buf);
111                 if (buf[0] == '3') {
112                         serv_printf("PASS %s", bstr("pass"));
113                         serv_getln(buf, sizeof 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 (strlen(bstr("newuser_action")) > 0) {
127                 if (strlen(bstr("pass")) == 0) {
128                         display_login(_("Blank passwords are not allowed."));
129                         return;
130                 }
131                 serv_printf("NEWU %s", bstr("name"));
132                 serv_getln(buf, sizeof buf);
133                 if (buf[0] == '2') {
134                         become_logged_in(bstr("name"), bstr("pass"), buf);
135                         serv_printf("SETP %s", bstr("pass"));
136                         serv_getln(buf, sizeof buf);
137                 } else {
138                         display_login(&buf[4]);
139                         return;
140                 }
141         }
142         if (WC->logged_in) {
143                 if (WC->need_regi) {
144                         display_reg(1);
145                 } else {
146                         do_welcome();
147                 }
148         } else {
149                 display_login(_("Your password was not accepted."));
150         }
151
152 }
153
154 void do_welcome(void)
155 {
156         char buf[SIZ];
157 #ifdef XXX_NOT_FINISHED_YET_XXX
158         FILE *fp;
159         int i;
160
161         /*
162          * See if we have to run the first-time setup wizard
163          */
164         if (WC->is_aide) {
165                 if (!setup_wizard) {
166                         sprintf(wizard_filename, "setupwiz.%s.%s",
167                                 ctdlhost, ctdlport);
168                         for (i=0; i<strlen(wizard_filename); ++i) {
169                                 if (    (wizard_filename[i]==' ')
170                                         || (wizard_filename[i] == '/')
171                                 ) {
172                                         wizard_filename[i] = '_';
173                                 }
174                         }
175         
176                         fp = fopen(wizard_filename, "r");
177                         if (fp != NULL) {
178                                 fgets(buf, sizeof buf, fp);
179                                 buf[strlen(buf)-1] = 0;
180                                 fclose(fp);
181                                 if (atoi(buf) == serv_info.serv_rev_level) {
182                                         setup_wizard = 1; /* already run */
183                                 }
184                         }
185                 }
186
187                 if (!setup_wizard) {
188                         http_redirect("/setup_wizard");
189                 }
190         }
191 #endif
192
193         /*
194          * Go to the user's preferred start page
195          */
196         get_preference("startpage", buf, sizeof buf);
197         if (strlen(buf)==0) {
198                 safestrncpy(buf, "/dotskip&room=_BASEROOM_", sizeof buf);
199                 set_preference("startpage", buf, 1);
200         }
201         http_redirect(buf);
202 }
203
204
205 /*
206  * Disconnect from the Citadel server, and end this WebCit session
207  */
208 void end_webcit_session(void) {
209         serv_puts("QUIT");
210         WC->killthis = 1;
211         /* close() of citadel socket will be done by do_housekeeping() */
212 }
213
214
215 void do_logout(void)
216 {
217         char buf[SIZ];
218
219         safestrncpy(WC->wc_username, "", sizeof WC->wc_username);
220         safestrncpy(WC->wc_password, "", sizeof WC->wc_password);
221         safestrncpy(WC->wc_roomname, "", sizeof WC->wc_roomname);
222
223         /* Calling output_headers() this way causes the cookies to be un-set */
224         output_headers(1, 1, 0, 1, 0, 0);
225
226         wprintf("<center>");
227         serv_puts("MESG goodbye");
228         serv_getln(buf, sizeof buf);
229
230         if (WC->serv_sock >= 0) {
231                 if (buf[0] == '1') {
232                         fmout("CENTER");
233                 } else {
234                         wprintf("Goodbye\n");
235                 }
236         }
237         else {
238                 wprintf(_("This program was unable to connect or stay "
239                         "connected to the Citadel server.  Please report "
240                         "this problem to your system administrator.")
241                 );
242         }
243
244         wprintf("<hr /><a href=\".\">Log in again</A>&nbsp;&nbsp;&nbsp;"
245                 "<a href=\"javascript:window.close();\">");
246         wprintf(_("Close window"));
247         wprintf("</a></center>\n");
248         wDumpContent(2);
249         end_webcit_session();
250 }
251
252
253 /* 
254  * validate new users
255  */
256 void validate(void)
257 {
258         char cmd[SIZ];
259         char user[SIZ];
260         char buf[SIZ];
261         int a;
262
263         output_headers(1, 1, 2, 0, 0, 0);
264         wprintf("<div id=\"banner\">\n"
265                 "<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#444455\"><TR><TD>"
266                 "<SPAN CLASS=\"titlebar\">");
267         wprintf(_("Validate new users"));
268         wprintf("</SPAN></TD></TR></TABLE>\n</div>\n<div id=\"content\">\n");
269
270         safestrncpy(buf, bstr("user"), sizeof buf);
271         if (strlen(buf) > 0)
272                 if (strlen(bstr("axlevel")) > 0) {
273                         serv_printf("VALI %s|%s", buf, bstr("axlevel"));
274                         serv_getln(buf, sizeof buf);
275                         if (buf[0] != '2') {
276                                 wprintf("<b>%s</b><br />\n", &buf[4]);
277                         }
278                 }
279         serv_puts("GNUR");
280         serv_getln(buf, sizeof buf);
281
282         if (buf[0] != '3') {
283                 wprintf("<b>%s</b><br />\n", &buf[4]);
284                 wDumpContent(1);
285                 return;
286         }
287
288         wprintf("<div id=\"fix_scrollbar_bug\">"
289                 "<table border=0 width=100%% bgcolor=\"#ffffff\"><tr><td>\n");
290         wprintf("<center>");
291
292         safestrncpy(user, &buf[4], sizeof user);
293         serv_printf("GREG %s", user);
294         serv_getln(cmd, sizeof cmd);
295         if (cmd[0] == '1') {
296                 a = 0;
297                 do {
298                         serv_getln(buf, sizeof buf);
299                         ++a;
300                         if (a == 1)
301                                 wprintf("User #%s<br /><H1>%s</H1>",
302                                         buf, &cmd[4]);
303                         if (a == 2)
304                                 wprintf("PW: %s<br />\n", buf);
305                         if (a == 3)
306                                 wprintf("%s<br />\n", buf);
307                         if (a == 4)
308                                 wprintf("%s<br />\n", buf);
309                         if (a == 5)
310                                 wprintf("%s, ", buf);
311                         if (a == 6)
312                                 wprintf("%s ", buf);
313                         if (a == 7)
314                                 wprintf("%s<br />\n", buf);
315                         if (a == 8)
316                                 wprintf("%s<br />\n", buf);
317                         if (a == 9)
318                                 wprintf(_("Current access level: %d (%s)\n"),
319                                         atoi(buf), axdefs[atoi(buf)]);
320                 } while (strcmp(buf, "000"));
321         } else {
322                 wprintf("<H1>%s</H1>%s<br />\n", user, &cmd[4]);
323         }
324
325         wprintf("<hr />");
326         wprintf(_("Select access level for this user:"));
327         wprintf("<br />\n");
328         for (a = 0; a <= 6; ++a) {
329                 wprintf("<a href=\"validate&user=");
330                 urlescputs(user);
331                 wprintf("&axlevel=%d\">%s</A>&nbsp;&nbsp;&nbsp;\n",
332                         a, axdefs[a]);
333         }
334         wprintf("<br />\n");
335
336         wprintf("</CENTER>\n");
337         wprintf("</td></tr></table></div>\n");
338         wDumpContent(1);
339 }
340
341
342
343 /* 
344  * Display form for registration.
345  * (Set during_login to 1 if this registration is being performed during
346  * new user login and will require chaining to the proper screen.)
347  */
348 void display_reg(int during_login)
349 {
350         long vcard_msgnum;
351
352         if (goto_config_room() != 0) {
353                 if (during_login) do_welcome();
354                 else display_main_menu();
355                 return;
356         }
357
358         vcard_msgnum = locate_user_vcard(WC->wc_username, -1);
359         if (vcard_msgnum < 0L) {
360                 if (during_login) do_welcome();
361                 else display_main_menu();
362                 return;
363         }
364
365         if (during_login) {
366                 do_edit_vcard(vcard_msgnum, "1", "/do_welcome");
367         }
368         else {
369                 do_edit_vcard(vcard_msgnum, "1", "/display_main_menu");
370         }
371
372 }
373
374
375
376
377 /* 
378  * display form for changing your password
379  */
380 void display_changepw(void)
381 {
382         char buf[SIZ];
383
384         output_headers(1, 1, 2, 0, 0, 0);
385         wprintf("<div id=\"banner\">\n"
386                 "<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#444455\"><TR><TD>"
387                 "<SPAN CLASS=\"titlebar\">");
388         wprintf(_("Change your password"));
389         wprintf("</SPAN>"
390                 "</TD></TR></TABLE>\n"
391                 "</div>\n<div id=\"content\">\n"
392         );
393
394         if (strlen(WC->ImportantMessage) > 0) {
395                 do_template("beginbox_nt");
396                 wprintf("<SPAN CLASS=\"errormsg\">"
397                         "%s</SPAN><br />\n", WC->ImportantMessage);
398                 do_template("endbox");
399                 safestrncpy(WC->ImportantMessage, "", sizeof WC->ImportantMessage);
400         }
401
402         wprintf("<div id=\"fix_scrollbar_bug\">"
403                 "<table border=0 width=100%% bgcolor=\"#ffffff\"><tr><td>\n");
404
405         wprintf("<CENTER><br />");
406         serv_puts("MESG changepw");
407         serv_getln(buf, sizeof buf);
408         if (buf[0] == '1') {
409                 fmout("CENTER");
410         }
411
412         wprintf("<form name=\"changepwform\" action=\"changepw\" method=\"post\">\n");
413         wprintf("<CENTER>"
414                 "<table border=\"0\" cellspacing=\"5\" cellpadding=\"5\" "
415                 "BGCOLOR=\"#EEEEEE\">"
416                 "<TR><TD>");
417         wprintf(_("Enter new password:"));
418         wprintf("</TD>\n");
419         wprintf("<TD><INPUT TYPE=\"password\" NAME=\"newpass1\" VALUE=\"\" MAXLENGTH=\"20\"></TD></TR>\n");
420         wprintf("<TR><TD>");
421         wprintf(_("Enter it again to confirm:"));
422         wprintf("</TD>\n");
423         wprintf("<TD><INPUT TYPE=\"password\" NAME=\"newpass2\" VALUE=\"\" MAXLENGTH=\"20\"></TD></TR>\n");
424
425         wprintf("</TABLE><br />\n");
426         wprintf("<INPUT type=\"submit\" name=\"change_action\" value=\"%s\">", _("Change password"));
427         wprintf("&nbsp;");
428         wprintf("<INPUT type=\"submit\" name=\"cancel_action\" value=\"%s\">\n", _("Cancel"));
429         wprintf("</form></center>\n");
430         wprintf("</td></tr></table></div>\n");
431         wDumpContent(1);
432 }
433
434 /*
435  * change password
436  */
437 void changepw(void)
438 {
439         char buf[SIZ];
440         char newpass1[32], newpass2[32];
441
442         if (strlen(bstr("change_action")) == 0) {
443                 safestrncpy(WC->ImportantMessage, 
444                         _("Cancelled.  Password was not changed."),
445                         sizeof WC->ImportantMessage);
446                 display_main_menu();
447                 return;
448         }
449
450         safestrncpy(newpass1, bstr("newpass1"), sizeof newpass1);
451         safestrncpy(newpass2, bstr("newpass2"), sizeof newpass2);
452
453         if (strcasecmp(newpass1, newpass2)) {
454                 safestrncpy(WC->ImportantMessage, 
455                         _("They don't match.  Password was not changed."),
456                         sizeof WC->ImportantMessage);
457                 display_changepw();
458                 return;
459         }
460
461         if (strlen(newpass1) == 0) {
462                 safestrncpy(WC->ImportantMessage, 
463                         _("Blank passwords are not allowed."),
464                         sizeof WC->ImportantMessage);
465                 display_changepw();
466                 return;
467         }
468
469         serv_printf("SETP %s", newpass1);
470         serv_getln(buf, sizeof buf);
471         sprintf(WC->ImportantMessage, "%s", &buf[4]);
472         if (buf[0] == '2') {
473                 safestrncpy(WC->wc_password, buf, sizeof WC->wc_password);
474                 display_main_menu();
475         }
476         else {
477                 display_changepw();
478         }
479 }