3 // Copyright (c) 1996-2022 by the citadel.org team
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
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.
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) {
24 int login_success = 0;
26 extract_token(username, h->request_body, 0, '|', sizeof username);
27 extract_token(password, h->request_body, 1, '|', sizeof password);
29 snprintf(buf, sizeof buf, "%s:%s", username, password);
30 CtdlEncodeBase64(auth, buf, strlen(buf), 0);
32 syslog(LOG_DEBUG, "try_login(username='%s',password=(%d bytes))", username, (int) strlen(password));
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
40 JsonValue *j = NewJsonObject(HKEY("login")); // Compose a JSON object with the results
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) ));
53 JsonObjectAppend(j, NewJsonBool(HKEY("result"), 0));
54 JsonObjectAppend(j, NewJsonPlainString(HKEY("message"), &buf[4], -1));
56 StrBuf *sj = NewStrBuf();
57 SerializeJson(sj, j, 1); // '1' == free the source object
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);
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) {
73 int login_success = 0;
75 ctdl_printf(c, "LOUT"); // log out
76 ctdl_readline(c, buf, sizeof(buf)); // ignore the result
78 memset(c->whoami, 0, 64); // if this connection had auth, it doesn't now.
80 http_redirect(h, "/ctdl/s/index.html"); // go back where we started :)
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);
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
101 if (!strcasecmp(h->url, "/ctdl/a/logout")) { // log out
106 if (!strcasecmp(h->url, "/ctdl/a/whoami")) { // return display name of user
111 do_404(h); // unknown