1 |
/* |
2 |
* ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd) |
3 |
* |
4 |
* Copyright (c) 1997-2020 ircd-hybrid development team |
5 |
* |
6 |
* This program is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License as published by |
8 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
19 |
* USA |
20 |
*/ |
21 |
|
22 |
/*! \file user.c |
23 |
* \brief User related functions. |
24 |
* \version $Id$ |
25 |
*/ |
26 |
|
27 |
#include "stdinc.h" |
28 |
#include "list.h" |
29 |
#include "user.h" |
30 |
#include "channel.h" |
31 |
#include "channel_mode.h" |
32 |
#include "client.h" |
33 |
#include "hash.h" |
34 |
#include "id.h" |
35 |
#include "irc_string.h" |
36 |
#include "ircd.h" |
37 |
#include "listener.h" |
38 |
#include "motd.h" |
39 |
#include "numeric.h" |
40 |
#include "conf.h" |
41 |
#include "conf_gecos.h" |
42 |
#include "log.h" |
43 |
#include "server.h" |
44 |
#include "send.h" |
45 |
#include "memory.h" |
46 |
#include "packet.h" |
47 |
#include "rng_mt.h" |
48 |
#include "misc.h" |
49 |
#include "parse.h" |
50 |
#include "monitor.h" |
51 |
#include "isupport.h" |
52 |
#include "tls.h" |
53 |
#include "patchlevel.h" |
54 |
#include "server_capab.h" /* TBR: RHOST compatibility mode */ |
55 |
|
56 |
static char umode_buffer[UMODE_MAX_STR]; |
57 |
|
58 |
const struct user_modes *umode_map[256]; |
59 |
const struct user_modes umode_tab[] = |
60 |
{ |
61 |
{ 'B', UMODE_BOT }, |
62 |
{ 'D', UMODE_DEAF }, |
63 |
{ 'F', UMODE_FARCONNECT }, |
64 |
{ 'G', UMODE_SOFTCALLERID }, |
65 |
{ 'H', UMODE_HIDDEN }, |
66 |
{ 'X', UMODE_EXPIRATION }, |
67 |
{ 'R', UMODE_REGONLY }, |
68 |
{ 'S', UMODE_SECURE }, |
69 |
{ 'W', UMODE_WEBIRC }, |
70 |
{ 'a', UMODE_ADMIN }, |
71 |
{ 'c', UMODE_CCONN }, |
72 |
{ 'd', UMODE_DEBUG }, |
73 |
{ 'e', UMODE_EXTERNAL }, |
74 |
{ 'f', UMODE_FLOOD }, |
75 |
{ 'g', UMODE_CALLERID }, |
76 |
{ 'i', UMODE_INVISIBLE }, |
77 |
{ 'j', UMODE_REJ }, |
78 |
{ 'k', UMODE_SKILL }, |
79 |
{ 'l', UMODE_LOCOPS }, |
80 |
{ 'n', UMODE_NCHANGE }, |
81 |
{ 'o', UMODE_OPER }, |
82 |
{ 'p', UMODE_HIDECHANS }, |
83 |
{ 'q', UMODE_HIDEIDLE }, |
84 |
{ 'r', UMODE_REGISTERED }, |
85 |
{ 's', UMODE_SERVNOTICE }, |
86 |
{ 'w', UMODE_WALLOP }, |
87 |
{ 'y', UMODE_SPY }, |
88 |
{ '\0', 0 } |
89 |
}; |
90 |
|
91 |
void |
92 |
user_modes_init(void) |
93 |
{ |
94 |
char *umode_buffer_ptr = umode_buffer; |
95 |
|
96 |
for (const struct user_modes *tab = umode_tab; tab->c; ++tab) |
97 |
{ |
98 |
umode_map[tab->c] = tab; |
99 |
*umode_buffer_ptr++ = tab->c; |
100 |
} |
101 |
|
102 |
*umode_buffer_ptr = '\0'; |
103 |
} |
104 |
|
105 |
/* show_lusers() |
106 |
* |
107 |
* inputs - pointer to client |
108 |
* output - NONE |
109 |
* side effects - display to client user counts etc. |
110 |
*/ |
111 |
void |
112 |
show_lusers(struct Client *client) |
113 |
{ |
114 |
if (ConfigServerHide.hide_servers == 0 || HasUMode(client, UMODE_OPER)) |
115 |
sendto_one_numeric(client, &me, RPL_LUSERCLIENT, (dlink_list_length(&global_client_list) - Count.invisi), |
116 |
Count.invisi, dlink_list_length(&global_server_list)); |
117 |
else |
118 |
sendto_one_numeric(client, &me, RPL_LUSERCLIENT, (dlink_list_length(&global_client_list) - Count.invisi), |
119 |
Count.invisi, 1); |
120 |
|
121 |
if (Count.oper) |
122 |
sendto_one_numeric(client, &me, RPL_LUSEROP, Count.oper); |
123 |
|
124 |
if (dlink_list_length(&unknown_list)) |
125 |
sendto_one_numeric(client, &me, RPL_LUSERUNKNOWN, dlink_list_length(&unknown_list)); |
126 |
|
127 |
if (dlink_list_length(channel_get_list())) |
128 |
sendto_one_numeric(client, &me, RPL_LUSERCHANNELS, dlink_list_length(channel_get_list())); |
129 |
|
130 |
if (ConfigServerHide.hide_servers == 0 || HasUMode(client, UMODE_OPER)) |
131 |
{ |
132 |
sendto_one_numeric(client, &me, RPL_LUSERME, dlink_list_length(&local_client_list), dlink_list_length(&local_server_list)); |
133 |
sendto_one_numeric(client, &me, RPL_LOCALUSERS, dlink_list_length(&local_client_list), Count.max_loc); |
134 |
sendto_one_numeric(client, &me, RPL_GLOBALUSERS, dlink_list_length(&global_client_list), Count.max_tot); |
135 |
sendto_one_numeric(client, &me, RPL_STATSCONN, Count.max_loc_con, Count.max_loc, Count.totalrestartcount); |
136 |
} |
137 |
else |
138 |
{ |
139 |
sendto_one_numeric(client, &me, RPL_LUSERME, dlink_list_length(&global_client_list), 0); |
140 |
sendto_one_numeric(client, &me, RPL_LOCALUSERS, dlink_list_length(&global_client_list), Count.max_tot); |
141 |
sendto_one_numeric(client, &me, RPL_GLOBALUSERS, dlink_list_length(&global_client_list), Count.max_tot); |
142 |
} |
143 |
} |
144 |
|
145 |
/* report_and_set_user_flags() |
146 |
* |
147 |
* inputs - pointer to client |
148 |
* - pointer to conf for this user |
149 |
* output - NONE |
150 |
* side effects - Report to user any special flags |
151 |
* they are getting, and set them. |
152 |
*/ |
153 |
static void |
154 |
report_and_set_user_flags(struct Client *client, const struct MaskItem *conf) |
155 |
{ |
156 |
/* If this user is being spoofed, tell them so */ |
157 |
if (IsConfDoSpoofIp(conf)) |
158 |
sendto_one_notice(client, &me, ":*** Spoofing your IP"); |
159 |
|
160 |
/* If this user is in the exception class, set it "E lined" */ |
161 |
if (IsConfExemptKline(conf)) |
162 |
{ |
163 |
AddFlag(client, FLAGS_EXEMPTKLINE); |
164 |
sendto_one_notice(client, &me, ":*** You are exempt from K/D lines"); |
165 |
} |
166 |
|
167 |
if (IsConfExemptXline(conf)) |
168 |
{ |
169 |
AddFlag(client, FLAGS_EXEMPTXLINE); |
170 |
sendto_one_notice(client, &me, ":*** You are exempt from X lines"); |
171 |
} |
172 |
|
173 |
if (IsConfExemptResv(conf)) |
174 |
{ |
175 |
AddFlag(client, FLAGS_EXEMPTRESV); |
176 |
sendto_one_notice(client, &me, ":*** You are exempt from resvs"); |
177 |
} |
178 |
|
179 |
/* If this user is exempt from user limits set it "F lined" */ |
180 |
if (IsConfExemptLimits(conf)) |
181 |
{ |
182 |
AddFlag(client, FLAGS_NOLIMIT); |
183 |
sendto_one_notice(client, &me, ":*** You are exempt from user limits"); |
184 |
} |
185 |
|
186 |
if (IsConfCanFlood(conf)) |
187 |
{ |
188 |
AddFlag(client, FLAGS_CANFLOOD); |
189 |
sendto_one_notice(client, &me, ":*** You are exempt from flood protection"); |
190 |
} |
191 |
} |
192 |
|
193 |
/* introduce_client() |
194 |
* |
195 |
* inputs - client |
196 |
* output - NONE |
197 |
* side effects - This common function introduces a client to the rest |
198 |
* of the net, either from a local client connect or |
199 |
* from a remote connect. |
200 |
*/ |
201 |
static void |
202 |
introduce_client(struct Client *client) |
203 |
{ |
204 |
dlink_node *node; |
205 |
char buf[UMODE_MAX_STR] = ""; |
206 |
|
207 |
send_umode(client, MyConnect(client), 0, buf); |
208 |
monitor_signon(client); |
209 |
|
210 |
if (buf[0] == '\0') |
211 |
{ |
212 |
buf[0] = '+'; |
213 |
buf[1] = '\0'; |
214 |
} |
215 |
|
216 |
DLINK_FOREACH(node, local_server_list.head) |
217 |
{ |
218 |
struct Client *server = node->data; |
219 |
|
220 |
if (server == client->from) |
221 |
continue; |
222 |
|
223 |
/* TBR: compatibility mode */ |
224 |
if (IsCapable(server, CAPAB_RHOST)) |
225 |
sendto_one(server, ":%s UID %s %u %ju %s %s %s %s %s %s %s :%s", |
226 |
client->servptr->id, |
227 |
client->name, client->hopcount+1, |
228 |
client->tsinfo, |
229 |
buf, client->username, client->host, client->realhost, |
230 |
client->sockhost, client->id, |
231 |
client->account, |
232 |
client->info); |
233 |
else |
234 |
sendto_one(server, ":%s UID %s %u %ju %s %s %s %s %s %s :%s", |
235 |
client->servptr->id, |
236 |
client->name, client->hopcount+1, |
237 |
client->tsinfo, |
238 |
buf, client->username, client->host, |
239 |
client->sockhost, client->id, |
240 |
client->account, |
241 |
client->info); |
242 |
|
243 |
if (!EmptyString(client->tls_certfp)) |
244 |
sendto_one(server, ":%s CERTFP %s", client->id, client->tls_certfp); |
245 |
} |
246 |
} |
247 |
|
248 |
/* user_welcome() |
249 |
* |
250 |
* inputs - client pointer to client to welcome |
251 |
* output - NONE |
252 |
* side effects - |
253 |
*/ |
254 |
static void |
255 |
user_welcome(struct Client *client) |
256 |
{ |
257 |
static const char built_date[] = __DATE__ " at " __TIME__; |
258 |
|
259 |
if (HasFlag(client, FLAGS_TLS)) |
260 |
{ |
261 |
AddUMode(client, UMODE_SECURE); |
262 |
|
263 |
client->tls_cipher = xstrdup(tls_get_cipher(&client->connection->fd->tls)); |
264 |
sendto_one_notice(client, &me, ":*** Connected securely via %s", |
265 |
client->tls_cipher); |
266 |
} |
267 |
|
268 |
sendto_one_numeric(client, &me, RPL_WELCOME, ConfigServerInfo.network_name, |
269 |
client->name, client->username, client->realhost); |
270 |
sendto_one_numeric(client, &me, RPL_YOURHOST, |
271 |
listener_get_name(client->connection->listener), PATCHLEVEL); |
272 |
sendto_one_numeric(client, &me, RPL_CREATED, built_date); |
273 |
sendto_one_numeric(client, &me, RPL_MYINFO, me.name, PATCHLEVEL, umode_buffer); |
274 |
|
275 |
isupport_show(client); |
276 |
show_lusers(client); |
277 |
motd_signon(client); |
278 |
} |
279 |
|
280 |
/*! \brief This function is called when both NICK and USER messages |
281 |
* have been accepted for the client, in whatever order. Only |
282 |
* after this, is the UID message propagated. |
283 |
* \param client Pointer to given client to introduce |
284 |
*/ |
285 |
void |
286 |
register_local_user(struct Client *client) |
287 |
{ |
288 |
const struct MaskItem *conf = NULL; |
289 |
|
290 |
assert(client == client->from); |
291 |
assert(client->connection->registration == 0); |
292 |
assert(MyConnect(client)); |
293 |
assert(IsUnknown(client)); |
294 |
|
295 |
if (ConfigGeneral.ping_cookie) |
296 |
{ |
297 |
if (!HasFlag(client, FLAGS_PINGSENT) && client->connection->random_ping == 0) |
298 |
{ |
299 |
do |
300 |
client->connection->random_ping = genrand_int32(); |
301 |
while (client->connection->random_ping == 0); |
302 |
|
303 |
sendto_one(client, "PING :%u", client->connection->random_ping); |
304 |
AddFlag(client, FLAGS_PINGSENT); |
305 |
return; |
306 |
} |
307 |
|
308 |
if (!HasFlag(client, FLAGS_PING_COOKIE)) |
309 |
return; |
310 |
} |
311 |
|
312 |
if (conf_check_client(client) == false) |
313 |
return; |
314 |
|
315 |
conf = client->connection->confs.head->data; |
316 |
|
317 |
if (!HasFlag(client, FLAGS_GOTID)) |
318 |
{ |
319 |
char username[USERLEN + 1]; |
320 |
unsigned int i = 0; |
321 |
|
322 |
if (IsNeedIdentd(conf)) |
323 |
{ |
324 |
++ServerStats.is_ref; |
325 |
sendto_one_notice(client, &me, ":*** Notice -- You need to install " |
326 |
"identd to use this server"); |
327 |
exit_client(client, "Install identd"); |
328 |
return; |
329 |
} |
330 |
|
331 |
strlcpy(username, client->username, sizeof(username)); |
332 |
|
333 |
if (!IsNoTilde(conf)) |
334 |
client->username[i++] = '~'; |
335 |
|
336 |
for (const char *p = username; *p && i < USERLEN; ++p) |
337 |
client->username[i++] = *p; |
338 |
|
339 |
client->username[i] = '\0'; |
340 |
} |
341 |
|
342 |
/* Password check */ |
343 |
if (!EmptyString(conf->passwd)) |
344 |
{ |
345 |
if (match_conf_password(client->connection->password, conf) == false) |
346 |
{ |
347 |
++ServerStats.is_ref; |
348 |
|
349 |
sendto_one_numeric(client, &me, ERR_PASSWDMISMATCH); |
350 |
exit_client(client, "Bad Password"); |
351 |
return; |
352 |
} |
353 |
} |
354 |
|
355 |
xfree(client->connection->password); |
356 |
client->connection->password = NULL; |
357 |
|
358 |
/* |
359 |
* Report if user has &^>= etc. and set flags as needed in client |
360 |
*/ |
361 |
report_and_set_user_flags(client, conf); |
362 |
|
363 |
if (IsDead(client)) |
364 |
return; |
365 |
|
366 |
/* |
367 |
* Limit clients - |
368 |
* We want to be able to have servers and F-line clients |
369 |
* connect, so save room for "buffer" connections. |
370 |
* Smaller servers may want to decrease this, and it should |
371 |
* probably be just a percentage of the MAXCLIENTS... |
372 |
* -Taner |
373 |
*/ |
374 |
if ((dlink_list_length(&local_client_list) >= GlobalSetOptions.maxclients + MAX_BUFFER) || |
375 |
(dlink_list_length(&local_client_list) >= GlobalSetOptions.maxclients && !HasFlag(client, FLAGS_NOLIMIT))) |
376 |
{ |
377 |
sendto_realops_flags(UMODE_REJ, L_ALL, SEND_NOTICE, |
378 |
"Too many clients, rejecting %s[%s].", |
379 |
client->name, client->host); |
380 |
++ServerStats.is_ref; |
381 |
exit_client(client, "Sorry, server is full - try later"); |
382 |
return; |
383 |
} |
384 |
|
385 |
if (valid_username(client->username, true) == false) |
386 |
{ |
387 |
char buf[sizeof("Invalid username []") + sizeof(client->username)]; |
388 |
|
389 |
sendto_realops_flags(UMODE_REJ, L_ALL, SEND_NOTICE, |
390 |
"Invalid username: %s (%s@%s)", |
391 |
client->name, client->username, client->host); |
392 |
++ServerStats.is_ref; |
393 |
snprintf(buf, sizeof(buf), "Invalid username [%s]", client->username); |
394 |
exit_client(client, buf); |
395 |
return; |
396 |
} |
397 |
|
398 |
if (!HasFlag(client, FLAGS_EXEMPTXLINE)) |
399 |
{ |
400 |
const struct GecosItem *gecos = gecos_find(client->info, match); |
401 |
if (gecos) |
402 |
{ |
403 |
sendto_realops_flags(UMODE_REJ, L_ALL, SEND_NOTICE, |
404 |
"X-line Rejecting [%s] [%s], user %s [%s]", |
405 |
client->info, gecos->reason, |
406 |
client_get_name(client, HIDE_IP), |
407 |
client->sockhost); |
408 |
++ServerStats.is_ref; |
409 |
exit_client(client, "Bad user info"); |
410 |
return; |
411 |
} |
412 |
} |
413 |
|
414 |
const char *id; |
415 |
while (hash_find_id((id = uid_get()))) |
416 |
; |
417 |
|
418 |
strlcpy(client->id, id, sizeof(client->id)); |
419 |
hash_add_id(client); |
420 |
|
421 |
sendto_realops_flags(UMODE_CCONN, L_ALL, SEND_NOTICE, |
422 |
"Client connecting: %s (%s@%s) [%s] {%s} [%s] <%s>", |
423 |
client->name, client->username, client->realhost, |
424 |
client->sockhost, |
425 |
get_client_class(&client->connection->confs), |
426 |
client->info, client->id); |
427 |
|
428 |
if (ConfigGeneral.invisible_on_connect) |
429 |
{ |
430 |
AddUMode(client, UMODE_INVISIBLE); |
431 |
++Count.invisi; |
432 |
} |
433 |
|
434 |
SetClient(client); |
435 |
|
436 |
client->servptr = &me; |
437 |
client->connection->last_privmsg = event_base->time.sec_monotonic; |
438 |
|
439 |
dlinkAdd(client, &client->lnode, &client->servptr->serv->client_list); |
440 |
dlinkAdd(client, &client->node, &global_client_list); |
441 |
|
442 |
assert(dlinkFind(&unknown_list, client)); |
443 |
|
444 |
dlink_move_node(&client->connection->lclient_node, |
445 |
&unknown_list, &local_client_list); |
446 |
|
447 |
if (dlink_list_length(&local_client_list) > Count.max_loc) |
448 |
{ |
449 |
Count.max_loc = dlink_list_length(&local_client_list); |
450 |
|
451 |
if (!(Count.max_loc % 10)) |
452 |
sendto_realops_flags(UMODE_SERVNOTICE, L_ALL, SEND_NOTICE, |
453 |
"New maximum local client connections: %u", |
454 |
Count.max_loc); |
455 |
} |
456 |
|
457 |
if ((dlink_list_length(&local_client_list) + |
458 |
dlink_list_length(&local_server_list)) > Count.max_loc_con) |
459 |
Count.max_loc_con = dlink_list_length(&local_client_list) + |
460 |
dlink_list_length(&local_server_list); |
461 |
|
462 |
if (dlink_list_length(&global_client_list) > Count.max_tot) |
463 |
Count.max_tot = dlink_list_length(&global_client_list); |
464 |
++Count.totalrestartcount; |
465 |
|
466 |
user_welcome(client); |
467 |
|
468 |
introduce_client(client); |
469 |
} |
470 |
|
471 |
/* register_remote_user() |
472 |
* |
473 |
* inputs - client remote or directly connected client |
474 |
* - username to register as |
475 |
* - host name to register as |
476 |
* - server name |
477 |
* output - NONE |
478 |
* side effects - This function is called when a remote client |
479 |
* is introduced by a server. |
480 |
*/ |
481 |
void |
482 |
register_remote_user(struct Client *client) |
483 |
{ |
484 |
assert(client->servptr->from == client->from); |
485 |
|
486 |
/* |
487 |
* If the nick has been introduced by a services server, |
488 |
* make it a service as well. |
489 |
*/ |
490 |
if (HasFlag(client->servptr, FLAGS_SERVICE)) |
491 |
AddFlag(client, FLAGS_SERVICE); |
492 |
|
493 |
SetClient(client); |
494 |
dlinkAdd(client, &client->lnode, &client->servptr->serv->client_list); |
495 |
dlinkAdd(client, &client->node, &global_client_list); |
496 |
|
497 |
if (dlink_list_length(&global_client_list) > Count.max_tot) |
498 |
Count.max_tot = dlink_list_length(&global_client_list); |
499 |
|
500 |
if (HasFlag(client->servptr, FLAGS_EOB)) |
501 |
sendto_realops_flags(UMODE_FARCONNECT, L_ALL, SEND_NOTICE, |
502 |
"Client connecting at %s: %s (%s@%s) [%s] [%s] <%s>", |
503 |
client->servptr->name, |
504 |
client->name, client->username, client->realhost, |
505 |
client->sockhost, client->info, client->id); |
506 |
|
507 |
introduce_client(client); |
508 |
} |
509 |
|
510 |
/* valid_hostname() |
511 |
* |
512 |
* Inputs - pointer to hostname |
513 |
* Output - 1 if valid, 0 if not |
514 |
* Side effects - check hostname for validity |
515 |
* |
516 |
* NOTE: this doesn't allow a hostname to begin with a dot and |
517 |
* will not allow more dots than chars. |
518 |
*/ |
519 |
bool |
520 |
valid_hostname(const char *hostname) |
521 |
{ |
522 |
const char *p = hostname; |
523 |
|
524 |
assert(p); |
525 |
|
526 |
if (EmptyString(p) || *p == '.' || *p == ':') |
527 |
return false; |
528 |
|
529 |
for (; *p; ++p) |
530 |
if (!IsHostChar(*p)) |
531 |
return false; |
532 |
|
533 |
return p - hostname <= HOSTLEN; |
534 |
} |
535 |
|
536 |
/* valid_username() |
537 |
* |
538 |
* Inputs - pointer to user |
539 |
* Output - 1 if valid, 0 if not |
540 |
* Side effects - check username for validity |
541 |
* |
542 |
* Absolutely always reject any '*' '!' '?' '@' in an user name |
543 |
* reject any odd control characters names. |
544 |
* Allow '.' in username to allow for "first.last" |
545 |
* style of username |
546 |
*/ |
547 |
bool |
548 |
valid_username(const char *username, bool local) |
549 |
{ |
550 |
const char *p = username; |
551 |
|
552 |
assert(p); |
553 |
|
554 |
if (*p == '~') |
555 |
++p; |
556 |
|
557 |
/* |
558 |
* Reject usernames that don't start with an alphanum |
559 |
* i.e. reject jokers who have '-@somehost' or '.@somehost' |
560 |
* or "-hi-@somehost", "h-----@somehost" would still be accepted. |
561 |
*/ |
562 |
if (!IsAlNum(*p)) |
563 |
return false; |
564 |
|
565 |
if (local) |
566 |
{ |
567 |
unsigned int dots = 0; |
568 |
|
569 |
while (*++p) |
570 |
{ |
571 |
if (*p == '.' && ConfigGeneral.dots_in_ident) |
572 |
{ |
573 |
if (++dots > ConfigGeneral.dots_in_ident) |
574 |
return false; |
575 |
if (!IsUserChar(*(p + 1))) |
576 |
return false; |
577 |
} |
578 |
else if (!IsUserChar(*p)) |
579 |
return false; |
580 |
} |
581 |
} |
582 |
else |
583 |
{ |
584 |
while (*++p) |
585 |
if (!IsUserChar(*p)) |
586 |
return false; |
587 |
} |
588 |
|
589 |
return p - username <= USERLEN; |
590 |
} |
591 |
|
592 |
/* clean_nick_name() |
593 |
* |
594 |
* input - nickname |
595 |
* - whether it's a local nick (1) or remote (0) |
596 |
* output - none |
597 |
* side effects - walks through the nickname, returning 0 if erroneous |
598 |
*/ |
599 |
bool |
600 |
valid_nickname(const char *nickname, bool local) |
601 |
{ |
602 |
const char *p = nickname; |
603 |
|
604 |
assert(p); |
605 |
|
606 |
/* |
607 |
* Nicks can't start with a digit or - or be 0 length. |
608 |
*/ |
609 |
if (EmptyString(p) || *p == '-' || (IsDigit(*p) && local)) |
610 |
return false; |
611 |
|
612 |
for (; *p; ++p) |
613 |
if (!IsNickChar(*p)) |
614 |
return false; |
615 |
|
616 |
return p - nickname <= NICKLEN; |
617 |
} |
618 |
|
619 |
/*! \brief Builds a mode change string to buffer pointed by \a buf |
620 |
* \param client Pointer to client |
621 |
* \param dispatch Whether to send a MODE message to client |
622 |
* \param old Old user mode to compare against when building new mode buffer |
623 |
* \param buf Pointer to buffer to build string in |
624 |
*/ |
625 |
void |
626 |
send_umode(struct Client *client, bool dispatch, unsigned int old, char *buf) |
627 |
{ |
628 |
char *m = buf; |
629 |
int what = 0; |
630 |
|
631 |
/* |
632 |
* Build a string in umode_buf to represent the change in the user's |
633 |
* mode between the new (client->umodes) and 'old'. |
634 |
*/ |
635 |
for (const struct user_modes *tab = umode_tab; tab->c; ++tab) |
636 |
{ |
637 |
if ((tab->flag & old) && !HasUMode(client, tab->flag)) |
638 |
{ |
639 |
if (what == MODE_DEL) |
640 |
*m++ = tab->c; |
641 |
else |
642 |
{ |
643 |
what = MODE_DEL; |
644 |
*m++ = '-'; |
645 |
*m++ = tab->c; |
646 |
} |
647 |
} |
648 |
else if (!(tab->flag & old) && HasUMode(client, tab->flag)) |
649 |
{ |
650 |
if (what == MODE_ADD) |
651 |
*m++ = tab->c; |
652 |
else |
653 |
{ |
654 |
what = MODE_ADD; |
655 |
*m++ = '+'; |
656 |
*m++ = tab->c; |
657 |
} |
658 |
} |
659 |
} |
660 |
|
661 |
*m = '\0'; |
662 |
|
663 |
if (dispatch == true && *buf) |
664 |
sendto_one(client, ":%s!%s@%s MODE %s :%s", |
665 |
client->name, client->username, |
666 |
client->host, client->name, buf); |
667 |
} |
668 |
|
669 |
/* send_umode_out() |
670 |
* |
671 |
* inputs - |
672 |
* output - NONE |
673 |
* side effects - Only send ubuf out to servers that know about this client |
674 |
*/ |
675 |
void |
676 |
send_umode_out(struct Client *client, unsigned int old) |
677 |
{ |
678 |
char buf[UMODE_MAX_STR] = ""; |
679 |
|
680 |
send_umode(client, MyConnect(client), old, buf); |
681 |
|
682 |
if (buf[0]) |
683 |
sendto_server(client, 0, 0, ":%s MODE %s :%s", |
684 |
client->id, client->id, buf); |
685 |
} |
686 |
|
687 |
void |
688 |
user_set_hostmask(struct Client *client, const char *hostname) |
689 |
{ |
690 |
dlink_node *node; |
691 |
|
692 |
if (strcmp(client->host, hostname) == 0) |
693 |
return; |
694 |
|
695 |
if (ConfigGeneral.cycle_on_host_change) |
696 |
sendto_common_channels_local(client, false, 0, CAP_CHGHOST, ":%s!%s@%s QUIT :Changing hostname", |
697 |
client->name, client->username, client->host); |
698 |
|
699 |
sendto_common_channels_local(client, true, CAP_CHGHOST, 0, ":%s!%s@%s CHGHOST %s %s", |
700 |
client->name, client->username, |
701 |
client->host, client->username, hostname); |
702 |
|
703 |
strlcpy(client->host, hostname, sizeof(client->host)); |
704 |
|
705 |
if (MyConnect(client)) |
706 |
{ |
707 |
sendto_one_numeric(client, &me, RPL_VISIBLEHOST, client->host); |
708 |
clear_ban_cache_list(&client->channel); |
709 |
} |
710 |
|
711 |
if (ConfigGeneral.cycle_on_host_change == 0) |
712 |
return; |
713 |
|
714 |
DLINK_FOREACH(node, client->channel.head) |
715 |
{ |
716 |
char modebuf[CMEMBER_STATUS_FLAGS_LEN + 1]; |
717 |
char nickbuf[CMEMBER_STATUS_FLAGS_LEN * NICKLEN + CMEMBER_STATUS_FLAGS_LEN] = ""; |
718 |
char *p = modebuf; |
719 |
int len = 0; |
720 |
const struct ChannelMember *member = node->data; |
721 |
|
722 |
if (member_has_flags(member, CHFL_CHANOP) == true) |
723 |
{ |
724 |
*p++ = 'o'; |
725 |
len += snprintf(nickbuf + len, sizeof(nickbuf) - len, len ? " %s" : "%s", client->name); |
726 |
} |
727 |
|
728 |
if (member_has_flags(member, CHFL_HALFOP) == true) |
729 |
{ |
730 |
*p++ = 'h'; |
731 |
len += snprintf(nickbuf + len, sizeof(nickbuf) - len, len ? " %s" : "%s", client->name); |
732 |
} |
733 |
|
734 |
if (member_has_flags(member, CHFL_VOICE) == true) |
735 |
{ |
736 |
*p++ = 'v'; |
737 |
len += snprintf(nickbuf + len, sizeof(nickbuf) - len, len ? " %s" : "%s", client->name); |
738 |
} |
739 |
|
740 |
*p = '\0'; |
741 |
|
742 |
sendto_channel_local(client, member->channel, 0, CAP_EXTENDED_JOIN, CAP_CHGHOST, ":%s!%s@%s JOIN %s %s :%s", |
743 |
client->name, client->username, |
744 |
client->host, member->channel->name, |
745 |
client->account, client->info); |
746 |
sendto_channel_local(client, member->channel, 0, 0, CAP_EXTENDED_JOIN | CAP_CHGHOST, ":%s!%s@%s JOIN :%s", |
747 |
client->name, client->username, |
748 |
client->host, member->channel->name); |
749 |
|
750 |
if (nickbuf[0]) |
751 |
sendto_channel_local(client, member->channel, 0, 0, CAP_CHGHOST, ":%s MODE %s +%s %s", |
752 |
client->servptr->name, member->channel->name, |
753 |
modebuf, nickbuf); |
754 |
} |
755 |
|
756 |
if (client->away[0]) |
757 |
sendto_common_channels_local(client, false, CAP_AWAY_NOTIFY, CAP_CHGHOST, |
758 |
":%s!%s@%s AWAY :%s", |
759 |
client->name, client->username, |
760 |
client->host, client->away); |
761 |
} |