1 |
michael |
503 |
/* |
2 |
michael |
2820 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
michael |
503 |
* |
4 |
michael |
2820 |
* Copyright (c) 2004 Kevin L. Mitchell <klmitch@mit.edu> |
5 |
|
|
* Copyright (c) 2006-2014 ircd-hybrid development team |
6 |
michael |
503 |
* |
7 |
|
|
* This program is free software; you can redistribute it and/or modify |
8 |
|
|
* it under the terms of the GNU General Public License as published by |
9 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
10 |
|
|
* (at your option) any later version. |
11 |
|
|
* |
12 |
|
|
* This program is distributed in the hope that it will be useful, |
13 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
|
|
* GNU General Public License for more details. |
16 |
|
|
* |
17 |
|
|
* You should have received a copy of the GNU General Public License |
18 |
|
|
* along with this program; if not, write to the Free Software |
19 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
20 |
|
|
* USA |
21 |
|
|
*/ |
22 |
|
|
|
23 |
michael |
2820 |
/*! \file m_cap.c |
24 |
|
|
* \brief Includes required functions for processing the CAP command. |
25 |
|
|
* \version $Id$ |
26 |
michael |
503 |
*/ |
27 |
|
|
|
28 |
|
|
#include "stdinc.h" |
29 |
|
|
#include "client.h" |
30 |
|
|
#include "hash.h" |
31 |
|
|
#include "ircd.h" |
32 |
|
|
#include "numeric.h" |
33 |
michael |
1309 |
#include "conf.h" |
34 |
michael |
503 |
#include "s_user.h" |
35 |
|
|
#include "s_serv.h" |
36 |
|
|
#include "send.h" |
37 |
|
|
#include "parse.h" |
38 |
|
|
#include "modules.h" |
39 |
|
|
#include "packet.h" |
40 |
|
|
#include "irc_string.h" |
41 |
|
|
|
42 |
|
|
|
43 |
|
|
#define CAPFL_HIDDEN 0x0001 /**< Do not advertize this capability */ |
44 |
|
|
#define CAPFL_PROHIBIT 0x0002 /**< Client may not set this capability */ |
45 |
|
|
#define CAPFL_PROTO 0x0004 /**< Cap must be acknowledged by client */ |
46 |
|
|
#define CAPFL_STICKY 0x0008 /**< Cap may not be cleared once set */ |
47 |
|
|
|
48 |
|
|
typedef int (*bqcmp)(const void *, const void *); |
49 |
|
|
|
50 |
|
|
static struct capabilities |
51 |
|
|
{ |
52 |
|
|
unsigned int cap; |
53 |
|
|
unsigned int flags; |
54 |
|
|
const char *name; |
55 |
|
|
size_t namelen; |
56 |
|
|
} capab_list[] = { |
57 |
|
|
#define _CAP(cap, flags, name) \ |
58 |
michael |
2345 |
{ (cap), (flags), (name), sizeof(name) - 1 } |
59 |
michael |
1734 |
_CAP(CAP_MULTI_PREFIX, 0, "multi-prefix"), |
60 |
|
|
_CAP(CAP_AWAY_NOTIFY, 0, "away-notify") |
61 |
michael |
503 |
#undef _CAP |
62 |
|
|
}; |
63 |
|
|
|
64 |
michael |
2345 |
#define CAPAB_LIST_LEN (sizeof(capab_list) / sizeof(struct capabilities)) |
65 |
michael |
503 |
|
66 |
|
|
static int |
67 |
|
|
capab_sort(const struct capabilities *cap1, const struct capabilities *cap2) |
68 |
|
|
{ |
69 |
michael |
536 |
return strcasecmp(cap1->name, cap2->name); |
70 |
michael |
503 |
} |
71 |
|
|
|
72 |
|
|
static int |
73 |
|
|
capab_search(const char *key, const struct capabilities *cap) |
74 |
|
|
{ |
75 |
|
|
const char *rb = cap->name; |
76 |
|
|
|
77 |
michael |
2820 |
while (ToLower(*key) == ToLower(*rb)) /* Walk equivalent part of strings */ |
78 |
|
|
if (*key++ == '\0') /* Hit the end, all right... */ |
79 |
michael |
503 |
return 0; |
80 |
|
|
else /* OK, let's move on... */ |
81 |
|
|
rb++; |
82 |
|
|
|
83 |
michael |
872 |
/* |
84 |
|
|
* If the character they differ on happens to be a space, and it happens |
85 |
michael |
503 |
* to be the same length as the capability name, then we've found a |
86 |
|
|
* match; otherwise, return the difference of the two. |
87 |
|
|
*/ |
88 |
|
|
return (IsSpace(*key) && *rb == '\0') ? 0 : (ToLower(*key) - ToLower(*rb)); |
89 |
|
|
} |
90 |
|
|
|
91 |
|
|
static struct capabilities * |
92 |
|
|
find_cap(const char **caplist_p, int *neg_p) |
93 |
|
|
{ |
94 |
|
|
static int inited = 0; |
95 |
|
|
const char *caplist = *caplist_p; |
96 |
michael |
872 |
struct capabilities *cap = NULL; |
97 |
michael |
503 |
|
98 |
michael |
2820 |
*neg_p = 0; /* Clear negative flag... */ |
99 |
michael |
503 |
|
100 |
|
|
if (!inited) |
101 |
|
|
{ |
102 |
|
|
/* First, let's sort the array... */ |
103 |
|
|
qsort(capab_list, CAPAB_LIST_LEN, sizeof(struct capabilities), (bqcmp)capab_sort); |
104 |
|
|
++inited; |
105 |
|
|
} |
106 |
|
|
|
107 |
|
|
/* Next, find first non-whitespace character... */ |
108 |
|
|
while (*caplist && IsSpace(*caplist)) |
109 |
|
|
++caplist; |
110 |
|
|
|
111 |
|
|
/* We are now at the beginning of an element of the list; is it negative? */ |
112 |
michael |
872 |
if (*caplist == '-') |
113 |
|
|
{ |
114 |
michael |
2820 |
++caplist; /* Yes; step past the flag... */ |
115 |
|
|
*neg_p = 1; /* Remember that it is negative... */ |
116 |
michael |
503 |
} |
117 |
|
|
|
118 |
|
|
/* OK, now see if we can look up the capability... */ |
119 |
michael |
872 |
if (*caplist) |
120 |
|
|
{ |
121 |
michael |
503 |
if (!(cap = bsearch(caplist, capab_list, CAPAB_LIST_LEN, |
122 |
|
|
sizeof(struct capabilities), |
123 |
|
|
(bqcmp)capab_search))) |
124 |
|
|
{ |
125 |
|
|
/* Couldn't find the capability; advance to first whitespace character */ |
126 |
|
|
while (*caplist && !IsSpace(*caplist)) |
127 |
michael |
2345 |
++caplist; |
128 |
michael |
503 |
} |
129 |
|
|
else |
130 |
michael |
2820 |
caplist += cap->namelen; /* Advance to end of capability name */ |
131 |
michael |
503 |
} |
132 |
|
|
|
133 |
michael |
2820 |
assert(caplist != *caplist_p || !*caplist); /* We *must* advance */ |
134 |
michael |
503 |
|
135 |
michael |
2820 |
/* Move ahead in capability list string--or zero pointer if we hit end */ |
136 |
michael |
503 |
*caplist_p = *caplist ? caplist : 0; |
137 |
|
|
|
138 |
michael |
2820 |
return cap; /* And return the capability (if any) */ |
139 |
michael |
503 |
} |
140 |
|
|
|
141 |
michael |
1230 |
/** Send a CAP \a subcmd list of capability changes to \a source_p. |
142 |
michael |
503 |
* If more than one line is necessary, each line before the last has |
143 |
|
|
* an added "*" parameter before that line's capability list. |
144 |
michael |
1230 |
* @param[in] source_p Client receiving capability list. |
145 |
michael |
503 |
* @param[in] set Capabilities to show as set (with ack and sticky modifiers). |
146 |
|
|
* @param[in] rem Capabalities to show as removed (with no other modifier). |
147 |
|
|
* @param[in] subcmd Name of capability subcommand. |
148 |
|
|
*/ |
149 |
|
|
static int |
150 |
michael |
1230 |
send_caplist(struct Client *source_p, unsigned int set, |
151 |
michael |
503 |
unsigned int rem, const char *subcmd) |
152 |
|
|
{ |
153 |
|
|
char capbuf[IRCD_BUFSIZE] = "", pfx[16]; |
154 |
|
|
char cmdbuf[IRCD_BUFSIZE] = ""; |
155 |
michael |
575 |
unsigned int i, loc, len, flags, pfx_len, clen; |
156 |
michael |
503 |
|
157 |
michael |
2820 |
/* Set up the buffer for the final LS message... */ |
158 |
michael |
503 |
clen = snprintf(cmdbuf, sizeof(capbuf), ":%s CAP %s %s ", me.name, |
159 |
michael |
1230 |
source_p->name[0] ? source_p->name : "*", subcmd); |
160 |
michael |
503 |
|
161 |
|
|
for (i = 0, loc = 0; i < CAPAB_LIST_LEN; ++i) |
162 |
|
|
{ |
163 |
|
|
flags = capab_list[i].flags; |
164 |
|
|
|
165 |
|
|
/* |
166 |
|
|
* This is a little bit subtle, but just involves applying de |
167 |
|
|
* Morgan's laws to the obvious check: We must display the |
168 |
|
|
* capability if (and only if) it is set in \a rem or \a set, or |
169 |
|
|
* if both are null and the capability is hidden. |
170 |
|
|
*/ |
171 |
|
|
if (!(rem && (rem & capab_list[i].cap)) && |
172 |
|
|
!(set && (set & capab_list[i].cap)) && |
173 |
|
|
(rem || set || (flags & CAPFL_HIDDEN))) |
174 |
|
|
continue; |
175 |
|
|
|
176 |
|
|
/* Build the prefix (space separator and any modifiers needed). */ |
177 |
|
|
pfx_len = 0; |
178 |
|
|
|
179 |
|
|
if (loc) |
180 |
|
|
pfx[pfx_len++] = ' '; |
181 |
|
|
if (rem && (rem & capab_list[i].cap)) |
182 |
|
|
pfx[pfx_len++] = '-'; |
183 |
|
|
else |
184 |
|
|
{ |
185 |
|
|
if (flags & CAPFL_PROTO) |
186 |
|
|
pfx[pfx_len++] = '~'; |
187 |
|
|
if (flags & CAPFL_STICKY) |
188 |
|
|
pfx[pfx_len++] = '='; |
189 |
|
|
} |
190 |
|
|
|
191 |
|
|
pfx[pfx_len] = '\0'; |
192 |
|
|
|
193 |
michael |
2820 |
len = capab_list[i].namelen + pfx_len; /* How much we'd add... */ |
194 |
michael |
503 |
|
195 |
|
|
if (sizeof(capbuf) < (clen + loc + len + 15)) |
196 |
|
|
{ |
197 |
michael |
2820 |
/* Would add too much; must flush */ |
198 |
michael |
1230 |
sendto_one(source_p, "%s* :%s", cmdbuf, capbuf); |
199 |
michael |
2820 |
capbuf[(loc = 0)] = '\0'; /* Re-terminate the buffer... */ |
200 |
michael |
503 |
} |
201 |
|
|
|
202 |
|
|
loc += snprintf(capbuf + loc, sizeof(capbuf) - loc, |
203 |
|
|
"%s%s", pfx, capab_list[i].name); |
204 |
|
|
} |
205 |
|
|
|
206 |
michael |
1230 |
sendto_one(source_p, "%s:%s", cmdbuf, capbuf); |
207 |
michael |
503 |
|
208 |
michael |
2820 |
return 0; /* Convenience return */ |
209 |
michael |
503 |
} |
210 |
|
|
|
211 |
|
|
static int |
212 |
michael |
1230 |
cap_ls(struct Client *source_p, const char *caplist) |
213 |
michael |
503 |
{ |
214 |
michael |
2820 |
if (IsUnknown(source_p)) /* Registration hasn't completed; suspend it... */ |
215 |
michael |
1230 |
source_p->localClient->registration |= REG_NEED_CAP; |
216 |
michael |
503 |
|
217 |
michael |
2820 |
return send_caplist(source_p, 0, 0, "LS"); /* Send list of capabilities */ |
218 |
michael |
503 |
} |
219 |
|
|
|
220 |
|
|
static int |
221 |
michael |
1230 |
cap_req(struct Client *source_p, const char *caplist) |
222 |
michael |
503 |
{ |
223 |
|
|
const char *cl = caplist; |
224 |
michael |
872 |
struct capabilities *cap = NULL; |
225 |
michael |
503 |
unsigned int set = 0, rem = 0; |
226 |
michael |
1230 |
unsigned int cs = source_p->localClient->cap_client; /* capability set */ |
227 |
|
|
unsigned int as = source_p->localClient->cap_active; /* active set */ |
228 |
michael |
872 |
int neg = 0; |
229 |
michael |
503 |
|
230 |
michael |
2820 |
if (IsUnknown(source_p)) /* Registration hasn't completed; suspend it... */ |
231 |
michael |
1230 |
source_p->localClient->registration |= REG_NEED_CAP; |
232 |
michael |
503 |
|
233 |
michael |
2820 |
while (cl) { /* Walk through the capabilities list... */ |
234 |
|
|
if (!(cap = find_cap(&cl, &neg)) /* Look up capability... */ |
235 |
|
|
|| (!neg && (cap->flags & CAPFL_PROHIBIT)) /* Is it prohibited? */ |
236 |
|
|
|| (neg && (cap->flags & CAPFL_STICKY))) { /* Is it sticky? */ |
237 |
michael |
1230 |
sendto_one(source_p, ":%s CAP %s NAK :%s", me.name, |
238 |
|
|
source_p->name[0] ? source_p->name : "*", caplist); |
239 |
michael |
2820 |
return 0; /* Can't complete requested op... */ |
240 |
michael |
503 |
} |
241 |
|
|
|
242 |
|
|
if (neg) |
243 |
|
|
{ |
244 |
michael |
2820 |
/* Set or clear the capability... */ |
245 |
michael |
503 |
rem |= cap->cap; |
246 |
|
|
set &= ~cap->cap; |
247 |
|
|
cs &= ~cap->cap; |
248 |
|
|
|
249 |
|
|
if (!(cap->flags & CAPFL_PROTO)) |
250 |
|
|
as &= ~cap->cap; |
251 |
|
|
} |
252 |
|
|
else |
253 |
|
|
{ |
254 |
|
|
rem &= ~cap->cap; |
255 |
|
|
set |= cap->cap; |
256 |
|
|
cs |= cap->cap; |
257 |
|
|
|
258 |
|
|
if (!(cap->flags & CAPFL_PROTO)) |
259 |
|
|
as |= cap->cap; |
260 |
|
|
} |
261 |
|
|
} |
262 |
|
|
|
263 |
|
|
/* Notify client of accepted changes and copy over results. */ |
264 |
michael |
1230 |
send_caplist(source_p, set, rem, "ACK"); |
265 |
michael |
503 |
|
266 |
michael |
1230 |
source_p->localClient->cap_client = cs; |
267 |
|
|
source_p->localClient->cap_active = as; |
268 |
michael |
503 |
|
269 |
|
|
return 0; |
270 |
|
|
} |
271 |
|
|
|
272 |
|
|
static int |
273 |
michael |
1230 |
cap_ack(struct Client *source_p, const char *caplist) |
274 |
michael |
503 |
{ |
275 |
|
|
const char *cl = caplist; |
276 |
|
|
struct capabilities *cap = NULL; |
277 |
michael |
872 |
int neg = 0; |
278 |
michael |
503 |
|
279 |
|
|
/* |
280 |
|
|
* Coming from the client, this generally indicates that the client |
281 |
michael |
2820 |
* is using a new backwards-incompatible protocol feature. As such, |
282 |
michael |
503 |
* it does not require further response from the server. |
283 |
|
|
*/ |
284 |
|
|
while (cl) |
285 |
|
|
{ |
286 |
michael |
2820 |
/* Walk through the capabilities list... */ |
287 |
|
|
if (!(cap = find_cap(&cl, &neg)) || /* Look up capability... */ |
288 |
michael |
2345 |
(neg ? (source_p->localClient->cap_active & cap->cap) : |
289 |
|
|
!(source_p->localClient->cap_active & cap->cap))) /* uh... */ |
290 |
michael |
503 |
continue; |
291 |
|
|
|
292 |
michael |
2820 |
if (neg) /* Set or clear the active capability... */ |
293 |
michael |
1230 |
source_p->localClient->cap_active &= ~cap->cap; |
294 |
michael |
503 |
else |
295 |
michael |
1230 |
source_p->localClient->cap_active |= cap->cap; |
296 |
michael |
503 |
} |
297 |
|
|
|
298 |
|
|
return 0; |
299 |
|
|
} |
300 |
|
|
|
301 |
|
|
static int |
302 |
michael |
1230 |
cap_clear(struct Client *source_p, const char *caplist) |
303 |
michael |
503 |
{ |
304 |
michael |
872 |
struct capabilities *cap = NULL; |
305 |
michael |
503 |
unsigned int ii; |
306 |
|
|
unsigned int cleared = 0; |
307 |
|
|
|
308 |
|
|
for (ii = 0; ii < CAPAB_LIST_LEN; ++ii) |
309 |
|
|
{ |
310 |
|
|
cap = &capab_list[ii]; |
311 |
|
|
|
312 |
|
|
/* Only clear active non-sticky capabilities. */ |
313 |
michael |
1230 |
if (!(source_p->localClient->cap_active & cap->cap) || (cap->flags & CAPFL_STICKY)) |
314 |
michael |
503 |
continue; |
315 |
|
|
|
316 |
|
|
cleared |= cap->cap; |
317 |
michael |
1230 |
source_p->localClient->cap_client &= ~cap->cap; |
318 |
michael |
503 |
|
319 |
|
|
if (!(cap->flags & CAPFL_PROTO)) |
320 |
michael |
1230 |
source_p->localClient->cap_active &= ~cap->cap; |
321 |
michael |
503 |
} |
322 |
|
|
|
323 |
michael |
1230 |
return send_caplist(source_p, 0, cleared, "ACK"); |
324 |
michael |
503 |
} |
325 |
|
|
|
326 |
|
|
static int |
327 |
michael |
1230 |
cap_end(struct Client *source_p, const char *caplist) |
328 |
michael |
503 |
{ |
329 |
michael |
2820 |
if (!IsUnknown(source_p)) /* Registration has completed... */ |
330 |
|
|
return 0; /* So just ignore the message... */ |
331 |
michael |
503 |
|
332 |
michael |
2820 |
/* Capability negotiation is now done... */ |
333 |
michael |
1230 |
source_p->localClient->registration &= ~REG_NEED_CAP; |
334 |
michael |
503 |
|
335 |
michael |
2820 |
/* If client is now done... */ |
336 |
michael |
1230 |
if (!source_p->localClient->registration) |
337 |
michael |
503 |
{ |
338 |
michael |
1230 |
register_local_user(source_p); |
339 |
michael |
503 |
return 0; |
340 |
|
|
} |
341 |
|
|
|
342 |
michael |
2345 |
return 0; /* Can't do registration yet... */ |
343 |
michael |
503 |
} |
344 |
|
|
|
345 |
|
|
static int |
346 |
michael |
1230 |
cap_list(struct Client *source_p, const char *caplist) |
347 |
michael |
503 |
{ |
348 |
|
|
/* Send the list of the client's capabilities */ |
349 |
michael |
1230 |
return send_caplist(source_p, source_p->localClient->cap_client, 0, "LIST"); |
350 |
michael |
503 |
} |
351 |
|
|
|
352 |
|
|
static struct subcmd |
353 |
|
|
{ |
354 |
michael |
575 |
const char *cmd; |
355 |
michael |
1230 |
int (*proc)(struct Client *, const char *); |
356 |
michael |
503 |
} cmdlist[] = { |
357 |
|
|
{ "ACK", cap_ack }, |
358 |
|
|
{ "CLEAR", cap_clear }, |
359 |
|
|
{ "END", cap_end }, |
360 |
|
|
{ "LIST", cap_list }, |
361 |
|
|
{ "LS", cap_ls }, |
362 |
michael |
575 |
{ "NAK", NULL }, |
363 |
michael |
503 |
{ "REQ", cap_req } |
364 |
|
|
}; |
365 |
|
|
|
366 |
|
|
static int |
367 |
|
|
subcmd_search(const char *cmd, const struct subcmd *elem) |
368 |
|
|
{ |
369 |
michael |
536 |
return strcasecmp(cmd, elem->cmd); |
370 |
michael |
503 |
} |
371 |
|
|
|
372 |
|
|
/** Handle a capability request or response from a client. |
373 |
michael |
1230 |
* \param client_p Client that sent us the message. |
374 |
|
|
* \param source_p Original source of message. |
375 |
|
|
* \param parc Number of arguments. |
376 |
|
|
* \param parv Argument vector. |
377 |
michael |
503 |
*/ |
378 |
michael |
2820 |
static int |
379 |
michael |
1230 |
m_cap(struct Client *client_p, struct Client *source_p, int parc, char *parv[]) |
380 |
michael |
503 |
{ |
381 |
michael |
1222 |
const char *subcmd = NULL, *caplist = NULL; |
382 |
michael |
503 |
struct subcmd *cmd = NULL; |
383 |
|
|
|
384 |
michael |
2820 |
if (EmptyString(parv[1])) /* A subcommand is required */ |
385 |
|
|
return 0; |
386 |
michael |
503 |
|
387 |
|
|
subcmd = parv[1]; |
388 |
|
|
|
389 |
michael |
2820 |
if (parc > 2) /* A capability list was provided */ |
390 |
michael |
503 |
caplist = parv[2]; |
391 |
|
|
|
392 |
michael |
2820 |
/* Find the subcommand handler */ |
393 |
michael |
503 |
if (!(cmd = bsearch(subcmd, cmdlist, |
394 |
|
|
sizeof(cmdlist) / sizeof(struct subcmd), |
395 |
|
|
sizeof(struct subcmd), (bqcmp)subcmd_search))) |
396 |
|
|
{ |
397 |
michael |
1834 |
sendto_one(source_p, form_str(ERR_INVALIDCAPCMD), me.name, |
398 |
michael |
1230 |
source_p->name[0] ? source_p->name : "*", subcmd); |
399 |
michael |
2820 |
return 0; |
400 |
michael |
503 |
} |
401 |
|
|
|
402 |
michael |
2820 |
/* Then execute it... */ |
403 |
michael |
503 |
if (cmd->proc) |
404 |
michael |
1230 |
(cmd->proc)(source_p, caplist); |
405 |
michael |
2820 |
return 0; |
406 |
michael |
503 |
} |
407 |
michael |
1230 |
|
408 |
michael |
2820 |
static struct Message cap_msgtab = |
409 |
|
|
{ |
410 |
michael |
1230 |
"CAP", 0, 0, 2, MAXPARA, MFLG_SLOW, 0, |
411 |
|
|
{ m_cap, m_cap, m_ignore, m_ignore, m_cap, m_ignore } |
412 |
|
|
}; |
413 |
|
|
|
414 |
|
|
static void |
415 |
|
|
module_init(void) |
416 |
|
|
{ |
417 |
|
|
mod_add_cmd(&cap_msgtab); |
418 |
|
|
} |
419 |
|
|
|
420 |
|
|
static void |
421 |
|
|
module_exit(void) |
422 |
|
|
{ |
423 |
|
|
mod_del_cmd(&cap_msgtab); |
424 |
|
|
} |
425 |
|
|
|
426 |
michael |
2820 |
struct module module_entry = |
427 |
|
|
{ |
428 |
michael |
1230 |
.node = { NULL, NULL, NULL }, |
429 |
|
|
.name = NULL, |
430 |
|
|
.version = "$Revision$", |
431 |
|
|
.handle = NULL, |
432 |
|
|
.modinit = module_init, |
433 |
|
|
.modexit = module_exit, |
434 |
|
|
.flags = 0 |
435 |
|
|
}; |