]> jfr.im git - irc/quakenet/snircd.git/blame - ircd/s_user.c
forward port of asuka-kX.patch to .12
[irc/quakenet/snircd.git] / ircd / s_user.c
CommitLineData
189935b1 1/*
2 * IRC - Internet Relay Chat, ircd/s_user.c (formerly ircd/s_msg.c)
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Computing Center
5 *
6 * See file AUTHORS in IRC package for additional names of
7 * the programmers.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 1, or (at your option)
12 * any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 */
23/** @file
24 * @brief Miscellaneous user-related helper functions.
25 * @version $Id: s_user.c,v 1.99 2005/08/17 01:57:03 entrope Exp $
26 */
27#include "config.h"
28
29#include "s_user.h"
30#include "IPcheck.h"
31#include "channel.h"
32#include "class.h"
33#include "client.h"
34#include "hash.h"
35#include "ircd.h"
36#include "ircd_alloc.h"
37#include "ircd_auth.h"
38#include "ircd_chattr.h"
39#include "ircd_features.h"
40#include "ircd_log.h"
41#include "ircd_reply.h"
42#include "ircd_snprintf.h"
43#include "ircd_string.h"
44#include "list.h"
45#include "match.h"
46#include "motd.h"
47#include "msg.h"
48#include "msgq.h"
49#include "numeric.h"
50#include "numnicks.h"
51#include "parse.h"
52#include "querycmds.h"
53#include "random.h"
54#include "s_bsd.h"
55#include "s_conf.h"
56#include "s_debug.h"
57#include "s_misc.h"
58#include "s_serv.h" /* max_client_count */
59#include "send.h"
60#include "struct.h"
61#include "supported.h"
62#include "sys.h"
63#include "userload.h"
64#include "version.h"
65#include "whowas.h"
66
67#include "handlers.h" /* m_motd and m_lusers */
68
69/* #include <assert.h> -- Now using assert in ircd_log.h */
70#include <fcntl.h>
71#include <stdio.h>
72#include <stdlib.h>
73#include <string.h>
74#include <sys/stat.h>
75
76/** Count of allocated User structures. */
77static int userCount = 0;
78
79/** Makes sure that \a cptr has a User information block.
80 * If cli_user(cptr) != NULL, does nothing.
81 * @param[in] cptr Client to attach User struct to.
82 * @return User struct associated with \a cptr.
83 */
84struct User *make_user(struct Client *cptr)
85{
86 assert(0 != cptr);
87
88 if (!cli_user(cptr)) {
89 cli_user(cptr) = (struct User*) MyMalloc(sizeof(struct User));
90 assert(0 != cli_user(cptr));
91
92 /* All variables are 0 by default */
93 memset(cli_user(cptr), 0, sizeof(struct User));
94 ++userCount;
95 cli_user(cptr)->refcnt = 1;
96 }
97 return cli_user(cptr);
98}
99
100/** Dereference \a user.
101 * User structures are reference-counted; if the refcount of \a user
102 * becomes zero, free it.
103 * @param[in] user User to dereference.
104 */
105void free_user(struct User* user)
106{
107 assert(0 != user);
108 assert(0 < user->refcnt);
109
110 if (--user->refcnt == 0) {
111 if (user->away)
112 MyFree(user->away);
113 /*
114 * sanity check
115 */
116 assert(0 == user->joined);
117 assert(0 == user->invited);
118 assert(0 == user->channel);
119
120 MyFree(user);
121 --userCount;
122 }
123}
124
125/** Find number of User structs allocated and memory used by them.
126 * @param[out] count_out Receives number of User structs allocated.
127 * @param[out] bytes_out Receives number of bytes used by User structs.
128 */
129void user_count_memory(size_t* count_out, size_t* bytes_out)
130{
131 assert(0 != count_out);
132 assert(0 != bytes_out);
133 *count_out = userCount;
134 *bytes_out = userCount * sizeof(struct User);
135}
136
137
138/** Find the next client (starting at \a next) with a name that matches \a ch.
139 * Normal usage loop is:
140 * for (x = client; x = next_client(x,mask); x = x->next)
141 * HandleMatchingClient;
142 *
143 * @param[in] next First client to check.
144 * @param[in] ch Name mask to check against.
145 * @return Next matching client found, or NULL if none.
146 */
147struct Client *next_client(struct Client *next, const char* ch)
148{
149 struct Client *tmp = next;
150
151 if (!tmp)
152 return NULL;
153
154 next = FindClient(ch);
155 next = next ? next : tmp;
156 if (cli_prev(tmp) == next)
157 return NULL;
158 if (next != tmp)
159 return next;
160 for (; next; next = cli_next(next))
161 if (!match(ch, cli_name(next)))
162 break;
163 return next;
164}
165
166/** Find the destination server for a command, and forward it if that is not us.
167 *
168 * \a server may be a nickname, server name, server mask (if \a from
169 * is a local user) or server numnick (if \a is a server or remote
170 * user).
171 *
172 * @param[in] from Client that sent the command to us.
173 * @param[in] cmd Long-form command text.
174 * @param[in] tok Token-form command text.
175 * @param[in] one Client that originated the command (ignored).
176 * @param[in] MustBeOper If non-zero and \a from is not an operator, return HUNTED_NOSUCH.
177 * @param[in] pattern Format string of arguments to command.
178 * @param[in] server Index of target name or mask in \a parv.
179 * @param[in] parc Number of valid elements in \a parv (must be less than 9).
180 * @param[in] parv Array of arguments to command.
181 * @return One of HUNTED_ISME, HUNTED_NOSUCH or HUNTED_PASS.
182 */
183int hunt_server_cmd(struct Client *from, const char *cmd, const char *tok,
184 struct Client *one, int MustBeOper, const char *pattern,
185 int server, int parc, char *parv[])
186{
187 struct Client *acptr;
188 char *to;
189
190 /* Assume it's me, if no server or an unregistered client */
191 if (parc <= server || EmptyString((to = parv[server])) || IsUnknown(from))
192 return (HUNTED_ISME);
193
194 if (MustBeOper && !IsPrivileged(from))
195 {
196 send_reply(from, ERR_NOPRIVILEGES);
197 return HUNTED_NOSUCH;
198 }
199
200 /* Make sure it's a server */
201 if (MyUser(from)) {
202 /* Make sure it's a server */
203 if (!strchr(to, '*')) {
204 if (0 == (acptr = FindClient(to))) {
205 send_reply(from, ERR_NOSUCHSERVER, to);
206 return HUNTED_NOSUCH;
207 }
208
209 if (cli_user(acptr))
210 acptr = cli_user(acptr)->server;
211 } else if (!(acptr = find_match_server(to))) {
212 send_reply(from, ERR_NOSUCHSERVER, to);
213 return (HUNTED_NOSUCH);
214 }
215 } else if (!(acptr = FindNServer(to))) {
216 send_reply(from, SND_EXPLICIT | ERR_NOSUCHSERVER, "* :Server has disconnected");
217 return (HUNTED_NOSUCH); /* Server broke off in the meantime */
218 }
219
220 if (IsMe(acptr))
221 return (HUNTED_ISME);
222
223 if (MustBeOper && !IsPrivileged(from)) {
224 send_reply(from, ERR_NOPRIVILEGES);
225 return HUNTED_NOSUCH;
226 }
227
228 /* assert(!IsServer(from)); */
229
230 parv[server] = (char *) acptr; /* HACK! HACK! HACK! ARGH! */
231
232 sendcmdto_one(from, cmd, tok, acptr, pattern, parv[1], parv[2], parv[3],
233 parv[4], parv[5], parv[6], parv[7], parv[8]);
234
235 return (HUNTED_PASS);
236}
237
238/** Find the destination server for a command, and forward it (as a
239 * high-priority command) if that is not us.
240 *
241 * \a server may be a nickname, server name, server mask (if \a from
242 * is a local user) or server numnick (if \a is a server or remote
243 * user).
244 * Unlike hunt_server_cmd(), this appends the message to the
245 * high-priority message queue for the destination server.
246 *
247 * @param[in] from Client that sent the command to us.
248 * @param[in] cmd Long-form command text.
249 * @param[in] tok Token-form command text.
250 * @param[in] one Client that originated the command (ignored).
251 * @param[in] MustBeOper If non-zero and \a from is not an operator, return HUNTED_NOSUCH.
252 * @param[in] pattern Format string of arguments to command.
253 * @param[in] server Index of target name or mask in \a parv.
254 * @param[in] parc Number of valid elements in \a parv (must be less than 9).
255 * @param[in] parv Array of arguments to command.
256 * @return One of HUNTED_ISME, HUNTED_NOSUCH or HUNTED_PASS.
257 */
258int hunt_server_prio_cmd(struct Client *from, const char *cmd, const char *tok,
259 struct Client *one, int MustBeOper,
260 const char *pattern, int server, int parc,
261 char *parv[])
262{
263 struct Client *acptr;
264 char *to;
265
266 /* Assume it's me, if no server or an unregistered client */
267 if (parc <= server || EmptyString((to = parv[server])) || IsUnknown(from))
268 return (HUNTED_ISME);
269
270 /* Make sure it's a server */
271 if (MyUser(from)) {
272 /* Make sure it's a server */
273 if (!strchr(to, '*')) {
274 if (0 == (acptr = FindClient(to))) {
275 send_reply(from, ERR_NOSUCHSERVER, to);
276 return HUNTED_NOSUCH;
277 }
278
279 if (cli_user(acptr))
280 acptr = cli_user(acptr)->server;
281 } else if (!(acptr = find_match_server(to))) {
282 send_reply(from, ERR_NOSUCHSERVER, to);
283 return (HUNTED_NOSUCH);
284 }
285 } else if (!(acptr = FindNServer(to)))
286 return (HUNTED_NOSUCH); /* Server broke off in the meantime */
287
288 if (IsMe(acptr))
289 return (HUNTED_ISME);
290
291 if (MustBeOper && !IsPrivileged(from)) {
292 send_reply(from, ERR_NOPRIVILEGES);
293 return HUNTED_NOSUCH;
294 }
295
296 /* assert(!IsServer(from)); SETTIME to particular destinations permitted */
297
298 parv[server] = (char *) acptr; /* HACK! HACK! HACK! ARGH! */
299
300 sendcmdto_prio_one(from, cmd, tok, acptr, pattern, parv[1], parv[2], parv[3],
301 parv[4], parv[5], parv[6], parv[7], parv[8]);
302
303 return (HUNTED_PASS);
304}
305
306
307/** Copy a cleaned-up version of a username.
308 * Replace all instances of '~' and "invalid" username characters
309 * (!isIrcUi()) with underscores, truncating at USERLEN or the first
310 * control character. \a dest and \a source may be the same buffer.
311 * @param[out] dest Destination buffer.
312 * @param[in] source Source buffer.
313 * @param[in] tilde If non-zero, prepend a '~' to \a dest.
314 */
315static char *clean_user_id(char *dest, char *source, int tilde)
316{
317 char ch;
318 char *d = dest;
319 char *s = source;
320 int rlen = USERLEN;
321
322 ch = *s++; /* Store first character to copy: */
323 if (tilde)
324 {
325 *d++ = '~'; /* If `dest' == `source', then this overwrites `ch' */
326 --rlen;
327 }
328 while (ch && !IsCntrl(ch) && rlen--)
329 {
330 char nch = *s++; /* Store next character to copy */
331 *d++ = IsUserChar(ch) ? ch : '_'; /* This possibly overwrites it */
332 if (nch == '~')
333 ch = '_';
334 else
335 ch = nch;
336 }
337 *d = 0;
338 return dest;
339}
340
341/*
342 * register_user
343 *
344 * This function is called when both NICK and USER messages
345 * have been accepted for the client, in whatever order. Only
346 * after this the USER message is propagated.
347 *
348 * NICK's must be propagated at once when received, although
349 * it would be better to delay them too until full info is
350 * available. Doing it is not so simple though, would have
351 * to implement the following:
352 *
353 * 1) user telnets in and gives only "NICK foobar" and waits
354 * 2) another user far away logs in normally with the nick
355 * "foobar" (quite legal, as this server didn't propagate it).
356 * 3) now this server gets nick "foobar" from outside, but
357 * has already the same defined locally. Current server
358 * would just issue "KILL foobar" to clean out dups. But,
359 * this is not fair. It should actually request another
360 * nick from local user or kill him/her...
361 */
362/** Finish registering a user who has sent both NICK and USER.
363 * For local connections, possibly check IAuth; make sure there is a
364 * matching Client config block; clean the username field; check
365 * K/k-lines; check for "hacked" looking usernames; assign a numnick;
366 * and send greeting (WELCOME, ISUPPORT, MOTD, etc).
367 * For all connections, update the invisible user and operator counts;
368 * run IPcheck against their address; and forward the NICK.
369 *
370 * @param[in] cptr Client who introduced the user.
371 * @param[in,out] sptr Client who has been fully introduced.
372 * @param[in] nick Client's new nickname.
373 * @param[in] username Client's username.
374 * @return Zero or CPTR_KILLED.
375 */
376int register_user(struct Client *cptr, struct Client *sptr,
377 const char *nick, char *username)
378{
379 struct ConfItem* aconf;
380 char* parv[4];
381 char* tmpstr;
382 char* tmpstr2;
383 char c = 0; /* not alphanum */
384 char d = 'a'; /* not a digit */
385 short upper = 0;
386 short lower = 0;
387 short pos = 0;
388 short leadcaps = 0;
389 short other = 0;
390 short digits = 0;
391 short badid = 0;
392 short digitgroups = 0;
393 struct User* user = cli_user(sptr);
394 int killreason;
395 char ip_base64[25];
396
397 user->last = CurrentTime;
398 parv[0] = cli_name(sptr);
399 parv[1] = parv[2] = NULL;
400
401 if (MyConnect(sptr))
402 {
403 static time_t last_too_many1;
404 static time_t last_too_many2;
405
406 assert(cptr == sptr);
407 assert(cli_unreg(sptr) == 0);
408 if (!IsIAuthed(sptr)) {
409 if (iauth_active)
410 return iauth_start_client(iauth_active, sptr);
411 else
412 SetIAuthed(sptr);
413 }
414 switch (conf_check_client(sptr))
415 {
416 case ACR_OK:
417 break;
418 case ACR_NO_AUTHORIZATION:
419 sendto_opmask_butone(0, SNO_UNAUTH, "Unauthorized connection from %s.",
420 get_client_name(sptr, HIDE_IP));
421 ++ServerStats->is_ref;
422 return exit_client(cptr, sptr, &me,
423 "No Authorization - use another server");
424 case ACR_TOO_MANY_IN_CLASS:
425 if (CurrentTime - last_too_many1 >= (time_t) 60)
426 {
427 last_too_many1 = CurrentTime;
428 sendto_opmask_butone(0, SNO_TOOMANY, "Too many connections in "
429 "class %s for %s.", get_client_class(sptr),
430 get_client_name(sptr, SHOW_IP));
431 }
432 ++ServerStats->is_ref;
433 IPcheck_connect_fail(sptr);
434 return exit_client(cptr, sptr, &me,
435 "Sorry, your connection class is full - try "
436 "again later or try another server");
437 case ACR_TOO_MANY_FROM_IP:
438 if (CurrentTime - last_too_many2 >= (time_t) 60)
439 {
440 last_too_many2 = CurrentTime;
441 sendto_opmask_butone(0, SNO_TOOMANY, "Too many connections from "
442 "same IP for %s.",
443 get_client_name(sptr, SHOW_IP));
444 }
445 ++ServerStats->is_ref;
446 return exit_client(cptr, sptr, &me,
447 "Too many connections from your host");
448 case ACR_ALREADY_AUTHORIZED:
449 /* Can this ever happen? */
450 case ACR_BAD_SOCKET:
451 ++ServerStats->is_ref;
452 IPcheck_connect_fail(sptr);
453 return exit_client(cptr, sptr, &me, "Unknown error -- Try again");
454 }
455 ircd_strncpy(user->host, cli_sockhost(sptr), HOSTLEN);
456 ircd_strncpy(user->realhost, cli_sockhost(sptr), HOSTLEN);
457 aconf = cli_confs(sptr)->value.aconf;
458
459 clean_user_id(user->username,
460 HasFlag(sptr, FLAG_GOTID) ? cli_username(sptr) : username,
461 HasFlag(sptr, FLAG_DOID) && !HasFlag(sptr, FLAG_GOTID));
462
463 if ((user->username[0] == '\0')
464 || ((user->username[0] == '~') && (user->username[1] == '\000')))
465 return exit_client(cptr, sptr, &me, "USER: Bogus userid.");
466
467 if (!EmptyString(aconf->passwd)
468 && strcmp(cli_passwd(sptr), aconf->passwd))
469 {
470 ServerStats->is_ref++;
471 send_reply(sptr, ERR_PASSWDMISMATCH);
472 return exit_client(cptr, sptr, &me, "Bad Password");
473 }
474 memset(cli_passwd(sptr), 0, sizeof(cli_passwd(sptr)));
475 /*
476 * following block for the benefit of time-dependent K:-lines
477 */
8b897318 478 killreason = find_kill(sptr, 1);
189935b1 479 if (killreason) {
480 ServerStats->is_ref++;
481 return exit_client(cptr, sptr, &me,
482 (killreason == -1 ? "K-lined" : "G-lined"));
483 }
484 /*
485 * Check for mixed case usernames, meaning probably hacked. Jon2 3-94
486 * Summary of rules now implemented in this patch: Ensor 11-94
487 * In a mixed-case name, if first char is upper, one more upper may
488 * appear anywhere. (A mixed-case name *must* have an upper first
489 * char, and may have one other upper.)
490 * A third upper may appear if all 3 appear at the beginning of the
491 * name, separated only by "others" (-/_/.).
492 * A single group of digits is allowed anywhere.
493 * Two groups of digits are allowed if at least one of the groups is
494 * at the beginning or the end.
495 * Only one '-', '_', or '.' is allowed (or two, if not consecutive).
496 * But not as the first or last char.
497 * No other special characters are allowed.
498 * Name must contain at least one letter.
499 */
500 tmpstr2 = tmpstr = (username[0] == '~' ? &username[1] : username);
501 while (*tmpstr && !badid)
502 {
503 pos++;
504 c = *tmpstr;
505 tmpstr++;
506 if (IsLower(c))
507 {
508 lower++;
509 }
510 else if (IsUpper(c))
511 {
512 upper++;
513 if ((leadcaps || pos == 1) && !lower && !digits)
514 leadcaps++;
515 }
516 else if (IsDigit(c))
517 {
518 digits++;
519 if (pos == 1 || !IsDigit(d))
520 {
521 digitgroups++;
522 if (digitgroups > 2)
523 badid = 1;
524 }
525 }
526 else if (c == '-' || c == '_' || c == '.')
527 {
528 other++;
529 if (pos == 1)
530 badid = 1;
531 else if (d == '-' || d == '_' || d == '.' || other > 2)
532 badid = 1;
533 }
534 else
535 badid = 1;
536 d = c;
537 }
538 if (!badid)
539 {
540 if (lower && upper && (!leadcaps || leadcaps > 3 ||
541 (upper > 2 && upper > leadcaps)))
542 badid = 1;
543 else if (digitgroups == 2 && !(IsDigit(tmpstr2[0]) || IsDigit(c)))
544 badid = 1;
545 else if ((!lower && !upper) || !IsAlnum(c))
546 badid = 1;
547 }
548 if (badid && (!HasFlag(sptr, FLAG_GOTID) ||
549 strcmp(cli_username(sptr), username) != 0))
550 {
551 ServerStats->is_ref++;
552
553 send_reply(cptr, SND_EXPLICIT | ERR_INVALIDUSERNAME,
554 ":Your username is invalid.");
555 send_reply(cptr, SND_EXPLICIT | ERR_INVALIDUSERNAME,
556 ":Connect with your real username, in lowercase.");
557 send_reply(cptr, SND_EXPLICIT | ERR_INVALIDUSERNAME,
558 ":If your mail address were foo@bar.com, your username "
559 "would be foo.");
560 return exit_client(cptr, sptr, &me, "USER: Bad username");
561 }
562 Count_unknownbecomesclient(sptr, UserStats);
563
fc04776e 564 if (MyConnect(sptr) && feature_bool(FEAT_AUTOINVISIBLE))
565 SetInvisible(sptr);
566
189935b1 567 SetUser(sptr);
568 cli_handler(sptr) = CLIENT_HANDLER;
569 SetLocalNumNick(sptr);
570 send_reply(sptr,
571 RPL_WELCOME,
572 feature_str(FEAT_NETWORK),
573 feature_str(FEAT_PROVIDER) ? " via " : "",
574 feature_str(FEAT_PROVIDER) ? feature_str(FEAT_PROVIDER) : "",
575 nick);
576 /*
577 * This is a duplicate of the NOTICE but see below...
578 */
579 send_reply(sptr, RPL_YOURHOST, cli_name(&me), version);
580 send_reply(sptr, RPL_CREATED, creation);
581 send_reply(sptr, RPL_MYINFO, cli_name(&me), version, infousermodes,
582 infochanmodes, infochanmodeswithparams);
583 send_supported(sptr);
584 m_lusers(sptr, sptr, 1, parv);
585 update_load();
586 motd_signon(sptr);
587 if (cli_snomask(sptr) & SNO_NOISY)
588 set_snomask(sptr, cli_snomask(sptr) & SNO_NOISY, SNO_ADD);
589 if (feature_bool(FEAT_CONNEXIT_NOTICES))
590 sendto_opmask_butone(0, SNO_CONNEXIT,
591 "Client connecting: %s (%s@%s) [%s] {%s} [%s] <%s%s>",
592 cli_name(sptr), user->username, user->host,
593 cli_sock_ip(sptr), get_client_class(sptr),
594 cli_info(sptr), NumNick(cptr) /* two %s's */);
595
596 IPcheck_connect_succeeded(sptr);
597 /*
598 * Set user's initial modes
599 */
600 tmpstr = (char*)client_get_default_umode(sptr);
601 if (tmpstr) for (; *tmpstr; ++tmpstr) {
602 switch (*tmpstr) {
603 case 's':
604 if (!feature_bool(FEAT_HIS_SNOTICES_OPER_ONLY)) {
605 SetServNotice(sptr);
606 set_snomask(sptr, SNO_DEFAULT, SNO_SET);
607 }
608 break;
609 case 'w':
610 if (!feature_bool(FEAT_WALLOPS_OPER_ONLY))
611 SetWallops(sptr);
612 break;
613 case 'i':
614 SetInvisible(sptr);
615 break;
616 case 'd':
617 SetDeaf(sptr);
618 break;
619 case 'g':
620 if (!feature_bool(FEAT_HIS_DEBUG_OPER_ONLY))
621 SetDebug(sptr);
622 break;
623 }
624 }
625 }
626 else {
627 struct Client *acptr;
628
629 ircd_strncpy(user->username, username, USERLEN);
630 Count_newremoteclient(UserStats, user->server);
631
632 acptr = user->server;
633 if (cli_from(acptr) != cli_from(sptr))
634 {
635 sendcmdto_one(&me, CMD_KILL, cptr, "%C :%s (%s != %s[%s])",
636 sptr, cli_name(&me), cli_name(user->server), cli_name(cli_from(acptr)),
637 cli_sockhost(cli_from(acptr)));
638 SetFlag(sptr, FLAG_KILLED);
639 return exit_client(cptr, sptr, &me, "NICK server wrong direction");
640 }
641 else if (HasFlag(acptr, FLAG_TS8))
642 SetFlag(sptr, FLAG_TS8);
643
644 /*
645 * Check to see if this user is being propagated
646 * as part of a net.burst, or is using protocol 9.
647 * FIXME: This can be sped up - its stupid to check it for
648 * every NICK message in a burst again --Run.
649 */
650 for (; acptr != &me; acptr = cli_serv(acptr)->up)
651 {
652 if (IsBurst(acptr) || Protocol(acptr) < 10)
653 break;
654 }
655 if (!IPcheck_remote_connect(sptr, (acptr != &me)))
656 {
657 /*
658 * We ran out of bits to count this
659 */
660 sendcmdto_one(&me, CMD_KILL, sptr, "%C :%s (Too many connections from your host -- Ghost)",
661 sptr, cli_name(&me));
662 return exit_client(cptr, sptr, &me,"Too many connections from your host -- throttled");
663 }
664 SetUser(sptr);
665 }
666
667 if (IsInvisible(sptr))
668 ++UserStats.inv_clients;
669 if (IsOper(sptr))
670 ++UserStats.opers;
671
672 tmpstr = umode_str(sptr);
673 /* Send full IP address to IPv6-grokking servers. */
674 sendcmdto_flag_serv_butone(user->server, CMD_NICK, cptr,
675 FLAG_IPV6, FLAG_LAST_FLAG,
676 "%s %d %Tu %s %s %s%s%s%s %s%s :%s",
677 nick, cli_hopcount(sptr) + 1, cli_lastnick(sptr),
678 user->username, user->realhost,
679 *tmpstr ? "+" : "", tmpstr, *tmpstr ? " " : "",
680 iptobase64(ip_base64, &cli_ip(sptr), sizeof(ip_base64), 1),
681 NumNick(sptr), cli_info(sptr));
682 /* Send fake IPv6 addresses to pre-IPv6 servers. */
683 sendcmdto_flag_serv_butone(user->server, CMD_NICK, cptr,
684 FLAG_LAST_FLAG, FLAG_IPV6,
685 "%s %d %Tu %s %s %s%s%s%s %s%s :%s",
686 nick, cli_hopcount(sptr) + 1, cli_lastnick(sptr),
687 user->username, user->realhost,
688 *tmpstr ? "+" : "", tmpstr, *tmpstr ? " " : "",
689 iptobase64(ip_base64, &cli_ip(sptr), sizeof(ip_base64), 0),
690 NumNick(sptr), cli_info(sptr));
691
692 /* Send user mode to client */
693 if (MyUser(sptr))
694 {
695 static struct Flags flags; /* automatically initialized to zeros */
696 send_umode(cptr, sptr, &flags, ALL_UMODES);
697 if ((cli_snomask(sptr) != SNO_DEFAULT) && HasFlag(sptr, FLAG_SERVNOTICE))
698 send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr));
699 }
700 return 0;
701}
702
703/** List of user mode characters. */
704static const struct UserMode {
705 unsigned int flag; /**< User mode constant. */
706 char c; /**< Character corresponding to the mode. */
707} userModeList[] = {
708 { FLAG_OPER, 'o' },
709 { FLAG_LOCOP, 'O' },
710 { FLAG_INVISIBLE, 'i' },
711 { FLAG_WALLOP, 'w' },
712 { FLAG_SERVNOTICE, 's' },
713 { FLAG_DEAF, 'd' },
714 { FLAG_CHSERV, 'k' },
715 { FLAG_DEBUG, 'g' },
716 { FLAG_ACCOUNT, 'r' },
08d23967 717 { FLAG_HIDDENHOST, 'x' },
943fcf70 718 { FLAG_ACCOUNTONLY, 'R' },
719 { FLAG_XTRAOP, 'X' },
720 { FLAG_NOCHAN, 'n' },
721 { FLAG_NOIDLE, 'I' }
189935b1 722};
723
724/** Length of #userModeList. */
725#define USERMODELIST_SIZE sizeof(userModeList) / sizeof(struct UserMode)
726
727/*
728 * XXX - find a way to get rid of this
729 */
730/** Nasty global buffer used for communications with umode_str() and others. */
731static char umodeBuf[BUFSIZE];
732
733/** Try to set a user's nickname.
734 * If \a sptr is a server, the client is being introduced for the first time.
735 * @param[in] cptr Client to set nickname.
736 * @param[in] sptr Client sending the NICK.
737 * @param[in] nick New nickname.
738 * @param[in] parc Number of arguments to NICK.
739 * @param[in] parv Argument list to NICK.
740 * @return CPTR_KILLED if \a cptr was killed, else 0.
741 */
742int set_nick_name(struct Client* cptr, struct Client* sptr,
743 const char* nick, int parc, char* parv[])
744{
745 if (IsServer(sptr)) {
746 int i;
747 const char* account = 0;
748 const char* p;
749
750 /*
751 * A server introducing a new client, change source
752 */
753 struct Client* new_client = make_client(cptr, STAT_UNKNOWN);
754 assert(0 != new_client);
755
756 cli_hopcount(new_client) = atoi(parv[2]);
757 cli_lastnick(new_client) = atoi(parv[3]);
758 if (Protocol(cptr) > 9 && parc > 7 && *parv[6] == '+')
759 {
760 for (p = parv[6] + 1; *p; p++)
761 {
762 for (i = 0; i < USERMODELIST_SIZE; ++i)
763 {
764 if (userModeList[i].c == *p)
765 {
766 SetFlag(new_client, userModeList[i].flag);
767 if (userModeList[i].flag == FLAG_ACCOUNT)
768 account = parv[7];
769 break;
770 }
771 }
772 }
773 }
774 client_set_privs(new_client, NULL); /* set privs on user */
775 /*
776 * Set new nick name.
777 */
778 strcpy(cli_name(new_client), nick);
779 cli_user(new_client) = make_user(new_client);
780 cli_user(new_client)->server = sptr;
781 SetRemoteNumNick(new_client, parv[parc - 2]);
782 /*
783 * IP# of remote client
784 */
785 base64toip(parv[parc - 3], &cli_ip(new_client));
786
787 add_client_to_list(new_client);
788 hAddClient(new_client);
789
790 cli_serv(sptr)->ghost = 0; /* :server NICK means end of net.burst */
791 ircd_strncpy(cli_username(new_client), parv[4], USERLEN);
792 ircd_strncpy(cli_user(new_client)->host, parv[5], HOSTLEN);
793 ircd_strncpy(cli_user(new_client)->realhost, parv[5], HOSTLEN);
794 ircd_strncpy(cli_info(new_client), parv[parc - 1], REALLEN);
795 if (account) {
796 int len = ACCOUNTLEN;
797 if ((p = strchr(account, ':'))) {
798 len = (p++) - account;
799 cli_user(new_client)->acc_create = atoi(p);
800 Debug((DEBUG_DEBUG, "Received timestamped account in user mode; "
801 "account \"%s\", timestamp %Tu", account,
802 cli_user(new_client)->acc_create));
803 }
804 ircd_strncpy(cli_user(new_client)->account, account, len);
805 }
806 if (HasHiddenHost(new_client))
807 ircd_snprintf(0, cli_user(new_client)->host, HOSTLEN, "%s.%s",
808 account, feature_str(FEAT_HIDDEN_HOST));
809
810 return register_user(cptr, new_client, cli_name(new_client), parv[4]);
811 }
812 else if ((cli_name(sptr))[0]) {
813 /*
814 * Client changing its nick
815 *
816 * If the client belongs to me, then check to see
817 * if client is on any channels where it is currently
818 * banned. If so, do not allow the nick change to occur.
819 */
820 if (MyUser(sptr)) {
821 const char* channel_name;
822 struct Membership *member;
943fcf70 823 if ((channel_name = find_no_nickchange_channel(sptr)) && !IsXtraOp(sptr)) {
189935b1 824 return send_reply(cptr, ERR_BANNICKCHANGE, channel_name);
825 }
826 /*
827 * Refuse nick change if the last nick change was less
828 * then 30 seconds ago. This is intended to get rid of
829 * clone bots doing NICK FLOOD. -SeKs
830 * If someone didn't change their nick for more then 60 seconds
831 * however, allow to do two nick changes immediately after another
832 * before limiting the nick flood. -Run
833 */
834 if (CurrentTime < cli_nextnick(cptr))
835 {
836 cli_nextnick(cptr) += 2;
837 send_reply(cptr, ERR_NICKTOOFAST, parv[1],
838 cli_nextnick(cptr) - CurrentTime);
839 /* Send error message */
840 sendcmdto_one(cptr, CMD_NICK, cptr, "%s", cli_name(cptr));
841 /* bounce NICK to user */
842 return 0; /* ignore nick change! */
843 }
844 else {
845 /* Limit total to 1 change per NICK_DELAY seconds: */
846 cli_nextnick(cptr) += NICK_DELAY;
847 /* However allow _maximal_ 1 extra consecutive nick change: */
848 if (cli_nextnick(cptr) < CurrentTime)
849 cli_nextnick(cptr) = CurrentTime;
850 }
851 /* Invalidate all bans against the user so we check them again */
852 for (member = (cli_user(cptr))->channel; member;
853 member = member->next_channel)
854 ClearBanValid(member);
855 }
856 /*
857 * Also set 'lastnick' to current time, if changed.
858 */
859 if (0 != ircd_strcmp(parv[0], nick))
860 cli_lastnick(sptr) = (sptr == cptr) ? TStime() : atoi(parv[2]);
861
862 /*
863 * Client just changing his/her nick. If he/she is
864 * on a channel, send note of change to all clients
865 * on that channel. Propagate notice to other servers.
866 */
867 if (IsUser(sptr)) {
868 sendcmdto_common_channels_butone(sptr, CMD_NICK, NULL, ":%s", nick);
869 add_history(sptr, 1);
870 sendcmdto_serv_butone(sptr, CMD_NICK, cptr, "%s %Tu", nick,
871 cli_lastnick(sptr));
872 }
873 else
874 sendcmdto_one(sptr, CMD_NICK, sptr, ":%s", nick);
875
876 if ((cli_name(sptr))[0])
877 hRemClient(sptr);
878 strcpy(cli_name(sptr), nick);
879 hAddClient(sptr);
880 }
881 else {
882 /* Local client setting NICK the first time */
883
884 strcpy(cli_name(sptr), nick);
885 if (!cli_user(sptr)) {
886 cli_user(sptr) = make_user(sptr);
887 cli_user(sptr)->server = &me;
888 }
889 hAddClient(sptr);
890
891 cli_unreg(sptr) &= ~CLIREG_NICK; /* nickname now set */
892
893 /*
894 * If the client hasn't gotten a cookie-ping yet,
895 * choose a cookie and send it. -record!jegelhof@cloud9.net
896 */
897 if (!cli_cookie(sptr)) {
898 do {
899 cli_cookie(sptr) = (ircrandom() & 0x7fffffff);
900 } while (!cli_cookie(sptr));
901 sendrawto_one(cptr, MSG_PING " :%u", cli_cookie(sptr));
902 }
903 else if (!cli_unreg(sptr)) {
904 /*
905 * USER and PONG already received, now we have NICK.
906 * register_user may reject the client and call exit_client
907 * for it - must test this and exit m_nick too !
908 */
909 cli_lastnick(sptr) = TStime(); /* Always local client */
910 if (register_user(cptr, sptr, nick, cli_user(sptr)->username) == CPTR_KILLED)
911 return CPTR_KILLED;
912 }
913 }
914 return 0;
915}
916
917/** Calculate the hash value for a target.
918 * @param[in] target Pointer to target, cast to unsigned int.
919 * @return Hash value constructed from the pointer.
920 */
921static unsigned char hash_target(unsigned int target)
922{
923 return (unsigned char) (target >> 16) ^ (target >> 8);
924}
925
926/** Records \a target as a recent target for \a sptr.
927 * @param[in] sptr User who has sent to a new target.
928 * @param[in] target Target to add.
929 */
930void
931add_target(struct Client *sptr, void *target)
932{
933 /* Ok, this shouldn't work esp on alpha
934 */
935 unsigned char hash = hash_target((unsigned long) target);
936 unsigned char* targets;
937 int i;
938 assert(0 != sptr);
939 assert(cli_local(sptr));
940
941 targets = cli_targets(sptr);
942
943 /*
944 * Already in table?
945 */
946 for (i = 0; i < MAXTARGETS; ++i) {
947 if (targets[i] == hash)
948 return;
949 }
950 /*
951 * New target
952 */
953 memmove(&targets[RESERVEDTARGETS + 1],
954 &targets[RESERVEDTARGETS], MAXTARGETS - RESERVEDTARGETS - 1);
955 targets[RESERVEDTARGETS] = hash;
956}
957
958/** Check whether \a sptr can send to or join \a target yet.
959 * @param[in] sptr User trying to join a channel or send a message.
960 * @param[in] target Target of the join or message.
961 * @param[in] name Name of the target.
962 * @param[in] created If non-zero, trying to join a new channel.
963 * @return Non-zero if too many target changes; zero if okay to send.
964 */
965int check_target_limit(struct Client *sptr, void *target, const char *name,
966 int created)
967{
968 unsigned char hash = hash_target((unsigned long) target);
969 int i;
970 unsigned char* targets;
971
972 assert(0 != sptr);
973 assert(cli_local(sptr));
974 targets = cli_targets(sptr);
975
976 /* If user is invited to channel, give him/her a free target */
977 if (IsChannelName(name) && IsInvited(sptr, target))
978 return 0;
979
980 /*
981 * Same target as last time?
982 */
983 if (targets[0] == hash)
984 return 0;
985 for (i = 1; i < MAXTARGETS; ++i) {
986 if (targets[i] == hash) {
987 memmove(&targets[1], &targets[0], i);
988 targets[0] = hash;
989 return 0;
990 }
991 }
992 /*
993 * New target
994 */
995 if (!created) {
996 if (CurrentTime < cli_nexttarget(sptr)) {
997 if (cli_nexttarget(sptr) - CurrentTime < TARGET_DELAY + 8) {
998 /*
999 * No server flooding
1000 */
1001 cli_nexttarget(sptr) += 2;
1002 send_reply(sptr, ERR_TARGETTOOFAST, name,
1003 cli_nexttarget(sptr) - CurrentTime);
1004 }
1005 return 1;
1006 }
1007 else {
1008 cli_nexttarget(sptr) += TARGET_DELAY;
1009 if (cli_nexttarget(sptr) < CurrentTime - (TARGET_DELAY * (MAXTARGETS - 1)))
1010 cli_nexttarget(sptr) = CurrentTime - (TARGET_DELAY * (MAXTARGETS - 1));
1011 }
1012 }
1013 memmove(&targets[1], &targets[0], MAXTARGETS - 1);
1014 targets[0] = hash;
1015 return 0;
1016}
1017
1018/** Allows a channel operator to avoid target change checks when
1019 * sending messages to users on their channel.
1020 * @param[in] source User sending the message.
1021 * @param[in] nick Destination of the message.
1022 * @param[in] channel Name of channel being sent to.
1023 * @param[in] text Message to send.
1024 * @param[in] is_notice If non-zero, use CNOTICE instead of CPRIVMSG.
1025 */
1026/* Added 971023 by Run. */
1027int whisper(struct Client* source, const char* nick, const char* channel,
1028 const char* text, int is_notice)
1029{
1030 struct Client* dest;
1031 struct Channel* chptr;
1032 struct Membership* membership;
1033
1034 assert(0 != source);
1035 assert(0 != nick);
1036 assert(0 != channel);
1037 assert(MyUser(source));
1038
1039 if (!(dest = FindUser(nick))) {
1040 return send_reply(source, ERR_NOSUCHNICK, nick);
1041 }
1042 if (!(chptr = FindChannel(channel))) {
1043 return send_reply(source, ERR_NOSUCHCHANNEL, channel);
1044 }
1045 /*
1046 * compare both users channel lists, instead of the channels user list
1047 * since the link is the same, this should be a little faster for channels
1048 * with a lot of users
1049 */
1050 for (membership = cli_user(source)->channel; membership; membership = membership->next_channel) {
1051 if (chptr == membership->channel)
1052 break;
1053 }
1054 if (0 == membership) {
1055 return send_reply(source, ERR_NOTONCHANNEL, chptr->chname);
1056 }
1057 if (!IsVoicedOrOpped(membership)) {
1058 return send_reply(source, ERR_VOICENEEDED, chptr->chname);
1059 }
1060 /*
1061 * lookup channel in destination
1062 */
1063 assert(0 != cli_user(dest));
1064 for (membership = cli_user(dest)->channel; membership; membership = membership->next_channel) {
1065 if (chptr == membership->channel)
1066 break;
1067 }
1068 if (0 == membership || IsZombie(membership)) {
1069 return send_reply(source, ERR_USERNOTINCHANNEL, cli_name(dest), chptr->chname);
1070 }
1071 if (is_silenced(source, dest))
1072 return 0;
1073
1074 if (cli_user(dest)->away)
1075 send_reply(source, RPL_AWAY, cli_name(dest), cli_user(dest)->away);
1076 if (is_notice)
1077 sendcmdto_one(source, CMD_NOTICE, dest, "%C :%s", dest, text);
1078 else
1079 sendcmdto_one(source, CMD_PRIVATE, dest, "%C :%s", dest, text);
1080 return 0;
1081}
1082
1083
1084/** Send a user mode change for \a cptr to neighboring servers.
1085 * @param[in] cptr User whose mode is changing.
1086 * @param[in] sptr Client who sent us the mode change message.
1087 * @param[in] old Prior set of user flags.
1088 * @param[in] prop If non-zero, also include FLAG_OPER.
1089 */
1090void send_umode_out(struct Client *cptr, struct Client *sptr,
1091 struct Flags *old, int prop)
1092{
1093 int i;
1094 struct Client *acptr;
1095
1096 send_umode(NULL, sptr, old, prop ? SEND_UMODES : SEND_UMODES_BUT_OPER);
1097
1098 for (i = HighestFd; i >= 0; i--)
1099 {
1100 if ((acptr = LocalClientArray[i]) && IsServer(acptr) &&
1101 (acptr != cptr) && (acptr != sptr) && *umodeBuf)
1102 sendcmdto_one(sptr, CMD_MODE, acptr, "%s :%s", cli_name(sptr), umodeBuf);
1103 }
1104 if (cptr && MyUser(cptr))
1105 send_umode(cptr, sptr, old, ALL_UMODES);
1106}
1107
1108
1109/** Call \a fmt for each Client named in \a names.
1110 * @param[in] sptr Client requesting information.
1111 * @param[in] names Space-delimited list of nicknames.
1112 * @param[in] rpl Base reply string for messages.
1113 * @param[in] fmt Formatting callback function.
1114 */
1115void send_user_info(struct Client* sptr, char* names, int rpl, InfoFormatter fmt)
1116{
1117 char* name;
1118 char* p = 0;
1119 int arg_count = 0;
1120 int users_found = 0;
1121 struct Client* acptr;
1122 struct MsgBuf* mb;
1123
1124 assert(0 != sptr);
1125 assert(0 != names);
1126 assert(0 != fmt);
1127
1128 mb = msgq_make(sptr, rpl_str(rpl), cli_name(&me), cli_name(sptr));
1129
1130 for (name = ircd_strtok(&p, names, " "); name; name = ircd_strtok(&p, 0, " ")) {
1131 if ((acptr = FindUser(name))) {
1132 if (users_found++)
1133 msgq_append(0, mb, " ");
1134 (*fmt)(acptr, sptr, mb);
1135 }
1136 if (5 == ++arg_count)
1137 break;
1138 }
1139 send_buffer(sptr, mb, 0);
1140 msgq_clean(mb);
1141}
1142
1143/** Set \a flag on \a cptr and possibly hide the client's hostmask.
1144 * @param[in,out] cptr User who is getting a new flag.
1145 * @param[in] flag Some flag that affects host-hiding (FLAG_HIDDENHOST, FLAG_ACCOUNT).
1146 * @return Zero.
1147 */
1148int
1149hide_hostmask(struct Client *cptr, unsigned int flag)
1150{
1151 struct Membership *chan;
1152
1153 switch (flag) {
1154 case FLAG_HIDDENHOST:
1155 /* Local users cannot set +x unless FEAT_HOST_HIDING is true. */
1156 if (MyConnect(cptr) && !feature_bool(FEAT_HOST_HIDING))
1157 return 0;
1158 break;
1159 case FLAG_ACCOUNT:
1160 /* Invalidate all bans against the user so we check them again */
1161 for (chan = (cli_user(cptr))->channel; chan;
1162 chan = chan->next_channel)
1163 ClearBanValid(chan);
1164 break;
1165 default:
1166 return 0;
1167 }
1168
1169 SetFlag(cptr, flag);
1170 if (!HasFlag(cptr, FLAG_HIDDENHOST) || !HasFlag(cptr, FLAG_ACCOUNT))
1171 return 0;
1172
1173 sendcmdto_common_channels_butone(cptr, CMD_QUIT, cptr, ":Registered");
1174 ircd_snprintf(0, cli_user(cptr)->host, HOSTLEN, "%s.%s",
1175 cli_user(cptr)->account, feature_str(FEAT_HIDDEN_HOST));
1176
1177 /* ok, the client is now fully hidden, so let them know -- hikari */
1178 if (MyConnect(cptr))
1179 send_reply(cptr, RPL_HOSTHIDDEN, cli_user(cptr)->host);
1180
1181 /*
1182 * Go through all channels the client was on, rejoin him
1183 * and set the modes, if any
1184 */
1185 for (chan = cli_user(cptr)->channel; chan; chan = chan->next_channel)
1186 {
1187 if (IsZombie(chan))
1188 continue;
1189 /* Send a JOIN unless the user's join has been delayed. */
1190 if (!IsDelayedJoin(chan))
1191 sendcmdto_channel_butserv_butone(cptr, CMD_JOIN, chan->channel, cptr, 0,
1192 "%H", chan->channel);
1193 if (IsChanOp(chan) && HasVoice(chan))
1194 sendcmdto_channel_butserv_butone(&his, CMD_MODE, chan->channel, cptr, 0,
1195 "%H +ov %C %C", chan->channel, cptr,
1196 cptr);
1197 else if (IsChanOp(chan) || HasVoice(chan))
1198 sendcmdto_channel_butserv_butone(&his, CMD_MODE, chan->channel, cptr, 0,
1199 "%H +%c %C", chan->channel, IsChanOp(chan) ? 'o' : 'v', cptr);
1200 }
1201 return 0;
1202}
1203
1204/** Set a user's mode. This function checks that \a cptr is trying to
1205 * set his own mode, prevents local users from setting inappropriate
1206 * modes through this function, and applies any other side effects of
1207 * a successful mode change.
1208 *
1209 * @param[in,out] cptr User setting someone's mode.
1210 * @param[in] sptr Client who sent the mode change message.
1211 * @param[in] parc Number of parameters in \a parv.
1212 * @param[in] parv Parameters to MODE.
1213 * @return Zero.
1214 */
1215int set_user_mode(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
1216{
1217 char** p;
1218 char* m;
1219 struct Client *acptr;
1220 int what;
1221 int i;
1222 struct Flags setflags;
1223 unsigned int tmpmask = 0;
1224 int snomask_given = 0;
1225 char buf[BUFSIZE];
1226 int prop = 0;
1227 int do_host_hiding = 0;
1228
1229 what = MODE_ADD;
1230
1231 if (parc < 2)
1232 return need_more_params(sptr, "MODE");
1233
1234 if (!(acptr = FindUser(parv[1])))
1235 {
1236 if (MyConnect(sptr))
1237 send_reply(sptr, ERR_NOSUCHCHANNEL, parv[1]);
1238 return 0;
1239 }
1240
1241 if (IsServer(sptr) || sptr != acptr)
1242 {
1243 if (IsServer(cptr))
1244 sendwallto_group_butone(&me, WALL_WALLOPS, 0,
1245 "MODE for User %s from %s!%s", parv[1],
1246 cli_name(cptr), cli_name(sptr));
1247 else
1248 send_reply(sptr, ERR_USERSDONTMATCH);
1249 return 0;
1250 }
1251
1252 if (parc < 3)
1253 {
1254 m = buf;
1255 *m++ = '+';
1256 for (i = 0; i < USERMODELIST_SIZE; i++)
1257 {
1258 if (HasFlag(sptr, userModeList[i].flag) &&
1259 userModeList[i].flag != FLAG_ACCOUNT)
1260 *m++ = userModeList[i].c;
1261 }
1262 *m = '\0';
1263 send_reply(sptr, RPL_UMODEIS, buf);
1264 if (HasFlag(sptr, FLAG_SERVNOTICE) && MyConnect(sptr)
1265 && cli_snomask(sptr) !=
1266 (unsigned int)(IsOper(sptr) ? SNO_OPERDEFAULT : SNO_DEFAULT))
1267 send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr));
1268 return 0;
1269 }
1270
1271 /*
1272 * find flags already set for user
1273 * why not just copy them?
1274 */
1275 setflags = cli_flags(sptr);
1276
1277 if (MyConnect(sptr))
1278 tmpmask = cli_snomask(sptr);
1279
1280 /*
1281 * parse mode change string(s)
1282 */
1283 for (p = &parv[2]; *p; p++) { /* p is changed in loop too */
1284 for (m = *p; *m; m++) {
1285 switch (*m) {
1286 case '+':
1287 what = MODE_ADD;
1288 break;
1289 case '-':
1290 what = MODE_DEL;
1291 break;
1292 case 's':
1293 if (*(p + 1) && is_snomask(*(p + 1))) {
1294 snomask_given = 1;
1295 tmpmask = umode_make_snomask(tmpmask, *++p, what);
1296 tmpmask &= (IsAnOper(sptr) ? SNO_ALL : SNO_USER);
1297 }
1298 else
1299 tmpmask = (what == MODE_ADD) ?
1300 (IsAnOper(sptr) ? SNO_OPERDEFAULT : SNO_DEFAULT) : 0;
1301 if (tmpmask)
1302 SetServNotice(sptr);
1303 else
1304 ClearServNotice(sptr);
1305 break;
1306 case 'w':
1307 if (what == MODE_ADD)
1308 SetWallops(sptr);
1309 else
1310 ClearWallops(sptr);
1311 break;
1312 case 'o':
1313 if (what == MODE_ADD)
1314 SetOper(sptr);
1315 else {
1316 ClrFlag(sptr, FLAG_OPER);
1317 ClrFlag(sptr, FLAG_LOCOP);
1318 if (MyConnect(sptr))
1319 {
1320 tmpmask = cli_snomask(sptr) & ~SNO_OPER;
1321 cli_handler(sptr) = CLIENT_HANDLER;
1322 }
1323 }
1324 break;
1325 case 'O':
1326 if (what == MODE_ADD)
1327 SetLocOp(sptr);
1328 else
1329 {
1330 ClrFlag(sptr, FLAG_OPER);
1331 ClrFlag(sptr, FLAG_LOCOP);
1332 if (MyConnect(sptr))
1333 {
1334 tmpmask = cli_snomask(sptr) & ~SNO_OPER;
1335 cli_handler(sptr) = CLIENT_HANDLER;
1336 }
1337 }
1338 break;
1339 case 'i':
1340 if (what == MODE_ADD)
1341 SetInvisible(sptr);
1342 else
fc04776e 1343 if (!feature_bool(FEAT_AUTOINVISIBLE) || IsOper(sptr)) /* Don't allow non-opers to -i if FEAT_AUTOINVISIBLE is set */
1344 ClearInvisible(sptr);
189935b1 1345 break;
1346 case 'd':
1347 if (what == MODE_ADD)
1348 SetDeaf(sptr);
1349 else
1350 ClearDeaf(sptr);
1351 break;
1352 case 'k':
1353 if (what == MODE_ADD)
1354 SetChannelService(sptr);
1355 else
1356 ClearChannelService(sptr);
1357 break;
943fcf70 1358 case 'X':
1359 if (what == MODE_ADD)
1360 SetXtraOp(sptr);
1361 else
1362 ClearXtraOp(sptr);
1363 break;
1364 case 'n':
1365 if (what == MODE_ADD)
1366 SetNoChan(sptr);
1367 else
1368 ClearNoChan(sptr);
1369 break;
1370 case 'I':
1371 if (what == MODE_ADD)
1372 SetNoIdle(sptr);
1373 else
1374 ClearNoIdle(sptr);
1375 break;
189935b1 1376 case 'g':
1377 if (what == MODE_ADD)
1378 SetDebug(sptr);
1379 else
1380 ClearDebug(sptr);
1381 break;
1382 case 'x':
1383 if (what == MODE_ADD)
1384 do_host_hiding = 1;
1385 break;
08d23967 1386 case 'R':
1387 if (what == MODE_ADD)
1388 SetAccountOnly(sptr);
1389 else
1390 ClearAccountOnly(sptr);
1391 break;
189935b1 1392 default:
1393 send_reply(sptr, ERR_UMODEUNKNOWNFLAG, *m);
1394 break;
1395 }
1396 }
1397 }
1398 /*
1399 * Evaluate rules for new user mode
1400 * Stop users making themselves operators too easily:
1401 */
1402 if (!IsServer(cptr))
1403 {
1404 if (!FlagHas(&setflags, FLAG_OPER) && IsOper(sptr))
1405 ClearOper(sptr);
1406 if (!FlagHas(&setflags, FLAG_LOCOP) && IsLocOp(sptr))
1407 ClearLocOp(sptr);
1408 /*
1409 * new umode; servers can set it, local users cannot;
1410 * prevents users from /kick'ing or /mode -o'ing
1411 */
943fcf70 1412 if (!FlagHas(&setflags, FLAG_CHSERV) && !IsOper(sptr))
189935b1 1413 ClearChannelService(sptr);
943fcf70 1414 if (!FlagHas(&setflags, FLAG_XTRAOP) && !IsOper(sptr))
1415 ClearXtraOp(sptr);
1416 if (!FlagHas(&setflags, FLAG_NOCHAN) && !(IsOper(sptr) || feature_bool(FEAT_USER_HIDECHANS)))
1417 ClearNoChan(sptr);
1418 if (!FlagHas(&setflags, FLAG_NOIDLE) && !IsOper(sptr))
1419 ClearNoIdle(sptr);
1420
189935b1 1421 /*
1422 * only send wallops to opers
1423 */
1424 if (feature_bool(FEAT_WALLOPS_OPER_ONLY) && !IsAnOper(sptr) &&
1425 !FlagHas(&setflags, FLAG_WALLOP))
1426 ClearWallops(sptr);
1427 if (feature_bool(FEAT_HIS_SNOTICES_OPER_ONLY) && MyConnect(sptr) &&
1428 !IsAnOper(sptr) && !FlagHas(&setflags, FLAG_SERVNOTICE))
1429 {
1430 ClearServNotice(sptr);
1431 set_snomask(sptr, 0, SNO_SET);
1432 }
1433 if (feature_bool(FEAT_HIS_DEBUG_OPER_ONLY) &&
1434 !IsAnOper(sptr) && !FlagHas(&setflags, FLAG_DEBUG))
1435 ClearDebug(sptr);
1436 }
1437 if (MyConnect(sptr))
1438 {
1439 if ((FlagHas(&setflags, FLAG_OPER) || FlagHas(&setflags, FLAG_LOCOP)) &&
1440 !IsAnOper(sptr))
1441 det_confs_butmask(sptr, CONF_CLIENT & ~CONF_OPERATOR);
1442
1443 if (SendServNotice(sptr))
1444 {
1445 if (tmpmask != cli_snomask(sptr))
1446 set_snomask(sptr, tmpmask, SNO_SET);
1447 if (cli_snomask(sptr) && snomask_given)
1448 send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr));
1449 }
1450 else
1451 set_snomask(sptr, 0, SNO_SET);
1452 }
1453 /*
1454 * Compare new flags with old flags and send string which
1455 * will cause servers to update correctly.
1456 */
1457 if (!FlagHas(&setflags, FLAG_OPER) && IsOper(sptr))
1458 {
1459 /* user now oper */
1460 ++UserStats.opers;
1461 client_set_privs(sptr, NULL); /* may set propagate privilege */
1462 }
1463 /* remember propagate privilege setting */
1464 if (HasPriv(sptr, PRIV_PROPAGATE))
1465 prop = 1;
1466 if (FlagHas(&setflags, FLAG_OPER) && !IsOper(sptr))
1467 {
1468 /* user no longer oper */
1469 --UserStats.opers;
1470 client_set_privs(sptr, NULL); /* will clear propagate privilege */
1471 }
1472 if (FlagHas(&setflags, FLAG_INVISIBLE) && !IsInvisible(sptr))
1473 --UserStats.inv_clients;
1474 if (!FlagHas(&setflags, FLAG_INVISIBLE) && IsInvisible(sptr))
1475 ++UserStats.inv_clients;
1476 if (!FlagHas(&setflags, FLAG_HIDDENHOST) && do_host_hiding)
1477 hide_hostmask(sptr, FLAG_HIDDENHOST);
1478 send_umode_out(cptr, sptr, &setflags, prop);
1479
1480 return 0;
1481}
1482
1483/** Build a mode string to describe modes for \a cptr.
1484 * @param[in] cptr Some user.
1485 * @return Pointer to a static buffer.
1486 */
1487char *umode_str(struct Client *cptr)
1488{
1489 /* Maximum string size: "owidgrx\0" */
1490 char *m = umodeBuf;
1491 int i;
1492 struct Flags c_flags = cli_flags(cptr);
1493
1494 if (!HasPriv(cptr, PRIV_PROPAGATE))
1495 FlagClr(&c_flags, FLAG_OPER);
1496
1497 for (i = 0; i < USERMODELIST_SIZE; ++i)
1498 {
1499 if (FlagHas(&c_flags, userModeList[i].flag) &&
1500 userModeList[i].flag >= FLAG_GLOBAL_UMODES)
1501 *m++ = userModeList[i].c;
1502 }
1503
1504 if (IsAccount(cptr))
1505 {
1506 char* t = cli_user(cptr)->account;
1507
1508 *m++ = ' ';
1509 while ((*m++ = *t++))
1510 ; /* Empty loop */
1511
1512 if (cli_user(cptr)->acc_create) {
1513 char nbuf[20];
1514 Debug((DEBUG_DEBUG, "Sending timestamped account in user mode for "
1515 "account \"%s\"; timestamp %Tu", cli_user(cptr)->account,
1516 cli_user(cptr)->acc_create));
1517 ircd_snprintf(0, t = nbuf, sizeof(nbuf), ":%Tu",
1518 cli_user(cptr)->acc_create);
1519 m--; /* back up over previous nul-termination */
1520 while ((*m++ = *t++))
1521 ; /* Empty loop */
1522 }
1523 }
1524
1525 *m = '\0';
1526
1527 return umodeBuf; /* Note: static buffer, gets
1528 overwritten by send_umode() */
1529}
1530
1531/** Send a mode change string for \a sptr to \a cptr.
1532 * @param[in] cptr Destination of mode change message.
1533 * @param[in] sptr User whose mode has changed.
1534 * @param[in] old Pre-change set of modes for \a sptr.
1535 * @param[in] sendset One of ALL_UMODES, SEND_UMODES_BUT_OPER,
1536 * SEND_UMODES, to select which changed user modes to send.
1537 */
1538void send_umode(struct Client *cptr, struct Client *sptr, struct Flags *old,
1539 int sendset)
1540{
1541 int i;
1542 int flag;
1543 char *m;
1544 int what = MODE_NULL;
1545
1546 /*
1547 * Build a string in umodeBuf to represent the change in the user's
1548 * mode between the new (cli_flags(sptr)) and 'old', but skipping
1549 * the modes indicated by sendset.
1550 */
1551 m = umodeBuf;
1552 *m = '\0';
1553 for (i = 0; i < USERMODELIST_SIZE; ++i)
1554 {
1555 flag = userModeList[i].flag;
1556 if (FlagHas(old, flag)
1557 == HasFlag(sptr, flag))
1558 continue;
1559 switch (sendset)
1560 {
1561 case ALL_UMODES:
1562 break;
1563 case SEND_UMODES_BUT_OPER:
1564 if (flag == FLAG_OPER)
1565 continue;
1566 /* and fall through */
1567 case SEND_UMODES:
1568 if (flag < FLAG_GLOBAL_UMODES)
1569 continue;
1570 break;
1571 }
1572 if (FlagHas(old, flag))
1573 {
1574 if (what == MODE_DEL)
1575 *m++ = userModeList[i].c;
1576 else
1577 {
1578 what = MODE_DEL;
1579 *m++ = '-';
1580 *m++ = userModeList[i].c;
1581 }
1582 }
1583 else /* !FlagHas(old, flag) */
1584 {
1585 if (what == MODE_ADD)
1586 *m++ = userModeList[i].c;
1587 else
1588 {
1589 what = MODE_ADD;
1590 *m++ = '+';
1591 *m++ = userModeList[i].c;
1592 }
1593 }
1594 }
1595 *m = '\0';
1596 if (*umodeBuf && cptr)
1597 sendcmdto_one(sptr, CMD_MODE, cptr, "%s :%s", cli_name(sptr), umodeBuf);
1598}
1599
1600/**
1601 * Check to see if this resembles a sno_mask. It is if 1) there is
1602 * at least one digit and 2) The first digit occurs before the first
1603 * alphabetic character.
1604 * @param[in] word Word to check for sno_mask-ness.
1605 * @return Non-zero if \a word looks like a server notice mask; zero if not.
1606 */
1607int is_snomask(char *word)
1608{
1609 if (word)
1610 {
1611 for (; *word; word++)
1612 if (IsDigit(*word))
1613 return 1;
1614 else if (IsAlpha(*word))
1615 return 0;
1616 }
1617 return 0;
1618}
1619
1620/** Update snomask \a oldmask according to \a arg and \a what.
1621 * @param[in] oldmask Original user mask.
1622 * @param[in] arg Update string (either a number or '+'/'-' followed by a number).
1623 * @param[in] what MODE_ADD if adding the mask.
1624 * @return New value of service notice mask.
1625 */
1626unsigned int umode_make_snomask(unsigned int oldmask, char *arg, int what)
1627{
1628 unsigned int sno_what;
1629 unsigned int newmask;
1630 if (*arg == '+')
1631 {
1632 arg++;
1633 if (what == MODE_ADD)
1634 sno_what = SNO_ADD;
1635 else
1636 sno_what = SNO_DEL;
1637 }
1638 else if (*arg == '-')
1639 {
1640 arg++;
1641 if (what == MODE_ADD)
1642 sno_what = SNO_DEL;
1643 else
1644 sno_what = SNO_ADD;
1645 }
1646 else
1647 sno_what = (what == MODE_ADD) ? SNO_SET : SNO_DEL;
1648 /* pity we don't have strtoul everywhere */
1649 newmask = (unsigned int)atoi(arg);
1650 if (sno_what == SNO_DEL)
1651 newmask = oldmask & ~newmask;
1652 else if (sno_what == SNO_ADD)
1653 newmask |= oldmask;
1654 return newmask;
1655}
1656
1657/** Remove \a cptr from the singly linked list \a list.
1658 * @param[in] cptr Client to remove from list.
1659 * @param[in,out] list Pointer to head of list containing \a cptr.
1660 */
1661static void delfrom_list(struct Client *cptr, struct SLink **list)
1662{
1663 struct SLink* tmp;
1664 struct SLink* prv = NULL;
1665
1666 for (tmp = *list; tmp; tmp = tmp->next) {
1667 if (tmp->value.cptr == cptr) {
1668 if (prv)
1669 prv->next = tmp->next;
1670 else
1671 *list = tmp->next;
1672 free_link(tmp);
1673 break;
1674 }
1675 prv = tmp;
1676 }
1677}
1678
1679/** Set \a cptr's server notice mask, according to \a what.
1680 * @param[in,out] cptr Client whose snomask is updating.
1681 * @param[in] newmask Base value for new snomask.
1682 * @param[in] what One of SNO_ADD, SNO_DEL, SNO_SET, to choose operation.
1683 */
1684void set_snomask(struct Client *cptr, unsigned int newmask, int what)
1685{
1686 unsigned int oldmask, diffmask; /* unsigned please */
1687 int i;
1688 struct SLink *tmp;
1689
1690 oldmask = cli_snomask(cptr);
1691
1692 if (what == SNO_ADD)
1693 newmask |= oldmask;
1694 else if (what == SNO_DEL)
1695 newmask = oldmask & ~newmask;
1696 else if (what != SNO_SET) /* absolute set, no math needed */
1697 sendto_opmask_butone(0, SNO_OLDSNO, "setsnomask called with %d ?!", what);
1698
1699 newmask &= (IsAnOper(cptr) ? SNO_ALL : SNO_USER);
1700
1701 diffmask = oldmask ^ newmask;
1702
1703 for (i = 0; diffmask >> i; i++) {
1704 if (((diffmask >> i) & 1))
1705 {
1706 if (((newmask >> i) & 1))
1707 {
1708 tmp = make_link();
1709 tmp->next = opsarray[i];
1710 tmp->value.cptr = cptr;
1711 opsarray[i] = tmp;
1712 }
1713 else
1714 /* not real portable :( */
1715 delfrom_list(cptr, &opsarray[i]);
1716 }
1717 }
1718 cli_snomask(cptr) = newmask;
1719}
1720
1721/** Check whether \a sptr is allowed to send a message to \a acptr.
1722 * If \a sptr is a remote user, it means some server has an outdated
1723 * SILENCE list for \a acptr, so send the missing SILENCE mask(s) back
1724 * in the direction of \a sptr. Skip the check if \a sptr is a server.
1725 * @param[in] sptr Client trying to send a message.
1726 * @param[in] acptr Destination of message.
1727 * @return Non-zero if \a sptr is SILENCEd by \a acptr, zero if not.
1728 */
1729int is_silenced(struct Client *sptr, struct Client *acptr)
1730{
1731 struct Ban *found;
1732 struct User *user;
1733 size_t buf_used, slen;
1734 char buf[BUFSIZE];
1735
1736 if (IsServer(sptr) || !(user = cli_user(acptr))
1737 || !(found = find_ban(sptr, user->silence)))
1738 return 0;
1739 assert(!(found->flags & BAN_EXCEPTION));
1740 if (!MyConnect(sptr)) {
1741 /* Buffer positive silence to send back. */
1742 buf_used = strlen(found->banstr);
1743 memcpy(buf, found->banstr, buf_used);
1744 /* Add exceptions to buffer. */
1745 for (found = user->silence; found; found = found->next) {
1746 if (!(found->flags & BAN_EXCEPTION))
1747 continue;
1748 slen = strlen(found->banstr);
1749 if (buf_used + slen + 4 > 400) {
1750 buf[buf_used] = '\0';
1751 sendcmdto_one(acptr, CMD_SILENCE, cli_from(sptr), "%C %s", sptr, buf);
1752 buf_used = 0;
1753 }
1754 if (buf_used)
1755 buf[buf_used++] = ',';
1756 buf[buf_used++] = '+';
1757 buf[buf_used++] = '~';
1758 memcpy(buf + buf_used, found->banstr, slen);
1759 buf_used += slen;
1760 }
1761 /* Flush silence buffer. */
1762 if (buf_used) {
1763 buf[buf_used] = '\0';
1764 sendcmdto_one(acptr, CMD_SILENCE, cli_from(sptr), "%C %s", sptr, buf);
1765 buf_used = 0;
1766 }
1767 }
1768 return 1;
1769}
1770
1771/** Send RPL_ISUPPORT lines to \a cptr.
1772 * @param[in] cptr Client to send ISUPPORT to.
1773 * @return Zero.
1774 */
1775int
1776send_supported(struct Client *cptr)
1777{
1778 char featurebuf[512];
1779
1780 ircd_snprintf(0, featurebuf, sizeof(featurebuf), FEATURES1, FEATURESVALUES1);
1781 send_reply(cptr, RPL_ISUPPORT, featurebuf);
1782 ircd_snprintf(0, featurebuf, sizeof(featurebuf), FEATURES2, FEATURESVALUES2);
1783 send_reply(cptr, RPL_ISUPPORT, featurebuf);
1784
1785 return 0; /* convenience return, if it's ever needed */
1786}