indent -kr -i8 -l132 on everything in webcit-ng
[citadel.git] / webcit-ng / admin_functions.c
1 /*
2  * Admin functions
3  *
4  * Copyright (c) 1996-2018 by the citadel.org team
5  *
6  * This program is open source software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 3.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include "webcit.h"
16
17
18 /*
19  * /ctdl/a/login is called when a user is trying to log in
20  */
21 void try_login(struct http_transaction *h, struct ctdlsession *c)
22 {
23         char buf[1024];
24         char auth[AUTH_MAX];
25         char username[256];
26         char password[256];
27         int login_success = 0;
28
29         extract_token(username, h->request_body, 0, '|', sizeof username);
30         extract_token(password, h->request_body, 1, '|', sizeof password);
31
32         snprintf(buf, sizeof buf, "%s:%s", username, password);
33         CtdlEncodeBase64(auth, buf, strlen(buf), 0);
34
35         syslog(LOG_DEBUG, "try_login(username='%s',password=(%d bytes))", username, (int) strlen(password));
36
37         ctdl_printf(c, "LOUT");                 // log out, in case we were logged in
38         ctdl_readline(c, buf, sizeof(buf));     // ignore the result
39         memset(c->auth, 0, AUTH_MAX);           // if this connection had auth, it doesn't now.
40         memset(c->whoami, 0, 64);               // if this connection had auth, it doesn't now.
41
42         login_success = login_to_citadel(c, auth, buf); // Now try logging in to Citadel
43
44         h->response_code = 200;                 // 'buf' will contain the relevant response
45         h->response_string = strdup("OK");
46         add_response_header(h, strdup("Content-type"), strdup("text/plain"));
47         h->response_body = strdup(buf);
48         h->response_body_length = strlen(h->response_body);
49 }
50
51
52 /*
53  * /ctdl/a/logout is called when a user is trying to log out.   Don't use this as an ajax.
54  */
55 void logout(struct http_transaction *h, struct ctdlsession *c)
56 {
57         char buf[1024];
58         char auth[AUTH_MAX];
59         char username[256];
60         char password[256];
61         int login_success = 0;
62
63         ctdl_printf(c, "LOUT"); // log out
64         ctdl_readline(c, buf, sizeof(buf));     // ignore the result
65         strcpy(c->auth, "x");
66         //memset(c->auth, 0, AUTH_MAX);         // if this connection had auth, it doesn't now.
67         memset(c->whoami, 0, 64);               // if this connection had auth, it doesn't now.
68
69         http_redirect(h, "/ctdl/s/index.html"); // go back where we started :)
70 }
71
72
73 /*
74  * /ctdl/a/whoami returns the name of the currently logged in user, or an empty string if not logged in
75  */
76 void whoami(struct http_transaction *h, struct ctdlsession *c)
77 {
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);
83 }
84
85
86 /*
87  * Dispatcher for paths starting with /ctdl/a/
88  */
89 void ctdl_a(struct http_transaction *h, struct ctdlsession *c)
90 {
91         if (!strcasecmp(h->uri, "/ctdl/a/login")) {     // log in
92                 try_login(h, c);
93                 return;
94         }
95
96         if (!strcasecmp(h->uri, "/ctdl/a/logout")) {    // log out
97                 logout(h, c);
98                 return;
99         }
100
101         if (!strcasecmp(h->uri, "/ctdl/a/whoami")) {    // return display name of user
102                 whoami(h, c);
103                 return;
104         }
105
106         do_404(h);                                      // unknown
107 }