* Smoothed out some of the login/logout code. Failed authentication
[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 #include <stdlib.h>
10 #ifdef HAVE_UNISTD_H
11 #include <unistd.h>
12 #endif
13 #include <stdio.h>
14 #include <ctype.h>
15 #include <string.h>
16 #include <errno.h>
17 #include "webcit.h"
18 #include "child.h"
19
20
21
22 /*
23  * Display the login screen
24  */
25 void display_login(char *mesg) {
26         char buf[256];
27
28         printf("HTTP/1.0 200 OK\n");
29         output_headers();
30
31         wprintf("<HTML><BODY>\n");
32         wprintf("<CENTER><TABLE border=0 width=100%><TR><TD>\n");
33
34         /* FIX replace with the correct image */
35         wprintf("<IMG SRC=\"/static/velma.gif\">");
36         wprintf("</TD><TD><CENTER>\n");
37
38         if (mesg != NULL) {
39                 wprintf("<font size=+1><b>%s</b></font>", mesg);
40                 }
41         else {
42                 serv_puts("MESG hello");
43                 serv_gets(buf);
44                 if (buf[0]=='1') fmout(NULL);
45                 }
46
47         wprintf("</CENTER></TD></TR></TABLE></CENTER>\n");
48
49         wprintf("<HR>\n");
50         /* FIX add instructions here */
51         wprintf("<CENTER><FORM ACTION=\"/login\" METHOD=\"POST\">\n");
52         wprintf("<TABLE border><TR>\n");
53         wprintf("<TD>User Name:</TD>\n");
54         wprintf("<TD><INPUT TYPE=\"text\" NAME=\"name\" MAXLENGTH=\"25\">\n");
55         wprintf("</TD></TR><TR>\n");
56         wprintf("<TD>Password:</TD>\n");
57         wprintf("<TD><INPUT TYPE=\"password\" NAME=\"pass\" MAXLENGTH=\"20\"></TD>\n");
58         wprintf("</TR></TABLE>\n");
59         wprintf("<INPUT type=\"submit\" NAME=\"action\" VALUE=\"Login\">\n");
60         wprintf("<INPUT type=\"submit\" NAME=\"action\" VALUE=\"New User\">\n");
61         wprintf("<INPUT type=\"submit\" NAME=\"action\" VALUE=\"Exit\">\n");
62         wprintf("</FORM></CENTER>\n");
63         wprintf("</BODY></HTML>\n");
64         wDumpContent();
65         }
66
67
68
69
70 /*
71  * This function needs to get called whenever a PASS or NEWU succeeds
72  */
73 void become_logged_in(char *user, char *pass, char *serv_response) {
74         logged_in = 1;
75         extract(wc_username, &serv_response[4], 0);
76         strcpy(wc_password, pass);
77         axlevel = extract_int(&serv_response[4], 1);
78         if (axlevel >=6) is_aide = 1;
79         fprintf(stderr, "become_logged_in(%s,%s)\n", user, pass);
80         }
81
82
83 void do_login(void) {
84         char buf[256];
85
86         if (!strcasecmp(bstr("action"), "Exit")) {
87                 do_logout();
88                 }
89
90         if (!strcasecmp(bstr("action"), "Login")) {
91                 serv_printf("USER %s", bstr("name"));
92                 serv_gets(buf);
93                 if (buf[0]=='3') {
94                         serv_printf("PASS %s", bstr("pass"));
95                         serv_gets(buf);
96                         if (buf[0]=='2') {
97                                 become_logged_in(bstr("name"),
98                                         bstr("pass"), buf);
99                                 }
100                         else {
101                                 display_login(&buf[4]);
102                                 return;
103                                 }
104                         }
105                 else {
106                         display_login(&buf[4]);
107                         return;
108                         }
109                 }
110
111         if (!strcasecmp(bstr("action"), "New User")) {
112                 display_login("New user login not yet supported.");
113                 return;
114                 }
115
116         if (logged_in) {
117                 output_static("frameset.html");
118                 }
119         else {
120                 display_login("Your password was not accepted.");
121                 }
122
123         }
124
125 void do_welcome(void) {
126         printf("HTTP/1.0 200 OK\n");
127         output_headers();
128         wprintf("<HTML><BODY>\n");
129         wprintf("<CENTER><H1>");
130         escputs(wc_username);
131         wprintf("</H1>\n");
132         /* other stuff here */
133         wprintf("</BODY></HTML>\n");
134         wDumpContent();
135         }
136
137
138 void do_logout(void) {
139         char buf[256];
140
141         strcpy(wc_username, "");
142         strcpy(wc_password, "");
143         strcpy(wc_roomname, "");
144         strcpy(wc_host, "");
145         strcpy(wc_port, "");
146
147         printf("HTTP/1.0 200 OK\n");
148         output_headers();
149         printf("X-WebCit-Session: close\n");
150         
151         wprintf("<HTML><HEAD><TITLE>Goodbye</TITLE></HEAD><BODY><CENTER>\n");
152
153         serv_puts("MESG goodbye");
154         serv_gets(buf);
155
156         if (buf[0]=='1') fmout(NULL);
157         else wprintf("Goodbye\n");
158
159         wprintf("<HR><A HREF=\"/\">Log in again</A>\n");
160
161         wprintf("</CENTER></BODY></HTML>\n");
162         wDumpContent();
163         serv_puts("QUIT");
164         exit(0);
165         }