]>
jfr.im git - irc/quakenet/newserv.git/blob - nterface/nterfacer_relay.c
3 Copyright (C) 2004-2005 Chris Porter.
8 - made sure stats buffer was checked (no security problem, just helps pauline)
12 - found \r bug, and another format string problem...
14 - oops, fixed quit bug
15 - oops, forgot about rehash hook
17 - added S support, though it's gross
19 - fixed audit found format string vunerability
21 - split O username and password from .h file into config file
22 - added rehash support
24 - item->schedule was not unset and checked upon timeout
31 #include "../lib/irc_string.h"
32 #include "../lib/strlfunc.h"
33 #include "../core/schedule.h"
34 #include "../irc/irc.h"
35 #include "../core/config.h"
37 #include "nterfacer_relay.h"
39 struct service_node
*node
;
40 sstring
*ousername
, *opassword
;
44 char nickprefix
[NICKLEN
- 5 + 1];
46 int stats_handler(struct rline
*ri
, int argc
, char **argv
);
52 node
= register_service("R");
55 register_handler(node
, "relay", 4, relay_handler
);
56 register_handler(node
, "stats", 2, stats_handler
);
58 registerhook(HOOK_NICK_QUIT
, &relay_quits
);
59 registerhook(HOOK_IRC_DISCON
, &relay_disconnect
);
60 registerhook(HOOK_CORE_REHASH
, &relay_rehash
);
65 int load_config(void) {
66 /* eww, I don't know how to make this any nicer! */
67 sstring
*ou
, *op
, *on
, *opr
;
70 memset(&newregex
, 0, sizeof(newregex
));
72 opr
= getcopyconfigitem("nterfacer", "nickprefix", DEFAULT_NICK_PREFIX
, sizeof(nickprefix
) - 1); /* the terminator is included in nickprefix */
73 strlcpy(nickprefix
, (opr
&&opr
->content
)?opr
->content
:DEFAULT_NICK_PREFIX
, sizeof(nickprefix
));
74 freesstring(opr
); /* works fine with NULL */
76 ou
= getcopyconfigitem("nterfacer", "serviceusername", "nterfacer", 100);
77 op
= getcopyconfigitem("nterfacer", "servicepassword", "setme", 100);
78 on
= getcopyconfigitem("nterfacer", "servicenickname", "^(O|S)$", 100);
83 newregex
.phrase
= pcre_compile(on
->content
, PCRE_CASELESS
, &rerror
, &erroroffset
, NULL
);
85 newregex
.hint
= pcre_study(newregex
.phrase
, 0, &rerror
);
87 pcre_free(newregex
.phrase
);
88 newregex
.phrase
= NULL
;
93 if(!ou
|| !op
|| !on
|| !newregex
.phrase
) {
98 pcre_free(newregex
.phrase
);
100 pcre_free(newregex
.hint
);
108 freesstring(ousername
);
110 freesstring(opassword
);
111 if(onickname
.phrase
) {
112 pcre_free(onickname
.phrase
);
114 pcre_free(onickname
.hint
);
122 memcpy(&onickname
, &newregex
, sizeof(onickname
));
126 void relay_rehash(int hook
, void *args
) {
137 freesstring(ousername
);
139 freesstring(opassword
);
140 if(onickname
.phrase
) {
141 pcre_free(onickname
.phrase
);
143 pcre_free(onickname
.hint
);
146 deregisterhook(HOOK_NICK_QUIT
, &relay_quits
);
147 deregisterhook(HOOK_IRC_DISCON
, &relay_disconnect
);
148 deregisterhook(HOOK_CORE_REHASH
, &relay_rehash
);
152 ri_error(np
->rline
, RELAY_UNLOADED
, "Module was unloaded");
159 deregister_service(node
);
162 nick
*relay_getnick(void) {
163 static char ournick
[NICKLEN
+ 1];
167 snprintf(ournick
, sizeof(ournick
), "%s%d", nickprefix
, number
++);
171 if(!getnickbynick(ournick
))
172 return registerlocaluser(ournick
, "nterf", "cer", "nterfacer relay", "nterfacer", UMODE_SERVICE
| UMODE_DEAF
| UMODE_OPER
| UMODE_INV
| UMODE_ACCOUNT
, &relay_messages
);
175 } while(attempts
> 0);
180 int relay_handler(struct rline
*ri
, int argc
, char **argv
) {
188 return ri_error(ri
, RELAY_NOT_ON_IRC
, "Not currently on IRC");
190 if(argv
[0][0] == '1') {
191 lines
= positive_atoi(argv
[1]);
193 return ri_error(ri
, RELAY_PARSE_ERROR
, "Parse error");
194 } else if(argv
[0][0] != '2') {
195 return ri_error(ri
, RELAY_PARSE_ERROR
, "Parse error");
198 dest
= getnickbynick(argv
[2]);
200 return ri_error(ri
, RELAY_NICK_NOT_FOUND
, "Nickname not found!");
203 if(strchr(argv
[i
], '\r'))
204 return ri_error(ri
, RELAY_INVALID_CHARS
, "Invalid character in input");
206 np
= malloc(sizeof(struct rld
));
207 MemCheckR(np
, ri_error(ri
, RELAY_MEMORY_ERROR
, "Memory error"));
211 np
->termination
.pcre
.phrase
= pcre_compile(argv
[1], PCRE_FLAGS
, &rerror
, &erroroffset
, NULL
);
212 if(!np
->termination
.pcre
.phrase
) {
215 return ri_error(ri
, RELAY_REGEX_ERROR
, "Regex compliation error");
217 np
->termination
.pcre
.hint
= pcre_study(np
->termination
.pcre
.phrase
, 0, &rerror
);
219 pcre_free(np
->termination
.pcre
.phrase
);
221 return ri_error(ri
, RELAY_REGEX_HINT_ERROR
, "Regex hint error");
226 np
->mode
= MODE_LINES
;
227 np
->termination
.remaining_lines
= lines
;
230 if(!(np
->nick
= relay_getnick())) {
232 pcre_free(np
->termination
.pcre
.phrase
);
233 if(np
->termination
.pcre
.hint
)
234 pcre_free(np
->termination
.pcre
.hint
);
237 return ri_error(ri
, RELAY_NICK_ERROR
, "Unable to get a nickname!");
240 np
->schedule
= scheduleoneshot(time(NULL
) + MESSAGE_TIMEOUT
, &relay_timeout
, np
);
244 pcre_free(np
->termination
.pcre
.phrase
);
245 if(np
->termination
.pcre
.hint
)
246 pcre_free(np
->termination
.pcre
.hint
);
248 deregisterlocaluser(np
->nick
, NULL
);
250 return ri_error(ri
, RELAY_MEMORY_ERROR
, "Memory error");
258 if(pcre_exec(onickname
.phrase
, onickname
.hint
, dest
->nick
, strlen(dest
->nick
), 0, 0, NULL
, 0) >= 0) {
260 sendsecuremessagetouser(np
->nick
, dest
, serverlist
[dest
->numeric
>>18].name
->content
, "AUTH %s %s", ousername
->content
, opassword
->content
);
264 sendmessagetouser(np
->nick
, dest
, "%s", argv
[i
]);
269 int stats_handler(struct rline
*ri
, int argc
, char **argv
) {
271 char *server
= argv
[0], *command
= argv
[1], *more
;
272 int serverid
= findserver(server
);
273 char targetnumeric
[3];
276 return ri_error(ri
, RELAY_SERVER_NOT_FOUND
, "Server not found");
278 if(!command
|| !command
[0] || (command
[0] == ' ') || (command
[0] == '\r') || (command
[0] == ':'))
279 return ri_error(ri
, RELAY_INVALID_COMMAND
, "Invalid command");
283 if(strchr(more
, '\r'))
284 return ri_error(ri
, RELAY_INVALID_CHARS
, "Invalid character in input");
289 np
= malloc(sizeof(struct rld
));
290 MemCheckR(np
, ri_error(ri
, RELAY_MEMORY_ERROR
, "Memory error"));
293 np
->mode
= MODE_STATS
;
296 if(!(np
->nick
= relay_getnick())) {
298 return ri_error(ri
, RELAY_NICK_ERROR
, "Unable to get a nickname!");
301 np
->schedule
= scheduleoneshot(time(NULL
) + MESSAGE_TIMEOUT
, &relay_timeout
, np
);
304 deregisterlocaluser(np
->nick
, NULL
);
311 memcpy(targetnumeric
, longtonumeric(serverid
, 2), 3);
312 targetnumeric
[2] = '\0';
314 irc_send("%s R %c %s :%s\r\n", longtonumeric(np
->nick
->numeric
,5), command
[0], targetnumeric
, more
);
316 irc_send("%s R %c :%s\r\n", longtonumeric(np
->nick
->numeric
,5), command
[0], targetnumeric
);
322 void relay_messages(nick
*target
, int messagetype
, void **args
) {
323 struct rld
*item
, *prev
= NULL
;
324 for(item
=list
;item
;prev
=item
,item
=item
->next
)
325 if(item
->nick
== target
)
331 switch(messagetype
) {
335 if(!item
->dest
|| (item
->dest
!= (nick
*)args
[0]))
338 if((item
->mode
& MODE_IS_O
) && !(item
->mode
& MODE_O_AUTH2
)) {
339 if(item
->mode
& MODE_O_AUTH1
) {
340 if(!strncmp((char *)args
[1], "Your authlevel is ", 18)) {
341 item
->mode
|=MODE_O_AUTH2
;
343 ri_error(item
->rline
, RELAY_O_AUTH_ERROR
, "Unable to auth with O");
344 dispose_rld_prev(item
, prev
);
347 if(!strcmp((char *)args
[1], "Auth successful.")) {
348 item
->mode
|=MODE_O_AUTH1
;
350 ri_error(item
->rline
, RELAY_O_AUTH_ERROR
, "Unable to auth with O");
351 dispose_rld_prev(item
, prev
);
354 } else if(ri_append(item
->rline
, "%s", (char *)args
[1]) == BF_OVER
) {
355 ri_error(item
->rline
, BF_OVER
, "Buffer overflow");
356 dispose_rld_prev(item
, prev
);
358 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))) {
359 ri_final(item
->rline
);
360 dispose_rld_prev(item
, prev
);
366 ri_error(item
->rline
, RELAY_KILLED
, "Killed");
368 prev
->next
= item
->next
;
372 dispose_rld_dontquit(item
, 1);
376 if(item
->mode
!= MODE_STATS
)
378 if(ri_append(item
->rline
, "%s", (char *)args
[2]) == BF_OVER
) {
379 ri_error(item
->rline
, BF_OVER
, "Buffer overflow");
380 dispose_rld_prev(item
, prev
);
385 if(item
->mode
!= MODE_STATS
)
387 ri_final(item
->rline
);
388 dispose_rld_prev(item
, prev
);
393 void dispose_rld_prev(struct rld
*item
, struct rld
*prev
) {
395 prev
->next
= item
->next
;
402 void dispose_rld_dontquit(struct rld
*item
, int dontquit
) {
404 deleteschedule(item
->schedule
, &relay_timeout
, item
);
406 deregisterlocaluser(item
->nick
, NULL
);
407 if(item
->mode
& MODE_TAG
) {
408 pcre_free(item
->termination
.pcre
.phrase
);
409 if(item
->termination
.pcre
.hint
)
410 pcre_free(item
->termination
.pcre
.hint
);
415 void relay_timeout(void *arg
) {
416 struct rld
*item
= (struct rld
*)arg
, *lp
= NULL
, *np
;
418 item
->schedule
= NULL
;
420 ri_error(item
->rline
, RELAY_TIMEOUT
, "Timed out");
422 for(np
=list
;np
;lp
=np
,np
=np
->next
) {
424 dispose_rld_prev(item
, lp
);
430 void relay_quits(int hook
, void *args
) {
431 void **vargs
= (void **)args
;
432 nick
*np
= (nick
*)vargs
[0];
435 for(lp
=NULL
,cp
=list
;cp
;) {
437 ri_error(cp
->rline
, RELAY_TARGET_LEFT
, "Target left QuakeNet");
454 void relay_disconnect(int hook
, void *args
) {
455 struct rld
*np
, *lp
= NULL
;
457 ri_error(list
->rline
, RELAY_DISCONNECTED
, "Disconnected from IRC");
458 dispose_rld_prev(list
, NULL
);