]>
Commit | Line | Data |
---|---|---|
189935b1 | 1 | /* |
2 | * IRC - Internet Relay Chat, ircd/s_user.c (formerly ircd/s_msg.c) | |
3 | * Copyright (C) 1990 Jarkko Oikarinen and | |
4 | * University of Oulu, Computing Center | |
5 | * | |
6 | * See file AUTHORS in IRC package for additional names of | |
7 | * the programmers. | |
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 1, or (at your option) | |
12 | * 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., 675 Mass Ave, Cambridge, MA 02139, USA. | |
22 | */ | |
23 | /** @file | |
24 | * @brief Miscellaneous user-related helper functions. | |
9f8856e9 | 25 | * @version $Id: s_user.c,v 1.99.2.2 2006/02/16 03:49:54 entrope Exp $ |
189935b1 | 26 | */ |
27 | #include "config.h" | |
28 | ||
29 | #include "s_user.h" | |
30 | #include "IPcheck.h" | |
31 | #include "channel.h" | |
32 | #include "class.h" | |
33 | #include "client.h" | |
34 | #include "hash.h" | |
35 | #include "ircd.h" | |
36 | #include "ircd_alloc.h" | |
189935b1 | 37 | #include "ircd_chattr.h" |
38 | #include "ircd_features.h" | |
39 | #include "ircd_log.h" | |
40 | #include "ircd_reply.h" | |
41 | #include "ircd_snprintf.h" | |
42 | #include "ircd_string.h" | |
43 | #include "list.h" | |
44 | #include "match.h" | |
45 | #include "motd.h" | |
46 | #include "msg.h" | |
47 | #include "msgq.h" | |
48 | #include "numeric.h" | |
49 | #include "numnicks.h" | |
50 | #include "parse.h" | |
51 | #include "querycmds.h" | |
52 | #include "random.h" | |
9f8856e9 | 53 | #include "s_auth.h" |
189935b1 | 54 | #include "s_bsd.h" |
55 | #include "s_conf.h" | |
56 | #include "s_debug.h" | |
57 | #include "s_misc.h" | |
58 | #include "s_serv.h" /* max_client_count */ | |
59 | #include "send.h" | |
60 | #include "struct.h" | |
61 | #include "supported.h" | |
62 | #include "sys.h" | |
63 | #include "userload.h" | |
64 | #include "version.h" | |
65 | #include "whowas.h" | |
66 | ||
67 | #include "handlers.h" /* m_motd and m_lusers */ | |
68 | ||
69 | /* #include <assert.h> -- Now using assert in ircd_log.h */ | |
70 | #include <fcntl.h> | |
71 | #include <stdio.h> | |
72 | #include <stdlib.h> | |
73 | #include <string.h> | |
74 | #include <sys/stat.h> | |
75 | ||
76 | /** Count of allocated User structures. */ | |
77 | static int userCount = 0; | |
78 | ||
79 | /** Makes sure that \a cptr has a User information block. | |
80 | * If cli_user(cptr) != NULL, does nothing. | |
81 | * @param[in] cptr Client to attach User struct to. | |
82 | * @return User struct associated with \a cptr. | |
83 | */ | |
84 | struct User *make_user(struct Client *cptr) | |
85 | { | |
86 | assert(0 != cptr); | |
87 | ||
88 | if (!cli_user(cptr)) { | |
89 | cli_user(cptr) = (struct User*) MyMalloc(sizeof(struct User)); | |
90 | assert(0 != cli_user(cptr)); | |
91 | ||
92 | /* All variables are 0 by default */ | |
93 | memset(cli_user(cptr), 0, sizeof(struct User)); | |
94 | ++userCount; | |
95 | cli_user(cptr)->refcnt = 1; | |
96 | } | |
97 | return cli_user(cptr); | |
98 | } | |
99 | ||
100 | /** Dereference \a user. | |
101 | * User structures are reference-counted; if the refcount of \a user | |
102 | * becomes zero, free it. | |
103 | * @param[in] user User to dereference. | |
104 | */ | |
105 | void free_user(struct User* user) | |
106 | { | |
107 | assert(0 != user); | |
108 | assert(0 < user->refcnt); | |
109 | ||
110 | if (--user->refcnt == 0) { | |
111 | if (user->away) | |
112 | MyFree(user->away); | |
113 | /* | |
114 | * sanity check | |
115 | */ | |
116 | assert(0 == user->joined); | |
117 | assert(0 == user->invited); | |
118 | assert(0 == user->channel); | |
119 | ||
120 | MyFree(user); | |
121 | --userCount; | |
122 | } | |
123 | } | |
124 | ||
125 | /** Find number of User structs allocated and memory used by them. | |
126 | * @param[out] count_out Receives number of User structs allocated. | |
127 | * @param[out] bytes_out Receives number of bytes used by User structs. | |
128 | */ | |
129 | void user_count_memory(size_t* count_out, size_t* bytes_out) | |
130 | { | |
131 | assert(0 != count_out); | |
132 | assert(0 != bytes_out); | |
133 | *count_out = userCount; | |
134 | *bytes_out = userCount * sizeof(struct User); | |
135 | } | |
136 | ||
137 | ||
138 | /** Find the next client (starting at \a next) with a name that matches \a ch. | |
139 | * Normal usage loop is: | |
140 | * for (x = client; x = next_client(x,mask); x = x->next) | |
141 | * HandleMatchingClient; | |
142 | * | |
143 | * @param[in] next First client to check. | |
144 | * @param[in] ch Name mask to check against. | |
145 | * @return Next matching client found, or NULL if none. | |
146 | */ | |
147 | struct Client *next_client(struct Client *next, const char* ch) | |
148 | { | |
149 | struct Client *tmp = next; | |
150 | ||
151 | if (!tmp) | |
152 | return NULL; | |
153 | ||
154 | next = FindClient(ch); | |
155 | next = next ? next : tmp; | |
156 | if (cli_prev(tmp) == next) | |
157 | return NULL; | |
158 | if (next != tmp) | |
159 | return next; | |
160 | for (; next; next = cli_next(next)) | |
161 | if (!match(ch, cli_name(next))) | |
162 | break; | |
163 | return next; | |
164 | } | |
165 | ||
166 | /** Find the destination server for a command, and forward it if that is not us. | |
167 | * | |
168 | * \a server may be a nickname, server name, server mask (if \a from | |
169 | * is a local user) or server numnick (if \a is a server or remote | |
170 | * user). | |
171 | * | |
172 | * @param[in] from Client that sent the command to us. | |
173 | * @param[in] cmd Long-form command text. | |
174 | * @param[in] tok Token-form command text. | |
175 | * @param[in] one Client that originated the command (ignored). | |
176 | * @param[in] MustBeOper If non-zero and \a from is not an operator, return HUNTED_NOSUCH. | |
177 | * @param[in] pattern Format string of arguments to command. | |
178 | * @param[in] server Index of target name or mask in \a parv. | |
179 | * @param[in] parc Number of valid elements in \a parv (must be less than 9). | |
180 | * @param[in] parv Array of arguments to command. | |
181 | * @return One of HUNTED_ISME, HUNTED_NOSUCH or HUNTED_PASS. | |
182 | */ | |
183 | int hunt_server_cmd(struct Client *from, const char *cmd, const char *tok, | |
184 | struct Client *one, int MustBeOper, const char *pattern, | |
185 | int server, int parc, char *parv[]) | |
186 | { | |
187 | struct Client *acptr; | |
188 | char *to; | |
189 | ||
190 | /* Assume it's me, if no server or an unregistered client */ | |
191 | if (parc <= server || EmptyString((to = parv[server])) || IsUnknown(from)) | |
192 | return (HUNTED_ISME); | |
193 | ||
194 | if (MustBeOper && !IsPrivileged(from)) | |
195 | { | |
196 | send_reply(from, ERR_NOPRIVILEGES); | |
197 | return HUNTED_NOSUCH; | |
198 | } | |
199 | ||
200 | /* Make sure it's a server */ | |
201 | if (MyUser(from)) { | |
202 | /* Make sure it's a server */ | |
203 | if (!strchr(to, '*')) { | |
204 | if (0 == (acptr = FindClient(to))) { | |
205 | send_reply(from, ERR_NOSUCHSERVER, to); | |
206 | return HUNTED_NOSUCH; | |
207 | } | |
208 | ||
209 | if (cli_user(acptr)) | |
210 | acptr = cli_user(acptr)->server; | |
211 | } else if (!(acptr = find_match_server(to))) { | |
212 | send_reply(from, ERR_NOSUCHSERVER, to); | |
213 | return (HUNTED_NOSUCH); | |
214 | } | |
215 | } else if (!(acptr = FindNServer(to))) { | |
216 | send_reply(from, SND_EXPLICIT | ERR_NOSUCHSERVER, "* :Server has disconnected"); | |
217 | return (HUNTED_NOSUCH); /* Server broke off in the meantime */ | |
218 | } | |
219 | ||
220 | if (IsMe(acptr)) | |
221 | return (HUNTED_ISME); | |
222 | ||
223 | if (MustBeOper && !IsPrivileged(from)) { | |
224 | send_reply(from, ERR_NOPRIVILEGES); | |
225 | return HUNTED_NOSUCH; | |
226 | } | |
227 | ||
228 | /* assert(!IsServer(from)); */ | |
229 | ||
230 | parv[server] = (char *) acptr; /* HACK! HACK! HACK! ARGH! */ | |
231 | ||
232 | sendcmdto_one(from, cmd, tok, acptr, pattern, parv[1], parv[2], parv[3], | |
233 | parv[4], parv[5], parv[6], parv[7], parv[8]); | |
234 | ||
235 | return (HUNTED_PASS); | |
236 | } | |
237 | ||
238 | /** Find the destination server for a command, and forward it (as a | |
239 | * high-priority command) if that is not us. | |
240 | * | |
241 | * \a server may be a nickname, server name, server mask (if \a from | |
242 | * is a local user) or server numnick (if \a is a server or remote | |
243 | * user). | |
244 | * Unlike hunt_server_cmd(), this appends the message to the | |
245 | * high-priority message queue for the destination server. | |
246 | * | |
247 | * @param[in] from Client that sent the command to us. | |
248 | * @param[in] cmd Long-form command text. | |
249 | * @param[in] tok Token-form command text. | |
250 | * @param[in] one Client that originated the command (ignored). | |
251 | * @param[in] MustBeOper If non-zero and \a from is not an operator, return HUNTED_NOSUCH. | |
252 | * @param[in] pattern Format string of arguments to command. | |
253 | * @param[in] server Index of target name or mask in \a parv. | |
254 | * @param[in] parc Number of valid elements in \a parv (must be less than 9). | |
255 | * @param[in] parv Array of arguments to command. | |
256 | * @return One of HUNTED_ISME, HUNTED_NOSUCH or HUNTED_PASS. | |
257 | */ | |
258 | int hunt_server_prio_cmd(struct Client *from, const char *cmd, const char *tok, | |
259 | struct Client *one, int MustBeOper, | |
260 | const char *pattern, int server, int parc, | |
261 | char *parv[]) | |
262 | { | |
263 | struct Client *acptr; | |
264 | char *to; | |
265 | ||
266 | /* Assume it's me, if no server or an unregistered client */ | |
267 | if (parc <= server || EmptyString((to = parv[server])) || IsUnknown(from)) | |
268 | return (HUNTED_ISME); | |
269 | ||
270 | /* Make sure it's a server */ | |
271 | if (MyUser(from)) { | |
272 | /* Make sure it's a server */ | |
273 | if (!strchr(to, '*')) { | |
274 | if (0 == (acptr = FindClient(to))) { | |
275 | send_reply(from, ERR_NOSUCHSERVER, to); | |
276 | return HUNTED_NOSUCH; | |
277 | } | |
278 | ||
279 | if (cli_user(acptr)) | |
280 | acptr = cli_user(acptr)->server; | |
281 | } else if (!(acptr = find_match_server(to))) { | |
282 | send_reply(from, ERR_NOSUCHSERVER, to); | |
283 | return (HUNTED_NOSUCH); | |
284 | } | |
285 | } else if (!(acptr = FindNServer(to))) | |
286 | return (HUNTED_NOSUCH); /* Server broke off in the meantime */ | |
287 | ||
288 | if (IsMe(acptr)) | |
289 | return (HUNTED_ISME); | |
290 | ||
291 | if (MustBeOper && !IsPrivileged(from)) { | |
292 | send_reply(from, ERR_NOPRIVILEGES); | |
293 | return HUNTED_NOSUCH; | |
294 | } | |
295 | ||
296 | /* assert(!IsServer(from)); SETTIME to particular destinations permitted */ | |
297 | ||
298 | parv[server] = (char *) acptr; /* HACK! HACK! HACK! ARGH! */ | |
299 | ||
300 | sendcmdto_prio_one(from, cmd, tok, acptr, pattern, parv[1], parv[2], parv[3], | |
301 | parv[4], parv[5], parv[6], parv[7], parv[8]); | |
302 | ||
303 | return (HUNTED_PASS); | |
304 | } | |
305 | ||
306 | ||
189935b1 | 307 | /* |
308 | * register_user | |
309 | * | |
310 | * This function is called when both NICK and USER messages | |
311 | * have been accepted for the client, in whatever order. Only | |
312 | * after this the USER message is propagated. | |
313 | * | |
314 | * NICK's must be propagated at once when received, although | |
315 | * it would be better to delay them too until full info is | |
316 | * available. Doing it is not so simple though, would have | |
317 | * to implement the following: | |
318 | * | |
319 | * 1) user telnets in and gives only "NICK foobar" and waits | |
320 | * 2) another user far away logs in normally with the nick | |
321 | * "foobar" (quite legal, as this server didn't propagate it). | |
322 | * 3) now this server gets nick "foobar" from outside, but | |
323 | * has already the same defined locally. Current server | |
324 | * would just issue "KILL foobar" to clean out dups. But, | |
325 | * this is not fair. It should actually request another | |
326 | * nick from local user or kill him/her... | |
327 | */ | |
328 | /** Finish registering a user who has sent both NICK and USER. | |
329 | * For local connections, possibly check IAuth; make sure there is a | |
330 | * matching Client config block; clean the username field; check | |
331 | * K/k-lines; check for "hacked" looking usernames; assign a numnick; | |
332 | * and send greeting (WELCOME, ISUPPORT, MOTD, etc). | |
333 | * For all connections, update the invisible user and operator counts; | |
334 | * run IPcheck against their address; and forward the NICK. | |
335 | * | |
336 | * @param[in] cptr Client who introduced the user. | |
337 | * @param[in,out] sptr Client who has been fully introduced. | |
189935b1 | 338 | * @return Zero or CPTR_KILLED. |
339 | */ | |
9f8856e9 | 340 | int register_user(struct Client *cptr, struct Client *sptr) |
189935b1 | 341 | { |
189935b1 | 342 | char* parv[4]; |
343 | char* tmpstr; | |
189935b1 | 344 | struct User* user = cli_user(sptr); |
189935b1 | 345 | char ip_base64[25]; |
346 | ||
347 | user->last = CurrentTime; | |
348 | parv[0] = cli_name(sptr); | |
349 | parv[1] = parv[2] = NULL; | |
350 | ||
351 | if (MyConnect(sptr)) | |
352 | { | |
189935b1 | 353 | assert(cptr == sptr); |
189935b1 | 354 | |
189935b1 | 355 | Count_unknownbecomesclient(sptr, UserStats); |
356 | ||
357 | SetUser(sptr); | |
358 | cli_handler(sptr) = CLIENT_HANDLER; | |
359 | SetLocalNumNick(sptr); | |
360 | send_reply(sptr, | |
361 | RPL_WELCOME, | |
362 | feature_str(FEAT_NETWORK), | |
363 | feature_str(FEAT_PROVIDER) ? " via " : "", | |
364 | feature_str(FEAT_PROVIDER) ? feature_str(FEAT_PROVIDER) : "", | |
9f8856e9 | 365 | cli_name(sptr)); |
189935b1 | 366 | /* |
367 | * This is a duplicate of the NOTICE but see below... | |
368 | */ | |
369 | send_reply(sptr, RPL_YOURHOST, cli_name(&me), version); | |
370 | send_reply(sptr, RPL_CREATED, creation); | |
371 | send_reply(sptr, RPL_MYINFO, cli_name(&me), version, infousermodes, | |
372 | infochanmodes, infochanmodeswithparams); | |
373 | send_supported(sptr); | |
374 | m_lusers(sptr, sptr, 1, parv); | |
375 | update_load(); | |
376 | motd_signon(sptr); | |
377 | if (cli_snomask(sptr) & SNO_NOISY) | |
378 | set_snomask(sptr, cli_snomask(sptr) & SNO_NOISY, SNO_ADD); | |
379 | if (feature_bool(FEAT_CONNEXIT_NOTICES)) | |
380 | sendto_opmask_butone(0, SNO_CONNEXIT, | |
381 | "Client connecting: %s (%s@%s) [%s] {%s} [%s] <%s%s>", | |
382 | cli_name(sptr), user->username, user->host, | |
383 | cli_sock_ip(sptr), get_client_class(sptr), | |
384 | cli_info(sptr), NumNick(cptr) /* two %s's */); | |
385 | ||
386 | IPcheck_connect_succeeded(sptr); | |
387 | /* | |
388 | * Set user's initial modes | |
389 | */ | |
390 | tmpstr = (char*)client_get_default_umode(sptr); | |
391 | if (tmpstr) for (; *tmpstr; ++tmpstr) { | |
392 | switch (*tmpstr) { | |
393 | case 's': | |
394 | if (!feature_bool(FEAT_HIS_SNOTICES_OPER_ONLY)) { | |
395 | SetServNotice(sptr); | |
396 | set_snomask(sptr, SNO_DEFAULT, SNO_SET); | |
397 | } | |
398 | break; | |
399 | case 'w': | |
400 | if (!feature_bool(FEAT_WALLOPS_OPER_ONLY)) | |
401 | SetWallops(sptr); | |
402 | break; | |
403 | case 'i': | |
404 | SetInvisible(sptr); | |
405 | break; | |
406 | case 'd': | |
407 | SetDeaf(sptr); | |
408 | break; | |
409 | case 'g': | |
410 | if (!feature_bool(FEAT_HIS_DEBUG_OPER_ONLY)) | |
411 | SetDebug(sptr); | |
412 | break; | |
413 | } | |
414 | } | |
415 | } | |
416 | else { | |
9f8856e9 | 417 | struct Client *acptr = user->server; |
189935b1 | 418 | |
9f8856e9 | 419 | Count_newremoteclient(UserStats, acptr); |
189935b1 | 420 | |
189935b1 | 421 | if (cli_from(acptr) != cli_from(sptr)) |
422 | { | |
423 | sendcmdto_one(&me, CMD_KILL, cptr, "%C :%s (%s != %s[%s])", | |
424 | sptr, cli_name(&me), cli_name(user->server), cli_name(cli_from(acptr)), | |
425 | cli_sockhost(cli_from(acptr))); | |
426 | SetFlag(sptr, FLAG_KILLED); | |
427 | return exit_client(cptr, sptr, &me, "NICK server wrong direction"); | |
428 | } | |
429 | else if (HasFlag(acptr, FLAG_TS8)) | |
430 | SetFlag(sptr, FLAG_TS8); | |
431 | ||
432 | /* | |
433 | * Check to see if this user is being propagated | |
434 | * as part of a net.burst, or is using protocol 9. | |
435 | * FIXME: This can be sped up - its stupid to check it for | |
436 | * every NICK message in a burst again --Run. | |
437 | */ | |
438 | for (; acptr != &me; acptr = cli_serv(acptr)->up) | |
439 | { | |
440 | if (IsBurst(acptr) || Protocol(acptr) < 10) | |
441 | break; | |
442 | } | |
443 | if (!IPcheck_remote_connect(sptr, (acptr != &me))) | |
444 | { | |
445 | /* | |
446 | * We ran out of bits to count this | |
447 | */ | |
448 | sendcmdto_one(&me, CMD_KILL, sptr, "%C :%s (Too many connections from your host -- Ghost)", | |
449 | sptr, cli_name(&me)); | |
450 | return exit_client(cptr, sptr, &me,"Too many connections from your host -- throttled"); | |
451 | } | |
452 | SetUser(sptr); | |
453 | } | |
454 | ||
455 | if (IsInvisible(sptr)) | |
456 | ++UserStats.inv_clients; | |
457 | if (IsOper(sptr)) | |
458 | ++UserStats.opers; | |
459 | ||
460 | tmpstr = umode_str(sptr); | |
461 | /* Send full IP address to IPv6-grokking servers. */ | |
462 | sendcmdto_flag_serv_butone(user->server, CMD_NICK, cptr, | |
463 | FLAG_IPV6, FLAG_LAST_FLAG, | |
464 | "%s %d %Tu %s %s %s%s%s%s %s%s :%s", | |
9f8856e9 | 465 | cli_name(sptr), cli_hopcount(sptr) + 1, |
466 | cli_lastnick(sptr), | |
189935b1 | 467 | user->username, user->realhost, |
468 | *tmpstr ? "+" : "", tmpstr, *tmpstr ? " " : "", | |
469 | iptobase64(ip_base64, &cli_ip(sptr), sizeof(ip_base64), 1), | |
470 | NumNick(sptr), cli_info(sptr)); | |
471 | /* Send fake IPv6 addresses to pre-IPv6 servers. */ | |
472 | sendcmdto_flag_serv_butone(user->server, CMD_NICK, cptr, | |
473 | FLAG_LAST_FLAG, FLAG_IPV6, | |
474 | "%s %d %Tu %s %s %s%s%s%s %s%s :%s", | |
9f8856e9 | 475 | cli_name(sptr), cli_hopcount(sptr) + 1, |
476 | cli_lastnick(sptr), | |
189935b1 | 477 | user->username, user->realhost, |
478 | *tmpstr ? "+" : "", tmpstr, *tmpstr ? " " : "", | |
479 | iptobase64(ip_base64, &cli_ip(sptr), sizeof(ip_base64), 0), | |
480 | NumNick(sptr), cli_info(sptr)); | |
481 | ||
482 | /* Send user mode to client */ | |
483 | if (MyUser(sptr)) | |
484 | { | |
485 | static struct Flags flags; /* automatically initialized to zeros */ | |
052b069e | 486 | /* To avoid sending +r to the client due to auth-on-connect, set |
487 | * the "old" FLAG_ACCOUNT bit to match the client's value. | |
488 | */ | |
489 | if (IsAccount(cptr)) | |
490 | FlagSet(&flags, FLAG_ACCOUNT); | |
491 | else | |
492 | FlagClr(&flags, FLAG_ACCOUNT); | |
189935b1 | 493 | send_umode(cptr, sptr, &flags, ALL_UMODES); |
494 | if ((cli_snomask(sptr) != SNO_DEFAULT) && HasFlag(sptr, FLAG_SERVNOTICE)) | |
495 | send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr)); | |
496 | } | |
497 | return 0; | |
498 | } | |
499 | ||
500 | /** List of user mode characters. */ | |
501 | static const struct UserMode { | |
502 | unsigned int flag; /**< User mode constant. */ | |
503 | char c; /**< Character corresponding to the mode. */ | |
504 | } userModeList[] = { | |
505 | { FLAG_OPER, 'o' }, | |
506 | { FLAG_LOCOP, 'O' }, | |
507 | { FLAG_INVISIBLE, 'i' }, | |
508 | { FLAG_WALLOP, 'w' }, | |
509 | { FLAG_SERVNOTICE, 's' }, | |
510 | { FLAG_DEAF, 'd' }, | |
511 | { FLAG_CHSERV, 'k' }, | |
512 | { FLAG_DEBUG, 'g' }, | |
513 | { FLAG_ACCOUNT, 'r' }, | |
514 | { FLAG_HIDDENHOST, 'x' } | |
515 | }; | |
516 | ||
517 | /** Length of #userModeList. */ | |
518 | #define USERMODELIST_SIZE sizeof(userModeList) / sizeof(struct UserMode) | |
519 | ||
520 | /* | |
521 | * XXX - find a way to get rid of this | |
522 | */ | |
523 | /** Nasty global buffer used for communications with umode_str() and others. */ | |
524 | static char umodeBuf[BUFSIZE]; | |
525 | ||
526 | /** Try to set a user's nickname. | |
527 | * If \a sptr is a server, the client is being introduced for the first time. | |
528 | * @param[in] cptr Client to set nickname. | |
529 | * @param[in] sptr Client sending the NICK. | |
530 | * @param[in] nick New nickname. | |
531 | * @param[in] parc Number of arguments to NICK. | |
532 | * @param[in] parv Argument list to NICK. | |
533 | * @return CPTR_KILLED if \a cptr was killed, else 0. | |
534 | */ | |
535 | int set_nick_name(struct Client* cptr, struct Client* sptr, | |
536 | const char* nick, int parc, char* parv[]) | |
537 | { | |
538 | if (IsServer(sptr)) { | |
539 | int i; | |
540 | const char* account = 0; | |
541 | const char* p; | |
542 | ||
543 | /* | |
544 | * A server introducing a new client, change source | |
545 | */ | |
546 | struct Client* new_client = make_client(cptr, STAT_UNKNOWN); | |
547 | assert(0 != new_client); | |
548 | ||
549 | cli_hopcount(new_client) = atoi(parv[2]); | |
550 | cli_lastnick(new_client) = atoi(parv[3]); | |
551 | if (Protocol(cptr) > 9 && parc > 7 && *parv[6] == '+') | |
552 | { | |
553 | for (p = parv[6] + 1; *p; p++) | |
554 | { | |
555 | for (i = 0; i < USERMODELIST_SIZE; ++i) | |
556 | { | |
557 | if (userModeList[i].c == *p) | |
558 | { | |
559 | SetFlag(new_client, userModeList[i].flag); | |
560 | if (userModeList[i].flag == FLAG_ACCOUNT) | |
561 | account = parv[7]; | |
562 | break; | |
563 | } | |
564 | } | |
565 | } | |
566 | } | |
567 | client_set_privs(new_client, NULL); /* set privs on user */ | |
568 | /* | |
569 | * Set new nick name. | |
570 | */ | |
571 | strcpy(cli_name(new_client), nick); | |
572 | cli_user(new_client) = make_user(new_client); | |
573 | cli_user(new_client)->server = sptr; | |
574 | SetRemoteNumNick(new_client, parv[parc - 2]); | |
575 | /* | |
576 | * IP# of remote client | |
577 | */ | |
578 | base64toip(parv[parc - 3], &cli_ip(new_client)); | |
579 | ||
580 | add_client_to_list(new_client); | |
581 | hAddClient(new_client); | |
582 | ||
583 | cli_serv(sptr)->ghost = 0; /* :server NICK means end of net.burst */ | |
584 | ircd_strncpy(cli_username(new_client), parv[4], USERLEN); | |
9f8856e9 | 585 | ircd_strncpy(cli_user(new_client)->username, parv[4], USERLEN); |
189935b1 | 586 | ircd_strncpy(cli_user(new_client)->host, parv[5], HOSTLEN); |
587 | ircd_strncpy(cli_user(new_client)->realhost, parv[5], HOSTLEN); | |
588 | ircd_strncpy(cli_info(new_client), parv[parc - 1], REALLEN); | |
589 | if (account) { | |
590 | int len = ACCOUNTLEN; | |
591 | if ((p = strchr(account, ':'))) { | |
592 | len = (p++) - account; | |
593 | cli_user(new_client)->acc_create = atoi(p); | |
594 | Debug((DEBUG_DEBUG, "Received timestamped account in user mode; " | |
595 | "account \"%s\", timestamp %Tu", account, | |
596 | cli_user(new_client)->acc_create)); | |
597 | } | |
598 | ircd_strncpy(cli_user(new_client)->account, account, len); | |
599 | } | |
600 | if (HasHiddenHost(new_client)) | |
601 | ircd_snprintf(0, cli_user(new_client)->host, HOSTLEN, "%s.%s", | |
602 | account, feature_str(FEAT_HIDDEN_HOST)); | |
603 | ||
9f8856e9 | 604 | return register_user(cptr, new_client); |
189935b1 | 605 | } |
606 | else if ((cli_name(sptr))[0]) { | |
607 | /* | |
608 | * Client changing its nick | |
609 | * | |
610 | * If the client belongs to me, then check to see | |
611 | * if client is on any channels where it is currently | |
612 | * banned. If so, do not allow the nick change to occur. | |
613 | */ | |
614 | if (MyUser(sptr)) { | |
615 | const char* channel_name; | |
616 | struct Membership *member; | |
617 | if ((channel_name = find_no_nickchange_channel(sptr))) { | |
618 | return send_reply(cptr, ERR_BANNICKCHANGE, channel_name); | |
619 | } | |
620 | /* | |
621 | * Refuse nick change if the last nick change was less | |
622 | * then 30 seconds ago. This is intended to get rid of | |
623 | * clone bots doing NICK FLOOD. -SeKs | |
624 | * If someone didn't change their nick for more then 60 seconds | |
625 | * however, allow to do two nick changes immediately after another | |
626 | * before limiting the nick flood. -Run | |
627 | */ | |
628 | if (CurrentTime < cli_nextnick(cptr)) | |
629 | { | |
630 | cli_nextnick(cptr) += 2; | |
631 | send_reply(cptr, ERR_NICKTOOFAST, parv[1], | |
632 | cli_nextnick(cptr) - CurrentTime); | |
633 | /* Send error message */ | |
634 | sendcmdto_one(cptr, CMD_NICK, cptr, "%s", cli_name(cptr)); | |
635 | /* bounce NICK to user */ | |
636 | return 0; /* ignore nick change! */ | |
637 | } | |
638 | else { | |
639 | /* Limit total to 1 change per NICK_DELAY seconds: */ | |
640 | cli_nextnick(cptr) += NICK_DELAY; | |
641 | /* However allow _maximal_ 1 extra consecutive nick change: */ | |
642 | if (cli_nextnick(cptr) < CurrentTime) | |
643 | cli_nextnick(cptr) = CurrentTime; | |
644 | } | |
645 | /* Invalidate all bans against the user so we check them again */ | |
646 | for (member = (cli_user(cptr))->channel; member; | |
647 | member = member->next_channel) | |
648 | ClearBanValid(member); | |
649 | } | |
650 | /* | |
651 | * Also set 'lastnick' to current time, if changed. | |
652 | */ | |
653 | if (0 != ircd_strcmp(parv[0], nick)) | |
654 | cli_lastnick(sptr) = (sptr == cptr) ? TStime() : atoi(parv[2]); | |
655 | ||
656 | /* | |
657 | * Client just changing his/her nick. If he/she is | |
658 | * on a channel, send note of change to all clients | |
659 | * on that channel. Propagate notice to other servers. | |
660 | */ | |
661 | if (IsUser(sptr)) { | |
662 | sendcmdto_common_channels_butone(sptr, CMD_NICK, NULL, ":%s", nick); | |
663 | add_history(sptr, 1); | |
664 | sendcmdto_serv_butone(sptr, CMD_NICK, cptr, "%s %Tu", nick, | |
665 | cli_lastnick(sptr)); | |
666 | } | |
667 | else | |
668 | sendcmdto_one(sptr, CMD_NICK, sptr, ":%s", nick); | |
669 | ||
670 | if ((cli_name(sptr))[0]) | |
671 | hRemClient(sptr); | |
672 | strcpy(cli_name(sptr), nick); | |
673 | hAddClient(sptr); | |
674 | } | |
675 | else { | |
676 | /* Local client setting NICK the first time */ | |
189935b1 | 677 | strcpy(cli_name(sptr), nick); |
189935b1 | 678 | hAddClient(sptr); |
9f8856e9 | 679 | return auth_set_nick(cli_auth(sptr), nick); |
189935b1 | 680 | } |
681 | return 0; | |
682 | } | |
683 | ||
684 | /** Calculate the hash value for a target. | |
685 | * @param[in] target Pointer to target, cast to unsigned int. | |
686 | * @return Hash value constructed from the pointer. | |
687 | */ | |
688 | static unsigned char hash_target(unsigned int target) | |
689 | { | |
690 | return (unsigned char) (target >> 16) ^ (target >> 8); | |
691 | } | |
692 | ||
693 | /** Records \a target as a recent target for \a sptr. | |
694 | * @param[in] sptr User who has sent to a new target. | |
695 | * @param[in] target Target to add. | |
696 | */ | |
697 | void | |
698 | add_target(struct Client *sptr, void *target) | |
699 | { | |
700 | /* Ok, this shouldn't work esp on alpha | |
701 | */ | |
702 | unsigned char hash = hash_target((unsigned long) target); | |
703 | unsigned char* targets; | |
704 | int i; | |
705 | assert(0 != sptr); | |
706 | assert(cli_local(sptr)); | |
707 | ||
708 | targets = cli_targets(sptr); | |
709 | ||
710 | /* | |
711 | * Already in table? | |
712 | */ | |
713 | for (i = 0; i < MAXTARGETS; ++i) { | |
714 | if (targets[i] == hash) | |
715 | return; | |
716 | } | |
717 | /* | |
718 | * New target | |
719 | */ | |
720 | memmove(&targets[RESERVEDTARGETS + 1], | |
721 | &targets[RESERVEDTARGETS], MAXTARGETS - RESERVEDTARGETS - 1); | |
722 | targets[RESERVEDTARGETS] = hash; | |
723 | } | |
724 | ||
725 | /** Check whether \a sptr can send to or join \a target yet. | |
726 | * @param[in] sptr User trying to join a channel or send a message. | |
727 | * @param[in] target Target of the join or message. | |
728 | * @param[in] name Name of the target. | |
729 | * @param[in] created If non-zero, trying to join a new channel. | |
730 | * @return Non-zero if too many target changes; zero if okay to send. | |
731 | */ | |
732 | int check_target_limit(struct Client *sptr, void *target, const char *name, | |
733 | int created) | |
734 | { | |
735 | unsigned char hash = hash_target((unsigned long) target); | |
736 | int i; | |
737 | unsigned char* targets; | |
738 | ||
739 | assert(0 != sptr); | |
740 | assert(cli_local(sptr)); | |
741 | targets = cli_targets(sptr); | |
742 | ||
743 | /* If user is invited to channel, give him/her a free target */ | |
744 | if (IsChannelName(name) && IsInvited(sptr, target)) | |
745 | return 0; | |
746 | ||
747 | /* | |
748 | * Same target as last time? | |
749 | */ | |
750 | if (targets[0] == hash) | |
751 | return 0; | |
752 | for (i = 1; i < MAXTARGETS; ++i) { | |
753 | if (targets[i] == hash) { | |
754 | memmove(&targets[1], &targets[0], i); | |
755 | targets[0] = hash; | |
756 | return 0; | |
757 | } | |
758 | } | |
759 | /* | |
760 | * New target | |
761 | */ | |
762 | if (!created) { | |
763 | if (CurrentTime < cli_nexttarget(sptr)) { | |
764 | if (cli_nexttarget(sptr) - CurrentTime < TARGET_DELAY + 8) { | |
765 | /* | |
766 | * No server flooding | |
767 | */ | |
768 | cli_nexttarget(sptr) += 2; | |
769 | send_reply(sptr, ERR_TARGETTOOFAST, name, | |
770 | cli_nexttarget(sptr) - CurrentTime); | |
771 | } | |
772 | return 1; | |
773 | } | |
774 | else { | |
775 | cli_nexttarget(sptr) += TARGET_DELAY; | |
776 | if (cli_nexttarget(sptr) < CurrentTime - (TARGET_DELAY * (MAXTARGETS - 1))) | |
777 | cli_nexttarget(sptr) = CurrentTime - (TARGET_DELAY * (MAXTARGETS - 1)); | |
778 | } | |
779 | } | |
780 | memmove(&targets[1], &targets[0], MAXTARGETS - 1); | |
781 | targets[0] = hash; | |
782 | return 0; | |
783 | } | |
784 | ||
785 | /** Allows a channel operator to avoid target change checks when | |
786 | * sending messages to users on their channel. | |
787 | * @param[in] source User sending the message. | |
788 | * @param[in] nick Destination of the message. | |
789 | * @param[in] channel Name of channel being sent to. | |
790 | * @param[in] text Message to send. | |
791 | * @param[in] is_notice If non-zero, use CNOTICE instead of CPRIVMSG. | |
792 | */ | |
793 | /* Added 971023 by Run. */ | |
794 | int whisper(struct Client* source, const char* nick, const char* channel, | |
795 | const char* text, int is_notice) | |
796 | { | |
797 | struct Client* dest; | |
798 | struct Channel* chptr; | |
799 | struct Membership* membership; | |
800 | ||
801 | assert(0 != source); | |
802 | assert(0 != nick); | |
803 | assert(0 != channel); | |
804 | assert(MyUser(source)); | |
805 | ||
806 | if (!(dest = FindUser(nick))) { | |
807 | return send_reply(source, ERR_NOSUCHNICK, nick); | |
808 | } | |
809 | if (!(chptr = FindChannel(channel))) { | |
810 | return send_reply(source, ERR_NOSUCHCHANNEL, channel); | |
811 | } | |
812 | /* | |
813 | * compare both users channel lists, instead of the channels user list | |
814 | * since the link is the same, this should be a little faster for channels | |
815 | * with a lot of users | |
816 | */ | |
817 | for (membership = cli_user(source)->channel; membership; membership = membership->next_channel) { | |
818 | if (chptr == membership->channel) | |
819 | break; | |
820 | } | |
821 | if (0 == membership) { | |
822 | return send_reply(source, ERR_NOTONCHANNEL, chptr->chname); | |
823 | } | |
824 | if (!IsVoicedOrOpped(membership)) { | |
825 | return send_reply(source, ERR_VOICENEEDED, chptr->chname); | |
826 | } | |
827 | /* | |
828 | * lookup channel in destination | |
829 | */ | |
830 | assert(0 != cli_user(dest)); | |
831 | for (membership = cli_user(dest)->channel; membership; membership = membership->next_channel) { | |
832 | if (chptr == membership->channel) | |
833 | break; | |
834 | } | |
835 | if (0 == membership || IsZombie(membership)) { | |
836 | return send_reply(source, ERR_USERNOTINCHANNEL, cli_name(dest), chptr->chname); | |
837 | } | |
838 | if (is_silenced(source, dest)) | |
839 | return 0; | |
840 | ||
841 | if (cli_user(dest)->away) | |
842 | send_reply(source, RPL_AWAY, cli_name(dest), cli_user(dest)->away); | |
843 | if (is_notice) | |
844 | sendcmdto_one(source, CMD_NOTICE, dest, "%C :%s", dest, text); | |
845 | else | |
846 | sendcmdto_one(source, CMD_PRIVATE, dest, "%C :%s", dest, text); | |
847 | return 0; | |
848 | } | |
849 | ||
850 | ||
851 | /** Send a user mode change for \a cptr to neighboring servers. | |
852 | * @param[in] cptr User whose mode is changing. | |
853 | * @param[in] sptr Client who sent us the mode change message. | |
854 | * @param[in] old Prior set of user flags. | |
855 | * @param[in] prop If non-zero, also include FLAG_OPER. | |
856 | */ | |
857 | void send_umode_out(struct Client *cptr, struct Client *sptr, | |
858 | struct Flags *old, int prop) | |
859 | { | |
860 | int i; | |
861 | struct Client *acptr; | |
862 | ||
863 | send_umode(NULL, sptr, old, prop ? SEND_UMODES : SEND_UMODES_BUT_OPER); | |
864 | ||
865 | for (i = HighestFd; i >= 0; i--) | |
866 | { | |
867 | if ((acptr = LocalClientArray[i]) && IsServer(acptr) && | |
868 | (acptr != cptr) && (acptr != sptr) && *umodeBuf) | |
869 | sendcmdto_one(sptr, CMD_MODE, acptr, "%s :%s", cli_name(sptr), umodeBuf); | |
870 | } | |
871 | if (cptr && MyUser(cptr)) | |
872 | send_umode(cptr, sptr, old, ALL_UMODES); | |
873 | } | |
874 | ||
875 | ||
876 | /** Call \a fmt for each Client named in \a names. | |
877 | * @param[in] sptr Client requesting information. | |
878 | * @param[in] names Space-delimited list of nicknames. | |
879 | * @param[in] rpl Base reply string for messages. | |
880 | * @param[in] fmt Formatting callback function. | |
881 | */ | |
882 | void send_user_info(struct Client* sptr, char* names, int rpl, InfoFormatter fmt) | |
883 | { | |
884 | char* name; | |
885 | char* p = 0; | |
886 | int arg_count = 0; | |
887 | int users_found = 0; | |
888 | struct Client* acptr; | |
889 | struct MsgBuf* mb; | |
890 | ||
891 | assert(0 != sptr); | |
892 | assert(0 != names); | |
893 | assert(0 != fmt); | |
894 | ||
895 | mb = msgq_make(sptr, rpl_str(rpl), cli_name(&me), cli_name(sptr)); | |
896 | ||
897 | for (name = ircd_strtok(&p, names, " "); name; name = ircd_strtok(&p, 0, " ")) { | |
898 | if ((acptr = FindUser(name))) { | |
899 | if (users_found++) | |
900 | msgq_append(0, mb, " "); | |
901 | (*fmt)(acptr, sptr, mb); | |
902 | } | |
903 | if (5 == ++arg_count) | |
904 | break; | |
905 | } | |
906 | send_buffer(sptr, mb, 0); | |
907 | msgq_clean(mb); | |
908 | } | |
909 | ||
910 | /** Set \a flag on \a cptr and possibly hide the client's hostmask. | |
911 | * @param[in,out] cptr User who is getting a new flag. | |
912 | * @param[in] flag Some flag that affects host-hiding (FLAG_HIDDENHOST, FLAG_ACCOUNT). | |
913 | * @return Zero. | |
914 | */ | |
915 | int | |
916 | hide_hostmask(struct Client *cptr, unsigned int flag) | |
917 | { | |
918 | struct Membership *chan; | |
919 | ||
920 | switch (flag) { | |
921 | case FLAG_HIDDENHOST: | |
922 | /* Local users cannot set +x unless FEAT_HOST_HIDING is true. */ | |
923 | if (MyConnect(cptr) && !feature_bool(FEAT_HOST_HIDING)) | |
924 | return 0; | |
925 | break; | |
926 | case FLAG_ACCOUNT: | |
927 | /* Invalidate all bans against the user so we check them again */ | |
928 | for (chan = (cli_user(cptr))->channel; chan; | |
929 | chan = chan->next_channel) | |
930 | ClearBanValid(chan); | |
931 | break; | |
932 | default: | |
933 | return 0; | |
934 | } | |
935 | ||
936 | SetFlag(cptr, flag); | |
937 | if (!HasFlag(cptr, FLAG_HIDDENHOST) || !HasFlag(cptr, FLAG_ACCOUNT)) | |
938 | return 0; | |
939 | ||
940 | sendcmdto_common_channels_butone(cptr, CMD_QUIT, cptr, ":Registered"); | |
941 | ircd_snprintf(0, cli_user(cptr)->host, HOSTLEN, "%s.%s", | |
942 | cli_user(cptr)->account, feature_str(FEAT_HIDDEN_HOST)); | |
943 | ||
944 | /* ok, the client is now fully hidden, so let them know -- hikari */ | |
945 | if (MyConnect(cptr)) | |
946 | send_reply(cptr, RPL_HOSTHIDDEN, cli_user(cptr)->host); | |
947 | ||
948 | /* | |
949 | * Go through all channels the client was on, rejoin him | |
950 | * and set the modes, if any | |
951 | */ | |
952 | for (chan = cli_user(cptr)->channel; chan; chan = chan->next_channel) | |
953 | { | |
954 | if (IsZombie(chan)) | |
955 | continue; | |
956 | /* Send a JOIN unless the user's join has been delayed. */ | |
957 | if (!IsDelayedJoin(chan)) | |
958 | sendcmdto_channel_butserv_butone(cptr, CMD_JOIN, chan->channel, cptr, 0, | |
959 | "%H", chan->channel); | |
960 | if (IsChanOp(chan) && HasVoice(chan)) | |
961 | sendcmdto_channel_butserv_butone(&his, CMD_MODE, chan->channel, cptr, 0, | |
962 | "%H +ov %C %C", chan->channel, cptr, | |
963 | cptr); | |
964 | else if (IsChanOp(chan) || HasVoice(chan)) | |
965 | sendcmdto_channel_butserv_butone(&his, CMD_MODE, chan->channel, cptr, 0, | |
966 | "%H +%c %C", chan->channel, IsChanOp(chan) ? 'o' : 'v', cptr); | |
967 | } | |
968 | return 0; | |
969 | } | |
970 | ||
971 | /** Set a user's mode. This function checks that \a cptr is trying to | |
972 | * set his own mode, prevents local users from setting inappropriate | |
973 | * modes through this function, and applies any other side effects of | |
974 | * a successful mode change. | |
975 | * | |
976 | * @param[in,out] cptr User setting someone's mode. | |
977 | * @param[in] sptr Client who sent the mode change message. | |
978 | * @param[in] parc Number of parameters in \a parv. | |
979 | * @param[in] parv Parameters to MODE. | |
980 | * @return Zero. | |
981 | */ | |
982 | int set_user_mode(struct Client *cptr, struct Client *sptr, int parc, char *parv[]) | |
983 | { | |
984 | char** p; | |
985 | char* m; | |
986 | struct Client *acptr; | |
987 | int what; | |
988 | int i; | |
989 | struct Flags setflags; | |
990 | unsigned int tmpmask = 0; | |
991 | int snomask_given = 0; | |
992 | char buf[BUFSIZE]; | |
993 | int prop = 0; | |
994 | int do_host_hiding = 0; | |
995 | ||
996 | what = MODE_ADD; | |
997 | ||
998 | if (parc < 2) | |
999 | return need_more_params(sptr, "MODE"); | |
1000 | ||
1001 | if (!(acptr = FindUser(parv[1]))) | |
1002 | { | |
1003 | if (MyConnect(sptr)) | |
1004 | send_reply(sptr, ERR_NOSUCHCHANNEL, parv[1]); | |
1005 | return 0; | |
1006 | } | |
1007 | ||
1008 | if (IsServer(sptr) || sptr != acptr) | |
1009 | { | |
1010 | if (IsServer(cptr)) | |
1011 | sendwallto_group_butone(&me, WALL_WALLOPS, 0, | |
1012 | "MODE for User %s from %s!%s", parv[1], | |
1013 | cli_name(cptr), cli_name(sptr)); | |
1014 | else | |
1015 | send_reply(sptr, ERR_USERSDONTMATCH); | |
1016 | return 0; | |
1017 | } | |
1018 | ||
1019 | if (parc < 3) | |
1020 | { | |
1021 | m = buf; | |
1022 | *m++ = '+'; | |
1023 | for (i = 0; i < USERMODELIST_SIZE; i++) | |
1024 | { | |
1025 | if (HasFlag(sptr, userModeList[i].flag) && | |
1026 | userModeList[i].flag != FLAG_ACCOUNT) | |
1027 | *m++ = userModeList[i].c; | |
1028 | } | |
1029 | *m = '\0'; | |
1030 | send_reply(sptr, RPL_UMODEIS, buf); | |
1031 | if (HasFlag(sptr, FLAG_SERVNOTICE) && MyConnect(sptr) | |
1032 | && cli_snomask(sptr) != | |
1033 | (unsigned int)(IsOper(sptr) ? SNO_OPERDEFAULT : SNO_DEFAULT)) | |
1034 | send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr)); | |
1035 | return 0; | |
1036 | } | |
1037 | ||
1038 | /* | |
1039 | * find flags already set for user | |
1040 | * why not just copy them? | |
1041 | */ | |
1042 | setflags = cli_flags(sptr); | |
1043 | ||
1044 | if (MyConnect(sptr)) | |
1045 | tmpmask = cli_snomask(sptr); | |
1046 | ||
1047 | /* | |
1048 | * parse mode change string(s) | |
1049 | */ | |
1050 | for (p = &parv[2]; *p; p++) { /* p is changed in loop too */ | |
1051 | for (m = *p; *m; m++) { | |
1052 | switch (*m) { | |
1053 | case '+': | |
1054 | what = MODE_ADD; | |
1055 | break; | |
1056 | case '-': | |
1057 | what = MODE_DEL; | |
1058 | break; | |
1059 | case 's': | |
1060 | if (*(p + 1) && is_snomask(*(p + 1))) { | |
1061 | snomask_given = 1; | |
1062 | tmpmask = umode_make_snomask(tmpmask, *++p, what); | |
1063 | tmpmask &= (IsAnOper(sptr) ? SNO_ALL : SNO_USER); | |
1064 | } | |
1065 | else | |
1066 | tmpmask = (what == MODE_ADD) ? | |
1067 | (IsAnOper(sptr) ? SNO_OPERDEFAULT : SNO_DEFAULT) : 0; | |
1068 | if (tmpmask) | |
1069 | SetServNotice(sptr); | |
1070 | else | |
1071 | ClearServNotice(sptr); | |
1072 | break; | |
1073 | case 'w': | |
1074 | if (what == MODE_ADD) | |
1075 | SetWallops(sptr); | |
1076 | else | |
1077 | ClearWallops(sptr); | |
1078 | break; | |
1079 | case 'o': | |
1080 | if (what == MODE_ADD) | |
1081 | SetOper(sptr); | |
1082 | else { | |
1083 | ClrFlag(sptr, FLAG_OPER); | |
1084 | ClrFlag(sptr, FLAG_LOCOP); | |
1085 | if (MyConnect(sptr)) | |
1086 | { | |
1087 | tmpmask = cli_snomask(sptr) & ~SNO_OPER; | |
1088 | cli_handler(sptr) = CLIENT_HANDLER; | |
1089 | } | |
1090 | } | |
1091 | break; | |
1092 | case 'O': | |
1093 | if (what == MODE_ADD) | |
1094 | SetLocOp(sptr); | |
1095 | else | |
1096 | { | |
1097 | ClrFlag(sptr, FLAG_OPER); | |
1098 | ClrFlag(sptr, FLAG_LOCOP); | |
1099 | if (MyConnect(sptr)) | |
1100 | { | |
1101 | tmpmask = cli_snomask(sptr) & ~SNO_OPER; | |
1102 | cli_handler(sptr) = CLIENT_HANDLER; | |
1103 | } | |
1104 | } | |
1105 | break; | |
1106 | case 'i': | |
1107 | if (what == MODE_ADD) | |
1108 | SetInvisible(sptr); | |
1109 | else | |
1110 | ClearInvisible(sptr); | |
1111 | break; | |
1112 | case 'd': | |
1113 | if (what == MODE_ADD) | |
1114 | SetDeaf(sptr); | |
1115 | else | |
1116 | ClearDeaf(sptr); | |
1117 | break; | |
1118 | case 'k': | |
1119 | if (what == MODE_ADD) | |
1120 | SetChannelService(sptr); | |
1121 | else | |
1122 | ClearChannelService(sptr); | |
1123 | break; | |
1124 | case 'g': | |
1125 | if (what == MODE_ADD) | |
1126 | SetDebug(sptr); | |
1127 | else | |
1128 | ClearDebug(sptr); | |
1129 | break; | |
1130 | case 'x': | |
1131 | if (what == MODE_ADD) | |
1132 | do_host_hiding = 1; | |
1133 | break; | |
1134 | default: | |
1135 | send_reply(sptr, ERR_UMODEUNKNOWNFLAG, *m); | |
1136 | break; | |
1137 | } | |
1138 | } | |
1139 | } | |
1140 | /* | |
1141 | * Evaluate rules for new user mode | |
1142 | * Stop users making themselves operators too easily: | |
1143 | */ | |
1144 | if (!IsServer(cptr)) | |
1145 | { | |
1146 | if (!FlagHas(&setflags, FLAG_OPER) && IsOper(sptr)) | |
1147 | ClearOper(sptr); | |
1148 | if (!FlagHas(&setflags, FLAG_LOCOP) && IsLocOp(sptr)) | |
1149 | ClearLocOp(sptr); | |
1150 | /* | |
1151 | * new umode; servers can set it, local users cannot; | |
1152 | * prevents users from /kick'ing or /mode -o'ing | |
1153 | */ | |
1154 | if (!FlagHas(&setflags, FLAG_CHSERV)) | |
1155 | ClearChannelService(sptr); | |
1156 | /* | |
1157 | * only send wallops to opers | |
1158 | */ | |
1159 | if (feature_bool(FEAT_WALLOPS_OPER_ONLY) && !IsAnOper(sptr) && | |
1160 | !FlagHas(&setflags, FLAG_WALLOP)) | |
1161 | ClearWallops(sptr); | |
1162 | if (feature_bool(FEAT_HIS_SNOTICES_OPER_ONLY) && MyConnect(sptr) && | |
1163 | !IsAnOper(sptr) && !FlagHas(&setflags, FLAG_SERVNOTICE)) | |
1164 | { | |
1165 | ClearServNotice(sptr); | |
1166 | set_snomask(sptr, 0, SNO_SET); | |
1167 | } | |
1168 | if (feature_bool(FEAT_HIS_DEBUG_OPER_ONLY) && | |
1169 | !IsAnOper(sptr) && !FlagHas(&setflags, FLAG_DEBUG)) | |
1170 | ClearDebug(sptr); | |
1171 | } | |
1172 | if (MyConnect(sptr)) | |
1173 | { | |
1174 | if ((FlagHas(&setflags, FLAG_OPER) || FlagHas(&setflags, FLAG_LOCOP)) && | |
1175 | !IsAnOper(sptr)) | |
1176 | det_confs_butmask(sptr, CONF_CLIENT & ~CONF_OPERATOR); | |
1177 | ||
1178 | if (SendServNotice(sptr)) | |
1179 | { | |
1180 | if (tmpmask != cli_snomask(sptr)) | |
1181 | set_snomask(sptr, tmpmask, SNO_SET); | |
1182 | if (cli_snomask(sptr) && snomask_given) | |
1183 | send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr)); | |
1184 | } | |
1185 | else | |
1186 | set_snomask(sptr, 0, SNO_SET); | |
1187 | } | |
1188 | /* | |
1189 | * Compare new flags with old flags and send string which | |
1190 | * will cause servers to update correctly. | |
1191 | */ | |
1192 | if (!FlagHas(&setflags, FLAG_OPER) && IsOper(sptr)) | |
1193 | { | |
1194 | /* user now oper */ | |
1195 | ++UserStats.opers; | |
1196 | client_set_privs(sptr, NULL); /* may set propagate privilege */ | |
1197 | } | |
1198 | /* remember propagate privilege setting */ | |
1199 | if (HasPriv(sptr, PRIV_PROPAGATE)) | |
1200 | prop = 1; | |
1201 | if (FlagHas(&setflags, FLAG_OPER) && !IsOper(sptr)) | |
1202 | { | |
1203 | /* user no longer oper */ | |
1204 | --UserStats.opers; | |
1205 | client_set_privs(sptr, NULL); /* will clear propagate privilege */ | |
1206 | } | |
1207 | if (FlagHas(&setflags, FLAG_INVISIBLE) && !IsInvisible(sptr)) | |
1208 | --UserStats.inv_clients; | |
1209 | if (!FlagHas(&setflags, FLAG_INVISIBLE) && IsInvisible(sptr)) | |
1210 | ++UserStats.inv_clients; | |
1211 | if (!FlagHas(&setflags, FLAG_HIDDENHOST) && do_host_hiding) | |
1212 | hide_hostmask(sptr, FLAG_HIDDENHOST); | |
1213 | send_umode_out(cptr, sptr, &setflags, prop); | |
1214 | ||
1215 | return 0; | |
1216 | } | |
1217 | ||
1218 | /** Build a mode string to describe modes for \a cptr. | |
1219 | * @param[in] cptr Some user. | |
1220 | * @return Pointer to a static buffer. | |
1221 | */ | |
1222 | char *umode_str(struct Client *cptr) | |
1223 | { | |
1224 | /* Maximum string size: "owidgrx\0" */ | |
1225 | char *m = umodeBuf; | |
1226 | int i; | |
1227 | struct Flags c_flags = cli_flags(cptr); | |
1228 | ||
1229 | if (!HasPriv(cptr, PRIV_PROPAGATE)) | |
1230 | FlagClr(&c_flags, FLAG_OPER); | |
1231 | ||
1232 | for (i = 0; i < USERMODELIST_SIZE; ++i) | |
1233 | { | |
1234 | if (FlagHas(&c_flags, userModeList[i].flag) && | |
1235 | userModeList[i].flag >= FLAG_GLOBAL_UMODES) | |
1236 | *m++ = userModeList[i].c; | |
1237 | } | |
1238 | ||
1239 | if (IsAccount(cptr)) | |
1240 | { | |
1241 | char* t = cli_user(cptr)->account; | |
1242 | ||
1243 | *m++ = ' '; | |
1244 | while ((*m++ = *t++)) | |
1245 | ; /* Empty loop */ | |
1246 | ||
1247 | if (cli_user(cptr)->acc_create) { | |
1248 | char nbuf[20]; | |
1249 | Debug((DEBUG_DEBUG, "Sending timestamped account in user mode for " | |
1250 | "account \"%s\"; timestamp %Tu", cli_user(cptr)->account, | |
1251 | cli_user(cptr)->acc_create)); | |
1252 | ircd_snprintf(0, t = nbuf, sizeof(nbuf), ":%Tu", | |
1253 | cli_user(cptr)->acc_create); | |
1254 | m--; /* back up over previous nul-termination */ | |
1255 | while ((*m++ = *t++)) | |
1256 | ; /* Empty loop */ | |
1257 | } | |
1258 | } | |
1259 | ||
1260 | *m = '\0'; | |
1261 | ||
1262 | return umodeBuf; /* Note: static buffer, gets | |
1263 | overwritten by send_umode() */ | |
1264 | } | |
1265 | ||
1266 | /** Send a mode change string for \a sptr to \a cptr. | |
1267 | * @param[in] cptr Destination of mode change message. | |
1268 | * @param[in] sptr User whose mode has changed. | |
1269 | * @param[in] old Pre-change set of modes for \a sptr. | |
1270 | * @param[in] sendset One of ALL_UMODES, SEND_UMODES_BUT_OPER, | |
1271 | * SEND_UMODES, to select which changed user modes to send. | |
1272 | */ | |
1273 | void send_umode(struct Client *cptr, struct Client *sptr, struct Flags *old, | |
1274 | int sendset) | |
1275 | { | |
1276 | int i; | |
1277 | int flag; | |
1278 | char *m; | |
1279 | int what = MODE_NULL; | |
1280 | ||
1281 | /* | |
1282 | * Build a string in umodeBuf to represent the change in the user's | |
1283 | * mode between the new (cli_flags(sptr)) and 'old', but skipping | |
1284 | * the modes indicated by sendset. | |
1285 | */ | |
1286 | m = umodeBuf; | |
1287 | *m = '\0'; | |
1288 | for (i = 0; i < USERMODELIST_SIZE; ++i) | |
1289 | { | |
1290 | flag = userModeList[i].flag; | |
1291 | if (FlagHas(old, flag) | |
1292 | == HasFlag(sptr, flag)) | |
1293 | continue; | |
1294 | switch (sendset) | |
1295 | { | |
1296 | case ALL_UMODES: | |
1297 | break; | |
1298 | case SEND_UMODES_BUT_OPER: | |
1299 | if (flag == FLAG_OPER) | |
1300 | continue; | |
1301 | /* and fall through */ | |
1302 | case SEND_UMODES: | |
1303 | if (flag < FLAG_GLOBAL_UMODES) | |
1304 | continue; | |
1305 | break; | |
1306 | } | |
1307 | if (FlagHas(old, flag)) | |
1308 | { | |
1309 | if (what == MODE_DEL) | |
1310 | *m++ = userModeList[i].c; | |
1311 | else | |
1312 | { | |
1313 | what = MODE_DEL; | |
1314 | *m++ = '-'; | |
1315 | *m++ = userModeList[i].c; | |
1316 | } | |
1317 | } | |
1318 | else /* !FlagHas(old, flag) */ | |
1319 | { | |
1320 | if (what == MODE_ADD) | |
1321 | *m++ = userModeList[i].c; | |
1322 | else | |
1323 | { | |
1324 | what = MODE_ADD; | |
1325 | *m++ = '+'; | |
1326 | *m++ = userModeList[i].c; | |
1327 | } | |
1328 | } | |
1329 | } | |
1330 | *m = '\0'; | |
1331 | if (*umodeBuf && cptr) | |
1332 | sendcmdto_one(sptr, CMD_MODE, cptr, "%s :%s", cli_name(sptr), umodeBuf); | |
1333 | } | |
1334 | ||
1335 | /** | |
1336 | * Check to see if this resembles a sno_mask. It is if 1) there is | |
1337 | * at least one digit and 2) The first digit occurs before the first | |
1338 | * alphabetic character. | |
1339 | * @param[in] word Word to check for sno_mask-ness. | |
1340 | * @return Non-zero if \a word looks like a server notice mask; zero if not. | |
1341 | */ | |
1342 | int is_snomask(char *word) | |
1343 | { | |
1344 | if (word) | |
1345 | { | |
1346 | for (; *word; word++) | |
1347 | if (IsDigit(*word)) | |
1348 | return 1; | |
1349 | else if (IsAlpha(*word)) | |
1350 | return 0; | |
1351 | } | |
1352 | return 0; | |
1353 | } | |
1354 | ||
1355 | /** Update snomask \a oldmask according to \a arg and \a what. | |
1356 | * @param[in] oldmask Original user mask. | |
1357 | * @param[in] arg Update string (either a number or '+'/'-' followed by a number). | |
1358 | * @param[in] what MODE_ADD if adding the mask. | |
1359 | * @return New value of service notice mask. | |
1360 | */ | |
1361 | unsigned int umode_make_snomask(unsigned int oldmask, char *arg, int what) | |
1362 | { | |
1363 | unsigned int sno_what; | |
1364 | unsigned int newmask; | |
1365 | if (*arg == '+') | |
1366 | { | |
1367 | arg++; | |
1368 | if (what == MODE_ADD) | |
1369 | sno_what = SNO_ADD; | |
1370 | else | |
1371 | sno_what = SNO_DEL; | |
1372 | } | |
1373 | else if (*arg == '-') | |
1374 | { | |
1375 | arg++; | |
1376 | if (what == MODE_ADD) | |
1377 | sno_what = SNO_DEL; | |
1378 | else | |
1379 | sno_what = SNO_ADD; | |
1380 | } | |
1381 | else | |
1382 | sno_what = (what == MODE_ADD) ? SNO_SET : SNO_DEL; | |
1383 | /* pity we don't have strtoul everywhere */ | |
1384 | newmask = (unsigned int)atoi(arg); | |
1385 | if (sno_what == SNO_DEL) | |
1386 | newmask = oldmask & ~newmask; | |
1387 | else if (sno_what == SNO_ADD) | |
1388 | newmask |= oldmask; | |
1389 | return newmask; | |
1390 | } | |
1391 | ||
1392 | /** Remove \a cptr from the singly linked list \a list. | |
1393 | * @param[in] cptr Client to remove from list. | |
1394 | * @param[in,out] list Pointer to head of list containing \a cptr. | |
1395 | */ | |
1396 | static void delfrom_list(struct Client *cptr, struct SLink **list) | |
1397 | { | |
1398 | struct SLink* tmp; | |
1399 | struct SLink* prv = NULL; | |
1400 | ||
1401 | for (tmp = *list; tmp; tmp = tmp->next) { | |
1402 | if (tmp->value.cptr == cptr) { | |
1403 | if (prv) | |
1404 | prv->next = tmp->next; | |
1405 | else | |
1406 | *list = tmp->next; | |
1407 | free_link(tmp); | |
1408 | break; | |
1409 | } | |
1410 | prv = tmp; | |
1411 | } | |
1412 | } | |
1413 | ||
1414 | /** Set \a cptr's server notice mask, according to \a what. | |
1415 | * @param[in,out] cptr Client whose snomask is updating. | |
1416 | * @param[in] newmask Base value for new snomask. | |
1417 | * @param[in] what One of SNO_ADD, SNO_DEL, SNO_SET, to choose operation. | |
1418 | */ | |
1419 | void set_snomask(struct Client *cptr, unsigned int newmask, int what) | |
1420 | { | |
1421 | unsigned int oldmask, diffmask; /* unsigned please */ | |
1422 | int i; | |
1423 | struct SLink *tmp; | |
1424 | ||
1425 | oldmask = cli_snomask(cptr); | |
1426 | ||
1427 | if (what == SNO_ADD) | |
1428 | newmask |= oldmask; | |
1429 | else if (what == SNO_DEL) | |
1430 | newmask = oldmask & ~newmask; | |
1431 | else if (what != SNO_SET) /* absolute set, no math needed */ | |
1432 | sendto_opmask_butone(0, SNO_OLDSNO, "setsnomask called with %d ?!", what); | |
1433 | ||
1434 | newmask &= (IsAnOper(cptr) ? SNO_ALL : SNO_USER); | |
1435 | ||
1436 | diffmask = oldmask ^ newmask; | |
1437 | ||
1438 | for (i = 0; diffmask >> i; i++) { | |
1439 | if (((diffmask >> i) & 1)) | |
1440 | { | |
1441 | if (((newmask >> i) & 1)) | |
1442 | { | |
1443 | tmp = make_link(); | |
1444 | tmp->next = opsarray[i]; | |
1445 | tmp->value.cptr = cptr; | |
1446 | opsarray[i] = tmp; | |
1447 | } | |
1448 | else | |
1449 | /* not real portable :( */ | |
1450 | delfrom_list(cptr, &opsarray[i]); | |
1451 | } | |
1452 | } | |
1453 | cli_snomask(cptr) = newmask; | |
1454 | } | |
1455 | ||
1456 | /** Check whether \a sptr is allowed to send a message to \a acptr. | |
1457 | * If \a sptr is a remote user, it means some server has an outdated | |
1458 | * SILENCE list for \a acptr, so send the missing SILENCE mask(s) back | |
1459 | * in the direction of \a sptr. Skip the check if \a sptr is a server. | |
1460 | * @param[in] sptr Client trying to send a message. | |
1461 | * @param[in] acptr Destination of message. | |
1462 | * @return Non-zero if \a sptr is SILENCEd by \a acptr, zero if not. | |
1463 | */ | |
1464 | int is_silenced(struct Client *sptr, struct Client *acptr) | |
1465 | { | |
1466 | struct Ban *found; | |
1467 | struct User *user; | |
1468 | size_t buf_used, slen; | |
1469 | char buf[BUFSIZE]; | |
1470 | ||
1471 | if (IsServer(sptr) || !(user = cli_user(acptr)) | |
1472 | || !(found = find_ban(sptr, user->silence))) | |
1473 | return 0; | |
1474 | assert(!(found->flags & BAN_EXCEPTION)); | |
1475 | if (!MyConnect(sptr)) { | |
1476 | /* Buffer positive silence to send back. */ | |
1477 | buf_used = strlen(found->banstr); | |
1478 | memcpy(buf, found->banstr, buf_used); | |
1479 | /* Add exceptions to buffer. */ | |
1480 | for (found = user->silence; found; found = found->next) { | |
1481 | if (!(found->flags & BAN_EXCEPTION)) | |
1482 | continue; | |
1483 | slen = strlen(found->banstr); | |
1484 | if (buf_used + slen + 4 > 400) { | |
1485 | buf[buf_used] = '\0'; | |
1486 | sendcmdto_one(acptr, CMD_SILENCE, cli_from(sptr), "%C %s", sptr, buf); | |
1487 | buf_used = 0; | |
1488 | } | |
1489 | if (buf_used) | |
1490 | buf[buf_used++] = ','; | |
1491 | buf[buf_used++] = '+'; | |
1492 | buf[buf_used++] = '~'; | |
1493 | memcpy(buf + buf_used, found->banstr, slen); | |
1494 | buf_used += slen; | |
1495 | } | |
1496 | /* Flush silence buffer. */ | |
1497 | if (buf_used) { | |
1498 | buf[buf_used] = '\0'; | |
1499 | sendcmdto_one(acptr, CMD_SILENCE, cli_from(sptr), "%C %s", sptr, buf); | |
1500 | buf_used = 0; | |
1501 | } | |
1502 | } | |
1503 | return 1; | |
1504 | } | |
1505 | ||
1506 | /** Send RPL_ISUPPORT lines to \a cptr. | |
1507 | * @param[in] cptr Client to send ISUPPORT to. | |
1508 | * @return Zero. | |
1509 | */ | |
1510 | int | |
1511 | send_supported(struct Client *cptr) | |
1512 | { | |
1513 | char featurebuf[512]; | |
1514 | ||
1515 | ircd_snprintf(0, featurebuf, sizeof(featurebuf), FEATURES1, FEATURESVALUES1); | |
1516 | send_reply(cptr, RPL_ISUPPORT, featurebuf); | |
1517 | ircd_snprintf(0, featurebuf, sizeof(featurebuf), FEATURES2, FEATURESVALUES2); | |
1518 | send_reply(cptr, RPL_ISUPPORT, featurebuf); | |
1519 | ||
1520 | return 0; /* convenience return, if it's ever needed */ | |
1521 | } |