7547331dbba99053fb5c644d2a35a3e32dda09f0
[citadel.git] / citadel / server / modules / ctdlproto / serv_session.c
1 // Server functions which perform operations on user objects.
2 //
3 // Copyright (c) 1987-2023 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() );
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         dev_code = extract_int(argbuf,0);
103         cli_code = extract_int(argbuf,1);
104         rev_level = extract_int(argbuf,2);
105         extract_token(desc, argbuf, 3, '|', sizeof desc);
106
107         safestrncpy(from_host, CtdlGetConfigStr("c_fqdn"), sizeof from_host);
108         from_host[sizeof from_host - 1] = 0;
109         if (num_parms(argbuf)>=5) extract_token(from_host, argbuf, 4, '|', sizeof from_host);
110
111         CC->cs_clientdev = dev_code;
112         CC->cs_clienttyp = cli_code;
113         CC->cs_clientver = rev_level;
114         safestrncpy(CC->cs_clientname, desc, sizeof CC->cs_clientname);
115         CC->cs_clientname[31] = 0;
116
117         // For local sockets, allow the client to supply the user's origin address
118         if ((CC->is_local_client) || (!IsEmptyStr(CC->cs_addr) && (!strcmp(CC->cs_addr, "127.0.0.1")) || (!strcmp(CC->cs_addr, "::1")))) {
119                 safestrncpy(CC->cs_host, from_host, sizeof CC->cs_host);
120                 CC->cs_host[sizeof CC->cs_host - 1] = 0;
121                 CC->cs_addr[0] = 0;
122         }
123
124         syslog(LOG_NOTICE, "session: client %d/%d/%01d.%02d (%s) from %s",
125                 dev_code,
126                 cli_code,
127                 (rev_level / 100),
128                 (rev_level % 100),
129                 desc,
130                 CC->cs_host
131         );
132         cprintf("%d Ok\n",CIT_OK);
133 }
134
135
136 // Terminate another running session
137 void cmd_term(char *cmdbuf) {
138         int session_num = extract_int(cmdbuf, 0);
139         int terminated = CtdlTerminateOtherSession(session_num);
140
141         if (terminated < 0) {
142                 cprintf("%d You can't kill your own session.\n", ERROR + ILLEGAL_VALUE);
143                 return;
144         }
145
146         if (terminated & TERM_FOUND) {
147                 if (terminated == TERM_KILLED) {
148                         cprintf("%d Session terminated.\n", CIT_OK);
149                 }
150                 else {
151                         cprintf("%d You are not allowed to do that.\n", ERROR + HIGHER_ACCESS_REQUIRED);
152                 }
153         }
154         else {
155                 cprintf("%d No such session.\n", ERROR + ILLEGAL_VALUE);
156         }
157 }
158
159
160 void cmd_time(char *argbuf) {
161         time_t tv;
162         struct tm tmp;
163
164         tv = time(NULL);
165         localtime_r(&tv, &tmp);
166
167         // timezone and daylight global variables are not portable.
168 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
169         cprintf("%d %ld|%ld|%d|%ld\n", CIT_OK, (long)tv, tmp.tm_gmtoff, tmp.tm_isdst, server_startup_time);
170 #else
171         cprintf("%d %ld|%ld|%d|%ld\n", CIT_OK, (long)tv, timezone, tmp.tm_isdst, server_startup_time);
172 #endif
173 }
174
175
176 // Initialization function, called from modules_init.c
177 char *ctdl_module_init_serv_session(void) {
178         if (!threading) {
179                 CtdlRegisterProtoHook(cmd_noop, "NOOP", "no operation");
180                 CtdlRegisterProtoHook(cmd_qnop, "QNOP", "no operation with no response");
181                 CtdlRegisterProtoHook(cmd_asyn, "ASYN", "enable asynchronous server responses");
182                 CtdlRegisterProtoHook(cmd_info, "INFO", "fetch server capabilities and configuration");
183                 CtdlRegisterProtoHook(cmd_echo, "ECHO", "echo text back to the client");
184                 CtdlRegisterProtoHook(cmd_more, "MORE", "fetch the paginator prompt");
185                 CtdlRegisterProtoHook(cmd_iden, "IDEN", "identify the client software and location");
186                 CtdlRegisterProtoHook(cmd_term, "TERM", "terminate another running session");
187                 CtdlRegisterProtoHook(cmd_time, "TIME", "fetch the date and time from the server");
188         }
189         // return our id for the log
190         return "serv_session";
191 }