1 /*
2  * mbsync - mailbox synchronizer
3  * Copyright (C) 2000-2002 Michael R. Elkins <me@mutt.org>
4  * Copyright (C) 2002-2006,2011 Oswald Buddenhagen <ossi@users.sf.net>
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  * As a special exception, mbsync may be linked with the OpenSSL library,
20  * despite that library's more restrictive license.
21  */
22 
23 #include "config.h"
24 
25 #include "sync.h"
26 
27 #include <assert.h>
28 #include <unistd.h>
29 #include <limits.h>
30 #include <pwd.h>
31 #include <sys/types.h>
32 #include <ctype.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36 
37 static store_conf_t *stores;
38 
39 char *
get_arg(conffile_t * cfile,int required,int * comment)40 get_arg( conffile_t *cfile, int required, int *comment )
41 {
42 	char *ret, *p, *t;
43 	int escaped, quoted;
44 	char c;
45 
46 	p = cfile->rest;
47 	assert( p );
48 	while ((c = *p) && isspace( (uchar)c ))
49 		p++;
50 	if (!c || c == '#') {
51 		if (comment)
52 			*comment = (c == '#');
53 		if (required) {
54 			error( "%s:%d: parameter missing\n", cfile->file, cfile->line );
55 			cfile->err = 1;
56 		}
57 		ret = NULL;
58 	} else {
59 		for (escaped = 0, quoted = 0, ret = t = p; c; c = *p) {
60 			p++;
61 			if (escaped && c >= 32) {
62 				escaped = 0;
63 				*t++ = c;
64 			} else if (c == '\\')
65 				escaped = 1;
66 			else if (c == '"')
67 				quoted ^= 1;
68 			else if (!quoted && isspace( (uchar)c ))
69 				break;
70 			else
71 				*t++ = c;
72 		}
73 		*t = 0;
74 		if (escaped) {
75 			error( "%s:%d: unterminated escape sequence\n", cfile->file, cfile->line );
76 			cfile->err = 1;
77 			ret = NULL;
78 		}
79 		if (quoted) {
80 			error( "%s:%d: missing closing quote\n", cfile->file, cfile->line );
81 			cfile->err = 1;
82 			ret = NULL;
83 		}
84 	}
85 	cfile->rest = p;
86 	return ret;
87 }
88 
89 char
parse_bool(conffile_t * cfile)90 parse_bool( conffile_t *cfile )
91 {
92 	if (!strcasecmp( cfile->val, "yes" ) ||
93 	    !strcasecmp( cfile->val, "true" ) ||
94 	    !strcasecmp( cfile->val, "on" ) ||
95 	    !strcmp( cfile->val, "1" ))
96 		return 1;
97 	if (strcasecmp( cfile->val, "no" ) &&
98 	    strcasecmp( cfile->val, "false" ) &&
99 	    strcasecmp( cfile->val, "off" ) &&
100 	    strcmp( cfile->val, "0" )) {
101 		error( "%s:%d: invalid boolean value '%s'\n",
102 		       cfile->file, cfile->line, cfile->val );
103 		cfile->err = 1;
104 	}
105 	return 0;
106 }
107 
108 int
parse_int(conffile_t * cfile)109 parse_int( conffile_t *cfile )
110 {
111 	char *p;
112 	int ret;
113 
114 	ret = strtol( cfile->val, &p, 10 );
115 	if (*p) {
116 		error( "%s:%d: invalid integer value '%s'\n",
117 		       cfile->file, cfile->line, cfile->val );
118 		cfile->err = 1;
119 		return 0;
120 	}
121 	return ret;
122 }
123 
124 uint
parse_size(conffile_t * cfile)125 parse_size( conffile_t *cfile )
126 {
127 	char *p;
128 	uint ret;
129 
130 	ret = strtoul( cfile->val, &p, 10 );
131 	if (*p == 'k' || *p == 'K')
132 		ret *= 1024, p++;
133 	else if (*p == 'm' || *p == 'M')
134 		ret *= 1024 * 1024, p++;
135 	if (*p == 'b' || *p == 'B')
136 		p++;
137 	if (*p) {
138 		fprintf (stderr, "%s:%d: invalid size '%s'\n",
139 		         cfile->file, cfile->line, cfile->val);
140 		cfile->err = 1;
141 		return 0;
142 	}
143 	return ret;
144 }
145 
146 static const struct {
147 	int op;
148 	const char *name;
149 } boxOps[] = {
150 	{ OP_EXPUNGE, "Expunge" },
151 	{ OP_CREATE, "Create" },
152 	{ OP_REMOVE, "Remove" },
153 };
154 
155 static int
getopt_helper(conffile_t * cfile,int * cops,channel_conf_t * conf)156 getopt_helper( conffile_t *cfile, int *cops, channel_conf_t *conf )
157 {
158 	char *arg;
159 	uint i;
160 
161 	if (!strcasecmp( "Sync", cfile->cmd )) {
162 		arg = cfile->val;
163 		do
164 			if (!strcasecmp( "Push", arg ))
165 				*cops |= XOP_PUSH;
166 			else if (!strcasecmp( "Pull", arg ))
167 				*cops |= XOP_PULL;
168 			else if (!strcasecmp( "ReNew", arg ))
169 				*cops |= OP_RENEW;
170 			else if (!strcasecmp( "New", arg ))
171 				*cops |= OP_NEW;
172 			else if (!strcasecmp( "Delete", arg ))
173 				*cops |= OP_DELETE;
174 			else if (!strcasecmp( "Flags", arg ))
175 				*cops |= OP_FLAGS;
176 			else if (!strcasecmp( "PullReNew", arg ))
177 				conf->ops[N] |= OP_RENEW;
178 			else if (!strcasecmp( "PullNew", arg ))
179 				conf->ops[N] |= OP_NEW;
180 			else if (!strcasecmp( "PullDelete", arg ))
181 				conf->ops[N] |= OP_DELETE;
182 			else if (!strcasecmp( "PullFlags", arg ))
183 				conf->ops[N] |= OP_FLAGS;
184 			else if (!strcasecmp( "PushReNew", arg ))
185 				conf->ops[F] |= OP_RENEW;
186 			else if (!strcasecmp( "PushNew", arg ))
187 				conf->ops[F] |= OP_NEW;
188 			else if (!strcasecmp( "PushDelete", arg ))
189 				conf->ops[F] |= OP_DELETE;
190 			else if (!strcasecmp( "PushFlags", arg ))
191 				conf->ops[F] |= OP_FLAGS;
192 			else if (!strcasecmp( "All", arg ) || !strcasecmp( "Full", arg ))
193 				*cops |= XOP_PULL|XOP_PUSH;
194 			else if (strcasecmp( "None", arg ) && strcasecmp( "Noop", arg )) {
195 				error( "%s:%d: invalid Sync arg '%s'\n",
196 				       cfile->file, cfile->line, arg );
197 				cfile->err = 1;
198 			}
199 		while ((arg = get_arg( cfile, ARG_OPTIONAL, NULL )));
200 		conf->ops[F] |= XOP_HAVE_TYPE;
201 	} else if (!strcasecmp( "SyncState", cfile->cmd ))
202 		conf->sync_state = expand_strdup( cfile->val );
203 	else if (!strcasecmp( "CopyArrivalDate", cfile->cmd ))
204 		conf->use_internal_date = parse_bool( cfile );
205 	else if (!strcasecmp( "MaxMessages", cfile->cmd ))
206 		conf->max_messages = parse_int( cfile );
207 	else if (!strcasecmp( "ExpireUnread", cfile->cmd ))
208 		conf->expire_unread = parse_bool( cfile );
209 	else {
210 		for (i = 0; i < as(boxOps); i++) {
211 			if (!strcasecmp( boxOps[i].name, cfile->cmd )) {
212 				int op = boxOps[i].op;
213 				arg = cfile->val;
214 				do {
215 					if (!strcasecmp( "Both", arg )) {
216 						*cops |= op;
217 					} else if (!strcasecmp( "Far", arg )) {
218 						conf->ops[F] |= op;
219 					} else if (!strcasecmp( "Master", arg )) {  // Pre-1.4 legacy
220 						conf->ops[F] |= op;
221 						cfile->ms_warn = 1;
222 					} else if (!strcasecmp( "Near", arg )) {
223 						conf->ops[N] |= op;
224 					} else if (!strcasecmp( "Slave", arg )) {  // Pre-1.4 legacy
225 						conf->ops[N] |= op;
226 						cfile->ms_warn = 1;
227 					} else if (strcasecmp( "None", arg )) {
228 						error( "%s:%d: invalid %s arg '%s'\n",
229 						       cfile->file, cfile->line, boxOps[i].name, arg );
230 						cfile->err = 1;
231 					}
232 				} while ((arg = get_arg( cfile, ARG_OPTIONAL, NULL )));
233 				conf->ops[F] |= op * (XOP_HAVE_EXPUNGE / OP_EXPUNGE);
234 				return 1;
235 			}
236 		}
237 		return 0;
238 	}
239 	return 1;
240 }
241 
242 int
getcline(conffile_t * cfile)243 getcline( conffile_t *cfile )
244 {
245 	char *arg;
246 	int comment;
247 
248 	if (cfile->rest && (arg = get_arg( cfile, ARG_OPTIONAL, NULL ))) {
249 		error( "%s:%d: excess token '%s'\n", cfile->file, cfile->line, arg );
250 		cfile->err = 1;
251 	}
252 	while (fgets( cfile->buf, cfile->bufl, cfile->fp )) {
253 		cfile->line++;
254 		cfile->rest = cfile->buf;
255 		if (!(cfile->cmd = get_arg( cfile, ARG_OPTIONAL, &comment ))) {
256 			if (comment)
257 				continue;
258 			return 1;
259 		}
260 		if (!(cfile->val = get_arg( cfile, ARG_REQUIRED, NULL )))
261 			continue;
262 		return 1;
263 	}
264 	return 0;
265 }
266 
267 /* XXX - this does not detect None conflicts ... */
268 int
merge_ops(int cops,int ops[])269 merge_ops( int cops, int ops[] )
270 {
271 	int aops, op;
272 	uint i;
273 
274 	aops = ops[F] | ops[N];
275 	if (ops[F] & XOP_HAVE_TYPE) {
276 		if (aops & OP_MASK_TYPE) {
277 			if (aops & cops & OP_MASK_TYPE) {
278 			  cfl:
279 				error( "Conflicting Sync args specified.\n" );
280 				return 1;
281 			}
282 			ops[F] |= cops & OP_MASK_TYPE;
283 			ops[N] |= cops & OP_MASK_TYPE;
284 			if (cops & XOP_PULL) {
285 				if (ops[N] & OP_MASK_TYPE)
286 					goto cfl;
287 				ops[N] |= OP_MASK_TYPE;
288 			}
289 			if (cops & XOP_PUSH) {
290 				if (ops[F] & OP_MASK_TYPE)
291 					goto cfl;
292 				ops[F] |= OP_MASK_TYPE;
293 			}
294 		} else if (cops & (OP_MASK_TYPE|XOP_MASK_DIR)) {
295 			if (!(cops & OP_MASK_TYPE))
296 				cops |= OP_MASK_TYPE;
297 			else if (!(cops & XOP_MASK_DIR))
298 				cops |= XOP_PULL|XOP_PUSH;
299 			if (cops & XOP_PULL)
300 				ops[N] |= cops & OP_MASK_TYPE;
301 			if (cops & XOP_PUSH)
302 				ops[F] |= cops & OP_MASK_TYPE;
303 		}
304 	}
305 	for (i = 0; i < as(boxOps); i++) {
306 		op = boxOps[i].op;
307 		if (ops[F] & (op * (XOP_HAVE_EXPUNGE / OP_EXPUNGE))) {
308 			if (aops & cops & op) {
309 				error( "Conflicting %s args specified.\n", boxOps[i].name );
310 				return 1;
311 			}
312 			ops[F] |= cops & op;
313 			ops[N] |= cops & op;
314 		}
315 	}
316 	return 0;
317 }
318 
319 int
load_config(const char * where)320 load_config( const char *where )
321 {
322 	conffile_t cfile;
323 	store_conf_t *store, **storeapp = &stores;
324 	channel_conf_t *channel, **channelapp = &channels;
325 	group_conf_t *group, **groupapp = &groups;
326 	string_list_t *chanlist, **chanlistapp;
327 	char *arg, *p;
328 	uint len, max_size;
329 	int cops, gcops, glob_ok, fn, i;
330 	char path[_POSIX_PATH_MAX];
331 	char buf[1024];
332 
333 	if (!where) {
334 		nfsnprintf( path, sizeof(path), "%s/." EXE "rc", Home );
335 		cfile.file = path;
336 	} else
337 		cfile.file = where;
338 
339 	info( "Reading configuration file %s\n", cfile.file );
340 
341 	if (!(cfile.fp = fopen( cfile.file, "r" ))) {
342 		sys_error( "Cannot open config file '%s'", cfile.file );
343 		return 1;
344 	}
345 	buf[sizeof(buf) - 1] = 0;
346 	cfile.buf = buf;
347 	cfile.bufl = sizeof(buf) - 1;
348 	cfile.line = 0;
349 	cfile.err = 0;
350 	cfile.ms_warn = 0;
351 	cfile.rest = NULL;
352 
353 	gcops = 0;
354 	glob_ok = 1;
355 	global_conf.expire_unread = -1;
356   reloop:
357 	while (getcline( &cfile )) {
358 		if (!cfile.cmd)
359 			continue;
360 		for (i = 0; i < N_DRIVERS; i++)
361 			if (drivers[i]->parse_store( &cfile, &store )) {
362 				if (store) {
363 					if (!store->max_size)
364 						store->max_size = UINT_MAX;
365 					if (!store->flat_delim)
366 						store->flat_delim = "";
367 					*storeapp = store;
368 					storeapp = &store->next;
369 					*storeapp = NULL;
370 				}
371 				glob_ok = 0;
372 				goto reloop;
373 			}
374 		if (!strcasecmp( "Channel", cfile.cmd ))
375 		{
376 			channel = nfcalloc( sizeof(*channel) );
377 			channel->name = nfstrdup( cfile.val );
378 			channel->max_messages = global_conf.max_messages;
379 			channel->expire_unread = global_conf.expire_unread;
380 			channel->use_internal_date = global_conf.use_internal_date;
381 			cops = 0;
382 			max_size = UINT_MAX;
383 			while (getcline( &cfile ) && cfile.cmd) {
384 				if (!strcasecmp( "MaxSize", cfile.cmd ))
385 					max_size = parse_size( &cfile );
386 				else if (!strcasecmp( "Pattern", cfile.cmd ) ||
387 				         !strcasecmp( "Patterns", cfile.cmd ))
388 				{
389 					arg = cfile.val;
390 					do
391 						add_string_list( &channel->patterns, arg );
392 					while ((arg = get_arg( &cfile, ARG_OPTIONAL, NULL )));
393 				}
394 				else if (!strcasecmp( "Far", cfile.cmd )) {
395 					fn = F;
396 					goto linkst;
397 				} else if (!strcasecmp( "Master", cfile.cmd )) {  // Pre-1.4 legacy
398 					fn = F;
399 					goto olinkst;
400 				} else if (!strcasecmp( "Near", cfile.cmd )) {
401 					fn = N;
402 					goto linkst;
403 				} else if (!strcasecmp( "Slave", cfile.cmd )) {  // Pre-1.4 legacy
404 					fn = N;
405 				  olinkst:
406 					cfile.ms_warn = 1;
407 				  linkst:
408 					if (*cfile.val != ':' || !(p = strchr( cfile.val + 1, ':' ))) {
409 						error( "%s:%d: malformed mailbox spec\n",
410 						       cfile.file, cfile.line );
411 						cfile.err = 1;
412 						continue;
413 					}
414 					*p = 0;
415 					for (store = stores; store; store = store->next)
416 						if (!strcmp( store->name, cfile.val + 1 )) {
417 							channel->stores[fn] = store;
418 							goto stpcom;
419 						}
420 					error( "%s:%d: unknown store '%s'\n",
421 					       cfile.file, cfile.line, cfile.val + 1 );
422 					cfile.err = 1;
423 					continue;
424 				  stpcom:
425 					if (*++p)
426 						channel->boxes[fn] = nfstrdup( p );
427 				} else if (!getopt_helper( &cfile, &cops, channel )) {
428 					error( "%s:%d: keyword '%s' is not recognized in Channel sections\n",
429 					       cfile.file, cfile.line, cfile.cmd );
430 					cfile.err = 1;
431 				}
432 			}
433 			if (!channel->stores[F]) {
434 				error( "channel '%s' refers to no far side store\n", channel->name );
435 				cfile.err = 1;
436 			} else if (!channel->stores[N]) {
437 				error( "channel '%s' refers to no near side store\n", channel->name );
438 				cfile.err = 1;
439 			} else if (merge_ops( cops, channel->ops ))
440 				cfile.err = 1;
441 			else {
442 				if (max_size != UINT_MAX) {
443 					if (!max_size)
444 						max_size = UINT_MAX;
445 					channel->stores[F]->max_size = channel->stores[N]->max_size = max_size;
446 				}
447 				*channelapp = channel;
448 				channelapp = &channel->next;
449 			}
450 			glob_ok = 0;
451 			goto reloop;
452 		}
453 		else if (!strcasecmp( "Group", cfile.cmd ))
454 		{
455 			group = nfmalloc( sizeof(*group) );
456 			group->name = nfstrdup( cfile.val );
457 			*groupapp = group;
458 			groupapp = &group->next;
459 			*groupapp = NULL;
460 			chanlistapp = &group->channels;
461 			*chanlistapp = NULL;
462 			while ((arg = get_arg( &cfile, ARG_OPTIONAL, NULL ))) {
463 			  addone:
464 				len = strlen( arg );
465 				chanlist = nfmalloc( sizeof(*chanlist) + len );
466 				memcpy( chanlist->string, arg, len + 1 );
467 				*chanlistapp = chanlist;
468 				chanlistapp = &chanlist->next;
469 				*chanlistapp = NULL;
470 			}
471 			while (getcline( &cfile ) && cfile.cmd) {
472 				if (!strcasecmp( "Channel", cfile.cmd ) ||
473 				    !strcasecmp( "Channels", cfile.cmd ))
474 				{
475 					arg = cfile.val;
476 					goto addone;
477 				}
478 				else
479 				{
480 					error( "%s:%d: keyword '%s' is not recognized in Group sections\n",
481 					       cfile.file, cfile.line, cfile.cmd );
482 					cfile.err = 1;
483 				}
484 			}
485 			glob_ok = 0;
486 			goto reloop;
487 		}
488 		else if (!strcasecmp( "FSync", cfile.cmd ))
489 		{
490 			UseFSync = parse_bool( &cfile );
491 		}
492 		else if (!strcasecmp( "FieldDelimiter", cfile.cmd ))
493 		{
494 			if (strlen( cfile.val ) != 1) {
495 				error( "%s:%d: Field delimiter must be exactly one character long\n", cfile.file, cfile.line );
496 				cfile.err = 1;
497 			} else {
498 				FieldDelimiter = cfile.val[0];
499 				if (!ispunct( FieldDelimiter )) {
500 					error( "%s:%d: Field delimiter must be a punctuation character\n", cfile.file, cfile.line );
501 					cfile.err = 1;
502 				}
503 			}
504 		}
505 		else if (!strcasecmp( "BufferLimit", cfile.cmd ))
506 		{
507 			BufferLimit = parse_size( &cfile );
508 			if (!BufferLimit) {
509 				error( "%s:%d: BufferLimit cannot be zero\n", cfile.file, cfile.line );
510 				cfile.err = 1;
511 			}
512 		}
513 		else if (!getopt_helper( &cfile, &gcops, &global_conf ))
514 		{
515 			error( "%s:%d: '%s' is not a recognized section-starting or global keyword\n",
516 			       cfile.file, cfile.line, cfile.cmd );
517 			cfile.err = 1;
518 			while (getcline( &cfile ))
519 				if (!cfile.cmd)
520 					goto reloop;
521 			break;
522 		}
523 		if (!glob_ok) {
524 			error( "%s:%d: global options may not follow sections\n",
525 			       cfile.file, cfile.line );
526 			cfile.err = 1;
527 		}
528 	}
529 	fclose (cfile.fp);
530 	if (cfile.ms_warn)
531 		warn( "Notice: Master/Slave are deprecated; use Far/Near instead.\n" );
532 	cfile.err |= merge_ops( gcops, global_conf.ops );
533 	if (!global_conf.sync_state)
534 		global_conf.sync_state = expand_strdup( "~/." EXE "/" );
535 	return cfile.err;
536 }
537