]>
Commit | Line | Data |
---|---|---|
212380e3 AC |
1 | /* modules/m_services.c |
2 | * Copyright (C) 2005 Lee Hardy <lee -at- leeh.co.uk> | |
3 | * Copyright (C) 2005 ircd-ratbox development team | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions are | |
7 | * met: | |
8 | * | |
9 | * 1.Redistributions of source code must retain the above copyright notice, | |
10 | * this list of conditions and the following disclaimer. | |
11 | * 2.Redistributions in binary form must reproduce the above copyright | |
12 | * notice, this list of conditions and the following disclaimer in the | |
13 | * documentation and/or other materials provided with the distribution. | |
14 | * 3.The name of the author may not be used to endorse or promote products | |
15 | * derived from this software without specific prior written permission. | |
16 | * | |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
19 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
20 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, | |
21 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
22 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
23 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
25 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING | |
26 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
27 | * POSSIBILITY OF SUCH DAMAGE. | |
28 | * | |
29 | * $Id: m_services.c 1907 2006-08-29 19:18:15Z jilles $ | |
30 | */ | |
31 | ||
32 | #include "stdinc.h" | |
33 | ||
212380e3 AC |
34 | #include "send.h" |
35 | #include "channel.h" | |
36 | #include "client.h" | |
37 | #include "common.h" | |
38 | #include "config.h" | |
39 | #include "ircd.h" | |
40 | #include "numeric.h" | |
212380e3 AC |
41 | #include "s_conf.h" |
42 | #include "s_newconf.h" | |
43 | #include "s_serv.h" | |
44 | #include "hash.h" | |
45 | #include "msg.h" | |
46 | #include "parse.h" | |
47 | #include "modules.h" | |
212380e3 AC |
48 | #include "whowas.h" |
49 | #include "monitor.h" | |
50 | ||
c46a4ecd KB |
51 | static int _modinit(void); |
52 | static void _moddeinit(void); | |
53 | ||
54 | static void mark_services(void); | |
55 | static void unmark_services(void); | |
56 | ||
212380e3 AC |
57 | static int me_su(struct Client *, struct Client *, int, const char **); |
58 | static int me_login(struct Client *, struct Client *, int, const char **); | |
59 | static int me_rsfnc(struct Client *, struct Client *, int, const char **); | |
60 | static int me_nickdelay(struct Client *, struct Client *, int, const char **); | |
61 | ||
62 | static void h_svc_server_introduced(hook_data_client *); | |
63 | static void h_svc_whois(hook_data_client *); | |
64 | static void h_svc_stats(hook_data_int *); | |
c46a4ecd KB |
65 | static void h_svc_conf_read_start(void *); |
66 | static void h_svc_conf_read_end(void *); | |
212380e3 AC |
67 | |
68 | struct Message su_msgtab = { | |
69 | "SU", 0, 0, 0, MFLG_SLOW, | |
70 | {mg_ignore, mg_ignore, mg_ignore, mg_ignore, {me_su, 2}, mg_ignore} | |
71 | }; | |
72 | struct Message login_msgtab = { | |
73 | "LOGIN", 0, 0, 0, MFLG_SLOW, | |
74 | {mg_ignore, mg_ignore, mg_ignore, mg_ignore, {me_login, 2}, mg_ignore} | |
75 | }; | |
76 | struct Message rsfnc_msgtab = { | |
77 | "RSFNC", 0, 0, 0, MFLG_SLOW, | |
78 | {mg_ignore, mg_ignore, mg_ignore, mg_ignore, {me_rsfnc, 4}, mg_ignore} | |
79 | }; | |
80 | struct Message nickdelay_msgtab = { | |
81 | "NICKDELAY", 0, 0, 0, MFLG_SLOW, | |
82 | {mg_unreg, mg_ignore, mg_ignore, mg_ignore, {me_nickdelay, 3}, mg_ignore} | |
83 | }; | |
84 | ||
85 | mapi_clist_av1 services_clist[] = { | |
86 | &su_msgtab, &login_msgtab, &rsfnc_msgtab, &nickdelay_msgtab, NULL | |
87 | }; | |
88 | mapi_hfn_list_av1 services_hfnlist[] = { | |
89 | { "doing_stats", (hookfn) h_svc_stats }, | |
90 | { "doing_whois", (hookfn) h_svc_whois }, | |
91 | { "doing_whois_global", (hookfn) h_svc_whois }, | |
92 | { "server_introduced", (hookfn) h_svc_server_introduced }, | |
c46a4ecd KB |
93 | { "conf_read_start", (hookfn) h_svc_conf_read_start }, |
94 | { "conf_read_end", (hookfn) h_svc_conf_read_end }, | |
212380e3 AC |
95 | { NULL, NULL } |
96 | }; | |
97 | ||
c46a4ecd KB |
98 | DECLARE_MODULE_AV1(services, _modinit, _moddeinit, services_clist, NULL, services_hfnlist, "$Revision: 1907 $"); |
99 | ||
100 | static int | |
101 | _modinit(void) | |
102 | { | |
103 | mark_services(); | |
104 | } | |
105 | ||
106 | static void | |
107 | _moddeinit(void) | |
108 | { | |
109 | unmark_services(); | |
110 | } | |
212380e3 AC |
111 | |
112 | static int | |
113 | me_su(struct Client *client_p, struct Client *source_p, | |
114 | int parc, const char *parv[]) | |
115 | { | |
116 | struct Client *target_p; | |
117 | ||
118 | if(!(source_p->flags & FLAGS_SERVICE)) | |
ec57fe67 KB |
119 | { |
120 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
121 | "Non-service server %s attempting to execute services-only command SU", source_p->name); | |
212380e3 | 122 | return 0; |
ec57fe67 | 123 | } |
212380e3 AC |
124 | |
125 | if((target_p = find_client(parv[1])) == NULL) | |
126 | return 0; | |
127 | ||
128 | if(!target_p->user) | |
129 | return 0; | |
130 | ||
131 | if(EmptyString(parv[2])) | |
132 | target_p->user->suser[0] = '\0'; | |
133 | else | |
f427c8b0 | 134 | rb_strlcpy(target_p->user->suser, parv[2], sizeof(target_p->user->suser)); |
212380e3 | 135 | |
261e2f93 | 136 | sendto_common_channels_local_butone(target_p, CLICAP_ACCOUNT_NOTIFY, ":%s!%s@%s ACCOUNT %s", |
7a7f86d3 AC |
137 | target_p->name, target_p->username, target_p->host, |
138 | EmptyString(target_p->user->suser) ? "*" : target_p->user->suser); | |
139 | ||
212380e3 AC |
140 | invalidate_bancache_user(target_p); |
141 | ||
142 | return 0; | |
143 | } | |
144 | ||
145 | static int | |
146 | me_login(struct Client *client_p, struct Client *source_p, | |
147 | int parc, const char *parv[]) | |
148 | { | |
149 | if(!IsPerson(source_p)) | |
150 | return 0; | |
151 | ||
f427c8b0 | 152 | rb_strlcpy(source_p->user->suser, parv[1], sizeof(source_p->user->suser)); |
212380e3 AC |
153 | return 0; |
154 | } | |
155 | ||
156 | static int | |
157 | clean_nick(const char *nick) | |
158 | { | |
159 | int len = 0; | |
160 | ||
161 | if(EmptyString(nick) || *nick == '-' || IsDigit(*nick)) | |
162 | return 0; | |
163 | ||
164 | for(; *nick; nick++) | |
165 | { | |
166 | len++; | |
167 | if(!IsNickChar(*nick)) | |
168 | return 0; | |
169 | } | |
170 | ||
171 | if(len >= NICKLEN) | |
172 | return 0; | |
173 | ||
174 | return 1; | |
175 | } | |
176 | ||
177 | static int | |
178 | me_rsfnc(struct Client *client_p, struct Client *source_p, | |
179 | int parc, const char *parv[]) | |
180 | { | |
181 | struct Client *target_p; | |
182 | struct Client *exist_p; | |
183 | time_t newts, curts; | |
75d60088 | 184 | char note[NICKLEN + 10]; |
212380e3 AC |
185 | |
186 | if(!(source_p->flags & FLAGS_SERVICE)) | |
ec57fe67 KB |
187 | { |
188 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
189 | "Non-service server %s attempting to execute services-only command RSFNC", source_p->name); | |
212380e3 | 190 | return 0; |
ec57fe67 | 191 | } |
212380e3 AC |
192 | |
193 | if((target_p = find_person(parv[1])) == NULL) | |
194 | return 0; | |
195 | ||
196 | if(!MyClient(target_p)) | |
197 | return 0; | |
198 | ||
199 | if(!clean_nick(parv[2])) | |
200 | return 0; | |
201 | ||
202 | curts = atol(parv[4]); | |
203 | ||
204 | /* if tsinfo is different from what it was when services issued the | |
205 | * RSFNC, then we ignore it. This can happen when a client changes | |
206 | * nicknames before the RSFNC arrives.. --anfl | |
207 | */ | |
208 | if(target_p->tsinfo != curts) | |
209 | return 0; | |
210 | ||
211 | if((exist_p = find_named_client(parv[2]))) | |
212 | { | |
213 | char buf[BUFSIZE]; | |
214 | ||
215 | /* this would be one hell of a race condition to trigger | |
216 | * this one given the tsinfo check above, but its here for | |
217 | * safety --anfl | |
218 | */ | |
219 | if(target_p == exist_p) | |
03510227 | 220 | goto doit; |
212380e3 AC |
221 | |
222 | if(MyClient(exist_p)) | |
223 | sendto_one(exist_p, ":%s KILL %s :(Nickname regained by services)", | |
224 | me.name, exist_p->name); | |
225 | ||
226 | exist_p->flags |= FLAGS_KILLED; | |
227 | /* Do not send kills to servers for unknowns -- jilles */ | |
228 | if(IsClient(exist_p)) | |
45ed8835 | 229 | { |
212380e3 AC |
230 | kill_client_serv_butone(NULL, exist_p, "%s (Nickname regained by services)", |
231 | me.name); | |
45ed8835 JT |
232 | sendto_realops_snomask(SNO_SKILL, L_ALL, |
233 | "Nick collision due to services forced nick change on %s", | |
234 | parv[2]); | |
235 | } | |
212380e3 | 236 | |
c2f73e5d | 237 | rb_snprintf(buf, sizeof(buf), "Killed (%s (Nickname regained by services))", |
212380e3 AC |
238 | me.name); |
239 | exit_client(NULL, exist_p, &me, buf); | |
240 | } | |
241 | ||
03510227 | 242 | doit: |
212380e3 AC |
243 | newts = atol(parv[3]); |
244 | ||
245 | /* timestamp is older than 15mins, ignore it */ | |
e3354945 VY |
246 | if(newts < (rb_current_time() - 900)) |
247 | newts = rb_current_time() - 900; | |
212380e3 AC |
248 | |
249 | target_p->tsinfo = newts; | |
250 | ||
251 | monitor_signoff(target_p); | |
252 | ||
253 | invalidate_bancache_user(target_p); | |
254 | ||
255 | sendto_realops_snomask(SNO_NCHANGE, L_ALL, | |
256 | "Nick change: From %s to %s [%s@%s]", | |
257 | target_p->name, parv[2], target_p->username, | |
258 | target_p->host); | |
259 | ||
7a948bda | 260 | sendto_common_channels_local(target_p, NOCAPS, ":%s!%s@%s NICK :%s", |
212380e3 AC |
261 | target_p->name, target_p->username, |
262 | target_p->host, parv[2]); | |
263 | ||
264 | add_history(target_p, 1); | |
265 | sendto_server(NULL, NULL, CAP_TS6, NOCAPS, ":%s NICK %s :%ld", | |
266 | use_id(target_p), parv[2], (long) target_p->tsinfo); | |
212380e3 AC |
267 | |
268 | del_from_client_hash(target_p->name, target_p); | |
e2606551 | 269 | rb_strlcpy(target_p->name, parv[2], NICKLEN); |
212380e3 AC |
270 | add_to_client_hash(target_p->name, target_p); |
271 | ||
272 | monitor_signon(target_p); | |
273 | ||
274 | del_all_accepts(target_p); | |
275 | ||
75d60088 AC |
276 | rb_snprintf(note, NICKLEN + 10, "Nick: %s", target_p->name); |
277 | rb_note(target_p->localClient->F, note); | |
212380e3 AC |
278 | return 0; |
279 | } | |
280 | ||
281 | /* | |
282 | ** me_nickdelay | |
212380e3 AC |
283 | ** parv[1] = duration in seconds (0 to remove) |
284 | ** parv[2] = nick | |
285 | */ | |
286 | static int | |
287 | me_nickdelay(struct Client *client_p, struct Client *source_p, int parc, const char *parv[]) | |
288 | { | |
289 | int duration; | |
290 | struct nd_entry *nd; | |
291 | ||
292 | if(!(source_p->flags & FLAGS_SERVICE)) | |
ec57fe67 KB |
293 | { |
294 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
295 | "Non-service server %s attempting to execute services-only command NICKDELAY", source_p->name); | |
212380e3 | 296 | return 0; |
ec57fe67 | 297 | } |
212380e3 AC |
298 | |
299 | duration = atoi(parv[1]); | |
300 | if (duration <= 0) | |
301 | { | |
b37021a4 | 302 | nd = irc_dictionary_retrieve(nd_dict, parv[2]); |
212380e3 AC |
303 | if (nd != NULL) |
304 | free_nd_entry(nd); | |
305 | } | |
306 | else | |
307 | { | |
308 | if (duration > 86400) | |
309 | duration = 86400; | |
310 | add_nd_entry(parv[2]); | |
b37021a4 | 311 | nd = irc_dictionary_retrieve(nd_dict, parv[2]); |
212380e3 | 312 | if (nd != NULL) |
e3354945 | 313 | nd->expire = rb_current_time() + duration; |
212380e3 AC |
314 | } |
315 | ||
316 | return 0; | |
317 | } | |
318 | ||
319 | static void | |
320 | h_svc_server_introduced(hook_data_client *hdata) | |
321 | { | |
5b96d9a6 | 322 | rb_dlink_node *ptr; |
212380e3 | 323 | |
5b96d9a6 | 324 | RB_DLINK_FOREACH(ptr, service_list.head) |
212380e3 AC |
325 | { |
326 | if(!irccmp((const char *) ptr->data, hdata->target->name)) | |
327 | { | |
328 | hdata->target->flags |= FLAGS_SERVICE; | |
329 | return; | |
330 | } | |
331 | } | |
332 | } | |
333 | ||
334 | static void | |
335 | h_svc_whois(hook_data_client *data) | |
336 | { | |
337 | char *p = data->target->user->suser; | |
338 | if(!EmptyString(p)) | |
339 | { | |
340 | /* Try to strip off any leading digits as this may be used to | |
341 | * store both an ID number and an account name in one field. | |
342 | * If only digits are present, leave as is. | |
343 | */ | |
344 | while(IsDigit(*p)) | |
345 | p++; | |
346 | if(*p == '\0') | |
347 | p = data->target->user->suser; | |
348 | ||
5b383ce0 JT |
349 | sendto_one_numeric(data->client, RPL_WHOISLOGGEDIN, |
350 | form_str(RPL_WHOISLOGGEDIN), | |
212380e3 AC |
351 | data->target->name, p); |
352 | } | |
353 | } | |
354 | ||
355 | static void | |
356 | h_svc_stats(hook_data_int *data) | |
357 | { | |
358 | char statchar = (char) data->arg2; | |
5b96d9a6 | 359 | rb_dlink_node *ptr; |
212380e3 AC |
360 | |
361 | if (statchar == 'U' && IsOper(data->client)) | |
362 | { | |
5b96d9a6 | 363 | RB_DLINK_FOREACH(ptr, service_list.head) |
212380e3 AC |
364 | { |
365 | sendto_one_numeric(data->client, RPL_STATSULINE, | |
366 | form_str(RPL_STATSULINE), | |
0cce01d3 | 367 | (const char *)ptr->data, "*", "*", "s"); |
212380e3 AC |
368 | } |
369 | } | |
370 | } | |
c46a4ecd KB |
371 | |
372 | static void | |
373 | h_svc_conf_read_start(void *dummy) | |
374 | { | |
375 | unmark_services(); | |
376 | } | |
377 | ||
378 | static void | |
379 | unmark_services(void) | |
380 | { | |
381 | struct Client *target_p; | |
382 | rb_dlink_node *ptr; | |
383 | ||
384 | RB_DLINK_FOREACH(ptr, global_serv_list.head) | |
385 | { | |
386 | target_p = ptr->data; | |
387 | ||
388 | target_p->flags &= ~FLAGS_SERVICE; | |
389 | } | |
390 | } | |
391 | ||
392 | static void | |
393 | h_svc_conf_read_end(void *dummy) | |
394 | { | |
395 | mark_services(); | |
396 | } | |
397 | ||
398 | static void | |
399 | mark_services(void) | |
400 | { | |
401 | struct Client *target_p; | |
402 | rb_dlink_node *ptr; | |
403 | ||
404 | RB_DLINK_FOREACH(ptr, service_list.head) | |
405 | { | |
406 | target_p = find_server(NULL, (const char *)ptr->data); | |
407 | ||
408 | if (target_p) | |
409 | target_p->flags |= FLAGS_SERVICE; | |
410 | } | |
411 | } |