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