2012-01-07 23:26:47 +01:00
|
|
|
/*
|
|
|
|
* pilerexport.c, SJ
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <locale.h>
|
|
|
|
#include <syslog.h>
|
2012-01-08 16:16:00 +01:00
|
|
|
#include <getopt.h>
|
2012-01-07 23:26:47 +01:00
|
|
|
#include <piler.h>
|
|
|
|
|
|
|
|
|
|
|
|
extern char *optarg;
|
|
|
|
extern int optind;
|
|
|
|
|
2012-01-08 16:16:00 +01:00
|
|
|
int dryrun = 0;
|
2015-08-12 15:38:02 +02:00
|
|
|
int exportall = 0;
|
|
|
|
int rc = 0;
|
2012-01-07 23:26:47 +01:00
|
|
|
char *query=NULL;
|
2015-08-12 15:38:02 +02:00
|
|
|
int verbosity = 0;
|
|
|
|
int max_matches = 1000;
|
|
|
|
char *index_list = "main1,dailydelta1,delta1";
|
2012-01-07 23:26:47 +01:00
|
|
|
regex_t regexp;
|
|
|
|
|
|
|
|
|
2017-08-08 15:34:45 +02:00
|
|
|
int export_emails_matching_to_query(struct session_data *sdata, struct data *data, char *s, struct config *cfg);
|
2015-08-12 15:38:02 +02:00
|
|
|
|
2012-01-07 23:26:47 +01:00
|
|
|
|
|
|
|
void usage(){
|
2015-11-04 08:50:41 +01:00
|
|
|
printf("\nusage: pilerexport\n\n");
|
|
|
|
|
|
|
|
printf(" [-c <config file>] Config file to use if not the default\n");
|
|
|
|
printf(" -a <start date> Start date in YYYY.MM.DD format\n");
|
|
|
|
printf(" -b <stop date> Stop date in YYYY.MM.DD format\n");
|
|
|
|
printf(" -f <email@address.com> From address\n");
|
|
|
|
printf(" -r <email@address.com> Recipient address\n");
|
|
|
|
printf(" -F <domain.com> From domain\n");
|
|
|
|
printf(" -R <domain.com> Recipient domain\n");
|
|
|
|
printf(" -s <size> Min. size\n");
|
|
|
|
printf(" -S <size> Max. size\n");
|
|
|
|
printf(" -w <where condition> Where condition to pass to sphinx, eg. \"match('@subject: piler')\"\n");
|
|
|
|
printf(" -m <max. matches> Max. matches to apply to sphinx query (default: %d)\n", max_matches);
|
|
|
|
printf(" -i <index list> Sphinx indices to use (default: %s)\n", index_list);
|
|
|
|
printf(" -A Export all emails from archive\n");
|
|
|
|
printf(" -d Dry run\n");
|
2012-01-08 16:16:00 +01:00
|
|
|
|
|
|
|
regfree(®exp);
|
|
|
|
|
2012-01-07 23:26:47 +01:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-29 10:22:31 +01:00
|
|
|
void p_clean_exit(char *msg, int rc){
|
2012-01-07 23:26:47 +01:00
|
|
|
if(msg) printf("error: %s\n", msg);
|
|
|
|
|
|
|
|
if(query) free(query);
|
|
|
|
|
|
|
|
exit(rc);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-08 16:16:00 +01:00
|
|
|
unsigned long convert_time(char *yyyymmdd, int h, int m, int s){
|
|
|
|
char *p;
|
|
|
|
struct tm tm;
|
|
|
|
|
|
|
|
if(yyyymmdd == NULL) return 0;
|
|
|
|
|
|
|
|
memset((char*)&tm, 0, sizeof(tm));
|
|
|
|
tm.tm_isdst = -1;
|
|
|
|
|
|
|
|
tm.tm_hour = h;
|
|
|
|
tm.tm_min = m;
|
|
|
|
tm.tm_sec = s;
|
|
|
|
|
|
|
|
p = strchr(yyyymmdd, '.'); if(!p) return 0;
|
|
|
|
*p = '\0'; tm.tm_year = atoi(yyyymmdd) - 1900; yyyymmdd = p+1;
|
|
|
|
|
|
|
|
p = strchr(yyyymmdd, '.'); if(!p) return 0;
|
|
|
|
*p = '\0'; tm.tm_mon = atoi(yyyymmdd) - 1; yyyymmdd = p+1;
|
|
|
|
|
|
|
|
tm.tm_mday = atoi(yyyymmdd);
|
|
|
|
|
|
|
|
|
|
|
|
tm.tm_isdst = -1;
|
|
|
|
|
|
|
|
return mktime(&tm);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-07 23:26:47 +01:00
|
|
|
int append_email_to_buffer(char **buffer, char *email){
|
|
|
|
int len, arglen;
|
|
|
|
char *s=NULL, emailaddress[SMALLBUFSIZE];
|
|
|
|
|
|
|
|
snprintf(emailaddress, sizeof(emailaddress)-1, "'%s'", email);
|
|
|
|
arglen = strlen(emailaddress);
|
|
|
|
|
|
|
|
if(!*buffer){
|
|
|
|
*buffer = malloc(arglen+1);
|
|
|
|
memset(*buffer, 0, arglen+1);
|
|
|
|
memcpy(*buffer, emailaddress, arglen);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
len = strlen(*buffer);
|
|
|
|
s = realloc(*buffer, len + arglen+2);
|
|
|
|
if(!s){
|
|
|
|
printf("malloc problem!\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*buffer = s;
|
|
|
|
|
|
|
|
memset(*buffer+len, 0, arglen+2);
|
|
|
|
strcat(*buffer, ",");
|
|
|
|
memcpy(*buffer+len+1, emailaddress, arglen);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int append_string_to_buffer(char **buffer, char *str){
|
|
|
|
int len, arglen;
|
|
|
|
char *s=NULL;
|
|
|
|
|
|
|
|
arglen = strlen(str);
|
|
|
|
|
|
|
|
if(!*buffer){
|
|
|
|
*buffer = malloc(arglen+1);
|
|
|
|
memset(*buffer, 0, arglen+1);
|
|
|
|
memcpy(*buffer, str, arglen);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
len = strlen(*buffer);
|
|
|
|
s = realloc(*buffer, len + arglen+1);
|
|
|
|
if(!s) return 1;
|
|
|
|
|
|
|
|
*buffer = s;
|
|
|
|
|
|
|
|
memset(*buffer+len, 0, arglen+1);
|
|
|
|
memcpy(*buffer+len, str, arglen);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-08 15:34:45 +02:00
|
|
|
uint64 run_query(struct session_data *sdata, struct session_data *sdata2, struct data *data, char *where_condition, uint64 last_id, int *num, struct config *cfg){
|
2015-08-12 15:38:02 +02:00
|
|
|
MYSQL_RES *res;
|
|
|
|
MYSQL_ROW row;
|
|
|
|
int rc=0;
|
|
|
|
uint64 id=0;
|
|
|
|
char s[SMALLBUFSIZE];
|
|
|
|
|
|
|
|
*num = 0;
|
|
|
|
|
|
|
|
if(!where_condition) return id;
|
|
|
|
|
|
|
|
snprintf(s, sizeof(s)-1, "SELECT `id`, `piler_id`, `digest`, `bodydigest` FROM %s WHERE id IN (", SQL_METADATA_TABLE);
|
|
|
|
rc += append_string_to_buffer(&query, s);
|
|
|
|
|
|
|
|
snprintf(s, sizeof(s)-1, "SELECT id FROM %s WHERE %s AND id > %llu ORDER BY id ASC LIMIT 0,%d", index_list, where_condition, last_id, max_matches);
|
|
|
|
|
|
|
|
if(mysql_real_query(&(sdata2->mysql), s, strlen(s)) == 0){
|
|
|
|
res = mysql_store_result(&(sdata2->mysql));
|
|
|
|
if(res != NULL){
|
|
|
|
while((row = mysql_fetch_row(res))){
|
|
|
|
id = strtoull(row[0], NULL, 10);
|
|
|
|
(*num)++;
|
|
|
|
rc += append_string_to_buffer(&query, row[0]);
|
|
|
|
rc += append_string_to_buffer(&query, ",");
|
|
|
|
|
|
|
|
}
|
|
|
|
mysql_free_result(res);
|
|
|
|
|
|
|
|
rc += append_string_to_buffer(&query, "-1)");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!rc) export_emails_matching_to_query(sdata, data, query, cfg);
|
|
|
|
|
|
|
|
free(query);
|
|
|
|
query = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint64 get_total_found(struct session_data *sdata){
|
|
|
|
MYSQL_RES *res;
|
|
|
|
MYSQL_ROW row;
|
|
|
|
uint64 total_found=0;
|
|
|
|
|
|
|
|
if(mysql_real_query(&(sdata->mysql), "SHOW META LIKE 'total_found'", 28) == 0){
|
|
|
|
res = mysql_store_result(&(sdata->mysql));
|
|
|
|
if(res != NULL){
|
|
|
|
while((row = mysql_fetch_row(res))){
|
|
|
|
total_found = strtoull(row[1], NULL, 10);
|
|
|
|
}
|
|
|
|
mysql_free_result(res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return total_found;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-08 15:34:45 +02:00
|
|
|
void export_emails_matching_id_list(struct session_data *sdata, struct session_data *sdata2, struct data *data, char *where_condition, struct config *cfg){
|
2015-08-12 15:38:02 +02:00
|
|
|
int n;
|
|
|
|
uint64 count=0, last_id=0, total_found=0;
|
|
|
|
|
|
|
|
last_id = run_query(sdata, sdata2, data, where_condition, last_id, &n, cfg);
|
|
|
|
count += n;
|
|
|
|
|
|
|
|
total_found = get_total_found(sdata2);
|
|
|
|
|
|
|
|
while(count < total_found){
|
|
|
|
last_id = run_query(sdata, sdata2, data, where_condition, last_id, &n, cfg);
|
|
|
|
count += n;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int build_query_from_args(char *from, char *to, char *fromdomain, char *todomain, int minsize, int maxsize, unsigned long startdate, unsigned long stopdate){
|
2016-02-19 21:22:53 +01:00
|
|
|
int where_condition=1;
|
2015-08-12 15:38:02 +02:00
|
|
|
char s[SMALLBUFSIZE];
|
|
|
|
|
|
|
|
if(exportall == 1){
|
2016-02-19 22:11:29 +01:00
|
|
|
rc = append_string_to_buffer(&query, "SELECT `id`, `piler_id`, `digest`, `bodydigest` FROM ");
|
2015-08-12 15:38:02 +02:00
|
|
|
rc += append_string_to_buffer(&query, SQL_METADATA_TABLE);
|
2016-02-19 22:11:29 +01:00
|
|
|
rc += append_string_to_buffer(&query, " WHERE deleted=0 ");
|
2015-08-12 15:38:02 +02:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2016-02-19 21:22:53 +01:00
|
|
|
snprintf(s, sizeof(s)-1, "SELECT DISTINCT `id`, `piler_id`, `digest`, `bodydigest` FROM %s WHERE deleted=0 ", SQL_MESSAGES_VIEW);
|
2015-08-12 15:38:02 +02:00
|
|
|
|
|
|
|
rc = append_string_to_buffer(&query, s);
|
|
|
|
|
|
|
|
if(from){
|
2016-02-19 21:22:53 +01:00
|
|
|
if(where_condition) rc = append_string_to_buffer(&query, " AND ");
|
|
|
|
|
2015-08-12 15:38:02 +02:00
|
|
|
rc += append_string_to_buffer(&query, "`from` IN (");
|
|
|
|
rc += append_string_to_buffer(&query, from);
|
|
|
|
rc += append_string_to_buffer(&query, ")");
|
|
|
|
|
|
|
|
free(from);
|
|
|
|
|
|
|
|
where_condition++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(to){
|
|
|
|
if(where_condition) rc = append_string_to_buffer(&query, " AND ");
|
|
|
|
|
|
|
|
rc += append_string_to_buffer(&query, "`to` IN (");
|
|
|
|
rc += append_string_to_buffer(&query, to);
|
|
|
|
rc += append_string_to_buffer(&query, ")");
|
|
|
|
|
|
|
|
free(to);
|
|
|
|
|
|
|
|
where_condition++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(fromdomain){
|
|
|
|
if(where_condition) rc = append_string_to_buffer(&query, " AND ");
|
|
|
|
|
|
|
|
rc += append_string_to_buffer(&query, "`fromdomain` IN (");
|
|
|
|
rc += append_string_to_buffer(&query, fromdomain);
|
|
|
|
rc += append_string_to_buffer(&query, ")");
|
|
|
|
|
|
|
|
free(fromdomain);
|
|
|
|
|
|
|
|
where_condition++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(todomain){
|
|
|
|
if(where_condition) rc = append_string_to_buffer(&query, " AND ");
|
|
|
|
|
|
|
|
rc += append_string_to_buffer(&query, "`todomain` IN (");
|
|
|
|
rc += append_string_to_buffer(&query, todomain);
|
|
|
|
rc += append_string_to_buffer(&query, ")");
|
|
|
|
|
|
|
|
free(todomain);
|
|
|
|
|
|
|
|
where_condition++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(minsize > 0){
|
|
|
|
if(where_condition) rc = append_string_to_buffer(&query, " AND ");
|
|
|
|
snprintf(s, sizeof(s)-1, " `size` >= %d", minsize);
|
|
|
|
rc += append_string_to_buffer(&query, s);
|
|
|
|
|
|
|
|
where_condition++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(maxsize > 0){
|
|
|
|
if(where_condition) rc = append_string_to_buffer(&query, " AND ");
|
|
|
|
snprintf(s, sizeof(s)-1, " `size` <= %d", maxsize);
|
|
|
|
rc += append_string_to_buffer(&query, s);
|
|
|
|
|
|
|
|
where_condition++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(startdate > 0){
|
|
|
|
if(where_condition) rc = append_string_to_buffer(&query, " AND ");
|
|
|
|
snprintf(s, sizeof(s)-1, " `sent` >= %ld", startdate);
|
|
|
|
rc += append_string_to_buffer(&query, s);
|
|
|
|
|
|
|
|
where_condition++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(stopdate > 0){
|
|
|
|
if(where_condition) rc = append_string_to_buffer(&query, " AND ");
|
|
|
|
snprintf(s, sizeof(s)-1, " `sent` <= %ld", stopdate);
|
|
|
|
rc += append_string_to_buffer(&query, s);
|
|
|
|
|
|
|
|
where_condition++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
rc += append_string_to_buffer(&query, " ORDER BY id ASC");
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-08 15:34:45 +02:00
|
|
|
int export_emails_matching_to_query(struct session_data *sdata, struct data *data, char *s, struct config *cfg){
|
2012-01-07 23:26:47 +01:00
|
|
|
FILE *f;
|
2013-01-03 15:14:07 +01:00
|
|
|
uint64 id, n=0;
|
2013-05-05 11:57:14 +02:00
|
|
|
char digest[SMALLBUFSIZE], bodydigest[SMALLBUFSIZE];
|
2012-01-07 23:26:47 +01:00
|
|
|
char filename[SMALLBUFSIZE];
|
|
|
|
int rc=0;
|
2017-08-11 18:18:45 +02:00
|
|
|
struct sql sql;
|
2012-01-07 23:26:47 +01:00
|
|
|
|
2017-08-11 18:18:45 +02:00
|
|
|
if(prepare_sql_statement(sdata, &sql, s) == ERR) return ERR;
|
2012-01-07 23:26:47 +01:00
|
|
|
|
|
|
|
|
2017-08-11 18:18:45 +02:00
|
|
|
p_bind_init(&sql);
|
2012-01-07 23:26:47 +01:00
|
|
|
|
2017-08-11 18:18:45 +02:00
|
|
|
if(p_exec_stmt(sdata, &sql) == ERR) goto ENDE;
|
2012-01-07 23:26:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2017-08-11 18:18:45 +02:00
|
|
|
p_bind_init(&sql);
|
2012-01-07 23:26:47 +01:00
|
|
|
|
2017-08-11 18:18:45 +02:00
|
|
|
sql.sql[sql.pos] = (char *)&id; sql.type[sql.pos] = TYPE_LONGLONG; sql.len[sql.pos] = sizeof(uint64); sql.pos++;
|
|
|
|
sql.sql[sql.pos] = sdata->ttmpfile; sql.type[sql.pos] = TYPE_STRING; sql.len[sql.pos] = RND_STR_LEN; sql.pos++;
|
|
|
|
sql.sql[sql.pos] = &digest[0]; sql.type[sql.pos] = TYPE_STRING; sql.len[sql.pos] = sizeof(digest)-2; sql.pos++;
|
|
|
|
sql.sql[sql.pos] = &bodydigest[0]; sql.type[sql.pos] = TYPE_STRING; sql.len[sql.pos] = sizeof(bodydigest)-2; sql.pos++;
|
2013-01-03 15:14:07 +01:00
|
|
|
|
2017-08-11 18:18:45 +02:00
|
|
|
p_store_results(&sql);
|
2012-01-07 23:26:47 +01:00
|
|
|
|
2017-08-11 18:18:45 +02:00
|
|
|
while(p_fetch_results(&sql) == OK){
|
2012-01-07 23:26:47 +01:00
|
|
|
|
2013-05-05 11:57:14 +02:00
|
|
|
if(id > 0){
|
|
|
|
|
|
|
|
if(dryrun == 0){
|
|
|
|
|
|
|
|
snprintf(filename, sizeof(filename)-1, "%llu.eml", id);
|
|
|
|
|
|
|
|
f = fopen(filename, "w");
|
|
|
|
if(f){
|
|
|
|
rc = retrieve_email_from_archive(sdata, data, f, cfg);
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
n++;
|
|
|
|
|
|
|
|
snprintf(sdata->filename, SMALLBUFSIZE-1, "%s", filename);
|
|
|
|
|
|
|
|
make_digests(sdata, cfg);
|
|
|
|
|
|
|
|
if(strcmp(digest, sdata->digest) == 0 && strcmp(bodydigest, sdata->bodydigest) == 0){
|
|
|
|
printf("exported: %10llu\r", n); fflush(stdout);
|
2012-01-07 23:26:47 +01:00
|
|
|
}
|
2013-05-05 11:57:14 +02:00
|
|
|
else
|
|
|
|
printf("verification FAILED. %s\n", filename);
|
2012-01-07 23:26:47 +01:00
|
|
|
|
|
|
|
}
|
2013-05-05 11:57:14 +02:00
|
|
|
else printf("cannot open: %s\n", filename);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf("id:%llu\n", id);
|
2012-01-07 23:26:47 +01:00
|
|
|
}
|
2013-05-05 11:57:14 +02:00
|
|
|
|
2012-01-07 23:26:47 +01:00
|
|
|
}
|
2013-05-05 11:57:14 +02:00
|
|
|
|
2012-01-07 23:26:47 +01:00
|
|
|
}
|
|
|
|
|
2017-08-11 18:18:45 +02:00
|
|
|
p_free_results(&sql);
|
2013-05-05 11:57:14 +02:00
|
|
|
|
|
|
|
ENDE:
|
2017-08-11 18:18:45 +02:00
|
|
|
close_prepared_statement(&sql);
|
2013-05-05 11:57:14 +02:00
|
|
|
|
|
|
|
|
2013-01-03 15:14:07 +01:00
|
|
|
printf("\n");
|
|
|
|
|
2012-01-07 23:26:47 +01:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char **argv){
|
2015-08-12 15:38:02 +02:00
|
|
|
int c, minsize=0, maxsize=0;
|
2012-01-07 23:26:47 +01:00
|
|
|
size_t nmatch=0;
|
2012-01-08 16:16:00 +01:00
|
|
|
unsigned long startdate=0, stopdate=0;
|
2012-01-07 23:26:47 +01:00
|
|
|
char *configfile=CONFIG_FILE;
|
2015-08-12 15:38:02 +02:00
|
|
|
char *to=NULL, *from=NULL, *todomain=NULL, *fromdomain=NULL, *where_condition=NULL;
|
|
|
|
struct session_data sdata, sdata2;
|
2017-08-08 15:34:45 +02:00
|
|
|
struct data data;
|
|
|
|
struct config cfg;
|
2012-01-07 23:26:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
if(regcomp(®exp, "^([\\+a-z0-9_\\.@\\-]+)$", REG_ICASE | REG_EXTENDED)){
|
2012-10-29 10:22:31 +01:00
|
|
|
p_clean_exit("cannot compile rule!", 1);
|
2012-01-07 23:26:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-08 16:16:00 +01:00
|
|
|
while(1){
|
|
|
|
|
|
|
|
#ifdef _GNU_SOURCE
|
|
|
|
static struct option long_options[] =
|
|
|
|
{
|
|
|
|
{"config", required_argument, 0, 'c' },
|
|
|
|
{"minsize", required_argument, 0, 's' },
|
|
|
|
{"maxsize", required_argument, 0, 'S' },
|
|
|
|
{"all", no_argument, 0, 'A' },
|
|
|
|
{"dry-run", no_argument, 0, 'd' },
|
|
|
|
{"dryrun", no_argument, 0, 'd' },
|
|
|
|
{"help", no_argument, 0, 'h' },
|
|
|
|
{"version", no_argument, 0, 'v' },
|
|
|
|
{"from", required_argument, 0, 'f' },
|
|
|
|
{"to", required_argument, 0, 'r' },
|
2014-01-20 15:24:36 +01:00
|
|
|
{"from-domain", required_argument, 0, 'F' },
|
|
|
|
{"to-domain", required_argument, 0, 'R' },
|
2012-01-08 16:16:00 +01:00
|
|
|
{"start-date", required_argument, 0, 'a' },
|
|
|
|
{"stop-date", required_argument, 0, 'b' },
|
2015-08-12 15:38:02 +02:00
|
|
|
{"where-condition", required_argument, 0, 'w' },
|
|
|
|
{"max-matches", required_argument, 0, 'm' },
|
|
|
|
{"index-list", required_argument, 0, 'i' },
|
2012-01-08 16:16:00 +01:00
|
|
|
{0,0,0,0}
|
|
|
|
};
|
|
|
|
|
|
|
|
int option_index = 0;
|
|
|
|
|
2015-08-12 15:38:02 +02:00
|
|
|
c = getopt_long(argc, argv, "c:s:S:f:r:F:R:a:b:w:m:i:Adhv?", long_options, &option_index);
|
2012-01-08 16:16:00 +01:00
|
|
|
#else
|
2015-08-12 15:38:02 +02:00
|
|
|
c = getopt(argc, argv, "c:s:S:f:r:F:R:a:b:w:m:i:Adhv?");
|
2012-01-08 16:16:00 +01:00
|
|
|
#endif
|
2012-01-07 23:26:47 +01:00
|
|
|
|
2012-01-08 16:16:00 +01:00
|
|
|
if(c == -1) break;
|
|
|
|
|
|
|
|
switch(c){
|
2012-01-07 23:26:47 +01:00
|
|
|
|
|
|
|
case 'c' :
|
|
|
|
configfile = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 's' :
|
2012-01-08 16:16:00 +01:00
|
|
|
minsize = atoi(optarg);
|
2012-01-07 23:26:47 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'S' :
|
2012-01-08 16:16:00 +01:00
|
|
|
maxsize = atoi(optarg);
|
2012-01-07 23:26:47 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
2012-01-08 16:16:00 +01:00
|
|
|
case 'A' :
|
2012-01-07 23:26:47 +01:00
|
|
|
exportall = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f' :
|
|
|
|
|
|
|
|
if(regexec(®exp, optarg, nmatch, NULL, 0)){
|
|
|
|
printf("%s is not a valid email address\n", optarg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = append_email_to_buffer(&from, optarg);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'r' :
|
|
|
|
|
|
|
|
if(regexec(®exp, optarg, nmatch, NULL, 0)){
|
|
|
|
printf("%s is not a valid email address\n", optarg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = append_email_to_buffer(&to, optarg);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2014-01-20 15:24:36 +01:00
|
|
|
case 'F' :
|
2012-01-07 23:26:47 +01:00
|
|
|
|
2014-01-20 15:24:36 +01:00
|
|
|
if(regexec(®exp, optarg, nmatch, NULL, 0)){
|
|
|
|
printf("%s is not a valid domain name\n", optarg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = append_email_to_buffer(&fromdomain, optarg);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'R' :
|
|
|
|
|
|
|
|
if(regexec(®exp, optarg, nmatch, NULL, 0)){
|
|
|
|
printf("%s is not a valid domain name\n", optarg);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = append_email_to_buffer(&todomain, optarg);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2012-01-08 16:16:00 +01:00
|
|
|
case 'a' :
|
|
|
|
startdate = convert_time(optarg, 0, 0, 0);
|
|
|
|
break;
|
2012-01-07 23:26:47 +01:00
|
|
|
|
2012-01-08 16:16:00 +01:00
|
|
|
|
|
|
|
case 'b' :
|
|
|
|
stopdate = convert_time(optarg, 23, 59, 59);
|
|
|
|
break;
|
|
|
|
|
2015-08-12 15:38:02 +02:00
|
|
|
case 'w' :
|
|
|
|
where_condition = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'm' :
|
|
|
|
max_matches = atoi(optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'i' :
|
|
|
|
index_list = optarg;
|
|
|
|
break;
|
2012-01-08 16:16:00 +01:00
|
|
|
|
|
|
|
case 'd' :
|
|
|
|
dryrun = 1;
|
2012-01-07 23:26:47 +01:00
|
|
|
break;
|
|
|
|
|
2012-01-08 16:16:00 +01:00
|
|
|
default :
|
|
|
|
usage();
|
2012-01-07 23:26:47 +01:00
|
|
|
break;
|
2012-01-08 16:16:00 +01:00
|
|
|
}
|
|
|
|
|
2012-01-07 23:26:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-12 15:38:02 +02:00
|
|
|
if(from == NULL && to == NULL && fromdomain == NULL && todomain == NULL && where_condition == NULL && startdate == 0 && stopdate == 0 && exportall == 0) usage();
|
2012-01-08 16:16:00 +01:00
|
|
|
|
|
|
|
|
2012-01-07 23:26:47 +01:00
|
|
|
regfree(®exp);
|
|
|
|
|
2017-05-14 17:18:09 +02:00
|
|
|
if(!can_i_write_directory(NULL)) __fatal("cannot write current directory!");
|
2014-10-02 10:02:33 +02:00
|
|
|
|
2012-03-10 14:52:50 +01:00
|
|
|
(void) openlog("pilerexport", LOG_PID, LOG_MAIL);
|
|
|
|
|
2012-01-07 23:26:47 +01:00
|
|
|
|
|
|
|
cfg = read_config(configfile);
|
|
|
|
|
|
|
|
|
2012-10-29 10:22:31 +01:00
|
|
|
if(read_key(&cfg)) p_clean_exit(ERR_READING_KEY, 1);
|
2012-01-07 23:26:47 +01:00
|
|
|
|
|
|
|
|
2013-02-22 15:01:21 +01:00
|
|
|
init_session_data(&sdata, &cfg);
|
2012-01-07 23:26:47 +01:00
|
|
|
|
|
|
|
|
2013-04-28 14:18:09 +02:00
|
|
|
if(open_database(&sdata, &cfg) == ERR){
|
2012-10-29 10:22:31 +01:00
|
|
|
p_clean_exit("cannot connect to mysql server", 1);
|
2012-01-07 23:26:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-12 15:38:02 +02:00
|
|
|
if(where_condition){
|
2012-01-07 23:26:47 +01:00
|
|
|
|
2015-08-12 15:38:02 +02:00
|
|
|
init_session_data(&sdata2, &cfg);
|
2012-01-07 23:26:47 +01:00
|
|
|
|
2015-08-12 15:38:02 +02:00
|
|
|
strcpy(cfg.mysqlhost, "127.0.0.1");
|
|
|
|
cfg.mysqlport = 9306;
|
|
|
|
cfg.mysqlsocket[0] = '\0';
|
|
|
|
|
|
|
|
if(open_database(&sdata2, &cfg) == ERR){
|
|
|
|
p_clean_exit("cannot connect to 127.0.0.1:9306", 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
export_emails_matching_id_list(&sdata, &sdata2, &data, where_condition, &cfg);
|
2012-01-07 23:26:47 +01:00
|
|
|
|
2015-08-12 15:38:02 +02:00
|
|
|
close_database(&sdata2);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if(build_query_from_args(from, to, fromdomain, todomain, minsize, maxsize, startdate, stopdate) > 0) p_clean_exit("malloc problem building query", 1);
|
|
|
|
export_emails_matching_to_query(&sdata, &data, query, &cfg);
|
|
|
|
free(query);
|
|
|
|
}
|
2012-01-07 23:26:47 +01:00
|
|
|
|
2013-04-28 14:18:09 +02:00
|
|
|
close_database(&sdata);
|
2012-01-07 23:26:47 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|