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