summaryrefslogtreecommitdiff
path: root/export.c
blob: 3cdada5e2bafe0ea062c7621b3f53644f65de338 (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
/*
 Copyright (C) 2005-2006 NFG Net Facilities Group BV, support@nfg.nl

 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.
*/

/* 
 * This is the dbmail-user program
 * It makes adding users easier 
 *
 *
 * - moving most code to dbmail-users.c. Just a thin wrapper left */

#include "dbmail.h"

char *configFile = DEFAULT_CONFIG_FILE;

#define PNAME "dbmail/export"

int quiet, reallyquiet, verbose;

extern db_param_t _db_params;

/* UI policy */

int do_showhelp(void) {
	
	printf("*** dbmail-export ***\n");
	printf("Use this program to export your DBMail mailboxes.\n");
	printf("     -u username   specify a user\n");
	printf("     -m mailbox    specify a mailbox (default export all mailboxes)\n");
	printf("     -o outfile    specify the destination mbox (default ./user/mailbox)\n");
	printf("\n");
	printf("Summary of options for all modes:\n");
	printf("\n");
        printf("Common options for all DBMail utilities:\n");
	printf("     -f file   specify an alternative config file\n");
	printf("     -q        quietly skip interactive prompts\n"
	       "               use twice to suppress error messages\n");
	printf("     -v        verbose details\n");
	printf("     -V        show the version\n");
	printf("     -h        show this help message\n");
	
	return 0;
	
}
static int mailbox_dump(u64_t mailbox_idnr, const char *outfile)
{
	FILE *ostream;
	struct DbmailMailbox *mb = NULL;
	char *dir;

	/* 
	 * For dbmail the usual filesystem semantics don't really 
	 * apply. Mailboxes can contain other mailboxes as well as
	 * messages. For now however, this is solved by appending
	 * the mailboxname with .mbox
	 *
	 * TODO: facilitate maildir type exports
	 */
	dir = g_path_get_dirname(outfile);
	if (g_mkdir_with_parents(dir,0700)) {
		qerrorf("can't create directory [%s]\n", dir);
		g_free(dir);
		return 1;
	}
	g_free(dir);

	if (! (ostream = fopen(outfile,"a"))) {
		int err=errno;
		qerrorf("opening [%s] failed [%s]\n", outfile, strerror(err));
		return -1;
	}

	mb = dbmail_mailbox_new(mailbox_idnr);
	if (dbmail_mailbox_dump(mb,ostream) < 0)
		qerrorf("exporing failed\n");

	if (mb)
		dbmail_mailbox_free(mb);
	return 0;
}
	

int main(int argc, char *argv[])
{
	int opt = 0, opt_prev = 0;
	int show_help = 0;
	int result = 0;
	char *user = NULL,*mailbox=NULL, *outfile=NULL;
	u64_t useridnr = 0, mailbox_idnr = 0;

	openlog(PNAME, LOG_PID, LOG_MAIL);
	setvbuf(stdout, 0, _IONBF, 0);

	g_mime_init(0);

	/* get options */
	opterr = 0;		/* suppress error message from getopt() */
	while ((opt = getopt(argc, argv,
		"-u:m:o:" /* Major modes */
		"f:qvVh" /* Common options */ )) != -1) {
		/* The initial "-" of optstring allows unaccompanied
		 * options and reports them as the optarg to opt 1 (not '1') */
		if (opt == 1)
			opt = opt_prev;
		opt_prev = opt;

		switch (opt) {
		/* Major modes of operation
		 * (exactly one of these is required) */
		case 'u':
			if (optarg && strlen(optarg))
				user = optarg;
			break;

		case 'm':
			if (optarg && strlen(optarg))
				mailbox = optarg;
			break;
		case 'o':
			if (optarg && strlen(optarg))
				outfile = optarg;
			break;

		/* Common options */
		case 'f':
			if (optarg && strlen(optarg) > 0)
				configFile = optarg;
			else {
				qerrorf("dbmail-mailbox: -f requires a filename\n\n");
				result = 1;
			}
			break;

		case 'h':
			show_help = 1;
			break;

		case 'q':
			/* If we get q twice, be really quiet! */
			if (quiet)
				reallyquiet = 1;
			if (!verbose)
				quiet = 1;
			break;

		case 'v':
			if (!quiet)
				verbose = 1;
			break;

		case 'V':
			/* Show the version and return non-zero. */
			printf("\n*** DBMAIL: dbmail-users version "
			       "$Revision: 1891 $ %s\n\n", COPYRIGHT);
			result = 1;
			break;

		default:
			/* printf("unrecognized option [%c], continuing...\n",optopt); */
			break;
		}

		/* If there's a non-negative return code,
		 * it's time to free memory and bail out. */
		if (result)
			goto freeall;
	}	

	/* If nothing is happening, show the help text. */
	if (!user || show_help) {
		do_showhelp();
		result = 1;
		goto freeall;
	}
	/* read the config file */
        if (config_read(configFile) == -1) {
                qerrorf("Failed. Unable to read config file %s\n", configFile);
                result = -1;
                goto freeall;
        }
                
	SetTraceLevel("DBMAIL");
	GetDBParams(&_db_params);

	/* open database connection */
	if (db_connect() != 0) {
		qerrorf ("Failed. Could not connect to database (check log)\n");
		result = -1;
		goto freeall;
	}

	/* open authentication connection */
	if (auth_connect() != 0) {
		qerrorf("Failed. Could not connect to authentication (check log)\n");
		result = -1;
		goto freeall;
	}

	/* Verify the existence of this user */
	if (auth_user_exists(user, &useridnr) == -1) {
		qerrorf("Error: cannot verify existence of user [%s].\n", user);
		result = -1;
		goto freeall;
	}
	if (useridnr == 0) {
		qerrorf("Error: user [%s] does not exist.\n", user);
		result = -1;
		goto freeall;
	}

	if (mailbox) {
		if (db_findmailbox(mailbox, useridnr, &mailbox_idnr) != 1) {
			qerrorf("Error: cannot verify existence of mailbox [%s].\n", mailbox);
			result = -1;
			goto freeall;
		}
		if (! outfile) {
			GString *t = g_string_new("");
			g_string_printf(t, "%s/%s", user, mailbox);
			outfile=t->str;
			g_string_free(t,FALSE);
		}
		
		qerrorf("  export mailbox /%s/%s -> %s\n", user, mailbox, outfile);
		mailbox_dump(mailbox_idnr, outfile);

	} else {
		u64_t *children;
		u64_t owner_idnr, mailbox_idnr;
		char *dumpfile;
		unsigned nchildren,i;

		mailbox = g_new0(char, IMAP_MAX_MAILBOX_NAMELEN);
		db_findmailbox_by_regex(useridnr, "*", &children, &nchildren, 0);
		qerrorf("Exporting [%u] mailboxes for [%s]\n", nchildren, user);
		for (i=0; i< nchildren; i++) {
			mailbox_idnr = children[i];
			db_getmailboxname(children[i], useridnr, mailbox);			
			if (! db_get_mailbox_owner(mailbox_idnr, &owner_idnr)) {
				qerrorf("Error checking mailbox ownership");
				goto freeall;
			}
			if (owner_idnr != useridnr)
				continue;
			
			if (outfile && chdir(outfile)) {
				int serr = errno;
				qerrorf("%s\n", strerror(serr));
				goto freeall;
			} else if (!outfile) {
				outfile = ".";
			}

			dumpfile = g_strdup_printf("%s/%s.mbox", user, mailbox);
			qerrorf(" export mailbox /%s/%s -> %s/%s\n", user, mailbox, outfile, dumpfile);
			if (mailbox_dump(mailbox_idnr, dumpfile)) {
				g_free(dumpfile);
				goto freeall;
			}
			g_free(dumpfile);
		}


	}

	/* Here's where we free memory and quit.
	 * Be sure that all of these are NULL safe! */
freeall:
	
	db_disconnect();
	auth_disconnect();
	config_free();
	g_mime_shutdown();

	if (result < 0)
		qerrorf("Command failed.\n");
	return result;
}