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