]>
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. | |
7e3a3d58 | 25 | * @version $Id: s_user.c,v 1.99.2.3 2007/01/13 18:43:41 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 | ||
d8e74551 | 76 | static char *IsVhost(char *hostmask, int oper); |
77 | static char *IsVhostPass(char *hostmask); | |
78 | ||
189935b1 | 79 | /** Count of allocated User structures. */ |
80 | static int userCount = 0; | |
81 | ||
82 | /** Makes sure that \a cptr has a User information block. | |
83 | * If cli_user(cptr) != NULL, does nothing. | |
84 | * @param[in] cptr Client to attach User struct to. | |
85 | * @return User struct associated with \a cptr. | |
86 | */ | |
87 | struct User *make_user(struct Client *cptr) | |
88 | { | |
89 | assert(0 != cptr); | |
90 | ||
91 | if (!cli_user(cptr)) { | |
92 | cli_user(cptr) = (struct User*) MyMalloc(sizeof(struct User)); | |
93 | assert(0 != cli_user(cptr)); | |
94 | ||
95 | /* All variables are 0 by default */ | |
96 | memset(cli_user(cptr), 0, sizeof(struct User)); | |
97 | ++userCount; | |
98 | cli_user(cptr)->refcnt = 1; | |
99 | } | |
100 | return cli_user(cptr); | |
101 | } | |
102 | ||
103 | /** Dereference \a user. | |
104 | * User structures are reference-counted; if the refcount of \a user | |
105 | * becomes zero, free it. | |
106 | * @param[in] user User to dereference. | |
107 | */ | |
108 | void free_user(struct User* user) | |
109 | { | |
110 | assert(0 != user); | |
111 | assert(0 < user->refcnt); | |
112 | ||
113 | if (--user->refcnt == 0) { | |
114 | if (user->away) | |
115 | MyFree(user->away); | |
116 | /* | |
117 | * sanity check | |
118 | */ | |
119 | assert(0 == user->joined); | |
120 | assert(0 == user->invited); | |
121 | assert(0 == user->channel); | |
122 | ||
123 | MyFree(user); | |
124 | --userCount; | |
125 | } | |
126 | } | |
127 | ||
128 | /** Find number of User structs allocated and memory used by them. | |
129 | * @param[out] count_out Receives number of User structs allocated. | |
130 | * @param[out] bytes_out Receives number of bytes used by User structs. | |
131 | */ | |
132 | void user_count_memory(size_t* count_out, size_t* bytes_out) | |
133 | { | |
134 | assert(0 != count_out); | |
135 | assert(0 != bytes_out); | |
136 | *count_out = userCount; | |
137 | *bytes_out = userCount * sizeof(struct User); | |
138 | } | |
139 | ||
140 | ||
141 | /** Find the next client (starting at \a next) with a name that matches \a ch. | |
142 | * Normal usage loop is: | |
143 | * for (x = client; x = next_client(x,mask); x = x->next) | |
144 | * HandleMatchingClient; | |
145 | * | |
146 | * @param[in] next First client to check. | |
147 | * @param[in] ch Name mask to check against. | |
148 | * @return Next matching client found, or NULL if none. | |
149 | */ | |
150 | struct Client *next_client(struct Client *next, const char* ch) | |
151 | { | |
152 | struct Client *tmp = next; | |
153 | ||
154 | if (!tmp) | |
155 | return NULL; | |
156 | ||
157 | next = FindClient(ch); | |
158 | next = next ? next : tmp; | |
159 | if (cli_prev(tmp) == next) | |
160 | return NULL; | |
161 | if (next != tmp) | |
162 | return next; | |
163 | for (; next; next = cli_next(next)) | |
164 | if (!match(ch, cli_name(next))) | |
165 | break; | |
166 | return next; | |
167 | } | |
168 | ||
169 | /** Find the destination server for a command, and forward it if that is not us. | |
170 | * | |
171 | * \a server may be a nickname, server name, server mask (if \a from | |
172 | * is a local user) or server numnick (if \a is a server or remote | |
173 | * user). | |
174 | * | |
175 | * @param[in] from Client that sent the command to us. | |
176 | * @param[in] cmd Long-form command text. | |
177 | * @param[in] tok Token-form command text. | |
178 | * @param[in] one Client that originated the command (ignored). | |
179 | * @param[in] MustBeOper If non-zero and \a from is not an operator, return HUNTED_NOSUCH. | |
180 | * @param[in] pattern Format string of arguments to command. | |
181 | * @param[in] server Index of target name or mask in \a parv. | |
182 | * @param[in] parc Number of valid elements in \a parv (must be less than 9). | |
183 | * @param[in] parv Array of arguments to command. | |
184 | * @return One of HUNTED_ISME, HUNTED_NOSUCH or HUNTED_PASS. | |
185 | */ | |
186 | int hunt_server_cmd(struct Client *from, const char *cmd, const char *tok, | |
187 | struct Client *one, int MustBeOper, const char *pattern, | |
188 | int server, int parc, char *parv[]) | |
189 | { | |
190 | struct Client *acptr; | |
191 | char *to; | |
192 | ||
193 | /* Assume it's me, if no server or an unregistered client */ | |
194 | if (parc <= server || EmptyString((to = parv[server])) || IsUnknown(from)) | |
195 | return (HUNTED_ISME); | |
196 | ||
197 | if (MustBeOper && !IsPrivileged(from)) | |
198 | { | |
199 | send_reply(from, ERR_NOPRIVILEGES); | |
200 | return HUNTED_NOSUCH; | |
201 | } | |
202 | ||
203 | /* Make sure it's a server */ | |
204 | if (MyUser(from)) { | |
205 | /* Make sure it's a server */ | |
206 | if (!strchr(to, '*')) { | |
207 | if (0 == (acptr = FindClient(to))) { | |
208 | send_reply(from, ERR_NOSUCHSERVER, to); | |
209 | return HUNTED_NOSUCH; | |
210 | } | |
211 | ||
212 | if (cli_user(acptr)) | |
213 | acptr = cli_user(acptr)->server; | |
214 | } else if (!(acptr = find_match_server(to))) { | |
215 | send_reply(from, ERR_NOSUCHSERVER, to); | |
216 | return (HUNTED_NOSUCH); | |
217 | } | |
218 | } else if (!(acptr = FindNServer(to))) { | |
219 | send_reply(from, SND_EXPLICIT | ERR_NOSUCHSERVER, "* :Server has disconnected"); | |
220 | return (HUNTED_NOSUCH); /* Server broke off in the meantime */ | |
221 | } | |
222 | ||
223 | if (IsMe(acptr)) | |
224 | return (HUNTED_ISME); | |
225 | ||
226 | if (MustBeOper && !IsPrivileged(from)) { | |
227 | send_reply(from, ERR_NOPRIVILEGES); | |
228 | return HUNTED_NOSUCH; | |
229 | } | |
230 | ||
231 | /* assert(!IsServer(from)); */ | |
232 | ||
233 | parv[server] = (char *) acptr; /* HACK! HACK! HACK! ARGH! */ | |
234 | ||
235 | sendcmdto_one(from, cmd, tok, acptr, pattern, parv[1], parv[2], parv[3], | |
236 | parv[4], parv[5], parv[6], parv[7], parv[8]); | |
237 | ||
238 | return (HUNTED_PASS); | |
239 | } | |
240 | ||
241 | /** Find the destination server for a command, and forward it (as a | |
242 | * high-priority command) if that is not us. | |
243 | * | |
244 | * \a server may be a nickname, server name, server mask (if \a from | |
245 | * is a local user) or server numnick (if \a is a server or remote | |
246 | * user). | |
247 | * Unlike hunt_server_cmd(), this appends the message to the | |
248 | * high-priority message queue for the destination server. | |
249 | * | |
250 | * @param[in] from Client that sent the command to us. | |
251 | * @param[in] cmd Long-form command text. | |
252 | * @param[in] tok Token-form command text. | |
253 | * @param[in] one Client that originated the command (ignored). | |
254 | * @param[in] MustBeOper If non-zero and \a from is not an operator, return HUNTED_NOSUCH. | |
255 | * @param[in] pattern Format string of arguments to command. | |
256 | * @param[in] server Index of target name or mask in \a parv. | |
257 | * @param[in] parc Number of valid elements in \a parv (must be less than 9). | |
258 | * @param[in] parv Array of arguments to command. | |
259 | * @return One of HUNTED_ISME, HUNTED_NOSUCH or HUNTED_PASS. | |
260 | */ | |
261 | int hunt_server_prio_cmd(struct Client *from, const char *cmd, const char *tok, | |
262 | struct Client *one, int MustBeOper, | |
263 | const char *pattern, int server, int parc, | |
264 | char *parv[]) | |
265 | { | |
266 | struct Client *acptr; | |
267 | char *to; | |
268 | ||
269 | /* Assume it's me, if no server or an unregistered client */ | |
270 | if (parc <= server || EmptyString((to = parv[server])) || IsUnknown(from)) | |
271 | return (HUNTED_ISME); | |
272 | ||
273 | /* Make sure it's a server */ | |
274 | if (MyUser(from)) { | |
275 | /* Make sure it's a server */ | |
276 | if (!strchr(to, '*')) { | |
277 | if (0 == (acptr = FindClient(to))) { | |
278 | send_reply(from, ERR_NOSUCHSERVER, to); | |
279 | return HUNTED_NOSUCH; | |
280 | } | |
281 | ||
282 | if (cli_user(acptr)) | |
283 | acptr = cli_user(acptr)->server; | |
284 | } else if (!(acptr = find_match_server(to))) { | |
285 | send_reply(from, ERR_NOSUCHSERVER, to); | |
286 | return (HUNTED_NOSUCH); | |
287 | } | |
288 | } else if (!(acptr = FindNServer(to))) | |
289 | return (HUNTED_NOSUCH); /* Server broke off in the meantime */ | |
290 | ||
291 | if (IsMe(acptr)) | |
292 | return (HUNTED_ISME); | |
293 | ||
294 | if (MustBeOper && !IsPrivileged(from)) { | |
295 | send_reply(from, ERR_NOPRIVILEGES); | |
296 | return HUNTED_NOSUCH; | |
297 | } | |
298 | ||
299 | /* assert(!IsServer(from)); SETTIME to particular destinations permitted */ | |
300 | ||
301 | parv[server] = (char *) acptr; /* HACK! HACK! HACK! ARGH! */ | |
302 | ||
303 | sendcmdto_prio_one(from, cmd, tok, acptr, pattern, parv[1], parv[2], parv[3], | |
304 | parv[4], parv[5], parv[6], parv[7], parv[8]); | |
305 | ||
306 | return (HUNTED_PASS); | |
307 | } | |
308 | ||
309 | ||
189935b1 | 310 | /* |
311 | * register_user | |
312 | * | |
313 | * This function is called when both NICK and USER messages | |
314 | * have been accepted for the client, in whatever order. Only | |
315 | * after this the USER message is propagated. | |
316 | * | |
317 | * NICK's must be propagated at once when received, although | |
318 | * it would be better to delay them too until full info is | |
319 | * available. Doing it is not so simple though, would have | |
320 | * to implement the following: | |
321 | * | |
322 | * 1) user telnets in and gives only "NICK foobar" and waits | |
323 | * 2) another user far away logs in normally with the nick | |
324 | * "foobar" (quite legal, as this server didn't propagate it). | |
325 | * 3) now this server gets nick "foobar" from outside, but | |
326 | * has already the same defined locally. Current server | |
327 | * would just issue "KILL foobar" to clean out dups. But, | |
328 | * this is not fair. It should actually request another | |
329 | * nick from local user or kill him/her... | |
330 | */ | |
331 | /** Finish registering a user who has sent both NICK and USER. | |
332 | * For local connections, possibly check IAuth; make sure there is a | |
333 | * matching Client config block; clean the username field; check | |
334 | * K/k-lines; check for "hacked" looking usernames; assign a numnick; | |
335 | * and send greeting (WELCOME, ISUPPORT, MOTD, etc). | |
336 | * For all connections, update the invisible user and operator counts; | |
337 | * run IPcheck against their address; and forward the NICK. | |
338 | * | |
339 | * @param[in] cptr Client who introduced the user. | |
340 | * @param[in,out] sptr Client who has been fully introduced. | |
189935b1 | 341 | * @return Zero or CPTR_KILLED. |
342 | */ | |
9f8856e9 | 343 | int register_user(struct Client *cptr, struct Client *sptr) |
189935b1 | 344 | { |
189935b1 | 345 | char* parv[4]; |
346 | char* tmpstr; | |
189935b1 | 347 | struct User* user = cli_user(sptr); |
189935b1 | 348 | char ip_base64[25]; |
349 | ||
350 | user->last = CurrentTime; | |
351 | parv[0] = cli_name(sptr); | |
352 | parv[1] = parv[2] = NULL; | |
353 | ||
354 | if (MyConnect(sptr)) | |
355 | { | |
189935b1 | 356 | assert(cptr == sptr); |
189935b1 | 357 | |
189935b1 | 358 | Count_unknownbecomesclient(sptr, UserStats); |
359 | ||
d8e74551 | 360 | if (MyConnect(sptr) && feature_bool(FEAT_AUTOINVISIBLE)) |
361 | SetInvisible(sptr); | |
362 | ||
363 | if(MyConnect(sptr) && feature_bool(FEAT_SETHOST_AUTO)) { | |
364 | if (conf_check_slines(sptr)) { | |
365 | send_reply(sptr, RPL_USINGSLINE); | |
366 | SetSetHost(sptr); | |
367 | } | |
368 | } | |
369 | ||
189935b1 | 370 | SetUser(sptr); |
371 | cli_handler(sptr) = CLIENT_HANDLER; | |
372 | SetLocalNumNick(sptr); | |
373 | send_reply(sptr, | |
374 | RPL_WELCOME, | |
375 | feature_str(FEAT_NETWORK), | |
376 | feature_str(FEAT_PROVIDER) ? " via " : "", | |
377 | feature_str(FEAT_PROVIDER) ? feature_str(FEAT_PROVIDER) : "", | |
9f8856e9 | 378 | cli_name(sptr)); |
189935b1 | 379 | /* |
380 | * This is a duplicate of the NOTICE but see below... | |
381 | */ | |
382 | send_reply(sptr, RPL_YOURHOST, cli_name(&me), version); | |
383 | send_reply(sptr, RPL_CREATED, creation); | |
384 | send_reply(sptr, RPL_MYINFO, cli_name(&me), version, infousermodes, | |
385 | infochanmodes, infochanmodeswithparams); | |
386 | send_supported(sptr); | |
387 | m_lusers(sptr, sptr, 1, parv); | |
388 | update_load(); | |
389 | motd_signon(sptr); | |
390 | if (cli_snomask(sptr) & SNO_NOISY) | |
391 | set_snomask(sptr, cli_snomask(sptr) & SNO_NOISY, SNO_ADD); | |
392 | if (feature_bool(FEAT_CONNEXIT_NOTICES)) | |
393 | sendto_opmask_butone(0, SNO_CONNEXIT, | |
394 | "Client connecting: %s (%s@%s) [%s] {%s} [%s] <%s%s>", | |
395 | cli_name(sptr), user->username, user->host, | |
396 | cli_sock_ip(sptr), get_client_class(sptr), | |
397 | cli_info(sptr), NumNick(cptr) /* two %s's */); | |
398 | ||
399 | IPcheck_connect_succeeded(sptr); | |
400 | /* | |
401 | * Set user's initial modes | |
402 | */ | |
403 | tmpstr = (char*)client_get_default_umode(sptr); | |
404 | if (tmpstr) for (; *tmpstr; ++tmpstr) { | |
405 | switch (*tmpstr) { | |
406 | case 's': | |
407 | if (!feature_bool(FEAT_HIS_SNOTICES_OPER_ONLY)) { | |
408 | SetServNotice(sptr); | |
409 | set_snomask(sptr, SNO_DEFAULT, SNO_SET); | |
410 | } | |
411 | break; | |
412 | case 'w': | |
413 | if (!feature_bool(FEAT_WALLOPS_OPER_ONLY)) | |
414 | SetWallops(sptr); | |
415 | break; | |
416 | case 'i': | |
417 | SetInvisible(sptr); | |
418 | break; | |
419 | case 'd': | |
420 | SetDeaf(sptr); | |
421 | break; | |
422 | case 'g': | |
423 | if (!feature_bool(FEAT_HIS_DEBUG_OPER_ONLY)) | |
424 | SetDebug(sptr); | |
425 | break; | |
426 | } | |
427 | } | |
428 | } | |
429 | else { | |
9f8856e9 | 430 | struct Client *acptr = user->server; |
189935b1 | 431 | |
7fafc378 | 432 | ircd_strncpy(user->username, cli_username(sptr), USERLEN); |
9f8856e9 | 433 | Count_newremoteclient(UserStats, acptr); |
189935b1 | 434 | |
189935b1 | 435 | if (cli_from(acptr) != cli_from(sptr)) |
436 | { | |
437 | sendcmdto_one(&me, CMD_KILL, cptr, "%C :%s (%s != %s[%s])", | |
438 | sptr, cli_name(&me), cli_name(user->server), cli_name(cli_from(acptr)), | |
439 | cli_sockhost(cli_from(acptr))); | |
440 | SetFlag(sptr, FLAG_KILLED); | |
441 | return exit_client(cptr, sptr, &me, "NICK server wrong direction"); | |
442 | } | |
443 | else if (HasFlag(acptr, FLAG_TS8)) | |
444 | SetFlag(sptr, FLAG_TS8); | |
445 | ||
446 | /* | |
447 | * Check to see if this user is being propagated | |
448 | * as part of a net.burst, or is using protocol 9. | |
449 | * FIXME: This can be sped up - its stupid to check it for | |
450 | * every NICK message in a burst again --Run. | |
451 | */ | |
452 | for (; acptr != &me; acptr = cli_serv(acptr)->up) | |
453 | { | |
454 | if (IsBurst(acptr) || Protocol(acptr) < 10) | |
455 | break; | |
456 | } | |
457 | if (!IPcheck_remote_connect(sptr, (acptr != &me))) | |
458 | { | |
459 | /* | |
460 | * We ran out of bits to count this | |
461 | */ | |
462 | sendcmdto_one(&me, CMD_KILL, sptr, "%C :%s (Too many connections from your host -- Ghost)", | |
463 | sptr, cli_name(&me)); | |
464 | return exit_client(cptr, sptr, &me,"Too many connections from your host -- throttled"); | |
465 | } | |
d8e74551 | 466 | |
467 | if(MyConnect(sptr) && feature_bool(FEAT_SETHOST_AUTO)) { | |
468 | if (conf_check_slines(sptr)) { | |
469 | send_reply(sptr, RPL_USINGSLINE); | |
470 | SetSetHost(sptr); | |
471 | } | |
472 | } | |
473 | ||
189935b1 | 474 | SetUser(sptr); |
475 | } | |
476 | ||
477 | if (IsInvisible(sptr)) | |
478 | ++UserStats.inv_clients; | |
479 | if (IsOper(sptr)) | |
480 | ++UserStats.opers; | |
481 | ||
482 | tmpstr = umode_str(sptr); | |
483 | /* Send full IP address to IPv6-grokking servers. */ | |
484 | sendcmdto_flag_serv_butone(user->server, CMD_NICK, cptr, | |
485 | FLAG_IPV6, FLAG_LAST_FLAG, | |
486 | "%s %d %Tu %s %s %s%s%s%s %s%s :%s", | |
9f8856e9 | 487 | cli_name(sptr), cli_hopcount(sptr) + 1, |
488 | cli_lastnick(sptr), | |
d8e74551 | 489 | user->realusername, user->realhost, |
189935b1 | 490 | *tmpstr ? "+" : "", tmpstr, *tmpstr ? " " : "", |
491 | iptobase64(ip_base64, &cli_ip(sptr), sizeof(ip_base64), 1), | |
492 | NumNick(sptr), cli_info(sptr)); | |
493 | /* Send fake IPv6 addresses to pre-IPv6 servers. */ | |
494 | sendcmdto_flag_serv_butone(user->server, CMD_NICK, cptr, | |
495 | FLAG_LAST_FLAG, FLAG_IPV6, | |
496 | "%s %d %Tu %s %s %s%s%s%s %s%s :%s", | |
9f8856e9 | 497 | cli_name(sptr), cli_hopcount(sptr) + 1, |
498 | cli_lastnick(sptr), | |
d8e74551 | 499 | user->realusername, user->realhost, |
189935b1 | 500 | *tmpstr ? "+" : "", tmpstr, *tmpstr ? " " : "", |
501 | iptobase64(ip_base64, &cli_ip(sptr), sizeof(ip_base64), 0), | |
502 | NumNick(sptr), cli_info(sptr)); | |
503 | ||
504 | /* Send user mode to client */ | |
505 | if (MyUser(sptr)) | |
506 | { | |
507 | static struct Flags flags; /* automatically initialized to zeros */ | |
052b069e | 508 | /* To avoid sending +r to the client due to auth-on-connect, set |
509 | * the "old" FLAG_ACCOUNT bit to match the client's value. | |
510 | */ | |
511 | if (IsAccount(cptr)) | |
512 | FlagSet(&flags, FLAG_ACCOUNT); | |
513 | else | |
514 | FlagClr(&flags, FLAG_ACCOUNT); | |
189935b1 | 515 | send_umode(cptr, sptr, &flags, ALL_UMODES); |
516 | if ((cli_snomask(sptr) != SNO_DEFAULT) && HasFlag(sptr, FLAG_SERVNOTICE)) | |
517 | send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr)); | |
518 | } | |
519 | return 0; | |
520 | } | |
521 | ||
522 | /** List of user mode characters. */ | |
523 | static const struct UserMode { | |
524 | unsigned int flag; /**< User mode constant. */ | |
525 | char c; /**< Character corresponding to the mode. */ | |
526 | } userModeList[] = { | |
527 | { FLAG_OPER, 'o' }, | |
528 | { FLAG_LOCOP, 'O' }, | |
529 | { FLAG_INVISIBLE, 'i' }, | |
530 | { FLAG_WALLOP, 'w' }, | |
531 | { FLAG_SERVNOTICE, 's' }, | |
532 | { FLAG_DEAF, 'd' }, | |
533 | { FLAG_CHSERV, 'k' }, | |
534 | { FLAG_DEBUG, 'g' }, | |
535 | { FLAG_ACCOUNT, 'r' }, | |
d8e74551 | 536 | { FLAG_HIDDENHOST, 'x' }, |
537 | { FLAG_ACCOUNTONLY, 'R' }, | |
538 | { FLAG_XTRAOP, 'X' }, | |
539 | { FLAG_NOCHAN, 'n' }, | |
540 | { FLAG_NOIDLE, 'I' }, | |
541 | { FLAG_SETHOST, 'h' }, | |
542 | { FLAG_PARANOID, 'P' } | |
189935b1 | 543 | }; |
544 | ||
545 | /** Length of #userModeList. */ | |
546 | #define USERMODELIST_SIZE sizeof(userModeList) / sizeof(struct UserMode) | |
547 | ||
548 | /* | |
549 | * XXX - find a way to get rid of this | |
550 | */ | |
551 | /** Nasty global buffer used for communications with umode_str() and others. */ | |
552 | static char umodeBuf[BUFSIZE]; | |
553 | ||
554 | /** Try to set a user's nickname. | |
555 | * If \a sptr is a server, the client is being introduced for the first time. | |
556 | * @param[in] cptr Client to set nickname. | |
557 | * @param[in] sptr Client sending the NICK. | |
558 | * @param[in] nick New nickname. | |
559 | * @param[in] parc Number of arguments to NICK. | |
560 | * @param[in] parv Argument list to NICK. | |
561 | * @return CPTR_KILLED if \a cptr was killed, else 0. | |
562 | */ | |
563 | int set_nick_name(struct Client* cptr, struct Client* sptr, | |
564 | const char* nick, int parc, char* parv[]) | |
565 | { | |
566 | if (IsServer(sptr)) { | |
567 | int i; | |
568 | const char* account = 0; | |
d8e74551 | 569 | char* hostmask = 0; |
570 | char* host = 0; | |
f687a4d7 | 571 | const char* p, *pp; |
189935b1 | 572 | |
573 | /* | |
574 | * A server introducing a new client, change source | |
575 | */ | |
576 | struct Client* new_client = make_client(cptr, STAT_UNKNOWN); | |
577 | assert(0 != new_client); | |
578 | ||
579 | cli_hopcount(new_client) = atoi(parv[2]); | |
580 | cli_lastnick(new_client) = atoi(parv[3]); | |
581 | if (Protocol(cptr) > 9 && parc > 7 && *parv[6] == '+') | |
582 | { | |
583 | for (p = parv[6] + 1; *p; p++) | |
584 | { | |
585 | for (i = 0; i < USERMODELIST_SIZE; ++i) | |
586 | { | |
587 | if (userModeList[i].c == *p) | |
588 | { | |
589 | SetFlag(new_client, userModeList[i].flag); | |
590 | if (userModeList[i].flag == FLAG_ACCOUNT) | |
591 | account = parv[7]; | |
d8e74551 | 592 | if (userModeList[i].flag == FLAG_SETHOST) |
593 | hostmask = parv[parc - 4]; | |
189935b1 | 594 | break; |
595 | } | |
596 | } | |
597 | } | |
598 | } | |
599 | client_set_privs(new_client, NULL); /* set privs on user */ | |
600 | /* | |
601 | * Set new nick name. | |
602 | */ | |
603 | strcpy(cli_name(new_client), nick); | |
604 | cli_user(new_client) = make_user(new_client); | |
605 | cli_user(new_client)->server = sptr; | |
606 | SetRemoteNumNick(new_client, parv[parc - 2]); | |
607 | /* | |
608 | * IP# of remote client | |
609 | */ | |
610 | base64toip(parv[parc - 3], &cli_ip(new_client)); | |
611 | ||
612 | add_client_to_list(new_client); | |
613 | hAddClient(new_client); | |
614 | ||
615 | cli_serv(sptr)->ghost = 0; /* :server NICK means end of net.burst */ | |
616 | ircd_strncpy(cli_username(new_client), parv[4], USERLEN); | |
d8e74551 | 617 | ircd_strncpy(cli_user(new_client)->realusername, parv[4], USERLEN); |
189935b1 | 618 | ircd_strncpy(cli_user(new_client)->host, parv[5], HOSTLEN); |
619 | ircd_strncpy(cli_user(new_client)->realhost, parv[5], HOSTLEN); | |
620 | ircd_strncpy(cli_info(new_client), parv[parc - 1], REALLEN); | |
621 | if (account) { | |
622 | int len = ACCOUNTLEN; | |
623 | if ((p = strchr(account, ':'))) { | |
624 | len = (p++) - account; | |
625 | cli_user(new_client)->acc_create = atoi(p); | |
f687a4d7 | 626 | if ((pp = strchr(p, ':'))) |
8172bdd4 | 627 | cli_user(new_client)->acc_id = strtoul(pp + 1, NULL, 10); |
189935b1 | 628 | Debug((DEBUG_DEBUG, "Received timestamped account in user mode; " |
8172bdd4 | 629 | "account \"%s\", timestamp %Tu id %lu", account, |
f687a4d7 | 630 | cli_user(new_client)->acc_create, |
631 | cli_user(new_client)->acc_id)); | |
189935b1 | 632 | } |
633 | ircd_strncpy(cli_user(new_client)->account, account, len); | |
634 | } | |
635 | if (HasHiddenHost(new_client)) | |
636 | ircd_snprintf(0, cli_user(new_client)->host, HOSTLEN, "%s.%s", | |
e3f1ad7a | 637 | cli_user(new_client)->account, |
638 | feature_str(FEAT_HIDDEN_HOST)); | |
d8e74551 | 639 | if (HasSetHost(new_client)) { |
640 | if ((host = strrchr(hostmask, '@')) != NULL) { | |
641 | *host++ = '\0'; | |
642 | ircd_strncpy(cli_username(new_client), hostmask, USERLEN); | |
643 | ircd_strncpy(cli_user(new_client)->host, host, HOSTLEN); | |
644 | } | |
645 | } | |
189935b1 | 646 | |
9f8856e9 | 647 | return register_user(cptr, new_client); |
189935b1 | 648 | } |
649 | else if ((cli_name(sptr))[0]) { | |
650 | /* | |
651 | * Client changing its nick | |
652 | * | |
653 | * If the client belongs to me, then check to see | |
654 | * if client is on any channels where it is currently | |
655 | * banned. If so, do not allow the nick change to occur. | |
656 | */ | |
657 | if (MyUser(sptr)) { | |
658 | const char* channel_name; | |
659 | struct Membership *member; | |
d8e74551 | 660 | if ((channel_name = find_no_nickchange_channel(sptr)) && !IsXtraOp(sptr)) { |
189935b1 | 661 | return send_reply(cptr, ERR_BANNICKCHANGE, channel_name); |
662 | } | |
663 | /* | |
664 | * Refuse nick change if the last nick change was less | |
665 | * then 30 seconds ago. This is intended to get rid of | |
666 | * clone bots doing NICK FLOOD. -SeKs | |
667 | * If someone didn't change their nick for more then 60 seconds | |
668 | * however, allow to do two nick changes immediately after another | |
669 | * before limiting the nick flood. -Run | |
670 | */ | |
671 | if (CurrentTime < cli_nextnick(cptr)) | |
672 | { | |
673 | cli_nextnick(cptr) += 2; | |
674 | send_reply(cptr, ERR_NICKTOOFAST, parv[1], | |
675 | cli_nextnick(cptr) - CurrentTime); | |
676 | /* Send error message */ | |
677 | sendcmdto_one(cptr, CMD_NICK, cptr, "%s", cli_name(cptr)); | |
678 | /* bounce NICK to user */ | |
679 | return 0; /* ignore nick change! */ | |
680 | } | |
681 | else { | |
682 | /* Limit total to 1 change per NICK_DELAY seconds: */ | |
683 | cli_nextnick(cptr) += NICK_DELAY; | |
684 | /* However allow _maximal_ 1 extra consecutive nick change: */ | |
685 | if (cli_nextnick(cptr) < CurrentTime) | |
686 | cli_nextnick(cptr) = CurrentTime; | |
687 | } | |
688 | /* Invalidate all bans against the user so we check them again */ | |
689 | for (member = (cli_user(cptr))->channel; member; | |
690 | member = member->next_channel) | |
691 | ClearBanValid(member); | |
692 | } | |
693 | /* | |
694 | * Also set 'lastnick' to current time, if changed. | |
695 | */ | |
696 | if (0 != ircd_strcmp(parv[0], nick)) | |
697 | cli_lastnick(sptr) = (sptr == cptr) ? TStime() : atoi(parv[2]); | |
698 | ||
699 | /* | |
700 | * Client just changing his/her nick. If he/she is | |
701 | * on a channel, send note of change to all clients | |
702 | * on that channel. Propagate notice to other servers. | |
703 | */ | |
704 | if (IsUser(sptr)) { | |
705 | sendcmdto_common_channels_butone(sptr, CMD_NICK, NULL, ":%s", nick); | |
706 | add_history(sptr, 1); | |
707 | sendcmdto_serv_butone(sptr, CMD_NICK, cptr, "%s %Tu", nick, | |
708 | cli_lastnick(sptr)); | |
709 | } | |
710 | else | |
711 | sendcmdto_one(sptr, CMD_NICK, sptr, ":%s", nick); | |
712 | ||
713 | if ((cli_name(sptr))[0]) | |
714 | hRemClient(sptr); | |
715 | strcpy(cli_name(sptr), nick); | |
716 | hAddClient(sptr); | |
717 | } | |
718 | else { | |
719 | /* Local client setting NICK the first time */ | |
189935b1 | 720 | strcpy(cli_name(sptr), nick); |
189935b1 | 721 | hAddClient(sptr); |
9f8856e9 | 722 | return auth_set_nick(cli_auth(sptr), nick); |
189935b1 | 723 | } |
724 | return 0; | |
725 | } | |
726 | ||
727 | /** Calculate the hash value for a target. | |
728 | * @param[in] target Pointer to target, cast to unsigned int. | |
729 | * @return Hash value constructed from the pointer. | |
730 | */ | |
731 | static unsigned char hash_target(unsigned int target) | |
732 | { | |
733 | return (unsigned char) (target >> 16) ^ (target >> 8); | |
734 | } | |
735 | ||
736 | /** Records \a target as a recent target for \a sptr. | |
737 | * @param[in] sptr User who has sent to a new target. | |
738 | * @param[in] target Target to add. | |
739 | */ | |
740 | void | |
741 | add_target(struct Client *sptr, void *target) | |
742 | { | |
743 | /* Ok, this shouldn't work esp on alpha | |
744 | */ | |
745 | unsigned char hash = hash_target((unsigned long) target); | |
746 | unsigned char* targets; | |
747 | int i; | |
748 | assert(0 != sptr); | |
749 | assert(cli_local(sptr)); | |
750 | ||
751 | targets = cli_targets(sptr); | |
752 | ||
753 | /* | |
754 | * Already in table? | |
755 | */ | |
756 | for (i = 0; i < MAXTARGETS; ++i) { | |
757 | if (targets[i] == hash) | |
758 | return; | |
759 | } | |
760 | /* | |
761 | * New target | |
762 | */ | |
763 | memmove(&targets[RESERVEDTARGETS + 1], | |
764 | &targets[RESERVEDTARGETS], MAXTARGETS - RESERVEDTARGETS - 1); | |
765 | targets[RESERVEDTARGETS] = hash; | |
766 | } | |
767 | ||
768 | /** Check whether \a sptr can send to or join \a target yet. | |
769 | * @param[in] sptr User trying to join a channel or send a message. | |
770 | * @param[in] target Target of the join or message. | |
771 | * @param[in] name Name of the target. | |
772 | * @param[in] created If non-zero, trying to join a new channel. | |
773 | * @return Non-zero if too many target changes; zero if okay to send. | |
774 | */ | |
775 | int check_target_limit(struct Client *sptr, void *target, const char *name, | |
776 | int created) | |
777 | { | |
778 | unsigned char hash = hash_target((unsigned long) target); | |
779 | int i; | |
780 | unsigned char* targets; | |
781 | ||
782 | assert(0 != sptr); | |
783 | assert(cli_local(sptr)); | |
784 | targets = cli_targets(sptr); | |
785 | ||
786 | /* If user is invited to channel, give him/her a free target */ | |
787 | if (IsChannelName(name) && IsInvited(sptr, target)) | |
788 | return 0; | |
789 | ||
d8e74551 | 790 | /* opers always have a free target */ |
791 | if (IsAnOper(sptr)) | |
792 | return 0; | |
793 | ||
189935b1 | 794 | /* |
795 | * Same target as last time? | |
796 | */ | |
797 | if (targets[0] == hash) | |
798 | return 0; | |
799 | for (i = 1; i < MAXTARGETS; ++i) { | |
800 | if (targets[i] == hash) { | |
801 | memmove(&targets[1], &targets[0], i); | |
802 | targets[0] = hash; | |
803 | return 0; | |
804 | } | |
805 | } | |
806 | /* | |
807 | * New target | |
808 | */ | |
809 | if (!created) { | |
810 | if (CurrentTime < cli_nexttarget(sptr)) { | |
811 | if (cli_nexttarget(sptr) - CurrentTime < TARGET_DELAY + 8) { | |
812 | /* | |
813 | * No server flooding | |
814 | */ | |
815 | cli_nexttarget(sptr) += 2; | |
816 | send_reply(sptr, ERR_TARGETTOOFAST, name, | |
817 | cli_nexttarget(sptr) - CurrentTime); | |
818 | } | |
819 | return 1; | |
820 | } | |
821 | else { | |
822 | cli_nexttarget(sptr) += TARGET_DELAY; | |
823 | if (cli_nexttarget(sptr) < CurrentTime - (TARGET_DELAY * (MAXTARGETS - 1))) | |
824 | cli_nexttarget(sptr) = CurrentTime - (TARGET_DELAY * (MAXTARGETS - 1)); | |
825 | } | |
826 | } | |
827 | memmove(&targets[1], &targets[0], MAXTARGETS - 1); | |
828 | targets[0] = hash; | |
829 | return 0; | |
830 | } | |
831 | ||
832 | /** Allows a channel operator to avoid target change checks when | |
833 | * sending messages to users on their channel. | |
834 | * @param[in] source User sending the message. | |
835 | * @param[in] nick Destination of the message. | |
836 | * @param[in] channel Name of channel being sent to. | |
837 | * @param[in] text Message to send. | |
838 | * @param[in] is_notice If non-zero, use CNOTICE instead of CPRIVMSG. | |
839 | */ | |
840 | /* Added 971023 by Run. */ | |
841 | int whisper(struct Client* source, const char* nick, const char* channel, | |
842 | const char* text, int is_notice) | |
843 | { | |
844 | struct Client* dest; | |
845 | struct Channel* chptr; | |
846 | struct Membership* membership; | |
847 | ||
848 | assert(0 != source); | |
849 | assert(0 != nick); | |
850 | assert(0 != channel); | |
851 | assert(MyUser(source)); | |
852 | ||
853 | if (!(dest = FindUser(nick))) { | |
854 | return send_reply(source, ERR_NOSUCHNICK, nick); | |
855 | } | |
856 | if (!(chptr = FindChannel(channel))) { | |
857 | return send_reply(source, ERR_NOSUCHCHANNEL, channel); | |
858 | } | |
859 | /* | |
860 | * compare both users channel lists, instead of the channels user list | |
861 | * since the link is the same, this should be a little faster for channels | |
862 | * with a lot of users | |
863 | */ | |
864 | for (membership = cli_user(source)->channel; membership; membership = membership->next_channel) { | |
865 | if (chptr == membership->channel) | |
866 | break; | |
867 | } | |
868 | if (0 == membership) { | |
869 | return send_reply(source, ERR_NOTONCHANNEL, chptr->chname); | |
870 | } | |
871 | if (!IsVoicedOrOpped(membership)) { | |
872 | return send_reply(source, ERR_VOICENEEDED, chptr->chname); | |
873 | } | |
874 | /* | |
875 | * lookup channel in destination | |
876 | */ | |
877 | assert(0 != cli_user(dest)); | |
878 | for (membership = cli_user(dest)->channel; membership; membership = membership->next_channel) { | |
879 | if (chptr == membership->channel) | |
880 | break; | |
881 | } | |
882 | if (0 == membership || IsZombie(membership)) { | |
883 | return send_reply(source, ERR_USERNOTINCHANNEL, cli_name(dest), chptr->chname); | |
884 | } | |
885 | if (is_silenced(source, dest)) | |
886 | return 0; | |
887 | ||
888 | if (cli_user(dest)->away) | |
889 | send_reply(source, RPL_AWAY, cli_name(dest), cli_user(dest)->away); | |
890 | if (is_notice) | |
891 | sendcmdto_one(source, CMD_NOTICE, dest, "%C :%s", dest, text); | |
892 | else | |
893 | sendcmdto_one(source, CMD_PRIVATE, dest, "%C :%s", dest, text); | |
894 | return 0; | |
895 | } | |
896 | ||
897 | ||
898 | /** Send a user mode change for \a cptr to neighboring servers. | |
899 | * @param[in] cptr User whose mode is changing. | |
900 | * @param[in] sptr Client who sent us the mode change message. | |
901 | * @param[in] old Prior set of user flags. | |
902 | * @param[in] prop If non-zero, also include FLAG_OPER. | |
903 | */ | |
904 | void send_umode_out(struct Client *cptr, struct Client *sptr, | |
905 | struct Flags *old, int prop) | |
906 | { | |
907 | int i; | |
908 | struct Client *acptr; | |
909 | ||
910 | send_umode(NULL, sptr, old, prop ? SEND_UMODES : SEND_UMODES_BUT_OPER); | |
911 | ||
912 | for (i = HighestFd; i >= 0; i--) | |
913 | { | |
914 | if ((acptr = LocalClientArray[i]) && IsServer(acptr) && | |
915 | (acptr != cptr) && (acptr != sptr) && *umodeBuf) | |
d8e74551 | 916 | sendcmdto_one(sptr, CMD_MODE, acptr, "%s %s", cli_name(sptr), umodeBuf); |
189935b1 | 917 | } |
918 | if (cptr && MyUser(cptr)) | |
919 | send_umode(cptr, sptr, old, ALL_UMODES); | |
920 | } | |
921 | ||
922 | ||
923 | /** Call \a fmt for each Client named in \a names. | |
924 | * @param[in] sptr Client requesting information. | |
925 | * @param[in] names Space-delimited list of nicknames. | |
926 | * @param[in] rpl Base reply string for messages. | |
927 | * @param[in] fmt Formatting callback function. | |
928 | */ | |
929 | void send_user_info(struct Client* sptr, char* names, int rpl, InfoFormatter fmt) | |
930 | { | |
931 | char* name; | |
932 | char* p = 0; | |
933 | int arg_count = 0; | |
934 | int users_found = 0; | |
935 | struct Client* acptr; | |
936 | struct MsgBuf* mb; | |
937 | ||
938 | assert(0 != sptr); | |
939 | assert(0 != names); | |
940 | assert(0 != fmt); | |
941 | ||
942 | mb = msgq_make(sptr, rpl_str(rpl), cli_name(&me), cli_name(sptr)); | |
943 | ||
944 | for (name = ircd_strtok(&p, names, " "); name; name = ircd_strtok(&p, 0, " ")) { | |
945 | if ((acptr = FindUser(name))) { | |
946 | if (users_found++) | |
947 | msgq_append(0, mb, " "); | |
948 | (*fmt)(acptr, sptr, mb); | |
949 | } | |
950 | if (5 == ++arg_count) | |
951 | break; | |
952 | } | |
953 | send_buffer(sptr, mb, 0); | |
954 | msgq_clean(mb); | |
955 | } | |
956 | ||
957 | /** Set \a flag on \a cptr and possibly hide the client's hostmask. | |
958 | * @param[in,out] cptr User who is getting a new flag. | |
959 | * @param[in] flag Some flag that affects host-hiding (FLAG_HIDDENHOST, FLAG_ACCOUNT). | |
960 | * @return Zero. | |
961 | */ | |
962 | int | |
963 | hide_hostmask(struct Client *cptr, unsigned int flag) | |
964 | { | |
965 | struct Membership *chan; | |
966 | ||
967 | switch (flag) { | |
968 | case FLAG_HIDDENHOST: | |
969 | /* Local users cannot set +x unless FEAT_HOST_HIDING is true. */ | |
970 | if (MyConnect(cptr) && !feature_bool(FEAT_HOST_HIDING)) | |
971 | return 0; | |
972 | break; | |
973 | case FLAG_ACCOUNT: | |
974 | /* Invalidate all bans against the user so we check them again */ | |
975 | for (chan = (cli_user(cptr))->channel; chan; | |
976 | chan = chan->next_channel) | |
977 | ClearBanValid(chan); | |
978 | break; | |
979 | default: | |
980 | return 0; | |
981 | } | |
982 | ||
983 | SetFlag(cptr, flag); | |
12c200ce | 984 | if (!HasFlag(cptr, FLAG_HIDDENHOST) || !HasFlag(cptr, FLAG_ACCOUNT) || HasSetHost(cptr)) |
189935b1 | 985 | return 0; |
986 | ||
987 | sendcmdto_common_channels_butone(cptr, CMD_QUIT, cptr, ":Registered"); | |
988 | ircd_snprintf(0, cli_user(cptr)->host, HOSTLEN, "%s.%s", | |
989 | cli_user(cptr)->account, feature_str(FEAT_HIDDEN_HOST)); | |
990 | ||
991 | /* ok, the client is now fully hidden, so let them know -- hikari */ | |
992 | if (MyConnect(cptr)) | |
993 | send_reply(cptr, RPL_HOSTHIDDEN, cli_user(cptr)->host); | |
994 | ||
995 | /* | |
996 | * Go through all channels the client was on, rejoin him | |
997 | * and set the modes, if any | |
998 | */ | |
999 | for (chan = cli_user(cptr)->channel; chan; chan = chan->next_channel) | |
1000 | { | |
1001 | if (IsZombie(chan)) | |
1002 | continue; | |
1003 | /* Send a JOIN unless the user's join has been delayed. */ | |
1004 | if (!IsDelayedJoin(chan)) | |
1005 | sendcmdto_channel_butserv_butone(cptr, CMD_JOIN, chan->channel, cptr, 0, | |
1006 | "%H", chan->channel); | |
1007 | if (IsChanOp(chan) && HasVoice(chan)) | |
1008 | sendcmdto_channel_butserv_butone(&his, CMD_MODE, chan->channel, cptr, 0, | |
1009 | "%H +ov %C %C", chan->channel, cptr, | |
1010 | cptr); | |
1011 | else if (IsChanOp(chan) || HasVoice(chan)) | |
1012 | sendcmdto_channel_butserv_butone(&his, CMD_MODE, chan->channel, cptr, 0, | |
1013 | "%H +%c %C", chan->channel, IsChanOp(chan) ? 'o' : 'v', cptr); | |
1014 | } | |
1015 | return 0; | |
1016 | } | |
1017 | ||
d8e74551 | 1018 | /* |
1019 | * set_hostmask() - derived from hide_hostmask() | |
1020 | * | |
1021 | */ | |
1022 | int set_hostmask(struct Client *cptr, char *hostmask, char *password) | |
1023 | { | |
1024 | int restore = 0; | |
1025 | int freeform = 0; | |
1026 | char *host, *new_vhost, *vhost_pass; | |
1027 | char hiddenhost[USERLEN + HOSTLEN + 2]; | |
1028 | struct Membership *chan; | |
1029 | ||
1030 | Debug((DEBUG_INFO, "set_hostmask() %C, %s, %s", cptr, hostmask, password)); | |
1031 | ||
1032 | /* sethost enabled? */ | |
1033 | if (MyConnect(cptr) && !feature_bool(FEAT_SETHOST)) { | |
1034 | send_reply(cptr, ERR_DISABLED, "SETHOST"); | |
1035 | return 0; | |
1036 | } | |
1037 | ||
1038 | /* sethost enabled for users? */ | |
1039 | if (MyConnect(cptr) && !IsAnOper(cptr) && !feature_bool(FEAT_SETHOST_USER)) { | |
1040 | send_reply(cptr, ERR_NOPRIVILEGES); | |
1041 | return 0; | |
1042 | } | |
1043 | ||
1044 | /* MODE_DEL: restore original hostmask */ | |
1045 | if (EmptyString(hostmask)) { | |
1046 | /* is already sethost'ed? */ | |
1047 | if (IsSetHost(cptr)) { | |
1048 | restore = 1; | |
1049 | sendcmdto_common_channels_butone(cptr, CMD_QUIT, cptr, ":Host change"); | |
1050 | /* If they are +rx, we need to return to their +x host, not their "real" host */ | |
1051 | if (HasHiddenHost(cptr)) | |
1052 | ircd_snprintf(0, cli_user(cptr)->host, HOSTLEN, "%s.%s", | |
1053 | cli_user(cptr)->account, feature_str(FEAT_HIDDEN_HOST)); | |
1054 | else | |
1055 | strncpy(cli_user(cptr)->host, cli_user(cptr)->realhost, HOSTLEN); | |
1056 | strncpy(cli_user(cptr)->username, cli_user(cptr)->realusername, USERLEN); | |
1057 | /* log it */ | |
1058 | if (MyConnect(cptr)) | |
1059 | log_write(LS_SETHOST, L_INFO, LOG_NOSNOTICE, | |
1060 | "SETHOST (%s@%s) by (%#R): restoring real hostmask", | |
1061 | cli_user(cptr)->username, cli_user(cptr)->host, cptr); | |
1062 | } else | |
1063 | return 0; | |
1064 | /* MODE_ADD: set a new hostmask */ | |
1065 | } else { | |
1066 | /* chop up ident and host.cc */ | |
1067 | if ((host = strrchr(hostmask, '@'))) /* oper can specifiy ident@host.cc */ | |
1068 | *host++ = '\0'; | |
1069 | else /* user can only specifiy host.cc [password] */ | |
1070 | host = hostmask; | |
1071 | /* | |
1072 | * Oper sethost | |
1073 | */ | |
1074 | if (MyConnect(cptr)) { | |
1075 | if (IsAnOper(cptr)) { | |
1076 | if ((new_vhost = IsVhost(host, 1)) == NULL) { | |
a0200102 | 1077 | if (!HasPriv(cptr, PRIV_FREEFORM)) { |
d8e74551 | 1078 | send_reply(cptr, ERR_HOSTUNAVAIL, hostmask); |
1079 | log_write(LS_SETHOST, L_INFO, LOG_NOSNOTICE, | |
1080 | "SETHOST (%s@%s) by (%#R): no such s-line", | |
1081 | (host != hostmask) ? hostmask : cli_user(cptr)->username, host, cptr); | |
1082 | return 0; | |
1083 | } else /* freeform active, log and go */ | |
1084 | freeform = 1; | |
1085 | } | |
1086 | sendcmdto_common_channels_butone(cptr, CMD_QUIT, cptr, ":Host change"); | |
1087 | /* set the new ident and host */ | |
1088 | if (host != hostmask) /* oper only specified host.cc */ | |
1089 | strncpy(cli_user(cptr)->username, hostmask, USERLEN); | |
1090 | strncpy(cli_user(cptr)->host, host, HOSTLEN); | |
1091 | /* log it */ | |
1092 | log_write(LS_SETHOST, (freeform) ? L_NOTICE : L_INFO, | |
1093 | (freeform) ? 0 : LOG_NOSNOTICE, "SETHOST (%s@%s) by (%#R)%s", | |
1094 | cli_user(cptr)->username, cli_user(cptr)->host, cptr, | |
1095 | (freeform) ? ": using freeform" : ""); | |
1096 | /* | |
1097 | * plain user sethost, handled here | |
1098 | */ | |
1099 | } else { | |
1100 | /* empty password? */ | |
1101 | if (EmptyString(password)) { | |
1102 | send_reply(cptr, ERR_NEEDMOREPARAMS, "MODE"); | |
1103 | return 0; | |
1104 | } | |
1105 | /* no such s-line */ | |
1106 | if ((new_vhost = IsVhost(host, 0)) == NULL) { | |
1107 | send_reply(cptr, ERR_HOSTUNAVAIL, hostmask); | |
1108 | log_write(LS_SETHOST, L_INFO, LOG_NOSNOTICE, "SETHOST (%s@%s %s) by (%#R): no such s-line", | |
1109 | cli_user(cptr)->username, host, password, cptr); | |
1110 | return 0; | |
1111 | } | |
1112 | /* no password */ | |
1113 | if ((vhost_pass = IsVhostPass(new_vhost)) == NULL) { | |
1114 | send_reply(cptr, ERR_PASSWDMISMATCH); | |
1115 | log_write(LS_SETHOST, L_INFO, 0, "SETHOST (%s@%s %s) by (%#R): trying to use an oper s-line", | |
1116 | cli_user(cptr)->username, host, password, cptr); | |
1117 | return 0; | |
1118 | } | |
1119 | /* incorrect password */ | |
1120 | if (strCasediff(vhost_pass, password)) { | |
1121 | send_reply(cptr, ERR_PASSWDMISMATCH); | |
1122 | log_write(LS_SETHOST, L_NOTICE, 0, "SETHOST (%s@%s %s) by (%#R): incorrect password", | |
1123 | cli_user(cptr)->username, host, password, cptr); | |
1124 | return 0; | |
1125 | } | |
1126 | sendcmdto_common_channels_butone(cptr, CMD_QUIT, cptr, ":Host change"); | |
1127 | /* set the new host */ | |
1128 | strncpy(cli_user(cptr)->host, new_vhost, HOSTLEN); | |
1129 | /* log it */ | |
1130 | log_write(LS_SETHOST, L_INFO, LOG_NOSNOTICE, "SETHOST (%s@%s) by (%#R)", | |
1131 | cli_user(cptr)->username, cli_user(cptr)->host, cptr); | |
1132 | } | |
1133 | } else { /* remote user */ | |
1134 | sendcmdto_common_channels_butone(cptr, CMD_QUIT, cptr, ":Host change"); | |
1135 | if (host != hostmask) /* oper only specified host.cc */ | |
1136 | strncpy(cli_user(cptr)->username, hostmask, USERLEN); | |
1137 | strncpy(cli_user(cptr)->host, host, HOSTLEN); | |
1138 | } | |
1139 | } | |
1140 | ||
1141 | if (restore) | |
1142 | ClearSetHost(cptr); | |
1143 | else | |
1144 | SetSetHost(cptr); | |
1145 | ||
1146 | if (MyConnect(cptr)) { | |
1147 | ircd_snprintf(0, hiddenhost, HOSTLEN + USERLEN + 2, "%s@%s", | |
1148 | cli_user(cptr)->username, cli_user(cptr)->host); | |
1149 | send_reply(cptr, RPL_HOSTHIDDEN, hiddenhost); | |
1150 | } | |
1151 | ||
1152 | #if 0 | |
1153 | /* Code copied from hide_hostmask(). This is the old (pre-delayedjoin) | |
1154 | * version. Switch this in if you're not using the delayed join patch. */ | |
1155 | /* | |
1156 | * Go through all channels the client was on, rejoin him | |
1157 | * and set the modes, if any | |
1158 | */ | |
1159 | for (chan = cli_user(cptr)->channel; chan; chan = chan->next_channel) { | |
1160 | if (IsZombie(chan)) | |
1161 | continue; | |
1162 | sendcmdto_channel_butserv_butone(cptr, CMD_JOIN, chan->channel, cptr, | |
1163 | "%H", chan->channel); | |
1164 | if (IsChanOp(chan) && HasVoice(chan)) { | |
1165 | sendcmdto_channel_butserv_butone(&me, CMD_MODE, chan->channel, cptr, | |
1166 | "%H +ov %C %C", chan->channel, cptr, cptr); | |
1167 | } else if (IsChanOp(chan) || HasVoice(chan)) { | |
1168 | sendcmdto_channel_butserv_butone(&me, CMD_MODE, chan->channel, cptr, | |
1169 | "%H +%c %C", chan->channel, IsChanOp(chan) ? 'o' : 'v', cptr); | |
1170 | } | |
1171 | } | |
1172 | #endif | |
1173 | ||
1174 | /* | |
1175 | * Go through all channels the client was on, rejoin him | |
1176 | * and set the modes, if any | |
1177 | */ | |
1178 | for (chan = cli_user(cptr)->channel; chan; chan = chan->next_channel) { | |
1179 | if (IsZombie(chan)) | |
1180 | continue; | |
1181 | /* If this channel has delayed joins and the user has no modes, just set | |
1182 | * the delayed join flag rather than showing the join, even if the user | |
1183 | * was visible before */ | |
1184 | if (!IsChanOp(chan) && !HasVoice(chan) | |
1185 | && (chan->channel->mode.mode & MODE_DELJOINS)) { | |
1186 | SetDelayedJoin(chan); | |
1187 | } else { | |
1188 | sendcmdto_channel_butserv_butone(cptr, CMD_JOIN, chan->channel, cptr, 0, | |
1189 | "%H", chan->channel); | |
1190 | } | |
1191 | if (IsChanOp(chan) && HasVoice(chan)) { | |
1192 | sendcmdto_channel_butserv_butone(&me, CMD_MODE, chan->channel, cptr, 0, | |
1193 | "%H +ov %C %C", chan->channel, cptr, cptr); | |
1194 | } else if (IsChanOp(chan) || HasVoice(chan)) { | |
1195 | sendcmdto_channel_butserv_butone(&me, CMD_MODE, chan->channel, cptr, 0, | |
1196 | "%H +%c %C", chan->channel, IsChanOp(chan) ? 'o' : 'v', cptr); | |
1197 | } | |
1198 | } | |
1199 | return 1; | |
1200 | } | |
1201 | ||
189935b1 | 1202 | /** Set a user's mode. This function checks that \a cptr is trying to |
1203 | * set his own mode, prevents local users from setting inappropriate | |
1204 | * modes through this function, and applies any other side effects of | |
1205 | * a successful mode change. | |
1206 | * | |
1207 | * @param[in,out] cptr User setting someone's mode. | |
1208 | * @param[in] sptr Client who sent the mode change message. | |
1209 | * @param[in] parc Number of parameters in \a parv. | |
1210 | * @param[in] parv Parameters to MODE. | |
1211 | * @return Zero. | |
1212 | */ | |
1213 | int set_user_mode(struct Client *cptr, struct Client *sptr, int parc, char *parv[]) | |
1214 | { | |
1215 | char** p; | |
1216 | char* m; | |
1217 | struct Client *acptr; | |
1218 | int what; | |
1219 | int i; | |
1220 | struct Flags setflags; | |
1221 | unsigned int tmpmask = 0; | |
1222 | int snomask_given = 0; | |
1223 | char buf[BUFSIZE]; | |
d8e74551 | 1224 | char *hostmask, *password; |
189935b1 | 1225 | int prop = 0; |
1226 | int do_host_hiding = 0; | |
d8e74551 | 1227 | int do_set_host = 0; |
189935b1 | 1228 | |
d8e74551 | 1229 | hostmask = password = NULL; |
189935b1 | 1230 | what = MODE_ADD; |
1231 | ||
1232 | if (parc < 2) | |
1233 | return need_more_params(sptr, "MODE"); | |
1234 | ||
1235 | if (!(acptr = FindUser(parv[1]))) | |
1236 | { | |
1237 | if (MyConnect(sptr)) | |
1238 | send_reply(sptr, ERR_NOSUCHCHANNEL, parv[1]); | |
1239 | return 0; | |
1240 | } | |
1241 | ||
1242 | if (IsServer(sptr) || sptr != acptr) | |
1243 | { | |
1244 | if (IsServer(cptr)) | |
1245 | sendwallto_group_butone(&me, WALL_WALLOPS, 0, | |
1246 | "MODE for User %s from %s!%s", parv[1], | |
1247 | cli_name(cptr), cli_name(sptr)); | |
1248 | else | |
1249 | send_reply(sptr, ERR_USERSDONTMATCH); | |
1250 | return 0; | |
1251 | } | |
1252 | ||
1253 | if (parc < 3) | |
1254 | { | |
1255 | m = buf; | |
1256 | *m++ = '+'; | |
1257 | for (i = 0; i < USERMODELIST_SIZE; i++) | |
1258 | { | |
1259 | if (HasFlag(sptr, userModeList[i].flag) && | |
d8e74551 | 1260 | ((userModeList[i].flag != FLAG_ACCOUNT) && |
1261 | (userModeList[i].flag != FLAG_SETHOST))) | |
189935b1 | 1262 | *m++ = userModeList[i].c; |
1263 | } | |
1264 | *m = '\0'; | |
1265 | send_reply(sptr, RPL_UMODEIS, buf); | |
1266 | if (HasFlag(sptr, FLAG_SERVNOTICE) && MyConnect(sptr) | |
1267 | && cli_snomask(sptr) != | |
1268 | (unsigned int)(IsOper(sptr) ? SNO_OPERDEFAULT : SNO_DEFAULT)) | |
1269 | send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr)); | |
1270 | return 0; | |
1271 | } | |
1272 | ||
1273 | /* | |
1274 | * find flags already set for user | |
1275 | * why not just copy them? | |
1276 | */ | |
1277 | setflags = cli_flags(sptr); | |
1278 | ||
1279 | if (MyConnect(sptr)) | |
1280 | tmpmask = cli_snomask(sptr); | |
1281 | ||
1282 | /* | |
1283 | * parse mode change string(s) | |
1284 | */ | |
1285 | for (p = &parv[2]; *p; p++) { /* p is changed in loop too */ | |
1286 | for (m = *p; *m; m++) { | |
1287 | switch (*m) { | |
1288 | case '+': | |
1289 | what = MODE_ADD; | |
1290 | break; | |
1291 | case '-': | |
1292 | what = MODE_DEL; | |
1293 | break; | |
1294 | case 's': | |
1295 | if (*(p + 1) && is_snomask(*(p + 1))) { | |
1296 | snomask_given = 1; | |
1297 | tmpmask = umode_make_snomask(tmpmask, *++p, what); | |
1298 | tmpmask &= (IsAnOper(sptr) ? SNO_ALL : SNO_USER); | |
1299 | } | |
1300 | else | |
1301 | tmpmask = (what == MODE_ADD) ? | |
1302 | (IsAnOper(sptr) ? SNO_OPERDEFAULT : SNO_DEFAULT) : 0; | |
1303 | if (tmpmask) | |
1304 | SetServNotice(sptr); | |
1305 | else | |
1306 | ClearServNotice(sptr); | |
1307 | break; | |
1308 | case 'w': | |
1309 | if (what == MODE_ADD) | |
1310 | SetWallops(sptr); | |
1311 | else | |
1312 | ClearWallops(sptr); | |
1313 | break; | |
1314 | case 'o': | |
1315 | if (what == MODE_ADD) | |
1316 | SetOper(sptr); | |
1317 | else { | |
1318 | ClrFlag(sptr, FLAG_OPER); | |
1319 | ClrFlag(sptr, FLAG_LOCOP); | |
1320 | if (MyConnect(sptr)) | |
1321 | { | |
1322 | tmpmask = cli_snomask(sptr) & ~SNO_OPER; | |
1323 | cli_handler(sptr) = CLIENT_HANDLER; | |
1324 | } | |
1325 | } | |
1326 | break; | |
1327 | case 'O': | |
1328 | if (what == MODE_ADD) | |
1329 | SetLocOp(sptr); | |
1330 | else | |
1331 | { | |
1332 | ClrFlag(sptr, FLAG_OPER); | |
1333 | ClrFlag(sptr, FLAG_LOCOP); | |
1334 | if (MyConnect(sptr)) | |
1335 | { | |
1336 | tmpmask = cli_snomask(sptr) & ~SNO_OPER; | |
1337 | cli_handler(sptr) = CLIENT_HANDLER; | |
1338 | } | |
1339 | } | |
1340 | break; | |
1341 | case 'i': | |
1342 | if (what == MODE_ADD) | |
1343 | SetInvisible(sptr); | |
1344 | else | |
d8e74551 | 1345 | if (!feature_bool(FEAT_AUTOINVISIBLE) || IsOper(sptr)) /* Don't allow non-opers to -i if FEAT_AUTOINVISIBLE is set */ |
1346 | ClearInvisible(sptr); | |
189935b1 | 1347 | break; |
1348 | case 'd': | |
1349 | if (what == MODE_ADD) | |
1350 | SetDeaf(sptr); | |
1351 | else | |
1352 | ClearDeaf(sptr); | |
1353 | break; | |
1354 | case 'k': | |
1355 | if (what == MODE_ADD) | |
1356 | SetChannelService(sptr); | |
1357 | else | |
1358 | ClearChannelService(sptr); | |
1359 | break; | |
d8e74551 | 1360 | case 'X': |
1361 | if (what == MODE_ADD) | |
1362 | SetXtraOp(sptr); | |
1363 | else | |
1364 | ClearXtraOp(sptr); | |
1365 | break; | |
1366 | case 'n': | |
1367 | if (what == MODE_ADD) | |
1368 | SetNoChan(sptr); | |
1369 | else | |
1370 | ClearNoChan(sptr); | |
1371 | break; | |
1372 | case 'I': | |
1373 | if (what == MODE_ADD) | |
1374 | SetNoIdle(sptr); | |
1375 | else | |
1376 | ClearNoIdle(sptr); | |
1377 | break; | |
189935b1 | 1378 | case 'g': |
1379 | if (what == MODE_ADD) | |
1380 | SetDebug(sptr); | |
1381 | else | |
1382 | ClearDebug(sptr); | |
1383 | break; | |
1384 | case 'x': | |
1385 | if (what == MODE_ADD) | |
d8e74551 | 1386 | do_host_hiding = 1; |
1387 | break; | |
1388 | case 'h': | |
1389 | if (what == MODE_ADD) { | |
1390 | if (*(p + 1) && is_hostmask(*(p + 1))) { | |
1391 | do_set_host = 1; | |
1392 | hostmask = *++p; | |
1393 | /* DON'T step p onto the trailing NULL in the parameter array! - splidge */ | |
1394 | if (*(p+1)) | |
1395 | password = *++p; | |
1396 | else | |
1397 | password = NULL; | |
1398 | } else { | |
1399 | if (!*(p+1)) | |
1400 | send_reply(sptr, ERR_NEEDMOREPARAMS, "SETHOST"); | |
1401 | else { | |
1402 | send_reply(sptr, ERR_BADHOSTMASK, *(p+1)); | |
1403 | p++; /* Swallow the arg anyway */ | |
1404 | } | |
1405 | } | |
1406 | } else { /* MODE_DEL */ | |
1407 | do_set_host = 1; | |
1408 | hostmask = NULL; | |
1409 | password = NULL; | |
1410 | } | |
1411 | break; | |
1412 | case 'R': | |
1413 | if (what == MODE_ADD) | |
1414 | SetAccountOnly(sptr); | |
1415 | else | |
1416 | ClearAccountOnly(sptr); | |
1417 | break; | |
1418 | case 'P': | |
1419 | if (what == MODE_ADD) | |
1420 | SetParanoid(sptr); | |
1421 | else | |
1422 | ClearParanoid(sptr); | |
ebebfa5f | 1423 | break; |
189935b1 | 1424 | default: |
1425 | send_reply(sptr, ERR_UMODEUNKNOWNFLAG, *m); | |
1426 | break; | |
1427 | } | |
1428 | } | |
1429 | } | |
1430 | /* | |
1431 | * Evaluate rules for new user mode | |
1432 | * Stop users making themselves operators too easily: | |
1433 | */ | |
1434 | if (!IsServer(cptr)) | |
1435 | { | |
1436 | if (!FlagHas(&setflags, FLAG_OPER) && IsOper(sptr)) | |
1437 | ClearOper(sptr); | |
1438 | if (!FlagHas(&setflags, FLAG_LOCOP) && IsLocOp(sptr)) | |
1439 | ClearLocOp(sptr); | |
1440 | /* | |
1441 | * new umode; servers can set it, local users cannot; | |
1442 | * prevents users from /kick'ing or /mode -o'ing | |
1443 | */ | |
a0200102 | 1444 | if (!FlagHas(&setflags, FLAG_CHSERV) && !(IsOper(sptr) && HasPriv(sptr, PRIV_CHANSERV))) |
189935b1 | 1445 | ClearChannelService(sptr); |
a0200102 | 1446 | if (!FlagHas(&setflags, FLAG_XTRAOP) && !(IsOper(sptr) && HasPriv(sptr, PRIV_XTRA_OPER))) |
d8e74551 | 1447 | ClearXtraOp(sptr); |
1448 | if (!FlagHas(&setflags, FLAG_NOCHAN) && !(IsOper(sptr) || feature_bool(FEAT_USER_HIDECHANS))) | |
1449 | ClearNoChan(sptr); | |
a0200102 | 1450 | if (!FlagHas(&setflags, FLAG_NOIDLE) && !((IsOper(sptr) && HasPriv(sptr, PRIV_NOIDLE)) || feature_bool(FEAT_USER_HIDEIDLETIME))) |
d8e74551 | 1451 | ClearNoIdle(sptr); |
a0200102 | 1452 | if (!FlagHas(&setflags, FLAG_PARANOID) && !(IsOper(sptr) && HasPriv(sptr, PRIV_PARANOID))) |
d8e74551 | 1453 | ClearParanoid(sptr); |
1454 | ||
189935b1 | 1455 | /* |
1456 | * only send wallops to opers | |
1457 | */ | |
1458 | if (feature_bool(FEAT_WALLOPS_OPER_ONLY) && !IsAnOper(sptr) && | |
1459 | !FlagHas(&setflags, FLAG_WALLOP)) | |
1460 | ClearWallops(sptr); | |
1461 | if (feature_bool(FEAT_HIS_SNOTICES_OPER_ONLY) && MyConnect(sptr) && | |
1462 | !IsAnOper(sptr) && !FlagHas(&setflags, FLAG_SERVNOTICE)) | |
1463 | { | |
1464 | ClearServNotice(sptr); | |
1465 | set_snomask(sptr, 0, SNO_SET); | |
1466 | } | |
1467 | if (feature_bool(FEAT_HIS_DEBUG_OPER_ONLY) && | |
1468 | !IsAnOper(sptr) && !FlagHas(&setflags, FLAG_DEBUG)) | |
1469 | ClearDebug(sptr); | |
1470 | } | |
1471 | if (MyConnect(sptr)) | |
1472 | { | |
1473 | if ((FlagHas(&setflags, FLAG_OPER) || FlagHas(&setflags, FLAG_LOCOP)) && | |
1474 | !IsAnOper(sptr)) | |
1475 | det_confs_butmask(sptr, CONF_CLIENT & ~CONF_OPERATOR); | |
1476 | ||
1477 | if (SendServNotice(sptr)) | |
1478 | { | |
1479 | if (tmpmask != cli_snomask(sptr)) | |
1480 | set_snomask(sptr, tmpmask, SNO_SET); | |
1481 | if (cli_snomask(sptr) && snomask_given) | |
1482 | send_reply(sptr, RPL_SNOMASK, cli_snomask(sptr), cli_snomask(sptr)); | |
1483 | } | |
1484 | else | |
1485 | set_snomask(sptr, 0, SNO_SET); | |
1486 | } | |
1487 | /* | |
1488 | * Compare new flags with old flags and send string which | |
1489 | * will cause servers to update correctly. | |
1490 | */ | |
1491 | if (!FlagHas(&setflags, FLAG_OPER) && IsOper(sptr)) | |
1492 | { | |
1493 | /* user now oper */ | |
1494 | ++UserStats.opers; | |
1495 | client_set_privs(sptr, NULL); /* may set propagate privilege */ | |
1496 | } | |
1497 | /* remember propagate privilege setting */ | |
1498 | if (HasPriv(sptr, PRIV_PROPAGATE)) | |
1499 | prop = 1; | |
1500 | if (FlagHas(&setflags, FLAG_OPER) && !IsOper(sptr)) | |
1501 | { | |
1502 | /* user no longer oper */ | |
1503 | --UserStats.opers; | |
1504 | client_set_privs(sptr, NULL); /* will clear propagate privilege */ | |
1505 | } | |
1506 | if (FlagHas(&setflags, FLAG_INVISIBLE) && !IsInvisible(sptr)) | |
1507 | --UserStats.inv_clients; | |
1508 | if (!FlagHas(&setflags, FLAG_INVISIBLE) && IsInvisible(sptr)) | |
1509 | ++UserStats.inv_clients; | |
1510 | if (!FlagHas(&setflags, FLAG_HIDDENHOST) && do_host_hiding) | |
1511 | hide_hostmask(sptr, FLAG_HIDDENHOST); | |
d8e74551 | 1512 | if (do_set_host) { |
1513 | /* We clear the flag in the old mask, so that the +h will be sent */ | |
1514 | /* Only do this if we're SETTING +h and it succeeded */ | |
1515 | if (set_hostmask(sptr, hostmask, password) && hostmask) | |
1516 | FlagClr(&setflags, FLAG_SETHOST); | |
1517 | } | |
189935b1 | 1518 | send_umode_out(cptr, sptr, &setflags, prop); |
1519 | ||
1520 | return 0; | |
1521 | } | |
1522 | ||
1523 | /** Build a mode string to describe modes for \a cptr. | |
1524 | * @param[in] cptr Some user. | |
1525 | * @return Pointer to a static buffer. | |
1526 | */ | |
1527 | char *umode_str(struct Client *cptr) | |
1528 | { | |
1529 | /* Maximum string size: "owidgrx\0" */ | |
1530 | char *m = umodeBuf; | |
1531 | int i; | |
1532 | struct Flags c_flags = cli_flags(cptr); | |
1533 | ||
1534 | if (!HasPriv(cptr, PRIV_PROPAGATE)) | |
1535 | FlagClr(&c_flags, FLAG_OPER); | |
1536 | ||
1537 | for (i = 0; i < USERMODELIST_SIZE; ++i) | |
1538 | { | |
1539 | if (FlagHas(&c_flags, userModeList[i].flag) && | |
1540 | userModeList[i].flag >= FLAG_GLOBAL_UMODES) | |
1541 | *m++ = userModeList[i].c; | |
1542 | } | |
1543 | ||
1544 | if (IsAccount(cptr)) | |
1545 | { | |
1546 | char* t = cli_user(cptr)->account; | |
1547 | ||
1548 | *m++ = ' '; | |
1549 | while ((*m++ = *t++)) | |
1550 | ; /* Empty loop */ | |
1551 | ||
1552 | if (cli_user(cptr)->acc_create) { | |
1553 | char nbuf[20]; | |
1554 | Debug((DEBUG_DEBUG, "Sending timestamped account in user mode for " | |
1555 | "account \"%s\"; timestamp %Tu", cli_user(cptr)->account, | |
1556 | cli_user(cptr)->acc_create)); | |
f687a4d7 | 1557 | if(cli_user(cptr)->acc_id) { |
1558 | ircd_snprintf(0, t = nbuf, sizeof(nbuf), ":%Tu:%lu", | |
1559 | cli_user(cptr)->acc_create, cli_user(cptr)->acc_id); | |
1560 | } else { | |
1561 | ircd_snprintf(0, t = nbuf, sizeof(nbuf), ":%Tu", | |
1562 | cli_user(cptr)->acc_create); | |
1563 | } | |
189935b1 | 1564 | m--; /* back up over previous nul-termination */ |
1565 | while ((*m++ = *t++)) | |
1566 | ; /* Empty loop */ | |
1567 | } | |
d8e74551 | 1568 | m--; /* Step back over the '\0' */ |
189935b1 | 1569 | } |
1570 | ||
d8e74551 | 1571 | if (IsSetHost(cptr)) { |
1572 | *m++ = ' '; | |
1573 | ircd_snprintf(0, m, USERLEN + HOSTLEN + 2, "%s@%s", cli_user(cptr)->username, | |
1574 | cli_user(cptr)->host); | |
1575 | } else | |
1576 | *m = '\0'; | |
189935b1 | 1577 | return umodeBuf; /* Note: static buffer, gets |
1578 | overwritten by send_umode() */ | |
1579 | } | |
1580 | ||
1581 | /** Send a mode change string for \a sptr to \a cptr. | |
1582 | * @param[in] cptr Destination of mode change message. | |
1583 | * @param[in] sptr User whose mode has changed. | |
1584 | * @param[in] old Pre-change set of modes for \a sptr. | |
1585 | * @param[in] sendset One of ALL_UMODES, SEND_UMODES_BUT_OPER, | |
1586 | * SEND_UMODES, to select which changed user modes to send. | |
1587 | */ | |
1588 | void send_umode(struct Client *cptr, struct Client *sptr, struct Flags *old, | |
1589 | int sendset) | |
1590 | { | |
1591 | int i; | |
1592 | int flag; | |
d8e74551 | 1593 | int needhost = 0; |
189935b1 | 1594 | char *m; |
1595 | int what = MODE_NULL; | |
1596 | ||
1597 | /* | |
1598 | * Build a string in umodeBuf to represent the change in the user's | |
1599 | * mode between the new (cli_flags(sptr)) and 'old', but skipping | |
1600 | * the modes indicated by sendset. | |
1601 | */ | |
1602 | m = umodeBuf; | |
1603 | *m = '\0'; | |
1604 | for (i = 0; i < USERMODELIST_SIZE; ++i) | |
1605 | { | |
1606 | flag = userModeList[i].flag; | |
1607 | if (FlagHas(old, flag) | |
1608 | == HasFlag(sptr, flag)) | |
1609 | continue; | |
1610 | switch (sendset) | |
1611 | { | |
1612 | case ALL_UMODES: | |
1613 | break; | |
1614 | case SEND_UMODES_BUT_OPER: | |
1615 | if (flag == FLAG_OPER) | |
1616 | continue; | |
1617 | /* and fall through */ | |
1618 | case SEND_UMODES: | |
1619 | if (flag < FLAG_GLOBAL_UMODES) | |
1620 | continue; | |
1621 | break; | |
1622 | } | |
d8e74551 | 1623 | /* Special case for SETHOST.. */ |
1624 | if (flag == FLAG_SETHOST) { | |
1625 | /* Don't send to users */ | |
1626 | if (cptr && MyUser(cptr)) | |
1627 | continue; | |
1628 | ||
1629 | /* If we're setting +h, add the parameter later */ | |
1630 | if (!FlagHas(old, flag)) | |
1631 | needhost++; | |
1632 | } | |
189935b1 | 1633 | if (FlagHas(old, flag)) |
1634 | { | |
1635 | if (what == MODE_DEL) | |
1636 | *m++ = userModeList[i].c; | |
1637 | else | |
1638 | { | |
1639 | what = MODE_DEL; | |
1640 | *m++ = '-'; | |
1641 | *m++ = userModeList[i].c; | |
1642 | } | |
1643 | } | |
1644 | else /* !FlagHas(old, flag) */ | |
1645 | { | |
1646 | if (what == MODE_ADD) | |
1647 | *m++ = userModeList[i].c; | |
1648 | else | |
1649 | { | |
1650 | what = MODE_ADD; | |
1651 | *m++ = '+'; | |
1652 | *m++ = userModeList[i].c; | |
1653 | } | |
1654 | } | |
1655 | } | |
d8e74551 | 1656 | if (needhost) { |
1657 | *m++ = ' '; | |
1658 | ircd_snprintf(0, m, USERLEN + HOSTLEN + 1, "%s@%s", cli_user(sptr)->username, | |
1659 | cli_user(sptr)->host); | |
1660 | } else | |
1661 | *m = '\0'; | |
189935b1 | 1662 | if (*umodeBuf && cptr) |
d8e74551 | 1663 | sendcmdto_one(sptr, CMD_MODE, cptr, "%s %s", cli_name(sptr), umodeBuf); |
189935b1 | 1664 | } |
1665 | ||
1666 | /** | |
1667 | * Check to see if this resembles a sno_mask. It is if 1) there is | |
1668 | * at least one digit and 2) The first digit occurs before the first | |
1669 | * alphabetic character. | |
1670 | * @param[in] word Word to check for sno_mask-ness. | |
1671 | * @return Non-zero if \a word looks like a server notice mask; zero if not. | |
1672 | */ | |
1673 | int is_snomask(char *word) | |
1674 | { | |
1675 | if (word) | |
1676 | { | |
1677 | for (; *word; word++) | |
1678 | if (IsDigit(*word)) | |
1679 | return 1; | |
1680 | else if (IsAlpha(*word)) | |
1681 | return 0; | |
1682 | } | |
1683 | return 0; | |
1684 | } | |
1685 | ||
d8e74551 | 1686 | /* |
1687 | * Check to see if it resembles a valid hostmask. | |
1688 | */ | |
1689 | int is_hostmask(char *word) | |
1690 | { | |
1691 | int i = 0; | |
1692 | char *host; | |
1693 | ||
1694 | Debug((DEBUG_INFO, "is_hostmask() %s", word)); | |
1695 | ||
1696 | if (strlen(word) > (HOSTLEN + USERLEN + 1) || strlen(word) <= 0) | |
1697 | return 0; | |
1698 | ||
1699 | /* if a host is specified, make sure it's valid */ | |
1700 | host = strrchr(word, '@'); | |
1701 | if (host) { | |
1702 | if (strlen(++host) < 1) | |
1703 | return 0; | |
1704 | if (strlen(host) > HOSTLEN) | |
1705 | return 0; | |
1706 | } | |
1707 | ||
1708 | if (word) { | |
1709 | if ('@' == *word) /* no leading @'s */ | |
1710 | return 0; | |
1711 | ||
1712 | if ('#' == *word) { /* numeric index given? */ | |
1713 | for (word++; *word; word++) { | |
1714 | if (!IsDigit(*word)) | |
1715 | return 0; | |
1716 | } | |
1717 | return 1; | |
1718 | } | |
1719 | ||
1720 | /* normal hostmask, account for at most one '@' */ | |
1721 | for (; *word; word++) { | |
1722 | if ('@' == *word) { | |
1723 | i++; | |
1724 | continue; | |
1725 | } | |
1726 | if (!IsHostChar(*word)) | |
1727 | return 0; | |
1728 | } | |
1729 | return (1 < i) ? 0 : 1; /* no more than on '@' */ | |
1730 | } | |
1731 | return 0; | |
1732 | } | |
1733 | ||
1734 | /* | |
1735 | * IsVhost() - Check if given host is a valid spoofhost | |
1736 | * (ie: configured thru a S:line) | |
1737 | */ | |
1738 | static char *IsVhost(char *hostmask, int oper) | |
1739 | { | |
1740 | unsigned int i = 0, y = 0; | |
1741 | struct sline *sconf; | |
1742 | ||
1743 | Debug((DEBUG_INFO, "IsVhost() %s", hostmask)); | |
1744 | ||
1745 | if (EmptyString(hostmask)) | |
1746 | return NULL; | |
1747 | ||
1748 | /* spoofhost specified as index, ie: #27 */ | |
1749 | if ('#' == hostmask[0]) { | |
1750 | y = atoi(hostmask + 1); | |
1751 | for (i = 0, sconf = GlobalSList; sconf; sconf = sconf->next) { | |
1752 | if (!oper && EmptyString(sconf->passwd)) | |
1753 | continue; | |
1754 | if (y == ++i) | |
1755 | return sconf->spoofhost; | |
1756 | } | |
1757 | return NULL; | |
1758 | } | |
1759 | ||
1760 | /* spoofhost specified as host, ie: host.cc */ | |
1761 | for (sconf = GlobalSList; sconf; sconf = sconf->next) | |
1762 | if (strCasediff(hostmask, sconf->spoofhost) == 0) | |
1763 | return sconf->spoofhost; | |
1764 | ||
1765 | return NULL; | |
1766 | } | |
1767 | ||
1768 | /* | |
1769 | * IsVhostPass() - Check if given spoofhost has a password | |
1770 | * associated with it, and if, return the password (cleartext) | |
1771 | */ | |
1772 | static char *IsVhostPass(char *hostmask) | |
1773 | { | |
1774 | struct sline *sconf; | |
1775 | ||
1776 | Debug((DEBUG_INFO, "IsVhostPass() %s", hostmask)); | |
1777 | ||
1778 | if (EmptyString(hostmask)) | |
1779 | return NULL; | |
1780 | ||
1781 | for (sconf = GlobalSList; sconf; sconf = sconf->next) | |
1782 | if (strCasediff(hostmask, sconf->spoofhost) == 0) { | |
1783 | Debug((DEBUG_INFO, "sconf->passwd %s", sconf->passwd)); | |
1784 | return EmptyString(sconf->passwd) ? NULL : sconf->passwd; | |
1785 | } | |
1786 | ||
1787 | return NULL; | |
1788 | } | |
1789 | ||
189935b1 | 1790 | /** Update snomask \a oldmask according to \a arg and \a what. |
1791 | * @param[in] oldmask Original user mask. | |
1792 | * @param[in] arg Update string (either a number or '+'/'-' followed by a number). | |
1793 | * @param[in] what MODE_ADD if adding the mask. | |
1794 | * @return New value of service notice mask. | |
1795 | */ | |
1796 | unsigned int umode_make_snomask(unsigned int oldmask, char *arg, int what) | |
1797 | { | |
1798 | unsigned int sno_what; | |
1799 | unsigned int newmask; | |
1800 | if (*arg == '+') | |
1801 | { | |
1802 | arg++; | |
1803 | if (what == MODE_ADD) | |
1804 | sno_what = SNO_ADD; | |
1805 | else | |
1806 | sno_what = SNO_DEL; | |
1807 | } | |
1808 | else if (*arg == '-') | |
1809 | { | |
1810 | arg++; | |
1811 | if (what == MODE_ADD) | |
1812 | sno_what = SNO_DEL; | |
1813 | else | |
1814 | sno_what = SNO_ADD; | |
1815 | } | |
1816 | else | |
1817 | sno_what = (what == MODE_ADD) ? SNO_SET : SNO_DEL; | |
1818 | /* pity we don't have strtoul everywhere */ | |
1819 | newmask = (unsigned int)atoi(arg); | |
1820 | if (sno_what == SNO_DEL) | |
1821 | newmask = oldmask & ~newmask; | |
1822 | else if (sno_what == SNO_ADD) | |
1823 | newmask |= oldmask; | |
1824 | return newmask; | |
1825 | } | |
1826 | ||
1827 | /** Remove \a cptr from the singly linked list \a list. | |
1828 | * @param[in] cptr Client to remove from list. | |
1829 | * @param[in,out] list Pointer to head of list containing \a cptr. | |
1830 | */ | |
1831 | static void delfrom_list(struct Client *cptr, struct SLink **list) | |
1832 | { | |
1833 | struct SLink* tmp; | |
1834 | struct SLink* prv = NULL; | |
1835 | ||
1836 | for (tmp = *list; tmp; tmp = tmp->next) { | |
1837 | if (tmp->value.cptr == cptr) { | |
1838 | if (prv) | |
1839 | prv->next = tmp->next; | |
1840 | else | |
1841 | *list = tmp->next; | |
1842 | free_link(tmp); | |
1843 | break; | |
1844 | } | |
1845 | prv = tmp; | |
1846 | } | |
1847 | } | |
1848 | ||
1849 | /** Set \a cptr's server notice mask, according to \a what. | |
1850 | * @param[in,out] cptr Client whose snomask is updating. | |
1851 | * @param[in] newmask Base value for new snomask. | |
1852 | * @param[in] what One of SNO_ADD, SNO_DEL, SNO_SET, to choose operation. | |
1853 | */ | |
1854 | void set_snomask(struct Client *cptr, unsigned int newmask, int what) | |
1855 | { | |
1856 | unsigned int oldmask, diffmask; /* unsigned please */ | |
1857 | int i; | |
1858 | struct SLink *tmp; | |
1859 | ||
1860 | oldmask = cli_snomask(cptr); | |
1861 | ||
1862 | if (what == SNO_ADD) | |
1863 | newmask |= oldmask; | |
1864 | else if (what == SNO_DEL) | |
1865 | newmask = oldmask & ~newmask; | |
1866 | else if (what != SNO_SET) /* absolute set, no math needed */ | |
1867 | sendto_opmask_butone(0, SNO_OLDSNO, "setsnomask called with %d ?!", what); | |
1868 | ||
1869 | newmask &= (IsAnOper(cptr) ? SNO_ALL : SNO_USER); | |
1870 | ||
1871 | diffmask = oldmask ^ newmask; | |
1872 | ||
1873 | for (i = 0; diffmask >> i; i++) { | |
1874 | if (((diffmask >> i) & 1)) | |
1875 | { | |
1876 | if (((newmask >> i) & 1)) | |
1877 | { | |
1878 | tmp = make_link(); | |
1879 | tmp->next = opsarray[i]; | |
1880 | tmp->value.cptr = cptr; | |
1881 | opsarray[i] = tmp; | |
1882 | } | |
1883 | else | |
1884 | /* not real portable :( */ | |
1885 | delfrom_list(cptr, &opsarray[i]); | |
1886 | } | |
1887 | } | |
1888 | cli_snomask(cptr) = newmask; | |
1889 | } | |
1890 | ||
1891 | /** Check whether \a sptr is allowed to send a message to \a acptr. | |
1892 | * If \a sptr is a remote user, it means some server has an outdated | |
1893 | * SILENCE list for \a acptr, so send the missing SILENCE mask(s) back | |
1894 | * in the direction of \a sptr. Skip the check if \a sptr is a server. | |
1895 | * @param[in] sptr Client trying to send a message. | |
1896 | * @param[in] acptr Destination of message. | |
1897 | * @return Non-zero if \a sptr is SILENCEd by \a acptr, zero if not. | |
1898 | */ | |
1899 | int is_silenced(struct Client *sptr, struct Client *acptr) | |
1900 | { | |
1901 | struct Ban *found; | |
1902 | struct User *user; | |
1903 | size_t buf_used, slen; | |
1904 | char buf[BUFSIZE]; | |
1905 | ||
1906 | if (IsServer(sptr) || !(user = cli_user(acptr)) | |
1907 | || !(found = find_ban(sptr, user->silence))) | |
1908 | return 0; | |
1909 | assert(!(found->flags & BAN_EXCEPTION)); | |
1910 | if (!MyConnect(sptr)) { | |
1911 | /* Buffer positive silence to send back. */ | |
1912 | buf_used = strlen(found->banstr); | |
1913 | memcpy(buf, found->banstr, buf_used); | |
1914 | /* Add exceptions to buffer. */ | |
1915 | for (found = user->silence; found; found = found->next) { | |
1916 | if (!(found->flags & BAN_EXCEPTION)) | |
1917 | continue; | |
1918 | slen = strlen(found->banstr); | |
1919 | if (buf_used + slen + 4 > 400) { | |
1920 | buf[buf_used] = '\0'; | |
1921 | sendcmdto_one(acptr, CMD_SILENCE, cli_from(sptr), "%C %s", sptr, buf); | |
1922 | buf_used = 0; | |
1923 | } | |
1924 | if (buf_used) | |
1925 | buf[buf_used++] = ','; | |
1926 | buf[buf_used++] = '+'; | |
1927 | buf[buf_used++] = '~'; | |
1928 | memcpy(buf + buf_used, found->banstr, slen); | |
1929 | buf_used += slen; | |
1930 | } | |
1931 | /* Flush silence buffer. */ | |
1932 | if (buf_used) { | |
1933 | buf[buf_used] = '\0'; | |
1934 | sendcmdto_one(acptr, CMD_SILENCE, cli_from(sptr), "%C %s", sptr, buf); | |
1935 | buf_used = 0; | |
1936 | } | |
1937 | } | |
1938 | return 1; | |
1939 | } | |
1940 | ||
1941 | /** Send RPL_ISUPPORT lines to \a cptr. | |
1942 | * @param[in] cptr Client to send ISUPPORT to. | |
1943 | * @return Zero. | |
1944 | */ | |
1945 | int | |
1946 | send_supported(struct Client *cptr) | |
1947 | { | |
1948 | char featurebuf[512]; | |
1949 | ||
1950 | ircd_snprintf(0, featurebuf, sizeof(featurebuf), FEATURES1, FEATURESVALUES1); | |
1951 | send_reply(cptr, RPL_ISUPPORT, featurebuf); | |
1952 | ircd_snprintf(0, featurebuf, sizeof(featurebuf), FEATURES2, FEATURESVALUES2); | |
1953 | send_reply(cptr, RPL_ISUPPORT, featurebuf); | |
1954 | ||
1955 | return 0; /* convenience return, if it's ever needed */ | |
1956 | } |