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