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