summaryrefslogtreecommitdiff
path: root/sievecmd.c
blob: 817bff4707df1f7397d7d263c30732541d5d939a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
 Copyright (C) 2003 Aaron Stone

 This program is free software; you can redistribute it and/or 
 modify it under the terms of the GNU General Public License 
 as published by the Free Software Foundation; either 
 version 2 of the License, or (at your option) any later 
 version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

/* $Id$
 * This is dbmail-sievecmd, which provides
 * a command line interface to the sievescripts */

#include "dbmail.h"

#define PNAME "dbmail/sievecmd"
char *configFile = DEFAULT_CONFIG_FILE;

/* set up database login data */
extern db_param_t _db_params;

int main(int argc, char *argv[])
{
	int res = 0, opt = 0, act = 0;
	u64_t user_idnr = 0;
	char *user_name = NULL;
	char *name = NULL;
	FILE *source = NULL;
	extern char *optarg;

	openlog(PNAME, LOG_PID, LOG_MAIL);

	setvbuf(stdout, 0, _IONBF, 0);

	config_read(configFile);
	SetTraceLevel("DBMAIL");
	GetDBParams(&_db_params);

	while (opt != -1 && act != 'h') {
		opt = getopt(argc, argv, "a:d:i:r:u:l");

		switch (opt) {
		case -1:
			/* Break right away if this is the end of the args */
			break;
		case 'a':
		case 'd':
		case 'i':
		case 'r':
			if (act != 0)
				act = 'h';
			else
				act = opt;
			name = optarg;
			source = stdin;	// FIXME to take files as input, too
			break;
		case 'u':
			user_name = dm_strdup(optarg);
			break;
		case 'l':
			if (act != 0)
				act = 'h';
			else
				act = opt;
			break;
		default:
			act = 'h';
			break;
		}
	}

	if (act != 'h' && act != 0) {
		printf("*** dbmail-sievecmd ***\n");

		/* Open database connection */
		printf("Opening connection to database...\n");
		if (db_connect() != 0) {
			printf
			    ("Failed. Could not connect to database (check log)\n");
			dm_free(user_name);
			return -1;
		}

		/* Open authentication connection */
		printf("Opening connection to authentication...\n");
		if (auth_connect() != 0) {
			printf
			    ("Failed. Could not connect to authentication (check log)\n");
			dm_free(user_name);
			return -1;
		}

		printf("Ok. Connected!\n");

		/* Retrieve the user ID number */
		switch (auth_user_exists(user_name, &user_idnr)) {
		case 0:
			printf("User [%s] does not exist!\n", user_name);
			goto mainend;
			break;
		case -1:
			printf("Error retrieving User ID Number\n");
			res = -1;
			goto mainend;
		}
	}

	switch (act) {
	case 'a':
		res = do_activate(user_idnr, name);
		break;
	case 'd':
		res = do_deactivate(user_idnr, name);
		break;
	case 'i':
		res = do_insert(user_idnr, name, source);
		break;
	case 'r':
		res = do_remove(user_idnr, name);
		break;
	case 'l':
		res = do_list(user_idnr);
		break;
	case 'h':
	default:
		res = do_showhelp();
		break;
	}

      mainend:
	dm_free(user_name);
	db_disconnect();
	auth_disconnect();
	config_free();
	return res;
}


int do_activate(u64_t user_idnr, char *name)
{
	int res = 0;

	res = db_activate_sievescript(user_idnr, name);
	if (res == -3) {
		printf("Script [%s] does not exist.\n", name);
		return -1;
	} else if (res != 0) {
		printf("Error activating script [%s].\n"
		       "It is possible that no script is currently active!\n",
		       name);
		return -1;
	}
	printf("Script [%s] is now active. All others are inactive.\n",
	       name);

	return 0;
}


int do_deactivate(u64_t user_idnr, char *name)
{
	int res = 0;

	res = db_deactivate_sievescript(user_idnr, name);
	if (res == -3) {
		printf("Script [%s] does not exist.\n", name);
		return -1;
	} else if (res != 0) {
		printf("Error deactivating script [%s].\n", name);
		return -1;
	}
	printf
	    ("Script [%s] is now deactivated. No scripts are currently active.\n",
	     name);

	return 0;
}


int do_insert(u64_t user_idnr, char *name, FILE * source)
{
	int res = 0;
	char *buf = NULL;
	sort_result_t *sort_result;

	/* Read the file into a char array */
	res = read_script_file(source, &buf);
	if (res != 0) {
		printf("Error reading in your script!\n");
		return -1;
	}

	/* Check if the script is valid */
	res = db_add_sievescript(user_idnr, "@!temp-script!@", buf);
	if (res != 0) {
		// FIXME: Error.
	}
	dm_free(buf);

	sort_result = sort_validate(user_idnr, "@!temp-script!@");
	if (sort_result == NULL) {
		printf("Script could not be validated.\n");
		db_delete_sievescript(user_idnr, "@!temp-script!@");
		sort_free_result(sort_result);
		return -1;
	}
	if (sort_get_error(sort_result) != 0) {
		printf("Script [%s] has errors: %s.\n",
			name, sort_get_errormsg(sort_result));
		db_delete_sievescript(user_idnr, "@!temp-script!@");
		sort_free_result(sort_result);
		return -1;
	}
	sort_free_result(sort_result);

	res = db_rename_sievescript(user_idnr, "@!temp-script!@", name);
	if (res == -3) {
		printf("Script [%s] already exists.\n", name);
		db_delete_sievescript(user_idnr, "@!temp-script!@");
		sort_free_result(sort_result);
		return -1;
	} else if (res != 0) {
		printf("Error inserting script [%s] into the database!\n",
		       name);
		db_delete_sievescript(user_idnr, "@!temp-script!@");
		sort_free_result(sort_result);
		return -1;
	}

	printf("Script [%s] successfully inserted and marked inactive!\n",
	       name);
	sort_free_result(sort_result);
	return 0;
}


int do_remove(u64_t user_idnr, char *name)
{
	int res;

	res = db_delete_sievescript(user_idnr, name);
	if (res == -3) {
		printf("Script [%s] does not exist.\n", name);
		return -1;
	} else if (res != 0) {
		printf("Error deleting script [%s].\n", name);
		return -1;
	}

	printf("Script [%s] deleted.\n", name);

	return 0;
}


int do_list(u64_t user_idnr)
{
	struct dm_list scriptlist;
	struct element *tmp;

	if (db_get_sievescript_listall(user_idnr, &scriptlist) < 0) {
		printf("Error retrieving Sieve script list.\n");
		return -1;
	}

	if (dm_list_length(&scriptlist) > 0) {
		printf("Found %ld scripts:\n",
		       dm_list_length(&scriptlist));
	} else
		printf("No scripts found!\n");

	tmp = dm_list_getstart(&scriptlist);
	while (tmp) {
		sievescript_info_t *info = (sievescript_info_t *) tmp->data;
		if (info->active == 1)
			printf("  + ");
		else
			printf("  - ");
		printf("%s\n", info->name);

		dm_free(info->name);
		tmp = tmp->nextnode;
	}

	if (scriptlist.start)
		dm_list_free(&scriptlist.start);

	return 0;
}


int do_showhelp(void)
{
	printf("*** dbmail-sievecmd ***\n");

	printf("Use this program to manage your users' Sieve scripts.\n");
	printf("See the man page for more info. Summary:\n\n");
	printf("     -u username            Username of script user \n");
	printf("     -l                     List scripts belonging to user \n");
	printf("     -a scriptname          Activate the named script \n");
	printf("                            (only one script can be active; \n"
	       "                             deactivates any others) \n");
	printf("     -d scriptname          Deactivate the named script \n");
	printf("                            (no scripts will be active after this) \n");
	printf("     -i scriptname file     Insert the named script from file \n");
	printf("                            (a single dash, -, indicates input \n"
	       "                             from STDIN) \n");
	printf("     -r scriptname          Remove the named script \n");
	printf("                            (if script was active, no script is \n"
	       "                             active after deletion) \n");
	printf("\n*** THESE COMMAND LINE OPTIONS WILL CHANGE! ***\n");

	return 0;
}


int read_script_file(FILE * f, char **m_buf)
{
	size_t f_len = 1024;
	size_t f_pos = 0;
	char *f_buf = NULL;

	if (!f) {
		printf("Received NULL as script input\n");
		return -1;
	}

	/* Allocate the initial input buffer. */
	f_buf = dm_malloc(sizeof(char) * f_len);
	if (f_buf == NULL)
		return -2;

	while (!feof(f)) {
		if (f_pos + 1 >= f_len) {
			f_buf =
			    dm_realloc(f_buf, sizeof(char) * (f_len *= 2));
			if (f_buf == NULL)
				return -2;
		}
		f_buf[f_pos] = fgetc(f);
		f_pos++;
	}

	if (f_pos)
		f_buf[f_pos] = '\0';

	*m_buf = f_buf;
	return 0;
}