* Replaced most of the very repetitive and very redundant access level checks
[citadel.git] / citadel / serv_rwho.c
1 /*
2  * $Id$
3  *
4  * This module implementsserver commands related to the display and
5  * manipulation of the "Who's online" list.
6  *
7  */
8
9 #include "sysdep.h"
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <fcntl.h>
14 #include <signal.h>
15 #include <pwd.h>
16 #include <errno.h>
17 #include <sys/types.h>
18 #include <sys/time.h>
19 #include <sys/wait.h>
20 #include <string.h>
21 #include <limits.h>
22 #include "citadel.h"
23 #include "server.h"
24 #include <time.h>
25 #include "sysdep_decls.h"
26 #include "citserver.h"
27 #include "support.h"
28 #include "config.h"
29 #include "control.h"
30 #include "dynloader.h"
31 #include "room_ops.h"
32 #include "user_ops.h"
33 #include "policy.h"
34 #include "database.h"
35 #include "msgbase.h"
36 #include "tools.h"
37
38
39
40 /*
41  * display who's online
42  */
43 void cmd_rwho(char *argbuf) {
44         struct CitContext *cptr;
45         int spoofed = 0;
46         int aide;
47         char un[40];
48         char real_room[ROOMNAMELEN], room[ROOMNAMELEN];
49         char host[40], flags[5];
50         
51         aide = CC->usersupp.axlevel >= 6;
52         cprintf("%d%c \n", LISTING_FOLLOWS, CtdlCheckExpress() );
53         
54         for (cptr = ContextList; cptr != NULL; cptr = cptr->next) 
55         {
56                 flags[0] = '\0';
57                 spoofed = 0;
58                 
59                 if (cptr->cs_flags & CS_POSTING)
60                    strcat(flags, "*");
61                 else
62                    strcat(flags, ".");
63                    
64                 if (cptr->fake_username[0])
65                 {
66                    strcpy(un, cptr->fake_username);
67                    spoofed = 1;
68                 }
69                 else
70                    strcpy(un, cptr->curr_user);
71                    
72                 if (cptr->fake_hostname[0])
73                 {
74                    strcpy(host, cptr->fake_hostname);
75                    spoofed = 1;
76                 }
77                 else
78                    strcpy(host, cptr->cs_host);
79
80                 GenerateRoomDisplay(real_room, cptr, CC);
81
82                 if (cptr->fake_roomname[0]) {
83                         strcpy(room, cptr->fake_roomname);
84                         spoofed = 1;
85                 }
86                 else {
87                         strcpy(room, real_room);
88                 }
89                 
90                 if ((aide) && (spoofed))
91                    strcat(flags, "+");
92                 
93                 if ((cptr->cs_flags & CS_STEALTH) && (aide))
94                    strcat(flags, "-");
95                 
96                 if (((cptr->cs_flags&CS_STEALTH)==0) || (aide))
97                 {
98                         cprintf("%d|%s|%s|%s|%s|%ld|%s|%s\n",
99                                 cptr->cs_pid, un, room,
100                                 host, cptr->cs_clientname,
101                                 (long)(cptr->lastidle),
102                                 cptr->lastcmdname, flags);
103                 }
104                 if ((spoofed) && (aide))
105                 {
106                         cprintf("%d|%s|%s|%s|%s|%ld|%s|%s\n",
107                                 cptr->cs_pid, cptr->curr_user,
108                                 real_room,
109                                 cptr->cs_host, cptr->cs_clientname,
110                                 (long)(cptr->lastidle),
111                                 cptr->lastcmdname, flags);
112                 
113                 }
114         }
115
116         /* Now it's magic time.  Before we finish, call any EVT_RWHO hooks
117          * so that external paging modules such as serv_icq can add more
118          * content to the Wholist.
119          */
120         PerformSessionHooks(EVT_RWHO);
121         cprintf("000\n");
122         }
123
124
125 /*
126  * Masquerade roomname
127  */
128 void cmd_rchg(char *argbuf)
129 {
130         char newroomname[256];
131
132         extract(newroomname, argbuf, 0);
133         newroomname[ROOMNAMELEN-1] = 0;
134         if (strlen(newroomname) > 0) {
135                 safestrncpy(CC->fake_roomname, newroomname,
136                         sizeof(CC->fake_roomname) );
137                 }
138         else {
139                 strcpy(CC->fake_roomname, "");
140                 }
141         cprintf("%d OK\n", OK);
142 }
143
144 /*
145  * Masquerade hostname 
146  */
147 void cmd_hchg(char *argbuf)
148 {
149         char newhostname[256];
150
151         extract(newhostname, argbuf, 0);
152         if (strlen(newhostname) > 0) {
153                 safestrncpy(CC->fake_hostname, newhostname,
154                         sizeof(CC->fake_hostname) );
155                 }
156         else {
157                 strcpy(CC->fake_hostname, "");
158                 }
159         cprintf("%d OK\n", OK);
160 }
161
162
163 /*
164  * Masquerade username (aides only)
165  */
166 void cmd_uchg(char *argbuf)
167 {
168
169         char newusername[256];
170
171         extract(newusername, argbuf, 0);
172
173         if (CtdlAccessCheck(ac_aide)) return;
174
175         if (strlen(newusername) > 0) {
176                 CC->cs_flags &= ~CS_STEALTH;
177                 memset(CC->fake_username, 0, 32);
178                 if (strncasecmp(newusername, CC->curr_user,
179                                 strlen(CC->curr_user)))
180                         safestrncpy(CC->fake_username, newusername,
181                                 sizeof(CC->fake_username));
182         }
183         else {
184                 CC->fake_username[0] = '\0';
185                 CC->cs_flags |= CS_STEALTH;
186         }
187         cprintf("%d\n",OK);
188 }
189
190
191
192
193 /*
194  * enter or exit "stealth mode"
195  */
196 void cmd_stel(char *cmdbuf)
197 {
198         int requested_mode;
199
200         requested_mode = extract_int(cmdbuf,0);
201         if (requested_mode !=0) requested_mode = 1;
202
203         if (CtdlAccessCheck(ac_aide)) return;
204
205         if (CC->cs_flags & CS_STEALTH) {
206                 if (requested_mode == 0)
207                         CC->cs_flags = CC->cs_flags-CS_STEALTH;
208                 }
209         else {
210                 if (requested_mode == 1)
211                         CC->cs_flags = CC->cs_flags|CS_STEALTH;
212                 }
213
214         cprintf("%d Ok\n",OK);
215         }
216
217
218
219
220
221
222
223 char *Dynamic_Module_Init(void)
224 {
225         CtdlRegisterProtoHook(cmd_rwho, "RWHO", "Display who is online");
226         CtdlRegisterProtoHook(cmd_hchg, "HCHG", "Masquerade hostname");
227         CtdlRegisterProtoHook(cmd_rchg, "RCHG", "Masquerade roomname");
228         CtdlRegisterProtoHook(cmd_uchg, "UCHG", "Masquerade username");
229         CtdlRegisterProtoHook(cmd_stel, "STEL", "Enter/exit stealth mode");
230         return "$Id$";
231 }