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