CtdlCheckExpress() now accepts a session context.
[citadel.git] / citadel / server / modules / ctdlproto / serv_session.c
1 // Server functions which perform operations on user objects.
2 //
3 // Copyright (c) 1987-2024 by the citadel.org team
4 //
5 // This program is open source software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License, version 3.
7
8 #include <stdio.h>
9 #include <libcitadel.h>
10 #include "../../citserver.h"
11 #include "../../ctdl_module.h"
12 #include "../../config.h"
13
14
15 void cmd_noop(char *argbuf) {
16         cprintf("%d%cok\n", CIT_OK, CtdlCheckExpress(CC) );
17 }
18
19
20 void cmd_qnop(char *argbuf) {
21         /* do nothing, this command returns no response */
22 }
23
24
25 // Set or unset asynchronous protocol mode
26 void cmd_asyn(char *argbuf) {
27         int new_state;
28
29         new_state = extract_int(argbuf, 0);
30         if ((new_state == 0) || (new_state == 1)) {
31                 CC->is_async = new_state;
32         }
33         cprintf("%d %d\n", CIT_OK, CC->is_async);
34 }
35
36
37 // cmd_info()  -  Identify this server and its capabilities to the client
38 void cmd_info(char *cmdbuf) {
39         cprintf("%d Server info:\n", LISTING_FOLLOWS);
40         cprintf("%d\n", CC->cs_pid);
41         cprintf("%s\n", CtdlGetConfigStr("c_nodename"));
42         cprintf("%s\n", CtdlGetConfigStr("c_humannode"));
43         cprintf("%s\n", CtdlGetConfigStr("c_fqdn"));
44         cprintf("%s\n", CITADEL);
45         cprintf("%d\n", REV_LEVEL);
46         cprintf("%s\n", CtdlGetConfigStr("c_site_location"));
47         cprintf("%s\n", CtdlGetConfigStr("c_sysadm"));
48         cprintf("%d\n", SERVER_TYPE);
49         cprintf("%s\n", CtdlGetConfigStr("c_moreprompt"));
50         cprintf("1\n"); // 1 = yes, this system supports floors
51         cprintf("1\n"); // 1 = we support the extended paging options
52         cprintf("\n");  // no longer used
53         cprintf("1\n"); // 1 = yes, this system supports the QNOP command
54         cprintf("1\n"); // 1 = yes, this server is LDAP-enabled
55
56         if ((CtdlGetConfigInt("c_auth_mode") == AUTHMODE_NATIVE) && (CtdlGetConfigInt("c_disable_newu") == 0)) {
57                 cprintf("%d\n", CtdlGetConfigInt("c_disable_newu"));
58         }
59         else {
60                 cprintf("1\n"); // "create new user" does not work with non-native auth modes
61         }
62
63         cprintf("%s\n", CtdlGetConfigStr("c_default_cal_zone"));
64
65         cprintf("0\n"); // no longer used
66         cprintf("0\n"); // no longer used
67         cprintf("0\n"); // no longer used
68         cprintf("0\n"); // no longer used
69         cprintf("%d\n", CtdlGetConfigInt("c_enable_fulltext"));
70         cprintf("%s\n", BUILD_ID);
71         cprintf("0\n"); // no longer used (support for OpenID has ended)
72         cprintf("%d\n", CtdlGetConfigInt("c_guest_logins"));
73         cprintf("000\n");
74 }
75
76
77 // echo 
78 void cmd_echo(char *etext) {
79         cprintf("%d %s\n", CIT_OK, etext);
80 }
81
82
83 // get the paginator prompt
84 void cmd_more(char *argbuf) {
85         cprintf("%d %s\n", CIT_OK, CtdlGetConfigStr("c_moreprompt"));
86 }
87
88
89 // the client is identifying itself to the server
90 void cmd_iden(char *argbuf) {
91         int dev_code;
92         int cli_code;
93         int rev_level;
94         char desc[128];
95         char from_host[128];
96
97         if (num_parms(argbuf)<4) {
98                 cprintf("%d usage error\n", ERROR + ILLEGAL_VALUE);
99                 return;
100         }
101
102         extract_token(desc, argbuf, 3, '|', sizeof desc);
103
104         safestrncpy(from_host, CtdlGetConfigStr("c_fqdn"), sizeof from_host);
105         from_host[sizeof from_host - 1] = 0;
106         if (num_parms(argbuf)>=5) extract_token(from_host, argbuf, 4, '|', sizeof from_host);
107
108         safestrncpy(CC->cs_clientname, desc, sizeof CC->cs_clientname);
109         CC->cs_clientname[31] = 0;
110
111         // For local sockets, allow the client to supply the user's origin address
112         if ((CC->is_local_client) || (!IsEmptyStr(CC->cs_addr) && (!strcmp(CC->cs_addr, "127.0.0.1")) || (!strcmp(CC->cs_addr, "::1")))) {
113                 safestrncpy(CC->cs_host, from_host, sizeof CC->cs_host);
114                 CC->cs_host[sizeof CC->cs_host - 1] = 0;
115                 CC->cs_addr[0] = 0;
116         }
117
118         syslog(LOG_NOTICE, "session: client %d/%d/%01d.%02d (%s) from %s",
119                 dev_code,
120                 cli_code,
121                 (rev_level / 100),
122                 (rev_level % 100),
123                 desc,
124                 CC->cs_host
125         );
126         cprintf("%d Ok\n",CIT_OK);
127 }
128
129
130 // Terminate another running session
131 void cmd_term(char *cmdbuf) {
132         int session_num = extract_int(cmdbuf, 0);
133         int terminated = CtdlTerminateOtherSession(session_num);
134
135         if (terminated < 0) {
136                 cprintf("%d You can't kill your own session.\n", ERROR + ILLEGAL_VALUE);
137                 return;
138         }
139
140         if (terminated & TERM_FOUND) {
141                 if (terminated == TERM_KILLED) {
142                         cprintf("%d Session terminated.\n", CIT_OK);
143                 }
144                 else {
145                         cprintf("%d You are not allowed to do that.\n", ERROR + HIGHER_ACCESS_REQUIRED);
146                 }
147         }
148         else {
149                 cprintf("%d No such session.\n", ERROR + ILLEGAL_VALUE);
150         }
151 }
152
153
154 void cmd_time(char *argbuf) {
155         time_t tv;
156         struct tm tmp;
157
158         tv = time(NULL);
159         localtime_r(&tv, &tmp);
160
161         // timezone and daylight global variables are not portable.
162 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
163         cprintf("%d %ld|%ld|%d|%ld\n", CIT_OK, (long)tv, tmp.tm_gmtoff, tmp.tm_isdst, server_startup_time);
164 #else
165         cprintf("%d %ld|%ld|%d|%ld\n", CIT_OK, (long)tv, timezone, tmp.tm_isdst, server_startup_time);
166 #endif
167 }
168
169
170 // Initialization function, called from modules_init.c
171 char *ctdl_module_init_serv_session(void) {
172         if (!threading) {
173                 CtdlRegisterProtoHook(cmd_noop, "NOOP", "no operation");
174                 CtdlRegisterProtoHook(cmd_qnop, "QNOP", "no operation with no response");
175                 CtdlRegisterProtoHook(cmd_asyn, "ASYN", "enable asynchronous server responses");
176                 CtdlRegisterProtoHook(cmd_info, "INFO", "fetch server capabilities and configuration");
177                 CtdlRegisterProtoHook(cmd_echo, "ECHO", "echo text back to the client");
178                 CtdlRegisterProtoHook(cmd_more, "MORE", "fetch the paginator prompt");
179                 CtdlRegisterProtoHook(cmd_iden, "IDEN", "identify the client software and location");
180                 CtdlRegisterProtoHook(cmd_term, "TERM", "terminate another running session");
181                 CtdlRegisterProtoHook(cmd_time, "TIME", "fetch the date and time from the server");
182         }
183         // return our id for the log
184         return "serv_session";
185 }