]> jfr.im git - solanum.git/blame - modules/m_chghost.c
m_error: use strncasecmp, not strcmp
[solanum.git] / modules / m_chghost.c
CommitLineData
212380e3
AC
1/*
2 * Copyright (c) 2005 William Pitcock <nenolod -at- nenolod.net>
3 * and Jilles Tjoelker <jilles -at- stack.nl>
4 * All rights reserved.
5 *
6 * Redistribution in both source and binary forms are permitted
7 * provided that the above copyright notice remains unchanged.
8 *
9 * m_chghost.c: A module for handling spoofing dynamically.
10 */
11
12#include "stdinc.h"
212380e3
AC
13#include "send.h"
14#include "channel.h"
15#include "client.h"
16#include "common.h"
17#include "config.h"
18#include "ircd.h"
19#include "numeric.h"
212380e3
AC
20#include "s_conf.h"
21#include "s_newconf.h"
22#include "s_serv.h"
23#include "s_user.h"
24#include "hash.h"
25#include "msg.h"
26#include "parse.h"
27#include "modules.h"
212380e3
AC
28#include "whowas.h"
29#include "monitor.h"
30
eeabf33a
EM
31static const char chghost_desc[] = "Provides commands used to change and retrieve client hostnames";
32
3c7d6fcc
EM
33static void me_realhost(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
34static void ms_chghost(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
35static void me_chghost(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
36static void mo_chghost(struct MsgBuf *, struct Client *, struct Client *, int, const char **);
212380e3
AC
37
38struct Message realhost_msgtab = {
7baa37a9 39 "REALHOST", 0, 0, 0, 0,
212380e3
AC
40 {mg_ignore, mg_ignore, mg_ignore, mg_ignore, {me_realhost, 2}, mg_ignore}
41};
42
43struct Message chghost_msgtab = {
7baa37a9 44 "CHGHOST", 0, 0, 0, 0,
212380e3
AC
45 {mg_ignore, mg_not_oper, {ms_chghost, 3}, {ms_chghost, 3}, {me_chghost, 3}, {mo_chghost, 3}}
46};
47
48mapi_clist_av1 chghost_clist[] = { &chghost_msgtab, &realhost_msgtab, NULL };
49
5544da98 50DECLARE_MODULE_AV2(chghost, NULL, NULL, chghost_clist, NULL, NULL, NULL, NULL, chghost_desc);
212380e3
AC
51
52/* clean_host()
53 *
54 * input - host to check
b8b72cbd 55 * output - false if erroneous, else true
212380e3
AC
56 * side effects -
57 */
b8b72cbd 58static bool
212380e3
AC
59clean_host(const char *host)
60{
61 int len = 0;
9a180ae3 62 const char *last_slash = 0;
55abcbb2 63
822a4a25 64 if (*host == '\0' || *host == ':')
b8b72cbd 65 return false;
212380e3
AC
66
67 for(; *host; host++)
68 {
69 len++;
70
71 if(!IsHostChar(*host))
b8b72cbd 72 return false;
9a180ae3
SB
73 if(*host == '/')
74 last_slash = host;
212380e3
AC
75 }
76
77 if(len > HOSTLEN)
b8b72cbd 78 return false;
212380e3 79
9a180ae3 80 if(last_slash && IsDigit(last_slash[1]))
b8b72cbd 81 return false;
9a180ae3 82
b8b72cbd 83 return true;
212380e3
AC
84}
85
86/*
87 * me_realhost
212380e3
AC
88 * parv[1] = real host
89 *
90 * Yes this contains a little race condition if someone does a whois
91 * in between the UID and REALHOST and use_whois_actually is enabled.
92 * I don't think that's a big problem as the whole thing is a
93 * race condition.
94 */
3c7d6fcc 95static void
428ca87b 96me_realhost(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p,
212380e3
AC
97 int parc, const char *parv[])
98{
99 if (!IsPerson(source_p))
3c7d6fcc 100 return;
212380e3
AC
101
102 del_from_hostname_hash(source_p->orighost, source_p);
f427c8b0 103 rb_strlcpy(source_p->orighost, parv[1], sizeof source_p->orighost);
212380e3
AC
104 if (irccmp(source_p->host, source_p->orighost))
105 SetDynSpoof(source_p);
106 else
107 ClearDynSpoof(source_p);
108 add_to_hostname_hash(source_p->orighost, source_p);
212380e3
AC
109}
110
b8b72cbd 111static bool
212380e3
AC
112do_chghost(struct Client *source_p, struct Client *target_p,
113 const char *newhost, int is_encap)
114{
115 if (!clean_host(newhost))
116 {
117 sendto_realops_snomask(SNO_GENERAL, is_encap ? L_ALL : L_NETWIDE, "%s attempted to change hostname for %s to %s (invalid)",
118 IsServer(source_p) ? source_p->name : get_oper_name(source_p),
119 target_p->name, newhost);
120 /* sending this remotely may disclose important
121 * routing information -- jilles */
122 if (is_encap ? MyClient(target_p) : !ConfigServerHide.flatten_links)
123 sendto_one_notice(target_p, ":*** Notice -- %s attempted to change your hostname to %s (invalid)",
124 source_p->name, newhost);
b8b72cbd 125 return false;
212380e3
AC
126 }
127 change_nick_user_host(target_p, target_p->name, target_p->username, newhost, 0, "Changing host");
128 if (irccmp(target_p->host, target_p->orighost))
129 {
130 SetDynSpoof(target_p);
131 if (MyClient(target_p))
132 sendto_one_numeric(target_p, RPL_HOSTHIDDEN, "%s :is now your hidden host (set by %s)", target_p->host, source_p->name);
133 }
134 else
135 {
136 ClearDynSpoof(target_p);
137 if (MyClient(target_p))
138 sendto_one_numeric(target_p, RPL_HOSTHIDDEN, "%s :hostname reset by %s", target_p->host, source_p->name);
139 }
140 if (MyClient(source_p))
141 sendto_one_notice(source_p, ":Changed hostname for %s to %s", target_p->name, target_p->host);
142 if (!IsServer(source_p) && !IsService(source_p))
143 sendto_realops_snomask(SNO_GENERAL, L_ALL, "%s changed hostname for %s to %s", get_oper_name(source_p), target_p->name, target_p->host);
b8b72cbd 144 return true;
212380e3
AC
145}
146
147/*
148 * ms_chghost
212380e3
AC
149 * parv[1] = target
150 * parv[2] = host
151 */
3c7d6fcc 152static void
428ca87b 153ms_chghost(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p,
212380e3
AC
154 int parc, const char *parv[])
155{
156 struct Client *target_p;
157
158 if (!(target_p = find_person(parv[1])))
3c7d6fcc 159 return;
212380e3
AC
160
161 if (do_chghost(source_p, target_p, parv[2], 0))
162 {
163 sendto_server(client_p, NULL,
164 CAP_EUID | CAP_TS6, NOCAPS, ":%s CHGHOST %s %s",
165 use_id(source_p), use_id(target_p), parv[2]);
166 sendto_server(client_p, NULL,
167 CAP_TS6, CAP_EUID, ":%s ENCAP * CHGHOST %s :%s",
168 use_id(source_p), use_id(target_p), parv[2]);
212380e3
AC
169 }
170
3c7d6fcc 171 return;
212380e3
AC
172}
173
174/*
175 * me_chghost
212380e3
AC
176 * parv[1] = target
177 * parv[2] = host
178 */
3c7d6fcc 179static void
428ca87b 180me_chghost(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p,
212380e3
AC
181 int parc, const char *parv[])
182{
183 struct Client *target_p;
184
185 if (!(target_p = find_person(parv[1])))
3c7d6fcc 186 return;
212380e3
AC
187
188 do_chghost(source_p, target_p, parv[2], 1);
212380e3
AC
189}
190
191/*
192 * mo_chghost
212380e3
AC
193 * parv[1] = target
194 * parv[2] = host
195 */
196/* Disable this because of the abuse potential -- jilles
197 * No, make it toggleable via ./configure. --nenolod
198 */
3c7d6fcc 199static void
428ca87b 200mo_chghost(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p,
212380e3
AC
201 int parc, const char *parv[])
202{
203#ifdef ENABLE_OPER_CHGHOST
204 struct Client *target_p;
205
206 if(!IsOperAdmin(source_p))
207 {
208 sendto_one(source_p, form_str(ERR_NOPRIVS),
209 me.name, source_p->name, "admin");
3c7d6fcc 210 return;
212380e3
AC
211 }
212
213 if (!(target_p = find_named_person(parv[1])))
214 {
215 sendto_one_numeric(source_p, ERR_NOSUCHNICK,
216 form_str(ERR_NOSUCHNICK), parv[1]);
3c7d6fcc 217 return;
212380e3
AC
218 }
219
220 if (!clean_host(parv[2]))
221 {
222 sendto_one_notice(source_p, ":Hostname %s is invalid", parv[2]);
3c7d6fcc 223 return;
212380e3
AC
224 }
225
226 do_chghost(source_p, target_p, parv[2], 0);
227
228 sendto_server(NULL, NULL,
229 CAP_EUID | CAP_TS6, NOCAPS, ":%s CHGHOST %s %s",
230 use_id(source_p), use_id(target_p), parv[2]);
231 sendto_server(NULL, NULL,
232 CAP_TS6, CAP_EUID, ":%s ENCAP * CHGHOST %s :%s",
233 use_id(source_p), use_id(target_p), parv[2]);
212380e3 234#else
22f2f68a
JT
235 sendto_one_numeric(source_p, ERR_DISABLED, form_str(ERR_DISABLED),
236 "CHGHOST");
212380e3 237#endif
212380e3
AC
238}
239