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