]> jfr.im git - solanum.git/blob - src/s_user.c
Fix buffer overflow in introduce_client and burst_TS6
[solanum.git] / src / s_user.c
1 /*
2 * ircd-ratbox: A slightly useful ircd.
3 * s_user.c: User related functions.
4 *
5 * Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6 * Copyright (C) 1996-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
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 2 of the License, or
12 * (at your option) 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
23 *
24 * $Id: s_user.c 3586 2007-11-20 11:16:43Z nenolod $
25 */
26
27 #include "stdinc.h"
28 #include "s_user.h"
29 #include "channel.h"
30 #include "class.h"
31 #include "client.h"
32 #include "common.h"
33 #include "hash.h"
34 #include "match.h"
35 #include "ircd.h"
36 #include "listener.h"
37 #include "msg.h"
38 #include "numeric.h"
39 #include "s_conf.h"
40 #include "s_newconf.h"
41 #include "logger.h"
42 #include "s_serv.h"
43 #include "s_stats.h"
44 #include "scache.h"
45 #include "send.h"
46 #include "supported.h"
47 #include "whowas.h"
48 #include "packet.h"
49 #include "reject.h"
50 #include "cache.h"
51 #include "hook.h"
52 #include "monitor.h"
53 #include "snomask.h"
54 #include "blacklist.h"
55 #include "substitution.h"
56 #include "chmode.h"
57 #include "s_assert.h"
58
59 static void report_and_set_user_flags(struct Client *, struct ConfItem *);
60 void user_welcome(struct Client *source_p);
61
62 char umodebuf[128];
63
64 static int orphaned_umodes = 0;
65 int user_modes[256] = {
66 /* 0x00 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x0F */
67 /* 0x10 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x1F */
68 /* 0x20 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x2F */
69 /* 0x30 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x3F */
70 0, /* @ */
71 0, /* A */
72 0, /* B */
73 0, /* C */
74 UMODE_DEAF, /* D */
75 0, /* E */
76 0, /* F */
77 0, /* G */
78 0, /* H */
79 0, /* I */
80 0, /* J */
81 0, /* K */
82 0, /* L */
83 0, /* M */
84 0, /* N */
85 0, /* O */
86 0, /* P */
87 UMODE_NOFORWARD, /* Q */
88 UMODE_REGONLYMSG, /* R */
89 UMODE_SERVICE, /* S */
90 0, /* T */
91 0, /* U */
92 0, /* V */
93 0, /* W */
94 0, /* X */
95 0, /* Y */
96 UMODE_SSLCLIENT, /* Z */
97 /* 0x5B */ 0, 0, 0, 0, 0, 0, /* 0x60 */
98 UMODE_ADMIN, /* a */
99 0, /* b */
100 0, /* c */
101 0, /* d */
102 0, /* e */
103 0, /* f */
104 UMODE_CALLERID, /* g */
105 0, /* h */
106 UMODE_INVISIBLE, /* i */
107 0, /* j */
108 0, /* k */
109 UMODE_LOCOPS, /* l */
110 0, /* m */
111 0, /* n */
112 UMODE_OPER, /* o */
113 0, /* p */
114 0, /* q */
115 0, /* r */
116 UMODE_SERVNOTICE, /* s */
117 0, /* t */
118 0, /* u */
119 0, /* v */
120 UMODE_WALLOP, /* w */
121 0, /* x */
122 0, /* y */
123 UMODE_OPERWALL, /* z */
124 /* 0x7B */ 0, 0, 0, 0, 0, /* 0x7F */
125 /* 0x80 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x9F */
126 /* 0x90 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x9F */
127 /* 0xA0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xAF */
128 /* 0xB0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xBF */
129 /* 0xC0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xCF */
130 /* 0xD0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xDF */
131 /* 0xE0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xEF */
132 /* 0xF0 */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 0xFF */
133 };
134 /* *INDENT-ON* */
135
136 /*
137 * show_lusers -
138 *
139 * inputs - pointer to client
140 * output -
141 * side effects - display to client user counts etc.
142 */
143 int
144 show_lusers(struct Client *source_p)
145 {
146 if(rb_dlink_list_length(&lclient_list) > (unsigned long)MaxClientCount)
147 MaxClientCount = rb_dlink_list_length(&lclient_list);
148
149 if((rb_dlink_list_length(&lclient_list) + rb_dlink_list_length(&serv_list)) >
150 (unsigned long)MaxConnectionCount)
151 MaxConnectionCount = rb_dlink_list_length(&lclient_list) +
152 rb_dlink_list_length(&serv_list);
153
154 sendto_one_numeric(source_p, RPL_LUSERCLIENT, form_str(RPL_LUSERCLIENT),
155 (Count.total - Count.invisi),
156 Count.invisi,
157 (int)rb_dlink_list_length(&global_serv_list));
158
159 if(rb_dlink_list_length(&oper_list) > 0)
160 sendto_one_numeric(source_p, RPL_LUSEROP,
161 form_str(RPL_LUSEROP),
162 (int)rb_dlink_list_length(&oper_list));
163
164 if(rb_dlink_list_length(&unknown_list) > 0)
165 sendto_one_numeric(source_p, RPL_LUSERUNKNOWN,
166 form_str(RPL_LUSERUNKNOWN),
167 (int)rb_dlink_list_length(&unknown_list));
168
169 if(rb_dlink_list_length(&global_channel_list) > 0)
170 sendto_one_numeric(source_p, RPL_LUSERCHANNELS,
171 form_str(RPL_LUSERCHANNELS),
172 rb_dlink_list_length(&global_channel_list));
173
174 sendto_one_numeric(source_p, RPL_LUSERME, form_str(RPL_LUSERME),
175 (int)rb_dlink_list_length(&lclient_list),
176 (int)rb_dlink_list_length(&serv_list));
177
178 sendto_one_numeric(source_p, RPL_LOCALUSERS,
179 form_str(RPL_LOCALUSERS),
180 (int)rb_dlink_list_length(&lclient_list),
181 Count.max_loc,
182 (int)rb_dlink_list_length(&lclient_list),
183 Count.max_loc);
184
185 sendto_one_numeric(source_p, RPL_GLOBALUSERS, form_str(RPL_GLOBALUSERS),
186 Count.total, Count.max_tot,
187 Count.total, Count.max_tot);
188
189 sendto_one_numeric(source_p, RPL_STATSCONN,
190 form_str(RPL_STATSCONN),
191 MaxConnectionCount, MaxClientCount,
192 Count.totalrestartcount);
193
194 return 0;
195 }
196
197 /*
198 ** register_local_user
199 ** This function is called when both NICK and USER messages
200 ** have been accepted for the client, in whatever order. Only
201 ** after this, is the USER message propagated.
202 **
203 ** NICK's must be propagated at once when received, although
204 ** it would be better to delay them too until full info is
205 ** available. Doing it is not so simple though, would have
206 ** to implement the following:
207 **
208 ** (actually it has been implemented already for a while) -orabidoo
209 **
210 ** 1) user telnets in and gives only "NICK foobar" and waits
211 ** 2) another user far away logs in normally with the nick
212 ** "foobar" (quite legal, as this server didn't propagate
213 ** it).
214 ** 3) now this server gets nick "foobar" from outside, but
215 ** has alread the same defined locally. Current server
216 ** would just issue "KILL foobar" to clean out dups. But,
217 ** this is not fair. It should actually request another
218 ** nick from local user or kill him/her...
219 */
220
221 int
222 register_local_user(struct Client *client_p, struct Client *source_p, const char *username)
223 {
224 struct ConfItem *aconf, *xconf;
225 struct User *user = source_p->user;
226 char tmpstr2[IRCD_BUFSIZE];
227 char ipaddr[HOSTIPLEN];
228 char myusername[USERLEN+1];
229 int status;
230
231 s_assert(NULL != source_p);
232 s_assert(MyConnect(source_p));
233 s_assert(source_p->username != username);
234
235 if(source_p == NULL)
236 return -1;
237
238 if(IsAnyDead(source_p))
239 return -1;
240
241 if(ConfigFileEntry.ping_cookie)
242 {
243 if(!(source_p->flags & FLAGS_PINGSENT) && source_p->localClient->random_ping == 0)
244 {
245 source_p->localClient->random_ping = (unsigned long) (rand() * rand()) << 1;
246 sendto_one(source_p, "PING :%08lX",
247 (unsigned long) source_p->localClient->random_ping);
248 source_p->flags |= FLAGS_PINGSENT;
249 return -1;
250 }
251 if(!(source_p->flags & FLAGS_PING_COOKIE))
252 {
253 return -1;
254 }
255 }
256
257 /* hasnt finished client cap negotiation */
258 if(source_p->flags & FLAGS_CLICAP)
259 return -1;
260
261 /* still has DNSbls to validate against */
262 if(rb_dlink_list_length(&source_p->preClient->dnsbl_queries) > 0)
263 return -1;
264
265 client_p->localClient->last = rb_current_time();
266
267 /* XXX - fixme. we shouldnt have to build a users buffer twice.. */
268 if(!IsGotId(source_p) && (strchr(username, '[') != NULL))
269 {
270 const char *p;
271 int i = 0;
272
273 p = username;
274
275 while(*p && i < USERLEN)
276 {
277 if(*p != '[')
278 myusername[i++] = *p;
279 p++;
280 }
281
282 myusername[i] = '\0';
283 username = myusername;
284 }
285
286 if((status = check_client(client_p, source_p, username)) < 0)
287 return (CLIENT_EXITED);
288
289 /* Apply nick override */
290 if(*source_p->preClient->spoofnick)
291 {
292 char note[NICKLEN + 10];
293
294 del_from_client_hash(source_p->name, source_p);
295 rb_strlcpy(source_p->name, source_p->preClient->spoofnick, NICKLEN + 1);
296 add_to_client_hash(source_p->name, source_p);
297
298 rb_snprintf(note, NICKLEN + 10, "Nick: %s", source_p->name);
299 rb_note(source_p->localClient->F, note);
300 }
301
302 if(!valid_hostname(source_p->host))
303 {
304 sendto_one_notice(source_p, ":*** Notice -- You have an illegal character in your hostname");
305
306 rb_strlcpy(source_p->host, source_p->sockhost, sizeof(source_p->host));
307 }
308
309
310 aconf = source_p->localClient->att_conf;
311
312 if(aconf == NULL)
313 {
314 exit_client(client_p, source_p, &me, "*** Not Authorised");
315 return (CLIENT_EXITED);
316 }
317
318 if(IsConfSSLNeeded(aconf) && !IsSSL(source_p))
319 {
320 ServerStats.is_ref++;
321 sendto_one_notice(source_p, ":*** Notice -- You need to use SSL/TLS to use this server");
322 exit_client(client_p, source_p, &me, "Use SSL/TLS");
323 return (CLIENT_EXITED);
324 }
325
326 if(!IsGotId(source_p))
327 {
328 const char *p;
329 int i = 0;
330
331 if(IsNeedIdentd(aconf))
332 {
333 ServerStats.is_ref++;
334 sendto_one_notice(source_p, ":*** Notice -- You need to install identd to use this server");
335 exit_client(client_p, source_p, &me, "Install identd");
336 return (CLIENT_EXITED);
337 }
338
339 /* dont replace username if its supposed to be spoofed --fl */
340 if(!IsConfDoSpoofIp(aconf) || !strchr(aconf->info.name, '@'))
341 {
342 p = username;
343
344 if(!IsNoTilde(aconf))
345 source_p->username[i++] = '~';
346
347 while (*p && i < USERLEN)
348 {
349 if(*p != '[')
350 source_p->username[i++] = *p;
351 p++;
352 }
353
354 source_p->username[i] = '\0';
355 }
356 }
357
358 if(IsNeedSasl(aconf) && !*user->suser)
359 {
360 ServerStats.is_ref++;
361 sendto_one_notice(source_p, ":*** Notice -- You need to identify via SASL to use this server");
362 exit_client(client_p, source_p, &me, "SASL access only");
363 return (CLIENT_EXITED);
364 }
365
366 /* password check */
367 if(!EmptyString(aconf->passwd))
368 {
369 const char *encr;
370
371 if(EmptyString(source_p->localClient->passwd))
372 encr = "";
373 else if(IsConfEncrypted(aconf))
374 encr = rb_crypt(source_p->localClient->passwd, aconf->passwd);
375 else
376 encr = source_p->localClient->passwd;
377
378 if(encr == NULL || strcmp(encr, aconf->passwd))
379 {
380 ServerStats.is_ref++;
381 sendto_one(source_p, form_str(ERR_PASSWDMISMATCH), me.name, source_p->name);
382 exit_client(client_p, source_p, &me, "Bad Password");
383 return (CLIENT_EXITED);
384 }
385
386 /* clear password only if used now, otherwise send it
387 * to services -- jilles */
388 if(source_p->localClient->passwd)
389 {
390 memset(source_p->localClient->passwd, 0, strlen(source_p->localClient->passwd));
391 rb_free(source_p->localClient->passwd);
392 source_p->localClient->passwd = NULL;
393 }
394 }
395
396 /* report if user has &^>= etc. and set flags as needed in source_p */
397 report_and_set_user_flags(source_p, aconf);
398
399 /* Limit clients */
400 /*
401 * We want to be able to have servers and F-line clients
402 * connect, so save room for "buffer" connections.
403 * Smaller servers may want to decrease this, and it should
404 * probably be just a percentage of the MAXCLIENTS...
405 * -Taner
406 */
407 /* Except "F:" clients */
408 if(rb_dlink_list_length(&lclient_list) >=
409 (unsigned long)GlobalSetOptions.maxclients && !IsConfExemptLimits(aconf))
410 {
411 sendto_realops_snomask(SNO_FULL, L_ALL,
412 "Too many clients, rejecting %s[%s].", source_p->name, source_p->host);
413
414 ServerStats.is_ref++;
415 exit_client(client_p, source_p, &me, "Sorry, server is full - try later");
416 return (CLIENT_EXITED);
417 }
418
419 /* kline exemption extends to xline too */
420 if(!IsExemptKline(source_p) &&
421 (xconf = find_xline(source_p->info, 1)) != NULL)
422 {
423 ServerStats.is_ref++;
424 add_reject(source_p, xconf->host, NULL);
425 exit_client(client_p, source_p, &me, "Bad user info");
426 return CLIENT_EXITED;
427 }
428
429 /* dnsbl check */
430 if (source_p->preClient->dnsbl_listed != NULL)
431 {
432 if (IsExemptKline(source_p) || IsConfExemptDNSBL(aconf))
433 sendto_one_notice(source_p, ":*** Your IP address %s is listed in %s, but you are exempt",
434 source_p->sockhost, source_p->preClient->dnsbl_listed->host);
435 else
436 {
437 sendto_realops_snomask(SNO_REJ, L_NETWIDE,
438 "Listed on DNSBL %s: %s (%s@%s) [%s] [%s]",
439 source_p->preClient->dnsbl_listed->host,
440 source_p->name,
441 source_p->username, source_p->host,
442 IsIPSpoof(source_p) ? "255.255.255.255" : source_p->sockhost,
443 source_p->info);
444
445 rb_dlink_list varlist = { NULL, NULL, 0 };
446
447 substitution_append_var(&varlist, "nick", source_p->name);
448 substitution_append_var(&varlist, "ip", source_p->sockhost);
449 substitution_append_var(&varlist, "host", source_p->host);
450 substitution_append_var(&varlist, "dnsbl-host", source_p->preClient->dnsbl_listed->host);
451 substitution_append_var(&varlist, "network-name", ServerInfo.network_name);
452
453 ServerStats.is_ref++;
454
455 sendto_one(source_p, form_str(ERR_YOUREBANNEDCREEP),
456 me.name, source_p->name,
457 substitution_parse(source_p->preClient->dnsbl_listed->reject_reason, &varlist));
458
459 substitution_free(&varlist);
460
461 sendto_one_notice(source_p, ":*** Your IP address %s is listed in %s",
462 source_p->sockhost, source_p->preClient->dnsbl_listed->host);
463 source_p->preClient->dnsbl_listed->hits++;
464 add_reject(source_p, NULL, NULL);
465 exit_client(client_p, source_p, &me, "*** Banned (DNS blacklist)");
466 return CLIENT_EXITED;
467 }
468 }
469
470 /* valid user name check */
471
472 if(!valid_username(source_p->username))
473 {
474 sendto_realops_snomask(SNO_REJ, L_ALL,
475 "Invalid username: %s (%s@%s)",
476 source_p->name, source_p->username, source_p->host);
477 ServerStats.is_ref++;
478 sendto_one_notice(source_p, ":*** Your username is invalid. Please make sure that your username contains "
479 "only alphanumeric characters.");
480 rb_sprintf(tmpstr2, "Invalid username [%s]", source_p->username);
481 exit_client(client_p, source_p, &me, tmpstr2);
482 return (CLIENT_EXITED);
483 }
484
485 /* end of valid user name check */
486
487 /* Store original hostname -- jilles */
488 rb_strlcpy(source_p->orighost, source_p->host, HOSTLEN + 1);
489
490 /* Spoof user@host */
491 if(*source_p->preClient->spoofuser)
492 rb_strlcpy(source_p->username, source_p->preClient->spoofuser, USERLEN + 1);
493 if(*source_p->preClient->spoofhost)
494 {
495 rb_strlcpy(source_p->host, source_p->preClient->spoofhost, HOSTLEN + 1);
496 if (irccmp(source_p->host, source_p->orighost))
497 SetDynSpoof(source_p);
498 }
499
500 source_p->umodes |= ConfigFileEntry.default_umodes & ~ConfigFileEntry.oper_only_umodes & ~orphaned_umodes;
501
502 call_hook(h_new_local_user, source_p);
503
504 /* If they have died in send_* or were thrown out by the
505 * new_local_user hook don't do anything. */
506 if(IsAnyDead(source_p))
507 return CLIENT_EXITED;
508
509 /* To avoid inconsistencies, do not abort the registration
510 * starting from this point -- jilles
511 */
512 rb_inet_ntop_sock((struct sockaddr *)&source_p->localClient->ip, ipaddr, sizeof(ipaddr));
513
514 sendto_realops_snomask(SNO_CCONN, L_ALL,
515 "Client connecting: %s (%s@%s) [%s] {%s} [%s]",
516 source_p->name, source_p->username, source_p->orighost,
517 show_ip(NULL, source_p) ? ipaddr : "255.255.255.255",
518 get_client_class(source_p), source_p->info);
519
520 sendto_realops_snomask(SNO_CCONNEXT, L_ALL,
521 "CLICONN %s %s %s %s %s %s 0 %s",
522 source_p->name, source_p->username, source_p->orighost,
523 show_ip(NULL, source_p) ? ipaddr : "255.255.255.255",
524 get_client_class(source_p),
525 /* mirc can sometimes send ips here */
526 show_ip(NULL, source_p) ? source_p->localClient->fullcaps : "<hidden> <hidden>",
527 source_p->info);
528
529 add_to_hostname_hash(source_p->orighost, source_p);
530
531 /* Allocate a UID if it was not previously allocated.
532 * If this already occured, it was probably during SASL auth...
533 */
534 if(!*source_p->id)
535 {
536 strcpy(source_p->id, generate_uid());
537 add_to_id_hash(source_p->id, source_p);
538 }
539
540 if (IsSSL(source_p))
541 source_p->umodes |= UMODE_SSLCLIENT;
542
543 if (source_p->umodes & UMODE_INVISIBLE)
544 Count.invisi++;
545
546 s_assert(!IsClient(source_p));
547 rb_dlinkMoveNode(&source_p->localClient->tnode, &unknown_list, &lclient_list);
548 SetClient(source_p);
549
550 source_p->servptr = &me;
551 rb_dlinkAdd(source_p, &source_p->lnode, &source_p->servptr->serv->users);
552
553 /* Increment our total user count here */
554 if(++Count.total > Count.max_tot)
555 Count.max_tot = Count.total;
556
557 Count.totalrestartcount++;
558
559 s_assert(source_p->localClient != NULL);
560
561 if(rb_dlink_list_length(&lclient_list) > (unsigned long)Count.max_loc)
562 {
563 Count.max_loc = rb_dlink_list_length(&lclient_list);
564 if(!(Count.max_loc % 10))
565 sendto_realops_snomask(SNO_GENERAL, L_ALL,
566 "New Max Local Clients: %d", Count.max_loc);
567 }
568
569 /* they get a reduced limit */
570 if(find_tgchange(source_p->sockhost))
571 source_p->localClient->targets_free = TGCHANGE_INITIAL_LOW;
572 else
573 source_p->localClient->targets_free = TGCHANGE_INITIAL;
574
575 monitor_signon(source_p);
576 user_welcome(source_p);
577
578 free_pre_client(source_p);
579
580 return (introduce_client(client_p, source_p, user, source_p->name, 1));
581 }
582
583 /*
584 * introduce_clients
585 *
586 * inputs -
587 * output -
588 * side effects - This common function introduces a client to the rest
589 * of the net, either from a local client connect or
590 * from a remote connect.
591 */
592 int
593 introduce_client(struct Client *client_p, struct Client *source_p, struct User *user, const char *nick, int use_euid)
594 {
595 char ubuf[BUFSIZE];
596 struct Client *identifyservice_p;
597 char *p;
598 hook_data_umode_changed hdata;
599 hook_data_client hdata2;
600 char sockhost[HOSTLEN];
601
602 if(MyClient(source_p))
603 send_umode(source_p, source_p, 0, 0, ubuf);
604 else
605 send_umode(NULL, source_p, 0, 0, ubuf);
606
607 if(!*ubuf)
608 {
609 ubuf[0] = '+';
610 ubuf[1] = '\0';
611 }
612
613 s_assert(has_id(source_p));
614
615 if(source_p->sockhost[0] == ':')
616 {
617 sockhost[0] = '0';
618 sockhost[1] = '\0';
619 rb_strlcat(sockhost, source_p->sockhost, sizeof(sockhost));
620 } else
621 strcpy(sockhost, source_p->sockhost);
622
623 if (use_euid)
624 sendto_server(client_p, NULL, CAP_EUID | CAP_TS6, NOCAPS,
625 ":%s EUID %s %d %ld %s %s %s %s %s %s %s :%s",
626 source_p->servptr->id, nick,
627 source_p->hopcount + 1,
628 (long) source_p->tsinfo, ubuf,
629 source_p->username, source_p->host,
630 IsIPSpoof(source_p) ? "0" : sockhost,
631 source_p->id,
632 IsDynSpoof(source_p) ? source_p->orighost : "*",
633 EmptyString(source_p->user->suser) ? "*" : source_p->user->suser,
634 source_p->info);
635
636 sendto_server(client_p, NULL, CAP_TS6, use_euid ? CAP_EUID : NOCAPS,
637 ":%s UID %s %d %ld %s %s %s %s %s :%s",
638 source_p->servptr->id, nick,
639 source_p->hopcount + 1,
640 (long) source_p->tsinfo, ubuf,
641 source_p->username, source_p->host,
642 IsIPSpoof(source_p) ? "0" : sockhost,
643 source_p->id, source_p->info);
644
645 if(!EmptyString(source_p->certfp))
646 sendto_server(client_p, NULL, CAP_TS6, NOCAPS,
647 ":%s ENCAP * CERTFP :%s",
648 use_id(source_p), source_p->certfp);
649
650 if (IsDynSpoof(source_p))
651 {
652 sendto_server(client_p, NULL, CAP_TS6, use_euid ? CAP_EUID : NOCAPS, ":%s ENCAP * REALHOST %s",
653 use_id(source_p), source_p->orighost);
654 }
655
656 if (!EmptyString(source_p->user->suser))
657 {
658 sendto_server(client_p, NULL, CAP_TS6, use_euid ? CAP_EUID : NOCAPS, ":%s ENCAP * LOGIN %s",
659 use_id(source_p), source_p->user->suser);
660 }
661
662 if(MyConnect(source_p) && source_p->localClient->passwd)
663 {
664 if (!EmptyString(ConfigFileEntry.identifyservice) &&
665 !EmptyString(ConfigFileEntry.identifycommand))
666 {
667 /* use user@server */
668 p = strchr(ConfigFileEntry.identifyservice, '@');
669 if (p != NULL)
670 identifyservice_p = find_named_client(p + 1);
671 else
672 identifyservice_p = NULL;
673 if (identifyservice_p != NULL)
674 {
675 if (!EmptyString(source_p->localClient->auth_user))
676 sendto_one(identifyservice_p, ":%s PRIVMSG %s :%s %s %s",
677 get_id(source_p, identifyservice_p),
678 ConfigFileEntry.identifyservice,
679 ConfigFileEntry.identifycommand,
680 source_p->localClient->auth_user,
681 source_p->localClient->passwd);
682 else
683 sendto_one(identifyservice_p, ":%s PRIVMSG %s :%s %s",
684 get_id(source_p, identifyservice_p),
685 ConfigFileEntry.identifyservice,
686 ConfigFileEntry.identifycommand,
687 source_p->localClient->passwd);
688 }
689 }
690 memset(source_p->localClient->passwd, 0, strlen(source_p->localClient->passwd));
691 rb_free(source_p->localClient->passwd);
692 source_p->localClient->passwd = NULL;
693 }
694
695 /* let modules providing usermodes know that we've got a new user,
696 * why is this here? -- well, some modules need to be able to send out new
697 * information about a client, so this was the best place to do it
698 * --nenolod
699 */
700 hdata.client = source_p;
701 hdata.oldumodes = 0;
702 hdata.oldsnomask = 0;
703 call_hook(h_umode_changed, &hdata);
704
705 /* On the other hand, some modules need to know when a client is
706 * being introduced, period.
707 * --gxti
708 */
709 hdata2.client = client_p;
710 hdata2.target = source_p;
711 call_hook(h_introduce_client, &hdata2);
712
713 return 0;
714 }
715
716 /*
717 * valid_hostname - check hostname for validity
718 *
719 * Inputs - pointer to user
720 * Output - YES if valid, NO if not
721 * Side effects - NONE
722 *
723 * NOTE: this doesn't allow a hostname to begin with a dot and
724 * will not allow more dots than chars.
725 */
726 int
727 valid_hostname(const char *hostname)
728 {
729 const char *p = hostname, *last_slash = 0;
730 int found_sep = 0;
731
732 s_assert(NULL != p);
733
734 if(hostname == NULL)
735 return NO;
736
737 if('.' == *p || ':' == *p || '/' == *p)
738 return NO;
739
740 while (*p)
741 {
742 if(!IsHostChar(*p))
743 return NO;
744 if(*p == '.' || *p == ':')
745 found_sep++;
746 else if(*p == '/')
747 {
748 found_sep++;
749 last_slash = p;
750 }
751 p++;
752 }
753
754 if(found_sep == 0)
755 return NO;
756
757 if(last_slash && IsDigit(last_slash[1]))
758 return NO;
759
760 return YES;
761 }
762
763 /*
764 * valid_username - check username for validity
765 *
766 * Inputs - pointer to user
767 * Output - YES if valid, NO if not
768 * Side effects - NONE
769 *
770 * Absolutely always reject any '*' '!' '?' '@' in an user name
771 * reject any odd control characters names.
772 * Allow '.' in username to allow for "first.last"
773 * style of username
774 */
775 int
776 valid_username(const char *username)
777 {
778 int dots = 0;
779 const char *p = username;
780
781 s_assert(NULL != p);
782
783 if(username == NULL)
784 return NO;
785
786 if('~' == *p)
787 ++p;
788
789 /* reject usernames that don't start with an alphanum
790 * i.e. reject jokers who have '-@somehost' or '.@somehost'
791 * or "-hi-@somehost", "h-----@somehost" would still be accepted.
792 */
793 if(!IsAlNum(*p))
794 return NO;
795
796 while (*++p)
797 {
798 if((*p == '.') && ConfigFileEntry.dots_in_ident)
799 {
800 dots++;
801 if(dots > ConfigFileEntry.dots_in_ident)
802 return NO;
803 if(!IsUserChar(p[1]))
804 return NO;
805 }
806 else if(!IsUserChar(*p))
807 return NO;
808 }
809 return YES;
810 }
811
812 /* report_and_set_user_flags
813 *
814 * Inputs - pointer to source_p
815 * - pointer to aconf for this user
816 * Output - NONE
817 * Side effects -
818 * Report to user any special flags they are getting, and set them.
819 */
820
821 static void
822 report_and_set_user_flags(struct Client *source_p, struct ConfItem *aconf)
823 {
824 /* If this user is being spoofed, tell them so */
825 if(IsConfDoSpoofIp(aconf))
826 {
827 sendto_one_notice(source_p, ":*** Spoofing your IP");
828 }
829
830 /* If this user is in the exception class, Set it "E lined" */
831 if(IsConfExemptKline(aconf))
832 {
833 SetExemptKline(source_p);
834 sendto_one_notice(source_p, ":*** You are exempt from K/X lines");
835 }
836
837 if(IsConfExemptDNSBL(aconf))
838 /* kline exempt implies this, don't send both */
839 if(!IsConfExemptKline(aconf))
840 sendto_one_notice(source_p, ":*** You are exempt from DNS blacklists");
841
842 /* If this user is exempt from user limits set it F lined" */
843 if(IsConfExemptLimits(aconf))
844 {
845 sendto_one_notice(source_p, ":*** You are exempt from user limits");
846 }
847
848 if(IsConfExemptFlood(aconf))
849 {
850 SetExemptFlood(source_p);
851 sendto_one_notice(source_p, ":*** You are exempt from flood limits");
852 }
853
854 if(IsConfExemptSpambot(aconf))
855 {
856 SetExemptSpambot(source_p);
857 sendto_one_notice(source_p, ":*** You are exempt from spambot checks");
858 }
859
860 if(IsConfExemptJupe(aconf))
861 {
862 SetExemptJupe(source_p);
863 sendto_one_notice(source_p, ":*** You are exempt from juped channel warnings");
864 }
865
866 if(IsConfExemptResv(aconf))
867 {
868 SetExemptResv(source_p);
869 sendto_one_notice(source_p, ":*** You are exempt from resvs");
870 }
871
872 if(IsConfExemptShide(aconf))
873 {
874 SetExemptShide(source_p);
875 sendto_one_notice(source_p, ":*** You are exempt from serverhiding");
876 }
877 }
878
879 static void
880 show_other_user_mode(struct Client *source_p, struct Client *target_p)
881 {
882 int i;
883 char buf[BUFSIZE];
884 char *m;
885
886 m = buf;
887 *m++ = '+';
888
889 for (i = 0; i < 128; i++) /* >= 127 is extended ascii */
890 if (target_p->umodes & user_modes[i])
891 *m++ = (char) i;
892 *m = '\0';
893
894 if (MyConnect(target_p) && target_p->snomask != 0)
895 sendto_one_notice(source_p, ":Modes for %s are %s %s",
896 target_p->name, buf,
897 construct_snobuf(target_p->snomask));
898 else
899 sendto_one_notice(source_p, ":Modes for %s are %s",
900 target_p->name, buf);
901 }
902
903 /*
904 * user_mode - set get current users mode
905 *
906 * m_umode() added 15/10/91 By Darren Reed.
907 * parv[1] - username to change mode for
908 * parv[2] - modes to change
909 */
910 int
911 user_mode(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
912 {
913 int flag;
914 int i;
915 char *m;
916 const char *pm;
917 struct Client *target_p;
918 int what, setflags;
919 int badflag = NO; /* Only send one bad flag notice */
920 int showsnomask = NO;
921 unsigned int setsnomask;
922 char buf[BUFSIZE];
923 hook_data_umode_changed hdata;
924
925 what = MODE_ADD;
926
927 if(parc < 2)
928 {
929 sendto_one(source_p, form_str(ERR_NEEDMOREPARAMS), me.name, source_p->name, "MODE");
930 return 0;
931 }
932
933 if((target_p = MyClient(source_p) ? find_named_person(parv[1]) : find_person(parv[1])) == NULL)
934 {
935 if(MyConnect(source_p))
936 sendto_one_numeric(source_p, ERR_NOSUCHCHANNEL,
937 form_str(ERR_NOSUCHCHANNEL), parv[1]);
938 return 0;
939 }
940
941 /* Dont know why these were commented out..
942 * put them back using new sendto() funcs
943 */
944
945 if(IsServer(source_p))
946 {
947 sendto_realops_snomask(SNO_GENERAL, L_ADMIN,
948 "*** Mode for User %s from %s", parv[1], source_p->name);
949 return 0;
950 }
951
952 if(source_p != target_p)
953 {
954 if (MyOper(source_p) && parc < 3)
955 show_other_user_mode(source_p, target_p);
956 else
957 sendto_one(source_p, form_str(ERR_USERSDONTMATCH), me.name, source_p->name);
958 return 0;
959 }
960
961 if(parc < 3)
962 {
963 m = buf;
964 *m++ = '+';
965
966 for (i = 0; i < 128; i++) /* >= 127 is extended ascii */
967 if (source_p->umodes & user_modes[i])
968 *m++ = (char) i;
969
970 *m = '\0';
971 sendto_one_numeric(source_p, RPL_UMODEIS, form_str(RPL_UMODEIS), buf);
972
973 if (source_p->snomask != 0)
974 sendto_one_numeric(source_p, RPL_SNOMASK, form_str(RPL_SNOMASK),
975 construct_snobuf(source_p->snomask));
976
977 return 0;
978 }
979
980 /* find flags already set for user */
981 setflags = source_p->umodes;
982 setsnomask = source_p->snomask;
983
984 /*
985 * parse mode change string(s)
986 */
987 for (pm = parv[2]; *pm; pm++)
988 switch (*pm)
989 {
990 case '+':
991 what = MODE_ADD;
992 break;
993 case '-':
994 what = MODE_DEL;
995 break;
996
997 case 'o':
998 if(what == MODE_ADD)
999 {
1000 if(IsServer(client_p) && !IsOper(source_p))
1001 {
1002 ++Count.oper;
1003 SetOper(source_p);
1004 rb_dlinkAddAlloc(source_p, &oper_list);
1005 }
1006 }
1007 else
1008 {
1009 /* Only decrement the oper counts if an oper to begin with
1010 * found by Pat Szuta, Perly , perly@xnet.com
1011 */
1012
1013 if(!IsOper(source_p))
1014 break;
1015
1016 ClearOper(source_p);
1017
1018 Count.oper--;
1019
1020 if(MyConnect(source_p))
1021 {
1022 source_p->umodes &= ~ConfigFileEntry.oper_only_umodes;
1023 if (!(source_p->umodes & UMODE_SERVNOTICE) && source_p->snomask != 0)
1024 {
1025 source_p->snomask = 0;
1026 showsnomask = YES;
1027 }
1028 source_p->flags2 &= ~OPER_FLAGS;
1029
1030 rb_free(source_p->localClient->opername);
1031 source_p->localClient->opername = NULL;
1032
1033 rb_dlinkFindDestroy(source_p, &local_oper_list);
1034 privilegeset_unref(source_p->localClient->privset);
1035 source_p->localClient->privset = NULL;
1036 }
1037
1038 rb_dlinkFindDestroy(source_p, &oper_list);
1039 }
1040 break;
1041
1042 /* we may not get these,
1043 * but they shouldnt be in default
1044 */
1045
1046 /* can only be set on burst */
1047 case 'S':
1048 case 'Z':
1049 case ' ':
1050 case '\n':
1051 case '\r':
1052 case '\t':
1053 break;
1054
1055 case 's':
1056 if (MyConnect(source_p))
1057 {
1058 if(!IsOper(source_p)
1059 && (ConfigFileEntry.oper_only_umodes & UMODE_SERVNOTICE))
1060 {
1061 if (what == MODE_ADD || source_p->umodes & UMODE_SERVNOTICE)
1062 badflag = YES;
1063 continue;
1064 }
1065 showsnomask = YES;
1066 if(what == MODE_ADD)
1067 {
1068 if (parc > 3)
1069 source_p->snomask = parse_snobuf_to_mask(source_p->snomask, parv[3]);
1070 else
1071 source_p->snomask |= SNO_GENERAL;
1072 }
1073 else
1074 source_p->snomask = 0;
1075 if (source_p->snomask != 0)
1076 source_p->umodes |= UMODE_SERVNOTICE;
1077 else
1078 source_p->umodes &= ~UMODE_SERVNOTICE;
1079 break;
1080 }
1081 /* FALLTHROUGH */
1082 default:
1083 if (MyConnect(source_p) && *pm == 'Q' && !ConfigChannel.use_forward)
1084 {
1085 badflag = YES;
1086 break;
1087 }
1088
1089 if((flag = user_modes[(unsigned char) *pm]))
1090 {
1091 if(MyConnect(source_p)
1092 && ((!IsOper(source_p)
1093 && (ConfigFileEntry.oper_only_umodes & flag))
1094 || (orphaned_umodes & flag)))
1095 {
1096 if (what == MODE_ADD || source_p->umodes & flag)
1097 badflag = YES;
1098 }
1099 else
1100 {
1101 if(what == MODE_ADD)
1102 source_p->umodes |= flag;
1103 else
1104 source_p->umodes &= ~flag;
1105 }
1106 }
1107 else
1108 {
1109 if(MyConnect(source_p))
1110 badflag = YES;
1111 }
1112 break;
1113 }
1114
1115 if(badflag)
1116 sendto_one(source_p, form_str(ERR_UMODEUNKNOWNFLAG), me.name, source_p->name);
1117
1118 if(MyClient(source_p) && (source_p->snomask & SNO_NCHANGE) && !IsOperN(source_p))
1119 {
1120 sendto_one_notice(source_p, ":*** You need oper and nick_changes flag for +s +n");
1121 source_p->snomask &= ~SNO_NCHANGE; /* only tcm's really need this */
1122 }
1123
1124 if(MyClient(source_p) && (source_p->umodes & UMODE_OPERWALL) && !IsOperOperwall(source_p))
1125 {
1126 sendto_one_notice(source_p, ":*** You need oper and operwall flag for +z");
1127 source_p->umodes &= ~UMODE_OPERWALL;
1128 }
1129
1130 if(MyConnect(source_p) && (source_p->umodes & UMODE_ADMIN) &&
1131 (!IsOperAdmin(source_p) || IsOperHiddenAdmin(source_p)))
1132 {
1133 sendto_one_notice(source_p, ":*** You need oper and admin flag for +a");
1134 source_p->umodes &= ~UMODE_ADMIN;
1135 }
1136
1137 /* let modules providing usermodes know that we've changed our usermode --nenolod */
1138 hdata.client = source_p;
1139 hdata.oldumodes = setflags;
1140 hdata.oldsnomask = setsnomask;
1141 call_hook(h_umode_changed, &hdata);
1142
1143 if(!(setflags & UMODE_INVISIBLE) && IsInvisible(source_p))
1144 ++Count.invisi;
1145 if((setflags & UMODE_INVISIBLE) && !IsInvisible(source_p))
1146 --Count.invisi;
1147 /*
1148 * compare new flags with old flags and send string which
1149 * will cause servers to update correctly.
1150 */
1151 send_umode_out(client_p, source_p, setflags);
1152 if (showsnomask && MyConnect(source_p))
1153 sendto_one_numeric(source_p, RPL_SNOMASK, form_str(RPL_SNOMASK),
1154 construct_snobuf(source_p->snomask));
1155
1156 return (0);
1157 }
1158
1159 /*
1160 * send the MODE string for user (user) to connection client_p
1161 * -avalon
1162 */
1163 void
1164 send_umode(struct Client *client_p, struct Client *source_p, int old, int sendmask, char *umode_buf)
1165 {
1166 int i;
1167 int flag;
1168 char *m;
1169 int what = 0;
1170
1171 /*
1172 * build a string in umode_buf to represent the change in the user's
1173 * mode between the new (source_p->flag) and 'old'.
1174 */
1175 m = umode_buf;
1176 *m = '\0';
1177
1178 for (i = 0; i < 128; i++)
1179 {
1180 flag = user_modes[i];
1181
1182 if((flag & old) && !(source_p->umodes & flag))
1183 {
1184 if(what == MODE_DEL)
1185 *m++ = (char) i;
1186 else
1187 {
1188 what = MODE_DEL;
1189 *m++ = '-';
1190 *m++ = (char) i;
1191 }
1192 }
1193 else if(!(flag & old) && (source_p->umodes & flag))
1194 {
1195 if(what == MODE_ADD)
1196 *m++ = (char) i;
1197 else
1198 {
1199 what = MODE_ADD;
1200 *m++ = '+';
1201 *m++ = (char) i;
1202 }
1203 }
1204 }
1205 *m = '\0';
1206
1207 if(*umode_buf && client_p)
1208 sendto_one(client_p, ":%s MODE %s :%s", source_p->name, source_p->name, umode_buf);
1209 }
1210
1211 /*
1212 * send_umode_out
1213 *
1214 * inputs -
1215 * output - NONE
1216 * side effects -
1217 */
1218 void
1219 send_umode_out(struct Client *client_p, struct Client *source_p, int old)
1220 {
1221 struct Client *target_p;
1222 char buf[BUFSIZE];
1223 rb_dlink_node *ptr;
1224
1225 send_umode(NULL, source_p, old, 0, buf);
1226
1227 RB_DLINK_FOREACH(ptr, serv_list.head)
1228 {
1229 target_p = ptr->data;
1230
1231 if((target_p != client_p) && (target_p != source_p) && (*buf))
1232 {
1233 sendto_one(target_p, ":%s MODE %s :%s",
1234 get_id(source_p, target_p),
1235 get_id(source_p, target_p), buf);
1236 }
1237 }
1238
1239 if(client_p && MyClient(client_p))
1240 send_umode(client_p, source_p, old, 0, buf);
1241 }
1242
1243 /*
1244 * user_welcome
1245 *
1246 * inputs - client pointer to client to welcome
1247 * output - NONE
1248 * side effects -
1249 */
1250 void
1251 user_welcome(struct Client *source_p)
1252 {
1253 sendto_one_numeric(source_p, RPL_WELCOME, form_str(RPL_WELCOME), ServerInfo.network_name, source_p->name);
1254 sendto_one_numeric(source_p, RPL_YOURHOST, form_str(RPL_YOURHOST),
1255 get_listener_name(source_p->localClient->listener), ircd_version);
1256 sendto_one_numeric(source_p, RPL_CREATED, form_str(RPL_CREATED), creation);
1257 sendto_one_numeric(source_p, RPL_MYINFO, form_str(RPL_MYINFO), me.name, ircd_version, umodebuf, cflagsmyinfo);
1258
1259 show_isupport(source_p);
1260
1261 show_lusers(source_p);
1262
1263 if(ConfigFileEntry.short_motd)
1264 {
1265 sendto_one_notice(source_p, ":*** Notice -- motd was last changed at %s", user_motd_changed);
1266 sendto_one_notice(source_p, ":*** Notice -- Please read the motd if you haven't read it");
1267
1268 sendto_one(source_p, form_str(RPL_MOTDSTART),
1269 me.name, source_p->name, me.name);
1270
1271 sendto_one(source_p, form_str(RPL_MOTD),
1272 me.name, source_p->name, "*** This is the short motd ***");
1273
1274 sendto_one(source_p, form_str(RPL_ENDOFMOTD), me.name, source_p->name);
1275 }
1276 else
1277 send_user_motd(source_p);
1278 }
1279
1280 /* oper_up()
1281 *
1282 * inputs - pointer to given client to oper
1283 * - pointer to ConfItem to use
1284 * output - none
1285 * side effects - opers up source_p using aconf for reference
1286 */
1287 int
1288 oper_up(struct Client *source_p, struct oper_conf *oper_p)
1289 {
1290 unsigned int old = source_p->umodes, oldsnomask = source_p->snomask;
1291 hook_data_umode_changed hdata;
1292
1293 SetOper(source_p);
1294
1295 if(oper_p->umodes)
1296 source_p->umodes |= oper_p->umodes;
1297 else if(ConfigFileEntry.oper_umodes)
1298 source_p->umodes |= ConfigFileEntry.oper_umodes;
1299 else
1300 source_p->umodes |= DEFAULT_OPER_UMODES;
1301
1302 if (oper_p->snomask)
1303 {
1304 source_p->snomask |= oper_p->snomask;
1305 source_p->umodes |= UMODE_SERVNOTICE;
1306 }
1307 else if (source_p->umodes & UMODE_SERVNOTICE)
1308 {
1309 /* Only apply these if +s is already set -- jilles */
1310 if (ConfigFileEntry.oper_snomask)
1311 source_p->snomask |= ConfigFileEntry.oper_snomask;
1312 else
1313 source_p->snomask |= DEFAULT_OPER_SNOMASK;
1314 }
1315
1316 Count.oper++;
1317
1318 SetExemptKline(source_p);
1319
1320 source_p->flags2 |= oper_p->flags;
1321 source_p->localClient->opername = rb_strdup(oper_p->name);
1322 source_p->localClient->privset = privilegeset_ref(oper_p->privset);
1323
1324 rb_dlinkAddAlloc(source_p, &local_oper_list);
1325 rb_dlinkAddAlloc(source_p, &oper_list);
1326
1327 if(IsOperAdmin(source_p) && !IsOperHiddenAdmin(source_p))
1328 source_p->umodes |= UMODE_ADMIN;
1329 if(!IsOperN(source_p))
1330 source_p->snomask &= ~SNO_NCHANGE;
1331 if(!IsOperOperwall(source_p))
1332 source_p->umodes &= ~UMODE_OPERWALL;
1333 hdata.client = source_p;
1334 hdata.oldumodes = old;
1335 hdata.oldsnomask = oldsnomask;
1336 call_hook(h_umode_changed, &hdata);
1337
1338 sendto_realops_snomask(SNO_GENERAL, L_ALL,
1339 "%s (%s!%s@%s) is now an operator", oper_p->name, source_p->name,
1340 source_p->username, source_p->host);
1341 if(!(old & UMODE_INVISIBLE) && IsInvisible(source_p))
1342 ++Count.invisi;
1343 if((old & UMODE_INVISIBLE) && !IsInvisible(source_p))
1344 --Count.invisi;
1345 send_umode_out(source_p, source_p, old);
1346 sendto_one_numeric(source_p, RPL_SNOMASK, form_str(RPL_SNOMASK),
1347 construct_snobuf(source_p->snomask));
1348 sendto_one(source_p, form_str(RPL_YOUREOPER), me.name, source_p->name);
1349 sendto_one_notice(source_p, ":*** Oper privilege set is %s", oper_p->privset->name);
1350 sendto_one_notice(source_p, ":*** Oper privs are %s", oper_p->privset->privs);
1351 send_oper_motd(source_p);
1352
1353 return (1);
1354 }
1355
1356 /*
1357 * find_umode_slot
1358 *
1359 * inputs - NONE
1360 * outputs - an available umode bitmask or
1361 * 0 if no umodes are available
1362 * side effects - NONE
1363 */
1364 unsigned int
1365 find_umode_slot(void)
1366 {
1367 unsigned int all_umodes = 0, my_umode = 0, i;
1368
1369 for (i = 0; i < 128; i++)
1370 all_umodes |= user_modes[i];
1371
1372 for (my_umode = 1; my_umode && (all_umodes & my_umode);
1373 my_umode <<= 1);
1374
1375 return my_umode;
1376 }
1377
1378 void
1379 construct_umodebuf(void)
1380 {
1381 int i;
1382 char *ptr = umodebuf;
1383 static int prev_user_modes[128];
1384
1385 *ptr = '\0';
1386
1387 for (i = 0; i < 128; i++)
1388 {
1389 if (prev_user_modes[i] != 0 && prev_user_modes[i] != user_modes[i])
1390 {
1391 if (user_modes[i] == 0)
1392 {
1393 orphaned_umodes |= prev_user_modes[i];
1394 sendto_realops_snomask(SNO_DEBUG, L_ALL, "Umode +%c is now orphaned", i);
1395 }
1396 else
1397 {
1398 orphaned_umodes &= ~prev_user_modes[i];
1399 sendto_realops_snomask(SNO_DEBUG, L_ALL, "Orphaned umode +%c is picked up by module", i);
1400 }
1401 user_modes[i] = prev_user_modes[i];
1402 }
1403 else
1404 prev_user_modes[i] = user_modes[i];
1405 if (user_modes[i])
1406 *ptr++ = (char) i;
1407 }
1408
1409 *ptr++ = '\0';
1410 }
1411
1412 void
1413 change_nick_user_host(struct Client *target_p, const char *nick, const char *user,
1414 const char *host, int newts, const char *format, ...)
1415 {
1416 rb_dlink_node *ptr;
1417 struct Channel *chptr;
1418 struct membership *mscptr;
1419 int changed = irccmp(target_p->name, nick);
1420 int changed_case = strcmp(target_p->name, nick);
1421 int do_qjm = irccmp(target_p->username, user) || irccmp(target_p->host, host);
1422 char mode[10], modeval[NICKLEN * 2 + 2], reason[256], *mptr;
1423 va_list ap;
1424
1425 modeval[0] = '\0';
1426
1427 if(changed)
1428 {
1429 target_p->tsinfo = newts;
1430 monitor_signoff(target_p);
1431 }
1432 invalidate_bancache_user(target_p);
1433
1434 if(do_qjm)
1435 {
1436 va_start(ap, format);
1437 vsnprintf(reason, 255, format, ap);
1438 va_end(ap);
1439
1440 sendto_common_channels_local_butone(target_p, NOCAPS, ":%s!%s@%s QUIT :%s",
1441 target_p->name, target_p->username, target_p->host,
1442 reason);
1443
1444 RB_DLINK_FOREACH(ptr, target_p->user->channel.head)
1445 {
1446 mscptr = ptr->data;
1447 chptr = mscptr->chptr;
1448 mptr = mode;
1449
1450 if(is_chanop(mscptr))
1451 {
1452 *mptr++ = 'o';
1453 strcat(modeval, nick);
1454 strcat(modeval, " ");
1455 }
1456
1457 if(is_voiced(mscptr))
1458 {
1459 *mptr++ = 'v';
1460 strcat(modeval, nick);
1461 }
1462
1463 *mptr = '\0';
1464
1465 sendto_channel_local_with_capability_butone(target_p, ALL_MEMBERS, NOCAPS, CLICAP_EXTENDED_JOIN, chptr,
1466 ":%s!%s@%s JOIN %s", nick, user, host, chptr->chname);
1467 sendto_channel_local_with_capability_butone(target_p, ALL_MEMBERS, CLICAP_EXTENDED_JOIN, NOCAPS, chptr,
1468 ":%s!%s@%s JOIN %s %s :%s", nick, user, host, chptr->chname,
1469 EmptyString(target_p->user->suser) ? "*" : target_p->user->suser,
1470 target_p->info);
1471
1472 if(*mode)
1473 sendto_channel_local_butone(target_p, ALL_MEMBERS, chptr,
1474 ":%s MODE %s +%s %s",
1475 target_p->servptr->name,
1476 chptr->chname, mode, modeval);
1477
1478 *modeval = '\0';
1479 }
1480
1481 /* Resend away message to away-notify enabled clients. */
1482 if (target_p->user->away)
1483 sendto_common_channels_local_butone(target_p, CLICAP_AWAY_NOTIFY, ":%s!%s@%s AWAY :%s",
1484 target_p->name, target_p->username, target_p->host,
1485 target_p->user->away);
1486
1487 if(MyClient(target_p) && changed_case)
1488 sendto_one(target_p, ":%s!%s@%s NICK %s",
1489 target_p->name, target_p->username, target_p->host, nick);
1490
1491 /* TODO: send some snotes to SNO_NCHANGE/SNO_CCONN/SNO_CCONNEXT? */
1492 }
1493 else if(changed_case)
1494 {
1495 sendto_common_channels_local(target_p, NOCAPS, ":%s!%s@%s NICK :%s",
1496 target_p->name, target_p->username,
1497 target_p->host, nick);
1498
1499 if(MyConnect(target_p))
1500 sendto_realops_snomask(SNO_NCHANGE, L_ALL,
1501 "Nick change: From %s to %s [%s@%s]",
1502 target_p->name, nick,
1503 target_p->username, target_p->host);
1504 }
1505
1506 rb_strlcpy(target_p->username, user, sizeof target_p->username);
1507 rb_strlcpy(target_p->host, host, sizeof target_p->host);
1508
1509 if (changed)
1510 add_history(target_p, 1);
1511
1512 del_from_client_hash(target_p->name, target_p);
1513 rb_strlcpy(target_p->name, nick, NICKLEN);
1514 add_to_client_hash(target_p->name, target_p);
1515
1516 if(changed)
1517 {
1518 monitor_signon(target_p);
1519 del_all_accepts(target_p);
1520 }
1521 }