8bfdfe8a01a686413e6d80316c90a91847d599e5
[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 static struct Privs privs_global;
124 static struct Privs privs_local;
125 static int privs_defaults_set;
126
127 /* client_set_privs(struct Client* client)
128  *
129  * Sets the privileges for opers.
130  */
131 void
132 client_set_privs(struct Client *client, struct ConfItem *oper)
133 {
134   struct Privs *source, *defaults;
135   enum Priv priv;
136
137   if (!privs_defaults_set)
138   {
139     memset(&privs_global, -1, sizeof(privs_global));
140     memset(&privs_local, 0, sizeof(privs_local));
141     PrivSet(&privs_local, PRIV_CHAN_LIMIT);
142     PrivSet(&privs_local, PRIV_MODE_LCHAN);
143     PrivSet(&privs_local, PRIV_SHOW_INVIS);
144     PrivSet(&privs_local, PRIV_SHOW_ALL_INVIS);
145     PrivSet(&privs_local, PRIV_LOCAL_KILL);
146     PrivSet(&privs_local, PRIV_REHASH);
147     PrivSet(&privs_local, PRIV_LOCAL_GLINE);
148     PrivSet(&privs_local, PRIV_LOCAL_JUPE);
149     PrivSet(&privs_local, PRIV_LOCAL_OPMODE);
150     PrivSet(&privs_local, PRIV_WHOX);
151     PrivSet(&privs_local, PRIV_DISPLAY);
152     PrivSet(&privs_local, PRIV_FORCE_LOCAL_OPMODE);
153     privs_defaults_set = 1;
154   }
155   memset(&(cli_privs(client)), 0, sizeof(struct Privs));
156
157   if (!IsAnOper(client))
158     return;
159   else if (!MyConnect(client))
160   {
161     memset(&(cli_privs(client)), 255, sizeof(struct Privs));
162     PrivClr(&(cli_privs(client)), PRIV_SET);
163     return;
164   }
165   else if (oper == NULL)
166     return;
167
168   /* Clear out client's privileges. */
169   memset(&cli_privs(client), 0, sizeof(struct Privs));
170
171   /* Decide whether to use global or local oper defaults. */
172   if (PrivHas(&oper->privs_dirty, PRIV_PROPAGATE))
173     defaults = PrivHas(&oper->privs, PRIV_PROPAGATE) ? &privs_global : &privs_local;
174   else if (PrivHas(&oper->conn_class->privs_dirty, PRIV_PROPAGATE))
175     defaults = PrivHas(&oper->conn_class->privs, PRIV_PROPAGATE) ? &privs_global : &privs_local;
176   else
177       assert(0 && "Oper has no propagation and neither does connection class");
178
179   /* For each feature, figure out whether it comes from the operator
180    * conf, the connection class conf, or the defaults, then apply it.
181    */
182   for (priv = 0; priv < PRIV_LAST_PRIV; ++priv)
183   {
184     /* Figure out most applicable definition for the privilege. */
185     if (PrivHas(&oper->privs_dirty, priv))
186       source = &oper->privs;
187     else if (PrivHas(&oper->conn_class->privs_dirty, priv))
188       source = &oper->conn_class->privs;
189     else
190       source = defaults;
191
192     /* Set it if necessary (privileges were already cleared). */
193     if (PrivHas(source, priv))
194       PrivSet(&cli_privs(client), priv);
195   }
196
197   /* This should be handled in the config, but lets be sure... */
198   if (PrivHas(&cli_privs(client), PRIV_PROPAGATE))
199   {
200     /* force propagating opers to display */
201     PrivSet(&cli_privs(client), PRIV_DISPLAY);
202   }
203   else
204   {
205     /* if they don't propagate oper status, prevent desyncs */
206     PrivClr(&cli_privs(client), PRIV_KILL);
207     PrivClr(&cli_privs(client), PRIV_GLINE);
208     PrivClr(&cli_privs(client), PRIV_JUPE);
209     PrivClr(&cli_privs(client), PRIV_OPMODE);
210     PrivClr(&cli_privs(client), PRIV_BADCHAN);
211   }
212 }
213
214 static struct {
215   char        *name;
216   unsigned int priv;
217 } privtab[] = {
218 #define P(priv)         { #priv, PRIV_ ## priv }
219   P(CHAN_LIMIT),     P(MODE_LCHAN),     P(WALK_LCHAN),    P(DEOP_LCHAN),
220   P(SHOW_INVIS),     P(SHOW_ALL_INVIS), P(UNLIMIT_QUERY), P(KILL),
221   P(LOCAL_KILL),     P(REHASH),         P(RESTART),       P(DIE),
222   P(GLINE),          P(LOCAL_GLINE),    P(JUPE),          P(LOCAL_JUPE),
223   P(OPMODE),         P(LOCAL_OPMODE),   P(SET),           P(WHOX),
224   P(BADCHAN),        P(LOCAL_BADCHAN),  P(SEE_CHAN),      P(PROPAGATE),
225   P(DISPLAY),        P(SEE_OPERS),      P(FORCE_OPMODE),  P(FORCE_LOCAL_OPMODE),
226   P(WIDE_GLINE),
227 #undef P
228   { 0, 0 }
229 };
230
231 /* client_report_privs(struct Client *to, struct Client *client)
232  *
233  * Sends a summary of the oper's privileges to the oper.
234  */
235 int
236 client_report_privs(struct Client *to, struct Client *client)
237 {
238   struct MsgBuf *mb;
239   int found1 = 0;
240   int i;
241
242   mb = msgq_make(to, rpl_str(RPL_PRIVS), cli_name(&me), cli_name(to),
243                  cli_name(client));
244
245   for (i = 0; privtab[i].name; i++)
246     if (HasPriv(client, privtab[i].priv))
247       msgq_append(0, mb, "%s%s", found1++ ? " " : "", privtab[i].name);
248
249   send_buffer(to, mb, 0); /* send response */
250   msgq_clean(mb);
251
252   return 0;
253 }