* move policy.c into modules/expire/expire_policy.c, since it just controls this.
[citadel.git] / citadel / modules / rwho / serv_rwho.c
1 /*
2  * $Id$
3  *
4  * This module implements server commands related to the display and
5  * manipulation of the "Who's online" list.
6  *
7  * Copyright (c) 1987-2009 by the citadel.org team
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 3 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 #include "sysdep.h"
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <stdio.h>
28 #include <fcntl.h>
29 #include <signal.h>
30 #include <pwd.h>
31 #include <errno.h>
32 #include <sys/types.h>
33
34 #if TIME_WITH_SYS_TIME
35 # include <sys/time.h>
36 # include <time.h>
37 #else
38 # if HAVE_SYS_TIME_H
39 #  include <sys/time.h>
40 # else
41 #  include <time.h>
42 # endif
43 #endif
44
45 #include <sys/wait.h>
46 #include <string.h>
47 #include <limits.h>
48 #include <libcitadel.h>
49 #include "citadel.h"
50 #include "server.h"
51 #include "citserver.h"
52 #include "support.h"
53 #include "config.h"
54 #include "control.h"
55 #include "user_ops.h"
56 #include "database.h"
57 #include "msgbase.h"
58
59
60 #include "ctdl_module.h"
61
62
63 /*
64  * display who's online
65  */
66 void cmd_rwho(char *argbuf) {
67         struct CitContext *nptr;
68         int nContexts, i;
69         int spoofed = 0;
70         int user_spoofed = 0;
71         int room_spoofed = 0;
72         int host_spoofed = 0;
73         int aide;
74         char un[40];
75         char real_room[ROOMNAMELEN], room[ROOMNAMELEN];
76         char host[64], flags[5];
77         
78         /* So that we don't keep the context list locked for a long time
79          * we create a copy of it first
80          */
81         
82
83         nptr = CtdlGetContextArray(&nContexts) ;
84         if (!nptr)
85         {
86                 /* Couldn't malloc so we have to bail but stick to the protocol */
87                 cprintf("%d%c \n", LISTING_FOLLOWS, CtdlCheckExpress() );
88                 cprintf("000\n");
89                 return;
90         }
91         
92         aide = CC->user.axlevel >= AxAideU;
93         cprintf("%d%c \n", LISTING_FOLLOWS, CtdlCheckExpress() );
94         
95         for (i=0; i<nContexts; i++) 
96         {
97                 flags[0] = '\0';
98                 spoofed = 0;
99                 user_spoofed = 0;
100                 room_spoofed = 0;
101                 host_spoofed = 0;
102                 
103                 if (nptr[i].cs_flags & CS_POSTING)
104                    strcat(flags, "*");
105                 else
106                    strcat(flags, ".");
107                    
108                 if (nptr[i].fake_username[0])
109                 {
110                    strcpy(un, nptr[i].fake_username);
111                    spoofed = 1;
112                    user_spoofed = 1;
113                 }
114                 else
115                    strcpy(un, nptr[i].curr_user);
116                    
117                 if (nptr[i].fake_hostname[0])
118                 {
119                    strcpy(host, nptr[i].fake_hostname);
120                    spoofed = 1;
121                    host_spoofed = 1;
122                 }
123                 else
124                    strcpy(host, nptr[i].cs_host);
125
126                 GenerateRoomDisplay(real_room, &nptr[i], CC);
127
128                 if (nptr[i].fake_roomname[0]) {
129                         strcpy(room, nptr[i].fake_roomname);
130                         spoofed = 1;
131                         room_spoofed = 1;
132                 }
133                 else {
134                         strcpy(room, real_room);
135                 }
136                 
137                 if ((aide) && (spoofed)) {
138                         strcat(flags, "+");
139                 }
140                 
141                 if ((nptr[i].cs_flags & CS_STEALTH) && (aide)) {
142                         strcat(flags, "-");
143                 }
144                 
145                 if (((nptr[i].cs_flags&CS_STEALTH)==0) || (aide))
146                 {
147                         cprintf("%d|%s|%s|%s|%s|%ld|%s|%s|",
148                                 nptr[i].cs_pid, un, room,
149                                 host, nptr[i].cs_clientname,
150                                 (long)(nptr[i].lastidle),
151                                 nptr[i].lastcmdname, flags
152                         );
153
154                         if ((user_spoofed) && (aide)) {
155                                 cprintf("%s|", nptr[i].curr_user);
156                         }
157                         else {
158                                 cprintf("|");
159                         }
160         
161                         if ((room_spoofed) && (aide)) {
162                                 cprintf("%s|", real_room);
163                         }
164                         else {
165                                 cprintf("|");
166                         }
167         
168                         if ((host_spoofed) && (aide)) {
169                                 cprintf("%s|", nptr[i].cs_host);
170                         }
171                         else {
172                                 cprintf("|");
173                         }
174         
175                         cprintf("%d\n", nptr[i].logged_in);
176                 }
177         }
178         
179         /* release out copy of the context list */
180         free(nptr);
181
182         /* Now it's magic time.  Before we finish, call any EVT_RWHO hooks
183          * so that external paging modules such as serv_icq can add more
184          * content to the Wholist.
185          */
186         PerformSessionHooks(EVT_RWHO);
187         cprintf("000\n");
188 }
189
190
191 /*
192  * Masquerade roomname
193  */
194 void cmd_rchg(char *argbuf)
195 {
196         char newroomname[ROOMNAMELEN];
197
198         extract_token(newroomname, argbuf, 0, '|', sizeof newroomname);
199         newroomname[ROOMNAMELEN-1] = 0;
200         if (!IsEmptyStr(newroomname)) {
201                 safestrncpy(CC->fake_roomname, newroomname,
202                         sizeof(CC->fake_roomname) );
203         }
204         else {
205                 safestrncpy(CC->fake_roomname, "", sizeof CC->fake_roomname);
206         }
207         cprintf("%d OK\n", CIT_OK);
208 }
209
210 /*
211  * Masquerade hostname 
212  */
213 void cmd_hchg(char *argbuf)
214 {
215         char newhostname[64];
216
217         extract_token(newhostname, argbuf, 0, '|', sizeof newhostname);
218         if (!IsEmptyStr(newhostname)) {
219                 safestrncpy(CC->fake_hostname, newhostname,
220                         sizeof(CC->fake_hostname) );
221         }
222         else {
223                 safestrncpy(CC->fake_hostname, "", sizeof CC->fake_hostname);
224         }
225         cprintf("%d OK\n", CIT_OK);
226 }
227
228
229 /*
230  * Masquerade username (aides only)
231  */
232 void cmd_uchg(char *argbuf)
233 {
234
235         char newusername[USERNAME_SIZE];
236
237         extract_token(newusername, argbuf, 0, '|', sizeof newusername);
238
239         if (CtdlAccessCheck(ac_aide)) return;
240
241         if (!IsEmptyStr(newusername)) {
242                 CC->cs_flags &= ~CS_STEALTH;
243                 memset(CC->fake_username, 0, 32);
244                 if (strncasecmp(newusername, CC->curr_user,
245                                 strlen(CC->curr_user)))
246                         safestrncpy(CC->fake_username, newusername,
247                                 sizeof(CC->fake_username));
248         }
249         else {
250                 CC->fake_username[0] = '\0';
251                 CC->cs_flags |= CS_STEALTH;
252         }
253         cprintf("%d\n",CIT_OK);
254 }
255
256
257
258
259 /*
260  * enter or exit "stealth mode"
261  */
262 void cmd_stel(char *cmdbuf)
263 {
264         int requested_mode;
265
266         requested_mode = extract_int(cmdbuf,0);
267
268         if (CtdlAccessCheck(ac_logged_in)) return;
269
270         if (requested_mode == 1) {
271                 CC->cs_flags = CC->cs_flags | CS_STEALTH;
272                 PerformSessionHooks(EVT_STEALTH);
273         }
274         if (requested_mode == 0) {
275                 CC->cs_flags = CC->cs_flags & ~CS_STEALTH;
276                 PerformSessionHooks(EVT_UNSTEALTH);
277         }
278
279         cprintf("%d %d\n", CIT_OK,
280                 ((CC->cs_flags & CS_STEALTH) ? 1 : 0) );
281 }
282
283
284 CTDL_MODULE_INIT(rwho)
285 {
286         if(!threading)
287         {
288                 CtdlRegisterProtoHook(cmd_rwho, "RWHO", "Display who is online");
289                 CtdlRegisterProtoHook(cmd_hchg, "HCHG", "Masquerade hostname");
290                 CtdlRegisterProtoHook(cmd_rchg, "RCHG", "Masquerade roomname");
291                 CtdlRegisterProtoHook(cmd_uchg, "UCHG", "Masquerade username");
292                 CtdlRegisterProtoHook(cmd_stel, "STEL", "Enter/exit stealth mode");
293         }
294         
295         /* return our Subversion id for the Log */
296         return "$Id$";
297 }