]> jfr.im git - solanum.git/blob - modules/m_cap.c
m_cap: do not allow sasl CAP when the agent is offline
[solanum.git] / modules / m_cap.c
1 /* modules/m_cap.c
2 *
3 * Copyright (C) 2005 Lee Hardy <lee@leeh.co.uk>
4 * Copyright (C) 2005 ircd-ratbox development team
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * 1.Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 * 2.Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3.The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 *
30 * $Id: m_cap.c 676 2006-02-03 20:05:09Z gxti $
31 */
32
33 #include "stdinc.h"
34 #include "class.h"
35 #include "client.h"
36 #include "match.h"
37 #include "ircd.h"
38 #include "numeric.h"
39 #include "msg.h"
40 #include "parse.h"
41 #include "modules.h"
42 #include "s_serv.h"
43 #include "s_user.h"
44 #include "send.h"
45 #include "s_conf.h"
46
47 typedef int (*bqcmp)(const void *, const void *);
48
49 static int m_cap(struct Client *, struct Client *, int, const char **);
50 static int modinit(void);
51
52 struct Message cap_msgtab = {
53 "CAP", 0, 0, 0, MFLG_SLOW,
54 {{m_cap, 2}, {m_cap, 2}, mg_ignore, mg_ignore, mg_ignore, {m_cap, 2}}
55 };
56
57 mapi_clist_av1 cap_clist[] = { &cap_msgtab, NULL };
58 DECLARE_MODULE_AV1(cap, modinit, NULL, cap_clist, NULL, NULL, "$Revision: 676 $");
59
60 #define _CLICAP(name, capserv, capclient, caprequired, flags) \
61 { (name), (capserv), (capclient), (caprequired), (flags), sizeof(name) - 1 }
62
63 #define CLICAP_FLAGS_STICKY 0x001
64
65 static struct clicap
66 {
67 const char *name;
68 int cap_serv; /* for altering s->c */
69 int cap_cli; /* for altering c->s */
70 int cap_required_serv; /* required dependency cap */
71 int flags;
72 int namelen;
73 } clicap_list[] = {
74 _CLICAP("multi-prefix", CLICAP_MULTI_PREFIX, 0, 0, 0),
75 _CLICAP("sasl", CLICAP_SASL, 0, 0, 0),
76 _CLICAP("account-notify", CLICAP_ACCOUNT_NOTIFY, 0, 0, 0),
77 _CLICAP("extended-join", CLICAP_EXTENDED_JOIN, 0, 0, 0),
78 _CLICAP("away-notify", CLICAP_AWAY_NOTIFY, 0, 0, 0),
79 _CLICAP("tls", CLICAP_TLS, 0, 0, 0),
80 _CLICAP("userhost-in-names", CLICAP_USERHOST_IN_NAMES, 0, 0, 0),
81 _CLICAP("cap-notify", CLICAP_CAP_NOTIFY, 0, 0, 0),
82 };
83
84 #define CLICAP_LIST_LEN (sizeof(clicap_list) / sizeof(struct clicap))
85
86 static int clicap_sort(struct clicap *, struct clicap *);
87
88 static int
89 modinit(void)
90 {
91 qsort(clicap_list, CLICAP_LIST_LEN, sizeof(struct clicap),
92 (bqcmp) clicap_sort);
93 return 0;
94 }
95
96 static int
97 clicap_sort(struct clicap *one, struct clicap *two)
98 {
99 return irccmp(one->name, two->name);
100 }
101
102 static int
103 clicap_compare(const char *name, struct clicap *cap)
104 {
105 return irccmp(name, cap->name);
106 }
107
108 /* clicap_find()
109 * Used iteratively over a buffer, extracts individual cap tokens.
110 *
111 * Inputs: buffer to start iterating over (NULL to iterate over existing buf)
112 * int pointer to whether the cap token is negated
113 * int pointer to whether we finish with success
114 * Ouputs: Cap entry if found, NULL otherwise.
115 */
116 static struct clicap *
117 clicap_find(const char *data, int *negate, int *finished)
118 {
119 static char buf[BUFSIZE];
120 static char *p;
121 struct clicap *cap;
122 char *s;
123
124 *negate = 0;
125
126 if(data)
127 {
128 rb_strlcpy(buf, data, sizeof(buf));
129 p = buf;
130 }
131
132 if(*finished)
133 return NULL;
134
135 /* skip any whitespace */
136 while(*p && IsSpace(*p))
137 p++;
138
139 if(EmptyString(p))
140 {
141 *finished = 1;
142 return NULL;
143 }
144
145 if(*p == '-')
146 {
147 *negate = 1;
148 p++;
149
150 /* someone sent a '-' without a parameter.. */
151 if(*p == '\0')
152 return NULL;
153 }
154
155 if((s = strchr(p, ' ')))
156 *s++ = '\0';
157
158 if((cap = bsearch(p, clicap_list, CLICAP_LIST_LEN,
159 sizeof(struct clicap), (bqcmp) clicap_compare)))
160 {
161 if(s)
162 p = s;
163 else
164 *finished = 1;
165 }
166
167 return cap;
168 }
169
170 /* clicap_generate()
171 * Generates a list of capabilities.
172 *
173 * Inputs: client to send to, subcmd to send,
174 * flags to match against: 0 to do none, -1 if client has no flags,
175 * int to whether we are doing CAP CLEAR
176 * Outputs: None
177 */
178 static void
179 clicap_generate(struct Client *source_p, const char *subcmd, int flags, int clear)
180 {
181 char buf[BUFSIZE];
182 char capbuf[BUFSIZE];
183 char *p;
184 int buflen = 0;
185 int curlen, mlen;
186 size_t i;
187
188 mlen = rb_sprintf(buf, ":%s CAP %s %s",
189 me.name,
190 EmptyString(source_p->name) ? "*" : source_p->name,
191 subcmd);
192
193 p = capbuf;
194 buflen = mlen;
195
196 /* shortcut, nothing to do */
197 if(flags == -1)
198 {
199 sendto_one(source_p, "%s :", buf);
200 return;
201 }
202
203 for(i = 0; i < CLICAP_LIST_LEN; i++)
204 {
205 if(flags)
206 {
207 if(!IsCapable(source_p, clicap_list[i].cap_serv))
208 continue;
209 /* they are capable of this, check sticky */
210 else if(clear && clicap_list[i].flags & CLICAP_FLAGS_STICKY)
211 continue;
212 }
213
214 if (clicap_list[i].cap_serv == CLICAP_SASL)
215 {
216 struct Client *agent_p = NULL;
217
218 if (!ConfigFileEntry.sasl_service)
219 continue;
220
221 agent_p = find_named_client(ConfigFileEntry.sasl_service);
222 if (agent_p == NULL || !IsService(agent_p))
223 continue;
224 }
225
226 /* \r\n\0, possible "-~=", space, " *" */
227 if(buflen + clicap_list[i].namelen >= BUFSIZE - 10)
228 {
229 /* remove our trailing space -- if buflen == mlen
230 * here, we didnt even succeed in adding one.
231 */
232 if(buflen != mlen)
233 *(p - 1) = '\0';
234 else
235 *p = '\0';
236
237 sendto_one(source_p, "%s * :%s", buf, capbuf);
238 p = capbuf;
239 buflen = mlen;
240 }
241
242 if(clear)
243 {
244 *p++ = '-';
245 buflen++;
246
247 /* needs a client ack */
248 if(clicap_list[i].cap_cli &&
249 IsCapable(source_p, clicap_list[i].cap_cli))
250 {
251 *p++ = '~';
252 buflen++;
253 }
254 }
255 else
256 {
257 if(clicap_list[i].flags & CLICAP_FLAGS_STICKY)
258 {
259 *p++ = '=';
260 buflen++;
261 }
262
263 /* if we're doing an LS, then we only send this if
264 * they havent ack'd
265 */
266 if(clicap_list[i].cap_cli &&
267 (!flags || !IsCapable(source_p, clicap_list[i].cap_cli)))
268 {
269 *p++ = '~';
270 buflen++;
271 }
272 }
273
274 curlen = rb_sprintf(p, "%s ", clicap_list[i].name);
275 p += curlen;
276 buflen += curlen;
277 }
278
279 /* remove trailing space */
280 if(buflen != mlen)
281 *(p - 1) = '\0';
282 else
283 *p = '\0';
284
285 sendto_one(source_p, "%s :%s", buf, capbuf);
286 }
287
288 static void
289 cap_ack(struct Client *source_p, const char *arg)
290 {
291 struct clicap *cap;
292 int capadd = 0, capdel = 0;
293 int finished = 0, negate;
294
295 if(EmptyString(arg))
296 return;
297
298 for(cap = clicap_find(arg, &negate, &finished); cap;
299 cap = clicap_find(NULL, &negate, &finished))
300 {
301 /* sent an ACK for something they havent REQd */
302 if(!IsCapable(source_p, cap->cap_serv))
303 continue;
304
305 if(negate)
306 {
307 /* dont let them ack something sticky off */
308 if(cap->flags & CLICAP_FLAGS_STICKY)
309 continue;
310
311 capdel |= cap->cap_cli;
312 }
313 else
314 capadd |= cap->cap_cli;
315 }
316
317 source_p->localClient->caps |= capadd;
318 source_p->localClient->caps &= ~capdel;
319 }
320
321 static void
322 cap_clear(struct Client *source_p, const char *arg)
323 {
324 clicap_generate(source_p, "ACK",
325 source_p->localClient->caps ? source_p->localClient->caps : -1, 1);
326
327 /* XXX - sticky capabs */
328 #ifdef CLICAP_STICKY
329 source_p->localClient->caps = source_p->localClient->caps & CLICAP_STICKY;
330 #else
331 source_p->localClient->caps = 0;
332 #endif
333 }
334
335 static void
336 cap_end(struct Client *source_p, const char *arg)
337 {
338 if(IsRegistered(source_p))
339 return;
340
341 source_p->flags &= ~FLAGS_CLICAP;
342
343 if(source_p->name[0] && source_p->flags & FLAGS_SENTUSER)
344 {
345 char buf[USERLEN+1];
346 rb_strlcpy(buf, source_p->username, sizeof(buf));
347 register_local_user(source_p, source_p, buf);
348 }
349 }
350
351 static void
352 cap_list(struct Client *source_p, const char *arg)
353 {
354 /* list of what theyre currently using */
355 clicap_generate(source_p, "LIST",
356 source_p->localClient->caps ? source_p->localClient->caps : -1, 0);
357 }
358
359 static void
360 cap_ls(struct Client *source_p, const char *arg)
361 {
362 if(!IsRegistered(source_p))
363 source_p->flags |= FLAGS_CLICAP;
364
365 /* list of what we support */
366 clicap_generate(source_p, "LS", 0, 0);
367 }
368
369 static void
370 cap_req(struct Client *source_p, const char *arg)
371 {
372 char buf[BUFSIZE];
373 char pbuf[2][BUFSIZE];
374 struct clicap *cap;
375 int buflen, plen;
376 int i = 0;
377 int capadd = 0, capdel = 0;
378 int finished = 0, negate;
379
380 if(!IsRegistered(source_p))
381 source_p->flags |= FLAGS_CLICAP;
382
383 if(EmptyString(arg))
384 return;
385
386 buflen = rb_snprintf(buf, sizeof(buf), ":%s CAP %s ACK",
387 me.name, EmptyString(source_p->name) ? "*" : source_p->name);
388
389 pbuf[0][0] = '\0';
390 plen = 0;
391
392 for(cap = clicap_find(arg, &negate, &finished); cap;
393 cap = clicap_find(NULL, &negate, &finished))
394 {
395 /* filled the first array, but cant send it in case the
396 * request fails. one REQ should never fill more than two
397 * buffers --fl
398 */
399 if(buflen + plen + cap->namelen + 6 >= BUFSIZE)
400 {
401 pbuf[1][0] = '\0';
402 plen = 0;
403 i = 1;
404 }
405
406 if(negate)
407 {
408 if(cap->flags & CLICAP_FLAGS_STICKY)
409 {
410 finished = 0;
411 break;
412 }
413
414 strcat(pbuf[i], "-");
415 plen++;
416
417 capdel |= cap->cap_serv;
418 }
419 else
420 {
421 if(cap->cap_required_serv && !((capadd & cap->cap_required_serv) == cap->cap_required_serv || IsCapable(source_p, cap->cap_required_serv)))
422 {
423 finished = 0;
424 break;
425 }
426
427 if (cap->cap_serv == CLICAP_SASL)
428 {
429 struct Client *agent_p = NULL;
430
431 if (!ConfigFileEntry.sasl_service)
432 {
433 finished = 0;
434 break;
435 }
436
437 agent_p = find_named_client(ConfigFileEntry.sasl_service);
438 if (agent_p == NULL || !IsService(agent_p))
439 {
440 finished = 0;
441 break;
442 }
443 }
444
445 if(cap->flags & CLICAP_FLAGS_STICKY)
446 {
447 strcat(pbuf[i], "=");
448 plen++;
449 }
450
451 capadd |= cap->cap_serv;
452 }
453
454 if(cap->cap_cli)
455 {
456 strcat(pbuf[i], "~");
457 plen++;
458 }
459
460 strcat(pbuf[i], cap->name);
461 strcat(pbuf[i], " ");
462 plen += (cap->namelen + 1);
463 }
464
465 if(!finished)
466 {
467 sendto_one(source_p, ":%s CAP %s NAK :%s",
468 me.name, EmptyString(source_p->name) ? "*" : source_p->name, arg);
469 return;
470 }
471
472 if(i)
473 {
474 sendto_one(source_p, "%s * :%s", buf, pbuf[0]);
475 sendto_one(source_p, "%s :%s", buf, pbuf[1]);
476 }
477 else
478 sendto_one(source_p, "%s :%s", buf, pbuf[0]);
479
480 source_p->localClient->caps |= capadd;
481 source_p->localClient->caps &= ~capdel;
482 }
483
484 static struct clicap_cmd
485 {
486 const char *cmd;
487 void (*func)(struct Client *source_p, const char *arg);
488 } clicap_cmdlist[] = {
489 /* This list *MUST* be in alphabetical order */
490 { "ACK", cap_ack },
491 { "CLEAR", cap_clear },
492 { "END", cap_end },
493 { "LIST", cap_list },
494 { "LS", cap_ls },
495 { "REQ", cap_req },
496 };
497
498 static int
499 clicap_cmd_search(const char *command, struct clicap_cmd *entry)
500 {
501 return irccmp(command, entry->cmd);
502 }
503
504 static int
505 m_cap(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
506 {
507 struct clicap_cmd *cmd;
508
509 if(!(cmd = bsearch(parv[1], clicap_cmdlist,
510 sizeof(clicap_cmdlist) / sizeof(struct clicap_cmd),
511 sizeof(struct clicap_cmd), (bqcmp) clicap_cmd_search)))
512 {
513 sendto_one(source_p, form_str(ERR_INVALIDCAPCMD),
514 me.name, EmptyString(source_p->name) ? "*" : source_p->name,
515 parv[1]);
516 return 0;
517 }
518
519 (cmd->func)(source_p, parv[2]);
520 return 0;
521 }