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