1 | /* $Id: dspam_admin.c,v 1.26 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 <signal.h> |
---|
36 | #include <unistd.h> |
---|
37 | #include "config.h" |
---|
38 | |
---|
39 | #include "libdspam.h" |
---|
40 | #include "read_config.h" |
---|
41 | #include "language.h" |
---|
42 | #include "pref.h" |
---|
43 | |
---|
44 | #define TSYNTAX "syntax: dspam_admin [function] [arguments] [--profile=PROFILE]" |
---|
45 | |
---|
46 | /* |
---|
47 | PREFERENCE FUNCTIONS |
---|
48 | |
---|
49 | Add a Preference: dspam_admin add preference [user] [attr] [value] |
---|
50 | Set a Preference: dspam_admin change preference [user] [attr] [value] |
---|
51 | Delete a Preference: dspam_admin delete preference [user] [attr] |
---|
52 | List Preferences: dspam_admin list preferences [user] |
---|
53 | |
---|
54 | */ |
---|
55 | |
---|
56 | int set_preference_attribute(const char *, const char *, const char *); |
---|
57 | int del_preference_attribute(const char *, const char *); |
---|
58 | int list_preference_attributes(const char *); |
---|
59 | int list_aggregate_preference_attributes(const char *); |
---|
60 | |
---|
61 | void dieout (int signal); |
---|
62 | int usage (void); |
---|
63 | int min_args(int argc, int min); |
---|
64 | |
---|
65 | int |
---|
66 | main (int argc, char **argv) |
---|
67 | { |
---|
68 | #ifdef TRUSTED_USER_SECURITY |
---|
69 | struct passwd *p = getpwuid (getuid ()); |
---|
70 | #endif |
---|
71 | int i, valid = 0; |
---|
72 | int ret = EXIT_SUCCESS; |
---|
73 | |
---|
74 | /* Read dspam.conf */ |
---|
75 | |
---|
76 | agent_config = read_config(NULL); |
---|
77 | if (!agent_config) { |
---|
78 | LOG(LOG_ERR, ERR_AGENT_READ_CONFIG); |
---|
79 | fprintf (stderr, ERR_AGENT_READ_CONFIG "\n"); |
---|
80 | exit(EXIT_FAILURE); |
---|
81 | } |
---|
82 | |
---|
83 | if (!_ds_read_attribute(agent_config, "Home")) { |
---|
84 | LOG(LOG_ERR, ERR_AGENT_DSPAM_HOME); |
---|
85 | fprintf (stderr, ERR_AGENT_DSPAM_HOME "\n"); |
---|
86 | _ds_destroy_config(agent_config); |
---|
87 | exit(EXIT_FAILURE); |
---|
88 | } |
---|
89 | |
---|
90 | if (libdspam_init(_ds_read_attribute(agent_config, "StorageDriver")) != 0) { |
---|
91 | LOG(LOG_ERR, ERR_DRV_INIT); |
---|
92 | fprintf (stderr, ERR_DRV_INIT "\n"); |
---|
93 | _ds_destroy_config(agent_config); |
---|
94 | exit(EXIT_FAILURE); |
---|
95 | } |
---|
96 | |
---|
97 | #ifndef _WIN32 |
---|
98 | #ifdef TRUSTED_USER_SECURITY |
---|
99 | if (!_ds_match_attribute(agent_config, "Trust", p->pw_name) && p->pw_uid) { |
---|
100 | fprintf(stderr, ERR_TRUSTED_MODE "\n"); |
---|
101 | _ds_destroy_config(agent_config); |
---|
102 | goto BAIL; |
---|
103 | } |
---|
104 | #endif |
---|
105 | #endif |
---|
106 | |
---|
107 | for(i=0;i<argc;i++) { |
---|
108 | if (!strncmp (argv[i], "--profile=", 10)) |
---|
109 | { |
---|
110 | if (!_ds_match_attribute(agent_config, "Profile", argv[i]+10)) { |
---|
111 | LOG(LOG_ERR, ERR_AGENT_NO_SUCH_PROFILE, argv[i]+10); |
---|
112 | fprintf (stderr, ERR_AGENT_NO_SUCH_PROFILE "\n", argv[i]+10); |
---|
113 | _ds_destroy_config(agent_config); |
---|
114 | goto BAIL; |
---|
115 | } else { |
---|
116 | _ds_overwrite_attribute(agent_config, "DefaultProfile", argv[i]+10); |
---|
117 | } |
---|
118 | break; |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | signal (SIGINT, dieout); |
---|
123 | signal (SIGPIPE, dieout); |
---|
124 | signal (SIGTERM, dieout); |
---|
125 | |
---|
126 | dspam_init_driver (NULL); |
---|
127 | |
---|
128 | if (argc < 3 || !strcmp(argv[1], "help")) |
---|
129 | { |
---|
130 | dspam_shutdown_driver (NULL); |
---|
131 | _ds_destroy_config(agent_config); |
---|
132 | usage(); |
---|
133 | goto BAIL; |
---|
134 | } |
---|
135 | |
---|
136 | /* PREFERENCE FUNCTIONS */ |
---|
137 | if (!strncmp(argv[2], "pref", 4)) { |
---|
138 | |
---|
139 | /* Delete */ |
---|
140 | if (!strncmp(argv[1], "d", 1)) { |
---|
141 | if (min_args(argc, 4)) { |
---|
142 | valid = 1; |
---|
143 | ret = del_preference_attribute(argv[3], argv[4]); |
---|
144 | } |
---|
145 | } |
---|
146 | |
---|
147 | /* Add, Change */ |
---|
148 | else if (!strncmp(argv[1], "ch", 2) || !strncmp(argv[1], "ad", 2)) { |
---|
149 | if (min_args(argc, 5)) { |
---|
150 | valid = 1; |
---|
151 | ret = set_preference_attribute(argv[3], argv[4], argv[5]); |
---|
152 | } |
---|
153 | } |
---|
154 | |
---|
155 | /* List */ |
---|
156 | else if (!strncmp(argv[1], "l", 1)) { |
---|
157 | if (min_args(argc, 3)) { |
---|
158 | valid = 1; |
---|
159 | ret = list_preference_attributes(argv[3]); |
---|
160 | } |
---|
161 | } |
---|
162 | |
---|
163 | /* Aggregate - Preference attr + AllowOverride attr + user prefs */ |
---|
164 | else if (!strncmp(argv[1], "ag", 2)) { |
---|
165 | if (min_args(argc, 3)) { |
---|
166 | valid = 1; |
---|
167 | ret = list_aggregate_preference_attributes(argv[3]); |
---|
168 | } |
---|
169 | } |
---|
170 | } |
---|
171 | |
---|
172 | dspam_shutdown_driver (NULL); |
---|
173 | _ds_destroy_config(agent_config); |
---|
174 | |
---|
175 | if (!valid) { |
---|
176 | usage(); |
---|
177 | goto BAIL; |
---|
178 | } |
---|
179 | |
---|
180 | libdspam_shutdown(); |
---|
181 | exit(ret); |
---|
182 | |
---|
183 | BAIL: |
---|
184 | libdspam_shutdown(); |
---|
185 | exit(EXIT_FAILURE); |
---|
186 | } |
---|
187 | |
---|
188 | void |
---|
189 | dieout (int signal) |
---|
190 | { |
---|
191 | signal = signal; /* Keep compile happy */ |
---|
192 | fprintf (stderr, "terminated.\n"); |
---|
193 | _ds_destroy_config(agent_config); |
---|
194 | exit (EXIT_SUCCESS); |
---|
195 | } |
---|
196 | |
---|
197 | int |
---|
198 | usage (void) |
---|
199 | { |
---|
200 | fprintf(stderr, "%s\n", TSYNTAX); |
---|
201 | fprintf(stderr, "\tadd preference [user] [attrib] [value]\n"); |
---|
202 | fprintf(stderr, "\tchange preference [user] [attrib] [value]\n"); |
---|
203 | fprintf(stderr, "\tdelete preference [user] [attrib] [value]\n"); |
---|
204 | fprintf(stderr, "\tlist preference [user] [attrib] [value]\n"); |
---|
205 | fprintf(stderr, "\taggregate preference [user]\n"); |
---|
206 | return 0; |
---|
207 | } |
---|
208 | |
---|
209 | int min_args(int argc, int min) { |
---|
210 | if (argc<(min+1)) { |
---|
211 | fprintf(stderr, "invalid command syntax.\n"); |
---|
212 | return 0; |
---|
213 | } |
---|
214 | return 1; |
---|
215 | } |
---|
216 | |
---|
217 | int set_preference_attribute( |
---|
218 | const char *username, |
---|
219 | const char *attr, |
---|
220 | const char *value) |
---|
221 | { |
---|
222 | int i; |
---|
223 | |
---|
224 | if (username[0] == 0 || !strcmp(username, "default")) |
---|
225 | i = _ds_pref_set(agent_config, NULL, _ds_read_attribute(agent_config, "Home"), attr, value, NULL); |
---|
226 | else |
---|
227 | i = _ds_pref_set(agent_config, username, _ds_read_attribute(agent_config, "Home"), attr, value, NULL); |
---|
228 | |
---|
229 | if (!i) |
---|
230 | printf("operation successful.\n"); |
---|
231 | else |
---|
232 | printf("operation failed with error %d.\n", i); |
---|
233 | |
---|
234 | return i; |
---|
235 | } |
---|
236 | |
---|
237 | int del_preference_attribute( |
---|
238 | const char *username, |
---|
239 | const char *attr) |
---|
240 | { |
---|
241 | int i; |
---|
242 | |
---|
243 | if (username[0] == 0 || !strncmp(username, "default", strlen(username))) |
---|
244 | i = _ds_pref_del(agent_config, NULL, _ds_read_attribute(agent_config, "Home"), attr, NULL); |
---|
245 | else |
---|
246 | i = _ds_pref_del(agent_config, username, _ds_read_attribute(agent_config, "Home"), attr, NULL); |
---|
247 | |
---|
248 | if (!i) |
---|
249 | printf("operation successful.\n"); |
---|
250 | else |
---|
251 | printf("operation failed with error %d.\n", i); |
---|
252 | |
---|
253 | return i; |
---|
254 | } |
---|
255 | |
---|
256 | int list_preference_attributes(const char *username) |
---|
257 | { |
---|
258 | agent_pref_t PTX; |
---|
259 | agent_attrib_t pref; |
---|
260 | int i; |
---|
261 | |
---|
262 | if (username[0] == 0 || !strncmp(username, "default", strlen(username))) |
---|
263 | PTX = _ds_pref_load(agent_config, NULL, _ds_read_attribute(agent_config, "Home"), NULL); |
---|
264 | else |
---|
265 | PTX = _ds_pref_load(agent_config, username, _ds_read_attribute(agent_config, "Home"), NULL); |
---|
266 | |
---|
267 | if (PTX == NULL) |
---|
268 | return 0; |
---|
269 | |
---|
270 | for(i=0;PTX[i];i++) { |
---|
271 | pref = PTX[i]; |
---|
272 | printf("%s=%s\n", pref->attribute, pref->value); |
---|
273 | } |
---|
274 | |
---|
275 | _ds_pref_free(PTX); |
---|
276 | free(PTX); |
---|
277 | return 0; |
---|
278 | } |
---|
279 | |
---|
280 | int list_aggregate_preference_attributes(const char *username) |
---|
281 | { |
---|
282 | agent_pref_t PTX = NULL; |
---|
283 | agent_pref_t STX = NULL; |
---|
284 | agent_pref_t UTX = NULL; |
---|
285 | agent_attrib_t pref; |
---|
286 | int i; |
---|
287 | |
---|
288 | STX = _ds_pref_load(agent_config, |
---|
289 | NULL, |
---|
290 | _ds_read_attribute(agent_config, "Home"), NULL); |
---|
291 | |
---|
292 | if (STX == NULL || STX[0] == 0) { |
---|
293 | if (STX) { |
---|
294 | _ds_pref_free(STX); |
---|
295 | } |
---|
296 | LOGDEBUG("default preferences empty. reverting to dspam.conf preferences."); |
---|
297 | STX = pref_config(); |
---|
298 | } else { |
---|
299 | LOGDEBUG("loaded default preferences externally"); |
---|
300 | } |
---|
301 | |
---|
302 | if (username[0] == 0 || !strncmp(username, "default", strlen(username))) |
---|
303 | UTX = _ds_pref_load(agent_config, NULL, _ds_read_attribute(agent_config, "Home"), NULL); |
---|
304 | else { |
---|
305 | UTX = _ds_pref_load(agent_config, username, _ds_read_attribute(agent_config, "Home"), NULL); |
---|
306 | } |
---|
307 | |
---|
308 | PTX = _ds_pref_aggregate(STX, UTX); |
---|
309 | _ds_pref_free(STX); |
---|
310 | free(STX); |
---|
311 | if (UTX != NULL) { |
---|
312 | _ds_pref_free(UTX); |
---|
313 | free(UTX); |
---|
314 | } |
---|
315 | |
---|
316 | for(i=0;PTX[i];i++) { |
---|
317 | pref = PTX[i]; |
---|
318 | printf("%s=%s\n", pref->attribute, pref->value); |
---|
319 | } |
---|
320 | |
---|
321 | _ds_pref_free(PTX); |
---|
322 | free(PTX); |
---|
323 | return 0; |
---|
324 | } |
---|