Set up login banner graphic and background image graphic
[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 BACKGROUND=\"/image&name=background\" TEXT=\"#000000\" LINK=\"#004400\">\n");
32
33         /* Da banner */
34         wprintf("<CENTER><TABLE border=0 width=100%><TR><TD>\n");
35         wprintf("<IMG SRC=\"/image&name=hello\">");
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         wprintf("<HR>\n");
49
50         /* Da login box */
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
64         /* Da instructions */
65         wprintf("<LI><EM>If you already have an account on %s,",
66                 serv_info.serv_humannode);
67         wprintf("</EM> enter your user name\n");
68         wprintf("and password and click \"<TT>Login</TT>.\"<BR>\n");
69         wprintf("<LI><EM>If you are a new user,</EM>\n");
70         wprintf("enter the name and password you wish to use, and click\n");
71         wprintf("\"New User.\"<BR><LI>");
72         wprintf("<EM>Please log off properly when finished.</EM>");
73         wprintf("<LI>You must use a browser that supports <i>frames</i> ");
74         wprintf("and <i>cookies</i>.\n");
75         wprintf("</EM></UL>\n");
76
77         wprintf("</BODY></HTML>\n");
78         wDumpContent();
79         }
80
81
82
83
84 /*
85  * This function needs to get called whenever a PASS or NEWU succeeds
86  */
87 void become_logged_in(char *user, char *pass, char *serv_response) {
88         logged_in = 1;
89         extract(wc_username, &serv_response[4], 0);
90         strcpy(wc_password, pass);
91         axlevel = extract_int(&serv_response[4], 1);
92         if (axlevel >=6) is_aide = 1;
93         fprintf(stderr, "become_logged_in(%s,%s)\n", user, pass);
94         }
95
96
97 void do_login(void) {
98         char buf[256];
99
100         if (!strcasecmp(bstr("action"), "Exit")) {
101                 do_logout();
102                 }
103
104         if (!strcasecmp(bstr("action"), "Login")) {
105                 serv_printf("USER %s", bstr("name"));
106                 serv_gets(buf);
107                 if (buf[0]=='3') {
108                         serv_printf("PASS %s", bstr("pass"));
109                         serv_gets(buf);
110                         if (buf[0]=='2') {
111                                 become_logged_in(bstr("name"),
112                                         bstr("pass"), buf);
113                                 }
114                         else {
115                                 display_login(&buf[4]);
116                                 return;
117                                 }
118                         }
119                 else {
120                         display_login(&buf[4]);
121                         return;
122                         }
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                         }
133                 else {
134                         display_login(&buf[4]);
135                         return;
136                         }
137                 }
138
139         if (logged_in) {
140                 output_static("frameset.html");
141                 }
142         else {
143                 display_login("Your password was not accepted.");
144                 }
145
146         }
147
148 void do_welcome(void) {
149         printf("HTTP/1.0 200 OK\n");
150         output_headers();
151         wprintf("<HTML><BODY BACKGROUND=\"/image&name=background\" TEXT=\"#000000\" LINK=\"#004400\">\n");
152         wprintf("<CENTER><H1>");
153         escputs(wc_username);
154         wprintf("</H1>\n");
155         /* FIX add user stats here */
156
157         wprintf("<HR>");
158         /* FIX  ---  what should we put here?  the main menu,
159          * or new messages in the lobby?
160          */
161         embed_main_menu();
162
163         wprintf("</BODY></HTML>\n");
164         wDumpContent();
165         }
166
167
168 void do_logout(void) {
169         char buf[256];
170
171         strcpy(wc_username, "");
172         strcpy(wc_password, "");
173         strcpy(wc_roomname, "");
174         strcpy(wc_host, "");
175         strcpy(wc_port, "");
176
177         printf("HTTP/1.0 200 OK\n");
178         output_headers();
179         printf("X-WebCit-Session: close\n");
180         
181         wprintf("<HTML><HEAD><TITLE>Goodbye</TITLE></HEAD><BODY BACKGROUND=\"/image&name=background\" TEXT=\"#000000\" LINK=\"#004400\"><CENTER>\n");
182
183         serv_puts("MESG goodbye");
184         serv_gets(buf);
185
186         if (buf[0]=='1') fmout(NULL);
187         else wprintf("Goodbye\n");
188
189         wprintf("<HR><A HREF=\"/\">Log in again</A>\n");
190
191         wprintf("</CENTER></BODY></HTML>\n");
192         wDumpContent();
193         serv_puts("QUIT");
194         exit(0);
195         }