| 1 | /* $Id: dspam_dump.c,v 1.21 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 | #ifdef HAVE_CONFIG_H |
|---|
| 23 | #include <auto-config.h> |
|---|
| 24 | #endif |
|---|
| 25 | |
|---|
| 26 | #include <stdio.h> |
|---|
| 27 | #include <stdlib.h> |
|---|
| 28 | #include <string.h> |
|---|
| 29 | #include <fcntl.h> |
|---|
| 30 | #include <ctype.h> |
|---|
| 31 | #include <errno.h> |
|---|
| 32 | #include <sys/types.h> |
|---|
| 33 | #include <sys/stat.h> |
|---|
| 34 | #include <signal.h> |
|---|
| 35 | #include "util.h" |
|---|
| 36 | |
|---|
| 37 | #ifdef TIME_WITH_SYS_TIME |
|---|
| 38 | # include <sys/time.h> |
|---|
| 39 | # include <time.h> |
|---|
| 40 | #else |
|---|
| 41 | # ifdef HAVE_SYS_TIME_H |
|---|
| 42 | # include <sys/time.h> |
|---|
| 43 | # else |
|---|
| 44 | # include <time.h> |
|---|
| 45 | # endif |
|---|
| 46 | #endif |
|---|
| 47 | |
|---|
| 48 | #include "libdspam.h" |
|---|
| 49 | #include "read_config.h" |
|---|
| 50 | #include "config_api.h" |
|---|
| 51 | #include "language.h" |
|---|
| 52 | |
|---|
| 53 | #ifdef _WIN32 |
|---|
| 54 | /* no trusted users under Windows */ |
|---|
| 55 | #undef TRUSTED_USER_SECURITY |
|---|
| 56 | #endif |
|---|
| 57 | |
|---|
| 58 | DSPAM_CTX *open_ctx; |
|---|
| 59 | |
|---|
| 60 | /* headers */ |
|---|
| 61 | int dump_database (DSPAM_CTX * CTX, const char *token, int sql); |
|---|
| 62 | void dieout (int signal); |
|---|
| 63 | |
|---|
| 64 | #define TSYNTAX \ |
|---|
| 65 | "syntax: dspam_dump [--profile=Profile] [-d sqlite_drv] username [token]" |
|---|
| 66 | |
|---|
| 67 | #define SQL_SQLITE_DRV 1 |
|---|
| 68 | |
|---|
| 69 | int |
|---|
| 70 | main (int argc, char **argv) |
|---|
| 71 | { |
|---|
| 72 | DSPAM_CTX *CTX; |
|---|
| 73 | int r, sql = 0, i; |
|---|
| 74 | char *username = NULL, *token = NULL; |
|---|
| 75 | #ifdef TRUSTED_USER_SECURITY |
|---|
| 76 | struct passwd *p = getpwuid (getuid ()); |
|---|
| 77 | #endif |
|---|
| 78 | |
|---|
| 79 | /* Read dspam.conf */ |
|---|
| 80 | |
|---|
| 81 | agent_config = read_config(NULL); |
|---|
| 82 | if (!agent_config) { |
|---|
| 83 | LOG(LOG_ERR, ERR_AGENT_READ_CONFIG); |
|---|
| 84 | fprintf (stderr, ERR_AGENT_READ_CONFIG "\n"); |
|---|
| 85 | exit(EXIT_FAILURE); |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | if (!_ds_read_attribute(agent_config, "Home")) { |
|---|
| 89 | LOG(LOG_ERR, ERR_AGENT_DSPAM_HOME); |
|---|
| 90 | fprintf (stderr, ERR_AGENT_DSPAM_HOME "\n"); |
|---|
| 91 | exit(EXIT_FAILURE); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | if (libdspam_init(_ds_read_attribute(agent_config, "StorageDriver")) != 0) { |
|---|
| 95 | LOG(LOG_ERR, ERR_DRV_INIT); |
|---|
| 96 | fprintf (stderr, ERR_DRV_INIT "\n"); |
|---|
| 97 | _ds_destroy_config(agent_config); |
|---|
| 98 | exit(EXIT_FAILURE); |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | open_ctx = NULL; |
|---|
| 102 | signal (SIGINT, dieout); |
|---|
| 103 | signal (SIGPIPE, dieout); |
|---|
| 104 | signal (SIGTERM, dieout); |
|---|
| 105 | |
|---|
| 106 | if (argc < 2) |
|---|
| 107 | { |
|---|
| 108 | fprintf (stderr, "%s\n", TSYNTAX); |
|---|
| 109 | goto BAIL; |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | for(i=1;i<argc;i++) { |
|---|
| 113 | |
|---|
| 114 | if (!strncmp (argv[i], "--profile=", 10)) |
|---|
| 115 | { |
|---|
| 116 | if (!_ds_match_attribute(agent_config, "Profile", argv[i]+10)) { |
|---|
| 117 | LOG(LOG_ERR, ERR_AGENT_NO_SUCH_PROFILE, argv[i]+10); |
|---|
| 118 | fprintf (stderr, ERR_AGENT_NO_SUCH_PROFILE "\n", argv[i]+10); |
|---|
| 119 | goto BAIL; |
|---|
| 120 | } else { |
|---|
| 121 | _ds_overwrite_attribute(agent_config, "DefaultProfile", argv[i]+10); |
|---|
| 122 | } |
|---|
| 123 | continue; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | if (!strcmp(argv[i], "-d")) { |
|---|
| 127 | if (i+1<argc && !strcmp(argv[i+1], "sqlite_drv")) { |
|---|
| 128 | sql = SQL_SQLITE_DRV; |
|---|
| 129 | i++; |
|---|
| 130 | } else { |
|---|
| 131 | fprintf(stderr, "invalid driver or no driver specified\n"); |
|---|
| 132 | goto BAIL; |
|---|
| 133 | } |
|---|
| 134 | } else { |
|---|
| 135 | if (username == NULL) { |
|---|
| 136 | username = argv[i]; |
|---|
| 137 | #ifdef TRUSTED_USER_SECURITY |
|---|
| 138 | if ( strcmp(username, p->pw_name) && |
|---|
| 139 | !_ds_match_attribute(agent_config, "Trust", p->pw_name) && |
|---|
| 140 | p->pw_uid) { |
|---|
| 141 | fprintf(stderr, ERR_TRUSTED_MODE "\n"); |
|---|
| 142 | goto BAIL; |
|---|
| 143 | } |
|---|
| 144 | #endif |
|---|
| 145 | } |
|---|
| 146 | else |
|---|
| 147 | token = argv[i]; |
|---|
| 148 | } |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | dspam_init_driver (NULL); |
|---|
| 152 | CTX = dspam_create (username, NULL, _ds_read_attribute(agent_config, "Home"), DSM_CLASSIFY, 0); |
|---|
| 153 | open_ctx = CTX; |
|---|
| 154 | if (CTX == NULL) |
|---|
| 155 | { |
|---|
| 156 | fprintf (stderr, "Could not init context: %s\n", strerror (errno)); |
|---|
| 157 | dspam_shutdown_driver (NULL); |
|---|
| 158 | goto BAIL; |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | set_libdspam_attributes(CTX); |
|---|
| 162 | if (dspam_attach(CTX, NULL)) { |
|---|
| 163 | LOG (LOG_WARNING, "unable to attach dspam context"); |
|---|
| 164 | fprintf (stderr, "Unable to attach DSPAM context\n"); |
|---|
| 165 | goto BAIL; |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | r = dump_database (CTX, token, sql); |
|---|
| 169 | dspam_destroy (CTX); |
|---|
| 170 | open_ctx = NULL; |
|---|
| 171 | dspam_shutdown_driver (NULL); |
|---|
| 172 | libdspam_shutdown(); |
|---|
| 173 | return (r) ? EXIT_FAILURE : EXIT_SUCCESS; |
|---|
| 174 | |
|---|
| 175 | BAIL: |
|---|
| 176 | libdspam_shutdown(); |
|---|
| 177 | exit(EXIT_FAILURE); |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | int |
|---|
| 181 | dump_database (DSPAM_CTX * CTX, const char *token, int sql) |
|---|
| 182 | { |
|---|
| 183 | struct _ds_storage_record *sr; |
|---|
| 184 | struct _ds_spam_stat s; |
|---|
| 185 | unsigned long long crc = 0; |
|---|
| 186 | |
|---|
| 187 | if (token) |
|---|
| 188 | crc = _ds_getcrc64(token); |
|---|
| 189 | |
|---|
| 190 | if (sql == SQL_SQLITE_DRV) { |
|---|
| 191 | printf("insert into dspam_stats (dspam_stat_id, " |
|---|
| 192 | "spam_learned, innocent_learned, " |
|---|
| 193 | "spam_misclassified, innocent_misclassified, " |
|---|
| 194 | "spam_corpusfed, innocent_corpusfed, " |
|---|
| 195 | "spam_classified, innocent_classified) values " |
|---|
| 196 | "(0, %ld, %ld, %ld, %ld, %ld, %ld, %ld, %ld)\n", |
|---|
| 197 | CTX->totals.spam_learned, CTX->totals.innocent_learned, |
|---|
| 198 | CTX->totals.spam_misclassified, CTX->totals.innocent_misclassified, |
|---|
| 199 | CTX->totals.spam_corpusfed, CTX->totals.innocent_corpusfed, |
|---|
| 200 | CTX->totals.spam_classified, CTX->totals.innocent_classified); |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | if (token == NULL) { |
|---|
| 204 | sr = _ds_get_nexttoken (CTX); |
|---|
| 205 | while (sr != NULL) |
|---|
| 206 | { |
|---|
| 207 | s.innocent_hits = sr->innocent_hits; |
|---|
| 208 | s.spam_hits = sr->spam_hits; |
|---|
| 209 | s.probability = 0.00000; |
|---|
| 210 | _ds_calc_stat(CTX, NULL, &s, DTT_DEFAULT, NULL); |
|---|
| 211 | if (!sql) { |
|---|
| 212 | printf ("%-20"LLU_FMT_SPEC" S: %05ld I: %05ld P: %0.4f LH: %s", sr->token, |
|---|
| 213 | sr->spam_hits, sr->innocent_hits, s.probability, |
|---|
| 214 | ctime (&sr->last_hit)); |
|---|
| 215 | free (sr); |
|---|
| 216 | } else if (sql == SQL_SQLITE_DRV) { |
|---|
| 217 | printf("insert into dspam_token_data(token, spam_hits, " |
|---|
| 218 | "innocent_hits, last_hit) values('%"LLU_FMT_SPEC"', %ld, %ld, " |
|---|
| 219 | "date('now'))\n", sr->token, sr->spam_hits, sr->innocent_hits); |
|---|
| 220 | } |
|---|
| 221 | sr = _ds_get_nexttoken (CTX); |
|---|
| 222 | } |
|---|
| 223 | } else { |
|---|
| 224 | if (_ds_get_spamrecord (CTX, crc, &s)) { |
|---|
| 225 | fprintf(stderr, "token not found\n"); |
|---|
| 226 | return -1; |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | _ds_calc_stat(CTX, NULL, &s, DTT_DEFAULT, NULL); |
|---|
| 230 | printf ("%-20"LLU_FMT_SPEC" S: %05ld I: %05ld P: %0.4f\n", crc, |
|---|
| 231 | s.spam_hits, s.innocent_hits, s.probability); |
|---|
| 232 | |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | return 0; |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | void |
|---|
| 239 | dieout (int signal) |
|---|
| 240 | { |
|---|
| 241 | signal = signal; /* Keep compiler happy */ |
|---|
| 242 | fprintf (stderr, "terminated.\n"); |
|---|
| 243 | if (open_ctx != NULL) |
|---|
| 244 | dspam_destroy (open_ctx); |
|---|
| 245 | exit (EXIT_SUCCESS); |
|---|
| 246 | } |
|---|