* More license declarations
[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 "room_ops.h"
56 #include "user_ops.h"
57 #include "policy.h"
58 #include "database.h"
59 #include "msgbase.h"
60
61
62 #include "ctdl_module.h"
63
64
65 /*
66  * display who's online
67  */
68 void cmd_rwho(char *argbuf) {
69         struct CitContext *nptr;
70         int nContexts, i;
71         int spoofed = 0;
72         int user_spoofed = 0;
73         int room_spoofed = 0;
74         int host_spoofed = 0;
75         int aide;
76         char un[40];
77         char real_room[ROOMNAMELEN], room[ROOMNAMELEN];
78         char host[64], flags[5];
79         
80         /* So that we don't keep the context list locked for a long time
81          * we create a copy of it first
82          */
83         
84
85         nptr = CtdlGetContextArray(&nContexts) ;
86         if (!nptr)
87         {
88                 /* Couldn't malloc so we have to bail but stick to the protocol */
89                 cprintf("%d%c \n", LISTING_FOLLOWS, CtdlCheckExpress() );
90                 cprintf("000\n");
91                 return;
92         }
93         
94         aide = CC->user.axlevel >= 6;
95         cprintf("%d%c \n", LISTING_FOLLOWS, CtdlCheckExpress() );
96         
97         for (i=0; i<nContexts; i++) 
98         {
99                 flags[0] = '\0';
100                 spoofed = 0;
101                 user_spoofed = 0;
102                 room_spoofed = 0;
103                 host_spoofed = 0;
104                 
105                 if (nptr[i].cs_flags & CS_POSTING)
106                    strcat(flags, "*");
107                 else
108                    strcat(flags, ".");
109                    
110                 if (nptr[i].fake_username[0])
111                 {
112                    strcpy(un, nptr[i].fake_username);
113                    spoofed = 1;
114                    user_spoofed = 1;
115                 }
116                 else
117                    strcpy(un, nptr[i].curr_user);
118                    
119                 if (nptr[i].fake_hostname[0])
120                 {
121                    strcpy(host, nptr[i].fake_hostname);
122                    spoofed = 1;
123                    host_spoofed = 1;
124                 }
125                 else
126                    strcpy(host, nptr[i].cs_host);
127
128                 GenerateRoomDisplay(real_room, &nptr[i], CC);
129
130                 if (nptr[i].fake_roomname[0]) {
131                         strcpy(room, nptr[i].fake_roomname);
132                         spoofed = 1;
133                         room_spoofed = 1;
134                 }
135                 else {
136                         strcpy(room, real_room);
137                 }
138                 
139                 if ((aide) && (spoofed)) {
140                         strcat(flags, "+");
141                 }
142                 
143                 if ((nptr[i].cs_flags & CS_STEALTH) && (aide)) {
144                         strcat(flags, "-");
145                 }
146                 
147                 if (((nptr[i].cs_flags&CS_STEALTH)==0) || (aide))
148                 {
149                         cprintf("%d|%s|%s|%s|%s|%ld|%s|%s|",
150                                 nptr[i].cs_pid, un, room,
151                                 host, nptr[i].cs_clientname,
152                                 (long)(nptr[i].lastidle),
153                                 nptr[i].lastcmdname, flags
154                         );
155
156                         if ((user_spoofed) && (aide)) {
157                                 cprintf("%s|", nptr[i].curr_user);
158                         }
159                         else {
160                                 cprintf("|");
161                         }
162         
163                         if ((room_spoofed) && (aide)) {
164                                 cprintf("%s|", real_room);
165                         }
166                         else {
167                                 cprintf("|");
168                         }
169         
170                         if ((host_spoofed) && (aide)) {
171                                 cprintf("%s|", nptr[i].cs_host);
172                         }
173                         else {
174                                 cprintf("|");
175                         }
176         
177                         cprintf("%d\n", nptr[i].logged_in);
178                 }
179         }
180         
181         /* release out copy of the context list */
182         free(nptr);
183
184         /* Now it's magic time.  Before we finish, call any EVT_RWHO hooks
185          * so that external paging modules such as serv_icq can add more
186          * content to the Wholist.
187          */
188         PerformSessionHooks(EVT_RWHO);
189         cprintf("000\n");
190 }
191
192
193 /*
194  * Masquerade roomname
195  */
196 void cmd_rchg(char *argbuf)
197 {
198         char newroomname[ROOMNAMELEN];
199
200         extract_token(newroomname, argbuf, 0, '|', sizeof newroomname);
201         newroomname[ROOMNAMELEN-1] = 0;
202         if (!IsEmptyStr(newroomname)) {
203                 safestrncpy(CC->fake_roomname, newroomname,
204                         sizeof(CC->fake_roomname) );
205         }
206         else {
207                 safestrncpy(CC->fake_roomname, "", sizeof CC->fake_roomname);
208         }
209         cprintf("%d OK\n", CIT_OK);
210 }
211
212 /*
213  * Masquerade hostname 
214  */
215 void cmd_hchg(char *argbuf)
216 {
217         char newhostname[64];
218
219         extract_token(newhostname, argbuf, 0, '|', sizeof newhostname);
220         if (!IsEmptyStr(newhostname)) {
221                 safestrncpy(CC->fake_hostname, newhostname,
222                         sizeof(CC->fake_hostname) );
223         }
224         else {
225                 safestrncpy(CC->fake_hostname, "", sizeof CC->fake_hostname);
226         }
227         cprintf("%d OK\n", CIT_OK);
228 }
229
230
231 /*
232  * Masquerade username (aides only)
233  */
234 void cmd_uchg(char *argbuf)
235 {
236
237         char newusername[USERNAME_SIZE];
238
239         extract_token(newusername, argbuf, 0, '|', sizeof newusername);
240
241         if (CtdlAccessCheck(ac_aide)) return;
242
243         if (!IsEmptyStr(newusername)) {
244                 CC->cs_flags &= ~CS_STEALTH;
245                 memset(CC->fake_username, 0, 32);
246                 if (strncasecmp(newusername, CC->curr_user,
247                                 strlen(CC->curr_user)))
248                         safestrncpy(CC->fake_username, newusername,
249                                 sizeof(CC->fake_username));
250         }
251         else {
252                 CC->fake_username[0] = '\0';
253                 CC->cs_flags |= CS_STEALTH;
254         }
255         cprintf("%d\n",CIT_OK);
256 }
257
258
259
260
261 /*
262  * enter or exit "stealth mode"
263  */
264 void cmd_stel(char *cmdbuf)
265 {
266         int requested_mode;
267
268         requested_mode = extract_int(cmdbuf,0);
269
270         if (CtdlAccessCheck(ac_logged_in)) return;
271
272         if (requested_mode == 1) {
273                 CC->cs_flags = CC->cs_flags | CS_STEALTH;
274                 PerformSessionHooks(EVT_STEALTH);
275         }
276         if (requested_mode == 0) {
277                 CC->cs_flags = CC->cs_flags & ~CS_STEALTH;
278                 PerformSessionHooks(EVT_UNSTEALTH);
279         }
280
281         cprintf("%d %d\n", CIT_OK,
282                 ((CC->cs_flags & CS_STEALTH) ? 1 : 0) );
283 }
284
285
286 CTDL_MODULE_INIT(rwho)
287 {
288         if(!threading)
289         {
290                 CtdlRegisterProtoHook(cmd_rwho, "RWHO", "Display who is online");
291                 CtdlRegisterProtoHook(cmd_hchg, "HCHG", "Masquerade hostname");
292                 CtdlRegisterProtoHook(cmd_rchg, "RCHG", "Masquerade roomname");
293                 CtdlRegisterProtoHook(cmd_uchg, "UCHG", "Masquerade username");
294                 CtdlRegisterProtoHook(cmd_stel, "STEL", "Enter/exit stealth mode");
295         }
296         
297         /* return our Subversion id for the Log */
298         return "$Id$";
299 }