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