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