Only allow current opers to WHOX.
[ircu2.10.12-pk.git] / ircd / m_who.c
1 /*
2  * IRC - Internet Relay Chat, ircd/m_who.c
3  * Copyright (C) 1990 Jarkko Oikarinen and
4  *                    University of Oulu, Computing Center
5  *
6  * See file AUTHORS in IRC package for additional names of
7  * the programmers.
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 1, or (at your option)
12  * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  * $Id$
24  */
25
26 /*
27  * m_functions execute protocol messages on this server:
28  *
29  *    cptr    is always NON-NULL, pointing to a *LOCAL* client
30  *            structure (with an open socket connected!). This
31  *            identifies the physical socket where the message
32  *            originated (or which caused the m_function to be
33  *            executed--some m_functions may call others...).
34  *
35  *    sptr    is the source of the message, defined by the
36  *            prefix part of the message if present. If not
37  *            or prefix not found, then sptr==cptr.
38  *
39  *            (!IsServer(cptr)) => (cptr == sptr), because
40  *            prefixes are taken *only* from servers...
41  *
42  *            (IsServer(cptr))
43  *                    (sptr == cptr) => the message didn't
44  *                    have the prefix.
45  *
46  *                    (sptr != cptr && IsServer(sptr) means
47  *                    the prefix specified servername. (?)
48  *
49  *                    (sptr != cptr && !IsServer(sptr) means
50  *                    that message originated from a remote
51  *                    user (not local).
52  *
53  *            combining
54  *
55  *            (!IsServer(sptr)) means that, sptr can safely
56  *            taken as defining the target structure of the
57  *            message in this server.
58  *
59  *    *Always* true (if 'parse' and others are working correct):
60  *
61  *    1)      sptr->from == cptr  (note: cptr->from == cptr)
62  *
63  *    2)      MyConnect(sptr) <=> sptr == cptr (e.g. sptr
64  *            *cannot* be a local connection, unless it's
65  *            actually cptr!). [MyConnect(x) should probably
66  *            be defined as (x == x->from) --msa ]
67  *
68  *    parc    number of variable parameter strings (if zero,
69  *            parv is allowed to be NULL)
70  *
71  *    parv    a NULL terminated list of parameter pointers,
72  *
73  *                    parv[0], sender (prefix string), if not present
74  *                            this points to an empty string.
75  *                    parv[1]...parv[parc-1]
76  *                            pointers to additional parameters
77  *                    parv[parc] == NULL, *always*
78  *
79  *            note:   it is guaranteed that parv[0]..parv[parc-1] are all
80  *                    non-NULL pointers.
81  */
82 #include "config.h"
83
84 #include "channel.h"
85 #include "client.h"
86 #include "hash.h"
87 #include "ircd.h"
88 #include "ircd_chattr.h"
89 #include "ircd_features.h"
90 #include "ircd_log.h"
91 #include "ircd_reply.h"
92 #include "ircd_string.h"
93 #include "match.h"
94 #include "numeric.h"
95 #include "numnicks.h"
96 #include "send.h"
97 #include "whocmds.h"
98
99 /* #include <assert.h> -- Now using assert in ircd_log.h */
100 #include <string.h>
101
102
103 /*
104  * A little spin-marking utility to tell us which clients we have already
105  * processed and which not
106  */
107 static int who_marker = 0;
108 static void move_marker(void)
109 {
110   if (!++who_marker)
111   {
112     struct Client *cptr = GlobalClientList;
113     while (cptr)
114     {
115       cli_marker(cptr) = 0;
116       cptr = cli_next(cptr);
117     }
118     who_marker++;
119   }
120 }
121
122 #define CheckMark(x, y) ((x == y) ? 0 : (x = y))
123 #define Process(cptr) CheckMark(cli_marker(cptr), who_marker)
124
125 /*
126  * m_who - generic message handler
127  *
128  *  parv[0] = sender prefix
129  *  parv[1] = nickname mask list
130  *  parv[2] = additional selection flag, only 'o' for now.
131  *            and %flags to specify what fields to output
132  *            plus a ,querytype if the t flag is specified
133  *            so the final thing will be like o%tnchu,777
134  *  parv[3] = _optional_ parameter that overrides parv[1]
135  *            This can be used as "/quote who foo % :The Black Hacker
136  *            to find me, parv[3] _can_ contain spaces !.
137  */
138 int m_who(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
139 {
140   char *mask;           /* The mask we are looking for              */
141   char ch;                      /* Scratch char register                    */
142   struct Channel *chptr;                /* Channel to show                          */
143   struct Client *acptr;         /* Client to show                           */
144
145   int bitsel;                   /* Mask of selectors to apply               */
146   int matchsel;                 /* Which fields the match should apply on    */
147   int counter;                  /* Query size counter,
148                                    initially used to count fields           */
149   int commas;                   /* Does our mask contain any comma ?
150                                    If so is a list..                        */
151   int fields;                   /* Mask of fields to show                   */
152   int isthere = 0;              /* When this set the user is member of chptr */
153   char *nick;                   /* Single element extracted from
154                                    the mask list                            */
155   char *p;                      /* Scratch char pointer                     */
156   char *qrt;                    /* Pointer to the query type                */
157   static char mymask[512];      /* To save the mask before corrupting it    */
158
159   /* Let's find where is our mask, and if actually contains something */
160   mask = ((parc > 1) ? parv[1] : 0);
161   if (parc > 3 && parv[3])
162     mask = parv[3];
163   if (mask && ((mask[0] == '\0') ||
164       (mask[1] == '\0' && ((mask[0] == '0') || (mask[0] == '*')))))
165     mask = 0;
166
167   /* Evaluate the flags now, we consider the second parameter 
168      as "matchFlags%fieldsToInclude,querytype"           */
169   bitsel = fields = counter = matchsel = 0;
170   qrt = 0;
171   if (parc > 2 && parv[2] && *parv[2])
172   {
173     p = parv[2];
174     while (((ch = *(p++))) && (ch != '%') && (ch != ','))
175       switch (ch)
176       {
177         case 'd':
178         case 'D':
179           bitsel |= WHOSELECT_DELAY;
180           continue;
181         case 'o':
182         case 'O':
183           bitsel |= WHOSELECT_OPER;
184           continue;
185         case 'x':
186         case 'X':
187           if (HasPriv(sptr, PRIV_WHOX) && IsAnOper(sptr)) {
188               bitsel |= WHOSELECT_EXTRA;
189               log_write(LS_WHO, L_INFO, LOG_NOSNOTICE, "%#C WHO %s %s", sptr,
190                         (BadPtr(parv[3]) ? parv[1] : parv[3]), parv[2]);
191           }
192           continue;
193         case 'n':
194         case 'N':
195           matchsel |= WHO_FIELD_NIC;
196           continue;
197         case 'u':
198         case 'U':
199           matchsel |= WHO_FIELD_UID;
200           continue;
201         case 'h':
202         case 'H':
203           matchsel |= WHO_FIELD_HOS;
204           continue;
205         case 'i':
206         case 'I':
207           matchsel |= WHO_FIELD_NIP;
208           continue;
209         case 's':
210         case 'S':
211           matchsel |= WHO_FIELD_SER;
212           continue;
213         case 'r':
214         case 'R':
215           matchsel |= WHO_FIELD_REN;
216           continue;
217         case 'a':
218         case 'A':
219           matchsel |= WHO_FIELD_ACC;
220           continue;
221       }
222     if (ch == '%')
223       while ((ch = *p++) && (ch != ','))
224       {
225         counter++;
226         switch (ch)
227         {
228           case 'c':
229           case 'C':
230             fields |= WHO_FIELD_CHA;
231             break;
232           case 'd':
233           case 'D':
234             fields |= WHO_FIELD_DIS;
235             break;
236           case 'f':
237           case 'F':
238             fields |= WHO_FIELD_FLA;
239             break;
240           case 'h':
241           case 'H':
242             fields |= WHO_FIELD_HOS;
243             break;
244           case 'i':
245           case 'I':
246             fields |= WHO_FIELD_NIP;
247             break;
248           case 'l':
249           case 'L':
250             fields |= WHO_FIELD_IDL;
251           case 'n':
252           case 'N':
253             fields |= WHO_FIELD_NIC;
254             break;
255           case 'r':
256           case 'R':
257             fields |= WHO_FIELD_REN;
258             break;
259           case 's':
260           case 'S':
261             fields |= WHO_FIELD_SER;
262             break;
263           case 't':
264           case 'T':
265             fields |= WHO_FIELD_QTY;
266             break;
267           case 'u':
268           case 'U':
269             fields |= WHO_FIELD_UID;
270             break;
271           case 'a':
272           case 'A':
273             fields |= WHO_FIELD_ACC;
274             break;
275           case 'o':
276           case 'O':
277             fields |= WHO_FIELD_OPL;
278             break;
279           default:
280             break;
281         }
282       };
283     if (ch)
284       qrt = p;
285   }
286
287   if (!matchsel)
288     matchsel = WHO_FIELD_DEF;
289   if (!fields)
290     counter = 7;
291
292   if (feature_bool(FEAT_HIS_WHO_SERVERNAME) && !IsAnOper(sptr))
293     matchsel &= ~WHO_FIELD_SER;
294
295   if (qrt && (fields & WHO_FIELD_QTY))
296   {
297     p = qrt;
298     if (!((*p > '9') || (*p < '0')))
299       p++;
300     if (!((*p > '9') || (*p < '0')))
301       p++;
302     if (!((*p > '9') || (*p < '0')))
303       p++;
304     *p = '\0';
305   }
306   else
307     qrt = 0;
308
309   /* I'd love to add also a check on the number of matches fields per time */
310   counter = (2048 / (counter + 4));
311   if (mask && (strlen(mask) > 510))
312     mask[510] = '\0';
313   move_marker();
314   commas = (mask && strchr(mask, ','));
315
316   /* First treat mask as a list of plain nicks/channels */
317   if (mask)
318   {
319     strcpy(mymask, mask);
320     for (p = 0, nick = ircd_strtok(&p, mymask, ","); nick;
321         nick = ircd_strtok(&p, 0, ","))
322     {
323       if (IsChannelName(nick) && (chptr = FindChannel(nick)))
324       {
325         isthere = (find_channel_member(sptr, chptr) != 0);
326         if (isthere || SEE_CHANNEL(sptr, chptr, bitsel))
327         {
328           struct Membership* member;
329           for (member = chptr->members; member; member = member->next_member)
330           {
331             acptr = member->user;
332             if ((bitsel & WHOSELECT_OPER) && !SeeOper(sptr,acptr))
333               continue;
334             if ((acptr != sptr)
335                 && ((member->status & CHFL_ZOMBIE)
336                     || ((member->status & CHFL_DELAYED)
337                         && !(bitsel & WHOSELECT_DELAY))))
338               continue;
339             if (!(isthere || (SEE_USER(sptr, acptr, bitsel))))
340               continue;
341             if (!Process(acptr))        /* This can't be moved before other checks */
342               continue;
343             if (!(isthere || (SHOW_MORE(sptr, counter))))
344               break;
345             do_who(sptr, acptr, chptr, fields, qrt);
346           }
347         }
348       }
349       else
350       {
351         if ((acptr = FindUser(nick)) &&
352             ((!(bitsel & WHOSELECT_OPER)) || SeeOper(sptr,acptr)) &&
353             Process(acptr) && SHOW_MORE(sptr, counter))
354         {
355           do_who(sptr, acptr, 0, fields, qrt);
356         }
357       }
358     }
359   }
360
361   /* If we didn't have any comma in the mask treat it as a
362      real mask and try to match all relevant fields */
363   if (!(commas || (counter < 1)))
364   {
365     struct irc_in_addr imask;
366     int minlen, cset;
367     unsigned char ibits;
368
369     if (mask)
370     {
371       matchcomp(mymask, &minlen, &cset, mask);
372       if (!ipmask_parse(mask, &imask, &ibits))
373         matchsel &= ~WHO_FIELD_NIP;
374       if ((minlen > NICKLEN) || !(cset & NTL_IRCNK))
375         matchsel &= ~WHO_FIELD_NIC;
376       if ((matchsel & WHO_FIELD_SER) &&
377           ((minlen > HOSTLEN) || (!(cset & NTL_IRCHN))
378           || (!markMatchexServer(mymask, minlen))))
379         matchsel &= ~WHO_FIELD_SER;
380       if ((minlen > USERLEN) || !(cset & NTL_IRCUI))
381         matchsel &= ~WHO_FIELD_UID;
382       if ((minlen > HOSTLEN) || !(cset & NTL_IRCHN))
383         matchsel &= ~WHO_FIELD_HOS;
384       if ((minlen > ACCOUNTLEN))
385         matchsel &= ~WHO_FIELD_ACC;
386     }
387
388     /* First of all loop through the clients in common channels */
389     if ((!(counter < 1)) && matchsel) {
390       struct Membership* member;
391       struct Membership* chan;
392       for (chan = cli_user(sptr)->channel; chan; chan = chan->next_channel) {
393         chptr = chan->channel;
394         for (member = chptr->members; member; member = member->next_member)
395         {
396           acptr = member->user;
397           if (!(IsUser(acptr) && Process(acptr)))
398             continue;           /* Now Process() is at the beginning, if we fail
399                                    we'll never have to show this acptr in this query */
400           if ((bitsel & WHOSELECT_OPER) && !SeeOper(sptr,acptr))
401             continue;
402           if ((mask) &&
403               ((!(matchsel & WHO_FIELD_NIC))
404               || matchexec(cli_name(acptr), mymask, minlen))
405               && ((!(matchsel & WHO_FIELD_UID))
406               || matchexec(cli_user(acptr)->username, mymask, minlen))
407               && ((!(matchsel & WHO_FIELD_SER))
408               || (!(HasFlag(cli_user(acptr)->server, FLAG_MAP))))
409               && ((!(matchsel & WHO_FIELD_HOS))
410               || matchexec(cli_user(acptr)->host, mymask, minlen))
411               && ((!(matchsel & WHO_FIELD_HOS))
412               || !HasHiddenHost(acptr)
413               || !IsAnOper(sptr)
414               || matchexec(cli_user(acptr)->realhost, mymask, minlen))
415               && ((!(matchsel & WHO_FIELD_REN))
416               || matchexec(cli_info(acptr), mymask, minlen))
417               && ((!(matchsel & WHO_FIELD_NIP))
418               || (HasHiddenHost(acptr) && !IsAnOper(sptr))
419               || !ipmask_check(&cli_ip(acptr), &imask, ibits))
420               && ((!(matchsel & WHO_FIELD_ACC))
421               || matchexec(cli_user(acptr)->account, mymask, minlen)))
422             continue;
423           if (!SHOW_MORE(sptr, counter))
424             break;
425           do_who(sptr, acptr, chptr, fields, qrt);
426         }
427       }
428     }
429     /* Loop through all clients :-\, if we still have something to match to 
430        and we can show more clients */
431     if ((!(counter < 1)) && matchsel)
432       for (acptr = cli_prev(&me); acptr; acptr = cli_prev(acptr))
433       {
434         if (!(IsUser(acptr) && Process(acptr)))
435           continue;
436         if ((bitsel & WHOSELECT_OPER) && !SeeOper(sptr,acptr))
437           continue;
438         if (!(SEE_USER(sptr, acptr, bitsel)))
439           continue;
440         if ((mask) &&
441             ((!(matchsel & WHO_FIELD_NIC))
442             || matchexec(cli_name(acptr), mymask, minlen))
443             && ((!(matchsel & WHO_FIELD_UID))
444             || matchexec(cli_user(acptr)->username, mymask, minlen))
445             && ((!(matchsel & WHO_FIELD_SER))
446                 || (!(HasFlag(cli_user(acptr)->server, FLAG_MAP))))
447             && ((!(matchsel & WHO_FIELD_HOS))
448             || matchexec(cli_user(acptr)->host, mymask, minlen))
449             && ((!(matchsel & WHO_FIELD_HOS))
450             || !HasHiddenHost(acptr)
451             || !IsAnOper(sptr)
452             || matchexec(cli_user(acptr)->realhost, mymask, minlen))
453             && ((!(matchsel & WHO_FIELD_REN))
454             || matchexec(cli_info(acptr), mymask, minlen))
455             && ((!(matchsel & WHO_FIELD_NIP))
456             || (HasHiddenHost(acptr) && !IsAnOper(sptr))
457             || !ipmask_check(&cli_ip(acptr), &imask, ibits))
458             && ((!(matchsel & WHO_FIELD_ACC))
459             || matchexec(cli_user(acptr)->account, mymask, minlen)))
460           continue;
461         if (!SHOW_MORE(sptr, counter))
462           break;
463         do_who(sptr, acptr, 0, fields, qrt);
464       }
465   }
466
467   /* Make a clean mask suitable to be sent in the "end of" */
468   if (mask && (p = strchr(mask, ' ')))
469     *p = '\0';
470   /* Notify the user if we decided that his query was too long */
471   if (counter < 0)
472     send_reply(sptr, ERR_QUERYTOOLONG, BadPtr(mask) ? "*" : mask);
473   send_reply(sptr, RPL_ENDOFWHO, BadPtr(mask) ? "*" : mask);
474
475   return 0;
476 }