]>
jfr.im git - irc/quakenet/newserv.git/blob - nterfacer/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"
40 #include "../lib/version.h"
42 #include "nterfacer_relay.h"
46 struct service_node
*node
;
47 sstring
*ousername
, *opassword
;
51 char nickprefix
[NICKLEN
- 5 + 1];
52 static unsigned long userid
;
54 int stats_handler(struct rline
*ri
, int argc
, char **argv
);
60 node
= register_service("R");
63 register_handler(node
, "relay", 4, relay_handler
);
64 register_handler(node
, "stats", 2, stats_handler
);
66 registerhook(HOOK_NICK_LOSTNICK
, &relay_quits
);
67 registerhook(HOOK_IRC_DISCON
, &relay_disconnect
);
68 registerhook(HOOK_CORE_REHASH
, &relay_rehash
);
73 int load_config(void) {
74 /* eww, I don't know how to make this any nicer! */
75 sstring
*ou
, *op
, *on
, *opr
;
78 memset(&newregex
, 0, sizeof(newregex
));
80 userid
= getcopyconfigitemintpositive("nterfacer", "serviceuserid", 0);
82 Error("nterfacer_relay", ERR_WARNING
, "No user id specified (serviceuserid)!");
86 opr
= getcopyconfigitem("nterfacer", "nickprefix", DEFAULT_NICK_PREFIX
, sizeof(nickprefix
) - 1); /* the terminator is included in nickprefix */
87 strlcpy(nickprefix
, (opr
&&opr
->content
)?opr
->content
:DEFAULT_NICK_PREFIX
, sizeof(nickprefix
));
88 freesstring(opr
); /* works fine with NULL */
90 ou
= getcopyconfigitem("nterfacer", "serviceusername", "nterfacer", 100);
91 op
= getcopyconfigitem("nterfacer", "servicepassword", "setme", 100);
92 on
= getcopyconfigitem("nterfacer", "servicenickname", "^(O|S)$", 100);
97 newregex
.phrase
= pcre_compile(on
->content
, PCRE_CASELESS
, &rerror
, &erroroffset
, NULL
);
99 newregex
.hint
= pcre_study(newregex
.phrase
, 0, &rerror
);
101 pcre_free(newregex
.phrase
);
102 newregex
.phrase
= NULL
;
107 if(!ou
|| !op
|| !on
|| !newregex
.phrase
) {
111 if(newregex
.phrase
) {
112 pcre_free(newregex
.phrase
);
114 pcre_free(newregex
.hint
);
122 freesstring(ousername
);
124 freesstring(opassword
);
125 if(onickname
.phrase
) {
126 pcre_free(onickname
.phrase
);
128 pcre_free(onickname
.hint
);
136 memcpy(&onickname
, &newregex
, sizeof(onickname
));
140 void relay_rehash(int hook
, void *args
) {
151 freesstring(ousername
);
153 freesstring(opassword
);
154 if(onickname
.phrase
) {
155 pcre_free(onickname
.phrase
);
157 pcre_free(onickname
.hint
);
160 deregisterhook(HOOK_NICK_LOSTNICK
, &relay_quits
);
161 deregisterhook(HOOK_IRC_DISCON
, &relay_disconnect
);
162 deregisterhook(HOOK_CORE_REHASH
, &relay_rehash
);
166 ri_error(np
->rline
, RELAY_UNLOADED
, "Module was unloaded");
173 deregister_service(node
);
176 nick
*relay_getnick(void) {
177 static char ournick
[NICKLEN
+ 1];
181 snprintf(ournick
, sizeof(ournick
), "%s%d", nickprefix
, number
++);
185 if(!getnickbynick(ournick
))
186 return registerlocaluserwithuserid(ournick
, "nterf", "cer", "nterfacer relay", "nterfacer", userid
, UMODE_SERVICE
| UMODE_DEAF
| UMODE_OPER
| UMODE_INV
| UMODE_ACCOUNT
, &relay_messages
);
189 } while(attempts
> 0);
194 int relay_handler(struct rline
*ri
, int argc
, char **argv
) {
202 return ri_error(ri
, RELAY_NOT_ON_IRC
, "Not currently on IRC");
204 if(argv
[0][0] == '1') {
205 lines
= positive_atoi(argv
[1]);
207 return ri_error(ri
, RELAY_PARSE_ERROR
, "Parse error");
208 } else if(argv
[0][0] != '2') {
209 return ri_error(ri
, RELAY_PARSE_ERROR
, "Parse error");
212 dest
= getnickbynick(argv
[2]);
214 return ri_error(ri
, RELAY_NICK_NOT_FOUND
, "Nickname not found!");
216 if(homeserver(dest
->numeric
) == mylongnum
)
217 return ri_error(ri
, RELAY_LOCAL_USER
, "Cannot relay to local users");
220 if(strchr(argv
[i
], '\r'))
221 return ri_error(ri
, RELAY_INVALID_CHARS
, "Invalid character in input");
223 np
= ntmalloc(sizeof(struct rld
));
224 MemCheckR(np
, ri_error(ri
, RELAY_MEMORY_ERROR
, "Memory error"));
228 np
->termination
.pcre
.phrase
= pcre_compile(argv
[1], PCRE_FLAGS
, &rerror
, &erroroffset
, NULL
);
229 if(!np
->termination
.pcre
.phrase
) {
232 return ri_error(ri
, RELAY_REGEX_ERROR
, "Regex compliation error");
234 np
->termination
.pcre
.hint
= pcre_study(np
->termination
.pcre
.phrase
, 0, &rerror
);
236 pcre_free(np
->termination
.pcre
.phrase
);
238 return ri_error(ri
, RELAY_REGEX_HINT_ERROR
, "Regex hint error");
243 np
->mode
= MODE_LINES
;
244 np
->termination
.remaining_lines
= lines
;
247 if(!(np
->nick
= relay_getnick())) {
249 pcre_free(np
->termination
.pcre
.phrase
);
250 if(np
->termination
.pcre
.hint
)
251 pcre_free(np
->termination
.pcre
.hint
);
254 return ri_error(ri
, RELAY_NICK_ERROR
, "Unable to get a nickname!");
257 np
->schedule
= scheduleoneshot(time(NULL
) + MESSAGE_TIMEOUT
, &relay_timeout
, np
);
261 pcre_free(np
->termination
.pcre
.phrase
);
262 if(np
->termination
.pcre
.hint
)
263 pcre_free(np
->termination
.pcre
.hint
);
265 deregisterlocaluser(np
->nick
, NULL
);
267 return ri_error(ri
, RELAY_MEMORY_ERROR
, "Memory error");
275 if(pcre_exec(onickname
.phrase
, onickname
.hint
, dest
->nick
, strlen(dest
->nick
), 0, 0, NULL
, 0) >= 0) {
277 sendsecuremessagetouser(np
->nick
, dest
, serverlist
[dest
->numeric
>>18].name
->content
, "AUTH %s %s", ousername
->content
, opassword
->content
);
281 sendmessagetouser(np
->nick
, dest
, "%s", argv
[i
]);
286 int stats_handler(struct rline
*ri
, int argc
, char **argv
) {
288 char *server
= argv
[0], *command
= argv
[1], *more
;
289 int serverid
= findserver(server
);
290 char targetnumeric
[3];
293 return ri_error(ri
, RELAY_SERVER_NOT_FOUND
, "Server not found");
295 if(!command
|| !command
[0] || (command
[0] == ' ') || (command
[0] == '\r') || (command
[0] == ':'))
296 return ri_error(ri
, RELAY_INVALID_COMMAND
, "Invalid command");
300 if(strchr(more
, '\r'))
301 return ri_error(ri
, RELAY_INVALID_CHARS
, "Invalid character in input");
306 np
= ntmalloc(sizeof(struct rld
));
307 MemCheckR(np
, ri_error(ri
, RELAY_MEMORY_ERROR
, "Memory error"));
310 np
->mode
= MODE_STATS
;
313 if(!(np
->nick
= relay_getnick())) {
315 return ri_error(ri
, RELAY_NICK_ERROR
, "Unable to get a nickname!");
318 np
->schedule
= scheduleoneshot(time(NULL
) + MESSAGE_TIMEOUT
, &relay_timeout
, np
);
321 deregisterlocaluser(np
->nick
, NULL
);
328 memcpy(targetnumeric
, longtonumeric(serverid
, 2), 3);
329 targetnumeric
[2] = '\0';
331 irc_send("%s R %c %s :%s\r\n", longtonumeric(np
->nick
->numeric
,5), command
[0], targetnumeric
, more
);
333 irc_send("%s R %c :%s\r\n", longtonumeric(np
->nick
->numeric
,5), command
[0], targetnumeric
);
339 void relay_messages(nick
*target
, int messagetype
, void **args
) {
340 struct rld
*item
, *prev
= NULL
;
341 for(item
=list
;item
;prev
=item
,item
=item
->next
)
342 if(item
->nick
== target
)
348 switch(messagetype
) {
352 if(!item
->dest
|| (item
->dest
!= (nick
*)args
[0]))
355 if((item
->mode
& MODE_IS_O
) && !(item
->mode
& MODE_O_AUTH2
)) {
356 if(item
->mode
& MODE_O_AUTH1
) {
357 if(!strncmp((char *)args
[1], "Your authlevel is ", 18)) {
358 item
->mode
|=MODE_O_AUTH2
;
360 ri_error(item
->rline
, RELAY_O_AUTH_ERROR
, "Unable to auth with O");
361 dispose_rld_prev(item
, prev
);
364 if(!strcmp((char *)args
[1], "Auth successful.")) {
365 item
->mode
|=MODE_O_AUTH1
;
367 ri_error(item
->rline
, RELAY_O_AUTH_ERROR
, "Unable to auth with O");
368 dispose_rld_prev(item
, prev
);
371 } else if(ri_append(item
->rline
, "%s", (char *)args
[1]) == BF_OVER
) {
372 ri_error(item
->rline
, BF_OVER
, "Buffer overflow");
373 dispose_rld_prev(item
, prev
);
375 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))) {
376 ri_final(item
->rline
);
377 dispose_rld_prev(item
, prev
);
383 ri_error(item
->rline
, RELAY_KILLED
, "Killed");
385 prev
->next
= item
->next
;
389 dispose_rld_dontquit(item
, 1);
393 if(item
->mode
!= MODE_STATS
)
395 if(ri_append(item
->rline
, "%s", (char *)args
[2]) == BF_OVER
) {
396 ri_error(item
->rline
, BF_OVER
, "Buffer overflow");
397 dispose_rld_prev(item
, prev
);
402 if(item
->mode
!= MODE_STATS
)
404 ri_final(item
->rline
);
405 dispose_rld_prev(item
, prev
);
410 void dispose_rld_prev(struct rld
*item
, struct rld
*prev
) {
412 prev
->next
= item
->next
;
419 void dispose_rld_dontquit(struct rld
*item
, int dontquit
) {
421 deleteschedule(item
->schedule
, &relay_timeout
, item
);
423 deregisterlocaluser(item
->nick
, NULL
);
424 if(item
->mode
& MODE_TAG
) {
425 pcre_free(item
->termination
.pcre
.phrase
);
426 if(item
->termination
.pcre
.hint
)
427 pcre_free(item
->termination
.pcre
.hint
);
432 void relay_timeout(void *arg
) {
433 struct rld
*item
= (struct rld
*)arg
, *lp
= NULL
, *np
;
435 item
->schedule
= NULL
;
437 ri_error(item
->rline
, RELAY_TIMEOUT
, "Timed out");
439 for(np
=list
;np
;lp
=np
,np
=np
->next
) {
441 dispose_rld_prev(item
, lp
);
447 void relay_quits(int hook
, void *args
) {
448 nick
*np
= (nick
*)args
;
451 for(lp
=NULL
,cp
=list
;cp
;) {
453 ri_error(cp
->rline
, RELAY_TARGET_LEFT
, "Target left QuakeNet");
470 void relay_disconnect(int hook
, void *args
) {
471 struct rld
*np
, *lp
= NULL
;
473 ri_error(list
->rline
, RELAY_DISCONNECTED
, "Disconnected from IRC");
474 dispose_rld_prev(list
, NULL
);