0.1.7 fixes

This commit is contained in:
SJ
2011-11-23 12:24:21 +01:00
parent 84e75fd9cf
commit 072caae0c9
13 changed files with 842 additions and 36 deletions

View File

@ -33,7 +33,7 @@ MAKE = `which make`
INSTALL = @INSTALL@
all: libpiler.a piler pilerconf test
all: libpiler.a piler pilerconf pilerdecrypt test
install: install-piler
@ -49,13 +49,13 @@ libpiler.a: $(OBJS) $(MYSQL_OBJS)
pilerdecrypt: pilerdecrypt.c cfg.o misc.o tai.o store.o
$(CC) $(CFLAGS) $(INCDIR) $(DEFS) -o $@ $^ $(LIBS) $(LIBDIR)
$(CC) $(CFLAGS) $(INCDIR) $(DEFS) -o $@ $^ -lcrypto -lz $(LIBDIR)
pilerconf: pilerconf.c cfg.o misc.o tai.o
$(CC) $(CFLAGS) $(INCDIR) $(DEFS) -o $@ $^ $(LIBS) $(LIBDIR)
$(CC) $(CFLAGS) $(INCDIR) $(DEFS) -o $@ $^ $(LIBDIR)
test:
$(CC) $(CFLAGS) $(INCDIR) $(DEFS) -o piletest $(srcdir)/test.c -lpiler $(LIBS) $(LDAP_LIBS) $(LIBDIR) @LDFLAGS@
$(CC) $(CFLAGS) $(INCDIR) $(DEFS) -o pilertest $(srcdir)/test.c -lpiler $(LIBS) $(LDAP_LIBS) $(LIBDIR) @LDFLAGS@
%.o: $(srcdir)/%.c
$(CC) $(CFLAGS) -fPIC $(INCDIR) $(DEFS) -c $< -o $@
@ -78,7 +78,7 @@ install-piler:
$(INSTALL) -m 0755 pilerconf $(DESTDIR)$(sbindir)
clean:
rm -f *.o *.a libpiler.so* piler pilerconf piletest
rm -f *.o *.a libpiler.so* piler pilerconf pilertest
distclean: clean
rm -f Makefile

View File

@ -23,6 +23,25 @@ int store_attachments(struct session_data *sdata, struct _state *state, struct _
MYSQL_RES *res;
MYSQL_ROW row;
MYSQL_STMT *stmt;
MYSQL_BIND bind[6];
unsigned long len[6];
stmt = mysql_stmt_init(&(sdata->mysql));
if(!stmt){
if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "%s: %s.mysql_stmt_init() error", sdata->ttmpfile, SQL_ATTACHMENT_TABLE);
return 1;
}
snprintf(s, sizeof(s)-1, "INSERT INTO %s (`piler_id`,`attachment_id`,`sig`,`type`,`size`,`ptr`) VALUES(?,?,?,?,?,?)", SQL_ATTACHMENT_TABLE);
if(mysql_stmt_prepare(stmt, s, strlen(s))){
syslog(LOG_PRIORITY, "%s: %s.mysql_stmt_prepare() error: %s", sdata->ttmpfile, SQL_ATTACHMENT_TABLE, mysql_stmt_error(stmt));
return 1;
}
for(i=1; i<=state->n_attachments; i++){
found = 0;
id = 0;
@ -53,9 +72,47 @@ int store_attachments(struct session_data *sdata, struct _state *state, struct _
}
}
snprintf(s, sizeof(s)-1, "INSERT INTO %s (`piler_id`,`attachment_id`,`sig`,`ptr`) VALUES('%s',%d,'%s',%llu)", SQL_ATTACHMENT_TABLE, sdata->ttmpfile, i, state->attachments[i].digest, id);
if(mysql_real_query(&(sdata->mysql), s, strlen(s))){
memset(bind, 0, sizeof(bind));
bind[0].buffer_type = MYSQL_TYPE_STRING;
bind[0].buffer = sdata->ttmpfile;
bind[0].is_null = 0;
len[0] = strlen(sdata->ttmpfile); bind[0].length = &len[0];
bind[1].buffer_type = MYSQL_TYPE_LONG;
bind[1].buffer = (char *)&i;
bind[1].is_null = 0;
bind[1].length = 0;
bind[2].buffer_type = MYSQL_TYPE_STRING;
bind[2].buffer = state->attachments[i].digest;
bind[2].is_null = 0;
len[2] = strlen(state->attachments[i].digest); bind[2].length = &len[2];
bind[3].buffer_type = MYSQL_TYPE_STRING;
bind[3].buffer = state->attachments[i].type;
bind[3].is_null = 0;
len[3] = strlen(state->attachments[i].digest); bind[3].length = &len[3];
bind[4].buffer_type = MYSQL_TYPE_LONG;
bind[4].buffer = (char *)&(state->attachments[i].size);
bind[4].is_null = 0;
bind[4].length = 0;
bind[5].buffer_type = MYSQL_TYPE_LONGLONG;
bind[5].buffer = (char *)&id;
bind[5].is_null = 0;
bind[5].length = 0;
if(mysql_stmt_bind_param(stmt, bind)){
syslog(LOG_PRIORITY, "%s: %s.mysql_stmt_bind_param() error: %s", sdata->ttmpfile, SQL_ATTACHMENT_TABLE, mysql_stmt_error(stmt));
return 1;
}
if(mysql_stmt_execute(stmt)){
syslog(LOG_PRIORITY, "%s attachment sql error: *%s*", sdata->ttmpfile, mysql_error(&(sdata->mysql)));
return 1;
}

View File

@ -11,7 +11,7 @@
#define PROGNAME "piler"
#define VERSION "0.1.6"
#define VERSION "0.1.7"
#define PROGINFO VERSION ", Janos SUTO <sj@acts.hu>\n\n" CONFIGURE_PARAMS "\n\nSend bugs/issues to https://jira.acts.hu:8443/\n"

View File

@ -70,9 +70,13 @@ struct rule {
regex_t from;
regex_t to;
regex_t subject;
regex_t attachment_type;
#endif
int size;
char _size[4];
int attachment_size;
char _attachment_size[4];
char *rulestr;
char compiled;
struct rule *r;

View File

@ -247,14 +247,18 @@ int processMessage(struct session_data *sdata, struct _state *state, struct __co
*/
/* store base64 encoded file attachments */
rc = store_attachments(sdata, state, cfg);
if(state->n_attachments > 0){
rc = store_attachments(sdata, state, cfg);
for(i=1; i<=state->n_attachments; i++){
unlink(state->attachments[i].internalname);
for(i=1; i<=state->n_attachments; i++){
unlink(state->attachments[i].internalname);
}
if(rc) return ERR;
}
if(rc) return ERR;
rc = store_file(sdata, sdata->tmpframe, 0, 0, cfg);
if(rc == 0){

View File

@ -115,10 +115,25 @@ int parse_line(char *buf, struct _state *state, struct session_data *sdata, stru
//printf("DUMP FILE: %s\n", state->attachments[state->n_attachments].internalname);
state->fd = open(state->attachments[state->n_attachments].internalname, O_CREAT|O_RDWR, S_IRUSR|S_IWUSR);
if(state->fd == -1){
snprintf(puf, sizeof(puf)-1, "ATTACHMENT_POINTER_%s.a%d", sdata->ttmpfile, state->n_attachments);
write(state->mfd, puf, strlen(puf));
//printf("%s", puf);
state->attachments[state->n_attachments].size = 0;
memset(state->attachments[state->n_attachments].type, 0, TINYBUFSIZE);
memset(state->attachments[state->n_attachments].filename, 0, TINYBUFSIZE);
memset(state->attachments[state->n_attachments].internalname, 0, TINYBUFSIZE);
memset(state->attachments[state->n_attachments].digest, 0, 2*DIGEST_LENGTH+1);
syslog(LOG_PRIORITY, "%s: error opening %s", sdata->ttmpfile, state->attachments[state->n_attachments].internalname);
state->n_attachments--;
state->has_to_dump = 0;
}
else {
snprintf(puf, sizeof(puf)-1, "ATTACHMENT_POINTER_%s.a%d", sdata->ttmpfile, state->n_attachments);
write(state->mfd, puf, strlen(puf));
//printf("%s", puf);
}
}
else {
state->has_to_dump = 0;

View File

@ -15,13 +15,13 @@ void load_archiving_rules(struct session_data *sdata, struct rule **rules){
MYSQL_RES *res;
MYSQL_ROW row;
snprintf(s, sizeof(s)-1, "SELECT `from`, `to`, `subject`, `_size`, `size` FROM `%s`", SQL_ARCHIVING_RULE_TABLE);
snprintf(s, sizeof(s)-1, "SELECT `from`, `to`, `subject`, `_size`, `size`, `attachment_type`, `_attachment_size`, `attachment_size` FROM `%s`", SQL_ARCHIVING_RULE_TABLE);
if(mysql_real_query(&(sdata->mysql), s, strlen(s)) == 0){
res = mysql_store_result(&(sdata->mysql));
if(res != NULL){
while((row = mysql_fetch_row(res))){
append_rule(rules, (char*)row[0], (char*)row[1], (char*)row[2], (char*)row[3], atoi(row[4]));
append_rule(rules, (char*)row[0], (char*)row[1], (char*)row[2], (char*)row[3], atoi(row[4]), (char*)row[5], (char*)row[6], atoi(row[7]));
}
mysql_free_result(res);
@ -32,7 +32,7 @@ void load_archiving_rules(struct session_data *sdata, struct rule **rules){
}
int append_rule(struct rule **rule, char *from, char *to, char *subject, char *_size, int size){
int append_rule(struct rule **rule, char *from, char *to, char *subject, char *_size, int size, char *attachment_type, char *_attachment_size, int attachment_size){
struct rule *q, *t, *u=NULL;
q = *rule;
@ -42,7 +42,7 @@ int append_rule(struct rule **rule, char *from, char *to, char *subject, char *_
q = q->r;
}
t = create_rule_item(from, to, subject, _size, size);
t = create_rule_item(from, to, subject, _size, size, attachment_type, _attachment_size, attachment_size);
if(t){
if(*rule == NULL)
*rule = t;
@ -56,7 +56,7 @@ int append_rule(struct rule **rule, char *from, char *to, char *subject, char *_
}
struct rule *create_rule_item(char *from, char *to, char *subject, char *_size, int size){
struct rule *create_rule_item(char *from, char *to, char *subject, char *_size, int size, char *attachment_type, char *_attachment_size, int attachment_size){
struct rule *h=NULL;
char empty = '\0';
int len;
@ -81,10 +81,22 @@ struct rule *create_rule_item(char *from, char *to, char *subject, char *_size,
if(!_size) _size = &empty;
snprintf(h->_size, 3, "%s", _size);
len = strlen(from)+6 + strlen(to)+4 + strlen(subject)+9 + strlen(_size)+6 + 15;
if(!attachment_type) attachment_type = &empty;
if(regcomp(&(h->attachment_type), attachment_type, REG_ICASE | REG_EXTENDED)) h->compiled = 0;
h->attachment_size = attachment_size;
if(!_attachment_size) _attachment_size = &empty;
snprintf(h->_attachment_size, 3, "%s", _attachment_size);
len = strlen(from)+6 + strlen(to)+4 + strlen(subject)+9 + strlen(_size)+6 + strlen(attachment_type)+10 + strlen(_attachment_size)+10 + 15 + 15;
h->rulestr = malloc(len);
if(h->rulestr) snprintf(h->rulestr, len-1, "from=%s,to=%s,subject=%s,size%s%d", from, to, subject, _size, size);
if(h->rulestr) snprintf(h->rulestr, len-1, "from=%s,to=%s,subject=%s,size%s%d,att.type=%s,att.size%s%d", from, to, subject, _size, size, attachment_type, _attachment_size, attachment_size);
else h->compiled = 0;
h->r = NULL;
@ -93,7 +105,7 @@ struct rule *create_rule_item(char *from, char *to, char *subject, char *_size,
}
char *check_againt_ruleset(struct rule *rule, char *from, char *to, char *subject, int size){
char *check_againt_ruleset(struct rule *rule, struct _state *state, int size){
size_t nmatch=0;
struct rule *p;
@ -103,10 +115,11 @@ char *check_againt_ruleset(struct rule *rule, char *from, char *to, char *subjec
if(
p->compiled == 1 &&
regexec(&(p->from), from, nmatch, NULL, 0) == 0 &&
regexec(&(p->to), to, nmatch, NULL, 0) == 0 &&
regexec(&(p->subject), subject, nmatch, NULL, 0) == 0 &&
check_size_rule(size, p->size, p->_size) == 1
regexec(&(p->from), state->b_from, nmatch, NULL, 0) == 0 &&
regexec(&(p->to), state->b_to, nmatch, NULL, 0) == 0 &&
regexec(&(p->subject), state->b_subject, nmatch, NULL, 0) == 0 &&
check_size_rule(size, p->size, p->_size) == 1 &&
check_attachment_rule(state, p) == 1
){
return p->rulestr;
}
@ -130,6 +143,23 @@ int check_size_rule(int message_size, int size, char *_size){
}
int check_attachment_rule(struct _state *state, struct rule *rule){
int i;
size_t nmatch=0;
for(i=1; i<=state->n_attachments; i++){
if(
regexec(&(rule->attachment_type), state->attachments[i].type, nmatch, NULL, 0) == 0 &&
check_size_rule(state->attachments[i].size, rule->attachment_size, rule->_attachment_size) == 1
){
return 1;
}
}
return 0;
}
void free_rule(struct rule *rule){
struct rule *p, *q;
@ -141,6 +171,8 @@ void free_rule(struct rule *rule){
if(p){
regfree(&(p->from));
regfree(&(p->to));
regfree(&(p->attachment_type));
free(p->rulestr);
free(p);

View File

@ -8,10 +8,11 @@
#include "defs.h"
void load_archiving_rules(struct session_data *sdata, struct rule **rules);
int append_rule(struct rule **rule, char *from, char *to, char *subject, char *_size, int size);
struct rule *create_rule_item(char *from, char *to, char *subject, char *_size, int size);
char *check_againt_ruleset(struct rule *rule, char *from, char *to, char *subject, int size);
int append_rule(struct rule **rule, char *from, char *to, char *subject, char *_size, int size, char *attachment_type, char *_attachment_size, int attachment_size);
struct rule *create_rule_item(char *from, char *to, char *subject, char *_size, int size, char *attachment_type, char *_attachment_size, int attachment_size);
char *check_againt_ruleset(struct rule *rule, struct _state *state, int size);
int check_size_rule(int message_size, int size, char *_size);
int check_attachment_rule(struct _state *state, struct rule *rule);
void free_rule(struct rule *rule);
#endif /* _RULES_H */

View File

@ -189,10 +189,10 @@ void handle_smtp_session(int new_sd, struct __data *data, struct __config *cfg){
/* check message against archiving rules */
arule = check_againt_ruleset(data->rules, sstate.b_from, sstate.b_to, sstate.b_subject, sdata.tot_len);
arule = check_againt_ruleset(data->rules, &sstate, sdata.tot_len);
if(arule){
syslog(LOG_PRIORITY, "%s: discarding message by policy: *%s*", sdata.ttmpfile, arule);
syslog(LOG_PRIORITY, "%s: discarding message by archiving policy: *%s*", sdata.ttmpfile, arule);
inj = OK;
}
else {

View File

@ -71,11 +71,11 @@ int main(int argc, char **argv){
//printf("body: *%s*\n", state.b_body);
make_body_digest(&sdata, &cfg);
rule = check_againt_ruleset(data.rules, state.b_from, state.b_to, state.b_subject, st.st_size);
rule = check_againt_ruleset(data.rules, &state, st.st_size);
//printf("body digest: %s\n", sdata.bodydigest);
//printf("rules check: %s\n", rule);
printf("rules check: %s\n", rule);
mysql_close(&(sdata.mysql));