#include "../core/config.h"
#include "../core/events.h"
#include "../lib/version.h"
+#include "../core/schedule.h"
#include "nterfacer.h"
#include "logging.h"
int permit_count = 0;
int ping_handler(struct rline *ri, int argc, char **argv);
+static void nterfacer_sendcallback(struct rline *ri, int error, char *buf);
void _init(void) {
int loaded;
nrl = nterface_open_log("nterfacer", "logs/nterfacer.log", debug_mode);
loaded = load_permits();
- if(!loaded) {
- nterface_log(nrl, NL_ERROR, "No permits loaded successfully.");
+ nterface_log(nrl, NL_INFO, "Loaded %d permit%s successfully.", loaded, loaded==1?"":"s");
+
+ if(!loaded)
return;
- } else {
- nterface_log(nrl, NL_INFO, "Loaded %d permit%s successfully.", loaded, loaded==1?"":"s");
- }
nterfacer_events.on_accept = nterfacer_accept_event;
nterfacer_events.on_line = nterfacer_line_event;
if(li->socket) {
esocket_write_line(li->socket, "%d,OE%d,%s", li->id, BF_UNLOADED, "Service was unloaded.");
} else if(li->callback) {
- li->callback(BF_UNLOADED, "Service was unloaded.", li->tag);
+ nterfacer_sendcallback(li, BF_UNLOADED, "Service was unloaded.");
}
if(pi) {
pi->next = li->next;
hostnamesa = getconfigitems("nterfacer", "hostname");
passwordsa = getconfigitems("nterfacer", "password");
- if(!hostnamesa || !passwordsa) {
- nterface_log(nrl, NL_ERROR, "Unable to load hostnames/passwords.");
+ if(!hostnamesa || !passwordsa)
return 0;
- }
if(hostnamesa->cursi != passwordsa->cursi) {
nterface_log(nrl, NL_ERROR, "Different number of hostnames/passwords in config file.");
return 0;
continue;
}
- item->ihost = (*(struct in_addr *)host->h_addr).s_addr;
+ item->ihost = (*(struct in_addr *)host->h_addr_list[0]).s_addr;
for(j=0;j<loaded_lines;j++) {
if(new_permits[j].ihost == item->ihost) {
nterface_log(nrl, NL_WARNING, "Host with items %d and %d is identical, dropping item %d.", j + 1, i + 1, i + 1);
if(argcount) {
parsebuf = (char *)ntmalloc(strlen(pp) + 1);
MemCheckR(parsebuf, RE_MEM_ERROR);
- newp = parsebuf;
for(newp=args[0]=parsebuf,pp++;*pp;pp++) {
if((*pp == '\\') && *(pp + 1)) {
if(error_code == 0) /* :P */
error_code = -10000;
- li->callback(error_code, escapedbuf, li->tag);
+ nterfacer_sendcallback(li, error_code, escapedbuf);
}
}
if(esocket_write_line(li->socket, "%d,OO%s", li->id, li->buf))
retval = RE_SOCKET_ERROR;
} else if(li->callback) {
- li->callback(0, li->buf, li->tag);
+ nterfacer_sendcallback(li, 0, li->buf);
}
for(pp=rlines;pp;lp=pp,pp=pp->next) {
return ri_final(ri);
}
+struct sched_rline {
+ rline rl;
+ int argc;
+ handler *hl;
+ void *schedule;
+ char argv[];
+};
+
+static const int XMAXARGS = 50;
+
+static void execrline(void *arg) {
+ struct sched_rline *sr = arg;
+ int re, i;
+ char *argv[XMAXARGS], *buf;
+
+ sr->schedule = NULL;
+
+ buf = sr->argv;
+ for(i=0;i<sr->argc;i++) {
+ argv[i] = buf;
+ buf+=strlen(buf) + 1;
+ }
+
+ re = (sr->hl->function)(&sr->rl, sr->argc, argv);
+
+ if(re)
+ Error("nterfacer", ERR_WARNING, "sendline: error occured calling %p %d: %s", sr->hl->function, re, request_error(re));
+}
+
void *nterfacer_sendline(char *service, char *command, int argc, char **argv, rline_callback callback, void *tag) {
struct service_node *servicep;
struct rline *prequest;
+ struct sched_rline *sr;
struct handler *hl;
- int re;
+ int totallen, i;
+ char *buf;
for(servicep=tree;servicep;servicep=servicep->next)
if(!strcmp(servicep->name->content, service))
break;
+ if(argc > XMAXARGS)
+ Error("nterfacer", ERR_STOP, "Over maximum arguments.");
+
if(!servicep) {
Error("nterfacer", ERR_WARNING, "sendline: service not found: %s", service);
return NULL;
return NULL;
}
- prequest = (struct rline *)ntmalloc(sizeof(struct rline));
- if(!prequest) {
+ /* we have to create a copy of the arguments for reentrancy reasons, grr */
+ totallen = 0;
+ for(i=0;i<argc;i++)
+ totallen+=strlen(argv[i]) + 1;
+
+ /* HACKY but allows existing code to still work */
+ sr = (struct sched_rline *)ntmalloc(sizeof(struct sched_rline) + totallen);
+ if(!sr) {
MemError();
return NULL;
}
+ prequest = &sr->rl;
+
+ sr->argc = argc;
+ buf = sr->argv;
+ for(i=0;i<argc;i++) {
+ size_t len = strlen(argv[i]) + 1;
+ memcpy(buf, argv[i], len);
+ buf+=len;
+ }
+ sr->hl = hl;
prequest->service = servicep;
prequest->handler = hl;
prequest->curpos = prequest->buf;
prequest->tag = tag;
prequest->id = 0;
- prequest->next = rlines;
prequest->socket = NULL;
prequest->callback = callback;
+ prequest->next = rlines;
rlines = prequest;
- re = (hl->function)(prequest, argc, argv);
- if(re) {
- Error("nterfacer", ERR_WARNING, "sendline: error occured calling %s %d: %s", command, re, request_error(re));
- return NULL;
- }
+ scheduleoneshot(time(NULL), execrline, sr);
- return (void *)prequest;
+ return (void *)sr;
}
void nterfacer_freeline(void *tag) {
- struct rline *prequest = tag;
+ struct sched_rline *prequest = tag;
- prequest->callback = NULL;
+ prequest->rl.callback = NULL;
+ if(prequest->schedule)
+ deleteschedule(prequest->schedule, execrline, NULL);
+}
+
+#define MAX_LINES 8192
+
+/* this bites */
+static void nterfacer_sendcallback(struct rline *ri, int error, char *buf) {
+ char *lines[MAX_LINES+1];
+ char newbuf[MAX_BUFSIZE+5];
+ char *s, *d, *laststart;
+ int linec = 0;
+
+ for(s=buf,laststart=d=newbuf;*s;s++) {
+ if((*s == '\\') && *(s + 1)) {
+ if(*(s + 1) == ',') {
+ *d++ = ',';
+ } else if(*(s + 1) == '\\') {
+ *d++ = '\\';
+ }
+ s++;
+ } else if(*s == ',') {
+ *d++ = '\0';
+ if(linec >= MAX_LINES - 5) {
+ nterfacer_sendcallback(ri, BF_OVER, "Buffer overflow.");
+ return;
+ }
+
+ lines[linec++] = laststart;
+ laststart = d;
+ } else {
+ *d++ = *s;
+ }
+ }
+ *d = '\0';
+ lines[linec++] = laststart;
+
+ ri->callback(error, linec, lines, ri->tag);
}