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