]> jfr.im git - irc/rqf/shadowircd.git/blob - modules/m_chghost.c
libcharybdis includes gone.
[irc/rqf/shadowircd.git] / modules / m_chghost.c
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"
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"
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"
28 #include "sprintf_irc.h"
29 #include "whowas.h"
30 #include "monitor.h"
31
32 static int me_realhost(struct Client *, struct Client *, int, const char **);
33 static int ms_chghost(struct Client *, struct Client *, int, const char **);
34 static int me_chghost(struct Client *, struct Client *, int, const char **);
35 static int mo_chghost(struct Client *, struct Client *, int, const char **);
36
37 struct Message realhost_msgtab = {
38 "REALHOST", 0, 0, 0, MFLG_SLOW,
39 {mg_ignore, mg_ignore, mg_ignore, mg_ignore, {me_realhost, 2}, mg_ignore}
40 };
41
42 struct Message chghost_msgtab = {
43 "CHGHOST", 0, 0, 0, MFLG_SLOW,
44 {mg_ignore, mg_not_oper, {ms_chghost, 3}, {ms_chghost, 3}, {me_chghost, 3}, {mo_chghost, 3}}
45 };
46
47 mapi_clist_av1 chghost_clist[] = { &chghost_msgtab, &realhost_msgtab, NULL };
48
49 DECLARE_MODULE_AV1(chghost, NULL, NULL, chghost_clist, NULL, NULL, "$Revision: 3424 $");
50
51 /* clean_host()
52 *
53 * input - host to check
54 * output - 0 if erroneous, else 0
55 * side effects -
56 */
57 static int
58 clean_host(const char *host)
59 {
60 int len = 0;
61
62 if (*host == '\0' || *host == ':')
63 return 0;
64
65 for(; *host; host++)
66 {
67 len++;
68
69 if(!IsHostChar(*host))
70 return 0;
71 }
72
73 if(len > HOSTLEN)
74 return 0;
75
76 return 1;
77 }
78
79 /*
80 * me_realhost
81 * parv[0] = origin
82 * parv[1] = real host
83 *
84 * Yes this contains a little race condition if someone does a whois
85 * in between the UID and REALHOST and use_whois_actually is enabled.
86 * I don't think that's a big problem as the whole thing is a
87 * race condition.
88 */
89 static int
90 me_realhost(struct Client *client_p, struct Client *source_p,
91 int parc, const char *parv[])
92 {
93 if (!IsPerson(source_p))
94 return 0;
95
96 del_from_hostname_hash(source_p->orighost, source_p);
97 strlcpy(source_p->orighost, parv[1], sizeof source_p->orighost);
98 if (irccmp(source_p->host, source_p->orighost))
99 SetDynSpoof(source_p);
100 else
101 ClearDynSpoof(source_p);
102 add_to_hostname_hash(source_p->orighost, source_p);
103 return 0;
104 }
105
106 static int
107 do_chghost(struct Client *source_p, struct Client *target_p,
108 const char *newhost, int is_encap)
109 {
110 if (!clean_host(newhost))
111 {
112 sendto_realops_snomask(SNO_GENERAL, is_encap ? L_ALL : L_NETWIDE, "%s attempted to change hostname for %s to %s (invalid)",
113 IsServer(source_p) ? source_p->name : get_oper_name(source_p),
114 target_p->name, newhost);
115 /* sending this remotely may disclose important
116 * routing information -- jilles */
117 if (is_encap ? MyClient(target_p) : !ConfigServerHide.flatten_links)
118 sendto_one_notice(target_p, ":*** Notice -- %s attempted to change your hostname to %s (invalid)",
119 source_p->name, newhost);
120 return 0;
121 }
122 change_nick_user_host(target_p, target_p->name, target_p->username, newhost, 0, "Changing host");
123 if (irccmp(target_p->host, target_p->orighost))
124 {
125 SetDynSpoof(target_p);
126 if (MyClient(target_p))
127 sendto_one_numeric(target_p, RPL_HOSTHIDDEN, "%s :is now your hidden host (set by %s)", target_p->host, source_p->name);
128 }
129 else
130 {
131 ClearDynSpoof(target_p);
132 if (MyClient(target_p))
133 sendto_one_numeric(target_p, RPL_HOSTHIDDEN, "%s :hostname reset by %s", target_p->host, source_p->name);
134 }
135 if (MyClient(source_p))
136 sendto_one_notice(source_p, ":Changed hostname for %s to %s", target_p->name, target_p->host);
137 if (!IsServer(source_p) && !IsService(source_p))
138 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);
139 return 1;
140 }
141
142 /*
143 * ms_chghost
144 * parv[0] = origin
145 * parv[1] = target
146 * parv[2] = host
147 */
148 static int
149 ms_chghost(struct Client *client_p, struct Client *source_p,
150 int parc, const char *parv[])
151 {
152 struct Client *target_p;
153
154 if (!(target_p = find_person(parv[1])))
155 return -1;
156
157 if (do_chghost(source_p, target_p, parv[2], 0))
158 {
159 sendto_server(client_p, NULL,
160 CAP_EUID | CAP_TS6, NOCAPS, ":%s CHGHOST %s %s",
161 use_id(source_p), use_id(target_p), parv[2]);
162 sendto_server(client_p, NULL,
163 CAP_TS6, CAP_EUID, ":%s ENCAP * CHGHOST %s :%s",
164 use_id(source_p), use_id(target_p), parv[2]);
165 sendto_server(client_p, NULL,
166 NOCAPS, CAP_TS6, ":%s ENCAP * CHGHOST %s :%s",
167 source_p->name, target_p->name, parv[2]);
168 }
169
170 return 0;
171 }
172
173 /*
174 * me_chghost
175 * parv[0] = origin
176 * parv[1] = target
177 * parv[2] = host
178 */
179 static int
180 me_chghost(struct Client *client_p, struct Client *source_p,
181 int parc, const char *parv[])
182 {
183 struct Client *target_p;
184
185 if (!(target_p = find_person(parv[1])))
186 return -1;
187
188 do_chghost(source_p, target_p, parv[2], 1);
189
190 return 0;
191 }
192
193 /*
194 * mo_chghost
195 * parv[0] = origin
196 * parv[1] = target
197 * parv[2] = host
198 */
199 /* Disable this because of the abuse potential -- jilles
200 * No, make it toggleable via ./configure. --nenolod
201 */
202 static int
203 mo_chghost(struct Client *client_p, struct Client *source_p,
204 int parc, const char *parv[])
205 {
206 #ifdef ENABLE_OPER_CHGHOST
207 struct Client *target_p;
208
209 if(!IsOperAdmin(source_p))
210 {
211 sendto_one(source_p, form_str(ERR_NOPRIVS),
212 me.name, source_p->name, "admin");
213 return 0;
214 }
215
216 if (!(target_p = find_named_person(parv[1])))
217 {
218 sendto_one_numeric(source_p, ERR_NOSUCHNICK,
219 form_str(ERR_NOSUCHNICK), parv[1]);
220 return 0;
221 }
222
223 if (!clean_host(parv[2]))
224 {
225 sendto_one_notice(source_p, ":Hostname %s is invalid", parv[2]);
226 return 0;
227 }
228
229 do_chghost(source_p, target_p, parv[2], 0);
230
231 sendto_server(NULL, NULL,
232 CAP_EUID | CAP_TS6, NOCAPS, ":%s CHGHOST %s %s",
233 use_id(source_p), use_id(target_p), parv[2]);
234 sendto_server(NULL, NULL,
235 CAP_TS6, CAP_EUID, ":%s ENCAP * CHGHOST %s :%s",
236 use_id(source_p), use_id(target_p), parv[2]);
237 sendto_server(NULL, NULL,
238 NOCAPS, CAP_TS6, ":%s ENCAP * CHGHOST %s :%s",
239 source_p->name, target_p->name, parv[2]);
240 #else
241 sendto_one_numeric(source_p, ERR_DISABLED, form_str(ERR_DISABLED),
242 "CHGHOST");
243 #endif
244
245 return 0;
246 }
247