5329bc70aaf9ab2aec92cd287a86759dc862b87b
[citadel.git] / citadel / modules / rwho / serv_rwho.c
1 /*
2  * This module implements server commands related to the display and
3  * manipulation of the "Who's online" list.
4  *
5  * Copyright (c) 1987-2012 by the citadel.org team
6  *
7  * This program is open source software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 3.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  */
16
17 #include "sysdep.h"
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <stdio.h>
21 #include <fcntl.h>
22 #include <signal.h>
23 #include <pwd.h>
24 #include <errno.h>
25 #include <sys/types.h>
26
27 #if TIME_WITH_SYS_TIME
28 # include <sys/time.h>
29 # include <time.h>
30 #else
31 # if HAVE_SYS_TIME_H
32 #  include <sys/time.h>
33 # else
34 #  include <time.h>
35 # endif
36 #endif
37
38 #include <sys/wait.h>
39 #include <string.h>
40 #include <limits.h>
41 #include <libcitadel.h>
42 #include "citadel.h"
43 #include "server.h"
44 #include "citserver.h"
45 #include "support.h"
46 #include "config.h"
47 #include "control.h"
48 #include "user_ops.h"
49 #include "database.h"
50 #include "msgbase.h"
51
52
53 #include "ctdl_module.h"
54
55 /* Don't show the names of private rooms unless the viewing
56  * user also knows the rooms.
57  */
58 void GenerateRoomDisplay(char *real_room,
59                         CitContext *viewed,
60                         CitContext *viewer) {
61
62         int ra;
63
64         strcpy(real_room, viewed->room.QRname);
65         if (viewed->room.QRflags & QR_MAILBOX) {
66                 strcpy(real_room, &real_room[11]);
67         }
68         if (viewed->room.QRflags & QR_PRIVATE) {
69                 CtdlRoomAccess(&viewed->room, &viewer->user, &ra, NULL);
70                 if ( (ra & UA_KNOWN) == 0) {
71                         strcpy(real_room, " ");
72                 }
73         }
74
75         if (viewed->cs_flags & CS_CHAT) {
76                 while (strlen(real_room) < 14) {
77                         strcat(real_room, " ");
78                 }
79                 strcpy(&real_room[14], "<chat>");
80         }
81
82 }
83
84
85
86 /*
87  * display who's online
88  */
89 void cmd_rwho(char *argbuf) {
90         struct CitContext *nptr;
91         int nContexts, i;
92         int spoofed = 0;
93         int user_spoofed = 0;
94         int room_spoofed = 0;
95         int host_spoofed = 0;
96         int aide;
97         char un[40];
98         char real_room[ROOMNAMELEN], room[ROOMNAMELEN];
99         char host[64], flags[5];
100         
101         /* So that we don't keep the context list locked for a long time
102          * we create a copy of it first
103          */
104         nptr = CtdlGetContextArray(&nContexts) ;
105         if (!nptr)
106         {
107                 /* Couldn't malloc so we have to bail but stick to the protocol */
108                 cprintf("%d%c \n", LISTING_FOLLOWS, CtdlCheckExpress() );
109                 cprintf("000\n");
110                 return;
111         }
112         
113         aide = ( (CC->user.axlevel >= AxAideU) || (CC->internal_pgm) ) ;
114         cprintf("%d%c \n", LISTING_FOLLOWS, CtdlCheckExpress() );
115         
116         for (i=0; i<nContexts; i++) 
117         {
118                 flags[0] = '\0';
119                 spoofed = 0;
120                 user_spoofed = 0;
121                 room_spoofed = 0;
122                 host_spoofed = 0;
123                 
124                 if (!aide && nptr[i].state == CON_SYS)
125                         continue;
126
127                 if (!aide && nptr[i].kill_me != 0)
128                         continue;
129
130                 if (nptr[i].cs_flags & CS_POSTING)
131                    strcat(flags, "*");
132                 else
133                    strcat(flags, ".");
134                    
135                 if (nptr[i].fake_username[0])
136                 {
137                    strcpy(un, nptr[i].fake_username);
138                    spoofed = 1;
139                    user_spoofed = 1;
140                 }
141                 else
142                    strcpy(un, nptr[i].curr_user);
143                    
144                 if (nptr[i].fake_hostname[0])
145                 {
146                    strcpy(host, nptr[i].fake_hostname);
147                    spoofed = 1;
148                    host_spoofed = 1;
149                 }
150                 else
151                    strcpy(host, nptr[i].cs_host);
152
153                 GenerateRoomDisplay(real_room, &nptr[i], CC);
154
155                 if (nptr[i].fake_roomname[0]) {
156                         strcpy(room, nptr[i].fake_roomname);
157                         spoofed = 1;
158                         room_spoofed = 1;
159                 }
160                 else {
161                         strcpy(room, real_room);
162                 }
163                 
164                 if ((aide) && (spoofed)) {
165                         strcat(flags, "+");
166                 }
167                 
168                 if ((nptr[i].cs_flags & CS_STEALTH) && (aide)) {
169                         strcat(flags, "-");
170                 }
171                 
172                 if (((nptr[i].cs_flags&CS_STEALTH)==0) || (aide))
173                 {
174                         cprintf("%d|%s|%s|%s|%s|%ld|%s|%s|",
175                                 nptr[i].cs_pid, un, room,
176                                 host, nptr[i].cs_clientname,
177                                 (long)(nptr[i].lastidle),
178                                 nptr[i].lastcmdname, flags
179                         );
180
181                         if ((user_spoofed) && (aide)) {
182                                 cprintf("%s|", nptr[i].curr_user);
183                         }
184                         else {
185                                 cprintf("|");
186                         }
187         
188                         if ((room_spoofed) && (aide)) {
189                                 cprintf("%s|", real_room);
190                         }
191                         else {
192                                 cprintf("|");
193                         }
194         
195                         if ((host_spoofed) && (aide)) {
196                                 cprintf("%s|", nptr[i].cs_host);
197                         }
198                         else {
199                                 cprintf("|");
200                         }
201         
202                         cprintf("%d\n", nptr[i].logged_in);
203                 }
204         }
205         
206         /* release out copy of the context list */
207         free(nptr);
208
209         /* Now it's magic time.  Before we finish, call any EVT_RWHO hooks
210          * so that external paging modules such as serv_icq can add more
211          * content to the Wholist.
212          */
213         PerformSessionHooks(EVT_RWHO);
214         cprintf("000\n");
215 }
216
217 /*
218  * check for async io jobs that are stuck (didn't ping back for 10 mins)
219  */
220 void dead_io_check(void) {
221         struct CitContext *nptr;
222         int nContexts, i;
223         char real_room[ROOMNAMELEN];
224         
225         /* So that we don't keep the context list locked for a long time
226          * we create a copy of it first
227          */
228         nptr = CtdlGetContextArray(&nContexts) ;
229         if (!nptr)
230         {
231                 /* Couldn't malloc so we have to bail but stick to the protocol */
232                 return;
233         }
234
235         time_t now = time(NULL);
236         time_t idle;
237
238         for (i=0; i<nContexts; i++) 
239         {
240                 if ((nptr[i].state != CON_SYS) || (nptr[i].IO == NULL) || (nptr[i].lastcmd == 0))
241                         continue;
242
243                 if (nptr[i].kill_me != 0)
244                         continue;
245                 idle = now - nptr[i].lastcmd;
246                 if (idle < 600) 
247                         continue;
248
249                 GenerateRoomDisplay(real_room, &nptr[i], CC);
250
251                 syslog(LOG_WARNING,
252                        "Found stuck event context: CC[%d] "
253
254                        "Username: '%s' "
255                        "Room: '%s' "
256                        "while talking to host: '%s' "
257                        "Status: '%s' "
258                        "stuck in IO State: '%s' "
259
260                        "idle since: %d:%d "
261                        "Triggering context termination now!",
262
263                        nptr[i].cs_pid,
264
265                        nptr[i].curr_user,
266                        real_room,
267                        nptr[i].cs_host,
268                        nptr[i].cs_clientname,
269                        nptr[i].lastcmdname,
270
271                        (int) idle / 60,
272                        (int) idle % 60);
273
274                 CtdlTerminateOtherSession(nptr[i].cs_pid);
275         }
276         
277         /* release out copy of the context list */
278         free(nptr);
279
280 }
281
282 /*
283  * Masquerade roomname
284  */
285 void cmd_rchg(char *argbuf)
286 {
287         char newroomname[ROOMNAMELEN];
288
289         extract_token(newroomname, argbuf, 0, '|', sizeof newroomname);
290         newroomname[ROOMNAMELEN-1] = 0;
291         if (!IsEmptyStr(newroomname)) {
292                 safestrncpy(CC->fake_roomname, newroomname,
293                         sizeof(CC->fake_roomname) );
294         }
295         else {
296                 safestrncpy(CC->fake_roomname, "", sizeof CC->fake_roomname);
297         }
298         cprintf("%d OK\n", CIT_OK);
299 }
300
301 /*
302  * Masquerade hostname 
303  */
304 void cmd_hchg(char *argbuf)
305 {
306         char newhostname[64];
307
308         extract_token(newhostname, argbuf, 0, '|', sizeof newhostname);
309         if (!IsEmptyStr(newhostname)) {
310                 safestrncpy(CC->fake_hostname, newhostname,
311                         sizeof(CC->fake_hostname) );
312         }
313         else {
314                 safestrncpy(CC->fake_hostname, "", sizeof CC->fake_hostname);
315         }
316         cprintf("%d OK\n", CIT_OK);
317 }
318
319
320 /*
321  * Masquerade username (aides only)
322  */
323 void cmd_uchg(char *argbuf)
324 {
325
326         char newusername[USERNAME_SIZE];
327
328         extract_token(newusername, argbuf, 0, '|', sizeof newusername);
329
330         if (CtdlAccessCheck(ac_aide)) return;
331
332         if (!IsEmptyStr(newusername)) {
333                 CC->cs_flags &= ~CS_STEALTH;
334                 memset(CC->fake_username, 0, 32);
335                 if (strncasecmp(newusername, CC->curr_user,
336                                 strlen(CC->curr_user)))
337                         safestrncpy(CC->fake_username, newusername,
338                                 sizeof(CC->fake_username));
339         }
340         else {
341                 CC->fake_username[0] = '\0';
342                 CC->cs_flags |= CS_STEALTH;
343         }
344         cprintf("%d\n",CIT_OK);
345 }
346
347
348
349
350 /*
351  * enter or exit "stealth mode"
352  */
353 void cmd_stel(char *cmdbuf)
354 {
355         int requested_mode;
356
357         requested_mode = extract_int(cmdbuf,0);
358
359         if (CtdlAccessCheck(ac_logged_in)) return;
360
361         if (requested_mode == 1) {
362                 CC->cs_flags = CC->cs_flags | CS_STEALTH;
363                 PerformSessionHooks(EVT_STEALTH);
364         }
365         if (requested_mode == 0) {
366                 CC->cs_flags = CC->cs_flags & ~CS_STEALTH;
367                 PerformSessionHooks(EVT_UNSTEALTH);
368         }
369
370         cprintf("%d %d\n", CIT_OK,
371                 ((CC->cs_flags & CS_STEALTH) ? 1 : 0) );
372 }
373
374
375 CTDL_MODULE_INIT(rwho)
376 {
377         if(!threading)
378         {
379                 CtdlRegisterProtoHook(cmd_rwho, "RWHO", "Display who is online");
380                 CtdlRegisterProtoHook(cmd_hchg, "HCHG", "Masquerade hostname");
381                 CtdlRegisterProtoHook(cmd_rchg, "RCHG", "Masquerade roomname");
382                 CtdlRegisterProtoHook(cmd_uchg, "UCHG", "Masquerade username");
383                 CtdlRegisterProtoHook(cmd_stel, "STEL", "Enter/exit stealth mode");
384                 CtdlRegisterSessionHook(dead_io_check, EVT_TIMER, PRIO_QUEUE + 50);
385
386         }
387         
388         /* return our module name for the log */
389         return "rwho";
390 }