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