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