From: michael Date: Wed, 24 Dec 2014 22:12:05 +0000 (+0000) Subject: - Style corrections X-Git-Tag: 1.0.0beta1~24 X-Git-Url: http://git.serene-ircd.net/?a=commitdiff_plain;h=3800cba1c0768ecdb9f1ea8a484311d271a989c6;p=hopm.git - Style corrections git-svn-id: svn://svn.ircd-hybrid.org/svnroot/hopm/trunk@5114 82007160-df01-0410-b94d-b575c5fd34c7 --- diff --git a/src/config.c b/src/config.c index 570199b..b9452d3 100644 --- a/src/config.c +++ b/src/config.c @@ -47,8 +47,6 @@ void config_init(void); -/* Configuration */ - struct OptionsConf *OptionsItem = NULL; struct IRCConf *IRCItem = NULL; struct OpmConf *OpmItem = NULL; @@ -56,95 +54,93 @@ struct ExemptConf *ExemptItem = NULL; list_t *UserItemList = NULL; list_t *ScannerItemList = NULL; -/* End Configuration */ - - /* Rehash or load new configuration from filename, via flex/bison parser */ -void config_load(const char *filename) +void +config_load(const char *filename) { + config_init(); + config_setup(); /* Setup/clear current configuration */ - config_init(); - config_setup(); /* Setup/clear current configuration */ + log_printf("CONFIG -> Loading %s", filename); - log_printf("CONFIG -> Loading %s", filename); + if ((yyin = fopen(filename, "r")) == NULL) + { + log_printf("CONFIG -> Error opening %s", filename); + exit(1); + } - if((yyin = fopen(filename, "r")) == NULL) - { - log_printf("CONFIG -> Error opening %s", filename); - exit(1); - } + yyparse(); - yyparse(); + scan_init(); /* Initialize the scanners once we have the configuration */ + command_init(); /* Initialize the command queue */ + stats_init(); /* Initialize stats (UPTIME) */ + firedns_init(); /* Initialize adns */ - scan_init(); /* Initialize the scanners once we have the configuration */ - command_init(); /* Initialize the command queue */ - stats_init(); /* Initialize stats (UPTIME) */ - firedns_init(); /* Initialize adns */ - - fclose(yyin); + fclose(yyin); } /* Malloc and initialize configuration data to NULL */ -void config_init() +void +config_init(void) { - /* Init IRC block */ - IRCItem = MyMalloc(sizeof *IRCItem); - IRCItem->channels = list_create(); - IRCItem->performs = list_create(); + /* Init IRC block */ + IRCItem = MyMalloc(sizeof *IRCItem); + IRCItem->channels = list_create(); + IRCItem->performs = list_create(); - /* Init Options block */ - OptionsItem = MyMalloc(sizeof *OptionsItem); + /* Init Options block */ + OptionsItem = MyMalloc(sizeof *OptionsItem); - /* Init OPM block */ - OpmItem = MyMalloc(sizeof *OpmItem); - OpmItem->blacklists = list_create(); + /* Init OPM block */ + OpmItem = MyMalloc(sizeof *OpmItem); + OpmItem->blacklists = list_create(); - /* Init list of User blocks */ - UserItemList = list_create(); + /* Init list of User blocks */ + UserItemList = list_create(); - /* Init list of Scanner blocks */ - ScannerItemList = list_create(); + /* Init list of Scanner blocks */ + ScannerItemList = list_create(); - /* Init list of Exempts */ - ExemptItem = MyMalloc(sizeof *ExemptItem); - ExemptItem->masks = list_create(); + /* Init list of Exempts */ + ExemptItem = MyMalloc(sizeof *ExemptItem); + ExemptItem->masks = list_create(); } /* Setup structs that hold configuration data and then reset default values */ - -void config_setup() +void +config_setup(void) { - - /* Setup IRC Block Defaults */ - IRCItem->mode = xstrdup("+cs"); - IRCItem->nick = xstrdup("hopm"); - IRCItem->nickserv = xstrdup(""); - IRCItem->password = xstrdup(""); - IRCItem->port = 6667; - IRCItem->oper = xstrdup("undefined"); - IRCItem->username = xstrdup("hopm"); - IRCItem->realname = xstrdup("Hybrid Open Proxy Monitor"); - IRCItem->server = xstrdup("irc.example.org"); - IRCItem->vhost = xstrdup(""); - IRCItem->connregex = xstrdup("\\*\\*\\* Notice -- Client connecting: ([^ ]+) \\(([^@]+)@([^\\)]+)\\) \\[([0-9\\.]+)\\].*"); - IRCItem->kline = xstrdup("KLINE %u@%h :Open Proxy found on your host."); - - - /* Setup options block defaults */ - OptionsItem->negcache = 0; /* 0 disabled negcache */ - OptionsItem->pidfile = xstrdup("hopm.pid"); - OptionsItem->dns_fdlimit = 50; - OptionsItem->scanlog = NULL; - - /* Setup OPM block defaults */ - OpmItem->sendmail = xstrdup("/usr/sbin/sendmail"); - OpmItem->dnsbl_from = xstrdup(""); - OpmItem->dnsbl_to = xstrdup(""); + /* Setup IRC Block Defaults */ + IRCItem->mode = xstrdup("+cs"); + IRCItem->nick = xstrdup("hopm"); + IRCItem->nickserv = xstrdup(""); + IRCItem->password = xstrdup(""); + IRCItem->port = 6667; + IRCItem->oper = xstrdup("undefined"); + IRCItem->username = xstrdup("hopm"); + IRCItem->realname = xstrdup("Hybrid Open Proxy Monitor"); + IRCItem->server = xstrdup("irc.example.org"); + IRCItem->vhost = xstrdup(""); + IRCItem->connregex = xstrdup("\\*\\*\\* Notice -- Client connecting: ([^ ]+) \\(([^@]+)@([^\\)]+)\\) \\[([0-9\\.]+)\\].*"); + IRCItem->kline = xstrdup("KLINE %u@%h :Open Proxy found on your host."); + + + /* Setup options block defaults */ + OptionsItem->negcache = 0; /* 0 disabled negcache */ + OptionsItem->pidfile = xstrdup("hopm.pid"); + OptionsItem->dns_fdlimit = 50; + OptionsItem->scanlog = NULL; + + /* Setup OPM block defaults */ + OpmItem->sendmail = xstrdup("/usr/sbin/sendmail"); + OpmItem->dnsbl_from = xstrdup(""); + OpmItem->dnsbl_to = xstrdup(""); } -void yyerror(const char *str) +void +yyerror(const char *str) { - log_printf("CONFIG -> %s: line %d", str, linenum); - exit(EXIT_FAILURE); + log_printf("CONFIG -> %s: line %d", str, linenum); + exit(EXIT_FAILURE); } diff --git a/src/dnsbl.c b/src/dnsbl.c index 2fa08dc..b66e590 100644 --- a/src/dnsbl.c +++ b/src/dnsbl.c @@ -52,232 +52,233 @@ along with this program; if not, write to the Free Software /* * Work out the DNSBL zones and send the dns query */ -void dnsbl_add(struct scan_struct *ss) +void +dnsbl_add(struct scan_struct *ss) { - struct in_addr in; - unsigned char a, b, c, d; - char lookup[128]; - node_t *p; - int res; - struct dnsbl_scan *ds; - struct BlacklistConf *bl; - - if (!inet_aton(ss->ip, &in)) - { - log_printf("DNSBL -> Invalid address '%s', ignoring.", ss->ip); - return; - } - - d = (unsigned char) (in.s_addr >> 24) & 0xFF; - c = (unsigned char) (in.s_addr >> 16) & 0xFF; - b = (unsigned char) (in.s_addr >> 8) & 0xFF; - a = (unsigned char) in.s_addr & 0xFF; - - LIST_FOREACH(p, OpmItem->blacklists->head) - { - bl = p->data; + struct in_addr in; + unsigned char a, b, c, d; + char lookup[128]; + node_t *p; + int res; + struct dnsbl_scan *ds; + + + if (!inet_aton(ss->ip, &in)) + { + log_printf("DNSBL -> Invalid address '%s', ignoring.", ss->ip); + return; + } + + d = (unsigned char)(in.s_addr >> 24) & 0xFF; + c = (unsigned char)(in.s_addr >> 16) & 0xFF; + b = (unsigned char)(in.s_addr >> 8) & 0xFF; + a = (unsigned char) in.s_addr & 0xFF; + + LIST_FOREACH(p, OpmItem->blacklists->head) + { + struct BlacklistConf *bl = p->data; #ifdef WORDS_BIGENDIAN - snprintf(lookup, 128, "%d.%d.%d.%d.%s", a, b, c, d, bl->name); + snprintf(lookup, 128, "%d.%d.%d.%d.%s", a, b, c, d, bl->name); #else - snprintf(lookup, 128, "%d.%d.%d.%d.%s", d, c, b, a, bl->name); + snprintf(lookup, 128, "%d.%d.%d.%d.%s", d, c, b, a, bl->name); #endif - ds = MyMalloc(sizeof *ds); - ds->ss = ss; - ds->bl = bl; + ds = MyMalloc(sizeof *ds); + ds->ss = ss; + ds->bl = bl; - if(OPT_DEBUG) - log_printf("DNSBL -> Passed '%s' to resolver", lookup); + if (OPT_DEBUG) + log_printf("DNSBL -> Passed '%s' to resolver", lookup); - res = firedns_getip(FDNS_QRY_A, lookup, (void *) ds); + res = firedns_getip(FDNS_QRY_A, lookup, (void *) ds); - if(res == -1 && fdns_errno != FDNS_ERR_FDLIMIT) - { - log_printf("DNSBL -> Error sending dns lookup for '%s': %s", lookup, firedns_strerror(fdns_errno)); - free(ds); - } - else - ss->scans++; /* Increase scan count - one for each blacklist */ - } + if (res == -1 && fdns_errno != FDNS_ERR_FDLIMIT) + { + log_printf("DNSBL -> Error sending dns lookup for '%s': %s", lookup, firedns_strerror(fdns_errno)); + free(ds); + } + else + ++ss->scans; /* Increase scan count - one for each blacklist */ + } } -static void dnsbl_positive(struct scan_struct *ss, struct BlacklistConf *bl, - unsigned char type) +static void +dnsbl_positive(struct scan_struct *ss, struct BlacklistConf *bl, unsigned char type) { - char text_type[128]; - struct BlacklistReplyConf *item; - node_t *p; - - text_type[0] = '\0'; - - if(bl->type == A_BITMASK) - { - LIST_FOREACH(p, bl->reply->head) + char text_type[128] = ""; + node_t *p; + + if (bl->type == A_BITMASK) + { + LIST_FOREACH(p, bl->reply->head) + { + struct BlacklistReplyConf *item = p->data; + + if (item->number & type) { - item = p->data; - if(item->number & type) - { - strncat(text_type, item->type, sizeof(text_type) - strlen(text_type) - 2); - text_type[sizeof(text_type) - 2] = '\0'; - strncat(text_type, ", ", sizeof(text_type) - strlen(text_type) - 1); - text_type[sizeof(text_type) - 1] = '\0'; - } + strncat(text_type, item->type, sizeof(text_type) - strlen(text_type) - 2); + text_type[sizeof(text_type) - 2] = '\0'; + + strncat(text_type, ", ", sizeof(text_type) - strlen(text_type) - 1); + text_type[sizeof(text_type) - 1] = '\0'; } - if(text_type[0] != '\0') - *(strrchr(text_type, ',')) = '\0'; - } - else - { - LIST_FOREACH(p, bl->reply->head) + } + + if (text_type[0]) + *(strrchr(text_type, ',')) = '\0'; + } + else + { + LIST_FOREACH(p, bl->reply->head) + { + struct BlacklistReplyConf *item = p->data; + + if (item->number == type) { - item = p->data; - if(item->number == type) - { - strlcpy(text_type, item->type, sizeof(text_type)); - break; - } + strlcpy(text_type, item->type, sizeof(text_type)); + break; } - } - - if(text_type[0] == '\0' && bl->ban_unknown == 0) - { - if(OPT_DEBUG) - log_printf("DNSBL -> Unknown result from BL zone %s (%d)", bl->name, type); - return; - } - - if(ss->manual_target) - { - irc_send("PRIVMSG %s :CHECK -> DNSBL -> %s appears in BL zone %s (%s)", - ss->manual_target->name, ss->ip, bl->name, text_type); - } - else if(!ss->positive) - { - /* Only report it if no other scans have found positives yet. */ - scan_positive(ss, (bl->kline[0] ? bl->kline : IRCItem->kline), - text_type); - - irc_send_channels("DNSBL -> %s!%s@%s appears in BL zone %s (%s)", - ss->irc_nick, ss->irc_username, ss->irc_hostname, bl->name, - text_type); - log_printf("DNSBL -> %s!%s@%s appears in BL zone %s (%s)", - ss->irc_nick, ss->irc_username, ss->irc_hostname, bl->name, - text_type); - } - - /* record stat */ - stats_dnsblrecv(bl); + } + } + + if (text_type[0] == '\0' && bl->ban_unknown == 0) + { + if (OPT_DEBUG) + log_printf("DNSBL -> Unknown result from BL zone %s (%d)", bl->name, type); + + return; + } + + if (ss->manual_target) + irc_send("PRIVMSG %s :CHECK -> DNSBL -> %s appears in BL zone %s (%s)", + ss->manual_target->name, ss->ip, bl->name, text_type); + else if (!ss->positive) + { + /* Only report it if no other scans have found positives yet. */ + scan_positive(ss, (bl->kline[0] ? bl->kline : IRCItem->kline), text_type); + + irc_send_channels("DNSBL -> %s!%s@%s appears in BL zone %s (%s)", + ss->irc_nick, ss->irc_username, ss->irc_hostname, bl->name, + text_type); + log_printf("DNSBL -> %s!%s@%s appears in BL zone %s (%s)", + ss->irc_nick, ss->irc_username, ss->irc_hostname, bl->name, + text_type); + } + + /* Record stat */ + stats_dnsblrecv(bl); } -void dnsbl_result(struct firedns_result *res) +void +dnsbl_result(struct firedns_result *res) { - struct dnsbl_scan *ds = res->info; + struct dnsbl_scan *ds = res->info; - if(OPT_DEBUG) + if (OPT_DEBUG) + { if (ds->ss->manual_target) log_printf("DNSBL -> Lookup result for %s (%s) %d.%d.%d.%d (error: %d)", - ds->ss->ip, - res->lookup, - (unsigned char)res->text[0], - (unsigned char)res->text[1], - (unsigned char)res->text[2], - (unsigned char)res->text[3], fdns_errno); + ds->ss->ip, + res->lookup, + (unsigned char)res->text[0], + (unsigned char)res->text[1], + (unsigned char)res->text[2], + (unsigned char)res->text[3], fdns_errno); else log_printf("DNSBL -> Lookup result for %s!%s@%s (%s) %d.%d.%d.%d (error: %d)", - ds->ss->irc_nick, - ds->ss->irc_username, - ds->ss->irc_hostname, - res->lookup, - (unsigned char)res->text[0], - (unsigned char)res->text[1], - (unsigned char)res->text[2], - (unsigned char)res->text[3], fdns_errno); - - /* Everything is OK */ - if(res->text[0] == '\0' && fdns_errno == FDNS_ERR_NXDOMAIN) - { - if(ds->ss->manual_target != NULL) - irc_send("PRIVMSG %s :CHECK -> DNSBL -> %s does not appear in BL zone %s", - ds->ss->manual_target->name, ds->ss->ip, - (strlen(ds->ss->ip) < strlen(res->lookup)) - ? (res->lookup + strlen(ds->ss->ip) + 1) - : res->lookup); - - - ds->ss->scans--; /* we are done with ss here */ - scan_checkfinished(ds->ss); /* this could free ss, don't use ss after this point */ - MyFree(ds); /* No longer need our information */ - return; - } - - /* Either an error, or a positive lookup */ - - if(fdns_errno == FDNS_ERR_NONE) - dnsbl_positive(ds->ss, ds->bl, (unsigned char)res->text[3]); - else - { - log_printf("DNSBL -> Lookup error on %s: %s", res->lookup, - firedns_strerror(fdns_errno)); - if(fdns_errno != FDNS_ERR_TIMEOUT) - irc_send_channels("DNSBL -> Lookup error on %s: %s", res->lookup, - firedns_strerror(fdns_errno)); - } - - /* Check if ss has any remaining scans */ - ds->ss->scans--; /* We are done with ss here */ - scan_checkfinished(ds->ss); /* this could free ss, don't use ss after this point */ - MyFree(ds); /* Finished with dnsbl_scan too */ + ds->ss->irc_nick, + ds->ss->irc_username, + ds->ss->irc_hostname, + res->lookup, + (unsigned char)res->text[0], + (unsigned char)res->text[1], + (unsigned char)res->text[2], + (unsigned char)res->text[3], fdns_errno); + } + + /* Everything is OK */ + if (res->text[0] == '\0' && fdns_errno == FDNS_ERR_NXDOMAIN) + { + if (ds->ss->manual_target) + irc_send("PRIVMSG %s :CHECK -> DNSBL -> %s does not appear in BL zone %s", + ds->ss->manual_target->name, ds->ss->ip, + (strlen(ds->ss->ip) < strlen(res->lookup)) ? (res->lookup + strlen(ds->ss->ip) + 1) : res->lookup); + + --ds->ss->scans; /* We are done with ss here */ + scan_checkfinished(ds->ss); /* This could free ss, don't use ss after this point */ + MyFree(ds); /* No longer need our information */ + return; + } + + /* Either an error, or a positive lookup */ + if (fdns_errno == FDNS_ERR_NONE) + dnsbl_positive(ds->ss, ds->bl, (unsigned char)res->text[3]); + else + { + log_printf("DNSBL -> Lookup error on %s: %s", res->lookup, + firedns_strerror(fdns_errno)); + + if (fdns_errno != FDNS_ERR_TIMEOUT) + irc_send_channels("DNSBL -> Lookup error on %s: %s", res->lookup, + firedns_strerror(fdns_errno)); + } + + /* Check if ss has any remaining scans */ + --ds->ss->scans; /* We are done with ss here */ + scan_checkfinished(ds->ss); /* This could free ss, don't use ss after this point */ + MyFree(ds); /* Finished with dnsbl_scan too */ } -void dnsbl_cycle(void) +void +dnsbl_cycle(void) { - firedns_cycle(); + firedns_cycle(); } /* * Send an email to report this open proxy. */ - -void dnsbl_report(struct scan_struct *ss) +void +dnsbl_report(struct scan_struct *ss) { - char buf[4096], cmdbuf[512]; - FILE *fp; - - if(ss->ip == NULL) - return; - - if (EmptyString(OpmItem->dnsbl_to) || EmptyString(OpmItem->dnsbl_from) || EmptyString(OpmItem->sendmail)) - return; - - - snprintf(cmdbuf, sizeof(cmdbuf), "%s -t", OpmItem->sendmail); - snprintf(buf, sizeof(buf), - "From: %s <%s>\n" - "To: %s\n" - "Subject: BOPM Report\n" - "X-BOPM-Version: %s\n\n" - "%s: %s:%d\n\n" - "%s\n", IRCItem->nick, OpmItem->dnsbl_from, OpmItem->dnsbl_to, - VERSION, scan_gettype(ss->remote->protocol), ss->ip, - ss->remote->port, ss->proof); - - if(OPT_DEBUG >= 3) - log_printf("DNSBL -> Sending following email:\n%s\n", buf); - - if ((fp = popen(cmdbuf, "w")) == NULL) - { - log_printf("DNSBL -> Failed to create pipe to '%s' for email report!", cmdbuf); - irc_send_channels("I was trying to create a pipe to'%s' to send a DNSBL " - "report, and it failed! I'll give up for now.", - cmdbuf); - return; - } - - fputs(buf, fp); - pclose(fp); - - log_printf("DNSBL -> Sent report to %s [%s]", OpmItem->dnsbl_to, ss->ip); - /* record send in stats */ - stats_dnsblsend(); + char buf[4096], cmdbuf[512]; + FILE *fp; + + if (ss->ip == NULL) + return; + + if (EmptyString(OpmItem->dnsbl_to) || EmptyString(OpmItem->dnsbl_from) || EmptyString(OpmItem->sendmail)) + return; + + snprintf(cmdbuf, sizeof(cmdbuf), "%s -t", OpmItem->sendmail); + snprintf(buf, sizeof(buf), + "From: %s <%s>\n" + "To: %s\n" + "Subject: BOPM Report\n" + "X-BOPM-Version: %s\n\n" + "%s: %s:%d\n\n" + "%s\n", IRCItem->nick, OpmItem->dnsbl_from, OpmItem->dnsbl_to, + VERSION, scan_gettype(ss->remote->protocol), ss->ip, + ss->remote->port, ss->proof); + + if (OPT_DEBUG >= 3) + log_printf("DNSBL -> Sending following email:\n%s\n", buf); + + if ((fp = popen(cmdbuf, "w")) == NULL) + { + log_printf("DNSBL -> Failed to create pipe to '%s' for email report!", cmdbuf); + irc_send_channels("I was trying to create a pipe to'%s' to send a DNSBL " + "report, and it failed! I'll give up for now.", + cmdbuf); + return; + } + + fputs(buf, fp); + pclose(fp); + + log_printf("DNSBL -> Sent report to %s [%s]", OpmItem->dnsbl_to, ss->ip); + + /* Record send in stats */ + stats_dnsblsend(); } diff --git a/src/irc.c b/src/irc.c index c9e6a30..fe21ec3 100644 --- a/src/irc.c +++ b/src/irc.c @@ -116,22 +116,24 @@ struct timeval IRC_TIMEOUT; /* timeval struct for select() time_t IRC_LAST = 0; /* Last full line of data from irc server*/ time_t IRC_LASTRECONNECT = 0; /* Time of last reconnection */ -/* Table should be ordered with most occuring (or priority) - commands at the top of the list. */ - -static struct CommandHash COMMAND_TABLE[] = { - {"NOTICE", m_notice }, - {"PRIVMSG", m_privmsg }, - {"PING", m_ping }, - {"INVITE", m_invite }, - {"001", m_perform }, - {"302", m_userhost }, - {"471", m_cannot_join }, - {"473", m_cannot_join }, - {"474", m_cannot_join }, - {"475", m_cannot_join }, - {"KILL", m_kill } - }; +/* + * Table should be ordered with most occuring (or priority) + * commands at the top of the list. + */ +static struct CommandHash COMMAND_TABLE[] = +{ + { "NOTICE", m_notice }, + { "PRIVMSG", m_privmsg }, + { "PING", m_ping }, + { "INVITE", m_invite }, + { "001", m_perform }, + { "302", m_userhost }, + { "471", m_cannot_join }, + { "473", m_cannot_join }, + { "474", m_cannot_join }, + { "475", m_cannot_join }, + { "KILL", m_kill } +}; /* irc_cycle * @@ -142,50 +144,47 @@ static struct CommandHash COMMAND_TABLE[] = { * * Return: * None - * */ - -void irc_cycle(void) +void +irc_cycle(void) { - if (IRC_FD <= 0) - { - /* Initialise negative cache. */ - if (OptionsItem->negcache > 0) - nc_init(&nc_head); - - /* Resolve remote host. */ - irc_init(); - - /* Connect to remote host. */ - irc_connect(); - - return; /* In case connect() immediately failed */ + if (IRC_FD <= 0) + { + /* Initialise negative cache. */ + if (OptionsItem->negcache > 0) + nc_init(&nc_head); + + /* Resolve remote host. */ + irc_init(); + + /* Connect to remote host. */ + irc_connect(); + return; /* In case connect() immediately failed */ } - IRC_TIMEOUT.tv_sec = 0; - /* Block .025 seconds to avoid excessive CPU use on select(). */ - IRC_TIMEOUT.tv_usec = 25000; - - FD_ZERO(&IRC_READ_FDSET); - FD_SET(IRC_FD, &IRC_READ_FDSET); - - switch (select((IRC_FD + 1), &IRC_READ_FDSET, 0, 0, &IRC_TIMEOUT)) - { - case -1: - return; - break; - case 0: - break; - default: - /* Check if IRC data is available. */ - if (FD_ISSET(IRC_FD, &IRC_READ_FDSET)) - irc_read(); - break; - } -} + IRC_TIMEOUT.tv_sec = 0; + /* Block .025 seconds to avoid excessive CPU use on select(). */ + IRC_TIMEOUT.tv_usec = 25000; + FD_ZERO(&IRC_READ_FDSET); + FD_SET(IRC_FD, &IRC_READ_FDSET); + switch (select((IRC_FD + 1), &IRC_READ_FDSET, 0, 0, &IRC_TIMEOUT)) + { + case -1: + return; + break; + case 0: + break; + default: + /* Check if IRC data is available. */ + if (FD_ISSET(IRC_FD, &IRC_READ_FDSET)) + irc_read(); + + break; + } +} /* irc_init * @@ -196,48 +195,46 @@ void irc_cycle(void) * * Return: * None - * */ - -static void irc_init(void) +static void +irc_init(void) { - node_t *node; - struct ChannelConf *chan; - struct bopm_sockaddr bsaddr; - struct in_addr *irc_host; - - - if (IRC_FD) - close(IRC_FD); - - memset(&IRC_SVR, 0, sizeof(IRC_SVR)); - memset(&IRC_LOCAL, 0, sizeof(IRC_LOCAL)); - memset(&bsaddr, 0, sizeof(struct bopm_sockaddr)); - - /* Resolve IRC host. */ - if ((irc_host = firedns_resolveip4(IRCItem->server)) == NULL) - { - log_printf("IRC -> firedns_resolveip4(\"%s\"): %s", IRCItem->server, - firedns_strerror(fdns_errno)); - exit(EXIT_FAILURE); - } + node_t *node; + struct ChannelConf *chan; + struct bopm_sockaddr bsaddr; + struct in_addr *irc_host; + + if (IRC_FD) + close(IRC_FD); + + memset(&IRC_SVR, 0, sizeof(IRC_SVR)); + memset(&IRC_LOCAL, 0, sizeof(IRC_LOCAL)); + memset(&bsaddr, 0, sizeof(struct bopm_sockaddr)); + + /* Resolve IRC host. */ + if ((irc_host = firedns_resolveip4(IRCItem->server)) == NULL) + { + log_printf("IRC -> firedns_resolveip4(\"%s\"): %s", IRCItem->server, + firedns_strerror(fdns_errno)); + exit(EXIT_FAILURE); + } - IRC_SVR.sa4.sin_family = AF_INET; - IRC_SVR.sa4.sin_port = htons(IRCItem->port); - IRC_SVR.sa4.sin_addr = *irc_host; + IRC_SVR.sa4.sin_family = AF_INET; + IRC_SVR.sa4.sin_port = htons(IRCItem->port); + IRC_SVR.sa4.sin_addr = *irc_host; - if (IRC_SVR.sa4.sin_addr.s_addr == INADDR_NONE) - { - log_printf("IRC -> Unknown error resolving remote host (%s)", - IRCItem->server); - exit(EXIT_FAILURE); - } + if (IRC_SVR.sa4.sin_addr.s_addr == INADDR_NONE) + { + log_printf("IRC -> Unknown error resolving remote host (%s)", + IRCItem->server); + exit(EXIT_FAILURE); + } - /* Request file desc for IRC client socket */ - IRC_FD = socket(AF_INET, SOCK_STREAM, 0); + /* Request file desc for IRC client socket */ + IRC_FD = socket(AF_INET, SOCK_STREAM, 0); - if (IRC_FD == -1) - { + if (IRC_FD == -1) + { switch(errno) { case EINVAL: @@ -266,6 +263,7 @@ static void irc_init(void) "socket"); break; } + exit(EXIT_FAILURE); } @@ -273,11 +271,13 @@ static void irc_init(void) if (!EmptyString(IRCItem->vhost)) { int bindret = 0; + if (!inet_pton(AF_INET, IRCItem->vhost, &(IRC_LOCAL.in4.s_addr))) { log_printf("IRC -> bind(): %s is an invalid address", IRCItem->vhost); exit(EXIT_FAILURE); } + bsaddr.sa4.sin_addr.s_addr = IRC_LOCAL.in4.s_addr; bsaddr.sa4.sin_family = AF_INET; bsaddr.sa4.sin_port = htons(0); @@ -298,12 +298,10 @@ static void irc_init(void) break; } exit(EXIT_FAILURE); - } - - } + } + } } - /* irc_send * * Send data to remote IRC host. @@ -314,29 +312,28 @@ static void irc_init(void) * * Return: NONE */ - - -void irc_send(const char *data, ...) +void +irc_send(const char *data, ...) { - va_list arglist; - char data2[MSGLENMAX]; - char tosend[MSGLENMAX]; + va_list arglist; + char data2[MSGLENMAX]; + char tosend[MSGLENMAX]; - va_start(arglist, data); - vsnprintf(data2, MSGLENMAX, data, arglist); - va_end(arglist); + va_start(arglist, data); + vsnprintf(data2, MSGLENMAX, data, arglist); + va_end(arglist); - if (OPT_DEBUG >= 2) - log_printf("IRC SEND -> %s", data2); + if (OPT_DEBUG >= 2) + log_printf("IRC SEND -> %s", data2); - snprintf(tosend, MSGLENMAX, "%s\n", data2); + snprintf(tosend, MSGLENMAX, "%s\n", data2); - if (send(IRC_FD, tosend, strlen(tosend), 0) == -1) - { - /* Return of -1 indicates error sending data; we reconnect. */ - log_printf("IRC -> Error sending data to server\n"); - irc_reconnect(); - } + if (send(IRC_FD, tosend, strlen(tosend), 0) == -1) + { + /* Return of -1 indicates error sending data; we reconnect. */ + log_printf("IRC -> Error sending data to server\n"); + irc_reconnect(); + } } /* irc_send @@ -349,29 +346,26 @@ void irc_send(const char *data, ...) * * Return: NONE */ - -void irc_send_channels(const char *data, ...) +void +irc_send_channels(const char *data, ...) { - const node_t *node; - va_list arglist; - char buf[MSGLENMAX]; + const node_t *node; + va_list arglist; + char buf[MSGLENMAX]; - va_start(arglist, data); - vsnprintf(buf, sizeof(buf), data, arglist); - va_end(arglist); + va_start(arglist, data); + vsnprintf(buf, sizeof(buf), data, arglist); + va_end(arglist); - LIST_FOREACH(node, IRCItem->channels->head) - { - const struct ChannelConf *chan = node->data; + LIST_FOREACH(node, IRCItem->channels->head) + { + const struct ChannelConf *chan = node->data; - irc_send("PRIVMSG %s :%s", chan->name, buf); - } + irc_send("PRIVMSG %s :%s", chan->name, buf); + } } - - - /* irc_connect * * Connect to IRC server. @@ -379,59 +373,58 @@ void irc_send_channels(const char *data, ...) * * Parameters: NONE * Return: NONE - * */ - -static void irc_connect(void) +static void +irc_connect(void) { - /* Connect to IRC server as client. */ - if (connect(IRC_FD, (struct sockaddr *) &IRC_SVR, - sizeof(IRC_SVR)) == -1) - { - switch(errno) - { - case EISCONN: - /* Already connected */ - return; - case ECONNREFUSED: - log_printf("IRC -> connect(): Connection refused by (%s)", - IRCItem->server); - break; - case ETIMEDOUT: - log_printf("IRC -> connect(): Timed out connecting to (%s)", - IRCItem->server); - break; - case ENETUNREACH: - log_printf("IRC -> connect(): Network unreachable"); - break; - case EALREADY: - /* Previous attempt not complete */ - return; - default: - log_printf("IRC -> connect(): Unknown error connecting to (%s)", - IRCItem->server); + /* Connect to IRC server as client. */ + if (connect(IRC_FD, (struct sockaddr *)&IRC_SVR, sizeof(IRC_SVR)) == -1) + { + switch (errno) + { + case EISCONN: + /* Already connected */ + return; + case ECONNREFUSED: + log_printf("IRC -> connect(): Connection refused by (%s)", + IRCItem->server); + break; + case ETIMEDOUT: + log_printf("IRC -> connect(): Timed out connecting to (%s)", + IRCItem->server); + break; + case ENETUNREACH: + log_printf("IRC -> connect(): Network unreachable"); + break; + case EALREADY: + /* Previous attempt not complete */ + return; + default: + log_printf("IRC -> connect(): Unknown error connecting to (%s)", + IRCItem->server); - if (OPT_DEBUG >= 1) - log_printf("%s", strerror(errno)); - } - /* Try to connect again */ - irc_reconnect(); - return; - } + if (OPT_DEBUG >= 1) + log_printf("%s", strerror(errno)); + } - irc_send("NICK %s", IRCItem->nick); + /* Try to connect again */ + irc_reconnect(); + return; + } - if (!EmptyString(IRCItem->password)) - irc_send("PASS %s", IRCItem->password); + irc_send("NICK %s", IRCItem->nick); - irc_send("USER %s %s %s :%s", - IRCItem->username, IRCItem->username, IRCItem->username, - IRCItem->realname); + if (!EmptyString(IRCItem->password)) + irc_send("PASS %s", IRCItem->password); - time(&IRC_LAST); + irc_send("USER %s %s %s :%s", + IRCItem->username, + IRCItem->username, + IRCItem->username, + IRCItem->realname); + time(&IRC_LAST); } - /* irc_reconnect * * Close connection to IRC server. @@ -440,46 +433,42 @@ static void irc_connect(void) * * Return: NONE */ - -static void irc_reconnect(void) +static void +irc_reconnect(void) { + time_t present; - time_t present; - - time(&present); - - /* Only try to reconnect every RECONNECT_INTERVAL seconds */ - if((present - IRC_LASTRECONNECT) < RECONNECTINTERVAL) - { - /* Sleep to avoid excessive CPU */ - sleep(1); - return; - } + time(&present); - time(&IRC_LASTRECONNECT); + /* Only try to reconnect every RECONNECT_INTERVAL seconds */ + if ((present - IRC_LASTRECONNECT) < RECONNECTINTERVAL) + { + /* Sleep to avoid excessive CPU */ + sleep(1); + return; + } - if(IRC_FD > 0) - close(IRC_FD); + time(&IRC_LASTRECONNECT); - /* Set IRC_FD 0 for reconnection on next irc_cycle(). */ - IRC_FD = 0; - - log_printf("IRC -> Connection to (%s) failed, reconnecting.", IRCItem->server); -} + if (IRC_FD > 0) + close(IRC_FD); + /* Set IRC_FD 0 for reconnection on next irc_cycle(). */ + IRC_FD = 0; + log_printf("IRC -> Connection to (%s) failed, reconnecting.", IRCItem->server); +} /* irc_read * * irc_read is called my irc_cycle when new data is ready to be - * read from the irc server. + * read from the irc server. * * Parameters: NONE * Return: NONE - * */ - -static void irc_read(void) +static void +irc_read(void) { int len; char c; @@ -514,7 +503,6 @@ static void irc_read(void) } } - /* irc_parse * * irc_parse is called by irc_read when a full line of data @@ -522,11 +510,9 @@ static void irc_read(void) * * Parameters: NONE * Return: NONE - * */ - - -static void irc_parse(void) +static void +irc_parse(void) { struct UserInfo *source_p; char *pos; @@ -611,49 +597,42 @@ static void irc_parse(void) userinfo_free(source_p); } - - - /* irc_timer * * Functions to be performed every ~seconds. * * Parameters: NONE * Return: NONE - * */ - -void irc_timer(void) +void +irc_timer(void) { - time_t present, delta; + time_t present, delta; - time(&present); + time(&present); - delta = present - IRC_LAST; + delta = present - IRC_LAST; - /* No data in NODATA_TIMEOUT minutes (set in options.h). */ - if (delta >= NODATA_TIMEOUT) - { - log_printf("IRC -> Timeout awaiting data from server."); - irc_reconnect(); - /* Make sure we dont do this again for a while */ - time(&IRC_LAST); - } - else if (delta >= NODATA_TIMEOUT / 2) - { - /* - * Generate some data so high ping times or bugs in certain - * ircds (*cough* unreal *cough*) don't cause uneeded - * reconnections - */ - irc_send("PING :HOPM"); - } + /* No data in NODATA_TIMEOUT minutes (set in options.h). */ + if (delta >= NODATA_TIMEOUT) + { + log_printf("IRC -> Timeout awaiting data from server."); + irc_reconnect(); + /* Make sure we dont do this again for a while */ + time(&IRC_LAST); + } + else if (delta >= NODATA_TIMEOUT / 2) + { + /* + * Generate some data so high ping times or bugs in certain + * ircds (*cough* unreal *cough*) don't cause uneeded + * reconnections + */ + irc_send("PING :HOPM"); + } } - - - /* get_channel * * Check if a channel is defined in our conf. If so return @@ -664,24 +643,23 @@ void irc_timer(void) * * Return: Pointer to ChannelConf containing the channel */ - -static struct ChannelConf *get_channel(const char *channel) +static struct ChannelConf * +get_channel(const char *channel) { - node_t *node; - struct ChannelConf *item; + node_t *node; + struct ChannelConf *item; - LIST_FOREACH(node, IRCItem->channels->head) - { - item = node->data; + LIST_FOREACH(node, IRCItem->channels->head) + { + item = node->data; - if(strcasecmp(item->name, channel) == 0) - return item; - } + if (strcasecmp(item->name, channel) == 0) + return item; + } - return NULL; + return NULL; } - /* userinfo_create * * Parse a nick!user@host into a UserInfo struct @@ -692,83 +670,74 @@ static struct ChannelConf *get_channel(const char *channel) * * Return: * pointer to new UserInfo struct, or NULL if parsing failed - * */ - -static struct UserInfo *userinfo_create(char *source) +static struct UserInfo * +userinfo_create(char *source) { - struct UserInfo *ret; - - char *nick; - char *username; - char *hostname; - char *tmp; - - int i, len; - - nick = username = hostname = NULL; - tmp = xstrdup(source); - len = strlen(tmp); - - nick = tmp; - - for(i = 0; i < len; i++) - { - if(tmp[i] == '!') - { - tmp[i] = '\0'; - username = tmp + i + 1; - } - if(tmp[i] == '@') - { - tmp[i] = '\0'; - hostname = tmp + i + 1; - } - } - - if(nick == NULL || username == NULL || hostname == NULL) - { - MyFree(tmp); - return NULL; - } + struct UserInfo *ret; + char *nick; + char *username; + char *hostname; + char *tmp; + int i, len; + + nick = username = hostname = NULL; + tmp = xstrdup(source); + len = strlen(tmp); + nick = tmp; + + for (i = 0; i < len; ++i) + { + if (tmp[i] == '!') + { + tmp[i] = '\0'; + username = tmp + i + 1; + } + + if (tmp[i] == '@') + { + tmp[i] = '\0'; + hostname = tmp + i + 1; + } + } - ret = MyMalloc(sizeof *ret); + if (nick == NULL || username == NULL || hostname == NULL) + { + MyFree(tmp); + return NULL; + } - ret->irc_nick = xstrdup(nick); - ret->irc_username = xstrdup(username); - ret->irc_hostname = xstrdup(hostname); + ret = MyMalloc(sizeof *ret); + ret->irc_nick = xstrdup(nick); + ret->irc_username = xstrdup(username); + ret->irc_hostname = xstrdup(hostname); - MyFree(tmp); + MyFree(tmp); - return ret; + return ret; }; - - /* userinfo_free * * Free a UserInfo struct created with userinfo_create. * * Parameters: * source: struct to free - * - * Return: None * + * Return: None */ - -static void userinfo_free(struct UserInfo *source_p) +static void +userinfo_free(struct UserInfo *source_p) { - if(source_p == NULL) - return; + if (source_p == NULL) + return; - MyFree(source_p->irc_nick); - MyFree(source_p->irc_username); - MyFree(source_p->irc_hostname); - MyFree(source_p); + MyFree(source_p->irc_nick); + MyFree(source_p->irc_username); + MyFree(source_p->irc_hostname); + MyFree(source_p); } - - /* m_perform * * actions to perform on IRC connection @@ -781,48 +750,46 @@ static void userinfo_free(struct UserInfo *source_p) * source_p: UserInfo struct of the source user, or NULL if * the source (parv[0]) is a server. */ - -static void m_perform(char **parv, unsigned int parc, char *msg, struct UserInfo *notused) +static void +m_perform(char **parv, unsigned int parc, char *msg, struct UserInfo *notused) { - node_t *node; - struct ChannelConf *channel; + node_t *node; - log_printf("IRC -> Connected to %s:%d", IRCItem->server, IRCItem->port); + log_printf("IRC -> Connected to %s:%d", IRCItem->server, IRCItem->port); - /* Identify to nickserv if needed */ - if (!EmptyString(IRCItem->nickserv)) - irc_send("%s", IRCItem->nickserv); + /* Identify to nickserv if needed */ + if (!EmptyString(IRCItem->nickserv)) + irc_send("%s", IRCItem->nickserv); - /* Oper */ - irc_send("OPER %s", IRCItem->oper); + /* Oper */ + irc_send("OPER %s", IRCItem->oper); - /* Set modes */ - irc_send("MODE %s %s", IRCItem->nick, IRCItem->mode); + /* Set modes */ + irc_send("MODE %s %s", IRCItem->nick, IRCItem->mode); - /* Set Away */ + /* Set Away */ if (!EmptyString(IRCItem->away)) irc_send("AWAY :%s", IRCItem->away); - /* Perform */ - LIST_FOREACH(node, IRCItem->performs->head) - irc_send("%s", node->data); + /* Perform */ + LIST_FOREACH(node, IRCItem->performs->head) + irc_send("%s", node->data); - /* Join all listed channels. */ - LIST_FOREACH(node, IRCItem->channels->head) - { - channel = node->data; + /* Join all listed channels. */ + LIST_FOREACH(node, IRCItem->channels->head) + { + struct ChannelConf *channel = node->data; - if (EmptyString(channel->name)) - continue; + if (EmptyString(channel->name)) + continue; - if (!EmptyString(channel->key)) - irc_send("JOIN %s %s", channel->name, channel->key); - else - irc_send("JOIN %s", channel->name); - } + if (!EmptyString(channel->key)) + irc_send("JOIN %s %s", channel->name, channel->key); + else + irc_send("JOIN %s", channel->name); + } } - /* m_ping * * parv[0] = source @@ -832,19 +799,18 @@ static void m_perform(char **parv, unsigned int parc, char *msg, struct UserInfo * source_p: UserInfo struct of the source user, or NULL if * the source (parv[0]) is a server. */ -static void m_ping(char **parv, unsigned int parc, char *msg, struct UserInfo *source_p) +static void +m_ping(char **parv, unsigned int parc, char *msg, struct UserInfo *source_p) { - if(parc < 3) - return; + if (parc < 3) + return; - if(OPT_DEBUG >= 2) - log_printf("IRC -> PING? PONG!"); + if (OPT_DEBUG >= 2) + log_printf("IRC -> PING? PONG!"); - irc_send("PONG %s", parv[2]); + irc_send("PONG %s", parv[2]); } - - /* m_invite * * parv[0] = source @@ -854,26 +820,22 @@ static void m_ping(char **parv, unsigned int parc, char *msg, struct UserInfo *s * * source_p: UserInfo struct of the source user, or NULL if * the source (parv[0]) is a server. - * */ - -static void m_invite(char **parv, unsigned int parc, char *msg, struct UserInfo *source_p) +static void +m_invite(char **parv, unsigned int parc, char *msg, struct UserInfo *source_p) { - struct ChannelConf *channel; - - if(parc < 4) - return; - - log_printf("IRC -> Invited to %s by %s", parv[3], parv[0]); - - if((channel = get_channel(parv[3])) == NULL) - return; + struct ChannelConf *channel; - irc_send("JOIN %s %s", channel->name, channel->key); -} + if (parc < 4) + return; + log_printf("IRC -> Invited to %s by %s", parv[3], parv[0]); + if ((channel = get_channel(parv[3])) == NULL) + return; + irc_send("JOIN %s %s", channel->name, channel->key); +} /* m_privmsg * @@ -884,49 +846,45 @@ static void m_invite(char **parv, unsigned int parc, char *msg, struct UserInfo * * source_p: UserInfo struct of the source user, or NULL if * the source (parv[0]) is a server. - * */ - -static void m_privmsg(char **parv, unsigned int parc, char *msg, struct UserInfo *source_p) +static void +m_privmsg(char **parv, unsigned int parc, char *msg, struct UserInfo *source_p) { - struct ChannelConf *channel; - size_t nick_len; + struct ChannelConf *channel; + size_t nick_len; - if(source_p == NULL) - return; + if (source_p == NULL) + return; - if(parc < 4) - return; + if (parc < 4) + return; - /* CTCP */ - if(parv[3][0] == '\001') - m_ctcp(parv, parc, msg, source_p); + /* CTCP */ + if (parv[3][0] == '\001') + m_ctcp(parv, parc, msg, source_p); - /* Only interested in privmsg to channels */ - if(parv[2][0] != '#' && parv[2][0] != '&') - return; - - /* Get a target */ - if((channel = get_channel(parv[2])) == NULL) - return; - - /* Find a suitable length to compare with */ - nick_len = strcspn(parv[3], " :,"); - if(nick_len < 3 && strlen(IRCItem->nick) >= 3) - nick_len = 3; - - /* message is a command */ - if(strncasecmp(parv[3], IRCItem->nick, nick_len) == 0 || - strncasecmp(parv[3], "!all", 4) == 0) - { - /* XXX command_parse will alter parv[3]. */ - command_parse(parv[3], msg, channel, source_p); - } -} + /* Only interested in privmsg to channels */ + if (parv[2][0] != '#' && parv[2][0] != '&') + return; + /* Get a target */ + if ((channel = get_channel(parv[2])) == NULL) + return; + /* Find a suitable length to compare with */ + nick_len = strcspn(parv[3], " :,"); + if (nick_len < 3 && strlen(IRCItem->nick) >= 3) + nick_len = 3; + /* message is a command */ + if (strncasecmp(parv[3], IRCItem->nick, nick_len) == 0 || + strncasecmp(parv[3], "!all", 4) == 0) + { + /* XXX command_parse will alter parv[3]. */ + command_parse(parv[3], msg, channel, source_p); + } +} /* m_ctcp * parv[0] = source @@ -938,18 +896,14 @@ static void m_privmsg(char **parv, unsigned int parc, char *msg, struct UserInfo * the source (parv[0]) is a server. * */ - -static void m_ctcp(char **parv, unsigned int parc, char *msg, struct UserInfo *source_p) +static void +m_ctcp(char **parv, unsigned int parc, char *msg, struct UserInfo *source_p) { if (strncasecmp(parv[3], "\001VERSION\001", 9) == 0) irc_send("NOTICE %s :\001VERSION Hybrid Open Proxy Monitor %s\001", source_p->irc_nick, VERSION); } - - - - /* m_notice * * parv[0] = source @@ -962,85 +916,80 @@ static void m_ctcp(char **parv, unsigned int parc, char *msg, struct UserInfo *s * the source (parv[0]) is a server. * */ - -static void m_notice(char **parv, unsigned int parc, char *msg, struct UserInfo *source_p) +static void +m_notice(char **parv, unsigned int parc, char *msg, struct UserInfo *source_p) { + static regex_t *preg = NULL; + regmatch_t pmatch[5]; + static char errmsg[256]; + int errnum, i; + char *user[4]; - static regex_t *preg = NULL; - regmatch_t pmatch[5]; + if (parc < 4) + return; - static char errmsg[256]; - int errnum, i; + /* Not interested in notices from users */ + if (source_p) + return; - char *user[4]; + /* Compile the regular expression if it has not been already */ + if (preg == NULL) + { + preg = MyMalloc(sizeof *preg); - if(parc < 4) - return; + if ((errnum = regcomp(preg, IRCItem->connregex, REG_ICASE | REG_EXTENDED))) + { + regerror(errnum, preg, errmsg, 256); + log_printf("IRC REGEX -> Error when compiling regular expression"); + log_printf("IRC REGEX -> %s", errmsg); - /* Not interested in notices from users */ - if(source_p != NULL) + MyFree(preg); + preg = NULL; return; + } + } - /* Compile the regular expression if it has not been already */ - if(preg == NULL) - { - preg = MyMalloc(sizeof *preg); - - if((errnum = regcomp(preg, IRCItem->connregex, REG_ICASE | REG_EXTENDED)) != 0) - { - - regerror(errnum, preg, errmsg, 256); - log_printf("IRC REGEX -> Error when compiling regular expression"); - log_printf("IRC REGEX -> %s", errmsg); - - MyFree(preg); - preg = NULL; - return; - } - } - - /* Match the expression against the possible connection notice */ - if(regexec(preg, parv[3], 5, pmatch, 0) != 0) - return; - - if(OPT_DEBUG > 0) - log_printf("IRC REGEX -> Regular expression caught connection notice. Parsing."); - - if(pmatch[4].rm_so == -1) - { - log_printf("IRC REGEX -> pmatch[4].rm_so is -1 while parsing??? Aborting."); - return; - } - - /* - Offsets for data in the connection notice: + /* Match the expression against the possible connection notice */ + if (regexec(preg, parv[3], 5, pmatch, 0)) + return; - NICKNAME: pmatch[1].rm_so TO pmatch[1].rm_eo - USERNAME: pmatch[2].rm_so TO pmatch[2].rm_eo - HOSTNAME: pmatch[3].rm_so TO pmatch[3].rm_eo - IP : pmatch[4].rm_so TO pmatch[4].rm_eo + if (OPT_DEBUG > 0) + log_printf("IRC REGEX -> Regular expression caught connection notice. Parsing."); - */ + if (pmatch[4].rm_so == -1) + { + log_printf("IRC REGEX -> pmatch[4].rm_so is -1 while parsing??? Aborting."); + return; + } - for(i = 0; i < 4; i++) - { - user[i] = (parv[3] + pmatch[i + 1].rm_so); - *(parv[3] + pmatch[i + 1].rm_eo) = '\0'; - } + /* + * Offsets for data in the connection notice: + * + * NICKNAME: pmatch[1].rm_so TO pmatch[1].rm_eo + * USERNAME: pmatch[2].rm_so TO pmatch[2].rm_eo + * HOSTNAME: pmatch[3].rm_so TO pmatch[3].rm_eo + * IP : pmatch[4].rm_so TO pmatch[4].rm_eo + */ + for (i = 0; i < 4; ++i) + { + user[i] = (parv[3] + pmatch[i + 1].rm_so); + *(parv[3] + pmatch[i + 1].rm_eo) = '\0'; + } - if(OPT_DEBUG > 0) - log_printf("IRC REGEX -> Parsed %s!%s@%s [%s] from connection notice.", - user[0], user[1], user[2], user[3]); + if (OPT_DEBUG > 0) + log_printf("IRC REGEX -> Parsed %s!%s@%s [%s] from connection notice.", + user[0], user[1], user[2], user[3]); - /*FIXME (reminder) In the case of any rehash to the regex, preg MUST be freed first. - regfree(preg); + /*FIXME (reminder) In the case of any rehash to the regex, preg MUST be freed first. + regfree(preg); */ - /* Pass this information off to scan.c */ - scan_connect(user, msg); - /* Record the connect for stats purposes */ - stats_connect(); + /* Pass this information off to scan.c */ + scan_connect(user, msg); + + /* Record the connect for stats purposes */ + stats_connect(); } /* m_userhost @@ -1055,14 +1004,13 @@ static void m_notice(char **parv, unsigned int parc, char *msg, struct UserInfo * the source (parv[0]) is a server. * */ - -static void m_userhost(char **parv, unsigned int parc, char *msg, - struct UserInfo *source_p) +static void +m_userhost(char **parv, unsigned int parc, char *msg, struct UserInfo *source_p) { - if(parc < 4) - return; + if (parc < 4) + return; - command_userhost(parv[3]); + command_userhost(parv[3]); } /* m_cannot_join @@ -1074,26 +1022,24 @@ static void m_userhost(char **parv, unsigned int parc, char *msg, * parv[4] = error text * */ - -static void m_cannot_join(char **parv, unsigned int parc, char *msg, - struct UserInfo *source_p) +static void +m_cannot_join(char **parv, unsigned int parc, char *msg, struct UserInfo *source_p) { - struct ChannelConf *channel; + struct ChannelConf *channel; - if(parc < 5) - return; + if (parc < 5) + return; - /* Is it one of our channels? */ - if((channel = get_channel(parv[3])) == NULL) - return; + /* Is it one of our channels? */ + if ((channel = get_channel(parv[3])) == NULL) + return; - if (EmptyString(channel->invite)) - return; + if (EmptyString(channel->invite)) + return; - irc_send("%s", channel->invite); + irc_send("%s", channel->invite); } - /* m_kill * * parv[0] = source @@ -1103,8 +1049,8 @@ static void m_cannot_join(char **parv, unsigned int parc, char *msg, * parv[4] = error text * */ - -static void m_kill(char **parv, unsigned int parc, char *msg, struct UserInfo *source_p) +static void +m_kill(char **parv, unsigned int parc, char *msg, struct UserInfo *source_p) { /* Restart hopm to rehash */ main_restart(); diff --git a/src/opercmd.c b/src/opercmd.c index 02d67ac..7603d3d 100644 --- a/src/opercmd.c +++ b/src/opercmd.c @@ -43,9 +43,8 @@ along with this program; if not, write to the Free Software #include "list.h" #include "stats.h" -/* List of active commands */ -list_t *COMMANDS = NULL; +list_t *COMMANDS = NULL; /* List of active commands */ static struct Command *command_create(unsigned short type, char *param, char *irc_nick, struct ChannelConf *target); @@ -56,15 +55,14 @@ static void cmd_stat(char *, char *, struct ChannelConf *); static void cmd_fdstat(char *, char *, struct ChannelConf *); static struct OperCommandHash COMMAND_TABLE[] = - { - {"CHECK", cmd_check }, - {"SCAN", cmd_check }, - {"STAT", cmd_stat }, - {"STATS", cmd_stat }, - {"STATUS", cmd_stat }, - {"FDSTAT", cmd_fdstat }, - }; - +{ + {"CHECK", cmd_check }, + {"SCAN", cmd_check }, + {"STAT", cmd_stat }, + {"STATS", cmd_stat }, + {"STATUS", cmd_stat }, + {"FDSTAT", cmd_fdstat }, +}; /* command_init @@ -75,16 +73,13 @@ static struct OperCommandHash COMMAND_TABLE[] = * Return: NONE * */ - -void command_init() +void +command_init(void) { - if(COMMANDS == NULL) - COMMANDS = list_create(); + if (COMMANDS == NULL) + COMMANDS = list_create(); } - - - /* command_timer * * Perform ~1 second actions. @@ -94,39 +89,37 @@ void command_init() * Return: NONE * */ - -void command_timer() +void +command_timer(void) { + static unsigned short interval; + node_t *node, *next; + struct Command *cs; + time_t present; + + /* Only perform command removal every COMMANDINTERVAL seconds */ + if (interval++ < COMMANDINTERVAL) + return; + else + interval = 0; - static unsigned short interval; + time(&present); - node_t *node, *next; - struct Command *cs; - time_t present; + LIST_FOREACH_SAFE(node, next, COMMANDS->head) + { + cs = node->data; - /* Only perform command removal every COMMANDINTERVAL seconds */ - if(interval++ < COMMANDINTERVAL) + if ((present - cs->added) > COMMANDTIMEOUT) + { + command_free(cs); + list_remove(COMMANDS, node); + node_free(node); + } + else /* Since the queue is in order, it's also ordered by time, no nodes after this will be timed out */ return; - else - interval = 0; - - time(&present); - - LIST_FOREACH_SAFE(node, next, COMMANDS->head) - { - cs = node->data; - if((present - cs->added) > COMMANDTIMEOUT) - { - command_free(cs); - list_remove(COMMANDS, node); - node_free(node); - } - else /* Since the queue is in order, it's also ordered by time, no nodes after this will be timed out */ - return; - } + } } - /* command_parse * * Parse a command to hopm (sent to a channel hopm is on). The command is parsed @@ -141,72 +134,68 @@ void command_timer() * source_p: Operator (hopefully) that sent the command. * */ - -void command_parse(char *command, char *msg, struct ChannelConf *target, - struct UserInfo *source_p) +void +command_parse(char *command, char *msg, struct ChannelConf *target, + struct UserInfo *source_p) { - unsigned int i; - char *param; /* Parsed parameters */ - struct Command *cs; - node_t *node; - - if(OPT_DEBUG) - { - log_printf("COMMAND -> Parsing command (%s) from %s [%s]", command, - source_p->irc_nick, target->name); - } - - /* Only allow COMMANDMAX commands in the queue */ - if(LIST_SIZE(COMMANDS) >= COMMANDMAX) - return; - - /* Parameter is the first character in command after the first space. - param will be NULL if: - 1. There was no space - 2. There was a space but it was the last character in command, in which case - param = '\0' + char *param; /* Parsed parameters */ + struct Command *cs; + node_t *node; + + if (OPT_DEBUG) + log_printf("COMMAND -> Parsing command (%s) from %s [%s]", command, + source_p->irc_nick, target->name); + + /* Only allow COMMANDMAX commands in the queue */ + if (LIST_SIZE(COMMANDS) >= COMMANDMAX) + return; + + /* + * Parameter is the first character in command after the first space. + * param will be NULL if: + * 1. There was no space + * 2. There was a space but it was the last character in command, in which case + * param = '\0' */ - /* Skip past the botname/!all */ - command = strchr(command, ' '); -/* TBD: skip leading spaces if there's more than one */ - /* There is no command OR - there is at least nothing - past that first space. */ - if (command == NULL || *++command == '\0') - return; - - - /* Find the parameters */ - param = strchr(command, ' '); - - if(param != NULL) - { - *param = '\0'; - param++; - } - else - param = ""; - - log_printf("COMMAND -> parsed [%s] [%s]", command, param); - - /* Lookup the command in the table */ - for(i = 0; i < sizeof(COMMAND_TABLE) / sizeof(struct OperCommandHash); i++) - { - if(strcasecmp(command, COMMAND_TABLE[i].command) == 0) - { - /* Queue this command */ - cs = command_create(i, param, source_p->irc_nick, target); - node = node_create(cs); - list_add(COMMANDS, node); - } - } - - irc_send("USERHOST %s", source_p->irc_nick); -} + /* Skip past the botname/!all */ + command = strchr(command, ' '); + /* TBD: skip leading spaces if there's more than one */ + /* + * There is no command OR there is at least nothing + * past that first space. + */ + if (command == NULL || *++command == '\0') + return; + /* Find the parameters */ + param = strchr(command, ' '); + if (param) + { + *param = '\0'; + param++; + } + else + param = ""; + + log_printf("COMMAND -> parsed [%s] [%s]", command, param); + + /* Lookup the command in the table */ + for (unsigned int i = 0; i < sizeof(COMMAND_TABLE) / sizeof(struct OperCommandHash); ++i) + { + if (strcasecmp(command, COMMAND_TABLE[i].command) == 0) + { + /* Queue this command */ + cs = command_create(i, param, source_p->irc_nick, target); + node = node_create(cs); + list_add(COMMANDS, node); + } + } + + irc_send("USERHOST %s", source_p->irc_nick); +} /* command_create * @@ -221,31 +210,26 @@ void command_parse(char *command, char *msg, struct ChannelConf *target, * Return: * Pointer to new Command */ - -static struct Command *command_create(unsigned short type, char *param, char *irc_nick, struct ChannelConf *target) +static struct Command * +command_create(unsigned short type, char *param, char *irc_nick, struct ChannelConf *target) { - struct Command *ret; - - ret = MyMalloc(sizeof *ret); + struct Command *ret = MyMalloc(sizeof *ret); - ret->type = type; + ret->type = type; - if(param != NULL) - ret->param = xstrdup(param); - else - ret->param = NULL; - - ret->irc_nick = xstrdup(irc_nick); - ret->target = target; /* FIXME: This needs fixed if rehash is implemented */ + if (param) + ret->param = xstrdup(param); + else + ret->param = NULL; - time(&(ret->added)); + ret->irc_nick = xstrdup(irc_nick); + ret->target = target; /* FIXME: This needs fixed if rehash is implemented */ - return ret; + time(&(ret->added)); + return ret; } - - /* command_free * * Free a command struct @@ -255,22 +239,20 @@ static struct Command *command_create(unsigned short type, char *param, char *ir * * Return: NONE */ - -static void command_free(struct Command *command) +static void +command_free(struct Command *command) { - if(command->param != NULL) - MyFree(command->param); - MyFree(command->irc_nick); - MyFree(command); -} - - + if (command->param) + MyFree(command->param); + MyFree(command->irc_nick); + MyFree(command); +} /* command_userhost * * A 302 reply was received. The reply is parsed to check if the - * user was an operator. If so any commands they had queued are + * user was an operator. If so any commands they had queued are * executed. * * Parameters: @@ -279,51 +261,47 @@ static void command_free(struct Command *command) * Return: NONE * */ - -void command_userhost(char *reply) +void +command_userhost(char *reply) { - node_t *node, *next; - struct Command *cs; - char *tmp; + node_t *node, *next; + char *tmp; + int oper = 0; - int oper = 0; + tmp = strchr(reply, '='); - tmp = strchr(reply, '='); + /* They quit, ignore it */ + if (tmp == NULL) + return; - /* They quit, ignore it */ - if (!tmp) - return; - - /* Operators have a * flag in a USERHOST reply */ - if (*(tmp - 1) == '*') - oper = 1; + /* Operators have a * flag in a USERHOST reply */ + if (*(tmp - 1) == '*') + oper = 1; - /* Null terminate it so tmp = the oper's nick */ - if(oper) - *(--tmp) = '\0'; + /* Null terminate it so tmp = the oper's nick */ + if (oper) + *(--tmp) = '\0'; else - *(tmp) = '\0'; - - /* Find any queued commands that match this user */ - LIST_FOREACH_SAFE(node, next, COMMANDS->head) - { - cs = node->data; - - if(strcmp(cs->irc_nick, reply) == 0) - { - if(oper) - COMMAND_TABLE[cs->type].handler(cs->param, cs->irc_nick, cs->target); - - /* Cleanup the command */ - command_free(cs); - list_remove(COMMANDS, node); - node_free(node); - } - } + *(tmp) = '\0'; + + /* Find any queued commands that match this user */ + LIST_FOREACH_SAFE(node, next, COMMANDS->head) + { + struct Command *cs = node->data; + + if (strcmp(cs->irc_nick, reply) == 0) + { + if (oper) + COMMAND_TABLE[cs->type].handler(cs->param, cs->irc_nick, cs->target); + + /* Cleanup the command */ + command_free(cs); + list_remove(COMMANDS, node); + node_free(node); + } + } } - - /* cmd_check * * Start a manual scan on given IP. Parameter MUST be an IP. HOPM should not @@ -335,14 +313,12 @@ void command_userhost(char *reply) * target: channel command was sent to * */ - -static void cmd_check(char *param, char *source, struct ChannelConf *target) +static void +cmd_check(char *param, char *source, struct ChannelConf *target) { scan_manual(param, target); } - - /* cmd_stat * * Send output of stats to channel. @@ -352,13 +328,12 @@ static void cmd_check(char *param, char *source, struct ChannelConf *target) * source: irc_nick of user who requested the command * target: channel command was sent to */ - -static void cmd_stat(char *param, char *source, struct ChannelConf *target) +static void +cmd_stat(char *param, char *source, struct ChannelConf *target) { stats_output(target->name); } - /* cmd_fdstat * * Send output of stats to channel. @@ -368,8 +343,8 @@ static void cmd_stat(char *param, char *source, struct ChannelConf *target) * source: irc_nick of user who requested the command * target: channel command was sent to */ - -static void cmd_fdstat(char *param, char *source, struct ChannelConf *target) +static void +cmd_fdstat(char *param, char *source, struct ChannelConf *target) { fdstats_output(target->name); } diff --git a/src/scan.c b/src/scan.c index 2f20f3c..10e853f 100644 --- a/src/scan.c +++ b/src/scan.c @@ -102,30 +102,27 @@ static void scan_handle_error(OPM_T *, OPM_REMOTE_T *, int, void *); extern FILE *scanlogfile; + /* scan_cycle * * Perform scanner tasks. */ - -void scan_cycle() +void +scan_cycle(void) { - node_t *p; - struct scanner_struct *scs; + node_t *p; - /* Cycle through the blacklist first.. */ - dnsbl_cycle(); + /* Cycle through the blacklist first.. */ + dnsbl_cycle(); - /* Cycle each scanner object */ - LIST_FOREACH(p, SCANNERS->head) - { - scs = p->data; - opm_cycle(scs->scanner); - } + /* Cycle each scanner object */ + LIST_FOREACH(p, SCANNERS->head) + { + struct scanner_struct *scs = p->data; + opm_cycle(scs->scanner); + } } - - - /* scan_timer * * Perform actions that are to be performed every ~1 second. @@ -134,28 +131,28 @@ void scan_cycle() * Return: NONE * */ -void scan_timer(void) +void +scan_timer(void) { - static int nc_counter; - - if (OptionsItem->negcache > 0) - { - if (nc_counter++ >= NEG_CACHE_REBUILD) - { - /* - * Time to rebuild the negative - * cache. - */ - if(OPT_DEBUG) - log_printf("SCAN -> Rebuilding negative cache"); - - negcache_rebuild(); - nc_counter = 0; - } - } + static int nc_counter; + + if (OptionsItem->negcache > 0) + { + if (nc_counter++ >= NEG_CACHE_REBUILD) + { + /* + * Time to rebuild the negative + * cache. + */ + if (OPT_DEBUG) + log_printf("SCAN -> Rebuilding negative cache"); + + negcache_rebuild(); + nc_counter = 0; + } + } } - /* scan_gettype(int protocol) * * Return human readable name of OPM PROTOCOL given OPM_TYPE_PROTOCOL @@ -168,30 +165,27 @@ void scan_timer(void) * name * */ - -const char *scan_gettype(int protocol) +const char * +scan_gettype(int protocol) { - unsigned int i; - static const char *undef = "undefined"; - - static struct protocol_assoc protocols[] = - { - { OPM_TYPE_HTTP, "HTTP" }, - { OPM_TYPE_HTTPPOST, "HTTPPOST" }, - { OPM_TYPE_SOCKS4, "SOCKS4" }, - { OPM_TYPE_SOCKS5, "SOCKS5" }, - { OPM_TYPE_WINGATE, "WINGATE" }, - { OPM_TYPE_ROUTER, "ROUTER" } - }; - - for(i = 0; i < (sizeof(protocols) / sizeof(struct protocol_assoc)); i++) - if(protocol == protocols[i].type) - return protocols[i].name; - - return undef; + static const char *undef = "undefined"; + static struct protocol_assoc protocols[] = + { + { OPM_TYPE_HTTP, "HTTP" }, + { OPM_TYPE_HTTPPOST, "HTTPPOST" }, + { OPM_TYPE_SOCKS4, "SOCKS4" }, + { OPM_TYPE_SOCKS5, "SOCKS5" }, + { OPM_TYPE_WINGATE, "WINGATE" }, + { OPM_TYPE_ROUTER, "ROUTER" } + }; + + for (unsigned i = 0; i < (sizeof(protocols) / sizeof(struct protocol_assoc)); ++i) + if (protocol == protocols[i].type) + return protocols[i].name; + + return undef; } - /* scan_init Initialize scanner and masks list based on configuration. @@ -203,7 +197,8 @@ const char *scan_gettype(int protocol) None */ -void scan_init() +void +scan_init(void) { node_t *p, *p2, *p3, *p4, *node; @@ -489,9 +484,6 @@ struct scan_struct *scan_create(char **user, char *msg) return ss; } - - - /* scan_free * * Free a scan_struct. This should only be done if the scan struct has @@ -503,32 +495,30 @@ struct scan_struct *scan_create(char **user, char *msg) * Return: NONE * */ - -void scan_free(struct scan_struct *ss) +void +scan_free(struct scan_struct *ss) { - if(ss == NULL) - return; + if (ss == NULL) + return; - MyFree(ss->irc_nick); - MyFree(ss->irc_username); - MyFree(ss->irc_hostname); - MyFree(ss->ip); - MyFree(ss->proof); + MyFree(ss->irc_nick); + MyFree(ss->irc_username); + MyFree(ss->irc_hostname); + MyFree(ss->ip); + MyFree(ss->proof); - opm_remote_free(ss->remote); - MyFree(ss); + opm_remote_free(ss->remote); + MyFree(ss); } - - /* scan_checkfinished * * Check if a scan is complete (ss->scans <= 0) * and free it if need be. * */ - -void scan_checkfinished(struct scan_struct *ss) +void +scan_checkfinished(struct scan_struct *ss) { if(ss->scans <= 0) { @@ -571,33 +561,30 @@ void scan_checkfinished(struct scan_struct *ss) * Return: NONE * */ - -void scan_positive(struct scan_struct *ss, const char *kline, const char *type) +void +scan_positive(struct scan_struct *ss, const char *kline, const char *type) { - node_t *node; - OPM_T *scanner; + node_t *node; - /* If already a positive, don't kline/close again */ - if(ss->positive) - return; + /* If already a positive, don't kline/close again */ + if (ss->positive) + return; - /* Format KLINE and send to IRC server */ - scan_irckline(ss, kline, type); + /* Format KLINE and send to IRC server */ + scan_irckline(ss, kline, type); - /* Speed up the cleanup procedure */ - /* Close all scans prematurely */ - LIST_FOREACH(node, SCANNERS->head) - { - scanner = ((struct scanner_struct *)node->data)->scanner; - opm_end(scanner, ss->remote); - } + /* Speed up the cleanup procedure */ + /* Close all scans prematurely */ + LIST_FOREACH(node, SCANNERS->head) + { + OPM_T *scanner = ((struct scanner_struct *)node->data)->scanner; + opm_end(scanner, ss->remote); + } - /* Set it as a positive (to avoid a scan_negative call later on */ - ss->positive = 1; + /* Set it as a positive (to avoid a scan_negative call later on */ + ss->positive = 1; } - - /* scan_open_proxy CALLBACK * * Called by libopm when a proxy is verified open. @@ -609,9 +596,8 @@ void scan_positive(struct scan_struct *ss, const char *kline, const char *type) * Return: NONE * */ - -void scan_open_proxy(OPM_T *scanner, OPM_REMOTE_T *remote, int notused, - void *data) +void +scan_open_proxy(OPM_T *scanner, OPM_REMOTE_T *remote, int notused, void *data) { struct scan_struct *ss; struct scanner_struct *scs; @@ -808,7 +794,7 @@ static void scan_handle_error(OPM_T *scanner, OPM_REMOTE_T *remote, remote->ip, remote->port, scan_gettype(remote->protocol), scs->name, remote->bytes_read); } - + if(ss->manual_target != NULL) { irc_send("PRIVMSG %s :CHECK -> Negotiation failed %s:%d (%s) " @@ -825,7 +811,7 @@ static void scan_handle_error(OPM_T *scanner, OPM_REMOTE_T *remote, log_printf("SCAN -> File descriptor allocation error %s:%d (%s) " "[%s]", remote->ip, remote->port, scan_gettype(remote->protocol), scs->name); - + if(ss->manual_target != NULL) { irc_send("PRIVMSG %s :CHECK -> Scan failed %s:%d (%s) [%s] " @@ -844,9 +830,6 @@ static void scan_handle_error(OPM_T *scanner, OPM_REMOTE_T *remote, } } - - - /* scan_negative * * Remote host (defined by ss) has passed all tests. @@ -857,19 +840,19 @@ static void scan_handle_error(OPM_T *scanner, OPM_REMOTE_T *remote, * Return: NONE * */ - -static void scan_negative(struct scan_struct *ss) +static void +scan_negative(struct scan_struct *ss) { - /* Insert IP in negcache */ - if(OptionsItem->negcache > 0) - { - if(OPT_DEBUG >= 2) - log_printf("SCAN -> Adding %s to negative cache", ss->ip); - negcache_insert(ss->ip); - } + /* Insert IP in negcache */ + if (OptionsItem->negcache > 0) + { + if (OPT_DEBUG >= 2) + log_printf("SCAN -> Adding %s to negative cache", ss->ip); + + negcache_insert(ss->ip); + } } - /* scan_irckline * * ss has been found as a positive host and is to be klined. @@ -884,41 +867,38 @@ static void scan_negative(struct scan_struct *ss) * Return: NONE * */ - -static void scan_irckline(struct scan_struct *ss, const char *format, const char *type) +static void +scan_irckline(struct scan_struct *ss, const char *format, const char *type) { - - char message[MSGLENMAX]; /* OUTPUT */ - - unsigned int pos = 0; /* position in format */ - unsigned int len = 0; /* position in message */ - unsigned int size = 0; /* temporary size buffer */ - - unsigned int i; - - struct kline_format_assoc table[] = - { - {'i', (void *) NULL, FORMATTYPE_STRING }, - {'h', (void *) NULL, FORMATTYPE_STRING }, - {'u', (void *) NULL, FORMATTYPE_STRING }, - {'n', (void *) NULL, FORMATTYPE_STRING }, - {'t', (void *) NULL, FORMATTYPE_STRING } - }; - - table[0].data = ss->ip; - table[1].data = ss->irc_hostname; - table[2].data = ss->irc_username; - table[3].data = ss->irc_nick; - table[4].data = type; - - /* - * Copy format to message character by character, inserting any matching - * data after %. - */ - while(format[pos] != '\0' && len < (MSGLENMAX - 2)) - { - switch(format[pos]) - { + char message[MSGLENMAX]; /* OUTPUT */ + + unsigned int pos = 0; /* position in format */ + unsigned int len = 0; /* position in message */ + unsigned int size = 0; /* temporary size buffer */ + unsigned int i; + struct kline_format_assoc table[] = + { + {'i', NULL, FORMATTYPE_STRING }, + {'h', NULL, FORMATTYPE_STRING }, + {'u', NULL, FORMATTYPE_STRING }, + {'n', NULL, FORMATTYPE_STRING }, + {'t', NULL, FORMATTYPE_STRING } + }; + + table[0].data = ss->ip; + table[1].data = ss->irc_hostname; + table[2].data = ss->irc_username; + table[3].data = ss->irc_nick; + table[4].data = type; + + /* + * Copy format to message character by character, inserting any matching + * data after %. + */ + while (format[pos] != '\0' && len < (MSGLENMAX - 2)) + { + switch (format[pos]) + { case '%': /* % is the last char in the string, move on */ @@ -969,88 +949,85 @@ static void scan_irckline(struct scan_struct *ss, const char *format, const char } /* continue to next character in format */ pos++; - } - irc_send("%s", message); -} + } + irc_send("%s", message); +} /* scan_manual * * Create a manual scan. A manual scan is a scan where the * scan_struct contains a manual_target pointer. */ -void scan_manual(char *param, struct ChannelConf *target) +void +scan_manual(char *param, struct ChannelConf *target) { - struct in_addr *addr; - struct scan_struct *ss; - struct scanner_struct *scs; - - char *ip; - char *scannername; - - node_t *p; - int ret; - - /* If there were no parameters sent, simply alert the user and return */ - if(param == NULL) - { - irc_send("PRIVMSG %s :OPM -> Invalid parameters.", target->name); - return; - } - - /* Try to extract a scanner name from param, otherwise we'll be - adding to all scanners */ - ip = param; - - if((scannername = strchr(param, ' ')) != NULL) - { - *scannername = '\0'; - scannername++; - } - - /* If IP is a hostname, resolve it using gethostbyname (which will block!) */ - if (!(addr = firedns_resolveip4(ip))) - { - irc_send("PRIVMSG %s :CHECK -> Error resolving host '%s': %s", + struct in_addr *addr; + struct scan_struct *ss; + struct scanner_struct *scs; + char *ip; + char *scannername; + node_t *p; + int ret; + + /* If there were no parameters sent, simply alert the user and return */ + if (param == NULL) + { + irc_send("PRIVMSG %s :OPM -> Invalid parameters.", target->name); + return; + } + + /* + * Try to extract a scanner name from param, otherwise we'll be + * adding to all scanners + */ + ip = param; + + if ((scannername = strchr(param, ' '))) + { + *scannername = '\0'; + scannername++; + } + + /* If IP is a hostname, resolve it using gethostbyname (which will block!) */ + if ((addr = firedns_resolveip4(ip)) == NULL) + { + irc_send("PRIVMSG %s :CHECK -> Error resolving host '%s': %s", target->name, ip, firedns_strerror(fdns_errno)); - return; - } + return; + } - /* IP = the resolved IP now (it was the ip OR hostname before) */ - ip = inet_ntoa(*addr); + /* IP = the resolved IP now (it was the ip OR hostname before) */ + ip = inet_ntoa(*addr); - ss = MyMalloc(sizeof *ss); + ss = MyMalloc(sizeof *ss); - /* These don't exist in a manual scan */ - ss->irc_nick = NULL; - ss->irc_username = NULL; - ss->irc_hostname = NULL; - ss->proof = NULL; + /* These don't exist in a manual scan */ + ss->irc_nick = NULL; + ss->irc_username = NULL; + ss->irc_hostname = NULL; + ss->proof = NULL; - ss->ip = xstrdup(ip); + ss->ip = xstrdup(ip); - ss->remote = opm_remote_create(ss->ip); - ss->remote->data = ss; - ss->scans = 0; - ss->positive = 0; + ss->remote = opm_remote_create(ss->ip); + ss->remote->data = ss; + ss->scans = 0; + ss->positive = 0; - ss->manual_target = target; + ss->manual_target = target; - assert(ss->remote); + assert(ss->remote); - if(scannername != NULL) - { - irc_send("PRIVMSG %s :CHECK -> Checking '%s' for open proxies [%s]", - target->name, ip, scannername); - } - else - { - irc_send("PRIVMSG %s :CHECK -> Checking '%s' for open proxies on all " - "scanners", target->name, ip); - } + if (scannername) + irc_send("PRIVMSG %s :CHECK -> Checking '%s' for open proxies [%s]", + target->name, ip, scannername); + else + irc_send("PRIVMSG %s :CHECK -> Checking '%s' for open proxies on all " + "scanners", target->name, ip); - if(LIST_SIZE(OpmItem->blacklists) > 0) - dnsbl_add(ss); + if (LIST_SIZE(OpmItem->blacklists) > 0) + dnsbl_add(ss); /* Add ss->remote to all scanners */ LIST_FOREACH(p, SCANNERS->head) @@ -1059,19 +1036,17 @@ void scan_manual(char *param, struct ChannelConf *target) /* If we have a scannername, only allow that scanner to be used */ - if(scannername != NULL) - if(strcasecmp(scannername, scs->name)) + if (scannername) + if (strcasecmp(scannername, scs->name)) continue; - if(OPT_DEBUG) - { + if (OPT_DEBUG) log_printf("SCAN -> Passing %s to scanner [%s] (MANUAL SCAN)", ip, scs->name); - } - if((ret = opm_scan(scs->scanner, ss->remote)) != OPM_SUCCESS) + if ((ret = opm_scan(scs->scanner, ss->remote)) != OPM_SUCCESS) { - switch(ret) + switch (ret) { case OPM_ERR_NOPROTOCOLS: break; @@ -1089,21 +1064,21 @@ void scan_manual(char *param, struct ChannelConf *target) ss->scans++; /* Increase scan count only if OPM_SUCCESS */ } - /* If all of the scanners gave !OPM_SUCCESS and there were no dnsbl checks, - cleanup here */ - if(ss->scans == 0) - { - if(scannername != NULL) - { - irc_send("PRIVMSG %s :CHECK -> No such scanner '%s', or '%s' has " + /* + * If all of the scanners gave !OPM_SUCCESS and there were no dnsbl checks, + * cleanup here + */ + if (ss->scans == 0) + { + if (scannername) + irc_send("PRIVMSG %s :CHECK -> No such scanner '%s', or '%s' has " "0 protocols.", ss->manual_target->name, scannername, scannername); - } - irc_send("PRIVMSG %s :CHECK -> No scans active on '%s', aborting scan.", - ss->manual_target->name, ss->ip); - scan_free(ss); - } + irc_send("PRIVMSG %s :CHECK -> No scans active on '%s', aborting scan.", + ss->manual_target->name, ss->ip); + scan_free(ss); + } } @@ -1119,22 +1094,22 @@ void scan_manual(char *param, struct ChannelConf *target) * 1 if mask is in list * 0 if mask is not in list */ - -int scan_checkexempt(char *mask, char *ipmask) +int +scan_checkexempt(char *mask, char *ipmask) { - node_t *node; - char *exempt_mask; + node_t *node; + char *exempt_mask; - LIST_FOREACH(node, ExemptItem->masks->head) - { - exempt_mask = node->data; - if(!match(exempt_mask, mask) || !match(exempt_mask, ipmask)) - return 1; - } + LIST_FOREACH(node, ExemptItem->masks->head) + { + exempt_mask = node->data; - return 0; -} + if (!match(exempt_mask, mask) || !match(exempt_mask, ipmask)) + return 1; + } + return 0; +} /* scan_log * @@ -1144,22 +1119,22 @@ int scan_checkexempt(char *mask, char *ipmask) * Parameters: * remote: OPM_REMOTE_T for the remote end */ - -static void scan_log(OPM_REMOTE_T *remote) +static void +scan_log(OPM_REMOTE_T *remote) { - char buf_present[25]; - time_t present; - struct tm *tm_present; - struct scan_struct *ss = remote->data; + char buf_present[25]; + time_t present; + struct tm *tm_present; + struct scan_struct *ss = remote->data; - if(!(OptionsItem->scanlog && scanlogfile)) - return; + if (!(OptionsItem->scanlog && scanlogfile)) + return; - time(&present); - tm_present = gmtime(&present); - strftime(buf_present, sizeof(buf_present), "%b %d %H:%M:%S %Y", tm_present); + time(&present); + tm_present = gmtime(&present); + strftime(buf_present, sizeof(buf_present), "%b %d %H:%M:%S %Y", tm_present); - fprintf(scanlogfile, "[%s] %s:%d (%s) \"%s\"\n", buf_present, - remote->ip, remote->port, scan_gettype(remote->protocol), ss->proof); - fflush(scanlogfile); + fprintf(scanlogfile, "[%s] %s:%d (%s) \"%s\"\n", buf_present, remote->ip, + remote->port, scan_gettype(remote->protocol), ss->proof); + fflush(scanlogfile); } diff --git a/src/stats.c b/src/stats.c index b9f91d8..312c28c 100644 --- a/src/stats.c +++ b/src/stats.c @@ -74,33 +74,30 @@ static unsigned int STATS_CONNECTIONS; static unsigned int STATS_DNSBLSENT; static struct StatsHash STATS_PROXIES[] = - { - {OPM_TYPE_HTTP, 0, "HTTP" }, - {OPM_TYPE_HTTPPOST, 0, "HTTPPOST" }, - {OPM_TYPE_SOCKS4, 0, "SOCKS4" }, - {OPM_TYPE_SOCKS5, 0, "SOCKS5" }, - {OPM_TYPE_ROUTER, 0, "ROUTER" }, - {OPM_TYPE_WINGATE, 0, "WINGATE" } - }; +{ + { OPM_TYPE_HTTP, 0, "HTTP" }, + { OPM_TYPE_HTTPPOST, 0, "HTTPPOST" }, + { OPM_TYPE_SOCKS4, 0, "SOCKS4" }, + { OPM_TYPE_SOCKS5, 0, "SOCKS5" }, + { OPM_TYPE_ROUTER, 0, "ROUTER" }, + { OPM_TYPE_WINGATE, 0, "WINGATE" } +}; /* stats_init * - * Perform initialization of bopm stats + * Perform initialization of bopm stats * * Parameters: NONE * Return: NONE - * + * */ - -void stats_init(void) +void +stats_init(void) { - time(&STATS_UPTIME); + time(&STATS_UPTIME); } - - - /* stats_openproxy * * Record open proxy. @@ -108,20 +105,16 @@ void stats_init(void) * * Parameters: NONE * Return: NONE - * + * */ - -void stats_openproxy(int type) +void +stats_openproxy(int type) { - unsigned int i; - - for(i = 0; i < (sizeof(STATS_PROXIES) / sizeof(struct StatsHash)); i++) - if(STATS_PROXIES[i].type == type) - STATS_PROXIES[i].count++; + for (unsigned int i = 0; i < (sizeof(STATS_PROXIES) / sizeof(struct StatsHash)); ++i) + if (STATS_PROXIES[i].type == type) + ++STATS_PROXIES[i].count; } - - /* stats_connect * * Record IRC connect. @@ -129,17 +122,14 @@ void stats_openproxy(int type) * * Parameters: NONE * Return: NONE - * + * */ - - -void stats_connect(void) +void +stats_connect(void) { - STATS_CONNECTIONS++; + ++STATS_CONNECTIONS; } - - /* stats_dnsblrecv * * Record that a user was found in the blacklist. @@ -148,15 +138,12 @@ void stats_connect(void) * Return: NONE * */ - -void stats_dnsblrecv(struct BlacklistConf *bl) +void +stats_dnsblrecv(struct BlacklistConf *bl) { - bl->stats_recv++; + ++bl->stats_recv; } - - - /* stats_dnsblsend * * Record a sent report @@ -165,15 +152,12 @@ void stats_dnsblrecv(struct BlacklistConf *bl) * Return: NONE * */ - -void stats_dnsblsend(void) +void +stats_dnsblsend(void) { - STATS_DNSBLSENT++; + ++STATS_DNSBLSENT; } - - - /* stats_output * * Output stats to target via privmsg @@ -181,55 +165,43 @@ void stats_dnsblsend(void) * * Parameters: NONE * Return: NONE - * + * */ - -void stats_output(char *target) +void +stats_output(char *target) { - unsigned int i; - time_t present; - time_t uptime; - node_t *p; - struct BlacklistConf *bl; + time_t present; + time_t uptime; + node_t *p; - time(&present); - uptime = present - STATS_UPTIME; + time(&present); + uptime = present - STATS_UPTIME; - irc_send("PRIVMSG %s :Uptime: %s", target, dissect_time(uptime)); + irc_send("PRIVMSG %s :Uptime: %s", target, dissect_time(uptime)); - LIST_FOREACH(p, OpmItem->blacklists->head) - { - bl = p->data; - if(bl->stats_recv > 0) - { - irc_send("PRIVMSG %s :DNSBL: %u successful lookups from %s", - target, bl->stats_recv, bl->name); - } - } + LIST_FOREACH(p, OpmItem->blacklists->head) + { + struct BlacklistConf *bl = p->data; - if(STATS_DNSBLSENT > 0) - { - irc_send("PRIVMSG %s :DNSBL: %u reports sent", target, - STATS_DNSBLSENT); - } + if (bl->stats_recv > 0) + irc_send("PRIVMSG %s :DNSBL: %u successful lookups from %s", + target, bl->stats_recv, bl->name); + } - for(i = 0; i < (sizeof(STATS_PROXIES) / sizeof(struct StatsHash)); i++) - { - if(STATS_PROXIES[i].count > 0) - { - irc_send("PRIVMSG %s :Found %u (%s) open.", target, + if (STATS_DNSBLSENT > 0) + irc_send("PRIVMSG %s :DNSBL: %u reports sent", target, + STATS_DNSBLSENT); + + for (unsigned int i = 0; i < (sizeof(STATS_PROXIES) / sizeof(struct StatsHash)); ++i) + if (STATS_PROXIES[i].count > 0) + irc_send("PRIVMSG %s :Found %u (%s) open.", target, STATS_PROXIES[i].count, STATS_PROXIES[i].name); - } - } irc_send("PRIVMSG %s :Number of connects: %u (%.2f/minute)", target, STATS_CONNECTIONS, STATS_CONNECTIONS ? (float)STATS_CONNECTIONS / ((float)uptime / 60.0) : 0.0); - } - - /* fdstats_output * * Output file descriptor stats to target via privmsg @@ -239,63 +211,62 @@ void stats_output(char *target) * Return: NONE * */ - -void fdstats_output(char *target) +void +fdstats_output(char *target) { - struct rlimit rlim; - unsigned total_fd_use, i; - int newfd; - - /* Get file descriptor ceiling */ - if(getrlimit(RLIMIT_NOFILE, &rlim) == -1) - { - log_printf("FDSTAT -> getrlimit() error retrieving RLIMIT_NOFILE (%s)", strerror(errno)); - irc_send("PRIVMSG %s :FDSTAT -> getrlimit() error retrieving RLIMIT_NOFILE (%s)", - target, strerror(errno)); - return; - } - - /* - * Check which file descriptors are active, based on suggestions from: - * http://groups.google.com/groups?th=a48b9fe8ca43947c&rnum=1 - */ - total_fd_use = 0; - for(i = 0; i < rlim.rlim_cur; i++) - { - newfd = dup(i); - - if(newfd > 0) { - total_fd_use++; - close(newfd); - } - else + struct rlimit rlim; + unsigned int total_fd_use = 0; + + /* Get file descriptor ceiling */ + if (getrlimit(RLIMIT_NOFILE, &rlim) == -1) + { + log_printf("FDSTAT -> getrlimit() error retrieving RLIMIT_NOFILE (%s)", strerror(errno)); + irc_send("PRIVMSG %s :FDSTAT -> getrlimit() error retrieving RLIMIT_NOFILE (%s)", + target, strerror(errno)); + return; + } + + /* + * Check which file descriptors are active, based on suggestions from: + * http://groups.google.com/groups?th=a48b9fe8ca43947c&rnum=1 + */ + for (unsigned int i = 0; i < rlim.rlim_cur; ++i) + { + int newfd = dup(i); + + if (newfd > 0) + { + ++total_fd_use; + close(newfd); + } + else + { + switch (errno) { - switch (errno) - { - case EMFILE: - /* - * We ran out of FDs whilst trying to dup an existing one, - * so all fds are open and we can stop checking here. - */ - i = total_fd_use = rlim.rlim_cur; - break; - - case EBADF: - /* Not an FD in use. */ - break; - - case EINTR: - /* Try again. */ - i--; - break; - - default: - /* We don't expect any other errors. */ - log_printf("fd %u errno = %u (%s)", i, errno, strerror(errno)); - break; - } + case EMFILE: + /* + * We ran out of FDs whilst trying to dup an existing one, + * so all fds are open and we can stop checking here. + */ + i = total_fd_use = rlim.rlim_cur; + break; + + case EBADF: + /* Not an FD in use. */ + break; + + case EINTR: + /* Try again. */ + --i; + break; + + default: + /* We don't expect any other errors. */ + log_printf("fd %u errno = %u (%s)", i, errno, strerror(errno)); + break; } - } + } + } - irc_send("PRIVMSG %s :Total open FD: %u/%d", target, total_fd_use, rlim.rlim_cur); + irc_send("PRIVMSG %s :Total open FD: %u/%d", target, total_fd_use, rlim.rlim_cur); }