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.
39 login_success = login_to_citadel(c, auth, buf); // Now try logging in to Citadel
41 h->response_code = 200; // 'buf' will contain the relevant response
42 h->response_string = strdup("OK");
43 add_response_header(h, strdup("Content-type"), strdup("text/plain"));
44 h->response_body = strdup(buf);
45 h->response_body_length = strlen(h->response_body);
49 // /ctdl/a/logout is called when a user is trying to log out. Don't use this as an ajax.
50 void logout(struct http_transaction *h, struct ctdlsession *c) {
55 int login_success = 0;
57 ctdl_printf(c, "LOUT"); // log out
58 ctdl_readline(c, buf, sizeof(buf)); // ignore the result
60 //memset(c->auth, 0, AUTH_MAX); // if this connection had auth, it doesn't now.
61 memset(c->whoami, 0, 64); // if this connection had auth, it doesn't now.
63 http_redirect(h, "/ctdl/s/index.html"); // go back where we started :)
67 // /ctdl/a/whoami returns the name of the currently logged in user, or an empty string if not logged in
68 void whoami(struct http_transaction *h, struct ctdlsession *c) {
69 h->response_code = 200;
70 h->response_string = strdup("OK");
71 add_response_header(h, strdup("Content-type"), strdup("text/plain"));
72 h->response_body = strdup(c->whoami);
73 h->response_body_length = strlen(h->response_body);
77 // Dispatcher for paths starting with /ctdl/a/
78 void ctdl_a(struct http_transaction *h, struct ctdlsession *c) {
79 if (!strcasecmp(h->url, "/ctdl/a/login")) { // log in
84 if (!strcasecmp(h->url, "/ctdl/a/logout")) { // log out
89 if (!strcasecmp(h->url, "/ctdl/a/whoami")) { // return display name of user