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