e12546651a6caf0b185355bb27729456e9117906
[ircu2.10.12-pk.git] / ircd / client.c
1 /*
2  * IRC - Internet Relay Chat, ircd/client.c
3  * Copyright (C) 1990 Darren Reed
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 1, or (at your option)
8  * any later version.
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  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  * $Id$
20  */
21 #include "config.h"
22
23 #include "client.h"
24 #include "class.h"
25 #include "ircd.h"
26 #include "ircd_features.h"
27 #include "ircd_reply.h"
28 #include "list.h"
29 #include "msgq.h"
30 #include "numeric.h"
31 #include "s_conf.h"
32 #include "s_debug.h"
33 #include "send.h"
34 #include "struct.h"
35
36 #include <assert.h>
37 #include <string.h>
38
39 #define BAD_PING                ((unsigned int)-2)
40
41 /*
42  * client_get_ping
43  * returns shortest ping time in attached server or client conf
44  * classes or PINGFREQUENCY
45  */
46 int client_get_ping(const struct Client* acptr)
47 {
48   int     ping = 0;
49   struct ConfItem* aconf;
50   struct SLink*    link;
51
52   assert(cli_verify(acptr));
53
54   for (link = cli_confs(acptr); link; link = link->next) {
55     aconf = link->value.aconf;
56     if (aconf->status & (CONF_CLIENT | CONF_SERVER)) {
57       int tmp = get_conf_ping(aconf);
58       if (0 < tmp && (ping > tmp || !ping))
59         ping = tmp;
60     }
61   }
62   if (0 == ping)
63     ping = feature_int(FEAT_PINGFREQUENCY);
64
65   Debug((DEBUG_DEBUG, "Client %s Ping %d", cli_name(acptr), ping));
66
67   return ping;
68 }
69
70 /*
71  * client_drop_sendq
72  * removes the client's connection from the list of connections with
73  * queued data
74  */
75 void client_drop_sendq(struct Connection* con)
76 {
77   if (con_prev_p(con)) { /* on the queued data list... */
78     if (con_next(con))
79       con_prev_p(con_next(con)) = con_prev_p(con);
80     *(con_prev_p(con)) = con_next(con);
81
82     con_next(con) = 0;
83     con_prev_p(con) = 0;
84   }
85 }
86
87 /*
88  * client_add_sendq
89  * adds the client's connection to the list of connections with
90  * queued data
91  */
92 void client_add_sendq(struct Connection* con, struct Connection** con_p)
93 {
94   if (!con_prev_p(con)) { /* not on the queued data list yet... */
95     con_prev_p(con) = con_p;
96     con_next(con) = *con_p;
97
98     if (*con_p)
99       con_prev_p(*con_p) = &(con_next(con));
100     *con_p = con;
101   }
102 }
103
104 enum FeatureFlag {
105   FEATFLAG_NULL,
106   FEATFLAG_DISABLES_PRIV,
107   FEATFLAG_ENABLES_PRIV,
108   FEATFLAG_GLOBAL_OPERS,
109   FEATFLAG_LOCAL_OPERS,
110   FEATFLAG_ALL_OPERS
111 };
112
113 static struct
114 {
115   enum Priv priv;
116   enum Feature feat;
117   enum FeatureFlag flag;
118 } feattab[] =
119   {
120     { PRIV_WHOX, FEAT_LAST_F, FEATFLAG_ALL_OPERS },
121     { PRIV_DISPLAY, FEAT_LAST_F, FEATFLAG_ALL_OPERS },
122     { PRIV_CHAN_LIMIT, FEAT_OPER_NO_CHAN_LIMIT, FEATFLAG_ALL_OPERS },
123     { PRIV_MODE_LCHAN, FEAT_OPER_MODE_LCHAN, FEATFLAG_ALL_OPERS },
124     { PRIV_LOCAL_OPMODE, FEAT_OPER_MODE_LCHAN, FEATFLAG_ALL_OPERS },
125     { PRIV_WALK_LCHAN, FEAT_OPER_WALK_THROUGH_LMODES, FEATFLAG_ALL_OPERS },
126     { PRIV_DEOP_LCHAN, FEAT_NO_OPER_DEOP_LCHAN, FEATFLAG_ALL_OPERS },
127     { PRIV_SHOW_INVIS, FEAT_SHOW_INVISIBLE_USERS, FEATFLAG_ALL_OPERS },
128     { PRIV_SHOW_ALL_INVIS, FEAT_SHOW_ALL_INVISIBLE_USERS, FEATFLAG_ALL_OPERS },
129     { PRIV_UNLIMIT_QUERY, FEAT_UNLIMIT_OPER_QUERY, FEATFLAG_ALL_OPERS },
130     
131     { PRIV_KILL, FEAT_LOCAL_KILL_ONLY, FEATFLAG_DISABLES_PRIV },
132     { PRIV_GLINE, FEAT_CONFIG_OPERCMDS, FEATFLAG_ENABLES_PRIV },
133     { PRIV_JUPE, FEAT_CONFIG_OPERCMDS, FEATFLAG_ENABLES_PRIV },
134     { PRIV_OPMODE, FEAT_CONFIG_OPERCMDS, FEATFLAG_ENABLES_PRIV },
135     { PRIV_BADCHAN, FEAT_CONFIG_OPERCMDS, FEATFLAG_ENABLES_PRIV },
136     
137     { PRIV_PROPAGATE, FEAT_LAST_F, FEATFLAG_GLOBAL_OPERS },
138     { PRIV_SEE_OPERS, FEAT_LAST_F, FEATFLAG_GLOBAL_OPERS },
139     { PRIV_KILL, FEAT_OPER_KILL, FEATFLAG_GLOBAL_OPERS },
140     { PRIV_LOCAL_KILL, FEAT_OPER_KILL, FEATFLAG_GLOBAL_OPERS },
141     { PRIV_REHASH, FEAT_OPER_REHASH, FEATFLAG_GLOBAL_OPERS },
142     { PRIV_RESTART, FEAT_OPER_RESTART, FEATFLAG_GLOBAL_OPERS },
143     { PRIV_DIE, FEAT_OPER_DIE, FEATFLAG_GLOBAL_OPERS },
144     { PRIV_GLINE, FEAT_OPER_GLINE, FEATFLAG_GLOBAL_OPERS },
145     { PRIV_LOCAL_GLINE, FEAT_OPER_LGLINE, FEATFLAG_GLOBAL_OPERS },
146     { PRIV_JUPE, FEAT_OPER_JUPE, FEATFLAG_GLOBAL_OPERS },
147     { PRIV_LOCAL_JUPE, FEAT_OPER_LJUPE, FEATFLAG_GLOBAL_OPERS },
148     { PRIV_OPMODE, FEAT_OPER_OPMODE, FEATFLAG_GLOBAL_OPERS },
149     { PRIV_LOCAL_OPMODE, FEAT_OPER_LOPMODE, FEATFLAG_GLOBAL_OPERS },
150     { PRIV_FORCE_OPMODE, FEAT_OPER_FORCE_OPMODE, FEATFLAG_GLOBAL_OPERS },
151     { PRIV_FORCE_LOCAL_OPMODE, FEAT_OPER_FORCE_LOPMODE, FEATFLAG_GLOBAL_OPERS },
152     { PRIV_BADCHAN, FEAT_OPER_BADCHAN, FEATFLAG_GLOBAL_OPERS },
153     { PRIV_LOCAL_BADCHAN, FEAT_OPER_LBADCHAN, FEATFLAG_GLOBAL_OPERS },
154     { PRIV_SET, FEAT_OPER_SET, FEATFLAG_GLOBAL_OPERS },
155     { PRIV_SEE_CHAN, FEAT_OPERS_SEE_IN_SECRET_CHANNELS, FEATFLAG_GLOBAL_OPERS },
156     { PRIV_WIDE_GLINE, FEAT_OPER_WIDE_GLINE, FEATFLAG_GLOBAL_OPERS },
157     
158     { PRIV_LOCAL_KILL, FEAT_LOCOP_KILL, FEATFLAG_LOCAL_OPERS },
159     { PRIV_REHASH, FEAT_LOCOP_REHASH, FEATFLAG_LOCAL_OPERS },
160     { PRIV_RESTART, FEAT_LOCOP_RESTART, FEATFLAG_LOCAL_OPERS },
161     { PRIV_DIE, FEAT_LOCOP_DIE, FEATFLAG_LOCAL_OPERS },
162     { PRIV_LOCAL_GLINE, FEAT_LOCOP_LGLINE, FEATFLAG_LOCAL_OPERS },
163     { PRIV_LOCAL_JUPE, FEAT_LOCOP_LJUPE, FEATFLAG_LOCAL_OPERS },
164     { PRIV_LOCAL_OPMODE, FEAT_LOCOP_LOPMODE, FEATFLAG_LOCAL_OPERS },
165     { PRIV_FORCE_LOCAL_OPMODE, FEAT_LOCOP_FORCE_LOPMODE, FEATFLAG_LOCAL_OPERS },
166     { PRIV_LOCAL_BADCHAN, FEAT_LOCOP_LBADCHAN, FEATFLAG_LOCAL_OPERS },
167     { PRIV_SET, FEAT_LOCOP_SET, FEATFLAG_LOCAL_OPERS },
168     { PRIV_SEE_CHAN, FEAT_LOCOP_SEE_IN_SECRET_CHANNELS, FEATFLAG_LOCAL_OPERS },
169     { PRIV_WIDE_GLINE, FEAT_LOCOP_WIDE_GLINE, FEATFLAG_LOCAL_OPERS },
170     
171     { PRIV_LAST_PRIV, FEAT_LAST_F, FEATFLAG_NULL }
172   };
173
174 /* client_set_privs(struct Client* client)
175  *
176  * Sets the privileges for opers.
177  */
178 void
179 client_set_privs(struct Client *client, struct ConfItem *oper)
180 {
181   int i;
182
183   memset(&(cli_privs(client)), 0, sizeof(struct Privs));
184
185   if (!IsAnOper(client))
186     return;
187   else if (!MyConnect(client))
188   {
189     memset(&(cli_privs(client)), 255, sizeof(struct Privs));
190     PrivClr(&(cli_privs(client)), PRIV_SET);
191     return;
192   }
193   else if (oper == NULL)
194     return;
195
196   /* Copy across privs from the config. */
197   cli_privs(client) = oper->privs;
198
199   /* Now go through the features and set the non-dirty flags to their feature
200    * determined defaults...
201    */
202   for (i = 0; feattab[i].priv != PRIV_LAST_PRIV; i++)
203   {
204     if (PrivHas(&oper->privs_dirty, feattab[i].priv))
205       continue;
206     if (feattab[i].feat != FEAT_LAST_F && !feature_bool(feattab[i].feat))
207       continue;
208     switch (feattab[i].flag)
209     {
210     case FEATFLAG_ENABLES_PRIV:
211       if (!feature_bool(feattab[i].feat))
212         continue;
213       PrivSet(&cli_privs(client), feattab[i].priv);
214       continue;
215     case FEATFLAG_DISABLES_PRIV:
216       PrivClr(&cli_privs(client), feattab[i].priv);
217       continue;
218     case FEATFLAG_ALL_OPERS:
219       if (IsAnOper(client))
220         PrivSet(&cli_privs(client), feattab[i].priv);
221       continue;
222     case FEATFLAG_GLOBAL_OPERS:
223       if (IsOper(client))
224         PrivSet(&cli_privs(client), feattab[i].priv);
225       continue;
226     case FEATFLAG_LOCAL_OPERS:
227       if (IsLocOp(client))
228         PrivSet(&cli_privs(client), feattab[i].priv);
229       continue;
230     default:
231       continue;  /* ?? */
232     }
233   }
234
235   /* This should be handled in the config, but lets be sure... */
236   if (PrivHas(&cli_privs(client), PRIV_PROPAGATE))
237     /* force propagating opers to display */
238     PrivSet(&cli_privs(client), PRIV_DISPLAY);
239   else
240   {
241     /* if they don't propagate oper status, prevent desyncs */
242     PrivClr(&cli_privs(client), PRIV_KILL);
243     PrivClr(&cli_privs(client), PRIV_GLINE);
244     PrivClr(&cli_privs(client), PRIV_JUPE);
245     PrivClr(&cli_privs(client), PRIV_OPMODE);
246     PrivClr(&cli_privs(client), PRIV_BADCHAN);
247   }
248 }
249
250 static struct {
251   char        *name;
252   unsigned int priv;
253 } privtab[] = {
254 #define P(priv)         { #priv, PRIV_ ## priv }
255   P(CHAN_LIMIT),     P(MODE_LCHAN),     P(WALK_LCHAN),    P(DEOP_LCHAN),
256   P(SHOW_INVIS),     P(SHOW_ALL_INVIS), P(UNLIMIT_QUERY), P(KILL),
257   P(LOCAL_KILL),     P(REHASH),         P(RESTART),       P(DIE),
258   P(GLINE),          P(LOCAL_GLINE),    P(JUPE),          P(LOCAL_JUPE),
259   P(OPMODE),         P(LOCAL_OPMODE),   P(SET),           P(WHOX),
260   P(BADCHAN),        P(LOCAL_BADCHAN),  P(SEE_CHAN),      P(PROPAGATE),
261   P(DISPLAY),        P(SEE_OPERS),      P(FORCE_OPMODE),  P(FORCE_LOCAL_OPMODE),
262   P(WIDE_GLINE),
263 #undef P
264   { 0, 0 }
265 };
266
267 /* client_report_privs(struct Client *to, struct Client *client)
268  *
269  * Sends a summary of the oper's privileges to the oper.
270  */
271 int
272 client_report_privs(struct Client *to, struct Client *client)
273 {
274   struct MsgBuf *mb;
275   int found1 = 0;
276   int i;
277
278   mb = msgq_make(to, rpl_str(RPL_PRIVS), cli_name(&me), cli_name(to),
279                  cli_name(client));
280
281   for (i = 0; privtab[i].name; i++)
282     if (HasPriv(client, privtab[i].priv))
283       msgq_append(0, mb, "%s%s", found1++ ? " " : "", privtab[i].name);
284
285   send_buffer(to, mb, 0); /* send response */
286   msgq_clean(mb);
287
288   return 0;
289 }