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