The battle against bit rot continues!
[citadel.git] / citadel / modules / ctdlproto / serv_session.c
1 /* 
2  * Server functions which perform operations on user objects.
3  *
4  * Copyright (c) 1987-2019 by the citadel.org team
5  *
6  * This program is open source software; you can redistribute it and/or
7  * modify 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 <stdio.h>
16 #include <libcitadel.h>
17 #include "citserver.h"
18 #include "svn_revision.h"
19 #include "ctdl_module.h"
20 #include "config.h"
21
22
23 void cmd_noop(char *argbuf)
24 {
25         cprintf("%d%cok\n", CIT_OK, CtdlCheckExpress() );
26 }
27
28
29 void cmd_qnop(char *argbuf)
30 {
31         /* do nothing, this command returns no response */
32 }
33
34
35 /*
36  * Set or unset asynchronous protocol mode
37  */
38 void cmd_asyn(char *argbuf)
39 {
40         int new_state;
41
42         new_state = extract_int(argbuf, 0);
43         if ((new_state == 0) || (new_state == 1)) {
44                 CC->is_async = new_state;
45         }
46         cprintf("%d %d\n", CIT_OK, CC->is_async);
47 }
48
49
50 /*
51  * cmd_info()  -  Identify this server and its capabilities to the client
52  */
53 void cmd_info(char *cmdbuf) {
54         cprintf("%d Server info:\n", LISTING_FOLLOWS);
55         cprintf("%d\n", CC->cs_pid);
56         cprintf("%s\n", CtdlGetConfigStr("c_nodename"));
57         cprintf("%s\n", CtdlGetConfigStr("c_humannode"));
58         cprintf("%s\n", CtdlGetConfigStr("c_fqdn"));
59         cprintf("%s\n", CITADEL);
60         cprintf("%d\n", REV_LEVEL);
61         cprintf("%s\n", CtdlGetConfigStr("c_site_location"));
62         cprintf("%s\n", CtdlGetConfigStr("c_sysadm"));
63         cprintf("%d\n", SERVER_TYPE);
64         cprintf("%s\n", CtdlGetConfigStr("c_moreprompt"));
65         cprintf("1\n"); /* 1 = yes, this system supports floors */
66         cprintf("1\n"); /* 1 = we support the extended paging options */
67         cprintf("\n");  /* nonce no longer supported */
68         cprintf("1\n"); /* 1 = yes, this system supports the QNOP command */
69
70 #ifdef HAVE_LDAP
71         cprintf("1\n"); /* 1 = yes, this server is LDAP-enabled */
72 #else
73         cprintf("0\n"); /* 1 = no, this server is not LDAP-enabled */
74 #endif
75
76         if ((CtdlGetConfigInt("c_auth_mode") == AUTHMODE_NATIVE) && (CtdlGetConfigInt("c_disable_newu") == 0))
77         {
78                 cprintf("%d\n", CtdlGetConfigInt("c_disable_newu"));
79         }
80         else {
81                 cprintf("1\n"); /* "create new user" does not work with non-native auth modes */
82         }
83
84         cprintf("%s\n", CtdlGetConfigStr("c_default_cal_zone"));
85
86         cprintf("0\n");         /* load average         (no longer used) */
87         cprintf("0\n");         /* worker average       (no longer used) */
88         cprintf("0\n");         /* thread count         (no longer used) */
89         cprintf("1\n");         /* yes, Sieve mail filtering is supported */
90
91         cprintf("%d\n", CtdlGetConfigInt("c_enable_fulltext"));
92         cprintf("%s\n", svn_revision());
93
94         if (CtdlGetConfigInt("c_auth_mode") == AUTHMODE_NATIVE) {
95                 cprintf("%d\n", openid_level_supported); /* OpenID is enabled when using native auth */
96         }
97         else {
98                 cprintf("0\n"); /* OpenID is disabled when using non-native auth */
99         }
100
101         cprintf("%d\n", CtdlGetConfigInt("c_guest_logins"));
102         cprintf("000\n");
103 }
104
105
106 /*
107  * echo 
108  */
109 void cmd_echo(char *etext)
110 {
111         cprintf("%d %s\n", CIT_OK, etext);
112 }
113
114
115 /* 
116  * get the paginator prompt
117  */
118 void cmd_more(char *argbuf) {
119         cprintf("%d %s\n", CIT_OK, CtdlGetConfigStr("c_moreprompt"));
120 }
121
122
123 /*
124  * the client is identifying itself to the server
125  */
126 void cmd_iden(char *argbuf)
127 {
128         int dev_code;
129         int cli_code;
130         int rev_level;
131         char desc[128];
132         char from_host[128];
133
134         if (num_parms(argbuf)<4) {
135                 cprintf("%d usage error\n", ERROR + ILLEGAL_VALUE);
136                 return;
137         }
138
139         dev_code = extract_int(argbuf,0);
140         cli_code = extract_int(argbuf,1);
141         rev_level = extract_int(argbuf,2);
142         extract_token(desc, argbuf, 3, '|', sizeof desc);
143
144         safestrncpy(from_host, CtdlGetConfigStr("c_fqdn"), sizeof from_host);
145         from_host[sizeof from_host - 1] = 0;
146         if (num_parms(argbuf)>=5) extract_token(from_host, argbuf, 4, '|', sizeof from_host);
147
148         CC->cs_clientdev = dev_code;
149         CC->cs_clienttyp = cli_code;
150         CC->cs_clientver = rev_level;
151         safestrncpy(CC->cs_clientname, desc, sizeof CC->cs_clientname);
152         CC->cs_clientname[31] = 0;
153
154         /* For local sockets, allow the client to supply the user's origin address */
155         if (CC->is_local_socket) {
156                 safestrncpy(CC->cs_host, from_host, sizeof CC->cs_host);
157                 CC->cs_host[sizeof CC->cs_host - 1] = 0;
158                 CC->cs_addr[0] = 0;
159         }
160
161         syslog(LOG_NOTICE, "session: client %d/%d/%01d.%02d (%s) from %s",
162                 dev_code,
163                 cli_code,
164                 (rev_level / 100),
165                 (rev_level % 100),
166                 desc,
167                 CC->cs_host
168         );
169         cprintf("%d Ok\n",CIT_OK);
170 }
171
172
173 /*
174  * Terminate another running session
175  */
176 void cmd_term(char *cmdbuf)
177 {
178         int session_num = extract_int(cmdbuf, 0);
179         int terminated = CtdlTerminateOtherSession(session_num);
180
181         if (terminated < 0) {
182                 cprintf("%d You can't kill your own session.\n", ERROR + ILLEGAL_VALUE);
183                 return;
184         }
185
186         if (terminated & TERM_FOUND) {
187                 if (terminated == TERM_KILLED) {
188                         cprintf("%d Session terminated.\n", CIT_OK);
189                 }
190                 else {
191                         cprintf("%d You are not allowed to do that.\n", ERROR + HIGHER_ACCESS_REQUIRED);
192                 }
193         }
194         else {
195                 cprintf("%d No such session.\n", ERROR + ILLEGAL_VALUE);
196         }
197 }
198
199
200 void cmd_time(char *argbuf)
201 {
202         time_t tv;
203         struct tm tmp;
204
205         tv = time(NULL);
206         localtime_r(&tv, &tmp);
207
208         /* timezone and daylight global variables are not portable. */
209 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
210         cprintf("%d %ld|%ld|%d|%ld\n", CIT_OK, (long)tv, tmp.tm_gmtoff, tmp.tm_isdst, server_startup_time);
211 #else
212         cprintf("%d %ld|%ld|%d|%ld\n", CIT_OK, (long)tv, timezone, tmp.tm_isdst, server_startup_time);
213 #endif
214 }
215
216
217 /*****************************************************************************/
218 /*                      MODULE INITIALIZATION STUFF                          */
219 /*****************************************************************************/
220
221 CTDL_MODULE_INIT(serv_session)
222 {
223         if (!threading) {
224                 CtdlRegisterProtoHook(cmd_noop, "NOOP", "no operation");
225                 CtdlRegisterProtoHook(cmd_qnop, "QNOP", "no operation with no response");
226                 CtdlRegisterProtoHook(cmd_asyn, "ASYN", "enable asynchronous server responses");
227                 CtdlRegisterProtoHook(cmd_info, "INFO", "fetch server capabilities and configuration");
228                 CtdlRegisterProtoHook(cmd_echo, "ECHO", "echo text back to the client");
229                 CtdlRegisterProtoHook(cmd_more, "MORE", "fetch the paginator prompt");
230                 CtdlRegisterProtoHook(cmd_iden, "IDEN", "identify the client software and location");
231                 CtdlRegisterProtoHook(cmd_term, "TERM", "terminate another running session");
232                 CtdlRegisterProtoHook(cmd_time, "TIME", "fetch the date and time from the server");
233         }
234         /* return our id for the Log */
235         return "serv_session";
236 }