]>
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 | ||
55abcbb2 | 85 | mapi_clist_av1 services_clist[] = { |
212380e3 AC |
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(); | |
8a4b8377 | 104 | return 0; |
c46a4ecd KB |
105 | } |
106 | ||
107 | static void | |
108 | _moddeinit(void) | |
109 | { | |
110 | unmark_services(); | |
111 | } | |
212380e3 AC |
112 | |
113 | static int | |
114 | me_su(struct Client *client_p, struct Client *source_p, | |
115 | int parc, const char *parv[]) | |
116 | { | |
117 | struct Client *target_p; | |
118 | ||
119 | if(!(source_p->flags & FLAGS_SERVICE)) | |
ec57fe67 KB |
120 | { |
121 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
122 | "Non-service server %s attempting to execute services-only command SU", source_p->name); | |
212380e3 | 123 | return 0; |
ec57fe67 | 124 | } |
212380e3 AC |
125 | |
126 | if((target_p = find_client(parv[1])) == NULL) | |
127 | return 0; | |
128 | ||
129 | if(!target_p->user) | |
130 | return 0; | |
131 | ||
132 | if(EmptyString(parv[2])) | |
133 | target_p->user->suser[0] = '\0'; | |
134 | else | |
f427c8b0 | 135 | rb_strlcpy(target_p->user->suser, parv[2], sizeof(target_p->user->suser)); |
212380e3 | 136 | |
583f064f | 137 | sendto_common_channels_local_butone(target_p, CLICAP_ACCOUNT_NOTIFY, NOCAPS, ":%s!%s@%s ACCOUNT %s", |
7a7f86d3 AC |
138 | target_p->name, target_p->username, target_p->host, |
139 | EmptyString(target_p->user->suser) ? "*" : target_p->user->suser); | |
140 | ||
212380e3 AC |
141 | invalidate_bancache_user(target_p); |
142 | ||
143 | return 0; | |
144 | } | |
145 | ||
146 | static int | |
147 | me_login(struct Client *client_p, struct Client *source_p, | |
148 | int parc, const char *parv[]) | |
149 | { | |
150 | if(!IsPerson(source_p)) | |
151 | return 0; | |
152 | ||
f427c8b0 | 153 | rb_strlcpy(source_p->user->suser, parv[1], sizeof(source_p->user->suser)); |
212380e3 AC |
154 | return 0; |
155 | } | |
156 | ||
157 | static int | |
158 | clean_nick(const char *nick) | |
159 | { | |
160 | int len = 0; | |
161 | ||
162 | if(EmptyString(nick) || *nick == '-' || IsDigit(*nick)) | |
163 | return 0; | |
164 | ||
165 | for(; *nick; nick++) | |
166 | { | |
167 | len++; | |
168 | if(!IsNickChar(*nick)) | |
169 | return 0; | |
170 | } | |
171 | ||
172 | if(len >= NICKLEN) | |
173 | return 0; | |
174 | ||
175 | return 1; | |
176 | } | |
177 | ||
178 | static int | |
179 | me_rsfnc(struct Client *client_p, struct Client *source_p, | |
180 | int parc, const char *parv[]) | |
181 | { | |
182 | struct Client *target_p; | |
183 | struct Client *exist_p; | |
184 | time_t newts, curts; | |
75d60088 | 185 | char note[NICKLEN + 10]; |
212380e3 AC |
186 | |
187 | if(!(source_p->flags & FLAGS_SERVICE)) | |
ec57fe67 KB |
188 | { |
189 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
190 | "Non-service server %s attempting to execute services-only command RSFNC", source_p->name); | |
212380e3 | 191 | return 0; |
ec57fe67 | 192 | } |
212380e3 AC |
193 | |
194 | if((target_p = find_person(parv[1])) == NULL) | |
195 | return 0; | |
196 | ||
197 | if(!MyClient(target_p)) | |
198 | return 0; | |
199 | ||
200 | if(!clean_nick(parv[2])) | |
201 | return 0; | |
202 | ||
203 | curts = atol(parv[4]); | |
204 | ||
205 | /* if tsinfo is different from what it was when services issued the | |
206 | * RSFNC, then we ignore it. This can happen when a client changes | |
207 | * nicknames before the RSFNC arrives.. --anfl | |
208 | */ | |
209 | if(target_p->tsinfo != curts) | |
210 | return 0; | |
211 | ||
212 | if((exist_p = find_named_client(parv[2]))) | |
213 | { | |
214 | char buf[BUFSIZE]; | |
215 | ||
216 | /* this would be one hell of a race condition to trigger | |
55abcbb2 | 217 | * this one given the tsinfo check above, but its here for |
212380e3 AC |
218 | * safety --anfl |
219 | */ | |
220 | if(target_p == exist_p) | |
03510227 | 221 | goto doit; |
212380e3 AC |
222 | |
223 | if(MyClient(exist_p)) | |
224 | sendto_one(exist_p, ":%s KILL %s :(Nickname regained by services)", | |
225 | me.name, exist_p->name); | |
226 | ||
227 | exist_p->flags |= FLAGS_KILLED; | |
228 | /* Do not send kills to servers for unknowns -- jilles */ | |
229 | if(IsClient(exist_p)) | |
45ed8835 | 230 | { |
212380e3 AC |
231 | kill_client_serv_butone(NULL, exist_p, "%s (Nickname regained by services)", |
232 | me.name); | |
45ed8835 JT |
233 | sendto_realops_snomask(SNO_SKILL, L_ALL, |
234 | "Nick collision due to services forced nick change on %s", | |
235 | parv[2]); | |
236 | } | |
212380e3 | 237 | |
c2f73e5d | 238 | rb_snprintf(buf, sizeof(buf), "Killed (%s (Nickname regained by services))", |
212380e3 AC |
239 | me.name); |
240 | exit_client(NULL, exist_p, &me, buf); | |
241 | } | |
242 | ||
03510227 | 243 | doit: |
212380e3 AC |
244 | newts = atol(parv[3]); |
245 | ||
246 | /* timestamp is older than 15mins, ignore it */ | |
e3354945 VY |
247 | if(newts < (rb_current_time() - 900)) |
248 | newts = rb_current_time() - 900; | |
212380e3 AC |
249 | |
250 | target_p->tsinfo = newts; | |
251 | ||
252 | monitor_signoff(target_p); | |
253 | ||
254 | invalidate_bancache_user(target_p); | |
255 | ||
256 | sendto_realops_snomask(SNO_NCHANGE, L_ALL, | |
257 | "Nick change: From %s to %s [%s@%s]", | |
258 | target_p->name, parv[2], target_p->username, | |
259 | target_p->host); | |
260 | ||
583f064f | 261 | sendto_common_channels_local(target_p, NOCAPS, NOCAPS, ":%s!%s@%s NICK :%s", |
212380e3 AC |
262 | target_p->name, target_p->username, |
263 | target_p->host, parv[2]); | |
264 | ||
265 | add_history(target_p, 1); | |
266 | sendto_server(NULL, NULL, CAP_TS6, NOCAPS, ":%s NICK %s :%ld", | |
267 | use_id(target_p), parv[2], (long) target_p->tsinfo); | |
212380e3 AC |
268 | |
269 | del_from_client_hash(target_p->name, target_p); | |
e2606551 | 270 | rb_strlcpy(target_p->name, parv[2], NICKLEN); |
212380e3 AC |
271 | add_to_client_hash(target_p->name, target_p); |
272 | ||
273 | monitor_signon(target_p); | |
274 | ||
275 | del_all_accepts(target_p); | |
276 | ||
75d60088 AC |
277 | rb_snprintf(note, NICKLEN + 10, "Nick: %s", target_p->name); |
278 | rb_note(target_p->localClient->F, note); | |
212380e3 AC |
279 | return 0; |
280 | } | |
281 | ||
282 | /* | |
283 | ** me_nickdelay | |
212380e3 AC |
284 | ** parv[1] = duration in seconds (0 to remove) |
285 | ** parv[2] = nick | |
286 | */ | |
287 | static int | |
288 | me_nickdelay(struct Client *client_p, struct Client *source_p, int parc, const char *parv[]) | |
289 | { | |
290 | int duration; | |
291 | struct nd_entry *nd; | |
292 | ||
293 | if(!(source_p->flags & FLAGS_SERVICE)) | |
ec57fe67 KB |
294 | { |
295 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
296 | "Non-service server %s attempting to execute services-only command NICKDELAY", source_p->name); | |
212380e3 | 297 | return 0; |
ec57fe67 | 298 | } |
212380e3 AC |
299 | |
300 | duration = atoi(parv[1]); | |
301 | if (duration <= 0) | |
302 | { | |
b37021a4 | 303 | nd = irc_dictionary_retrieve(nd_dict, parv[2]); |
212380e3 AC |
304 | if (nd != NULL) |
305 | free_nd_entry(nd); | |
306 | } | |
307 | else | |
308 | { | |
309 | if (duration > 86400) | |
310 | duration = 86400; | |
311 | add_nd_entry(parv[2]); | |
b37021a4 | 312 | nd = irc_dictionary_retrieve(nd_dict, parv[2]); |
212380e3 | 313 | if (nd != NULL) |
e3354945 | 314 | nd->expire = rb_current_time() + duration; |
212380e3 AC |
315 | } |
316 | ||
317 | return 0; | |
318 | } | |
319 | ||
320 | static void | |
321 | h_svc_server_introduced(hook_data_client *hdata) | |
322 | { | |
5b96d9a6 | 323 | rb_dlink_node *ptr; |
212380e3 | 324 | |
5b96d9a6 | 325 | RB_DLINK_FOREACH(ptr, service_list.head) |
212380e3 AC |
326 | { |
327 | if(!irccmp((const char *) ptr->data, hdata->target->name)) | |
328 | { | |
329 | hdata->target->flags |= FLAGS_SERVICE; | |
330 | return; | |
331 | } | |
332 | } | |
333 | } | |
334 | ||
335 | static void | |
336 | h_svc_whois(hook_data_client *data) | |
337 | { | |
338 | char *p = data->target->user->suser; | |
339 | if(!EmptyString(p)) | |
340 | { | |
341 | /* Try to strip off any leading digits as this may be used to | |
342 | * store both an ID number and an account name in one field. | |
343 | * If only digits are present, leave as is. | |
344 | */ | |
345 | while(IsDigit(*p)) | |
346 | p++; | |
347 | if(*p == '\0') | |
348 | p = data->target->user->suser; | |
349 | ||
5b383ce0 JT |
350 | sendto_one_numeric(data->client, RPL_WHOISLOGGEDIN, |
351 | form_str(RPL_WHOISLOGGEDIN), | |
212380e3 AC |
352 | data->target->name, p); |
353 | } | |
354 | } | |
355 | ||
356 | static void | |
357 | h_svc_stats(hook_data_int *data) | |
358 | { | |
359 | char statchar = (char) data->arg2; | |
5b96d9a6 | 360 | rb_dlink_node *ptr; |
212380e3 AC |
361 | |
362 | if (statchar == 'U' && IsOper(data->client)) | |
363 | { | |
5b96d9a6 | 364 | RB_DLINK_FOREACH(ptr, service_list.head) |
212380e3 AC |
365 | { |
366 | sendto_one_numeric(data->client, RPL_STATSULINE, | |
367 | form_str(RPL_STATSULINE), | |
0cce01d3 | 368 | (const char *)ptr->data, "*", "*", "s"); |
212380e3 AC |
369 | } |
370 | } | |
371 | } | |
c46a4ecd KB |
372 | |
373 | static void | |
374 | h_svc_conf_read_start(void *dummy) | |
375 | { | |
376 | unmark_services(); | |
377 | } | |
378 | ||
379 | static void | |
380 | unmark_services(void) | |
381 | { | |
382 | struct Client *target_p; | |
383 | rb_dlink_node *ptr; | |
384 | ||
385 | RB_DLINK_FOREACH(ptr, global_serv_list.head) | |
386 | { | |
387 | target_p = ptr->data; | |
388 | ||
389 | target_p->flags &= ~FLAGS_SERVICE; | |
390 | } | |
391 | } | |
392 | ||
393 | static void | |
394 | h_svc_conf_read_end(void *dummy) | |
395 | { | |
396 | mark_services(); | |
397 | } | |
398 | ||
399 | static void | |
400 | mark_services(void) | |
401 | { | |
402 | struct Client *target_p; | |
403 | rb_dlink_node *ptr; | |
404 | ||
405 | RB_DLINK_FOREACH(ptr, service_list.head) | |
406 | { | |
407 | target_p = find_server(NULL, (const char *)ptr->data); | |
408 | ||
409 | if (target_p) | |
410 | target_p->flags |= FLAGS_SERVICE; | |
411 | } | |
412 | } |