c829fca07a8e5863d848121fb554d912128115ab
[citadel.git] / citadel / modules / ctdlproto / serv_session.c
1 /* 
2  * Server functions which perform operations on user objects.
3  *
4  * Copyright (c) 1987-2011 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 "sysdep.h"
16 #include <errno.h>
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <stdio.h>
20 #include <fcntl.h>
21 #include <signal.h>
22 #include <pwd.h>
23 #include <ctype.h>
24 #include <sys/types.h>
25 #include <sys/wait.h>
26 #include <syslog.h>
27 #ifdef HAVE_SYS_STAT_H
28 #include <sys/stat.h>
29 #endif
30
31 #if TIME_WITH_SYS_TIME
32 # include <sys/time.h>
33 # include <time.h>
34 #else
35 # if HAVE_SYS_TIME_H
36 #  include <sys/time.h>
37 # else
38 #  include <time.h>
39 # endif
40 #endif
41
42 #include <string.h>
43 #include <limits.h>
44 #include <libcitadel.h>
45 #include "auth.h"
46 #include "citadel.h"
47 #include "server.h"
48 #include "database.h"
49 #include "sysdep_decls.h"
50 #include "support.h"
51 #include "room_ops.h"
52 #include "svn_revision.h"
53 #include "control.h"
54 #include "msgbase.h"
55 #include "config.h"
56 #include "citserver.h"
57 #include "citadel_dirs.h"
58 #include "genstamp.h"
59 #include "threads.h"
60 #include "citadel_ldap.h"
61 #include "context.h"
62 #include "ctdl_module.h"
63
64 void cmd_noop(char *argbuf)
65 {
66         cprintf("%d%cok\n", CIT_OK, CtdlCheckExpress() );
67 }
68
69
70 void cmd_qnop(char *argbuf)
71 {
72         /* do nothing, this command returns no response */
73 }
74
75 /*
76  * Set or unset asynchronous protocol mode
77  */
78 void cmd_asyn(char *argbuf)
79 {
80         int new_state;
81
82         new_state = extract_int(argbuf, 0);
83         if ((new_state == 0) || (new_state == 1)) {
84                 CC->is_async = new_state;
85         }
86         cprintf("%d %d\n", CIT_OK, CC->is_async);
87 }
88
89
90
91 /*
92  * cmd_info()  -  tell the client about this server
93  */
94 void cmd_info(char *cmdbuf) {
95         cprintf("%d Server info:\n", LISTING_FOLLOWS);
96         cprintf("%d\n", CC->cs_pid);
97         cprintf("%s\n", config.c_nodename);
98         cprintf("%s\n", config.c_humannode);
99         cprintf("%s\n", config.c_fqdn);
100         cprintf("%s\n", CITADEL);
101         cprintf("%d\n", REV_LEVEL);
102         cprintf("%s\n", config.c_site_location);
103         cprintf("%s\n", config.c_sysadm);
104         cprintf("%d\n", SERVER_TYPE);
105         cprintf("%s\n", config.c_moreprompt);
106         cprintf("1\n"); /* 1 = yes, this system supports floors */
107         cprintf("1\n"); /* 1 = we support the extended paging options */
108         cprintf("\n");  /* nonce no longer supported */
109         cprintf("1\n"); /* 1 = yes, this system supports the QNOP command */
110
111 #ifdef HAVE_LDAP
112         cprintf("1\n"); /* 1 = yes, this server is LDAP-enabled */
113 #else
114         cprintf("0\n"); /* 1 = no, this server is not LDAP-enabled */
115 #endif
116
117         if ((config.c_auth_mode == AUTHMODE_NATIVE) &&
118             (config.c_disable_newu == 0))
119         {
120                 cprintf("%d\n", config.c_disable_newu);
121         }
122         else {
123                 cprintf("1\n"); /* "create new user" does not work with non-native auth modes */
124         }
125
126         cprintf("%s\n", config.c_default_cal_zone);
127
128         /* thread load averages -- temporarily disabled during refactoring of this code */
129         cprintf("0\n");         /* load average */
130         cprintf("0\n");         /* worker average */
131         cprintf("0\n");         /* thread count */
132
133         cprintf("1\n");         /* yes, Sieve mail filtering is supported */
134         cprintf("%d\n", config.c_enable_fulltext);
135         cprintf("%s\n", svn_revision());
136
137         if (config.c_auth_mode == AUTHMODE_NATIVE) {
138                 cprintf("%d\n", openid_level_supported); /* OpenID is enabled when using native auth */
139         }
140         else {
141                 cprintf("0\n"); /* OpenID is disabled when using non-native auth */
142         }
143
144         cprintf("%d\n", config.c_guest_logins);
145         
146         cprintf("000\n");
147 }
148
149 /*
150  * echo 
151  */
152 void cmd_echo(char *etext)
153 {
154         cprintf("%d %s\n", CIT_OK, etext);
155 }
156
157 /* 
158  * get the paginator prompt
159  */
160 void cmd_more(char *argbuf) {
161         cprintf("%d %s\n", CIT_OK, config.c_moreprompt);
162 }
163
164
165 /*
166  * the client is identifying itself to the server
167  */
168 void cmd_iden(char *argbuf)
169 {
170         CitContext *CCC = MyContext();
171         int dev_code;
172         int cli_code;
173         int rev_level;
174         char desc[128];
175         char from_host[128];
176
177         if (num_parms(argbuf)<4) {
178                 cprintf("%d usage error\n", ERROR + ILLEGAL_VALUE);
179                 return;
180         }
181
182         dev_code = extract_int(argbuf,0);
183         cli_code = extract_int(argbuf,1);
184         rev_level = extract_int(argbuf,2);
185         extract_token(desc, argbuf, 3, '|', sizeof desc);
186
187         safestrncpy(from_host, config.c_fqdn, sizeof from_host);
188         from_host[sizeof from_host - 1] = 0;
189         if (num_parms(argbuf)>=5) extract_token(from_host, argbuf, 4, '|', sizeof from_host);
190
191         CCC->cs_clientdev = dev_code;
192         CCC->cs_clienttyp = cli_code;
193         CCC->cs_clientver = rev_level;
194         safestrncpy(CCC->cs_clientname, desc, sizeof CCC->cs_clientname);
195         CCC->cs_clientname[31] = 0;
196
197         /* For local sockets and public clients, trust the hostname supplied by the client */
198         if ( (CCC->is_local_socket) || (CtdlIsPublicClient()) ) {
199                 safestrncpy(CCC->cs_host, from_host, sizeof CCC->cs_host);
200                 CCC->cs_host[sizeof CCC->cs_host - 1] = 0;
201                 CCC->cs_addr[0] = 0;
202         }
203
204         syslog(LOG_NOTICE, "Client %d/%d/%01d.%02d (%s) from %s\n",
205                 dev_code,
206                 cli_code,
207                 (rev_level / 100),
208                 (rev_level % 100),
209                 desc,
210                 CCC->cs_host
211         );
212         cprintf("%d Ok\n",CIT_OK);
213 }
214
215 /*
216  * Terminate another running session
217  */
218 void cmd_term(char *cmdbuf)
219 {
220         int session_num;
221         int terminated = 0;
222
223         session_num = extract_int(cmdbuf, 0);
224
225         terminated = CtdlTerminateOtherSession(session_num);
226
227         if (terminated < 0) {
228                 cprintf("%d You can't kill your own session.\n", ERROR + ILLEGAL_VALUE);
229                 return;
230         }
231
232         if (terminated & TERM_FOUND) {
233                 if (terminated == TERM_KILLED) {
234                         cprintf("%d Session terminated.\n", CIT_OK);
235                 }
236                 else {
237                         cprintf("%d You are not allowed to do that.\n",
238                                 ERROR + HIGHER_ACCESS_REQUIRED);
239                 }
240         }
241         else {
242                 cprintf("%d No such session.\n", ERROR + ILLEGAL_VALUE);
243         }
244 }
245
246
247 void cmd_time(char *argbuf)
248 {
249    time_t tv;
250    struct tm tmp;
251    
252    tv = time(NULL);
253    localtime_r(&tv, &tmp);
254    
255    /* timezone and daylight global variables are not portable. */
256 #ifdef HAVE_STRUCT_TM_TM_GMTOFF
257    cprintf("%d %ld|%ld|%d|%ld\n", CIT_OK, (long)tv, tmp.tm_gmtoff, tmp.tm_isdst, server_startup_time);
258 #else
259    cprintf("%d %ld|%ld|%d|%ld\n", CIT_OK, (long)tv, timezone, tmp.tm_isdst, server_startup_time);
260 #endif
261 }
262
263
264
265
266 /*****************************************************************************/
267 /*                      MODULE INITIALIZATION STUFF                          */
268 /*****************************************************************************/
269
270 CTDL_MODULE_INIT(serv_session)
271 {
272         if (!threading) {
273                 CtdlRegisterProtoHook(cmd_noop, "NOOP", "no operation");
274                 CtdlRegisterProtoHook(cmd_qnop, "QNOP", "no operation with no response");
275 ;
276                 CtdlRegisterProtoHook(cmd_asyn, "ASYN", "enable asynchronous server responses");
277                 CtdlRegisterProtoHook(cmd_info, "INFO", "fetch server capabilities and configuration");
278                 CtdlRegisterProtoHook(cmd_echo, "ECHO", "echo text back to the client");
279                 CtdlRegisterProtoHook(cmd_more, "MORE", "fetch the paginator prompt");
280                 CtdlRegisterProtoHook(cmd_iden, "IDEN", "identify the client software and location");
281                 CtdlRegisterProtoHook(cmd_term, "TERM", "terminate another running session");
282
283                 CtdlRegisterProtoHook(cmd_time, "TIME", "fetch the date and time from the server");
284         }
285         /* return our id for the Log */
286         return "serv_session";
287 }