]>
jfr.im git - irc/quakenet/newserv.git/blob - nterface/nterfacer_relay.c
653c019c87b7e4cbbde6e6f9e248d61663b6d5c0
3 Copyright (C) 2004-2005 Chris Porter.
6 - found a load of stuff on froo's box
10 - made sure stats buffer was checked (no security problem, just helps pauline)
14 - found \r bug, and another format string problem...
16 - oops, fixed quit bug
17 - oops, forgot about rehash hook
19 - added S support, though it's gross
21 - fixed audit found format string vunerability
23 - split O username and password from .h file into config file
24 - added rehash support
26 - item->schedule was not unset and checked upon timeout
33 #include "../lib/irc_string.h"
34 #include "../lib/strlfunc.h"
35 #include "../core/schedule.h"
36 #include "../irc/irc.h"
37 #include "../core/config.h"
39 #include "nterfacer_relay.h"
41 struct service_node
*node
;
42 sstring
*ousername
, *opassword
;
46 char nickprefix
[NICKLEN
- 5 + 1];
48 int stats_handler(struct rline
*ri
, int argc
, char **argv
);
54 node
= register_service("R");
57 register_handler(node
, "relay", 4, relay_handler
);
58 register_handler(node
, "stats", 2, stats_handler
);
60 registerhook(HOOK_NICK_LOSTNICK
, &relay_quits
);
61 registerhook(HOOK_IRC_DISCON
, &relay_disconnect
);
62 registerhook(HOOK_CORE_REHASH
, &relay_rehash
);
67 int load_config(void) {
68 /* eww, I don't know how to make this any nicer! */
69 sstring
*ou
, *op
, *on
, *opr
;
72 memset(&newregex
, 0, sizeof(newregex
));
74 opr
= getcopyconfigitem("nterfacer", "nickprefix", DEFAULT_NICK_PREFIX
, sizeof(nickprefix
) - 1); /* the terminator is included in nickprefix */
75 strlcpy(nickprefix
, (opr
&&opr
->content
)?opr
->content
:DEFAULT_NICK_PREFIX
, sizeof(nickprefix
));
76 freesstring(opr
); /* works fine with NULL */
78 ou
= getcopyconfigitem("nterfacer", "serviceusername", "nterfacer", 100);
79 op
= getcopyconfigitem("nterfacer", "servicepassword", "setme", 100);
80 on
= getcopyconfigitem("nterfacer", "servicenickname", "^(O|S)$", 100);
85 newregex
.phrase
= pcre_compile(on
->content
, PCRE_CASELESS
, &rerror
, &erroroffset
, NULL
);
87 newregex
.hint
= pcre_study(newregex
.phrase
, 0, &rerror
);
89 pcre_free(newregex
.phrase
);
90 newregex
.phrase
= NULL
;
95 if(!ou
|| !op
|| !on
|| !newregex
.phrase
) {
100 pcre_free(newregex
.phrase
);
102 pcre_free(newregex
.hint
);
110 freesstring(ousername
);
112 freesstring(opassword
);
113 if(onickname
.phrase
) {
114 pcre_free(onickname
.phrase
);
116 pcre_free(onickname
.hint
);
124 memcpy(&onickname
, &newregex
, sizeof(onickname
));
128 void relay_rehash(int hook
, void *args
) {
139 freesstring(ousername
);
141 freesstring(opassword
);
142 if(onickname
.phrase
) {
143 pcre_free(onickname
.phrase
);
145 pcre_free(onickname
.hint
);
148 deregisterhook(HOOK_NICK_LOSTNICK
, &relay_quits
);
149 deregisterhook(HOOK_IRC_DISCON
, &relay_disconnect
);
150 deregisterhook(HOOK_CORE_REHASH
, &relay_rehash
);
154 ri_error(np
->rline
, RELAY_UNLOADED
, "Module was unloaded");
161 deregister_service(node
);
164 nick
*relay_getnick(void) {
165 static char ournick
[NICKLEN
+ 1];
169 snprintf(ournick
, sizeof(ournick
), "%s%d", nickprefix
, number
++);
173 if(!getnickbynick(ournick
))
174 return registerlocaluser(ournick
, "nterf", "cer", "nterfacer relay", "nterfacer", UMODE_SERVICE
| UMODE_DEAF
| UMODE_OPER
| UMODE_INV
| UMODE_ACCOUNT
, &relay_messages
);
177 } while(attempts
> 0);
182 int relay_handler(struct rline
*ri
, int argc
, char **argv
) {
190 return ri_error(ri
, RELAY_NOT_ON_IRC
, "Not currently on IRC");
192 if(argv
[0][0] == '1') {
193 lines
= positive_atoi(argv
[1]);
195 return ri_error(ri
, RELAY_PARSE_ERROR
, "Parse error");
196 } else if(argv
[0][0] != '2') {
197 return ri_error(ri
, RELAY_PARSE_ERROR
, "Parse error");
200 dest
= getnickbynick(argv
[2]);
202 return ri_error(ri
, RELAY_NICK_NOT_FOUND
, "Nickname not found!");
205 if(strchr(argv
[i
], '\r'))
206 return ri_error(ri
, RELAY_INVALID_CHARS
, "Invalid character in input");
208 np
= malloc(sizeof(struct rld
));
209 MemCheckR(np
, ri_error(ri
, RELAY_MEMORY_ERROR
, "Memory error"));
213 np
->termination
.pcre
.phrase
= pcre_compile(argv
[1], PCRE_FLAGS
, &rerror
, &erroroffset
, NULL
);
214 if(!np
->termination
.pcre
.phrase
) {
217 return ri_error(ri
, RELAY_REGEX_ERROR
, "Regex compliation error");
219 np
->termination
.pcre
.hint
= pcre_study(np
->termination
.pcre
.phrase
, 0, &rerror
);
221 pcre_free(np
->termination
.pcre
.phrase
);
223 return ri_error(ri
, RELAY_REGEX_HINT_ERROR
, "Regex hint error");
228 np
->mode
= MODE_LINES
;
229 np
->termination
.remaining_lines
= lines
;
232 if(!(np
->nick
= relay_getnick())) {
234 pcre_free(np
->termination
.pcre
.phrase
);
235 if(np
->termination
.pcre
.hint
)
236 pcre_free(np
->termination
.pcre
.hint
);
239 return ri_error(ri
, RELAY_NICK_ERROR
, "Unable to get a nickname!");
242 np
->schedule
= scheduleoneshot(time(NULL
) + MESSAGE_TIMEOUT
, &relay_timeout
, np
);
246 pcre_free(np
->termination
.pcre
.phrase
);
247 if(np
->termination
.pcre
.hint
)
248 pcre_free(np
->termination
.pcre
.hint
);
250 deregisterlocaluser(np
->nick
, NULL
);
252 return ri_error(ri
, RELAY_MEMORY_ERROR
, "Memory error");
260 if(pcre_exec(onickname
.phrase
, onickname
.hint
, dest
->nick
, strlen(dest
->nick
), 0, 0, NULL
, 0) >= 0) {
262 sendsecuremessagetouser(np
->nick
, dest
, serverlist
[dest
->numeric
>>18].name
->content
, "AUTH %s %s", ousername
->content
, opassword
->content
);
266 sendmessagetouser(np
->nick
, dest
, "%s", argv
[i
]);
271 int stats_handler(struct rline
*ri
, int argc
, char **argv
) {
273 char *server
= argv
[0], *command
= argv
[1], *more
;
274 int serverid
= findserver(server
);
275 char targetnumeric
[3];
278 return ri_error(ri
, RELAY_SERVER_NOT_FOUND
, "Server not found");
280 if(!command
|| !command
[0] || (command
[0] == ' ') || (command
[0] == '\r') || (command
[0] == ':'))
281 return ri_error(ri
, RELAY_INVALID_COMMAND
, "Invalid command");
285 if(strchr(more
, '\r'))
286 return ri_error(ri
, RELAY_INVALID_CHARS
, "Invalid character in input");
291 np
= malloc(sizeof(struct rld
));
292 MemCheckR(np
, ri_error(ri
, RELAY_MEMORY_ERROR
, "Memory error"));
295 np
->mode
= MODE_STATS
;
298 if(!(np
->nick
= relay_getnick())) {
300 return ri_error(ri
, RELAY_NICK_ERROR
, "Unable to get a nickname!");
303 np
->schedule
= scheduleoneshot(time(NULL
) + MESSAGE_TIMEOUT
, &relay_timeout
, np
);
306 deregisterlocaluser(np
->nick
, NULL
);
313 memcpy(targetnumeric
, longtonumeric(serverid
, 2), 3);
314 targetnumeric
[2] = '\0';
316 irc_send("%s R %c %s :%s\r\n", longtonumeric(np
->nick
->numeric
,5), command
[0], targetnumeric
, more
);
318 irc_send("%s R %c :%s\r\n", longtonumeric(np
->nick
->numeric
,5), command
[0], targetnumeric
);
324 void relay_messages(nick
*target
, int messagetype
, void **args
) {
325 struct rld
*item
, *prev
= NULL
;
326 for(item
=list
;item
;prev
=item
,item
=item
->next
)
327 if(item
->nick
== target
)
333 switch(messagetype
) {
337 if(!item
->dest
|| (item
->dest
!= (nick
*)args
[0]))
340 if((item
->mode
& MODE_IS_O
) && !(item
->mode
& MODE_O_AUTH2
)) {
341 if(item
->mode
& MODE_O_AUTH1
) {
342 if(!strncmp((char *)args
[1], "Your authlevel is ", 18)) {
343 item
->mode
|=MODE_O_AUTH2
;
345 ri_error(item
->rline
, RELAY_O_AUTH_ERROR
, "Unable to auth with O");
346 dispose_rld_prev(item
, prev
);
349 if(!strcmp((char *)args
[1], "Auth successful.")) {
350 item
->mode
|=MODE_O_AUTH1
;
352 ri_error(item
->rline
, RELAY_O_AUTH_ERROR
, "Unable to auth with O");
353 dispose_rld_prev(item
, prev
);
356 } else if(ri_append(item
->rline
, "%s", (char *)args
[1]) == BF_OVER
) {
357 ri_error(item
->rline
, BF_OVER
, "Buffer overflow");
358 dispose_rld_prev(item
, prev
);
360 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))) {
361 ri_final(item
->rline
);
362 dispose_rld_prev(item
, prev
);
368 ri_error(item
->rline
, RELAY_KILLED
, "Killed");
370 prev
->next
= item
->next
;
374 dispose_rld_dontquit(item
, 1);
378 if(item
->mode
!= MODE_STATS
)
380 if(ri_append(item
->rline
, "%s", (char *)args
[2]) == BF_OVER
) {
381 ri_error(item
->rline
, BF_OVER
, "Buffer overflow");
382 dispose_rld_prev(item
, prev
);
387 if(item
->mode
!= MODE_STATS
)
389 ri_final(item
->rline
);
390 dispose_rld_prev(item
, prev
);
395 void dispose_rld_prev(struct rld
*item
, struct rld
*prev
) {
397 prev
->next
= item
->next
;
404 void dispose_rld_dontquit(struct rld
*item
, int dontquit
) {
406 deleteschedule(item
->schedule
, &relay_timeout
, item
);
408 deregisterlocaluser(item
->nick
, NULL
);
409 if(item
->mode
& MODE_TAG
) {
410 pcre_free(item
->termination
.pcre
.phrase
);
411 if(item
->termination
.pcre
.hint
)
412 pcre_free(item
->termination
.pcre
.hint
);
417 void relay_timeout(void *arg
) {
418 struct rld
*item
= (struct rld
*)arg
, *lp
= NULL
, *np
;
420 item
->schedule
= NULL
;
422 ri_error(item
->rline
, RELAY_TIMEOUT
, "Timed out");
424 for(np
=list
;np
;lp
=np
,np
=np
->next
) {
426 dispose_rld_prev(item
, lp
);
432 void relay_quits(int hook
, void *args
) {
433 nick
*np
= (nick
*)args
;
436 for(lp
=NULL
,cp
=list
;cp
;) {
438 ri_error(cp
->rline
, RELAY_TARGET_LEFT
, "Target left QuakeNet");
455 void relay_disconnect(int hook
, void *args
) {
456 struct rld
*np
, *lp
= NULL
;
458 ri_error(list
->rline
, RELAY_DISCONNECTED
, "Disconnected from IRC");
459 dispose_rld_prev(list
, NULL
);