Moved the remaining else blocks
[citadel.git] / webcit-ng / admin_functions.c
1 //
2 // Admin functions
3 //
4 // Copyright (c) 1996-2021 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.
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 // /ctdl/a/login is called when a user is trying to log in
20 void try_login(struct http_transaction *h, struct ctdlsession *c) {
21         char buf[1024];
22         char auth[AUTH_MAX];
23         char username[256];
24         char password[256];
25         int login_success = 0;
26
27         extract_token(username, h->request_body, 0, '|', sizeof username);
28         extract_token(password, h->request_body, 1, '|', sizeof password);
29
30         snprintf(buf, sizeof buf, "%s:%s", username, password);
31         CtdlEncodeBase64(auth, buf, strlen(buf), 0);
32
33         syslog(LOG_DEBUG, "try_login(username='%s',password=(%d bytes))", username, (int) strlen(password));
34
35         ctdl_printf(c, "LOUT");                 // log out, in case we were logged in
36         ctdl_readline(c, buf, sizeof(buf));     // ignore the result
37         memset(c->auth, 0, AUTH_MAX);           // if this connection had auth, it doesn't now.
38         memset(c->whoami, 0, 64);               // if this connection had auth, it doesn't now.
39
40         login_success = login_to_citadel(c, auth, buf); // Now try logging in to Citadel
41
42         h->response_code = 200;                 // 'buf' will contain the relevant response
43         h->response_string = strdup("OK");
44         add_response_header(h, strdup("Content-type"), strdup("text/plain"));
45         h->response_body = strdup(buf);
46         h->response_body_length = strlen(h->response_body);
47 }
48
49
50 // /ctdl/a/logout is called when a user is trying to log out.   Don't use this as an ajax.
51 void logout(struct http_transaction *h, struct ctdlsession *c) {
52         char buf[1024];
53         char auth[AUTH_MAX];
54         char username[256];
55         char password[256];
56         int login_success = 0;
57
58         ctdl_printf(c, "LOUT"); // log out
59         ctdl_readline(c, buf, sizeof(buf));     // ignore the result
60         strcpy(c->auth, "x");
61         //memset(c->auth, 0, AUTH_MAX);         // if this connection had auth, it doesn't now.
62         memset(c->whoami, 0, 64);               // if this connection had auth, it doesn't now.
63
64         http_redirect(h, "/ctdl/s/index.html"); // go back where we started :)
65 }
66
67
68 // /ctdl/a/whoami returns the name of the currently logged in user, or an empty string if not logged in
69 void whoami(struct http_transaction *h, struct ctdlsession *c) {
70         h->response_code = 200;
71         h->response_string = strdup("OK");
72         add_response_header(h, strdup("Content-type"), strdup("text/plain"));
73         h->response_body = strdup(c->whoami);
74         h->response_body_length = strlen(h->response_body);
75 }
76
77
78 // Dispatcher for paths starting with /ctdl/a/
79 void ctdl_a(struct http_transaction *h, struct ctdlsession *c) {
80         if (!strcasecmp(h->uri, "/ctdl/a/login")) {     // log in
81                 try_login(h, c);
82                 return;
83         }
84
85         if (!strcasecmp(h->uri, "/ctdl/a/logout")) {    // log out
86                 logout(h, c);
87                 return;
88         }
89
90         if (!strcasecmp(h->uri, "/ctdl/a/whoami")) {    // return display name of user
91                 whoami(h, c);
92                 return;
93         }
94
95         do_404(h);                                      // unknown
96 }