0b4e4f93daed6282a1bc688e7e69a4e9116f728d
[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_get_default_umode
72  * returns default usermode in attached client connection class
73  */
74 const char* client_get_default_umode(const struct Client* sptr)
75 {
76   struct ConfItem* aconf;
77   struct SLink* link;
78
79   assert(cli_verify(sptr));
80
81   for (link = cli_confs(sptr); link; link = link->next) {
82     aconf = link->value.aconf;
83     if ((aconf->status & CONF_CLIENT) && ConfUmode(aconf))
84       return ConfUmode(aconf);
85   }
86   return NULL;
87 }
88
89 /*
90  * client_drop_sendq
91  * removes the client's connection from the list of connections with
92  * queued data
93  */
94 void client_drop_sendq(struct Connection* con)
95 {
96   if (con_prev_p(con)) { /* on the queued data list... */
97     if (con_next(con))
98       con_prev_p(con_next(con)) = con_prev_p(con);
99     *(con_prev_p(con)) = con_next(con);
100
101     con_next(con) = 0;
102     con_prev_p(con) = 0;
103   }
104 }
105
106 /*
107  * client_add_sendq
108  * adds the client's connection to the list of connections with
109  * queued data
110  */
111 void client_add_sendq(struct Connection* con, struct Connection** con_p)
112 {
113   if (!con_prev_p(con)) { /* not on the queued data list yet... */
114     con_prev_p(con) = con_p;
115     con_next(con) = *con_p;
116
117     if (*con_p)
118       con_prev_p(*con_p) = &(con_next(con));
119     *con_p = con;
120   }
121 }
122
123 enum FeatureFlag {
124   FEATFLAG_NULL,
125   FEATFLAG_DISABLES_PRIV,
126   FEATFLAG_ENABLES_PRIV,
127   FEATFLAG_GLOBAL_OPERS,
128   FEATFLAG_LOCAL_OPERS,
129   FEATFLAG_ALL_OPERS
130 };
131
132 static struct
133 {
134   enum Priv priv;
135   enum Feature feat;
136   enum FeatureFlag flag;
137 } feattab[] =
138   {
139     { PRIV_WHOX, FEAT_LAST_F, FEATFLAG_ALL_OPERS },
140     { PRIV_DISPLAY, FEAT_LAST_F, FEATFLAG_ALL_OPERS },
141     { PRIV_CHAN_LIMIT, FEAT_OPER_NO_CHAN_LIMIT, FEATFLAG_ALL_OPERS },
142     { PRIV_MODE_LCHAN, FEAT_OPER_MODE_LCHAN, FEATFLAG_ALL_OPERS },
143     { PRIV_LOCAL_OPMODE, FEAT_OPER_MODE_LCHAN, FEATFLAG_ALL_OPERS },
144     { PRIV_WALK_LCHAN, FEAT_OPER_WALK_THROUGH_LMODES, FEATFLAG_ALL_OPERS },
145     { PRIV_DEOP_LCHAN, FEAT_NO_OPER_DEOP_LCHAN, FEATFLAG_ALL_OPERS },
146     { PRIV_SHOW_INVIS, FEAT_SHOW_INVISIBLE_USERS, FEATFLAG_ALL_OPERS },
147     { PRIV_SHOW_ALL_INVIS, FEAT_SHOW_ALL_INVISIBLE_USERS, FEATFLAG_ALL_OPERS },
148     { PRIV_UNLIMIT_QUERY, FEAT_UNLIMIT_OPER_QUERY, FEATFLAG_ALL_OPERS },
149     
150     { PRIV_KILL, FEAT_LOCAL_KILL_ONLY, FEATFLAG_DISABLES_PRIV },
151     { PRIV_GLINE, FEAT_CONFIG_OPERCMDS, FEATFLAG_ENABLES_PRIV },
152     { PRIV_JUPE, FEAT_CONFIG_OPERCMDS, FEATFLAG_ENABLES_PRIV },
153     { PRIV_OPMODE, FEAT_CONFIG_OPERCMDS, FEATFLAG_ENABLES_PRIV },
154     { PRIV_BADCHAN, FEAT_CONFIG_OPERCMDS, FEATFLAG_ENABLES_PRIV },
155     
156     { PRIV_PROPAGATE, FEAT_LAST_F, FEATFLAG_GLOBAL_OPERS },
157     { PRIV_SEE_OPERS, FEAT_LAST_F, FEATFLAG_GLOBAL_OPERS },
158     { PRIV_KILL, FEAT_OPER_KILL, FEATFLAG_GLOBAL_OPERS },
159     { PRIV_LOCAL_KILL, FEAT_OPER_KILL, FEATFLAG_GLOBAL_OPERS },
160     { PRIV_REHASH, FEAT_OPER_REHASH, FEATFLAG_GLOBAL_OPERS },
161     { PRIV_RESTART, FEAT_OPER_RESTART, FEATFLAG_GLOBAL_OPERS },
162     { PRIV_DIE, FEAT_OPER_DIE, FEATFLAG_GLOBAL_OPERS },
163     { PRIV_GLINE, FEAT_OPER_GLINE, FEATFLAG_GLOBAL_OPERS },
164     { PRIV_LOCAL_GLINE, FEAT_OPER_LGLINE, FEATFLAG_GLOBAL_OPERS },
165     { PRIV_JUPE, FEAT_OPER_JUPE, FEATFLAG_GLOBAL_OPERS },
166     { PRIV_LOCAL_JUPE, FEAT_OPER_LJUPE, FEATFLAG_GLOBAL_OPERS },
167     { PRIV_OPMODE, FEAT_OPER_OPMODE, FEATFLAG_GLOBAL_OPERS },
168     { PRIV_LOCAL_OPMODE, FEAT_OPER_LOPMODE, FEATFLAG_GLOBAL_OPERS },
169     { PRIV_FORCE_OPMODE, FEAT_OPER_FORCE_OPMODE, FEATFLAG_GLOBAL_OPERS },
170     { PRIV_FORCE_LOCAL_OPMODE, FEAT_OPER_FORCE_LOPMODE, FEATFLAG_GLOBAL_OPERS },
171     { PRIV_BADCHAN, FEAT_OPER_BADCHAN, FEATFLAG_GLOBAL_OPERS },
172     { PRIV_LOCAL_BADCHAN, FEAT_OPER_LBADCHAN, FEATFLAG_GLOBAL_OPERS },
173     { PRIV_SET, FEAT_OPER_SET, FEATFLAG_GLOBAL_OPERS },
174     { PRIV_SEE_CHAN, FEAT_OPERS_SEE_IN_SECRET_CHANNELS, FEATFLAG_GLOBAL_OPERS },
175     { PRIV_WIDE_GLINE, FEAT_OPER_WIDE_GLINE, FEATFLAG_GLOBAL_OPERS },
176     
177     { PRIV_LOCAL_KILL, FEAT_LOCOP_KILL, FEATFLAG_LOCAL_OPERS },
178     { PRIV_REHASH, FEAT_LOCOP_REHASH, FEATFLAG_LOCAL_OPERS },
179     { PRIV_RESTART, FEAT_LOCOP_RESTART, FEATFLAG_LOCAL_OPERS },
180     { PRIV_DIE, FEAT_LOCOP_DIE, FEATFLAG_LOCAL_OPERS },
181     { PRIV_LOCAL_GLINE, FEAT_LOCOP_LGLINE, FEATFLAG_LOCAL_OPERS },
182     { PRIV_LOCAL_JUPE, FEAT_LOCOP_LJUPE, FEATFLAG_LOCAL_OPERS },
183     { PRIV_LOCAL_OPMODE, FEAT_LOCOP_LOPMODE, FEATFLAG_LOCAL_OPERS },
184     { PRIV_FORCE_LOCAL_OPMODE, FEAT_LOCOP_FORCE_LOPMODE, FEATFLAG_LOCAL_OPERS },
185     { PRIV_LOCAL_BADCHAN, FEAT_LOCOP_LBADCHAN, FEATFLAG_LOCAL_OPERS },
186     { PRIV_SET, FEAT_LOCOP_SET, FEATFLAG_LOCAL_OPERS },
187     { PRIV_SEE_CHAN, FEAT_LOCOP_SEE_IN_SECRET_CHANNELS, FEATFLAG_LOCAL_OPERS },
188     { PRIV_WIDE_GLINE, FEAT_LOCOP_WIDE_GLINE, FEATFLAG_LOCAL_OPERS },
189     
190     { PRIV_LAST_PRIV, FEAT_LAST_F, FEATFLAG_NULL }
191   };
192
193 /* client_set_privs(struct Client* client)
194  *
195  * Sets the privileges for opers.
196  */
197 void
198 client_set_privs(struct Client *client, struct ConfItem *oper)
199 {
200   int i;
201
202   memset(&(cli_privs(client)), 0, sizeof(struct Privs));
203
204   if (!IsAnOper(client))
205     return;
206   else if (!MyConnect(client))
207   {
208     memset(&(cli_privs(client)), 255, sizeof(struct Privs));
209     PrivClr(&(cli_privs(client)), PRIV_SET);
210     return;
211   }
212   else if (oper == NULL)
213     return;
214
215   /* Copy across privs from the config. */
216   cli_privs(client) = oper->privs;
217
218   /* Now go through the features and set the non-dirty flags to their feature
219    * determined defaults...
220    */
221   for (i = 0; feattab[i].priv != PRIV_LAST_PRIV; i++)
222   {
223     if (PrivHas(&oper->privs_dirty, feattab[i].priv))
224       continue;
225     if (feattab[i].feat != FEAT_LAST_F && !feature_bool(feattab[i].feat))
226       continue;
227     switch (feattab[i].flag)
228     {
229     case FEATFLAG_ENABLES_PRIV:
230       if (!feature_bool(feattab[i].feat))
231         continue;
232       PrivSet(&cli_privs(client), feattab[i].priv);
233       continue;
234     case FEATFLAG_DISABLES_PRIV:
235       PrivClr(&cli_privs(client), feattab[i].priv);
236       continue;
237     case FEATFLAG_ALL_OPERS:
238       if (IsAnOper(client))
239         PrivSet(&cli_privs(client), feattab[i].priv);
240       continue;
241     case FEATFLAG_GLOBAL_OPERS:
242       if (IsOper(client))
243         PrivSet(&cli_privs(client), feattab[i].priv);
244       continue;
245     case FEATFLAG_LOCAL_OPERS:
246       if (IsLocOp(client))
247         PrivSet(&cli_privs(client), feattab[i].priv);
248       continue;
249     default:
250       continue;  /* ?? */
251     }
252   }
253
254   /* This should be handled in the config, but lets be sure... */
255   if (PrivHas(&cli_privs(client), PRIV_PROPAGATE))
256     /* force propagating opers to display */
257     PrivSet(&cli_privs(client), PRIV_DISPLAY);
258   else
259   {
260     /* if they don't propagate oper status, prevent desyncs */
261     PrivClr(&cli_privs(client), PRIV_KILL);
262     PrivClr(&cli_privs(client), PRIV_GLINE);
263     PrivClr(&cli_privs(client), PRIV_JUPE);
264     PrivClr(&cli_privs(client), PRIV_OPMODE);
265     PrivClr(&cli_privs(client), PRIV_BADCHAN);
266   }
267 }
268
269 static struct {
270   char        *name;
271   unsigned int priv;
272 } privtab[] = {
273 #define P(priv)         { #priv, PRIV_ ## priv }
274   P(CHAN_LIMIT),     P(MODE_LCHAN),     P(WALK_LCHAN),    P(DEOP_LCHAN),
275   P(SHOW_INVIS),     P(SHOW_ALL_INVIS), P(UNLIMIT_QUERY), P(KILL),
276   P(LOCAL_KILL),     P(REHASH),         P(RESTART),       P(DIE),
277   P(GLINE),          P(LOCAL_GLINE),    P(JUPE),          P(LOCAL_JUPE),
278   P(OPMODE),         P(LOCAL_OPMODE),   P(SET),           P(WHOX),
279   P(BADCHAN),        P(LOCAL_BADCHAN),  P(SEE_CHAN),      P(PROPAGATE),
280   P(DISPLAY),        P(SEE_OPERS),      P(FORCE_OPMODE),  P(FORCE_LOCAL_OPMODE),
281   P(WIDE_GLINE),
282 #undef P
283   { 0, 0 }
284 };
285
286 /* client_report_privs(struct Client *to, struct Client *client)
287  *
288  * Sends a summary of the oper's privileges to the oper.
289  */
290 int
291 client_report_privs(struct Client *to, struct Client *client)
292 {
293   struct MsgBuf *mb;
294   int found1 = 0;
295   int i;
296
297   mb = msgq_make(to, rpl_str(RPL_PRIVS), cli_name(&me), cli_name(to),
298                  cli_name(client));
299
300   for (i = 0; privtab[i].name; i++)
301     if (HasPriv(client, privtab[i].priv))
302       msgq_append(0, mb, "%s%s", found1++ ? " " : "", privtab[i].name);
303
304   send_buffer(to, mb, 0); /* send response */
305   msgq_clean(mb);
306
307   return 0;
308 }