Doxyfy client.h and client.c.
[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$
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_reply.h"
30 #include "list.h"
31 #include "msgq.h"
32 #include "numeric.h"
33 #include "s_conf.h"
34 #include "s_debug.h"
35 #include "send.h"
36 #include "struct.h"
37
38 #include <assert.h>
39 #include <string.h>
40
41 /** Find the shortest non-zero ping time attached to a client.
42  * If all attached ping times are zero, return the value for
43  * FEAT_PINGFREQUENCY.
44  * @param[in] acptr Client to find ping time for.
45  * @return Ping time in seconds.
46  */
47 int client_get_ping(const struct Client* acptr)
48 {
49   int     ping = 0;
50   struct ConfItem* aconf;
51   struct SLink*    link;
52
53   assert(cli_verify(acptr));
54
55   for (link = cli_confs(acptr); link; link = link->next) {
56     aconf = link->value.aconf;
57     if (aconf->status & (CONF_CLIENT | CONF_SERVER)) {
58       int tmp = get_conf_ping(aconf);
59       if (0 < tmp && (ping > tmp || !ping))
60         ping = tmp;
61     }
62   }
63   if (0 == ping)
64     ping = feature_int(FEAT_PINGFREQUENCY);
65
66   Debug((DEBUG_DEBUG, "Client %s Ping %d", cli_name(acptr), ping));
67
68   return ping;
69 }
70
71 /** Find the default usermode for a client.
72  * @param[in] sptr Client to find default usermode for.
73  * @return Pointer to usermode string (or NULL, if there is no default).
74  */
75 const char* client_get_default_umode(const struct Client* sptr)
76 {
77   struct ConfItem* aconf;
78   struct SLink* link;
79
80   assert(cli_verify(sptr));
81
82   for (link = cli_confs(sptr); link; link = link->next) {
83     aconf = link->value.aconf;
84     if ((aconf->status & CONF_CLIENT) && ConfUmode(aconf))
85       return ConfUmode(aconf);
86   }
87   return NULL;
88 }
89
90 /** Remove a connection from the list of connections with queued data.
91  * @param[in] con Connection with no queued data.
92  */
93 void client_drop_sendq(struct Connection* con)
94 {
95   if (con_prev_p(con)) { /* on the queued data list... */
96     if (con_next(con))
97       con_prev_p(con_next(con)) = con_prev_p(con);
98     *(con_prev_p(con)) = con_next(con);
99
100     con_next(con) = 0;
101     con_prev_p(con) = 0;
102   }
103 }
104
105 /** Add a connection to the list of connections with queued data.
106  * @param[in] con Connection with queued data.
107  * @param[in,out] con_p Previous pointer to next connection.
108  */
109 void client_add_sendq(struct Connection* con, struct Connection** con_p)
110 {
111   if (!con_prev_p(con)) { /* not on the queued data list yet... */
112     con_prev_p(con) = con_p;
113     con_next(con) = *con_p;
114
115     if (*con_p)
116       con_prev_p(*con_p) = &(con_next(con));
117     *con_p = con;
118   }
119 }
120
121 /** Default privilege set for global operators. */
122 static struct Privs privs_global;
123 /** Default privilege set for local operators. */
124 static struct Privs privs_local;
125 /** Non-zero if #privs_global and #privs_local have been initialized. */
126 static int privs_defaults_set;
127
128 /* client_set_privs(struct Client* client)
129  *
130  * Sets the privileges for opers.
131  */
132 /** Set the privileges for a client.
133  * @param[in] client Client who has become an operator.
134  * @param[in] oper Configuration item describing oper's privileges.
135  */
136 void
137 client_set_privs(struct Client *client, struct ConfItem *oper)
138 {
139   struct Privs *source, *defaults;
140   enum Priv priv;
141
142   if (!privs_defaults_set)
143   {
144     memset(&privs_global, -1, sizeof(privs_global));
145     memset(&privs_local, 0, sizeof(privs_local));
146     PrivSet(&privs_local, PRIV_CHAN_LIMIT);
147     PrivSet(&privs_local, PRIV_MODE_LCHAN);
148     PrivSet(&privs_local, PRIV_SHOW_INVIS);
149     PrivSet(&privs_local, PRIV_SHOW_ALL_INVIS);
150     PrivSet(&privs_local, PRIV_LOCAL_KILL);
151     PrivSet(&privs_local, PRIV_REHASH);
152     PrivSet(&privs_local, PRIV_LOCAL_GLINE);
153     PrivSet(&privs_local, PRIV_LOCAL_JUPE);
154     PrivSet(&privs_local, PRIV_LOCAL_OPMODE);
155     PrivSet(&privs_local, PRIV_WHOX);
156     PrivSet(&privs_local, PRIV_DISPLAY);
157     PrivSet(&privs_local, PRIV_FORCE_LOCAL_OPMODE);
158     privs_defaults_set = 1;
159   }
160   memset(&(cli_privs(client)), 0, sizeof(struct Privs));
161
162   if (!IsAnOper(client))
163     return;
164   else if (!MyConnect(client))
165   {
166     memset(&(cli_privs(client)), 255, sizeof(struct Privs));
167     PrivClr(&(cli_privs(client)), PRIV_SET);
168     return;
169   }
170   else if (oper == NULL)
171     return;
172
173   /* Clear out client's privileges. */
174   memset(&cli_privs(client), 0, sizeof(struct Privs));
175
176   /* Decide whether to use global or local oper defaults. */
177   if (PrivHas(&oper->privs_dirty, PRIV_PROPAGATE))
178     defaults = PrivHas(&oper->privs, PRIV_PROPAGATE) ? &privs_global : &privs_local;
179   else if (PrivHas(&oper->conn_class->privs_dirty, PRIV_PROPAGATE))
180     defaults = PrivHas(&oper->conn_class->privs, PRIV_PROPAGATE) ? &privs_global : &privs_local;
181   else {
182     assert(0 && "Oper has no propagation and neither does connection class");
183     return;
184   }
185
186   /* For each feature, figure out whether it comes from the operator
187    * conf, the connection class conf, or the defaults, then apply it.
188    */
189   for (priv = 0; priv < PRIV_LAST_PRIV; ++priv)
190   {
191     /* Figure out most applicable definition for the privilege. */
192     if (PrivHas(&oper->privs_dirty, priv))
193       source = &oper->privs;
194     else if (PrivHas(&oper->conn_class->privs_dirty, priv))
195       source = &oper->conn_class->privs;
196     else
197       source = defaults;
198
199     /* Set it if necessary (privileges were already cleared). */
200     if (PrivHas(source, priv))
201       PrivSet(&cli_privs(client), priv);
202   }
203
204   /* This should be handled in the config, but lets be sure... */
205   if (PrivHas(&cli_privs(client), PRIV_PROPAGATE))
206   {
207     /* force propagating opers to display */
208     PrivSet(&cli_privs(client), PRIV_DISPLAY);
209   }
210   else
211   {
212     /* if they don't propagate oper status, prevent desyncs */
213     PrivClr(&cli_privs(client), PRIV_KILL);
214     PrivClr(&cli_privs(client), PRIV_GLINE);
215     PrivClr(&cli_privs(client), PRIV_JUPE);
216     PrivClr(&cli_privs(client), PRIV_OPMODE);
217     PrivClr(&cli_privs(client), PRIV_BADCHAN);
218   }
219 }
220
221 /** Array mapping privilege values to names and vice versa. */
222 static struct {
223   char        *name; /**< Name of privilege. */
224   unsigned int priv; /**< Enumeration value of privilege */
225 } privtab[] = {
226 /** Helper macro to define an array entry for a privilege. */
227 #define P(priv)         { #priv, PRIV_ ## priv }
228   P(CHAN_LIMIT),     P(MODE_LCHAN),     P(WALK_LCHAN),    P(DEOP_LCHAN),
229   P(SHOW_INVIS),     P(SHOW_ALL_INVIS), P(UNLIMIT_QUERY), P(KILL),
230   P(LOCAL_KILL),     P(REHASH),         P(RESTART),       P(DIE),
231   P(GLINE),          P(LOCAL_GLINE),    P(JUPE),          P(LOCAL_JUPE),
232   P(OPMODE),         P(LOCAL_OPMODE),   P(SET),           P(WHOX),
233   P(BADCHAN),        P(LOCAL_BADCHAN),  P(SEE_CHAN),      P(PROPAGATE),
234   P(DISPLAY),        P(SEE_OPERS),      P(FORCE_OPMODE),  P(FORCE_LOCAL_OPMODE),
235   P(WIDE_GLINE),
236 #undef P
237   { 0, 0 }
238 };
239
240 /** Report privileges of \a client to \a to.
241  * @param[in] to Client requesting privilege list.
242  * @param[in] client Client whos privileges should be listed.
243  * @return Zero.
244  */
245 int
246 client_report_privs(struct Client *to, struct Client *client)
247 {
248   struct MsgBuf *mb;
249   int found1 = 0;
250   int i;
251
252   mb = msgq_make(to, rpl_str(RPL_PRIVS), cli_name(&me), cli_name(to),
253                  cli_name(client));
254
255   for (i = 0; privtab[i].name; i++)
256     if (HasPriv(client, privtab[i].priv))
257       msgq_append(0, mb, "%s%s", found1++ ? " " : "", privtab[i].name);
258
259   send_buffer(to, mb, 0); /* send response */
260   msgq_clean(mb);
261
262   return 0;
263 }