ircu2.10.12 pk910 fork
[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 /** @file
20  * @brief Implementation of functions for handling local clients.
21  * @version $Id: client.c 1523 2005-10-12 23:52:12Z entrope $
22  */
23 #include "config.h"
24
25 #include "client.h"
26 #include "class.h"
27 #include "ircd.h"
28 #include "ircd_features.h"
29 #include "ircd_log.h"
30 #include "ircd_reply.h"
31 #include "list.h"
32 #include "msgq.h"
33 #include "numeric.h"
34 #include "s_conf.h"
35 #include "s_debug.h"
36 #include "send.h"
37 #include "struct.h"
38
39 /* #include <assert.h> -- Now using assert in ircd_log.h */
40 #include <string.h>
41
42 /** Find the shortest non-zero ping time attached to a client.
43  * If all attached ping times are zero, return the value for
44  * FEAT_PINGFREQUENCY.
45  * @param[in] acptr Client to find ping time for.
46  * @return Ping time in seconds.
47  */
48 int client_get_ping(const struct Client* acptr)
49 {
50   int     ping = 0;
51   struct ConfItem* aconf;
52   struct SLink*    link;
53
54   assert(cli_verify(acptr));
55
56   for (link = cli_confs(acptr); link; link = link->next) {
57     aconf = link->value.aconf;
58     if (aconf->status & (CONF_CLIENT | CONF_SERVER)) {
59       int tmp = get_conf_ping(aconf);
60       if (0 < tmp && (ping > tmp || !ping))
61         ping = tmp;
62     }
63   }
64   if (0 == ping)
65     ping = feature_int(FEAT_PINGFREQUENCY);
66
67   Debug((DEBUG_DEBUG, "Client %s Ping %d", cli_name(acptr), ping));
68
69   return ping;
70 }
71
72 /** Find the default usermode for a client.
73  * @param[in] sptr Client to find default usermode for.
74  * @return Pointer to usermode string (or NULL, if there is no default).
75  */
76 const char* client_get_default_umode(const struct Client* sptr)
77 {
78   struct ConfItem* aconf;
79   struct SLink* link;
80
81   assert(cli_verify(sptr));
82
83   for (link = cli_confs(sptr); link; link = link->next) {
84     aconf = link->value.aconf;
85     if ((aconf->status & CONF_CLIENT) && ConfUmode(aconf))
86       return ConfUmode(aconf);
87   }
88   return NULL;
89 }
90
91 /** Remove a connection from the list of connections with queued data.
92  * @param[in] con Connection with no 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 /** Add a connection to the list of connections with queued data.
107  * @param[in] con Connection with queued data.
108  * @param[in,out] con_p Previous pointer to next connection.
109  */
110 void client_add_sendq(struct Connection* con, struct Connection** con_p)
111 {
112   if (!con_prev_p(con)) { /* not on the queued data list yet... */
113     con_prev_p(con) = con_p;
114     con_next(con) = *con_p;
115
116     if (*con_p)
117       con_prev_p(*con_p) = &(con_next(con));
118     *con_p = con;
119   }
120 }
121
122 /** Default privilege set for global operators. */
123 static struct Privs privs_global;
124 /** Default privilege set for local operators. */
125 static struct Privs privs_local;
126 /** Non-zero if #privs_global and #privs_local have been initialized. */
127 static int privs_defaults_set;
128
129 /* client_set_privs(struct Client* client)
130  *
131  * Sets the privileges for opers.
132  */
133 /** Set the privileges for a client.
134  * @param[in] client Client who has become an operator.
135  * @param[in] oper Configuration item describing oper's privileges.
136  */
137 void
138 client_set_privs(struct Client *client, struct ConfItem *oper)
139 {
140   struct Privs *source, *defaults;
141   enum Priv priv;
142
143   if (!MyConnect(client))
144     return;
145
146   /* Clear out client's privileges. */
147   memset(cli_privs(client), 0, sizeof(struct Privs));
148
149   if (!IsAnOper(client) || !oper)
150       return;
151
152   if (!privs_defaults_set)
153   {
154     memset(&privs_global, -1, sizeof(privs_global));
155     FlagClr(&privs_global, PRIV_WALK_LCHAN);
156     FlagClr(&privs_global, PRIV_UNLIMIT_QUERY);
157     FlagClr(&privs_global, PRIV_SET);
158     FlagClr(&privs_global, PRIV_BADCHAN);
159     FlagClr(&privs_global, PRIV_LOCAL_BADCHAN);
160     FlagClr(&privs_global, PRIV_APASS_OPMODE);
161     FlagClr(&privs_global, PRIV_UMODE_CHSERV);
162     FlagClr(&privs_global, PRIV_UMODE_XTRAOP);
163     FlagClr(&privs_global, PRIV_UMODE_NETSERV);
164
165     memset(&privs_local, 0, sizeof(privs_local));
166     //FlagSet(&privs_local, PRIV_CHAN_LIMIT);
167     //FlagSet(&privs_local, PRIV_MODE_LCHAN);
168     //FlagSet(&privs_local, PRIV_SHOW_INVIS);
169     //FlagSet(&privs_local, PRIV_SHOW_ALL_INVIS);
170     //FlagSet(&privs_local, PRIV_LOCAL_KILL);
171     //FlagSet(&privs_local, PRIV_REHASH);
172     //FlagSet(&privs_local, PRIV_LOCAL_GLINE);
173     //FlagSet(&privs_local, PRIV_LOCAL_JUPE);
174     //FlagSet(&privs_local, PRIV_LOCAL_OPMODE);
175     //FlagSet(&privs_local, PRIV_WHOX);
176     //FlagSet(&privs_local, PRIV_DISPLAY);
177     //FlagSet(&privs_local, PRIV_FORCE_LOCAL_OPMODE);
178     //FlagSet(&privs_local, PRIV_UMODE_NOCHAN);
179     //FlagSet(&privs_local, PRIV_UMODE_NOIDLE);
180     //FlagSet(&privs_local, PRIV_SEE_IDLETIME);
181     //FlagSet(&privs_local, PRIV_HALFFLOOD);
182     //FlagSet(&privs_local, PRIV_UMODE_OVERRIDECC);
183
184     privs_defaults_set = 1;
185   }
186
187   /* Decide whether to use global or local oper defaults. */
188   if (FlagHas(&oper->privs_dirty, PRIV_PROPAGATE))
189     defaults = FlagHas(&oper->privs, PRIV_PROPAGATE) ? &privs_global : &privs_local;
190   else if (FlagHas(&oper->conn_class->privs_dirty, PRIV_PROPAGATE))
191     defaults = FlagHas(&oper->conn_class->privs, PRIV_PROPAGATE) ? &privs_global : &privs_local;
192   else {
193     assert(0 && "Oper has no propagation and neither does connection class");
194     return;
195   }
196
197   /* For each feature, figure out whether it comes from the operator
198    * conf, the connection class conf, or the defaults, then apply it.
199    */
200   for (priv = 0; priv < PRIV_LAST_PRIV; ++priv)
201   {
202     /* Figure out most applicable definition for the privilege. */
203     if (FlagHas(&oper->privs_dirty, priv))
204       source = &oper->privs;
205     else if (FlagHas(&oper->conn_class->privs_dirty, priv))
206       source = &oper->conn_class->privs;
207     else
208       source = defaults;
209
210     /* Set it if necessary (privileges were already cleared). */
211     if (FlagHas(source, priv))
212       SetPriv(client, priv);
213   }
214
215   /* This should be handled in the config, but lets be sure... */
216   if (HasPriv(client, PRIV_PROPAGATE))
217   {
218     /* force propagating opers to display */
219     SetPriv(client, PRIV_DISPLAY);
220   }
221   else
222   {
223     /* if they don't propagate oper status, prevent desyncs */
224     ClrPriv(client, PRIV_KILL);
225     ClrPriv(client, PRIV_GLINE);
226     ClrPriv(client, PRIV_JUPE);
227     ClrPriv(client, PRIV_OPMODE);
228     ClrPriv(client, PRIV_BADCHAN);
229   }
230 }
231
232 /** Array mapping privilege values to names and vice versa. */
233 static struct {
234   char        *name; /**< Name of privilege. */
235   unsigned int priv; /**< Enumeration value of privilege */
236   unsigned int oper; /**< Oper only privileges. */
237 } privtab[] = {
238 /** Helper macro to define an array entry for a privilege. */
239 #define P(priv)      { #priv, PRIV_ ## priv, 1 }
240 #define UP(priv)  { #priv, PRIV_ ## priv, 0 }
241   UP(CHAN_LIMIT),     P(MODE_LCHAN),     P(WALK_LCHAN),    P(DEOP_LCHAN),
242   P(SHOW_INVIS),     P(SHOW_ALL_INVIS), P(UNLIMIT_QUERY), P(KILL),
243   P(LOCAL_KILL),     P(REHASH),         P(RESTART),       P(DIE),
244   P(GLINE),          P(LOCAL_GLINE),    P(JUPE),          P(LOCAL_JUPE),
245   P(OPMODE),         P(LOCAL_OPMODE),   P(SET),           P(WHOX),
246   P(BADCHAN),        P(LOCAL_BADCHAN),  P(SEE_CHAN),      P(PROPAGATE),
247   P(DISPLAY),        P(SEE_OPERS),      P(WIDE_GLINE),    P(LIST_CHAN),
248   P(FORCE_OPMODE),   P(FORCE_LOCAL_OPMODE), P(APASS_OPMODE),
249   UP(UMODE_NOCHAN),  UP(UMODE_NOIDLE),  UP(UMODE_CHSERV), P(UMODE_XTRAOP),
250   UP(UMODE_NETSERV), UP(SEE_IDLETIME),  UP(HALFFLOOD),    UP(FLOOD),
251   UP(UMODE_OVERRIDECC), UP(UNLIMITED_TARGET), UP(HIDE_IDLETIME),
252   UP(NOAMSG_OVERRIDE),
253 #undef UP
254 #undef P
255   { 0, 0, 0 }
256 };
257
258 /** Set the user privileges for a client.
259  * @param[in] client Client who should get user privileges.
260  * @param[in] aconf Configuration item describing user's privileges.
261  */
262 void
263 client_set_uprivs(struct Client *client, struct ConfItem *aconf) {
264   struct ConnectionClass *conn_class;
265   enum Priv priv;
266   int i;
267
268   if (!MyConnect(client))
269     return;
270
271   /* Clear out client's privileges. */
272   //memset(cli_privs(client), 0, sizeof(struct Privs));
273
274   if (!aconf || !(conn_class = aconf->conn_class))
275     return;
276
277   for (priv = 0; priv < PRIV_LAST_PRIV; ++priv)
278   {
279     if (FlagHas(&conn_class->privs_dirty, priv) && FlagHas(&conn_class->privs, priv)) {
280       for (i = 0; privtab[i].name; i++) {
281         if(privtab[i].priv == priv && !privtab[i].oper) {
282           SetPriv(client, priv);
283           break;
284         }
285         else if(privtab[i].priv == priv && privtab[i].oper) {
286           break;
287         }
288       }
289     }
290   }
291 }
292
293 /** Report privileges of \a client to \a to.
294  * @param[in] to Client requesting privilege list.
295  * @param[in] client Client whos privileges should be listed.
296  * @return Zero.
297  */
298 int
299 client_report_privs(struct Client *to, struct Client *client)
300 {
301   struct MsgBuf *mb;
302   int found1 = 0;
303   int i;
304
305   mb = msgq_make(to, rpl_str(RPL_PRIVS), cli_name(&me), cli_name(to),
306        cli_name(client));
307
308   for (i = 0; privtab[i].name; i++)
309     if (HasPriv(client, privtab[i].priv))
310       msgq_append(0, mb, "%s%s%s", found1++ ? " " : "", (privtab[i].oper ? "" : "*"), privtab[i].name);
311
312   send_buffer(to, mb, 0); /* send response */
313   msgq_clean(mb);
314
315   return 0;
316 }