3 // Copyright (c) 1996-2023 by the citadel.org team
5 // This program is open source software. Use, duplication, or disclosure is subject to the GNU General Public License v3.
10 // /ctdl/a/login is called when a user is trying to log in
11 void try_login(struct http_transaction *h, struct ctdlsession *c) {
16 int login_success = 0;
18 extract_token(username, h->request_body, 0, '|', sizeof username);
19 extract_token(password, h->request_body, 1, '|', sizeof password);
21 snprintf(buf, sizeof buf, "%s:%s", username, password);
22 CtdlEncodeBase64(auth, buf, strlen(buf), BASE64_NO_LINEBREAKS);
24 syslog(LOG_DEBUG, "try_login(username='%s',password=(%d bytes))", username, (int) strlen(password));
26 ctdl_printf(c, "LOUT"); // log out, in case we were logged in
27 ctdl_readline(c, buf, sizeof(buf)); // ignore the result
28 memset(c->auth, 0, AUTH_MAX); // if this connection had auth, it doesn't now.
29 memset(c->whoami, 0, 64); // if this connection had auth, it doesn't now.
30 login_success = login_to_citadel(c, auth, buf); // Now try logging in to Citadel
32 JsonValue *j = NewJsonObject(HKEY("login")); // Compose a JSON object with the results
34 JsonObjectAppend(j, NewJsonBool(HKEY("result"), 1));
35 JsonObjectAppend(j, NewJsonPlainString(HKEY("message"), "logged in", -1));
36 extract_token(username, &buf[4], 0, '|', sizeof username); // This will have the proper capitalization etc.
37 JsonObjectAppend(j, NewJsonPlainString(HKEY("fullname"), username, -1));
38 JsonObjectAppend(j, NewJsonNumber(HKEY("axlevel"), extract_int(&buf[4], 1) ));
39 JsonObjectAppend(j, NewJsonNumber(HKEY("timescalled"), extract_long(&buf[4], 2) ));
40 JsonObjectAppend(j, NewJsonNumber(HKEY("posted"), extract_long(&buf[4], 3) ));
41 JsonObjectAppend(j, NewJsonNumber(HKEY("usernum"), extract_long(&buf[4], 5) ));
42 JsonObjectAppend(j, NewJsonNumber(HKEY("previous_login"), extract_long(&buf[4], 6) ));
45 JsonObjectAppend(j, NewJsonBool(HKEY("result"), 0));
46 JsonObjectAppend(j, NewJsonPlainString(HKEY("message"), &buf[4], -1));
48 StrBuf *sj = NewStrBuf();
49 SerializeJson(sj, j, 1); // '1' == free the source object
51 add_response_header(h, strdup("Content-type"), strdup("application/json"));
52 h->response_code = 200;
53 h->response_string = strdup("OK");
54 h->response_body_length = StrLength(sj);
55 h->response_body = SmashStrBuf(&sj);
59 // /ctdl/a/logout is called when a user is trying to log out. Don't use this as an ajax.
60 void logout(struct http_transaction *h, struct ctdlsession *c) {
65 int login_success = 0;
67 ctdl_printf(c, "LOUT"); // log out
68 ctdl_readline(c, buf, sizeof(buf)); // ignore the result
70 memset(c->whoami, 0, 64); // if this connection had auth, it doesn't now.
72 http_redirect(h, "/ctdl/s/index.html"); // go back where we started :)
76 // /ctdl/a/whoami returns the name of the currently logged in user, or an empty string if not logged in
77 void whoami(struct http_transaction *h, struct ctdlsession *c) {
78 h->response_code = 200;
79 h->response_string = strdup("OK");
80 add_response_header(h, strdup("Content-type"), strdup("text/plain"));
81 h->response_body = strdup(c->whoami);
82 h->response_body_length = strlen(h->response_body);
86 // /ctdl/a/biff returns the number of new messages that have arrived in the inbox
87 // since the beginning of the session or since the last call to biff
88 void biff(struct http_transaction *h, struct ctdlsession *c) {
91 ctdl_printf(c, "BIFF"); // send the command
92 ctdl_readline(c, biffbuff, sizeof(biffbuff)); // read the result
93 h->response_code = 200;
94 h->response_string = strdup("OK");
95 add_response_header(h, strdup("Content-type"), strdup("text/plain"));
96 h->response_body = strdup(&biffbuff[4]);
97 h->response_body_length = strlen(h->response_body);
101 // Dispatcher for paths starting with /ctdl/a/
102 void ctdl_a(struct http_transaction *h, struct ctdlsession *c) {
103 if (!strcasecmp(h->url, "/ctdl/a/login")) { // log in
108 if (!strcasecmp(h->url, "/ctdl/a/logout")) { // log out
113 if (!strcasecmp(h->url, "/ctdl/a/whoami")) { // return display name of user
118 if (!strcasecmp(h->url, "/ctdl/a/biff")) { // check for new messages in the inbox
123 do_404(h); // unknown