pilerget fixes

This commit is contained in:
SJ 2011-11-30 23:42:15 +01:00
parent b806c3548f
commit 8e9f3d066a
6 changed files with 211 additions and 80 deletions

View File

@ -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

View File

@ -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"

View File

@ -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);

View File

@ -1,74 +0,0 @@
/*
* pilerdecrypt.c, SJ
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <syslog.h>
#include <openssl/blowfish.h>
#include <openssl/evp.h>
#include <piler.h>
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 <encrypted file>\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;
}

194
src/pilerget.c Normal file
View File

@ -0,0 +1,194 @@
/*
* pilerget.c, SJ
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <syslog.h>
#include <openssl/blowfish.h>
#include <openssl/evp.h>
#include <zlib.h>
#include <assert.h>
#include <piler.h>
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 <encrypted file>\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;
}

View File

@ -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`);