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