]> jfr.im git - irc/quakenet/snircd.git/blob - ircd/s_user.c
Merged revisions 59-76 via svnmerge from
[irc/quakenet/snircd.git] / ircd / s_user.c
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.2.1 2006/01/10 02:06:36 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 static char *IsVhost(char *hostmask, int oper);
77 static char *IsVhostPass(char *hostmask);
78
79 /** Count of allocated User structures. */
80 static 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 */
87 struct 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 */
108 void 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 */
132 void 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 */
150 struct 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 */
186 int 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 */
261 int 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 */
318 static 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 */
379 int 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,
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);
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 */
485 killreason = find_kill(sptr, 1);
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
571 if (MyConnect(sptr) && feature_bool(FEAT_AUTOINVISIBLE))
572 SetInvisible(sptr);
573
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
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 }
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
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),
700 user->realusername, user->realhost,
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),
709 user->realusername, user->realhost,
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 /* To avoid sending +r to the client due to auth-on-connect, set
719 * the "old" FLAG_ACCOUNT bit to match the client's value.
720 */
721 if (IsAccount(cptr))
722 FlagSet(&flags, FLAG_ACCOUNT);
723 else
724 FlagClr(&flags, FLAG_ACCOUNT);
725 send_umode(cptr, sptr, &flags, ALL_UMODES);
726 if ((cli_snomask(sptr) != SNO_DEFAULT) && HasFlag(sptr, FLAG_SERVNOTICE))
727 send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr));
728 }
729 return 0;
730 }
731
732 /** List of user mode characters. */
733 static const struct UserMode {
734 unsigned int flag; /**< User mode constant. */
735 char c; /**< Character corresponding to the mode. */
736 } userModeList[] = {
737 { FLAG_OPER, 'o' },
738 { FLAG_LOCOP, 'O' },
739 { FLAG_INVISIBLE, 'i' },
740 { FLAG_WALLOP, 'w' },
741 { FLAG_SERVNOTICE, 's' },
742 { FLAG_DEAF, 'd' },
743 { FLAG_CHSERV, 'k' },
744 { FLAG_DEBUG, 'g' },
745 { FLAG_ACCOUNT, 'r' },
746 { FLAG_HIDDENHOST, 'x' },
747 { FLAG_ACCOUNTONLY, 'R' },
748 { FLAG_XTRAOP, 'X' },
749 { FLAG_NOCHAN, 'n' },
750 { FLAG_NOIDLE, 'I' },
751 { FLAG_SETHOST, 'h' },
752 { FLAG_PARANOID, 'P' }
753 };
754
755 /** Length of #userModeList. */
756 #define USERMODELIST_SIZE sizeof(userModeList) / sizeof(struct UserMode)
757
758 /*
759 * XXX - find a way to get rid of this
760 */
761 /** Nasty global buffer used for communications with umode_str() and others. */
762 static char umodeBuf[BUFSIZE];
763
764 /** Try to set a user's nickname.
765 * If \a sptr is a server, the client is being introduced for the first time.
766 * @param[in] cptr Client to set nickname.
767 * @param[in] sptr Client sending the NICK.
768 * @param[in] nick New nickname.
769 * @param[in] parc Number of arguments to NICK.
770 * @param[in] parv Argument list to NICK.
771 * @return CPTR_KILLED if \a cptr was killed, else 0.
772 */
773 int set_nick_name(struct Client* cptr, struct Client* sptr,
774 const char* nick, int parc, char* parv[])
775 {
776 if (IsServer(sptr)) {
777 int i;
778 const char* account = 0;
779 char* hostmask = 0;
780 char* host = 0;
781 const char* p;
782
783 /*
784 * A server introducing a new client, change source
785 */
786 struct Client* new_client = make_client(cptr, STAT_UNKNOWN);
787 assert(0 != new_client);
788
789 cli_hopcount(new_client) = atoi(parv[2]);
790 cli_lastnick(new_client) = atoi(parv[3]);
791 if (Protocol(cptr) > 9 && parc > 7 && *parv[6] == '+')
792 {
793 for (p = parv[6] + 1; *p; p++)
794 {
795 for (i = 0; i < USERMODELIST_SIZE; ++i)
796 {
797 if (userModeList[i].c == *p)
798 {
799 SetFlag(new_client, userModeList[i].flag);
800 if (userModeList[i].flag == FLAG_ACCOUNT)
801 account = parv[7];
802 if (userModeList[i].flag == FLAG_SETHOST)
803 hostmask = parv[parc - 4];
804 break;
805 }
806 }
807 }
808 }
809 client_set_privs(new_client, NULL); /* set privs on user */
810 /*
811 * Set new nick name.
812 */
813 strcpy(cli_name(new_client), nick);
814 cli_user(new_client) = make_user(new_client);
815 cli_user(new_client)->server = sptr;
816 SetRemoteNumNick(new_client, parv[parc - 2]);
817 /*
818 * IP# of remote client
819 */
820 base64toip(parv[parc - 3], &cli_ip(new_client));
821
822 add_client_to_list(new_client);
823 hAddClient(new_client);
824
825 cli_serv(sptr)->ghost = 0; /* :server NICK means end of net.burst */
826 ircd_strncpy(cli_username(new_client), parv[4], USERLEN);
827 ircd_strncpy(cli_user(new_client)->realusername, parv[4], USERLEN);
828 ircd_strncpy(cli_user(new_client)->host, parv[5], HOSTLEN);
829 ircd_strncpy(cli_user(new_client)->realhost, parv[5], HOSTLEN);
830 ircd_strncpy(cli_info(new_client), parv[parc - 1], REALLEN);
831 if (account) {
832 int len = ACCOUNTLEN;
833 if ((p = strchr(account, ':'))) {
834 len = (p++) - account;
835 cli_user(new_client)->acc_create = atoi(p);
836 Debug((DEBUG_DEBUG, "Received timestamped account in user mode; "
837 "account \"%s\", timestamp %Tu", account,
838 cli_user(new_client)->acc_create));
839 }
840 ircd_strncpy(cli_user(new_client)->account, account, len);
841 }
842 if (HasHiddenHost(new_client))
843 ircd_snprintf(0, cli_user(new_client)->host, HOSTLEN, "%s.%s",
844 account, feature_str(FEAT_HIDDEN_HOST));
845 if (HasSetHost(new_client)) {
846 if ((host = strrchr(hostmask, '@')) != NULL) {
847 *host++ = '\0';
848 ircd_strncpy(cli_username(new_client), hostmask, USERLEN);
849 ircd_strncpy(cli_user(new_client)->host, host, HOSTLEN);
850 }
851 }
852
853 return register_user(cptr, new_client, cli_name(new_client), cli_username(new_client));
854 }
855 else if ((cli_name(sptr))[0]) {
856 /*
857 * Client changing its nick
858 *
859 * If the client belongs to me, then check to see
860 * if client is on any channels where it is currently
861 * banned. If so, do not allow the nick change to occur.
862 */
863 if (MyUser(sptr)) {
864 const char* channel_name;
865 struct Membership *member;
866 if ((channel_name = find_no_nickchange_channel(sptr)) && !IsXtraOp(sptr)) {
867 return send_reply(cptr, ERR_BANNICKCHANGE, channel_name);
868 }
869 /*
870 * Refuse nick change if the last nick change was less
871 * then 30 seconds ago. This is intended to get rid of
872 * clone bots doing NICK FLOOD. -SeKs
873 * If someone didn't change their nick for more then 60 seconds
874 * however, allow to do two nick changes immediately after another
875 * before limiting the nick flood. -Run
876 */
877 if (CurrentTime < cli_nextnick(cptr))
878 {
879 cli_nextnick(cptr) += 2;
880 send_reply(cptr, ERR_NICKTOOFAST, parv[1],
881 cli_nextnick(cptr) - CurrentTime);
882 /* Send error message */
883 sendcmdto_one(cptr, CMD_NICK, cptr, "%s", cli_name(cptr));
884 /* bounce NICK to user */
885 return 0; /* ignore nick change! */
886 }
887 else {
888 /* Limit total to 1 change per NICK_DELAY seconds: */
889 cli_nextnick(cptr) += NICK_DELAY;
890 /* However allow _maximal_ 1 extra consecutive nick change: */
891 if (cli_nextnick(cptr) < CurrentTime)
892 cli_nextnick(cptr) = CurrentTime;
893 }
894 /* Invalidate all bans against the user so we check them again */
895 for (member = (cli_user(cptr))->channel; member;
896 member = member->next_channel)
897 ClearBanValid(member);
898 }
899 /*
900 * Also set 'lastnick' to current time, if changed.
901 */
902 if (0 != ircd_strcmp(parv[0], nick))
903 cli_lastnick(sptr) = (sptr == cptr) ? TStime() : atoi(parv[2]);
904
905 /*
906 * Client just changing his/her nick. If he/she is
907 * on a channel, send note of change to all clients
908 * on that channel. Propagate notice to other servers.
909 */
910 if (IsUser(sptr)) {
911 sendcmdto_common_channels_butone(sptr, CMD_NICK, NULL, ":%s", nick);
912 add_history(sptr, 1);
913 sendcmdto_serv_butone(sptr, CMD_NICK, cptr, "%s %Tu", nick,
914 cli_lastnick(sptr));
915 }
916 else
917 sendcmdto_one(sptr, CMD_NICK, sptr, ":%s", nick);
918
919 if ((cli_name(sptr))[0])
920 hRemClient(sptr);
921 strcpy(cli_name(sptr), nick);
922 hAddClient(sptr);
923 }
924 else {
925 /* Local client setting NICK the first time */
926
927 strcpy(cli_name(sptr), nick);
928 if (!cli_user(sptr)) {
929 cli_user(sptr) = make_user(sptr);
930 cli_user(sptr)->server = &me;
931 }
932 hAddClient(sptr);
933
934 cli_unreg(sptr) &= ~CLIREG_NICK; /* nickname now set */
935
936 /*
937 * If the client hasn't gotten a cookie-ping yet,
938 * choose a cookie and send it. -record!jegelhof@cloud9.net
939 */
940 if (!cli_cookie(sptr)) {
941 do {
942 cli_cookie(sptr) = (ircrandom() & 0x7fffffff);
943 } while (!cli_cookie(sptr));
944 sendrawto_one(cptr, MSG_PING " :%u", cli_cookie(sptr));
945 }
946 else if (!cli_unreg(sptr)) {
947 /*
948 * USER and PONG already received, now we have NICK.
949 * register_user may reject the client and call exit_client
950 * for it - must test this and exit m_nick too !
951 */
952 cli_lastnick(sptr) = TStime(); /* Always local client */
953 if (register_user(cptr, sptr, nick, cli_user(sptr)->username) == CPTR_KILLED)
954 return CPTR_KILLED;
955 }
956 }
957 return 0;
958 }
959
960 /** Calculate the hash value for a target.
961 * @param[in] target Pointer to target, cast to unsigned int.
962 * @return Hash value constructed from the pointer.
963 */
964 static unsigned char hash_target(unsigned int target)
965 {
966 return (unsigned char) (target >> 16) ^ (target >> 8);
967 }
968
969 /** Records \a target as a recent target for \a sptr.
970 * @param[in] sptr User who has sent to a new target.
971 * @param[in] target Target to add.
972 */
973 void
974 add_target(struct Client *sptr, void *target)
975 {
976 /* Ok, this shouldn't work esp on alpha
977 */
978 unsigned char hash = hash_target((unsigned long) target);
979 unsigned char* targets;
980 int i;
981 assert(0 != sptr);
982 assert(cli_local(sptr));
983
984 targets = cli_targets(sptr);
985
986 /*
987 * Already in table?
988 */
989 for (i = 0; i < MAXTARGETS; ++i) {
990 if (targets[i] == hash)
991 return;
992 }
993 /*
994 * New target
995 */
996 memmove(&targets[RESERVEDTARGETS + 1],
997 &targets[RESERVEDTARGETS], MAXTARGETS - RESERVEDTARGETS - 1);
998 targets[RESERVEDTARGETS] = hash;
999 }
1000
1001 /** Check whether \a sptr can send to or join \a target yet.
1002 * @param[in] sptr User trying to join a channel or send a message.
1003 * @param[in] target Target of the join or message.
1004 * @param[in] name Name of the target.
1005 * @param[in] created If non-zero, trying to join a new channel.
1006 * @return Non-zero if too many target changes; zero if okay to send.
1007 */
1008 int check_target_limit(struct Client *sptr, void *target, const char *name,
1009 int created)
1010 {
1011 unsigned char hash = hash_target((unsigned long) target);
1012 int i;
1013 unsigned char* targets;
1014
1015 assert(0 != sptr);
1016 assert(cli_local(sptr));
1017 targets = cli_targets(sptr);
1018
1019 /* If user is invited to channel, give him/her a free target */
1020 if (IsChannelName(name) && IsInvited(sptr, target))
1021 return 0;
1022
1023 /* opers always have a free target */
1024 if (IsAnOper(sptr))
1025 return 0;
1026
1027 /*
1028 * Same target as last time?
1029 */
1030 if (targets[0] == hash)
1031 return 0;
1032 for (i = 1; i < MAXTARGETS; ++i) {
1033 if (targets[i] == hash) {
1034 memmove(&targets[1], &targets[0], i);
1035 targets[0] = hash;
1036 return 0;
1037 }
1038 }
1039 /*
1040 * New target
1041 */
1042 if (!created) {
1043 if (CurrentTime < cli_nexttarget(sptr)) {
1044 if (cli_nexttarget(sptr) - CurrentTime < TARGET_DELAY + 8) {
1045 /*
1046 * No server flooding
1047 */
1048 cli_nexttarget(sptr) += 2;
1049 send_reply(sptr, ERR_TARGETTOOFAST, name,
1050 cli_nexttarget(sptr) - CurrentTime);
1051 }
1052 return 1;
1053 }
1054 else {
1055 cli_nexttarget(sptr) += TARGET_DELAY;
1056 if (cli_nexttarget(sptr) < CurrentTime - (TARGET_DELAY * (MAXTARGETS - 1)))
1057 cli_nexttarget(sptr) = CurrentTime - (TARGET_DELAY * (MAXTARGETS - 1));
1058 }
1059 }
1060 memmove(&targets[1], &targets[0], MAXTARGETS - 1);
1061 targets[0] = hash;
1062 return 0;
1063 }
1064
1065 /** Allows a channel operator to avoid target change checks when
1066 * sending messages to users on their channel.
1067 * @param[in] source User sending the message.
1068 * @param[in] nick Destination of the message.
1069 * @param[in] channel Name of channel being sent to.
1070 * @param[in] text Message to send.
1071 * @param[in] is_notice If non-zero, use CNOTICE instead of CPRIVMSG.
1072 */
1073 /* Added 971023 by Run. */
1074 int whisper(struct Client* source, const char* nick, const char* channel,
1075 const char* text, int is_notice)
1076 {
1077 struct Client* dest;
1078 struct Channel* chptr;
1079 struct Membership* membership;
1080
1081 assert(0 != source);
1082 assert(0 != nick);
1083 assert(0 != channel);
1084 assert(MyUser(source));
1085
1086 if (!(dest = FindUser(nick))) {
1087 return send_reply(source, ERR_NOSUCHNICK, nick);
1088 }
1089 if (!(chptr = FindChannel(channel))) {
1090 return send_reply(source, ERR_NOSUCHCHANNEL, channel);
1091 }
1092 /*
1093 * compare both users channel lists, instead of the channels user list
1094 * since the link is the same, this should be a little faster for channels
1095 * with a lot of users
1096 */
1097 for (membership = cli_user(source)->channel; membership; membership = membership->next_channel) {
1098 if (chptr == membership->channel)
1099 break;
1100 }
1101 if (0 == membership) {
1102 return send_reply(source, ERR_NOTONCHANNEL, chptr->chname);
1103 }
1104 if (!IsVoicedOrOpped(membership)) {
1105 return send_reply(source, ERR_VOICENEEDED, chptr->chname);
1106 }
1107 /*
1108 * lookup channel in destination
1109 */
1110 assert(0 != cli_user(dest));
1111 for (membership = cli_user(dest)->channel; membership; membership = membership->next_channel) {
1112 if (chptr == membership->channel)
1113 break;
1114 }
1115 if (0 == membership || IsZombie(membership)) {
1116 return send_reply(source, ERR_USERNOTINCHANNEL, cli_name(dest), chptr->chname);
1117 }
1118 if (is_silenced(source, dest))
1119 return 0;
1120
1121 if (cli_user(dest)->away)
1122 send_reply(source, RPL_AWAY, cli_name(dest), cli_user(dest)->away);
1123 if (is_notice)
1124 sendcmdto_one(source, CMD_NOTICE, dest, "%C :%s", dest, text);
1125 else
1126 sendcmdto_one(source, CMD_PRIVATE, dest, "%C :%s", dest, text);
1127 return 0;
1128 }
1129
1130
1131 /** Send a user mode change for \a cptr to neighboring servers.
1132 * @param[in] cptr User whose mode is changing.
1133 * @param[in] sptr Client who sent us the mode change message.
1134 * @param[in] old Prior set of user flags.
1135 * @param[in] prop If non-zero, also include FLAG_OPER.
1136 */
1137 void send_umode_out(struct Client *cptr, struct Client *sptr,
1138 struct Flags *old, int prop)
1139 {
1140 int i;
1141 struct Client *acptr;
1142
1143 send_umode(NULL, sptr, old, prop ? SEND_UMODES : SEND_UMODES_BUT_OPER);
1144
1145 for (i = HighestFd; i >= 0; i--)
1146 {
1147 if ((acptr = LocalClientArray[i]) && IsServer(acptr) &&
1148 (acptr != cptr) && (acptr != sptr) && *umodeBuf)
1149 sendcmdto_one(sptr, CMD_MODE, acptr, "%s %s", cli_name(sptr), umodeBuf);
1150 }
1151 if (cptr && MyUser(cptr))
1152 send_umode(cptr, sptr, old, ALL_UMODES);
1153 }
1154
1155
1156 /** Call \a fmt for each Client named in \a names.
1157 * @param[in] sptr Client requesting information.
1158 * @param[in] names Space-delimited list of nicknames.
1159 * @param[in] rpl Base reply string for messages.
1160 * @param[in] fmt Formatting callback function.
1161 */
1162 void send_user_info(struct Client* sptr, char* names, int rpl, InfoFormatter fmt)
1163 {
1164 char* name;
1165 char* p = 0;
1166 int arg_count = 0;
1167 int users_found = 0;
1168 struct Client* acptr;
1169 struct MsgBuf* mb;
1170
1171 assert(0 != sptr);
1172 assert(0 != names);
1173 assert(0 != fmt);
1174
1175 mb = msgq_make(sptr, rpl_str(rpl), cli_name(&me), cli_name(sptr));
1176
1177 for (name = ircd_strtok(&p, names, " "); name; name = ircd_strtok(&p, 0, " ")) {
1178 if ((acptr = FindUser(name))) {
1179 if (users_found++)
1180 msgq_append(0, mb, " ");
1181 (*fmt)(acptr, sptr, mb);
1182 }
1183 if (5 == ++arg_count)
1184 break;
1185 }
1186 send_buffer(sptr, mb, 0);
1187 msgq_clean(mb);
1188 }
1189
1190 /** Set \a flag on \a cptr and possibly hide the client's hostmask.
1191 * @param[in,out] cptr User who is getting a new flag.
1192 * @param[in] flag Some flag that affects host-hiding (FLAG_HIDDENHOST, FLAG_ACCOUNT).
1193 * @return Zero.
1194 */
1195 int
1196 hide_hostmask(struct Client *cptr, unsigned int flag)
1197 {
1198 struct Membership *chan;
1199
1200 switch (flag) {
1201 case FLAG_HIDDENHOST:
1202 /* Local users cannot set +x unless FEAT_HOST_HIDING is true. */
1203 if (MyConnect(cptr) && !feature_bool(FEAT_HOST_HIDING))
1204 return 0;
1205 /* If the user is +h, we don't hide the hostmask. Set the flag to keep sync though */
1206 if (HasSetHost(cptr)) {
1207 SetFlag(cptr, flag);
1208 return 0;
1209 }
1210 break;
1211 case FLAG_ACCOUNT:
1212 /* Invalidate all bans against the user so we check them again */
1213 for (chan = (cli_user(cptr))->channel; chan;
1214 chan = chan->next_channel)
1215 ClearBanValid(chan);
1216 break;
1217 default:
1218 return 0;
1219 }
1220
1221 SetFlag(cptr, flag);
1222 if (!HasFlag(cptr, FLAG_HIDDENHOST) || !HasFlag(cptr, FLAG_ACCOUNT))
1223 return 0;
1224
1225 sendcmdto_common_channels_butone(cptr, CMD_QUIT, cptr, ":Registered");
1226 ircd_snprintf(0, cli_user(cptr)->host, HOSTLEN, "%s.%s",
1227 cli_user(cptr)->account, feature_str(FEAT_HIDDEN_HOST));
1228
1229 /* ok, the client is now fully hidden, so let them know -- hikari */
1230 if (MyConnect(cptr))
1231 send_reply(cptr, RPL_HOSTHIDDEN, cli_user(cptr)->host);
1232
1233 /*
1234 * Go through all channels the client was on, rejoin him
1235 * and set the modes, if any
1236 */
1237 for (chan = cli_user(cptr)->channel; chan; chan = chan->next_channel)
1238 {
1239 if (IsZombie(chan))
1240 continue;
1241 /* Send a JOIN unless the user's join has been delayed. */
1242 if (!IsDelayedJoin(chan))
1243 sendcmdto_channel_butserv_butone(cptr, CMD_JOIN, chan->channel, cptr, 0,
1244 "%H", chan->channel);
1245 if (IsChanOp(chan) && HasVoice(chan))
1246 sendcmdto_channel_butserv_butone(&his, CMD_MODE, chan->channel, cptr, 0,
1247 "%H +ov %C %C", chan->channel, cptr,
1248 cptr);
1249 else if (IsChanOp(chan) || HasVoice(chan))
1250 sendcmdto_channel_butserv_butone(&his, CMD_MODE, chan->channel, cptr, 0,
1251 "%H +%c %C", chan->channel, IsChanOp(chan) ? 'o' : 'v', cptr);
1252 }
1253 return 0;
1254 }
1255
1256 /*
1257 * set_hostmask() - derived from hide_hostmask()
1258 *
1259 */
1260 int set_hostmask(struct Client *cptr, char *hostmask, char *password)
1261 {
1262 int restore = 0;
1263 int freeform = 0;
1264 char *host, *new_vhost, *vhost_pass;
1265 char hiddenhost[USERLEN + HOSTLEN + 2];
1266 struct Membership *chan;
1267
1268 Debug((DEBUG_INFO, "set_hostmask() %C, %s, %s", cptr, hostmask, password));
1269
1270 /* sethost enabled? */
1271 if (MyConnect(cptr) && !feature_bool(FEAT_SETHOST)) {
1272 send_reply(cptr, ERR_DISABLED, "SETHOST");
1273 return 0;
1274 }
1275
1276 /* sethost enabled for users? */
1277 if (MyConnect(cptr) && !IsAnOper(cptr) && !feature_bool(FEAT_SETHOST_USER)) {
1278 send_reply(cptr, ERR_NOPRIVILEGES);
1279 return 0;
1280 }
1281
1282 /* MODE_DEL: restore original hostmask */
1283 if (EmptyString(hostmask)) {
1284 /* is already sethost'ed? */
1285 if (IsSetHost(cptr)) {
1286 restore = 1;
1287 sendcmdto_common_channels_butone(cptr, CMD_QUIT, cptr, ":Host change");
1288 /* If they are +rx, we need to return to their +x host, not their "real" host */
1289 if (HasHiddenHost(cptr))
1290 ircd_snprintf(0, cli_user(cptr)->host, HOSTLEN, "%s.%s",
1291 cli_user(cptr)->account, feature_str(FEAT_HIDDEN_HOST));
1292 else
1293 strncpy(cli_user(cptr)->host, cli_user(cptr)->realhost, HOSTLEN);
1294 strncpy(cli_user(cptr)->username, cli_user(cptr)->realusername, USERLEN);
1295 /* log it */
1296 if (MyConnect(cptr))
1297 log_write(LS_SETHOST, L_INFO, LOG_NOSNOTICE,
1298 "SETHOST (%s@%s) by (%#R): restoring real hostmask",
1299 cli_user(cptr)->username, cli_user(cptr)->host, cptr);
1300 } else
1301 return 0;
1302 /* MODE_ADD: set a new hostmask */
1303 } else {
1304 /* chop up ident and host.cc */
1305 if ((host = strrchr(hostmask, '@'))) /* oper can specifiy ident@host.cc */
1306 *host++ = '\0';
1307 else /* user can only specifiy host.cc [password] */
1308 host = hostmask;
1309 /*
1310 * Oper sethost
1311 */
1312 if (MyConnect(cptr)) {
1313 if (IsAnOper(cptr)) {
1314 if ((new_vhost = IsVhost(host, 1)) == NULL) {
1315 if (!feature_bool(FEAT_SETHOST_FREEFORM)) {
1316 send_reply(cptr, ERR_HOSTUNAVAIL, hostmask);
1317 log_write(LS_SETHOST, L_INFO, LOG_NOSNOTICE,
1318 "SETHOST (%s@%s) by (%#R): no such s-line",
1319 (host != hostmask) ? hostmask : cli_user(cptr)->username, host, cptr);
1320 return 0;
1321 } else /* freeform active, log and go */
1322 freeform = 1;
1323 }
1324 sendcmdto_common_channels_butone(cptr, CMD_QUIT, cptr, ":Host change");
1325 /* set the new ident and host */
1326 if (host != hostmask) /* oper only specified host.cc */
1327 strncpy(cli_user(cptr)->username, hostmask, USERLEN);
1328 strncpy(cli_user(cptr)->host, host, HOSTLEN);
1329 /* log it */
1330 log_write(LS_SETHOST, (freeform) ? L_NOTICE : L_INFO,
1331 (freeform) ? 0 : LOG_NOSNOTICE, "SETHOST (%s@%s) by (%#R)%s",
1332 cli_user(cptr)->username, cli_user(cptr)->host, cptr,
1333 (freeform) ? ": using freeform" : "");
1334 /*
1335 * plain user sethost, handled here
1336 */
1337 } else {
1338 /* empty password? */
1339 if (EmptyString(password)) {
1340 send_reply(cptr, ERR_NEEDMOREPARAMS, "MODE");
1341 return 0;
1342 }
1343 /* no such s-line */
1344 if ((new_vhost = IsVhost(host, 0)) == NULL) {
1345 send_reply(cptr, ERR_HOSTUNAVAIL, hostmask);
1346 log_write(LS_SETHOST, L_INFO, LOG_NOSNOTICE, "SETHOST (%s@%s %s) by (%#R): no such s-line",
1347 cli_user(cptr)->username, host, password, cptr);
1348 return 0;
1349 }
1350 /* no password */
1351 if ((vhost_pass = IsVhostPass(new_vhost)) == NULL) {
1352 send_reply(cptr, ERR_PASSWDMISMATCH);
1353 log_write(LS_SETHOST, L_INFO, 0, "SETHOST (%s@%s %s) by (%#R): trying to use an oper s-line",
1354 cli_user(cptr)->username, host, password, cptr);
1355 return 0;
1356 }
1357 /* incorrect password */
1358 if (strCasediff(vhost_pass, password)) {
1359 send_reply(cptr, ERR_PASSWDMISMATCH);
1360 log_write(LS_SETHOST, L_NOTICE, 0, "SETHOST (%s@%s %s) by (%#R): incorrect password",
1361 cli_user(cptr)->username, host, password, cptr);
1362 return 0;
1363 }
1364 sendcmdto_common_channels_butone(cptr, CMD_QUIT, cptr, ":Host change");
1365 /* set the new host */
1366 strncpy(cli_user(cptr)->host, new_vhost, HOSTLEN);
1367 /* log it */
1368 log_write(LS_SETHOST, L_INFO, LOG_NOSNOTICE, "SETHOST (%s@%s) by (%#R)",
1369 cli_user(cptr)->username, cli_user(cptr)->host, cptr);
1370 }
1371 } else { /* remote user */
1372 sendcmdto_common_channels_butone(cptr, CMD_QUIT, cptr, ":Host change");
1373 if (host != hostmask) /* oper only specified host.cc */
1374 strncpy(cli_user(cptr)->username, hostmask, USERLEN);
1375 strncpy(cli_user(cptr)->host, host, HOSTLEN);
1376 }
1377 }
1378
1379 if (restore)
1380 ClearSetHost(cptr);
1381 else
1382 SetSetHost(cptr);
1383
1384 if (MyConnect(cptr)) {
1385 ircd_snprintf(0, hiddenhost, HOSTLEN + USERLEN + 2, "%s@%s",
1386 cli_user(cptr)->username, cli_user(cptr)->host);
1387 send_reply(cptr, RPL_HOSTHIDDEN, hiddenhost);
1388 }
1389
1390 #if 0
1391 /* Code copied from hide_hostmask(). This is the old (pre-delayedjoin)
1392 * version. Switch this in if you're not using the delayed join patch. */
1393 /*
1394 * Go through all channels the client was on, rejoin him
1395 * and set the modes, if any
1396 */
1397 for (chan = cli_user(cptr)->channel; chan; chan = chan->next_channel) {
1398 if (IsZombie(chan))
1399 continue;
1400 sendcmdto_channel_butserv_butone(cptr, CMD_JOIN, chan->channel, cptr,
1401 "%H", chan->channel);
1402 if (IsChanOp(chan) && HasVoice(chan)) {
1403 sendcmdto_channel_butserv_butone(&me, CMD_MODE, chan->channel, cptr,
1404 "%H +ov %C %C", chan->channel, cptr, cptr);
1405 } else if (IsChanOp(chan) || HasVoice(chan)) {
1406 sendcmdto_channel_butserv_butone(&me, CMD_MODE, chan->channel, cptr,
1407 "%H +%c %C", chan->channel, IsChanOp(chan) ? 'o' : 'v', cptr);
1408 }
1409 }
1410 #endif
1411
1412 /*
1413 * Go through all channels the client was on, rejoin him
1414 * and set the modes, if any
1415 */
1416 for (chan = cli_user(cptr)->channel; chan; chan = chan->next_channel) {
1417 if (IsZombie(chan))
1418 continue;
1419 /* If this channel has delayed joins and the user has no modes, just set
1420 * the delayed join flag rather than showing the join, even if the user
1421 * was visible before */
1422 if (!IsChanOp(chan) && !HasVoice(chan)
1423 && (chan->channel->mode.mode & MODE_DELJOINS)) {
1424 SetDelayedJoin(chan);
1425 } else {
1426 sendcmdto_channel_butserv_butone(cptr, CMD_JOIN, chan->channel, cptr, 0,
1427 "%H", chan->channel);
1428 }
1429 if (IsChanOp(chan) && HasVoice(chan)) {
1430 sendcmdto_channel_butserv_butone(&me, CMD_MODE, chan->channel, cptr, 0,
1431 "%H +ov %C %C", chan->channel, cptr, cptr);
1432 } else if (IsChanOp(chan) || HasVoice(chan)) {
1433 sendcmdto_channel_butserv_butone(&me, CMD_MODE, chan->channel, cptr, 0,
1434 "%H +%c %C", chan->channel, IsChanOp(chan) ? 'o' : 'v', cptr);
1435 }
1436 }
1437 return 1;
1438 }
1439
1440 /** Set a user's mode. This function checks that \a cptr is trying to
1441 * set his own mode, prevents local users from setting inappropriate
1442 * modes through this function, and applies any other side effects of
1443 * a successful mode change.
1444 *
1445 * @param[in,out] cptr User setting someone's mode.
1446 * @param[in] sptr Client who sent the mode change message.
1447 * @param[in] parc Number of parameters in \a parv.
1448 * @param[in] parv Parameters to MODE.
1449 * @return Zero.
1450 */
1451 int set_user_mode(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
1452 {
1453 char** p;
1454 char* m;
1455 struct Client *acptr;
1456 int what;
1457 int i;
1458 struct Flags setflags;
1459 unsigned int tmpmask = 0;
1460 int snomask_given = 0;
1461 char buf[BUFSIZE];
1462 char *hostmask, *password;
1463 int prop = 0;
1464 int do_host_hiding = 0;
1465 int do_set_host = 0;
1466
1467 hostmask = password = NULL;
1468 what = MODE_ADD;
1469
1470 if (parc < 2)
1471 return need_more_params(sptr, "MODE");
1472
1473 if (!(acptr = FindUser(parv[1])))
1474 {
1475 if (MyConnect(sptr))
1476 send_reply(sptr, ERR_NOSUCHCHANNEL, parv[1]);
1477 return 0;
1478 }
1479
1480 if (IsServer(sptr) || sptr != acptr)
1481 {
1482 if (IsServer(cptr))
1483 sendwallto_group_butone(&me, WALL_WALLOPS, 0,
1484 "MODE for User %s from %s!%s", parv[1],
1485 cli_name(cptr), cli_name(sptr));
1486 else
1487 send_reply(sptr, ERR_USERSDONTMATCH);
1488 return 0;
1489 }
1490
1491 if (parc < 3)
1492 {
1493 m = buf;
1494 *m++ = '+';
1495 for (i = 0; i < USERMODELIST_SIZE; i++)
1496 {
1497 if (HasFlag(sptr, userModeList[i].flag) &&
1498 ((userModeList[i].flag != FLAG_ACCOUNT) &&
1499 (userModeList[i].flag != FLAG_SETHOST)))
1500 *m++ = userModeList[i].c;
1501 }
1502 *m = '\0';
1503 send_reply(sptr, RPL_UMODEIS, buf);
1504 if (HasFlag(sptr, FLAG_SERVNOTICE) && MyConnect(sptr)
1505 && cli_snomask(sptr) !=
1506 (unsigned int)(IsOper(sptr) ? SNO_OPERDEFAULT : SNO_DEFAULT))
1507 send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr));
1508 return 0;
1509 }
1510
1511 /*
1512 * find flags already set for user
1513 * why not just copy them?
1514 */
1515 setflags = cli_flags(sptr);
1516
1517 if (MyConnect(sptr))
1518 tmpmask = cli_snomask(sptr);
1519
1520 /*
1521 * parse mode change string(s)
1522 */
1523 for (p = &parv[2]; *p; p++) { /* p is changed in loop too */
1524 for (m = *p; *m; m++) {
1525 switch (*m) {
1526 case '+':
1527 what = MODE_ADD;
1528 break;
1529 case '-':
1530 what = MODE_DEL;
1531 break;
1532 case 's':
1533 if (*(p + 1) && is_snomask(*(p + 1))) {
1534 snomask_given = 1;
1535 tmpmask = umode_make_snomask(tmpmask, *++p, what);
1536 tmpmask &= (IsAnOper(sptr) ? SNO_ALL : SNO_USER);
1537 }
1538 else
1539 tmpmask = (what == MODE_ADD) ?
1540 (IsAnOper(sptr) ? SNO_OPERDEFAULT : SNO_DEFAULT) : 0;
1541 if (tmpmask)
1542 SetServNotice(sptr);
1543 else
1544 ClearServNotice(sptr);
1545 break;
1546 case 'w':
1547 if (what == MODE_ADD)
1548 SetWallops(sptr);
1549 else
1550 ClearWallops(sptr);
1551 break;
1552 case 'o':
1553 if (what == MODE_ADD)
1554 SetOper(sptr);
1555 else {
1556 ClrFlag(sptr, FLAG_OPER);
1557 ClrFlag(sptr, FLAG_LOCOP);
1558 if (MyConnect(sptr))
1559 {
1560 tmpmask = cli_snomask(sptr) & ~SNO_OPER;
1561 cli_handler(sptr) = CLIENT_HANDLER;
1562 }
1563 }
1564 break;
1565 case 'O':
1566 if (what == MODE_ADD)
1567 SetLocOp(sptr);
1568 else
1569 {
1570 ClrFlag(sptr, FLAG_OPER);
1571 ClrFlag(sptr, FLAG_LOCOP);
1572 if (MyConnect(sptr))
1573 {
1574 tmpmask = cli_snomask(sptr) & ~SNO_OPER;
1575 cli_handler(sptr) = CLIENT_HANDLER;
1576 }
1577 }
1578 break;
1579 case 'i':
1580 if (what == MODE_ADD)
1581 SetInvisible(sptr);
1582 else
1583 if (!feature_bool(FEAT_AUTOINVISIBLE) || IsOper(sptr)) /* Don't allow non-opers to -i if FEAT_AUTOINVISIBLE is set */
1584 ClearInvisible(sptr);
1585 break;
1586 case 'd':
1587 if (what == MODE_ADD)
1588 SetDeaf(sptr);
1589 else
1590 ClearDeaf(sptr);
1591 break;
1592 case 'k':
1593 if (what == MODE_ADD)
1594 SetChannelService(sptr);
1595 else
1596 ClearChannelService(sptr);
1597 break;
1598 case 'X':
1599 if (what == MODE_ADD)
1600 SetXtraOp(sptr);
1601 else
1602 ClearXtraOp(sptr);
1603 break;
1604 case 'n':
1605 if (what == MODE_ADD)
1606 SetNoChan(sptr);
1607 else
1608 ClearNoChan(sptr);
1609 break;
1610 case 'I':
1611 if (what == MODE_ADD)
1612 SetNoIdle(sptr);
1613 else
1614 ClearNoIdle(sptr);
1615 break;
1616 case 'g':
1617 if (what == MODE_ADD)
1618 SetDebug(sptr);
1619 else
1620 ClearDebug(sptr);
1621 break;
1622 case 'x':
1623 if (what == MODE_ADD)
1624 do_host_hiding = 1;
1625 break;
1626 case 'h':
1627 if (what == MODE_ADD) {
1628 if (*(p + 1) && is_hostmask(*(p + 1))) {
1629 do_set_host = 1;
1630 hostmask = *++p;
1631 /* DON'T step p onto the trailing NULL in the parameter array! - splidge */
1632 if (*(p+1))
1633 password = *++p;
1634 else
1635 password = NULL;
1636 } else {
1637 if (!*(p+1))
1638 send_reply(sptr, ERR_NEEDMOREPARAMS, "SETHOST");
1639 else {
1640 send_reply(sptr, ERR_BADHOSTMASK, *(p+1));
1641 p++; /* Swallow the arg anyway */
1642 }
1643 }
1644 } else { /* MODE_DEL */
1645 do_set_host = 1;
1646 hostmask = NULL;
1647 password = NULL;
1648 }
1649 break;
1650 case 'R':
1651 if (what == MODE_ADD)
1652 SetAccountOnly(sptr);
1653 else
1654 ClearAccountOnly(sptr);
1655 break;
1656 case 'P':
1657 if (what == MODE_ADD)
1658 SetParanoid(sptr);
1659 else
1660 ClearParanoid(sptr);
1661 break;
1662 default:
1663 send_reply(sptr, ERR_UMODEUNKNOWNFLAG, *m);
1664 break;
1665 }
1666 }
1667 }
1668 /*
1669 * Evaluate rules for new user mode
1670 * Stop users making themselves operators too easily:
1671 */
1672 if (!IsServer(cptr))
1673 {
1674 if (!FlagHas(&setflags, FLAG_OPER) && IsOper(sptr))
1675 ClearOper(sptr);
1676 if (!FlagHas(&setflags, FLAG_LOCOP) && IsLocOp(sptr))
1677 ClearLocOp(sptr);
1678 /*
1679 * new umode; servers can set it, local users cannot;
1680 * prevents users from /kick'ing or /mode -o'ing
1681 */
1682 if (!FlagHas(&setflags, FLAG_CHSERV) && !IsOper(sptr))
1683 ClearChannelService(sptr);
1684 if (!FlagHas(&setflags, FLAG_XTRAOP) && !IsOper(sptr))
1685 ClearXtraOp(sptr);
1686 if (!FlagHas(&setflags, FLAG_NOCHAN) && !(IsOper(sptr) || feature_bool(FEAT_USER_HIDECHANS)))
1687 ClearNoChan(sptr);
1688 if (!FlagHas(&setflags, FLAG_NOIDLE) && !IsOper(sptr))
1689 ClearNoIdle(sptr);
1690 if (!FlagHas(&setflags, FLAG_PARANOID) && !IsOper(sptr))
1691 ClearParanoid(sptr);
1692
1693 /*
1694 * only send wallops to opers
1695 */
1696 if (feature_bool(FEAT_WALLOPS_OPER_ONLY) && !IsAnOper(sptr) &&
1697 !FlagHas(&setflags, FLAG_WALLOP))
1698 ClearWallops(sptr);
1699 if (feature_bool(FEAT_HIS_SNOTICES_OPER_ONLY) && MyConnect(sptr) &&
1700 !IsAnOper(sptr) && !FlagHas(&setflags, FLAG_SERVNOTICE))
1701 {
1702 ClearServNotice(sptr);
1703 set_snomask(sptr, 0, SNO_SET);
1704 }
1705 if (feature_bool(FEAT_HIS_DEBUG_OPER_ONLY) &&
1706 !IsAnOper(sptr) && !FlagHas(&setflags, FLAG_DEBUG))
1707 ClearDebug(sptr);
1708 }
1709 if (MyConnect(sptr))
1710 {
1711 if ((FlagHas(&setflags, FLAG_OPER) || FlagHas(&setflags, FLAG_LOCOP)) &&
1712 !IsAnOper(sptr))
1713 det_confs_butmask(sptr, CONF_CLIENT & ~CONF_OPERATOR);
1714
1715 if (SendServNotice(sptr))
1716 {
1717 if (tmpmask != cli_snomask(sptr))
1718 set_snomask(sptr, tmpmask, SNO_SET);
1719 if (cli_snomask(sptr) && snomask_given)
1720 send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr));
1721 }
1722 else
1723 set_snomask(sptr, 0, SNO_SET);
1724 }
1725 /*
1726 * Compare new flags with old flags and send string which
1727 * will cause servers to update correctly.
1728 */
1729 if (!FlagHas(&setflags, FLAG_OPER) && IsOper(sptr))
1730 {
1731 /* user now oper */
1732 ++UserStats.opers;
1733 client_set_privs(sptr, NULL); /* may set propagate privilege */
1734 }
1735 /* remember propagate privilege setting */
1736 if (HasPriv(sptr, PRIV_PROPAGATE))
1737 prop = 1;
1738 if (FlagHas(&setflags, FLAG_OPER) && !IsOper(sptr))
1739 {
1740 /* user no longer oper */
1741 --UserStats.opers;
1742 client_set_privs(sptr, NULL); /* will clear propagate privilege */
1743 }
1744 if (FlagHas(&setflags, FLAG_INVISIBLE) && !IsInvisible(sptr))
1745 --UserStats.inv_clients;
1746 if (!FlagHas(&setflags, FLAG_INVISIBLE) && IsInvisible(sptr))
1747 ++UserStats.inv_clients;
1748 if (!FlagHas(&setflags, FLAG_HIDDENHOST) && do_host_hiding)
1749 hide_hostmask(sptr, FLAG_HIDDENHOST);
1750 if (do_set_host) {
1751 /* We clear the flag in the old mask, so that the +h will be sent */
1752 /* Only do this if we're SETTING +h and it succeeded */
1753 if (set_hostmask(sptr, hostmask, password) && hostmask)
1754 FlagClr(&setflags, FLAG_SETHOST);
1755 }
1756 send_umode_out(cptr, sptr, &setflags, prop);
1757
1758 return 0;
1759 }
1760
1761 /** Build a mode string to describe modes for \a cptr.
1762 * @param[in] cptr Some user.
1763 * @return Pointer to a static buffer.
1764 */
1765 char *umode_str(struct Client *cptr)
1766 {
1767 /* Maximum string size: "owidgrx\0" */
1768 char *m = umodeBuf;
1769 int i;
1770 struct Flags c_flags = cli_flags(cptr);
1771
1772 if (!HasPriv(cptr, PRIV_PROPAGATE))
1773 FlagClr(&c_flags, FLAG_OPER);
1774
1775 for (i = 0; i < USERMODELIST_SIZE; ++i)
1776 {
1777 if (FlagHas(&c_flags, userModeList[i].flag) &&
1778 userModeList[i].flag >= FLAG_GLOBAL_UMODES)
1779 *m++ = userModeList[i].c;
1780 }
1781
1782 if (IsAccount(cptr))
1783 {
1784 char* t = cli_user(cptr)->account;
1785
1786 *m++ = ' ';
1787 while ((*m++ = *t++))
1788 ; /* Empty loop */
1789
1790 if (cli_user(cptr)->acc_create) {
1791 char nbuf[20];
1792 Debug((DEBUG_DEBUG, "Sending timestamped account in user mode for "
1793 "account \"%s\"; timestamp %Tu", cli_user(cptr)->account,
1794 cli_user(cptr)->acc_create));
1795 ircd_snprintf(0, t = nbuf, sizeof(nbuf), ":%Tu",
1796 cli_user(cptr)->acc_create);
1797 m--; /* back up over previous nul-termination */
1798 while ((*m++ = *t++))
1799 ; /* Empty loop */
1800 }
1801 m--; /* Step back over the '\0' */
1802 }
1803
1804 if (IsSetHost(cptr)) {
1805 *m++ = ' ';
1806 ircd_snprintf(0, m, USERLEN + HOSTLEN + 2, "%s@%s", cli_user(cptr)->username,
1807 cli_user(cptr)->host);
1808 } else
1809 *m = '\0';
1810 return umodeBuf; /* Note: static buffer, gets
1811 overwritten by send_umode() */
1812 }
1813
1814 /** Send a mode change string for \a sptr to \a cptr.
1815 * @param[in] cptr Destination of mode change message.
1816 * @param[in] sptr User whose mode has changed.
1817 * @param[in] old Pre-change set of modes for \a sptr.
1818 * @param[in] sendset One of ALL_UMODES, SEND_UMODES_BUT_OPER,
1819 * SEND_UMODES, to select which changed user modes to send.
1820 */
1821 void send_umode(struct Client *cptr, struct Client *sptr, struct Flags *old,
1822 int sendset)
1823 {
1824 int i;
1825 int flag;
1826 int needhost = 0;
1827 char *m;
1828 int what = MODE_NULL;
1829
1830 /*
1831 * Build a string in umodeBuf to represent the change in the user's
1832 * mode between the new (cli_flags(sptr)) and 'old', but skipping
1833 * the modes indicated by sendset.
1834 */
1835 m = umodeBuf;
1836 *m = '\0';
1837 for (i = 0; i < USERMODELIST_SIZE; ++i)
1838 {
1839 flag = userModeList[i].flag;
1840 if (FlagHas(old, flag)
1841 == HasFlag(sptr, flag))
1842 continue;
1843 switch (sendset)
1844 {
1845 case ALL_UMODES:
1846 break;
1847 case SEND_UMODES_BUT_OPER:
1848 if (flag == FLAG_OPER)
1849 continue;
1850 /* and fall through */
1851 case SEND_UMODES:
1852 if (flag < FLAG_GLOBAL_UMODES)
1853 continue;
1854 break;
1855 }
1856 /* Special case for SETHOST.. */
1857 if (flag == FLAG_SETHOST) {
1858 /* Don't send to users */
1859 if (cptr && MyUser(cptr))
1860 continue;
1861
1862 /* If we're setting +h, add the parameter later */
1863 if (!FlagHas(old, flag))
1864 needhost++;
1865 }
1866 if (FlagHas(old, flag))
1867 {
1868 if (what == MODE_DEL)
1869 *m++ = userModeList[i].c;
1870 else
1871 {
1872 what = MODE_DEL;
1873 *m++ = '-';
1874 *m++ = userModeList[i].c;
1875 }
1876 }
1877 else /* !FlagHas(old, flag) */
1878 {
1879 if (what == MODE_ADD)
1880 *m++ = userModeList[i].c;
1881 else
1882 {
1883 what = MODE_ADD;
1884 *m++ = '+';
1885 *m++ = userModeList[i].c;
1886 }
1887 }
1888 }
1889 if (needhost) {
1890 *m++ = ' ';
1891 ircd_snprintf(0, m, USERLEN + HOSTLEN + 1, "%s@%s", cli_user(sptr)->username,
1892 cli_user(sptr)->host);
1893 } else
1894 *m = '\0';
1895 if (*umodeBuf && cptr)
1896 sendcmdto_one(sptr, CMD_MODE, cptr, "%s %s", cli_name(sptr), umodeBuf);
1897 }
1898
1899 /**
1900 * Check to see if this resembles a sno_mask. It is if 1) there is
1901 * at least one digit and 2) The first digit occurs before the first
1902 * alphabetic character.
1903 * @param[in] word Word to check for sno_mask-ness.
1904 * @return Non-zero if \a word looks like a server notice mask; zero if not.
1905 */
1906 int is_snomask(char *word)
1907 {
1908 if (word)
1909 {
1910 for (; *word; word++)
1911 if (IsDigit(*word))
1912 return 1;
1913 else if (IsAlpha(*word))
1914 return 0;
1915 }
1916 return 0;
1917 }
1918
1919 /*
1920 * Check to see if it resembles a valid hostmask.
1921 */
1922 int is_hostmask(char *word)
1923 {
1924 int i = 0;
1925 char *host;
1926
1927 Debug((DEBUG_INFO, "is_hostmask() %s", word));
1928
1929 if (strlen(word) > (HOSTLEN + USERLEN + 1) || strlen(word) <= 0)
1930 return 0;
1931
1932 /* if a host is specified, make sure it's valid */
1933 host = strrchr(word, '@');
1934 if (host) {
1935 if (strlen(++host) < 1)
1936 return 0;
1937 if (strlen(host) > HOSTLEN)
1938 return 0;
1939 }
1940
1941 if (word) {
1942 if ('@' == *word) /* no leading @'s */
1943 return 0;
1944
1945 if ('#' == *word) { /* numeric index given? */
1946 for (word++; *word; word++) {
1947 if (!IsDigit(*word))
1948 return 0;
1949 }
1950 return 1;
1951 }
1952
1953 /* normal hostmask, account for at most one '@' */
1954 for (; *word; word++) {
1955 if ('@' == *word) {
1956 i++;
1957 continue;
1958 }
1959 if (!IsHostChar(*word))
1960 return 0;
1961 }
1962 return (1 < i) ? 0 : 1; /* no more than on '@' */
1963 }
1964 return 0;
1965 }
1966
1967 /*
1968 * IsVhost() - Check if given host is a valid spoofhost
1969 * (ie: configured thru a S:line)
1970 */
1971 static char *IsVhost(char *hostmask, int oper)
1972 {
1973 unsigned int i = 0, y = 0;
1974 struct sline *sconf;
1975
1976 Debug((DEBUG_INFO, "IsVhost() %s", hostmask));
1977
1978 if (EmptyString(hostmask))
1979 return NULL;
1980
1981 /* spoofhost specified as index, ie: #27 */
1982 if ('#' == hostmask[0]) {
1983 y = atoi(hostmask + 1);
1984 for (i = 0, sconf = GlobalSList; sconf; sconf = sconf->next) {
1985 if (!oper && EmptyString(sconf->passwd))
1986 continue;
1987 if (y == ++i)
1988 return sconf->spoofhost;
1989 }
1990 return NULL;
1991 }
1992
1993 /* spoofhost specified as host, ie: host.cc */
1994 for (sconf = GlobalSList; sconf; sconf = sconf->next)
1995 if (strCasediff(hostmask, sconf->spoofhost) == 0)
1996 return sconf->spoofhost;
1997
1998 return NULL;
1999 }
2000
2001 /*
2002 * IsVhostPass() - Check if given spoofhost has a password
2003 * associated with it, and if, return the password (cleartext)
2004 */
2005 static char *IsVhostPass(char *hostmask)
2006 {
2007 struct sline *sconf;
2008
2009 Debug((DEBUG_INFO, "IsVhostPass() %s", hostmask));
2010
2011 if (EmptyString(hostmask))
2012 return NULL;
2013
2014 for (sconf = GlobalSList; sconf; sconf = sconf->next)
2015 if (strCasediff(hostmask, sconf->spoofhost) == 0) {
2016 Debug((DEBUG_INFO, "sconf->passwd %s", sconf->passwd));
2017 return EmptyString(sconf->passwd) ? NULL : sconf->passwd;
2018 }
2019
2020 return NULL;
2021 }
2022
2023 /** Update snomask \a oldmask according to \a arg and \a what.
2024 * @param[in] oldmask Original user mask.
2025 * @param[in] arg Update string (either a number or '+'/'-' followed by a number).
2026 * @param[in] what MODE_ADD if adding the mask.
2027 * @return New value of service notice mask.
2028 */
2029 unsigned int umode_make_snomask(unsigned int oldmask, char *arg, int what)
2030 {
2031 unsigned int sno_what;
2032 unsigned int newmask;
2033 if (*arg == '+')
2034 {
2035 arg++;
2036 if (what == MODE_ADD)
2037 sno_what = SNO_ADD;
2038 else
2039 sno_what = SNO_DEL;
2040 }
2041 else if (*arg == '-')
2042 {
2043 arg++;
2044 if (what == MODE_ADD)
2045 sno_what = SNO_DEL;
2046 else
2047 sno_what = SNO_ADD;
2048 }
2049 else
2050 sno_what = (what == MODE_ADD) ? SNO_SET : SNO_DEL;
2051 /* pity we don't have strtoul everywhere */
2052 newmask = (unsigned int)atoi(arg);
2053 if (sno_what == SNO_DEL)
2054 newmask = oldmask & ~newmask;
2055 else if (sno_what == SNO_ADD)
2056 newmask |= oldmask;
2057 return newmask;
2058 }
2059
2060 /** Remove \a cptr from the singly linked list \a list.
2061 * @param[in] cptr Client to remove from list.
2062 * @param[in,out] list Pointer to head of list containing \a cptr.
2063 */
2064 static void delfrom_list(struct Client *cptr, struct SLink **list)
2065 {
2066 struct SLink* tmp;
2067 struct SLink* prv = NULL;
2068
2069 for (tmp = *list; tmp; tmp = tmp->next) {
2070 if (tmp->value.cptr == cptr) {
2071 if (prv)
2072 prv->next = tmp->next;
2073 else
2074 *list = tmp->next;
2075 free_link(tmp);
2076 break;
2077 }
2078 prv = tmp;
2079 }
2080 }
2081
2082 /** Set \a cptr's server notice mask, according to \a what.
2083 * @param[in,out] cptr Client whose snomask is updating.
2084 * @param[in] newmask Base value for new snomask.
2085 * @param[in] what One of SNO_ADD, SNO_DEL, SNO_SET, to choose operation.
2086 */
2087 void set_snomask(struct Client *cptr, unsigned int newmask, int what)
2088 {
2089 unsigned int oldmask, diffmask; /* unsigned please */
2090 int i;
2091 struct SLink *tmp;
2092
2093 oldmask = cli_snomask(cptr);
2094
2095 if (what == SNO_ADD)
2096 newmask |= oldmask;
2097 else if (what == SNO_DEL)
2098 newmask = oldmask & ~newmask;
2099 else if (what != SNO_SET) /* absolute set, no math needed */
2100 sendto_opmask_butone(0, SNO_OLDSNO, "setsnomask called with %d ?!", what);
2101
2102 newmask &= (IsAnOper(cptr) ? SNO_ALL : SNO_USER);
2103
2104 diffmask = oldmask ^ newmask;
2105
2106 for (i = 0; diffmask >> i; i++) {
2107 if (((diffmask >> i) & 1))
2108 {
2109 if (((newmask >> i) & 1))
2110 {
2111 tmp = make_link();
2112 tmp->next = opsarray[i];
2113 tmp->value.cptr = cptr;
2114 opsarray[i] = tmp;
2115 }
2116 else
2117 /* not real portable :( */
2118 delfrom_list(cptr, &opsarray[i]);
2119 }
2120 }
2121 cli_snomask(cptr) = newmask;
2122 }
2123
2124 /** Check whether \a sptr is allowed to send a message to \a acptr.
2125 * If \a sptr is a remote user, it means some server has an outdated
2126 * SILENCE list for \a acptr, so send the missing SILENCE mask(s) back
2127 * in the direction of \a sptr. Skip the check if \a sptr is a server.
2128 * @param[in] sptr Client trying to send a message.
2129 * @param[in] acptr Destination of message.
2130 * @return Non-zero if \a sptr is SILENCEd by \a acptr, zero if not.
2131 */
2132 int is_silenced(struct Client *sptr, struct Client *acptr)
2133 {
2134 struct Ban *found;
2135 struct User *user;
2136 size_t buf_used, slen;
2137 char buf[BUFSIZE];
2138
2139 if (IsServer(sptr) || !(user = cli_user(acptr))
2140 || !(found = find_ban(sptr, user->silence)))
2141 return 0;
2142 assert(!(found->flags & BAN_EXCEPTION));
2143 if (!MyConnect(sptr)) {
2144 /* Buffer positive silence to send back. */
2145 buf_used = strlen(found->banstr);
2146 memcpy(buf, found->banstr, buf_used);
2147 /* Add exceptions to buffer. */
2148 for (found = user->silence; found; found = found->next) {
2149 if (!(found->flags & BAN_EXCEPTION))
2150 continue;
2151 slen = strlen(found->banstr);
2152 if (buf_used + slen + 4 > 400) {
2153 buf[buf_used] = '\0';
2154 sendcmdto_one(acptr, CMD_SILENCE, cli_from(sptr), "%C %s", sptr, buf);
2155 buf_used = 0;
2156 }
2157 if (buf_used)
2158 buf[buf_used++] = ',';
2159 buf[buf_used++] = '+';
2160 buf[buf_used++] = '~';
2161 memcpy(buf + buf_used, found->banstr, slen);
2162 buf_used += slen;
2163 }
2164 /* Flush silence buffer. */
2165 if (buf_used) {
2166 buf[buf_used] = '\0';
2167 sendcmdto_one(acptr, CMD_SILENCE, cli_from(sptr), "%C %s", sptr, buf);
2168 buf_used = 0;
2169 }
2170 }
2171 return 1;
2172 }
2173
2174 /** Send RPL_ISUPPORT lines to \a cptr.
2175 * @param[in] cptr Client to send ISUPPORT to.
2176 * @return Zero.
2177 */
2178 int
2179 send_supported(struct Client *cptr)
2180 {
2181 char featurebuf[512];
2182
2183 ircd_snprintf(0, featurebuf, sizeof(featurebuf), FEATURES1, FEATURESVALUES1);
2184 send_reply(cptr, RPL_ISUPPORT, featurebuf);
2185 ircd_snprintf(0, featurebuf, sizeof(featurebuf), FEATURES2, FEATURESVALUES2);
2186 send_reply(cptr, RPL_ISUPPORT, featurebuf);
2187
2188 return 0; /* convenience return, if it's ever needed */
2189 }