4865b8c661d87663eeb04b0f1056b17e9435df35
[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         login_success = login_to_citadel(c, auth, buf);                         // Now try logging in to Citadel
39
40         JsonValue *j = NewJsonObject(HKEY("login"));                            // Compose a JSON object with the results
41         if (buf[0] == '2') {
42                 JsonObjectAppend(j, NewJsonBool(HKEY("result"), 1));
43                 JsonObjectAppend(j, NewJsonPlainString(HKEY("message"), "logged in", -1));
44                 extract_token(username, &buf[4], 0, '|', sizeof username);      // This will have the proper capitalization etc.
45                 JsonObjectAppend(j, NewJsonPlainString(HKEY("fullname"), username, -1));
46                 JsonObjectAppend(j, NewJsonNumber(HKEY("axlevel"), extract_int(&buf[4], 1) ));
47                 JsonObjectAppend(j, NewJsonNumber(HKEY("timescalled"), extract_long(&buf[4], 2) ));
48                 JsonObjectAppend(j, NewJsonNumber(HKEY("posted"), extract_long(&buf[4], 3) ));
49                 JsonObjectAppend(j, NewJsonNumber(HKEY("usernum"), extract_long(&buf[4], 5) ));
50                 JsonObjectAppend(j, NewJsonNumber(HKEY("previous_login"), extract_long(&buf[4], 6) ));
51         }
52         else {
53                 JsonObjectAppend(j, NewJsonBool(HKEY("result"), 0));
54                 JsonObjectAppend(j, NewJsonPlainString(HKEY("message"), &buf[4], -1));
55         }
56         StrBuf *sj = NewStrBuf();
57         SerializeJson(sj, j, 1);                                                // '1' == free the source object
58
59         add_response_header(h, strdup("Content-type"), strdup("application/json"));
60         h->response_code = 200;
61         h->response_string = strdup("OK");
62         h->response_body_length = StrLength(sj);
63         h->response_body = SmashStrBuf(&sj);
64 }
65
66
67 // /ctdl/a/logout is called when a user is trying to log out.   Don't use this as an ajax.
68 void logout(struct http_transaction *h, struct ctdlsession *c) {
69         char buf[1024];
70         char auth[AUTH_MAX];
71         char username[256];
72         char password[256];
73         int login_success = 0;
74
75         ctdl_printf(c, "LOUT"); // log out
76         ctdl_readline(c, buf, sizeof(buf));     // ignore the result
77         strcpy(c->auth, "x");
78         memset(c->whoami, 0, 64);               // if this connection had auth, it doesn't now.
79
80         http_redirect(h, "/ctdl/s/index.html"); // go back where we started :)
81 }
82
83
84 // /ctdl/a/whoami returns the name of the currently logged in user, or an empty string if not logged in
85 void whoami(struct http_transaction *h, struct ctdlsession *c) {
86         h->response_code = 200;
87         h->response_string = strdup("OK");
88         add_response_header(h, strdup("Content-type"), strdup("text/plain"));
89         h->response_body = strdup(c->whoami);
90         h->response_body_length = strlen(h->response_body);
91 }
92
93
94 // Dispatcher for paths starting with /ctdl/a/
95 void ctdl_a(struct http_transaction *h, struct ctdlsession *c) {
96         if (!strcasecmp(h->url, "/ctdl/a/login")) {     // log in
97                 try_login(h, c);
98                 return;
99         }
100
101         if (!strcasecmp(h->url, "/ctdl/a/logout")) {    // log out
102                 logout(h, c);
103                 return;
104         }
105
106         if (!strcasecmp(h->url, "/ctdl/a/whoami")) {    // return display name of user
107                 whoami(h, c);
108                 return;
109         }
110
111         do_404(h);                                      // unknown
112 }