2011-11-14 15:57:52 +01:00
|
|
|
/*
|
|
|
|
* store.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 <piler.h>
|
|
|
|
#include <zlib.h>
|
|
|
|
#include <openssl/blowfish.h>
|
|
|
|
#include <openssl/evp.h>
|
2012-03-20 10:35:22 +01:00
|
|
|
#include <errno.h>
|
2011-11-14 15:57:52 +01:00
|
|
|
|
|
|
|
|
2011-11-22 12:31:54 +01:00
|
|
|
int read_key(struct __config *cfg){
|
|
|
|
int fd, n;
|
|
|
|
|
|
|
|
fd = open(KEYFILE, O_RDONLY);
|
|
|
|
if(fd == -1){
|
|
|
|
syslog(LOG_PRIORITY, "cannot read keyfile: %s", KEYFILE);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = read(fd, cfg->key, KEYLEN);
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
if(n > 5) return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-19 21:25:44 +01:00
|
|
|
int store_file(struct session_data *sdata, char *filename, int startpos, int len, struct __config *cfg){
|
|
|
|
int ret=0, rc, fd, n;
|
|
|
|
char *addr, *p, *p0, *p1, *p2, s[SMALLBUFSIZE];
|
2011-11-14 15:57:52 +01:00
|
|
|
struct stat st;
|
|
|
|
Bytef *z=NULL;
|
|
|
|
uLongf dstlen;
|
|
|
|
|
|
|
|
EVP_CIPHER_CTX ctx;
|
|
|
|
unsigned char *outbuf=NULL;
|
2013-01-11 11:37:23 +01:00
|
|
|
int outlen, writelen, tmplen;
|
2011-11-14 15:57:52 +01:00
|
|
|
|
|
|
|
struct timezone tz;
|
|
|
|
struct timeval tv1, tv2;
|
|
|
|
|
|
|
|
|
2011-11-19 21:25:44 +01:00
|
|
|
fd = open(filename, O_RDONLY);
|
2011-11-22 12:31:54 +01:00
|
|
|
if(fd == -1){
|
|
|
|
syslog(LOG_PRIORITY, "%s: cannot open: %s", sdata->ttmpfile, filename);
|
|
|
|
return ret;
|
|
|
|
}
|
2011-11-14 15:57:52 +01:00
|
|
|
|
2011-11-19 21:25:44 +01:00
|
|
|
if(len == 0){
|
|
|
|
if(fstat(fd, &st)) return ret;
|
|
|
|
len = st.st_size;
|
2011-11-22 12:31:54 +01:00
|
|
|
if(len == 0) return 1;
|
2011-11-19 21:25:44 +01:00
|
|
|
}
|
|
|
|
|
2011-11-14 15:57:52 +01:00
|
|
|
gettimeofday(&tv1, &tz);
|
|
|
|
|
2011-11-19 21:25:44 +01:00
|
|
|
addr = mmap(NULL, len, PROT_READ, MAP_PRIVATE, fd, 0);
|
2011-11-14 15:57:52 +01:00
|
|
|
close(fd);
|
|
|
|
|
|
|
|
if(addr == MAP_FAILED) return ret;
|
|
|
|
|
|
|
|
dstlen = compressBound(len);
|
|
|
|
|
|
|
|
z = malloc(dstlen);
|
|
|
|
|
|
|
|
if(z == NULL){
|
2011-11-19 21:25:44 +01:00
|
|
|
munmap(addr, len);
|
2011-11-22 12:31:54 +01:00
|
|
|
syslog(LOG_PRIORITY, "%s: cannot malloc for z buffer", sdata->ttmpfile);
|
2011-11-14 15:57:52 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = compress(z, &dstlen, (const Bytef *)addr, len);
|
|
|
|
gettimeofday(&tv2, &tz);
|
|
|
|
sdata->__compress += tvdiff(tv2, tv1);
|
|
|
|
|
2011-11-19 21:25:44 +01:00
|
|
|
munmap(addr, len);
|
2011-11-14 15:57:52 +01:00
|
|
|
|
|
|
|
if(rc != Z_OK) goto ENDE;
|
|
|
|
|
2013-01-11 11:37:23 +01:00
|
|
|
if(cfg->encrypt_messages == 1){
|
|
|
|
gettimeofday(&tv1, &tz);
|
2011-11-14 15:57:52 +01:00
|
|
|
|
2013-01-11 11:37:23 +01:00
|
|
|
EVP_CIPHER_CTX_init(&ctx);
|
|
|
|
EVP_EncryptInit_ex(&ctx, EVP_bf_cbc(), NULL, cfg->key, cfg->iv);
|
2011-11-14 15:57:52 +01:00
|
|
|
|
2013-01-11 11:37:23 +01:00
|
|
|
outbuf = malloc(dstlen + EVP_MAX_BLOCK_LENGTH);
|
|
|
|
if(outbuf == NULL) goto ENDE;
|
2011-11-14 15:57:52 +01:00
|
|
|
|
2013-01-11 11:37:23 +01:00
|
|
|
if(!EVP_EncryptUpdate(&ctx, outbuf, &outlen, z, dstlen)) goto ENDE;
|
|
|
|
if(!EVP_EncryptFinal_ex(&ctx, outbuf + outlen, &tmplen)) goto ENDE;
|
|
|
|
outlen += tmplen;
|
|
|
|
EVP_CIPHER_CTX_cleanup(&ctx);
|
2011-11-14 15:57:52 +01:00
|
|
|
|
2013-01-11 11:37:23 +01:00
|
|
|
gettimeofday(&tv2, &tz);
|
|
|
|
sdata->__encrypt += tvdiff(tv2, tv1);
|
|
|
|
}
|
2011-11-14 15:57:52 +01:00
|
|
|
|
2011-11-19 21:25:44 +01:00
|
|
|
/* create a filename in the store based on piler_id */
|
|
|
|
|
|
|
|
p = strchr(filename, '.');
|
|
|
|
if(p) *p = '\0';
|
|
|
|
|
2013-02-15 20:58:58 +01:00
|
|
|
snprintf(s, sizeof(s)-1, "%s/%02x/%c%c%c/%c%c/%c%c/%s", cfg->queuedir, cfg->server_id, filename[8], filename[9], filename[10], filename[RND_STR_LEN-4], filename[RND_STR_LEN-3], filename[RND_STR_LEN-2], filename[RND_STR_LEN-1], filename);
|
2011-11-19 21:25:44 +01:00
|
|
|
|
|
|
|
if(p){
|
|
|
|
*p = '.';
|
|
|
|
strncat(s, p, sizeof(s)-1);
|
|
|
|
}
|
2011-11-14 15:57:52 +01:00
|
|
|
|
|
|
|
|
|
|
|
p0 = strrchr(s, '/'); if(!p0) goto ENDE;
|
|
|
|
*p0 = '\0';
|
|
|
|
|
|
|
|
if(stat(s, &st)){
|
|
|
|
p1 = strrchr(s, '/'); if(!p1) goto ENDE;
|
|
|
|
*p1 = '\0';
|
|
|
|
p2 = strrchr(s, '/'); if(!p2) goto ENDE;
|
|
|
|
*p2 = '\0';
|
|
|
|
|
2012-04-27 21:25:54 +02:00
|
|
|
rc = mkdir(s, 0750);
|
2011-11-14 15:57:52 +01:00
|
|
|
*p2 = '/';
|
2012-04-27 21:25:54 +02:00
|
|
|
rc = mkdir(s, 0750);
|
2011-11-14 15:57:52 +01:00
|
|
|
*p1 = '/';
|
2012-03-20 10:35:22 +01:00
|
|
|
rc = mkdir(s, 0770); if(rc == -1) syslog(LOG_PRIORITY, "%s: mkdir %s: error=%s", sdata->ttmpfile, s, strerror(errno));
|
2011-11-14 15:57:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
*p0 = '/';
|
|
|
|
|
2011-11-19 21:25:44 +01:00
|
|
|
fd = open(s, O_CREAT|O_RDWR, S_IRUSR|S_IWUSR|S_IRGRP);
|
2011-11-22 12:31:54 +01:00
|
|
|
if(fd == -1){
|
|
|
|
syslog(LOG_PRIORITY, "%s: cannot open: %s", sdata->ttmpfile, s);
|
|
|
|
goto ENDE;
|
|
|
|
}
|
|
|
|
|
2011-11-14 15:57:52 +01:00
|
|
|
|
2013-01-11 11:37:23 +01:00
|
|
|
if(cfg->encrypt_messages == 1){
|
|
|
|
n = write(fd, outbuf, outlen);
|
|
|
|
writelen = outlen;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
n = write(fd, z, dstlen);
|
|
|
|
writelen = dstlen;
|
|
|
|
}
|
2011-11-14 15:57:52 +01:00
|
|
|
|
2013-01-11 11:37:23 +01:00
|
|
|
if(n == writelen){
|
2011-11-14 15:57:52 +01:00
|
|
|
ret = 1;
|
2013-01-11 11:37:23 +01:00
|
|
|
if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "%s: stored '%s' %d/%d bytes", sdata->ttmpfile, filename, len, writelen);
|
2011-11-14 15:57:52 +01:00
|
|
|
}
|
2011-11-22 12:31:54 +01:00
|
|
|
else {
|
2013-01-11 11:37:23 +01:00
|
|
|
syslog(LOG_PRIORITY, "%s: cannot write %d bytes (only %d)", sdata->ttmpfile, writelen, n);
|
2011-11-22 12:31:54 +01:00
|
|
|
}
|
2011-11-14 15:57:52 +01:00
|
|
|
|
|
|
|
fsync(fd);
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
if(ret == 0){
|
|
|
|
unlink(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ENDE:
|
|
|
|
if(outbuf) free(outbuf);
|
|
|
|
if(z) free(z);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-29 14:08:56 +02:00
|
|
|
int remove_stored_message_files(struct session_data *sdata, struct _state *state, struct __config *cfg){
|
|
|
|
int i;
|
|
|
|
char s[SMALLBUFSIZE];
|
|
|
|
|
|
|
|
if(state->n_attachments > 0){
|
|
|
|
|
|
|
|
for(i=1; i<=state->n_attachments; i++){
|
2013-02-15 20:58:58 +01:00
|
|
|
snprintf(s, sizeof(s)-1, "%s/%02x/%c%c%c/%c%c/%c%c/%s.a%d", cfg->queuedir, cfg->server_id, sdata->ttmpfile[8], sdata->ttmpfile[9], sdata->ttmpfile[10], sdata->ttmpfile[RND_STR_LEN-4], sdata->ttmpfile[RND_STR_LEN-3], sdata->ttmpfile[RND_STR_LEN-2], sdata->ttmpfile[RND_STR_LEN-1], sdata->ttmpfile, i);
|
2012-05-29 14:08:56 +02:00
|
|
|
|
|
|
|
if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "%s: unlinking %s", sdata->ttmpfile, s);
|
|
|
|
|
|
|
|
unlink(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-15 20:58:58 +01:00
|
|
|
snprintf(s, sizeof(s)-1, "%s/%02x/%c%c%c/%c%c/%c%c/%s.m", cfg->queuedir, cfg->server_id, sdata->ttmpfile[8], sdata->ttmpfile[9], sdata->ttmpfile[10], sdata->ttmpfile[RND_STR_LEN-4], sdata->ttmpfile[RND_STR_LEN-3], sdata->ttmpfile[RND_STR_LEN-2], sdata->ttmpfile[RND_STR_LEN-1], sdata->ttmpfile);
|
2012-05-29 14:08:56 +02:00
|
|
|
|
|
|
|
if(cfg->verbosity >= _LOG_DEBUG) syslog(LOG_PRIORITY, "%s: unlinking %s", sdata->ttmpfile, s);
|
|
|
|
|
|
|
|
unlink(s);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|