]>
jfr.im git - irc/quakenet/newserv.git/blob - nterface/nterfacer_relay.c
3 Copyright (C) 2004-2005 Chris Porter.
6 - made it return an error instead of a timeout when sending to local users
8 - found a load of stuff on froo's box
12 - made sure stats buffer was checked (no security problem, just helps pauline)
16 - found \r bug, and another format string problem...
18 - oops, fixed quit bug
19 - oops, forgot about rehash hook
21 - added S support, though it's gross
23 - fixed audit found format string vunerability
25 - split O username and password from .h file into config file
26 - added rehash support
28 - item->schedule was not unset and checked upon timeout
35 #include "../lib/irc_string.h"
36 #include "../lib/strlfunc.h"
37 #include "../core/schedule.h"
38 #include "../irc/irc.h"
39 #include "../core/config.h"
41 #include "nterfacer_relay.h"
43 struct service_node
*node
;
44 sstring
*ousername
, *opassword
;
48 char nickprefix
[NICKLEN
- 5 + 1];
50 int stats_handler(struct rline
*ri
, int argc
, char **argv
);
56 node
= register_service("R");
59 register_handler(node
, "relay", 4, relay_handler
);
60 register_handler(node
, "stats", 2, stats_handler
);
62 registerhook(HOOK_NICK_LOSTNICK
, &relay_quits
);
63 registerhook(HOOK_IRC_DISCON
, &relay_disconnect
);
64 registerhook(HOOK_CORE_REHASH
, &relay_rehash
);
69 int load_config(void) {
70 /* eww, I don't know how to make this any nicer! */
71 sstring
*ou
, *op
, *on
, *opr
;
74 memset(&newregex
, 0, sizeof(newregex
));
76 opr
= getcopyconfigitem("nterfacer", "nickprefix", DEFAULT_NICK_PREFIX
, sizeof(nickprefix
) - 1); /* the terminator is included in nickprefix */
77 strlcpy(nickprefix
, (opr
&&opr
->content
)?opr
->content
:DEFAULT_NICK_PREFIX
, sizeof(nickprefix
));
78 freesstring(opr
); /* works fine with NULL */
80 ou
= getcopyconfigitem("nterfacer", "serviceusername", "nterfacer", 100);
81 op
= getcopyconfigitem("nterfacer", "servicepassword", "setme", 100);
82 on
= getcopyconfigitem("nterfacer", "servicenickname", "^(O|S)$", 100);
87 newregex
.phrase
= pcre_compile(on
->content
, PCRE_CASELESS
, &rerror
, &erroroffset
, NULL
);
89 newregex
.hint
= pcre_study(newregex
.phrase
, 0, &rerror
);
91 pcre_free(newregex
.phrase
);
92 newregex
.phrase
= NULL
;
97 if(!ou
|| !op
|| !on
|| !newregex
.phrase
) {
101 if(newregex
.phrase
) {
102 pcre_free(newregex
.phrase
);
104 pcre_free(newregex
.hint
);
112 freesstring(ousername
);
114 freesstring(opassword
);
115 if(onickname
.phrase
) {
116 pcre_free(onickname
.phrase
);
118 pcre_free(onickname
.hint
);
126 memcpy(&onickname
, &newregex
, sizeof(onickname
));
130 void relay_rehash(int hook
, void *args
) {
141 freesstring(ousername
);
143 freesstring(opassword
);
144 if(onickname
.phrase
) {
145 pcre_free(onickname
.phrase
);
147 pcre_free(onickname
.hint
);
150 deregisterhook(HOOK_NICK_LOSTNICK
, &relay_quits
);
151 deregisterhook(HOOK_IRC_DISCON
, &relay_disconnect
);
152 deregisterhook(HOOK_CORE_REHASH
, &relay_rehash
);
156 ri_error(np
->rline
, RELAY_UNLOADED
, "Module was unloaded");
163 deregister_service(node
);
166 nick
*relay_getnick(void) {
167 static char ournick
[NICKLEN
+ 1];
171 snprintf(ournick
, sizeof(ournick
), "%s%d", nickprefix
, number
++);
175 if(!getnickbynick(ournick
))
176 return registerlocaluser(ournick
, "nterf", "cer", "nterfacer relay", "nterfacer", UMODE_SERVICE
| UMODE_DEAF
| UMODE_OPER
| UMODE_INV
| UMODE_ACCOUNT
, &relay_messages
);
179 } while(attempts
> 0);
184 int relay_handler(struct rline
*ri
, int argc
, char **argv
) {
192 return ri_error(ri
, RELAY_NOT_ON_IRC
, "Not currently on IRC");
194 if(argv
[0][0] == '1') {
195 lines
= positive_atoi(argv
[1]);
197 return ri_error(ri
, RELAY_PARSE_ERROR
, "Parse error");
198 } else if(argv
[0][0] != '2') {
199 return ri_error(ri
, RELAY_PARSE_ERROR
, "Parse error");
202 dest
= getnickbynick(argv
[2]);
204 return ri_error(ri
, RELAY_NICK_NOT_FOUND
, "Nickname not found!");
206 if(homeserver(dest
->numeric
) == mylongnum
)
207 return ri_error(ri
, RELAY_LOCAL_USER
, "Cannot relay to local users");
210 if(strchr(argv
[i
], '\r'))
211 return ri_error(ri
, RELAY_INVALID_CHARS
, "Invalid character in input");
213 np
= malloc(sizeof(struct rld
));
214 MemCheckR(np
, ri_error(ri
, RELAY_MEMORY_ERROR
, "Memory error"));
218 np
->termination
.pcre
.phrase
= pcre_compile(argv
[1], PCRE_FLAGS
, &rerror
, &erroroffset
, NULL
);
219 if(!np
->termination
.pcre
.phrase
) {
222 return ri_error(ri
, RELAY_REGEX_ERROR
, "Regex compliation error");
224 np
->termination
.pcre
.hint
= pcre_study(np
->termination
.pcre
.phrase
, 0, &rerror
);
226 pcre_free(np
->termination
.pcre
.phrase
);
228 return ri_error(ri
, RELAY_REGEX_HINT_ERROR
, "Regex hint error");
233 np
->mode
= MODE_LINES
;
234 np
->termination
.remaining_lines
= lines
;
237 if(!(np
->nick
= relay_getnick())) {
239 pcre_free(np
->termination
.pcre
.phrase
);
240 if(np
->termination
.pcre
.hint
)
241 pcre_free(np
->termination
.pcre
.hint
);
244 return ri_error(ri
, RELAY_NICK_ERROR
, "Unable to get a nickname!");
247 np
->schedule
= scheduleoneshot(time(NULL
) + MESSAGE_TIMEOUT
, &relay_timeout
, np
);
251 pcre_free(np
->termination
.pcre
.phrase
);
252 if(np
->termination
.pcre
.hint
)
253 pcre_free(np
->termination
.pcre
.hint
);
255 deregisterlocaluser(np
->nick
, NULL
);
257 return ri_error(ri
, RELAY_MEMORY_ERROR
, "Memory error");
265 if(pcre_exec(onickname
.phrase
, onickname
.hint
, dest
->nick
, strlen(dest
->nick
), 0, 0, NULL
, 0) >= 0) {
267 sendsecuremessagetouser(np
->nick
, dest
, serverlist
[dest
->numeric
>>18].name
->content
, "AUTH %s %s", ousername
->content
, opassword
->content
);
271 sendmessagetouser(np
->nick
, dest
, "%s", argv
[i
]);
276 int stats_handler(struct rline
*ri
, int argc
, char **argv
) {
278 char *server
= argv
[0], *command
= argv
[1], *more
;
279 int serverid
= findserver(server
);
280 char targetnumeric
[3];
283 return ri_error(ri
, RELAY_SERVER_NOT_FOUND
, "Server not found");
285 if(!command
|| !command
[0] || (command
[0] == ' ') || (command
[0] == '\r') || (command
[0] == ':'))
286 return ri_error(ri
, RELAY_INVALID_COMMAND
, "Invalid command");
290 if(strchr(more
, '\r'))
291 return ri_error(ri
, RELAY_INVALID_CHARS
, "Invalid character in input");
296 np
= malloc(sizeof(struct rld
));
297 MemCheckR(np
, ri_error(ri
, RELAY_MEMORY_ERROR
, "Memory error"));
300 np
->mode
= MODE_STATS
;
303 if(!(np
->nick
= relay_getnick())) {
305 return ri_error(ri
, RELAY_NICK_ERROR
, "Unable to get a nickname!");
308 np
->schedule
= scheduleoneshot(time(NULL
) + MESSAGE_TIMEOUT
, &relay_timeout
, np
);
311 deregisterlocaluser(np
->nick
, NULL
);
318 memcpy(targetnumeric
, longtonumeric(serverid
, 2), 3);
319 targetnumeric
[2] = '\0';
321 irc_send("%s R %c %s :%s\r\n", longtonumeric(np
->nick
->numeric
,5), command
[0], targetnumeric
, more
);
323 irc_send("%s R %c :%s\r\n", longtonumeric(np
->nick
->numeric
,5), command
[0], targetnumeric
);
329 void relay_messages(nick
*target
, int messagetype
, void **args
) {
330 struct rld
*item
, *prev
= NULL
;
331 for(item
=list
;item
;prev
=item
,item
=item
->next
)
332 if(item
->nick
== target
)
338 switch(messagetype
) {
342 if(!item
->dest
|| (item
->dest
!= (nick
*)args
[0]))
345 if((item
->mode
& MODE_IS_O
) && !(item
->mode
& MODE_O_AUTH2
)) {
346 if(item
->mode
& MODE_O_AUTH1
) {
347 if(!strncmp((char *)args
[1], "Your authlevel is ", 18)) {
348 item
->mode
|=MODE_O_AUTH2
;
350 ri_error(item
->rline
, RELAY_O_AUTH_ERROR
, "Unable to auth with O");
351 dispose_rld_prev(item
, prev
);
354 if(!strcmp((char *)args
[1], "Auth successful.")) {
355 item
->mode
|=MODE_O_AUTH1
;
357 ri_error(item
->rline
, RELAY_O_AUTH_ERROR
, "Unable to auth with O");
358 dispose_rld_prev(item
, prev
);
361 } else if(ri_append(item
->rline
, "%s", (char *)args
[1]) == BF_OVER
) {
362 ri_error(item
->rline
, BF_OVER
, "Buffer overflow");
363 dispose_rld_prev(item
, prev
);
365 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))) {
366 ri_final(item
->rline
);
367 dispose_rld_prev(item
, prev
);
373 ri_error(item
->rline
, RELAY_KILLED
, "Killed");
375 prev
->next
= item
->next
;
379 dispose_rld_dontquit(item
, 1);
383 if(item
->mode
!= MODE_STATS
)
385 if(ri_append(item
->rline
, "%s", (char *)args
[2]) == BF_OVER
) {
386 ri_error(item
->rline
, BF_OVER
, "Buffer overflow");
387 dispose_rld_prev(item
, prev
);
392 if(item
->mode
!= MODE_STATS
)
394 ri_final(item
->rline
);
395 dispose_rld_prev(item
, prev
);
400 void dispose_rld_prev(struct rld
*item
, struct rld
*prev
) {
402 prev
->next
= item
->next
;
409 void dispose_rld_dontquit(struct rld
*item
, int dontquit
) {
411 deleteschedule(item
->schedule
, &relay_timeout
, item
);
413 deregisterlocaluser(item
->nick
, NULL
);
414 if(item
->mode
& MODE_TAG
) {
415 pcre_free(item
->termination
.pcre
.phrase
);
416 if(item
->termination
.pcre
.hint
)
417 pcre_free(item
->termination
.pcre
.hint
);
422 void relay_timeout(void *arg
) {
423 struct rld
*item
= (struct rld
*)arg
, *lp
= NULL
, *np
;
425 item
->schedule
= NULL
;
427 ri_error(item
->rline
, RELAY_TIMEOUT
, "Timed out");
429 for(np
=list
;np
;lp
=np
,np
=np
->next
) {
431 dispose_rld_prev(item
, lp
);
437 void relay_quits(int hook
, void *args
) {
438 nick
*np
= (nick
*)args
;
441 for(lp
=NULL
,cp
=list
;cp
;) {
443 ri_error(cp
->rline
, RELAY_TARGET_LEFT
, "Target left QuakeNet");
460 void relay_disconnect(int hook
, void *args
) {
461 struct rld
*np
, *lp
= NULL
;
463 ri_error(list
->rline
, RELAY_DISCONNECTED
, "Disconnected from IRC");
464 dispose_rld_prev(list
, NULL
);