]>
jfr.im git - irc/quakenet/newserv.git/blob - nterface/nterfacer_relay.c
3 Copyright (C) 2004-2005 Chris Porter.
6 - made sure stats buffer was checked (no security problem, just helps pauline)
10 - found \r bug, and another format string problem...
12 - oops, fixed quit bug
13 - oops, forgot about rehash hook
15 - added S support, though it's gross
17 - fixed audit found format string vunerability
19 - split O username and password from .h file into config file
20 - added rehash support
22 - item->schedule was not unset and checked upon timeout
29 #include "../lib/irc_string.h"
30 #include "../core/schedule.h"
31 #include "../irc/irc.h"
32 #include "../core/config.h"
34 #include "nterfacer_relay.h"
36 struct service_node
*node
;
37 sstring
*ousername
, *opassword
;
42 int stats_handler(struct rline
*ri
, int argc
, char **argv
);
48 node
= register_service("R");
51 register_handler(node
, "relay", 4, relay_handler
);
52 register_handler(node
, "stats", 2, stats_handler
);
54 registerhook(HOOK_NICK_QUIT
, &relay_quits
);
55 registerhook(HOOK_IRC_DISCON
, &relay_disconnect
);
56 registerhook(HOOK_CORE_REHASH
, &relay_rehash
);
61 int load_config(void) {
62 /* eww, I don't know how to make this any nicer! */
63 sstring
*ou
, *op
, *on
;
66 memset(&newregex
, 0, sizeof(newregex
));
68 ou
= getcopyconfigitem("nterfacer", "serviceusername", "nterfacer", 100);
69 op
= getcopyconfigitem("nterfacer", "servicepassword", "setme", 100);
70 on
= getcopyconfigitem("nterfacer", "servicenickname", "^(O|S)$", 100);
75 newregex
.phrase
= pcre_compile(on
->content
, PCRE_CASELESS
, &rerror
, &erroroffset
, NULL
);
77 newregex
.hint
= pcre_study(newregex
.phrase
, 0, &rerror
);
79 pcre_free(newregex
.phrase
);
80 newregex
.phrase
= NULL
;
85 if(!ou
|| !op
|| !on
|| !newregex
.phrase
) {
93 pcre_free(newregex
.phrase
);
95 pcre_free(newregex
.hint
);
103 freesstring(ousername
);
105 freesstring(opassword
);
106 if(onickname
.phrase
) {
107 pcre_free(onickname
.phrase
);
109 pcre_free(onickname
.hint
);
117 memcpy(&onickname
, &newregex
, sizeof(onickname
));
121 void relay_rehash(int hook
, void *args
) {
132 freesstring(ousername
);
134 freesstring(opassword
);
135 if(onickname
.phrase
) {
136 pcre_free(onickname
.phrase
);
138 pcre_free(onickname
.hint
);
141 deregisterhook(HOOK_NICK_QUIT
, &relay_quits
);
142 deregisterhook(HOOK_IRC_DISCON
, &relay_disconnect
);
143 deregisterhook(HOOK_CORE_REHASH
, &relay_rehash
);
147 ri_error(np
->rline
, RELAY_UNLOADED
, "Module was unloaded");
154 deregister_service(node
);
157 nick
*relay_getnick(void) {
158 static char ournick
[NICKLEN
+ 1];
162 snprintf(ournick
, sizeof(ournick
), "nterfacer%d", number
++);
166 if(!getnickbynick(ournick
))
167 return registerlocaluser(ournick
, "nterf", "cer", "nterfacer relay", "nterfacer", UMODE_SERVICE
| UMODE_DEAF
| UMODE_OPER
| UMODE_INV
| UMODE_ACCOUNT
, &relay_messages
);
170 } while(attempts
> 0);
175 int relay_handler(struct rline
*ri
, int argc
, char **argv
) {
183 return ri_error(ri
, RELAY_NOT_ON_IRC
, "Not currently on IRC");
185 if(argv
[0][0] == '1') {
186 lines
= positive_atoi(argv
[1]);
188 return ri_error(ri
, RELAY_PARSE_ERROR
, "Parse error");
189 } else if(argv
[0][0] != '2') {
190 return ri_error(ri
, RELAY_PARSE_ERROR
, "Parse error");
193 dest
= getnickbynick(argv
[2]);
195 return ri_error(ri
, RELAY_NICK_NOT_FOUND
, "Nickname not found!");
198 if(strchr(argv
[i
], '\r'))
199 return ri_error(ri
, RELAY_INVALID_CHARS
, "Invalid character in input");
201 np
= malloc(sizeof(struct rld
));
202 MemCheckR(np
, ri_error(ri
, RELAY_MEMORY_ERROR
, "Memory error"));
206 np
->termination
.pcre
.phrase
= pcre_compile(argv
[1], PCRE_FLAGS
, &rerror
, &erroroffset
, NULL
);
207 if(!np
->termination
.pcre
.phrase
) {
210 return ri_error(ri
, RELAY_REGEX_ERROR
, "Regex compliation error");
212 np
->termination
.pcre
.hint
= pcre_study(np
->termination
.pcre
.phrase
, 0, &rerror
);
214 pcre_free(np
->termination
.pcre
.phrase
);
216 return ri_error(ri
, RELAY_REGEX_HINT_ERROR
, "Regex hint error");
221 np
->mode
= MODE_LINES
;
222 np
->termination
.remaining_lines
= lines
;
225 if(!(np
->nick
= relay_getnick())) {
227 pcre_free(np
->termination
.pcre
.phrase
);
228 if(np
->termination
.pcre
.hint
)
229 pcre_free(np
->termination
.pcre
.hint
);
232 return ri_error(ri
, RELAY_NICK_ERROR
, "Unable to get a nickname!");
235 np
->schedule
= scheduleoneshot(time(NULL
) + MESSAGE_TIMEOUT
, &relay_timeout
, np
);
239 pcre_free(np
->termination
.pcre
.phrase
);
240 if(np
->termination
.pcre
.hint
)
241 pcre_free(np
->termination
.pcre
.hint
);
243 deregisterlocaluser(np
->nick
, NULL
);
245 return ri_error(ri
, RELAY_MEMORY_ERROR
, "Memory error");
253 if(pcre_exec(onickname
.phrase
, onickname
.hint
, dest
->nick
, strlen(dest
->nick
), 0, 0, NULL
, 0) >= 0) {
255 sendsecuremessagetouser(np
->nick
, dest
, serverlist
[dest
->numeric
>>18].name
->content
, "AUTH %s %s", ousername
->content
, opassword
->content
);
259 sendmessagetouser(np
->nick
, dest
, "%s", argv
[i
]);
264 int stats_handler(struct rline
*ri
, int argc
, char **argv
) {
266 char *server
= argv
[0], *command
= argv
[1], *more
;
267 int serverid
= findserver(server
);
268 char targetnumeric
[3];
271 return ri_error(ri
, RELAY_SERVER_NOT_FOUND
, "Server not found");
273 if(!command
|| !command
[0] || (command
[0] == ' ') || (command
[0] == '\r') || (command
[0] == ':'))
274 return ri_error(ri
, RELAY_INVALID_COMMAND
, "Invalid command");
278 if(strchr(more
, '\r'))
279 return ri_error(ri
, RELAY_INVALID_CHARS
, "Invalid character in input");
284 np
= malloc(sizeof(struct rld
));
285 MemCheckR(np
, ri_error(ri
, RELAY_MEMORY_ERROR
, "Memory error"));
288 np
->mode
= MODE_STATS
;
291 if(!(np
->nick
= relay_getnick())) {
293 return ri_error(ri
, RELAY_NICK_ERROR
, "Unable to get a nickname!");
296 np
->schedule
= scheduleoneshot(time(NULL
) + MESSAGE_TIMEOUT
, &relay_timeout
, np
);
299 deregisterlocaluser(np
->nick
, NULL
);
306 memcpy(targetnumeric
, longtonumeric(serverid
, 2), 3);
307 targetnumeric
[2] = '\0';
309 irc_send("%s R %c %s :%s\r\n", longtonumeric(np
->nick
->numeric
,5), command
[0], targetnumeric
, more
);
311 irc_send("%s R %c :%s\r\n", longtonumeric(np
->nick
->numeric
,5), command
[0], targetnumeric
);
317 void relay_messages(nick
*target
, int messagetype
, void **args
) {
318 struct rld
*item
, *prev
= NULL
;
319 for(item
=list
;item
;prev
=item
,item
=item
->next
)
320 if(item
->nick
== target
)
326 switch(messagetype
) {
330 if(!item
->dest
|| (item
->dest
!= (nick
*)args
[0]))
333 if((item
->mode
& MODE_IS_O
) && !(item
->mode
& MODE_O_AUTH2
)) {
334 if(item
->mode
& MODE_O_AUTH1
) {
335 if(!strncmp((char *)args
[1], "Your authlevel is ", 18)) {
336 item
->mode
|=MODE_O_AUTH2
;
338 ri_error(item
->rline
, RELAY_O_AUTH_ERROR
, "Unable to auth with O");
339 dispose_rld_prev(item
, prev
);
342 if(!strcmp((char *)args
[1], "Auth successful.")) {
343 item
->mode
|=MODE_O_AUTH1
;
345 ri_error(item
->rline
, RELAY_O_AUTH_ERROR
, "Unable to auth with O");
346 dispose_rld_prev(item
, prev
);
349 } else if(ri_append(item
->rline
, "%s", (char *)args
[1]) == BF_OVER
) {
350 ri_error(item
->rline
, BF_OVER
, "Buffer overflow");
351 dispose_rld_prev(item
, prev
);
353 if(((item
->mode
& MODE_LINES
) && (!--item
->termination
.remaining_lines
)) || ((item
->mode
& MODE_TAG
) && (pcre_exec(item
->termination
.pcre
.phrase
, item
->termination
.pcre
.hint
, (char *)args
[1], strlen((char *)args
[1]), 0, 0, NULL
, 0) >= 0))) {
354 ri_final(item
->rline
);
355 dispose_rld_prev(item
, prev
);
361 ri_error(item
->rline
, RELAY_KILLED
, "Killed");
363 prev
->next
= item
->next
;
367 dispose_rld_dontquit(item
, 1);
371 if(item
->mode
!= MODE_STATS
)
373 if(ri_append(item
->rline
, "%s", (char *)args
[2]) == BF_OVER
) {
374 ri_error(item
->rline
, BF_OVER
, "Buffer overflow");
375 dispose_rld_prev(item
, prev
);
380 if(item
->mode
!= MODE_STATS
)
382 ri_final(item
->rline
);
383 dispose_rld_prev(item
, prev
);
388 void dispose_rld_prev(struct rld
*item
, struct rld
*prev
) {
390 prev
->next
= item
->next
;
397 void dispose_rld_dontquit(struct rld
*item
, int dontquit
) {
399 deleteschedule(item
->schedule
, &relay_timeout
, item
);
401 deregisterlocaluser(item
->nick
, NULL
);
402 if(item
->mode
& MODE_TAG
) {
403 pcre_free(item
->termination
.pcre
.phrase
);
404 if(item
->termination
.pcre
.hint
)
405 pcre_free(item
->termination
.pcre
.hint
);
410 void relay_timeout(void *arg
) {
411 struct rld
*item
= (struct rld
*)arg
, *lp
= NULL
, *np
;
413 item
->schedule
= NULL
;
415 ri_error(item
->rline
, RELAY_TIMEOUT
, "Timed out");
417 for(np
=list
;np
;lp
=np
,np
=np
->next
) {
419 dispose_rld_prev(item
, lp
);
425 void relay_quits(int hook
, void *args
) {
426 void **vargs
= (void **)args
;
427 nick
*np
= (nick
*)vargs
[0];
430 for(lp
=NULL
,cp
=list
;cp
;) {
432 ri_error(cp
->rline
, RELAY_TARGET_LEFT
, "Target left QuakeNet");
449 void relay_disconnect(int hook
, void *args
) {
450 struct rld
*np
, *lp
= NULL
;
452 ri_error(list
->rline
, RELAY_DISCONNECTED
, "Disconnected from IRC");
453 dispose_rld_prev(list
, NULL
);