]>
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. | |
212380e3 AC |
28 | */ |
29 | ||
30 | #include "stdinc.h" | |
31 | ||
212380e3 AC |
32 | #include "send.h" |
33 | #include "channel.h" | |
34 | #include "client.h" | |
35 | #include "common.h" | |
36 | #include "config.h" | |
37 | #include "ircd.h" | |
38 | #include "numeric.h" | |
212380e3 AC |
39 | #include "s_conf.h" |
40 | #include "s_newconf.h" | |
41 | #include "s_serv.h" | |
42 | #include "hash.h" | |
43 | #include "msg.h" | |
44 | #include "parse.h" | |
45 | #include "modules.h" | |
212380e3 AC |
46 | #include "whowas.h" |
47 | #include "monitor.h" | |
0b904d91 | 48 | #include "supported.h" |
212380e3 | 49 | |
eeabf33a EM |
50 | static const char services_desc[] = "Provides support for running a services daemon"; |
51 | ||
c46a4ecd KB |
52 | static int _modinit(void); |
53 | static void _moddeinit(void); | |
54 | ||
55 | static void mark_services(void); | |
56 | static void unmark_services(void); | |
57 | ||
3c7d6fcc EM |
58 | static void me_su(struct MsgBuf *, struct Client *, struct Client *, int, const char **); |
59 | static void me_login(struct MsgBuf *, struct Client *, struct Client *, int, const char **); | |
60 | static void me_rsfnc(struct MsgBuf *, struct Client *, struct Client *, int, const char **); | |
61 | static void me_nickdelay(struct MsgBuf *, struct Client *, struct Client *, int, const char **); | |
212380e3 AC |
62 | |
63 | static void h_svc_server_introduced(hook_data_client *); | |
64 | static void h_svc_whois(hook_data_client *); | |
65 | static void h_svc_stats(hook_data_int *); | |
c46a4ecd KB |
66 | static void h_svc_conf_read_start(void *); |
67 | static void h_svc_conf_read_end(void *); | |
212380e3 AC |
68 | |
69 | struct Message su_msgtab = { | |
7baa37a9 | 70 | "SU", 0, 0, 0, 0, |
212380e3 AC |
71 | {mg_ignore, mg_ignore, mg_ignore, mg_ignore, {me_su, 2}, mg_ignore} |
72 | }; | |
73 | struct Message login_msgtab = { | |
7baa37a9 | 74 | "LOGIN", 0, 0, 0, 0, |
212380e3 AC |
75 | {mg_ignore, mg_ignore, mg_ignore, mg_ignore, {me_login, 2}, mg_ignore} |
76 | }; | |
77 | struct Message rsfnc_msgtab = { | |
7baa37a9 | 78 | "RSFNC", 0, 0, 0, 0, |
212380e3 AC |
79 | {mg_ignore, mg_ignore, mg_ignore, mg_ignore, {me_rsfnc, 4}, mg_ignore} |
80 | }; | |
81 | struct Message nickdelay_msgtab = { | |
7baa37a9 | 82 | "NICKDELAY", 0, 0, 0, 0, |
212380e3 AC |
83 | {mg_unreg, mg_ignore, mg_ignore, mg_ignore, {me_nickdelay, 3}, mg_ignore} |
84 | }; | |
85 | ||
55abcbb2 | 86 | mapi_clist_av1 services_clist[] = { |
212380e3 AC |
87 | &su_msgtab, &login_msgtab, &rsfnc_msgtab, &nickdelay_msgtab, NULL |
88 | }; | |
89 | mapi_hfn_list_av1 services_hfnlist[] = { | |
90 | { "doing_stats", (hookfn) h_svc_stats }, | |
91 | { "doing_whois", (hookfn) h_svc_whois }, | |
92 | { "doing_whois_global", (hookfn) h_svc_whois }, | |
93 | { "server_introduced", (hookfn) h_svc_server_introduced }, | |
c46a4ecd KB |
94 | { "conf_read_start", (hookfn) h_svc_conf_read_start }, |
95 | { "conf_read_end", (hookfn) h_svc_conf_read_end }, | |
212380e3 AC |
96 | { NULL, NULL } |
97 | }; | |
98 | ||
3abc337f | 99 | DECLARE_MODULE_AV2(services, _modinit, _moddeinit, services_clist, NULL, services_hfnlist, NULL, NULL, services_desc); |
c46a4ecd KB |
100 | |
101 | static int | |
102 | _modinit(void) | |
103 | { | |
104 | mark_services(); | |
0b904d91 | 105 | add_isupport("FNC", isupport_string, ""); |
8a4b8377 | 106 | return 0; |
c46a4ecd KB |
107 | } |
108 | ||
109 | static void | |
110 | _moddeinit(void) | |
111 | { | |
0b904d91 | 112 | delete_isupport("FNC"); |
c46a4ecd KB |
113 | unmark_services(); |
114 | } | |
212380e3 | 115 | |
3c7d6fcc | 116 | static void |
428ca87b | 117 | me_su(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, |
212380e3 AC |
118 | int parc, const char *parv[]) |
119 | { | |
120 | struct Client *target_p; | |
121 | ||
122 | if(!(source_p->flags & FLAGS_SERVICE)) | |
ec57fe67 KB |
123 | { |
124 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
125 | "Non-service server %s attempting to execute services-only command SU", source_p->name); | |
3c7d6fcc | 126 | return; |
ec57fe67 | 127 | } |
212380e3 AC |
128 | |
129 | if((target_p = find_client(parv[1])) == NULL) | |
3c7d6fcc | 130 | return; |
212380e3 AC |
131 | |
132 | if(!target_p->user) | |
3c7d6fcc | 133 | return; |
212380e3 AC |
134 | |
135 | if(EmptyString(parv[2])) | |
136 | target_p->user->suser[0] = '\0'; | |
137 | else | |
f427c8b0 | 138 | rb_strlcpy(target_p->user->suser, parv[2], sizeof(target_p->user->suser)); |
212380e3 | 139 | |
583f064f | 140 | sendto_common_channels_local_butone(target_p, CLICAP_ACCOUNT_NOTIFY, NOCAPS, ":%s!%s@%s ACCOUNT %s", |
7a7f86d3 AC |
141 | target_p->name, target_p->username, target_p->host, |
142 | EmptyString(target_p->user->suser) ? "*" : target_p->user->suser); | |
143 | ||
212380e3 | 144 | invalidate_bancache_user(target_p); |
212380e3 AC |
145 | } |
146 | ||
3c7d6fcc | 147 | static void |
428ca87b | 148 | me_login(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, |
212380e3 AC |
149 | int parc, const char *parv[]) |
150 | { | |
151 | if(!IsPerson(source_p)) | |
3c7d6fcc | 152 | return; |
212380e3 | 153 | |
f427c8b0 | 154 | rb_strlcpy(source_p->user->suser, parv[1], sizeof(source_p->user->suser)); |
212380e3 AC |
155 | } |
156 | ||
3c7d6fcc | 157 | static void |
428ca87b | 158 | me_rsfnc(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, |
212380e3 AC |
159 | int parc, const char *parv[]) |
160 | { | |
161 | struct Client *target_p; | |
162 | struct Client *exist_p; | |
163 | time_t newts, curts; | |
75d60088 | 164 | char note[NICKLEN + 10]; |
212380e3 AC |
165 | |
166 | if(!(source_p->flags & FLAGS_SERVICE)) | |
ec57fe67 KB |
167 | { |
168 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
169 | "Non-service server %s attempting to execute services-only command RSFNC", source_p->name); | |
3c7d6fcc | 170 | return; |
ec57fe67 | 171 | } |
212380e3 AC |
172 | |
173 | if((target_p = find_person(parv[1])) == NULL) | |
3c7d6fcc | 174 | return; |
212380e3 AC |
175 | |
176 | if(!MyClient(target_p)) | |
3c7d6fcc | 177 | return; |
212380e3 | 178 | |
2d28539c | 179 | if(!clean_nick(parv[2], 0) || IsDigit(parv[2][0])) |
3c7d6fcc | 180 | return; |
212380e3 AC |
181 | |
182 | curts = atol(parv[4]); | |
183 | ||
184 | /* if tsinfo is different from what it was when services issued the | |
185 | * RSFNC, then we ignore it. This can happen when a client changes | |
186 | * nicknames before the RSFNC arrives.. --anfl | |
187 | */ | |
188 | if(target_p->tsinfo != curts) | |
3c7d6fcc | 189 | return; |
212380e3 AC |
190 | |
191 | if((exist_p = find_named_client(parv[2]))) | |
192 | { | |
193 | char buf[BUFSIZE]; | |
194 | ||
195 | /* this would be one hell of a race condition to trigger | |
55abcbb2 | 196 | * this one given the tsinfo check above, but its here for |
212380e3 AC |
197 | * safety --anfl |
198 | */ | |
199 | if(target_p == exist_p) | |
03510227 | 200 | goto doit; |
212380e3 AC |
201 | |
202 | if(MyClient(exist_p)) | |
203 | sendto_one(exist_p, ":%s KILL %s :(Nickname regained by services)", | |
204 | me.name, exist_p->name); | |
205 | ||
206 | exist_p->flags |= FLAGS_KILLED; | |
207 | /* Do not send kills to servers for unknowns -- jilles */ | |
208 | if(IsClient(exist_p)) | |
45ed8835 | 209 | { |
212380e3 AC |
210 | kill_client_serv_butone(NULL, exist_p, "%s (Nickname regained by services)", |
211 | me.name); | |
45ed8835 JT |
212 | sendto_realops_snomask(SNO_SKILL, L_ALL, |
213 | "Nick collision due to services forced nick change on %s", | |
214 | parv[2]); | |
215 | } | |
212380e3 | 216 | |
5203cba5 | 217 | snprintf(buf, sizeof(buf), "Killed (%s (Nickname regained by services))", |
212380e3 AC |
218 | me.name); |
219 | exit_client(NULL, exist_p, &me, buf); | |
220 | } | |
221 | ||
03510227 | 222 | doit: |
212380e3 AC |
223 | newts = atol(parv[3]); |
224 | ||
225 | /* timestamp is older than 15mins, ignore it */ | |
e3354945 VY |
226 | if(newts < (rb_current_time() - 900)) |
227 | newts = rb_current_time() - 900; | |
212380e3 AC |
228 | |
229 | target_p->tsinfo = newts; | |
230 | ||
231 | monitor_signoff(target_p); | |
232 | ||
233 | invalidate_bancache_user(target_p); | |
234 | ||
235 | sendto_realops_snomask(SNO_NCHANGE, L_ALL, | |
236 | "Nick change: From %s to %s [%s@%s]", | |
237 | target_p->name, parv[2], target_p->username, | |
238 | target_p->host); | |
239 | ||
583f064f | 240 | sendto_common_channels_local(target_p, NOCAPS, NOCAPS, ":%s!%s@%s NICK :%s", |
212380e3 AC |
241 | target_p->name, target_p->username, |
242 | target_p->host, parv[2]); | |
243 | ||
b47f8a4f | 244 | whowas_add_history(target_p, 1); |
212380e3 AC |
245 | sendto_server(NULL, NULL, CAP_TS6, NOCAPS, ":%s NICK %s :%ld", |
246 | use_id(target_p), parv[2], (long) target_p->tsinfo); | |
212380e3 AC |
247 | |
248 | del_from_client_hash(target_p->name, target_p); | |
e2606551 | 249 | rb_strlcpy(target_p->name, parv[2], NICKLEN); |
212380e3 AC |
250 | add_to_client_hash(target_p->name, target_p); |
251 | ||
252 | monitor_signon(target_p); | |
253 | ||
254 | del_all_accepts(target_p); | |
255 | ||
5203cba5 | 256 | snprintf(note, NICKLEN + 10, "Nick: %s", target_p->name); |
75d60088 | 257 | rb_note(target_p->localClient->F, note); |
212380e3 AC |
258 | } |
259 | ||
260 | /* | |
261 | ** me_nickdelay | |
212380e3 AC |
262 | ** parv[1] = duration in seconds (0 to remove) |
263 | ** parv[2] = nick | |
264 | */ | |
3c7d6fcc | 265 | static void |
428ca87b | 266 | me_nickdelay(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[]) |
212380e3 AC |
267 | { |
268 | int duration; | |
269 | struct nd_entry *nd; | |
270 | ||
271 | if(!(source_p->flags & FLAGS_SERVICE)) | |
ec57fe67 KB |
272 | { |
273 | sendto_realops_snomask(SNO_GENERAL, L_ALL, | |
274 | "Non-service server %s attempting to execute services-only command NICKDELAY", source_p->name); | |
3c7d6fcc | 275 | return; |
ec57fe67 | 276 | } |
212380e3 AC |
277 | |
278 | duration = atoi(parv[1]); | |
279 | if (duration <= 0) | |
280 | { | |
a4bf26dd | 281 | nd = rb_dictionary_retrieve(nd_dict, parv[2]); |
212380e3 AC |
282 | if (nd != NULL) |
283 | free_nd_entry(nd); | |
284 | } | |
285 | else | |
286 | { | |
287 | if (duration > 86400) | |
288 | duration = 86400; | |
289 | add_nd_entry(parv[2]); | |
a4bf26dd | 290 | nd = rb_dictionary_retrieve(nd_dict, parv[2]); |
212380e3 | 291 | if (nd != NULL) |
e3354945 | 292 | nd->expire = rb_current_time() + duration; |
212380e3 | 293 | } |
212380e3 AC |
294 | } |
295 | ||
296 | static void | |
297 | h_svc_server_introduced(hook_data_client *hdata) | |
298 | { | |
5b96d9a6 | 299 | rb_dlink_node *ptr; |
212380e3 | 300 | |
5b96d9a6 | 301 | RB_DLINK_FOREACH(ptr, service_list.head) |
212380e3 AC |
302 | { |
303 | if(!irccmp((const char *) ptr->data, hdata->target->name)) | |
304 | { | |
305 | hdata->target->flags |= FLAGS_SERVICE; | |
306 | return; | |
307 | } | |
308 | } | |
309 | } | |
310 | ||
311 | static void | |
312 | h_svc_whois(hook_data_client *data) | |
313 | { | |
314 | char *p = data->target->user->suser; | |
315 | if(!EmptyString(p)) | |
316 | { | |
317 | /* Try to strip off any leading digits as this may be used to | |
318 | * store both an ID number and an account name in one field. | |
319 | * If only digits are present, leave as is. | |
320 | */ | |
321 | while(IsDigit(*p)) | |
322 | p++; | |
323 | if(*p == '\0') | |
324 | p = data->target->user->suser; | |
325 | ||
5b383ce0 JT |
326 | sendto_one_numeric(data->client, RPL_WHOISLOGGEDIN, |
327 | form_str(RPL_WHOISLOGGEDIN), | |
212380e3 AC |
328 | data->target->name, p); |
329 | } | |
330 | } | |
331 | ||
332 | static void | |
333 | h_svc_stats(hook_data_int *data) | |
334 | { | |
335 | char statchar = (char) data->arg2; | |
5b96d9a6 | 336 | rb_dlink_node *ptr; |
212380e3 AC |
337 | |
338 | if (statchar == 'U' && IsOper(data->client)) | |
339 | { | |
5b96d9a6 | 340 | RB_DLINK_FOREACH(ptr, service_list.head) |
212380e3 AC |
341 | { |
342 | sendto_one_numeric(data->client, RPL_STATSULINE, | |
343 | form_str(RPL_STATSULINE), | |
0cce01d3 | 344 | (const char *)ptr->data, "*", "*", "s"); |
212380e3 AC |
345 | } |
346 | } | |
347 | } | |
c46a4ecd KB |
348 | |
349 | static void | |
350 | h_svc_conf_read_start(void *dummy) | |
351 | { | |
352 | unmark_services(); | |
353 | } | |
354 | ||
355 | static void | |
356 | unmark_services(void) | |
357 | { | |
358 | struct Client *target_p; | |
359 | rb_dlink_node *ptr; | |
360 | ||
361 | RB_DLINK_FOREACH(ptr, global_serv_list.head) | |
362 | { | |
363 | target_p = ptr->data; | |
364 | ||
365 | target_p->flags &= ~FLAGS_SERVICE; | |
366 | } | |
367 | } | |
368 | ||
369 | static void | |
370 | h_svc_conf_read_end(void *dummy) | |
371 | { | |
372 | mark_services(); | |
373 | } | |
374 | ||
375 | static void | |
376 | mark_services(void) | |
377 | { | |
378 | struct Client *target_p; | |
379 | rb_dlink_node *ptr; | |
380 | ||
381 | RB_DLINK_FOREACH(ptr, service_list.head) | |
382 | { | |
383 | target_p = find_server(NULL, (const char *)ptr->data); | |
384 | ||
385 | if (target_p) | |
386 | target_p->flags |= FLAGS_SERVICE; | |
387 | } | |
388 | } |