File: sieve-file-storage-active.c

package info (click to toggle)
dovecot 1%3A2.3.4.1-5%2Bdeb10u6
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 48,648 kB
  • sloc: ansic: 500,433; makefile: 7,372; sh: 5,592; cpp: 1,555; perl: 303; python: 73; xml: 44; pascal: 27
file content (403 lines) | stat: -rw-r--r-- 10,302 bytes parent folder | download
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/* Copyright (c) 2002-2018 Pigeonhole authors, see the included COPYING file
 */

#include "lib.h"
#include "path-util.h"
#include "ioloop.h"
#include "hostpid.h"
#include "file-copy.h"

#include "sieve-file-storage.h"

#include <unistd.h>

/*
 * Symlink manipulation
 */

static int sieve_file_storage_active_read_link
(struct sieve_file_storage *fstorage, const char **link_r)
{
	struct sieve_storage *storage = &fstorage->storage;
	const char *error = NULL;
	int ret;

	ret = t_readlink(fstorage->active_path, link_r, &error);

	if ( ret < 0 ) {
		*link_r = NULL;

		if ( errno == EINVAL ) {
			/* Our symlink is no symlink. Report 'no active script'.
			 * Activating a script will automatically resolve this, so
			 * there is no need to panic on this one.
			 */
			if ( (storage->flags & SIEVE_STORAGE_FLAG_READWRITE) != 0 &&
				(storage->flags & SIEVE_STORAGE_FLAG_SYNCHRONIZING) == 0 ) {
				sieve_storage_sys_warning(storage,
					"Active sieve script symlink %s is no symlink.",
				  fstorage->active_path);
			}
			return 0;
		}

		if ( errno == ENOENT ) {
			/* Symlink not found */
			return 0;
		}

		/* We do need to panic otherwise */
		sieve_storage_set_critical(storage,
			"Performing t_readlink() on active sieve symlink '%s' failed: %s",
			fstorage->active_path, error);
		return -1;
	}

	/* ret is now assured to be valid, i.e. > 0 */
	return 1;
}

static const char *sieve_file_storage_active_parse_link
(struct sieve_file_storage *fstorage, const char *link,
	const char **scriptname_r)
{
	struct sieve_storage *storage = &fstorage->storage;
	const char *fname, *scriptname, *scriptpath, *link_dir;

	/* Split off directory from link path */
	fname = strrchr(fstorage->active_path, '/');
	if (fname == NULL)
		link_dir = "";
	else
		link_dir = t_strdup_until(fstorage->active_path, fname+1);

	/* Split link into path and filename */
	fname = strrchr(link, '/');
	if ( fname != NULL ) {
		scriptpath = t_strdup_until(link, fname+1);
		fname++;
	} else {
		scriptpath = "";
		fname = link;
	}

	/* Check the script name */
	scriptname = sieve_script_file_get_scriptname(fname);

	/* Warn if link is deemed to be invalid */
	if ( scriptname == NULL ) {
		sieve_storage_sys_warning(storage,
			"Active Sieve script symlink %s is broken: "
			"Invalid scriptname (points to %s).",
			fstorage->active_path, link);
		return NULL;
	}

	/* Check whether the path is any good */
	const char *error = NULL;
	if ( t_normpath_to(scriptpath, link_dir, &scriptpath, &error) < 0 ) {
		sieve_storage_sys_warning(storage,
			"Failed to check active Sieve script symlink %s: "
			"Failed to normalize path (points to %s): %s",
			fstorage->active_path, scriptpath, error);
		return NULL;
	}
	if ( strcmp(scriptpath, fstorage->path) != 0 ) {
		sieve_storage_sys_warning(storage,
			"Active sieve script symlink %s is broken: "
			"Invalid/unknown path to storage (points to %s).",
			fstorage->active_path, scriptpath);
		return NULL;
	}

	if ( scriptname_r != NULL )
		*scriptname_r = scriptname;

	return fname;
}

int sieve_file_storage_active_replace_link
(struct sieve_file_storage *fstorage, const char *link_path)
{
	struct sieve_storage *storage = &fstorage->storage;
	const char *active_path_new;
	struct timeval *tv, tv_now;
	int ret = 0;

	tv = &ioloop_timeval;

	for (;;) {
		/* First the new symlink is created with a different filename */
		active_path_new = t_strdup_printf
			("%s-new.%s.P%sM%s.%s",
				fstorage->active_path,
				dec2str(tv->tv_sec), my_pid,
				dec2str(tv->tv_usec), my_hostname);

		ret = symlink(link_path, active_path_new);

		if ( ret < 0 ) {
			/* If link exists we try again later */
			if ( errno == EEXIST ) {
				/* Wait and try again - very unlikely */
				sleep(2);
				tv = &tv_now;
				if (gettimeofday(&tv_now, NULL) < 0)
					i_fatal("gettimeofday(): %m");
				continue;
			}

			/* Other error, critical */
			sieve_storage_set_critical(storage,
				"Creating symlink() %s to %s failed: %m",
				active_path_new, link_path);
			return -1;
		}

		/* Link created */
		break;
	}

	/* Replace the existing link. This activates the new script */
	ret = rename(active_path_new, fstorage->active_path);

	if ( ret < 0 ) {
		/* Failed; created symlink must be deleted */
		i_unlink(active_path_new);
		sieve_storage_set_critical(storage,
			"Performing rename() %s to %s failed: %m",
			active_path_new, fstorage->active_path);
		return -1;
	}

	return 1;
}

/*
 * Active script properties
 */

int sieve_file_storage_active_script_get_file
(struct sieve_file_storage *fstorage, const char **file_r)
{
	const char *link, *scriptfile;
	int ret;

	*file_r = NULL;

	/* Read the active link */
	if ( (ret=sieve_file_storage_active_read_link(fstorage, &link)) <= 0 )
		return ret;

	/* Parse the link */
	scriptfile = sieve_file_storage_active_parse_link(fstorage, link, NULL);

	if (scriptfile == NULL) {
		/* Obviously, someone has been playing with our symlink:
		 * ignore this situation and report 'no active script'.
		 * Activation should fix this situation.
		 */
		return 0;
	}

	*file_r = scriptfile;
	return 1;
}

int sieve_file_storage_active_script_get_name
(struct sieve_storage *storage, const char **name_r)
{
	struct sieve_file_storage *fstorage =
		(struct sieve_file_storage *)storage;
	const char *link;
	int ret;

	*name_r = NULL;

	/* Read the active link */
	if ( (ret=sieve_file_storage_active_read_link
		(fstorage, &link)) <= 0 )
		return ret;

	if ( sieve_file_storage_active_parse_link
		(fstorage, link, name_r) == NULL ) {
		/* Obviously, someone has been playing with our symlink:
		 * ignore this situation and report 'no active script'.
		 * Activation should fix this situation.
		 */
		return 0;
	}

	return 1;
}

/*
 * Active script
 */ 

struct sieve_script *sieve_file_storage_active_script_open
(struct sieve_storage *storage)
{
	struct sieve_file_storage *fstorage =
		(struct sieve_file_storage *)storage;
	struct sieve_file_script *fscript;
	const char *scriptfile, *link;
	int ret;

	sieve_storage_clear_error(storage);

	/* Read the active link */
	if ( (ret=sieve_file_storage_active_read_link(fstorage, &link)) <= 0 ) {
		if ( ret < 0 )
			return NULL;

		/* Try to open the active_path as a regular file */
		if ( S_ISDIR(fstorage->st.st_mode) ) {
			fscript = sieve_file_script_open_from_path(fstorage,
				fstorage->active_path, NULL, NULL);			
		} else {
			fscript = sieve_file_script_open_from_name(fstorage, NULL);
		}
		if ( fscript == NULL ) {
			if ( storage->error_code != SIEVE_ERROR_NOT_FOUND ) {
				sieve_storage_set_critical(storage,
					"Failed to open active path `%s' as regular file: %s",
					fstorage->active_path, storage->error);
			}
			return NULL;
		}

		return &fscript->script;
	} 

	/* Parse the link */
	scriptfile = sieve_file_storage_active_parse_link(fstorage, link, NULL);
	if (scriptfile == NULL) {
		/* Obviously someone has been playing with our symlink,
		 * ignore this situation and report 'no active script'.
		 * Activation should fix this situation.
		 */
		sieve_storage_set_error(storage, SIEVE_ERROR_NOT_FOUND,
			"Active script is invalid");
		return NULL;
	}

	fscript = sieve_file_script_open_from_path(fstorage,
		fstorage->active_path,
		sieve_script_file_get_scriptname(scriptfile),
		NULL);
	if ( fscript == NULL && storage->error_code == SIEVE_ERROR_NOT_FOUND ) {
		sieve_storage_sys_warning(storage,
			"Active sieve script symlink %s points to non-existent script "
			"(points to %s).", fstorage->active_path, link);
	}
	return (fscript != NULL ? &fscript->script : NULL);
}

int sieve_file_storage_active_script_get_last_change
(struct sieve_storage *storage, time_t *last_change_r)
{
	struct sieve_file_storage *fstorage =
		(struct sieve_file_storage *)storage;
	struct stat st;

	/* Try direct lstat first */
	if ( lstat(fstorage->active_path, &st) == 0 ) {
		if ( !S_ISLNK(st.st_mode) ) {
			*last_change_r = st.st_mtime;
			return 0;
		}
	}
	/* Check error */
	else if ( errno != ENOENT ) {
		sieve_storage_set_critical(storage,
			"lstat(%s) failed: %m", fstorage->active_path);
	}

	/* Fall back to statting storage directory */
	return sieve_storage_get_last_change(storage, last_change_r);
}

bool sieve_file_storage_active_rescue_regular
(struct sieve_file_storage *fstorage)
{
	struct sieve_storage *storage = &fstorage->storage;
	struct stat st;

	/* Stat the file */
	if ( lstat(fstorage->active_path, &st) != 0 ) {
		if ( errno != ENOENT ) {
			sieve_storage_set_critical(storage,
				"Failed to stat active sieve script symlink (%s): %m.",
				fstorage->active_path);
			return FALSE;
		}
		return TRUE;
	}

	if ( S_ISLNK( st.st_mode ) ) {
		sieve_storage_sys_debug(storage,
			"Nothing to rescue %s.", fstorage->active_path);
		return TRUE; /* Nothing to rescue */
	}

	/* Only regular files can be rescued */
	if ( S_ISREG( st.st_mode ) ) {
		const char *dstpath;
		bool result = TRUE;

 		T_BEGIN {

			dstpath = t_strconcat( fstorage->path, "/",
				sieve_script_file_from_name("dovecot.orig"), NULL );
			if ( file_copy(fstorage->active_path, dstpath, TRUE) < 1 ) {
				sieve_storage_set_critical(storage,
					"Active sieve script file '%s' is a regular file "
					"and copying it to the script storage as '%s' failed. "
					"This needs to be fixed manually.",
					fstorage->active_path, dstpath);
				result = FALSE;
			} else {
				sieve_storage_sys_info(storage,
					"Moved active sieve script file '%s' "
					"to script storage as '%s'.",
					fstorage->active_path, dstpath);
			}
		} T_END;

		return result;
	}

	sieve_storage_set_critical(storage,
		"Active sieve script file '%s' is no symlink nor a regular file. "
		"This needs to be fixed manually.", fstorage->active_path);
	return FALSE;
}

int sieve_file_storage_deactivate(struct sieve_storage *storage)
{
	struct sieve_file_storage *fstorage =
		(struct sieve_file_storage *)storage;
	int ret;

	if ( sieve_file_storage_pre_modify(storage) < 0 )
		return -1;

	if ( !sieve_file_storage_active_rescue_regular(fstorage) )
		return -1;

	/* Delete the symlink, so no script is active */
	ret = unlink(fstorage->active_path);

	if ( ret < 0 ) {
		if ( errno != ENOENT ) {
			sieve_storage_set_critical(storage,
				"Failed to deactivate Sieve: "
				"unlink(%s) failed: %m", fstorage->active_path);
			return -1;
		} else {
			return 0;
		}
	}
	return 1;
}