summaryrefslogtreecommitdiffabout
path: root/src/wydawca.c
blob: 9ec0cc7c2fee4072510bfacf844b98e59ab91dc4 (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
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
/* wydawca - automatic release submission daemon
   Copyright (C) 2007, 2009, 2010, 2011, 2012, 2013, 2017, 2019 Sergey
   Poznyakoff

   Wydawca 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 3 of the License, or (at your
   option) any later version.

   Wydawca 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 wydawca. If not, see <http://www.gnu.org/licenses/>. */

#include "wydawca.h"
#include <gpgme.h>

char *program_name;
uid_t wydawca_uid;
gid_t wydawca_gid;
size_t wydawca_supp_groupc;
gid_t *wydawca_supp_groups;
int wydawca_runas; /* Set to 1 if any of the four variables above have
		      been set in the configuration */
char *conffile = SYSCONFDIR "/wydawca.rc";
const char *wy_version = "wydawca (" PACKAGE_STRING ")";
int wy_debug_level;
int wy_dry_run;
int wy_log_to_stderr = -1;		/* -1 means autodetect */
int wy_log_facility = LOG_LOCAL1;
char *wy_syslog_tag = "wydawca";
int syslog_include_prio;	/* syslog messages include priority */
unsigned long print_stats;	/* Print final statistics output */
time_t file_sweep_time = 0;
char *tar_command_name = "tar";
int archive_signatures = 1;	/* Archive sig files by default */
int lint_mode = 0;
int preprocess_only = 0;
int cron_option = 0;
int foreground_option = -1;
int single_process_option = -1;

int daemon_mode = 0;
int foreground;
int single_process;
time_t wakeup_interval;
struct grecs_list *all_spool_aliases;
char *wy_gpg_homedir;
char *default_check_script;
struct grecs_sockaddr listen_sockaddr;

unsigned wydawca_stat[WY_MAX_STAT];

unsigned min_directive_version = MIN_DIRECTIVE_VERSION;
unsigned max_directive_version = MAX_DIRECTIVE_VERSION;

int inotify_enable = 1;

void
initstats()
{
	memset(wydawca_stat, 0, sizeof wydawca_stat);
}

/* Logging */
void
syslog_printer(int prio, const char *fmt, va_list ap)
{
	if (syslog_include_prio) {
		static char *fmtbuf;
		static size_t fmtsize;
		const char *p = wy_pritostr(prio);
		size_t size = strlen(p) + 3 + strlen(fmt) + 1;

		if (size > fmtsize) {
			fmtsize = size;
			fmtbuf = grecs_realloc(fmtbuf, fmtsize);
		}
		sprintf(fmtbuf, "[%s] %s", p, fmt);
		fmt = fmtbuf;
	}
#if HAVE_VSYSLOG
	vsyslog(prio, fmt, ap);
#else
	char buf[128];
	vsnprintf(buf, sizeof buf, fmt, ap);
	syslog(prio, "%s", buf);
#endif
}

void
stderr_printer(int prio, const char *fmt, va_list ap)
{
	const char *p = wy_pritostr(prio);
	fprintf(stderr, "%s: ", program_name);

	if (p)
		fprintf(stderr, "[%s] ", p);

	vfprintf(stderr, fmt, ap);
	fputc('\n', stderr);
}

static void (*log_printer) (int prio, const char *fmt, va_list ap) =
    stderr_printer;

void
wy_vlog(int prio, char const *fmt, va_list ap)
{
	switch (prio) {
	case LOG_EMERG:
	case LOG_ALERT:
	case LOG_CRIT:
	case LOG_ERR:
		UPDATE_STATS(WY_STAT_ERRORS);
		break;

	case LOG_WARNING:
		UPDATE_STATS(WY_STAT_WARNINGS);
	}

	log_printer(prio, fmt, ap);
}

void
wy_log(int prio, char const *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);
	wy_vlog(prio, fmt, ap);
	va_end(ap);
}

void
wy_dbg(char const *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	log_printer(LOG_DEBUG, fmt, ap);
	va_end(ap);
}


static char *stat_name[WY_MAX_STAT] = {
	N_("errors"),
	N_("warnings"),
	N_("bad signatures"),
	N_("access violation attempts"),
	N_("complete triplets"),
	N_("incomplete triplets"),
	N_("bad triplets"),
	N_("expired triplets"),
	N_("triplet successes"),
	N_("files uploaded"),
	N_("files archived"),
	N_("symlinks created"),
	N_("symlinks removed"),
	N_("check failures"),
};

static char *stat_kwname[WY_MAX_STAT] = {
	"stat:errors",
	"stat:warnings",
	"stat:bad_signatures",
	"stat:access_violations",
	"stat:complete_triplets",
	"stat:incomplete_triplets",
	"stat:bad_triplets",
	"stat:expired_triplets",
	"stat:triplet_success",
	"stat:uploads",
	"stat:archives",
	"stat:symlinks",
	"stat:rmsymlinks",
	"stat:check_failures"
};

int
wy_stat_mask_p(unsigned long mask)
{
	int i;

	for (i = 0; i < WY_MAX_STAT; i++)
		if (wydawca_stat[i] != 0 && (mask && WY_STAT_MASK(i)))
			return 1;
	return 0;
}

int
wy_stat_expansion(char **ret, char const *name, size_t len)
{
	int i;

	for (i = 0; i < WY_MAX_STAT; i++) {
		if (strlen(stat_kwname[i]) == len
		    && memcmp(stat_kwname[i], name, len) == 0) {
			size_t size = 0;
			*ret = NULL;
			if (grecs_asprintf(ret, &size, "%u", wydawca_stat[i]))
				return WRDSE_NOSPACE;
		}
	}
	return WRDSE_OK;
}

void
logstats()
{
	int i;

	if (wy_stat_mask_p(print_stats)) {
		for (i = 0; i < WY_MAX_STAT; i++)
			if (print_stats & WY_STAT_MASK(i))
				wy_log(LOG_INFO, "%s: %u",
				       gettext(stat_name[i]), wydawca_stat[i]);
	}

	notify_finish();
}

void
grecs_print_diag(grecs_locus_t *locus, int err, int errcode, const char *msg)
{
	char *locstr = NULL;

	if (locus) {
		size_t size = 0;

		if (locus->beg.col == 0)
			grecs_asprintf(&locstr, &size, "%s:%u",
				       locus->beg.file, locus->beg.line);
		else if (strcmp(locus->beg.file, locus->end.file))
			grecs_asprintf(&locstr, &size, "%s:%u.%u-%s:%u.%u",
				       locus->beg.file,
				       locus->beg.line, locus->beg.col,
				       locus->end.file,
				       locus->end.line, locus->end.col);
		else if (locus->beg.line != locus->end.line)
			grecs_asprintf(&locstr, &size, "%s:%u.%u-%u.%u",
				       locus->beg.file,
				       locus->beg.line, locus->beg.col,
				       locus->end.line, locus->end.col);
		else
			grecs_asprintf(&locstr, &size, "%s:%u.%u-%u",
				       locus->beg.file,
				       locus->beg.line, locus->beg.col,
				       locus->end.col);
	}

	if (locstr) {
		if (errcode)
			wy_log(err ? LOG_ERR : LOG_WARNING, "%s: %s: %s",
			       locstr, msg, strerror(errcode));
		else
			wy_log(err ? LOG_ERR : LOG_WARNING, "%s: %s",
			       locstr, msg);
		free(locstr);
	} else {
		if (errcode)
			wy_log(err ? LOG_ERR : LOG_WARNING, "%s: %s", msg,
			       strerror(errcode));
		else
			wy_log(err ? LOG_ERR : LOG_WARNING, "%s", msg);
	}
}

static int uidc;
static uid_t *uidv;

static void
collect_uids(int argc, char **argv)
{
	int i;

	uidc = argc;
	uidv = grecs_calloc(uidc, sizeof(uidv[0]));
	for (i = 0; i < argc; i++) {
		struct passwd *pw = getpwnam(argv[i]);
		if (!pw) {
			char *p;
			unsigned n = strtoul(argv[i], &p, 10);
			if (*p) {
				wy_log(LOG_ERR, _("no such user: %s"),
				       argv[i]);
				exit(EX_NOUSER);
			}
			uidv[i] = n;
		} else
			uidv[i] = pw->pw_uid;
	}
}

char **x_argv;
extern int reconfigure;

void
wydawca_daemon()
{
	if (!foreground) {
		if (daemon(0, 0)) {
			wy_log(LOG_ERR, "%s", strerror(errno));
			exit(EX_OSERR);
		}
		wy_log(LOG_NOTICE, _("daemon launched"));
	}

	check_pidfile();
	wydawca_listener();
	remove_pidfile();
}

#include "cmdline.h"

void
version_hook(FILE * stream)
{
	printf("Compiled with:");
#ifdef WITH_LIBWRAP
	printf(" libwrap");
#endif
#ifdef WITH_INOTIFY
	printf(" inotify");
#endif
	putchar('\n');
}

int
main(int argc, char **argv)
{
	struct grecs_node *tree;
	const char *p;

	program_name = argv[0];
	proginfo.print_version_hook = version_hook;
	config_init();

	x_argv = argv;
	parse_options(argc, argv);

	wydawca_uid = getuid();
	wydawca_gid = getgid();
	
	argv += optind;
	argc -= optind;

	p = gpgme_check_version("1.1.0");
	if (!p) {
		wy_log(LOG_CRIT, _("GPGMe version check failed"));
		exit(EX_UNAVAILABLE);
	} else 
		wy_debug(4, (_("using GPGME version %s"), p));

	if (argc)
		collect_uids(argc, argv);

	if (preprocess_only)
		exit(grecs_preproc_run(conffile, grecs_preprocessor) ?
		     EX_CONFIG : 0);

	tree = grecs_parse(conffile);
	if (!tree)
		exit(EX_CONFIG);

	config_finish(tree);
	modules_load();
	if (lint_mode && wy_debug_level) {
		grecs_print_node(tree,
				 GRECS_NODE_FLAG_DEFAULT|
				  (wy_debug_level > 1 ?
				    GRECS_NODE_FLAG_LOCUS : 0),
				 stdout);
		fputc('\n', stdout);
	}
	grecs_tree_free(tree);

	if (lint_mode)
		exit(0);

	if (wy_dry_run || selected_spools())
		cron_option = 1;
	if (cron_option)
		daemon_mode = 0;
	if (foreground_option >= 0)
		foreground = foreground_option;
	if (single_process_option >= 0)
		single_process = single_process_option;

	if (wy_log_to_stderr == -1)
		wy_log_to_stderr = !daemon_mode || foreground;

	grecs_log_to_stderr = wy_log_to_stderr;
	if (!wy_log_to_stderr) {
		openlog(wy_syslog_tag, LOG_PID, wy_log_facility);
		log_printer = syslog_printer;
	}

	if (getgid() == 0) {
		if (wydawca_uid == 0) {
			if (!force_startup) {
				wy_log(LOG_CRIT,
				       _("won't run with root privileges"));
				exit(EX_UNAVAILABLE);
			}
		} else if (wydawca_runas
			   && wydawca_userprivs(wydawca_uid, wydawca_gid,
						wydawca_supp_groups,
						wydawca_supp_groupc))
			exit(EX_UNAVAILABLE);
	}

	wydawca_lock_init();

	wy_log(LOG_NOTICE, _("wydawca (%s) started"), PACKAGE_STRING);

	if (!daemon_mode) {
		scan_all_spools(uidc, uidv);
		logstats();
	} else
		wydawca_daemon();

	modules_close();
	
	wy_log(LOG_NOTICE, _("wydawca (%s) finished"), PACKAGE_STRING);

	if (reconfigure) {
		int i;
		for (i = getdtablesize(); i > 2; i--)
			close(i);
		remove_pidfile();
		execv(x_argv[0], x_argv);
	}

	exit(0);
}

Return to:

Send suggestions and report system problems to the System administrator.