| 1 | /* $Id: cssconvert.c,v 1.626 2011/06/28 00:13:48 sbajic Exp $ */ |
|---|
| 2 | |
|---|
| 3 | /* |
|---|
| 4 | DSPAM |
|---|
| 5 | COPYRIGHT (C) 2002-2012 DSPAM PROJECT |
|---|
| 6 | |
|---|
| 7 | This program is free software: you can redistribute it and/or modify |
|---|
| 8 | it under the terms of the GNU Affero General Public License as |
|---|
| 9 | published by the Free Software Foundation, either version 3 of the |
|---|
| 10 | License, or (at your option) any later version. |
|---|
| 11 | |
|---|
| 12 | This program is distributed in the hope that it will be useful, |
|---|
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 15 | GNU Affero General Public License for more details. |
|---|
| 16 | |
|---|
| 17 | You should have received a copy of the GNU Affero General Public License |
|---|
| 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 19 | |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | /* cssconvert.c - convert a v3.6.0 hash database to v3.6.1 (which includes |
|---|
| 23 | * 8-byte alignment for 64-bit systems */ |
|---|
| 24 | |
|---|
| 25 | #ifdef HAVE_CONFIG_H |
|---|
| 26 | #include <auto-config.h> |
|---|
| 27 | #endif |
|---|
| 28 | |
|---|
| 29 | #include <string.h> |
|---|
| 30 | #include <sys/types.h> |
|---|
| 31 | #include <sys/mman.h> |
|---|
| 32 | #include <sys/stat.h> |
|---|
| 33 | #include <sys/uio.h> |
|---|
| 34 | #include <dirent.h> |
|---|
| 35 | #include <unistd.h> |
|---|
| 36 | #include <errno.h> |
|---|
| 37 | #include <stdlib.h> |
|---|
| 38 | #include <stdio.h> |
|---|
| 39 | #include <fcntl.h> |
|---|
| 40 | #include <signal.h> |
|---|
| 41 | #include <libgen.h> |
|---|
| 42 | |
|---|
| 43 | #ifdef TIME_WITH_SYS_TIME |
|---|
| 44 | # include <sys/time.h> |
|---|
| 45 | # include <time.h> |
|---|
| 46 | #else |
|---|
| 47 | # ifdef HAVE_SYS_TIME_H |
|---|
| 48 | # include <sys/time.h> |
|---|
| 49 | # else |
|---|
| 50 | # include <time.h> |
|---|
| 51 | # endif |
|---|
| 52 | #endif |
|---|
| 53 | |
|---|
| 54 | #define READ_ATTRIB(A) _ds_read_attribute(agent_config, A) |
|---|
| 55 | #define MATCH_ATTRIB(A, B) _ds_match_attribute(agent_config, A, B) |
|---|
| 56 | |
|---|
| 57 | int DO_DEBUG |
|---|
| 58 | #ifdef DEBUG |
|---|
| 59 | = 1 |
|---|
| 60 | #else |
|---|
| 61 | = 0 |
|---|
| 62 | #endif |
|---|
| 63 | ; |
|---|
| 64 | |
|---|
| 65 | #include "read_config.h" |
|---|
| 66 | #include "hash_drv.h" |
|---|
| 67 | #include "error.h" |
|---|
| 68 | #include "language.h" |
|---|
| 69 | |
|---|
| 70 | #define SYNTAX "syntax: cssconvert [filename]" |
|---|
| 71 | |
|---|
| 72 | typedef struct _old_hash_drv_header |
|---|
| 73 | { |
|---|
| 74 | unsigned long hash_rec_max; |
|---|
| 75 | struct _ds_spam_totals totals; |
|---|
| 76 | } *old_hash_drv_header_t; |
|---|
| 77 | |
|---|
| 78 | int cssconvert(const char *filename); |
|---|
| 79 | |
|---|
| 80 | int main(int argc, char *argv[]) { |
|---|
| 81 | int r; |
|---|
| 82 | |
|---|
| 83 | if (argc<2) { |
|---|
| 84 | fprintf(stderr, "%s\n", SYNTAX); |
|---|
| 85 | exit(EXIT_FAILURE); |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | agent_config = read_config(NULL); |
|---|
| 89 | if (!agent_config) { |
|---|
| 90 | LOG(LOG_ERR, ERR_AGENT_READ_CONFIG); |
|---|
| 91 | exit(EXIT_FAILURE); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | r = cssconvert(argv[1]); |
|---|
| 95 | |
|---|
| 96 | if (r) { |
|---|
| 97 | fprintf(stderr, "cssconvert failed on error %d\n", r); |
|---|
| 98 | exit(EXIT_FAILURE); |
|---|
| 99 | } |
|---|
| 100 | exit(EXIT_SUCCESS); |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | int cssconvert(const char *filename) { |
|---|
| 104 | unsigned long i; |
|---|
| 105 | hash_drv_header_t header; |
|---|
| 106 | void *offset; |
|---|
| 107 | struct _hash_drv_map old, new; |
|---|
| 108 | hash_drv_spam_record_t rec; |
|---|
| 109 | unsigned long filepos; |
|---|
| 110 | char newfile[128]; |
|---|
| 111 | char *filenamecopy; |
|---|
| 112 | unsigned long hash_rec_max = HASH_REC_MAX; |
|---|
| 113 | unsigned long max_seek = HASH_SEEK_MAX; |
|---|
| 114 | unsigned long max_extents = 0; |
|---|
| 115 | unsigned long extent_size = HASH_EXTENT_MAX; |
|---|
| 116 | int pctincrease = 0; |
|---|
| 117 | int flags = 0; |
|---|
| 118 | |
|---|
| 119 | if (READ_ATTRIB("HashRecMax")) |
|---|
| 120 | hash_rec_max = strtol(READ_ATTRIB("HashRecMax"), NULL, 0); |
|---|
| 121 | |
|---|
| 122 | if (READ_ATTRIB("HashExtentSize")) |
|---|
| 123 | extent_size = strtol(READ_ATTRIB("HashExtentSize"), NULL, 0); |
|---|
| 124 | |
|---|
| 125 | if (READ_ATTRIB("HashMaxExtents")) |
|---|
| 126 | max_extents = strtol(READ_ATTRIB("HashMaxExtents"), NULL, 0); |
|---|
| 127 | |
|---|
| 128 | if (MATCH_ATTRIB("HashAutoExtend", "on")) |
|---|
| 129 | flags = HMAP_AUTOEXTEND; |
|---|
| 130 | |
|---|
| 131 | if (READ_ATTRIB("HashMaxSeek")) |
|---|
| 132 | max_seek = strtol(READ_ATTRIB("HashMaxSeek"), NULL, 0); |
|---|
| 133 | |
|---|
| 134 | if (READ_ATTRIB("HashPctIncrease")) { |
|---|
| 135 | pctincrease = atoi(READ_ATTRIB("HashPctIncrease")); |
|---|
| 136 | if (pctincrease > 100) { |
|---|
| 137 | LOG(LOG_ERR, "HashPctIncrease out of range; ignoring"); |
|---|
| 138 | pctincrease = 0; |
|---|
| 139 | } |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | filenamecopy = strdup(filename); |
|---|
| 143 | if (filenamecopy == NULL) |
|---|
| 144 | return EFAILURE; |
|---|
| 145 | |
|---|
| 146 | snprintf(newfile, sizeof(newfile), "/%s/.dspam%u.css", dirname((char *)filenamecopy), (unsigned int) getpid()); |
|---|
| 147 | |
|---|
| 148 | if (_hash_drv_open(filename, &old, 0, max_seek, |
|---|
| 149 | max_extents, extent_size, pctincrease, flags)) |
|---|
| 150 | { |
|---|
| 151 | return EFAILURE; |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | if (_hash_drv_open(newfile, &new, hash_rec_max, max_seek, |
|---|
| 155 | max_extents, extent_size, pctincrease, flags)) |
|---|
| 156 | { |
|---|
| 157 | _hash_drv_close(&old); |
|---|
| 158 | return EFAILURE; |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | filepos = sizeof(struct _old_hash_drv_header); |
|---|
| 162 | header = old.addr; |
|---|
| 163 | while(filepos < old.file_len) { |
|---|
| 164 | for(i=0;i<header->hash_rec_max;i++) { |
|---|
| 165 | rec = (void *)((unsigned long) old.addr + filepos); |
|---|
| 166 | if (_hash_drv_set_spamrecord(&new, rec, 0)) { |
|---|
| 167 | LOG(LOG_WARNING, "aborting on error"); |
|---|
| 168 | _hash_drv_close(&new); |
|---|
| 169 | _hash_drv_close(&old); |
|---|
| 170 | unlink(newfile); |
|---|
| 171 | return EFAILURE; |
|---|
| 172 | } |
|---|
| 173 | filepos += sizeof(struct _hash_drv_spam_record); |
|---|
| 174 | } |
|---|
| 175 | offset = (void *)((unsigned long) old.addr + filepos); |
|---|
| 176 | header = offset; |
|---|
| 177 | filepos += sizeof(struct _old_hash_drv_header); |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | _hash_drv_close(&new); |
|---|
| 181 | _hash_drv_close(&old); |
|---|
| 182 | rename(newfile, filename); |
|---|
| 183 | return 0; |
|---|
| 184 | } |
|---|
| 185 | |
|---|