From 8e9f3d066a9ec993dbcae096c86d756b5807ab6d Mon Sep 17 00:00:00 2001 From: SJ Date: Wed, 30 Nov 2011 23:42:15 +0100 Subject: [PATCH] pilerget fixes --- src/Makefile.in | 6 +- src/config.h | 2 +- src/parser.c | 1 + src/pilerdecrypt.c | 74 ----------------- src/pilerget.c | 194 +++++++++++++++++++++++++++++++++++++++++++++ util/db-mysql.sql | 14 +++- 6 files changed, 211 insertions(+), 80 deletions(-) delete mode 100644 src/pilerdecrypt.c create mode 100644 src/pilerget.c diff --git a/src/Makefile.in b/src/Makefile.in index f55cc1ba..237f190b 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -33,7 +33,7 @@ MAKE = `which make` INSTALL = @INSTALL@ -all: libpiler.a piler pilerconf pilerdecrypt test +all: libpiler.a piler pilerconf pilerget test install: install-piler @@ -48,7 +48,7 @@ libpiler.a: $(OBJS) $(MYSQL_OBJS) ln -sf libpiler.so.$(LIBPILER_VERSION) libpiler.so.$(PILER_VERSION) -pilerdecrypt: pilerdecrypt.c cfg.o misc.o tai.o store.o +pilerget: pilerget.c cfg.o misc.o tai.o store.o $(CC) $(CFLAGS) $(INCDIR) $(DEFS) -o $@ $^ -lcrypto -lz $(LIBDIR) pilerconf: pilerconf.c cfg.o misc.o tai.o @@ -78,7 +78,7 @@ install-piler: $(INSTALL) -m 0755 pilerconf $(DESTDIR)$(sbindir) clean: - rm -f *.o *.a libpiler.so* piler pilerconf pilertest + rm -f *.o *.a libpiler.so* piler pilerconf pilerget pilertest distclean: clean rm -f Makefile diff --git a/src/config.h b/src/config.h index 6d277b83..6a38416f 100644 --- a/src/config.h +++ b/src/config.h @@ -31,7 +31,7 @@ #define SESSION_TIMEOUT 420 #define MAXBUFSIZE 8192 #define SMALLBUFSIZE 512 -#define BIGBUFSIZE 65535 +#define BIGBUFSIZE 65536 #define TINYBUFSIZE 128 #define MAXVAL 256 #define RANDOM_POOL "/dev/urandom" diff --git a/src/parser.c b/src/parser.c index d75cc6a2..e17abab7 100644 --- a/src/parser.c +++ b/src/parser.c @@ -51,6 +51,7 @@ struct _state parse_message(struct session_data *sdata, struct __config *cfg){ trimBuffer(state.b_subject); fixupEncodedHeaderLine(state.b_subject); + state.message_state = MSG_SUBJECT; translateLine((unsigned char*)&state.b_subject, &state); diff --git a/src/pilerdecrypt.c b/src/pilerdecrypt.c deleted file mode 100644 index 65a560ab..00000000 --- a/src/pilerdecrypt.c +++ /dev/null @@ -1,74 +0,0 @@ -/* - * pilerdecrypt.c, SJ - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - - -char *configfile = CONFIG_FILE; - - -int main(int argc, char **argv){ - int fd, n, olen, tlen; - unsigned char inbuf[BIGBUFSIZE], outbuf[BIGBUFSIZE+EVP_MAX_BLOCK_LENGTH]; - EVP_CIPHER_CTX ctx; - struct __config cfg; - - cfg = read_config(configfile); - - if(read_key(&cfg)){ - printf("%s\n", ERR_READING_KEY); - return 1; - } - - - EVP_CIPHER_CTX_init(&ctx); - EVP_DecryptInit_ex(&ctx, EVP_bf_cbc(), NULL, cfg.key, cfg.iv); - - if(argc != 2){ - printf("usage: $0 \n"); - return 1; - } - - fd = open(argv[1], O_RDONLY); - if(fd == -1){ - printf("error reading file: %s\n", argv[0]); - return 1; - } - - - while((n = read(fd, inbuf, sizeof(inbuf)))){ - bzero(&outbuf, sizeof(outbuf)); - - if(EVP_DecryptUpdate(&ctx, outbuf, &olen, inbuf, n) != 1){ - return 0; - } - - if(EVP_DecryptFinal(&ctx, outbuf + olen, &tlen) != 1){ - return 0; - } - - olen += tlen; - - write(1, outbuf, olen); - } - - EVP_CIPHER_CTX_cleanup(&ctx); - - close(fd); - - return 0; -} diff --git a/src/pilerget.c b/src/pilerget.c new file mode 100644 index 00000000..8cedee67 --- /dev/null +++ b/src/pilerget.c @@ -0,0 +1,194 @@ +/* + * pilerget.c, SJ + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +char *configfile = CONFIG_FILE; + +int fd=-1; +EVP_CIPHER_CTX ctx; +unsigned char *s=NULL; + + +void clean_exit(){ + if(s) free(s); + + EVP_CIPHER_CTX_cleanup(&ctx); + + if(fd != -1) close(fd); + + exit(0); +} + + +void zerr(int ret) +{ + fputs("zpipe: ", stderr); + switch (ret) { + case Z_ERRNO: + if (ferror(stdin)) + fputs("error reading stdin\n", stderr); + if (ferror(stdout)) + fputs("error writing stdout\n", stderr); + break; + case Z_STREAM_ERROR: + fputs("invalid compression level\n", stderr); + break; + case Z_DATA_ERROR: + fputs("invalid or incomplete deflate data\n", stderr); + break; + case Z_MEM_ERROR: + fputs("out of memory\n", stderr); + break; + case Z_VERSION_ERROR: + fputs("zlib version mismatch!\n", stderr); + } +} + + +int inf(unsigned char *in, int len, FILE *dest){ + int ret; + unsigned have; + z_stream strm; + unsigned char out[BIGBUFSIZE]; + + /* allocate inflate state */ + strm.zalloc = Z_NULL; + strm.zfree = Z_NULL; + strm.opaque = Z_NULL; + strm.avail_in = 0; + strm.next_in = Z_NULL; + ret = inflateInit(&strm); + + + if(ret != Z_OK) return ret; + + strm.avail_in = len; + strm.next_in = in; + + do { + strm.avail_out = BIGBUFSIZE; + strm.next_out = out; + ret = inflate(&strm, Z_NO_FLUSH); + + assert(ret != Z_STREAM_ERROR); /* state not clobbered */ + switch (ret) { + case Z_NEED_DICT: + ret = Z_DATA_ERROR; /* and fall through */ + case Z_DATA_ERROR: + case Z_MEM_ERROR: + (void)inflateEnd(&strm); + return ret; + } + + have = BIGBUFSIZE - strm.avail_out; + if(fwrite(out, 1, have, dest) != have){ + (void)inflateEnd(&strm); + return Z_ERRNO; + } + + } while (strm.avail_out == 0); + + + (void)inflateEnd(&strm); + + return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR; +} + + +int main(int argc, char **argv){ + int rc, n, olen, tlen, len; + unsigned char inbuf[BIGBUFSIZE]; + struct __config cfg; + struct stat st; + + + cfg = read_config(configfile); + + if(read_key(&cfg)){ + printf("%s\n", ERR_READING_KEY); + return 1; + } + + + if(argc != 2){ + printf("usage: $0 \n"); + return 1; + } + + + fd = open(argv[1], O_RDONLY); + if(fd == -1){ + printf("error reading file: %s\n", argv[1]); + return 1; + } + + + if(fstat(fd, &st)){ + perror("fstat()"); + close(fd); + return 1; + } + + + EVP_CIPHER_CTX_init(&ctx); + EVP_DecryptInit_ex(&ctx, EVP_bf_cbc(), NULL, cfg.key, cfg.iv); + + len = st.st_size+EVP_MAX_BLOCK_LENGTH; + + s = malloc(len); + + if(!s){ + perror("malloc"); + clean_exit(); + } + + + tlen = 0; + + while((n = read(fd, inbuf, sizeof(inbuf)))){ + + if(!EVP_DecryptUpdate(&ctx, s+tlen, &olen, inbuf, n)){ + fprintf(stderr, "EVP_DecryptUpdate()\n"); + clean_exit(); + } + + tlen += olen; + } + + close(fd); + + if(EVP_DecryptFinal(&ctx, s + tlen, &olen) != 1){ + fprintf(stderr, "EVP_DecryptFinal()\n"); + clean_exit(); + } + + EVP_CIPHER_CTX_cleanup(&ctx); + + tlen += olen; + + + rc = inf(s, tlen, stdout); if(rc != Z_OK) zerr(rc); + + + if(s) free(s); + + return 0; +} diff --git a/util/db-mysql.sql b/util/db-mysql.sql index 591630ac..18852614 100644 --- a/util/db-mysql.sql +++ b/util/db-mysql.sql @@ -25,7 +25,7 @@ drop table if exists `metadata`; create table `metadata` ( `id` bigint unsigned not null auto_increment, `from` char(255) not null, - `to` char(255) default null, + `to` text(2048) character set 'latin1' not null, `subject` text(512) default null, `arrived` int not null, `sent` int not null, @@ -37,7 +37,7 @@ create table `metadata` ( `message_id` char(128) character set 'latin1' not null, `digest` char(64) not null, `bodydigest` char(64) not null, - primary key (`id`), unique(`to`,`message_id`) + primary key (`id`), unique(`message_id`) ) Engine=InnoDB; create index metadata_idx on metadata(`piler_id`); @@ -83,3 +83,13 @@ create table if not exists `counter` ( insert into `counter` values(0, 0, 0); +drop table if exists `search`; +create table `search` ( + `email` char(128) not null, + `ts` int default 0, + `term` text(512) not null +) Engine=InnoDB; + +create index `search_idx` on `search`(`email`); + +