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