Simplified the logout javascript
[citadel.git] / webcit-ng / admin_functions.c
1 // Admin functions
2 //
3 // Copyright (c) 1996-2022 by the citadel.org team
4 //
5 // This program is open source software.  It runs great on the
6 // Linux operating system (and probably elsewhere).  You can use,
7 // copy, and run it under the terms of the GNU General Public
8 // License version 3.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14
15 #include "webcit.h"
16
17
18 // /ctdl/a/login is called when a user is trying to log in
19 void try_login(struct http_transaction *h, struct ctdlsession *c) {
20         char buf[1024];
21         char auth[AUTH_MAX];
22         char username[256];
23         char password[256];
24         int login_success = 0;
25
26         extract_token(username, h->request_body, 0, '|', sizeof username);
27         extract_token(password, h->request_body, 1, '|', sizeof password);
28
29         snprintf(buf, sizeof buf, "%s:%s", username, password);
30         CtdlEncodeBase64(auth, buf, strlen(buf), 0);
31
32         syslog(LOG_DEBUG, "try_login(username='%s',password=(%d bytes))", username, (int) strlen(password));
33
34         ctdl_printf(c, "LOUT");                 // log out, in case we were logged in
35         ctdl_readline(c, buf, sizeof(buf));     // ignore the result
36         memset(c->auth, 0, AUTH_MAX);           // if this connection had auth, it doesn't now.
37         memset(c->whoami, 0, 64);               // if this connection had auth, it doesn't now.
38
39         login_success = login_to_citadel(c, auth, buf); // Now try logging in to Citadel
40
41         h->response_code = 200;                 // 'buf' will contain the relevant response
42         h->response_string = strdup("OK");
43         add_response_header(h, strdup("Content-type"), strdup("text/plain"));
44         h->response_body = strdup(buf);
45         h->response_body_length = strlen(h->response_body);
46 }
47
48
49 // /ctdl/a/logout is called when a user is trying to log out.   Don't use this as an ajax.
50 void logout(struct http_transaction *h, struct ctdlsession *c) {
51         char buf[1024];
52         char auth[AUTH_MAX];
53         char username[256];
54         char password[256];
55         int login_success = 0;
56
57         ctdl_printf(c, "LOUT"); // log out
58         ctdl_readline(c, buf, sizeof(buf));     // ignore the result
59         strcpy(c->auth, "x");
60         //memset(c->auth, 0, AUTH_MAX);         // if this connection had auth, it doesn't now.
61         memset(c->whoami, 0, 64);               // if this connection had auth, it doesn't now.
62
63         http_redirect(h, "/ctdl/s/index.html"); // go back where we started :)
64 }
65
66
67 // /ctdl/a/whoami returns the name of the currently logged in user, or an empty string if not logged in
68 void whoami(struct http_transaction *h, struct ctdlsession *c) {
69         h->response_code = 200;
70         h->response_string = strdup("OK");
71         add_response_header(h, strdup("Content-type"), strdup("text/plain"));
72         h->response_body = strdup(c->whoami);
73         h->response_body_length = strlen(h->response_body);
74 }
75
76
77 // Dispatcher for paths starting with /ctdl/a/
78 void ctdl_a(struct http_transaction *h, struct ctdlsession *c) {
79         if (!strcasecmp(h->url, "/ctdl/a/login")) {     // log in
80                 try_login(h, c);
81                 return;
82         }
83
84         if (!strcasecmp(h->url, "/ctdl/a/logout")) {    // log out
85                 logout(h, c);
86                 return;
87         }
88
89         if (!strcasecmp(h->url, "/ctdl/a/whoami")) {    // return display name of user
90                 whoami(h, c);
91                 return;
92         }
93
94         do_404(h);                                      // unknown
95 }