1 | /* $Id: dspam_2sql.c,v 1.17 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 <dirent.h> |
---|
31 | #include <ctype.h> |
---|
32 | #include <sys/types.h> |
---|
33 | #include <sys/stat.h> |
---|
34 | #include <errno.h> |
---|
35 | #include <pwd.h> |
---|
36 | #include <signal.h> |
---|
37 | |
---|
38 | #include "libdspam.h" |
---|
39 | #include "util.h" |
---|
40 | #include "read_config.h" |
---|
41 | #include "config_api.h" |
---|
42 | #include "language.h" |
---|
43 | |
---|
44 | DSPAM_CTX *open_ctx, *open_mtx; |
---|
45 | |
---|
46 | int dump_user (const char *username); |
---|
47 | int process_all_users (void); |
---|
48 | void dieout (int signal); |
---|
49 | |
---|
50 | int |
---|
51 | main (int argc, char **argv) |
---|
52 | { |
---|
53 | #ifndef _WIN32 |
---|
54 | #ifdef TRUSTED_USER_SECURITY |
---|
55 | struct passwd *p = getpwuid (getuid ()); |
---|
56 | #endif |
---|
57 | #endif |
---|
58 | int i, ret; |
---|
59 | |
---|
60 | /* Read dspam.conf */ |
---|
61 | |
---|
62 | agent_config = read_config(NULL); |
---|
63 | if (!agent_config) { |
---|
64 | LOG(LOG_ERR, ERR_AGENT_READ_CONFIG); |
---|
65 | fprintf (stderr, ERR_AGENT_READ_CONFIG "\n"); |
---|
66 | exit(EXIT_FAILURE); |
---|
67 | } |
---|
68 | |
---|
69 | if (!_ds_read_attribute(agent_config, "Home")) { |
---|
70 | LOG(LOG_ERR, ERR_AGENT_DSPAM_HOME); |
---|
71 | fprintf (stderr, ERR_AGENT_DSPAM_HOME "\n"); |
---|
72 | _ds_destroy_config(agent_config); |
---|
73 | exit(EXIT_FAILURE); |
---|
74 | } |
---|
75 | |
---|
76 | if (libdspam_init(_ds_read_attribute(agent_config, "StorageDriver")) != 0) { |
---|
77 | LOG(LOG_ERR, ERR_DRV_INIT); |
---|
78 | fprintf (stderr, ERR_DRV_INIT "\n"); |
---|
79 | _ds_destroy_config(agent_config); |
---|
80 | exit(EXIT_FAILURE); |
---|
81 | } |
---|
82 | |
---|
83 | #ifndef _WIN32 |
---|
84 | #ifdef TRUSTED_USER_SECURITY |
---|
85 | if (!_ds_match_attribute(agent_config, "Trust", p->pw_name) && p->pw_uid) { |
---|
86 | fprintf(stderr, ERR_TRUSTED_MODE "\n"); |
---|
87 | _ds_destroy_config(agent_config); |
---|
88 | goto BAIL; |
---|
89 | } |
---|
90 | #endif |
---|
91 | #endif |
---|
92 | |
---|
93 | for(i=0;i<argc;i++) { |
---|
94 | |
---|
95 | if (!strncmp (argv[i], "--profile=", 10)) |
---|
96 | { |
---|
97 | if (!_ds_match_attribute(agent_config, "Profile", argv[i]+10)) { |
---|
98 | LOG(LOG_ERR, ERR_AGENT_NO_SUCH_PROFILE, argv[i]+10); |
---|
99 | fprintf (stderr, ERR_AGENT_NO_SUCH_PROFILE "\n", argv[i]+10); |
---|
100 | _ds_destroy_config(agent_config); |
---|
101 | goto BAIL; |
---|
102 | } else { |
---|
103 | _ds_overwrite_attribute(agent_config, "DefaultProfile", argv[i]+10); |
---|
104 | } |
---|
105 | break; |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | open_ctx = open_mtx = NULL; |
---|
110 | |
---|
111 | signal (SIGINT, dieout); |
---|
112 | signal (SIGPIPE, dieout); |
---|
113 | signal (SIGTERM, dieout); |
---|
114 | |
---|
115 | dspam_init_driver (NULL); |
---|
116 | |
---|
117 | if (argc<2) |
---|
118 | { |
---|
119 | ret = process_all_users(); |
---|
120 | } |
---|
121 | else |
---|
122 | { |
---|
123 | ret = dump_user(argv[1]); |
---|
124 | } |
---|
125 | |
---|
126 | |
---|
127 | dspam_shutdown_driver (NULL); |
---|
128 | _ds_destroy_config(agent_config); |
---|
129 | libdspam_shutdown(); |
---|
130 | exit((ret == 0) ? EXIT_SUCCESS : EXIT_FAILURE); |
---|
131 | |
---|
132 | BAIL: |
---|
133 | libdspam_shutdown(); |
---|
134 | exit(EXIT_FAILURE); |
---|
135 | } |
---|
136 | |
---|
137 | int |
---|
138 | process_all_users (void) |
---|
139 | { |
---|
140 | DSPAM_CTX *CTX; |
---|
141 | char *user; |
---|
142 | |
---|
143 | CTX = dspam_create (NULL, NULL, _ds_read_attribute(agent_config, "Home"), DSM_TOOLS, 0); |
---|
144 | open_ctx = CTX; |
---|
145 | if (CTX == NULL) |
---|
146 | { |
---|
147 | fprintf (stderr, "Could not initialize context: %s\n", strerror (errno)); |
---|
148 | return EFAILURE; |
---|
149 | } |
---|
150 | |
---|
151 | set_libdspam_attributes(CTX); |
---|
152 | if (dspam_attach(CTX, NULL)) { |
---|
153 | LOG (LOG_WARNING, "unable to attach dspam context"); |
---|
154 | fprintf (stderr, "Unable to attach DSPAM context\n"); |
---|
155 | return EFAILURE; |
---|
156 | } |
---|
157 | |
---|
158 | |
---|
159 | user = _ds_get_nextuser (CTX); |
---|
160 | while (user != NULL) |
---|
161 | { |
---|
162 | dump_user (user); |
---|
163 | user = _ds_get_nextuser (CTX); |
---|
164 | } |
---|
165 | |
---|
166 | dspam_destroy (CTX); |
---|
167 | open_ctx = NULL; |
---|
168 | return 0; |
---|
169 | } |
---|
170 | |
---|
171 | int |
---|
172 | dump_user (const char *username) |
---|
173 | { |
---|
174 | int uid; |
---|
175 | struct passwd *p; |
---|
176 | struct _ds_storage_record *record; |
---|
177 | DSPAM_CTX *CTX; |
---|
178 | |
---|
179 | p = getpwnam (username); |
---|
180 | if (p == NULL) |
---|
181 | { |
---|
182 | fprintf (stderr, "Unable to obtain uid for user %s. Using uid=1 instead!\n", username); |
---|
183 | uid=1; |
---|
184 | } |
---|
185 | else |
---|
186 | { |
---|
187 | uid=p->pw_uid; |
---|
188 | } |
---|
189 | |
---|
190 | CTX = dspam_create (username, NULL, _ds_read_attribute(agent_config, "Home"), DSM_CLASSIFY, 0); |
---|
191 | open_mtx = CTX; |
---|
192 | if (CTX == NULL) |
---|
193 | { |
---|
194 | fprintf (stderr, "Could not init context: %s\n", strerror (errno)); |
---|
195 | return EUNKNOWN; |
---|
196 | } |
---|
197 | |
---|
198 | set_libdspam_attributes(CTX); |
---|
199 | if (dspam_attach(CTX, NULL)) { |
---|
200 | LOG (LOG_WARNING, "unable to attach dspam context"); |
---|
201 | fprintf (stderr, "Unable to attach DSPAM context\n"); |
---|
202 | return EFAILURE; |
---|
203 | } |
---|
204 | |
---|
205 | printf |
---|
206 | ("insert into dspam_stats (uid, spam_learned, innocent_learned, spam_misclassified, innocent_misclassified, spam_corpusfed, innocent_corpusfed, spam_classified, innocent_classified) values(%d, %ld, %ld, %ld, %ld, %ld, %ld, %ld, %ld);\n", |
---|
207 | uid, CTX->totals.spam_learned, CTX->totals.innocent_learned, |
---|
208 | CTX->totals.spam_misclassified, CTX->totals.innocent_misclassified, |
---|
209 | CTX->totals.spam_corpusfed, CTX->totals.innocent_corpusfed, |
---|
210 | CTX->totals.spam_classified, CTX->totals.innocent_classified); |
---|
211 | |
---|
212 | record = _ds_get_nexttoken (CTX); |
---|
213 | while (record != NULL) |
---|
214 | { |
---|
215 | printf |
---|
216 | ("insert into dspam_token_data (uid, token, spam_hits, innocent_hits, last_hit) values(%d, \"%"LLU_FMT_SPEC"\", %ld, %ld, %ld);\n", |
---|
217 | uid, record->token, record->spam_hits, |
---|
218 | record->innocent_hits, (long) record->last_hit); |
---|
219 | record = _ds_get_nexttoken (CTX); |
---|
220 | } |
---|
221 | |
---|
222 | dspam_destroy (CTX); |
---|
223 | open_mtx = NULL; |
---|
224 | return 0; |
---|
225 | } |
---|
226 | |
---|
227 | void |
---|
228 | dieout (int signal) |
---|
229 | { |
---|
230 | signal = signal; /* Keep compiler happy */ |
---|
231 | fprintf (stderr, "terminated.\n"); |
---|
232 | if (open_ctx != NULL) |
---|
233 | dspam_destroy (open_ctx); |
---|
234 | if (open_mtx != NULL) |
---|
235 | dspam_destroy (open_mtx); |
---|
236 | _ds_destroy_config(agent_config); |
---|
237 | exit (EXIT_SUCCESS); |
---|
238 | } |
---|