1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
|
/************************************************************************
* IRC - Internet Relay Chat, contrib/m_operspy.c
* Copyright (C) 2002 William Bierman III and the Hybrid Development Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 1, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* $Id$
*/
/*** PLEASE READ ME ***/
/*
* This module gives an extraordinary amount of power to the opers
* who have the access to use it. It allows for users' privacy to
* be pretty much obliterated. The Hybrid Team assumes absolutely
* no responsibility for this file's (mis)use.
*
* - billy-jon
*/
#include "stdinc.h"
#include "list.h"
#include "irc_string.h"
#include "channel.h"
#include "channel_mode.h"
#include "client.h"
#include "ircd.h"
#include "sprintf_irc.h"
#include "numeric.h"
#include "fdlist.h"
#include "s_bsd.h"
#include "conf.h"
#include "log.h"
#include "s_serv.h"
#include "s_misc.h"
#include "send.h"
#include "parse.h"
#include "modules.h"
#include "hash.h"
/* enable logging of OPERSPY functions */
#undef OPERSPY_LOG
/* enable this to log all local/remote operspy usage to a logfile */
#undef OPERSPY_LOGFILE
/* enable this to send incoming operspy usage to connected +y (UMODE_SPY) opers */
#undef OPERSPY_NOTICE
/* enable OPERSPY version of LIST */
#define OPERSPY_LIST
/* enable OPERSPY version of MODE */
#define OPERSPY_MODE
/* enable OPERSPY version of NAMES */
#define OPERSPY_NAMES
/* enable OPERSPY version of WHO */
#define OPERSPY_WHO
/* enable OPERSPY version of WHOIS */
#define OPERSPY_WHOIS
/* enable OPERSPY version of TOPIC */
#define OPERSPY_TOPIC
#define IsOperspy(x) (HasUMode(x, UMODE_OPER) && MyClient(x))
/* extensions for OPERSPY WHO */
static void do_who(struct Client *, struct Client *, char *, const char *);
static void who_global(struct Client *, char *, int);
static void do_who_on_channel(struct Client *, struct Channel *, char *);
static void operspy_list(struct Client *, int, char *[]);
static void operspy_mode(struct Client *, int, char *[]);
static void operspy_names(struct Client *, int, char *[]);
static void operspy_topic(struct Client *, int, char *[]);
static void operspy_who(struct Client *, int, char *[]);
static void operspy_whois(struct Client *, int, char *[]);
#ifdef OPERSPY_LOG
static void operspy_log(struct Client *, const char *, const char *);
#endif
static const struct operspy_s {
const char *const cmd;
void (*const func_p)(struct Client *, int, char *[]);
} operspy_table[] = {
#ifdef OPERSPY_LIST
{ "LIST", operspy_list },
#endif
#ifdef OPERSPY_MODE
{ "MODE", operspy_mode },
#endif
#ifdef OPERSPY_NAMES
{ "NAMES", operspy_names },
#endif
#ifdef OPERSPY_TOPIC
{ "TOPIC", operspy_topic },
#endif
#ifdef OPERSPY_WHO
{ "WHO", operspy_who },
#endif
#ifdef OPERSPY_WHOIS
{ "WHOIS", operspy_whois },
#endif
{ NULL, NULL }
};
static void
ms_operspy(struct Client *client_p, struct Client *source_p,
int parc, char *parv[])
{
#ifdef OPERSPY_LOG
operspy_log(source_p, parv[1], parv[2]);
#endif
}
/* mo_operspy()
* parv[1] = operspy command
* parv[2] = command parameter
*/
static void
mo_operspy(struct Client *client_p, struct Client *source_p,
int parc, char *parv[])
{
char cmdbuf[IRCD_BUFSIZE] = "<NONE>"; /* in case everything is undef'd */
size_t bcnt = 0;
const struct operspy_s *optr = NULL;
if (!IsOperspy(client_p))
{
sendto_one(client_p, form_str(ERR_NOPRIVILEGES),
me.name, client_p->name);
return;
}
assert(client_p == source_p);
for (optr = operspy_table; optr->cmd; ++optr)
{
if (!irccmp(optr->cmd, parv[1]))
{
(*optr->func_p)(client_p, parc, parv);
return;
}
}
for (optr = operspy_table; optr->cmd; ++optr)
{
/* str*cat is slow and sucks */
bcnt += strlcpy(cmdbuf+bcnt, optr->cmd, sizeof(cmdbuf)-bcnt);
if ((optr + 1)->cmd != NULL && bcnt < (sizeof(cmdbuf)-2))
{
cmdbuf[bcnt++] = ',';
cmdbuf[bcnt++] = ' ';
}
}
sendto_one(client_p, ":%s NOTICE %s :%s is not a valid option. Choose from %s",
me.name, client_p->name, parv[1], cmdbuf);
}
static void
operspy_list(struct Client *client_p, int parc, char *parv[])
{
const dlink_node *ptr = NULL;
#ifdef OPERSPY_LOG
operspy_log(client_p, "LIST", parv[2]);
#endif
if (*parv[2] == '\0')
return;
sendto_one(client_p, form_str(RPL_LISTSTART),
me.name, client_p->name);
DLINK_FOREACH(ptr, global_channel_list.head)
{
const struct Channel *chptr_list = ptr->data;
if (!match(parv[2], chptr_list->chname))
sendto_one(client_p, form_str(RPL_LIST), me.name, client_p->name,
chptr_list->chname, dlink_list_length(&chptr_list->members),
chptr_list->topic);
}
sendto_one(client_p, form_str(RPL_LISTEND),
me.name, client_p->name);
}
static void
operspy_mode(struct Client *client_p, int parc, char *parv[])
{
/* needed to preserve actual client status */
int c_status = 0;
char modebuf[MODEBUFLEN];
char parabuf[MODEBUFLEN];
struct Channel *chptr_mode = NULL;
if ((chptr_mode = hash_find_channel(parv[2])) == NULL)
{
/*
* according to m_mode.c, the channel *could* exist on the uplink still,
* but I don't see how. Even if it does, we won't be able to spy without
* info.
*/
sendto_one(client_p, form_str(ERR_NOSUCHCHANNEL),
me.name, client_p->name, parv[2]);
return;
}
#ifdef OPERSPY_LOG
operspy_log(client_p, "MODE", parv[2]);
#endif
/*
* XXX - this is a dirty nasty kludge to trick channel_modes()
* into giving us the key
*/
c_status = client_p->status;
client_p->status = STAT_SERVER;
channel_modes(chptr_mode, client_p, modebuf, parabuf);
client_p->status = c_status;
sendto_one(client_p, form_str(RPL_CHANNELMODEIS),
me.name, client_p->name, parv[2], modebuf, parabuf);
sendto_one(client_p, form_str(RPL_CREATIONTIME),
me.name, client_p->name, parv[2], chptr_mode->channelts);
}
static void
operspy_names(struct Client *client_p, int parc, char *parv[])
{
/* as with mode, must preserve channel modes */
struct Channel *chptr_names = NULL;
if ((chptr_names = hash_find_channel(parv[2])) == NULL)
{
sendto_one(client_p, form_str(ERR_NOSUCHCHANNEL),
me.name, client_p->name, parv[2]);
return;
}
#ifdef OPERSPY_LOG
operspy_log(client_p, "NAMES", parv[2]);
#endif
/*
* the way to go with this, rather than temporarily setting -sp,
* is to temporarily add our client to the member list. then
* we can also list +i users. an unfortunate side-effect of this
* is that your nickname shows up in the list. for now, there is
* no easy way around it.
*/
if (IsMember(client_p, chptr_names))
channel_member_names(client_p, chptr_names, 1);
else
{
add_user_to_channel(chptr_names, client_p, CHFL_CHANOP, 0);
channel_member_names(client_p, chptr_names, 1);
remove_user_from_channel(find_channel_link(client_p, chptr_names));
}
}
static void
operspy_topic(struct Client *client_p, int parc, char *parv[])
{
const struct Channel *chptr_topic = NULL;
if ((chptr_topic = hash_find_channel(parv[2])) == NULL)
{
sendto_one(client_p, form_str(ERR_NOSUCHCHANNEL),
me.name, client_p->name, parv[2]);
return;
}
#ifdef OPERSPY_LOG
operspy_log(client_p, "TOPIC", parv[2]);
#endif
if (chptr_topic->topic[0] == '\0')
sendto_one(client_p, form_str(RPL_NOTOPIC),
me.name, client_p->name, parv[2]);
else
{
sendto_one(client_p, form_str(RPL_TOPIC), me.name, client_p->name,
chptr_topic->chname, chptr_topic->topic);
sendto_one(client_p, form_str(RPL_TOPICWHOTIME), me.name,
client_p->name, chptr_topic->chname, chptr_topic->topic_info,
chptr_topic->topic_time);
}
}
static void
operspy_who(struct Client *client_p, int parc, char *parv[])
{
char *mask = parc > 2 ? parv[2] : NULL;
int server_oper = parc > 3 ? (*parv[3] == 'o') : 0;
struct Channel *chptr_who = NULL;
struct Client *target_p_who = NULL;
if (mask != NULL)
{
collapse(mask);
if (*mask == '\0')
{
sendto_one(client_p, form_str(RPL_ENDOFWHO),
me.name, client_p->name, "*");
return;
}
}
else
{
#ifdef OPERSPY_LOG
operspy_log(client_p, "WHO", "*");
#endif
who_global(client_p, NULL, server_oper);
sendto_one(client_p, form_str(RPL_ENDOFWHO),
me.name, client_p->name, "*");
return;
}
/* /who #channel */
if (IsChanPrefix(*mask))
{
if ((chptr_who = hash_find_channel(mask)) != NULL)
{
#ifdef OPERSPY_LOG
operspy_log(client_p, "WHO", mask);
#endif
do_who_on_channel(client_p, chptr_who, chptr_who->chname);
}
sendto_one(client_p, form_str(RPL_ENDOFWHO),
me.name, client_p->name, mask);
return;
}
/* /who nick */
if ((target_p_who = find_person(client_p, mask)) != NULL)
{
#ifdef OPERSPY_LOG
/* "nick!user@host server\0" */
char nuh[NICKLEN + 1 + USERLEN + 1 + HOSTLEN + 1 + HOSTLEN + 1];
snprintf(nuh, sizeof(nuh), "%s!%s@%s %s", target_p_who->name,
target_p_who->username, target_p_who->host,
target_p_who->servptr->name);
operspy_log(client_p, "WHO", nuh);
#endif
if (target_p_who->channel.head != NULL)
{
chptr_who =
((struct Membership *)target_p_who->channel.head->data)->chptr;
do_who(client_p, target_p_who, chptr_who->chname,
get_member_status(target_p_who->channel.head->data, 0));
}
else
do_who(client_p, target_p_who, NULL, "");
sendto_one(client_p, form_str(RPL_ENDOFWHO),
me.name, client_p->name, mask);
return;
}
#ifdef OPERSPY_LOG
operspy_log(client_p, "WHO", parv[2]);
#endif
/* /who 0 */
if (!strcmp(mask, "0"))
who_global(client_p, NULL, server_oper);
else
who_global(client_p, mask, server_oper);
/* nothing else? end of /who. */
sendto_one(client_p, form_str(RPL_ENDOFWHO),
me.name, client_p->name, mask);
}
static void
operspy_whois(struct Client *client_p, int parc, char *parv[])
{
const dlink_node *lp;
struct Channel *chptr_whois = NULL;
struct Client *a2client_p;
struct Client *target_p = NULL;
char buf[IRCD_BUFSIZE];
#ifdef OPERSPY_LOG
/* "nick!user@host server\0" */
char nuh[NICKLEN + 1 + USERLEN + 1 + HOSTLEN + 1 + HOSTLEN + 1];
#endif
char *t = NULL;
int mlen, tlen;
int cur_len = 0;
int reply_to_send = 0;
if (has_wildcards(parv[2]))
{
sendto_one(client_p, ":%s NOTICE %s :Do not use wildcards with this.",
me.name, client_p->name);
return;
}
if ((target_p = find_person(client_p, parv[2])) == NULL)
{
sendto_one(client_p, form_str(ERR_NOSUCHNICK),
me.name, client_p->name, parv[2]);
return;
}
#ifdef OPERSPY_LOG
snprintf(nuh, sizeof(nuh), "%s!%s@%s %s",
target_p->name, target_p->username, target_p->host,
target_p->servptr->name);
operspy_log(client_p, "WHOIS", nuh);
#endif
a2client_p = target_p->servptr;
sendto_one(client_p, form_str(RPL_WHOISUSER), me.name,
client_p->name, target_p->name, target_p->username,
target_p->host, target_p->info);
mlen = ircsprintf(buf, form_str(RPL_WHOISCHANNELS), me.name,
client_p->name, target_p->name, "");
cur_len = mlen;
t = buf + mlen;
DLINK_FOREACH(lp, target_p->channel.head)
{
chptr_whois = ((struct Membership *)lp->data)->chptr;
if ((cur_len + strlen(chptr_whois->chname) + 2) > (IRCD_BUFSIZE - 4))
{
sendto_one(client_p, "%s", buf);
cur_len = mlen;
t = buf + mlen;
}
tlen = ircsprintf(t, "%s%s%s ",
ShowChannel(client_p, chptr_whois) ? "" : "%",
get_member_status((struct Membership *)lp->data, 1),
chptr_whois->chname);
t += tlen;
cur_len += tlen;
reply_to_send = 1;
}
if (reply_to_send == 1)
sendto_one(client_p, "%s", buf);
sendto_one(client_p, form_str(RPL_WHOISSERVER), me.name,
client_p->name, target_p->name, a2client_p->name,
a2client_p->info);
if (HasUMode(target_p, UMODE_REGISTERED))
sendto_one(client_p, form_str(RPL_WHOISREGNICK),
me.name, client_p->name, target_p->name);
if (target_p->away[0])
sendto_one(client_p, form_str(RPL_AWAY),
me.name, client_p->name, target_p->name,
target_p->away);
if (HasUMode(target_p, UMODE_CALLERID) && !HasUMode(target_p, UMODE_SOFTCALLERID))
sendto_one(client_p, form_str(RPL_TARGUMODEG),
me.name, client_p->name, target_p->name);
if (HasUMode(target_p, UMODE_OPER))
sendto_one(client_p, form_str(HasUMode(target_p, UMODE_ADMIN) ? RPL_WHOISADMIN :
RPL_WHOISOPERATOR), me.name, client_p->name, target_p->name);
if (strcmp(target_p->sockhost, "0") && target_p->sockhost[0] != '\0')
{
sendto_one(client_p, form_str(RPL_WHOISACTUALLY),
me.name, client_p->name, target_p->name,
target_p->sockhost);
}
if (MyConnect(target_p))
{
#ifdef HAVE_LIBCRYPTO
if (target_p->localClient->fd.ssl)
sendto_one(client_p, form_str(RPL_WHOISSECURE),
me.name, client_p->name, target_p->name);
#endif
sendto_one(client_p, form_str(RPL_WHOISIDLE), me.name,
client_p->name, target_p->name,
idle_time_get(client_p, target_p),
target_p->localClient->firsttime);
}
sendto_one(client_p, form_str(RPL_ENDOFWHOIS),
me.name, client_p->name, parv[2]);
}
/* extensions for OPERSPY WHO */
static void
do_who(struct Client *source_p, struct Client *target_p,
char *chname, const char *op_flags)
{
char status[8];
snprintf(status, sizeof(status), "%c%s%s", target_p->away[0] ? 'G' : 'H',
HasUMode(target_p, UMODE_OPER) ? "*" : "", op_flags);
sendto_one(source_p, form_str(RPL_WHOREPLY), me.name, source_p->name,
(chname) ? (chname) : "*",
target_p->username,
target_p->host, target_p->servptr->name, target_p->name,
status, target_p->hopcount, target_p->info);
}
static void
who_global(struct Client *source_p, char *mask, int server_oper)
{
struct Client *target_p;
dlink_node *lp;
int maxmatches = 500;
/* list all matching visible clients */
DLINK_FOREACH(lp, global_client_list.head)
{
target_p = lp->data;
if (!IsClient(target_p))
continue;
if (server_oper && !HasUMode(target_p, UMODE_OPER))
continue;
if (!mask ||
!match(mask, target_p->name) || !match(mask, target_p->username) ||
!match(mask, target_p->host) || !match(mask, target_p->servptr->name) ||
!match(mask, target_p->info) ||
(MyClient(target_p) && !match(mask, target_p->sockhost)))
{
if (dlink_list_length(&target_p->channel))
{
struct Channel *chptr;
static char fl[5];
chptr = ((struct Membership *)(target_p->channel.head->data))->chptr;
snprintf(fl, sizeof(fl), "%s",
get_member_status((struct Membership *)(target_p->channel.head->data), 0));
do_who(source_p, target_p, chptr->chname, fl);
}
else
do_who(source_p, target_p, NULL, "");
if (maxmatches > 0)
{
if (--maxmatches == 0)
return;
}
}
}
}
static void
do_who_on_channel(struct Client *source_p, struct Channel *chptr,
char *chname)
{
dlink_node *ptr;
struct Membership *ms;
DLINK_FOREACH(ptr, chptr->members.head)
{
ms = ptr->data;
do_who(source_p, ms->client_p, chname, get_member_status(ms, 0));
}
}
#ifdef OPERSPY_LOG
static void
operspy_log(struct Client *source_p, const char *command, const char *target)
{
struct MaskItem *conf = NULL;
#ifdef OPERSPY_LOGFILE
FILE *operspy_fb;
dlink_node *cnode;
const char *opername = source_p->name;
char linebuf[IRCD_BUFSIZE], logfile[IRCD_BUFSIZE];
#endif
assert(source_p != NULL);
#ifdef OPERSPY_LOGFILE
if (HasUMode(source_p, UMODE_OPER) && MyClient(source_p))
{
DLINK_FOREACH(cnode, source_p->localClient->confs.head)
{
conf = cnode->data;
if (conf->type == CONF_OPER)
opername = conf->name;
}
}
else if (!MyClient(source_p))
opername = "remote";
snprintf(logfile, sizeof(logfile), "%s/operspy.%s.log", LOGPATH, opername);
if ((operspy_fb = fopen(logfile, "a")) == NULL)
return;
snprintf(linebuf, sizeof(linebuf), "[%s] OPERSPY %s %s %s\n",
smalldate(CurrentTime),
get_oper_name(source_p),
command, target);
fputs(linebuf, operspy_fb);
fclose(operspy_fb);
#endif
#ifdef OPERSPY_NOTICE
sendto_realops_flags(UMODE_SPY, L_ALL, SEND_NOTICE, "OPERSPY %s %s %s",
get_oper_name(source_p), command, target);
#endif
if (MyClient(source_p))
sendto_match_servs(source_p, "*", CAP_ENCAP, "ENCAP * OPERSPY %s :%s",
command, target);
}
#endif /* OPERSPY_LOG */
static struct Message operspy_msgtab = {
"OPERSPY", 0, 0, 3, MAXPARA, MFLG_SLOW | MFLG_HIDDEN, 0,
{m_ignore, m_not_oper, ms_operspy, ms_operspy, mo_operspy, m_ignore}
};
static void
module_init(void)
{
mod_add_cmd(&operspy_msgtab);
}
static void
module_exit(void)
{
mod_del_cmd(&operspy_msgtab);
}
struct module module_entry = {
.node = { NULL, NULL, NULL },
.name = NULL,
.version = "$Revision$",
.handle = NULL,
.modinit = module_init,
.modexit = module_exit,
.flags = 0
};
|