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