aboutsummaryrefslogtreecommitdiff
path: root/src/wydawca.c
blob: 509947b8250f68dd2317a7623c4b683cd9561037 (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
/* wydawca - automatic release submission daemon
   Copyright (C) 2007, 2009 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 "mail.h"
#include "argmatch.h"
#include "version-etc.h"

uid_t wydawca_uid;
gid_t wydawca_gid;
size_t wydawca_supp_groupc;
gid_t *wydawca_supp_groups;
char *conffile = SYSCONFDIR "/wydawca.rc" ;
int debug_level;
int dry_run_mode;
int log_to_stderr = -1; /* -1 means autodetect */
int log_facility = LOG_LOCAL1;
char *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;
gl_list_t all_spool_aliases;

struct grecs_sockaddr listen_sockaddr;

unsigned wydawca_stat[MAX_STAT];

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 = mu_syslog_priority_to_string (prio);
      size_t size = strlen (p) + 3 + strlen (fmt) + 1;

      if (size > fmtsize)
	{
	  fmtsize = size;
	  fmtbuf = x2realloc (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 = mu_syslog_priority_to_string (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
logmsg (int prio, char *fmt, ...)
{
  va_list ap;

  switch (prio)
    {
    case LOG_EMERG:
    case LOG_ALERT:
    case LOG_CRIT:
    case LOG_ERR:
      UPDATE_STATS (STAT_ERRORS);
      break;

    case LOG_WARNING:
      UPDATE_STATS (STAT_WARNINGS);
    }

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


static char *stat_name[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"),
};

static char *stat_kwname[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",
};

int
stat_mask_p (unsigned long mask)
{
  int i;

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

static const char *
stat_expand (struct metadef *def, void *data)
{
  char sbuf[INT_BUFSIZE_BOUND (wydawca_stat[0])];
  def->storage = xstrdup (uinttostr (wydawca_stat[(int) def->data], sbuf));
  def->value = def->storage;
  return def->value;
}

struct metadef *
make_stat_expansion (size_t count)
{
  int i;
  struct metadef *def, *p;
  def = xcalloc (MAX_STAT + count + 1, sizeof (def[0]));
  p = def + count;
  for (i = 0; i < MAX_STAT; i++, p++)
    {
      p->kw = stat_kwname[i];
      p->value = NULL;
      p->storage = NULL;
      p->expand = stat_expand;
      p->data = (void*) i;
    }
  p->kw = NULL;
  return def;
}

void
logstats ()
{
  int i;

  if (stat_mask_p (print_stats))
    {
      for (i = 0; i < MAX_STAT; i++)
	if (print_stats & STAT_MASK (i))
	  logmsg (LOG_INFO, "%s: %u", gettext (stat_name[i]), wydawca_stat[i]);
    }

  mail_stats ();
}


void
grecs_print_diag (grecs_locus_t *locus, int err, int errcode, const char *msg)
{
  if (locus)
    {
      if (errcode)
	logmsg (err ? LOG_ERR : LOG_WARNING, "%s:%lu: %s: %s",
		locus->file, (unsigned long)locus->line, msg,
		strerror (errcode));
      else
	logmsg (err ? LOG_ERR : LOG_WARNING, "%s:%lu: %s",
		locus->file, (unsigned long)locus->line, msg);
    }
  else
    {
      if (errcode)
	logmsg (err ? LOG_ERR : LOG_WARNING, "%s: %s", msg,
		strerror (errcode));
      else
	logmsg (err ? LOG_ERR : LOG_WARNING, "%s", msg);
    }
}



static int uidc;
static uid_t *uidv;

static int
collect_uids (int argc, char **argv)
{
  int i;
    
  uidc = argc;
  uidv = xcalloc (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)
	    {
	      logmsg (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))
	{
	  logmsg (LOG_ERR, "%s", strerror (errno));
	  exit (EX_OSERR);
	}
      logmsg (LOG_NOTICE, _("daemon launched"));
    }

  check_pidfile ();
  wydawca_listener ();
  remove_pidfile ();
}


#include "cmdline.h"

int
main (int argc, char **argv)
{
  program_name = argv[0];
  mu_register_all_mailer_formats ();
  config_init ();

  x_argv = argv;
  parse_options (argc, argv);

  argv += optind;
  argc -= optind;

  if (argc)
    collect_uids (argc, argv);

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

  if (grecs_parse (conffile))
    exit (EX_CONFIG);

  if (lint_mode)
    exit (0);

  if (dry_run_mode)
    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 (log_to_stderr == -1)
    log_to_stderr = (!daemon_mode || foreground) && isatty (0);

  grecs_log_to_stderr = log_to_stderr;
  if (!log_to_stderr)
    {
      openlog (syslog_tag, LOG_PID, log_facility);
      log_printer = syslog_printer;
    }

  if (getgid () == 0)
    {
      if (wydawca_uid == 0)
	{
	  if (!force_startup)
	    {
	      logmsg (LOG_CRIT, _("won't run with root privileges"));
	      exit (EX_UNAVAILABLE);
	    }
	}
      else if (wydawca_userprivs (wydawca_uid, wydawca_gid,
				  wydawca_supp_groups,
				  wydawca_supp_groupc))
	exit (EX_UNAVAILABLE);
    }
	
  mail_init ();
  wydawca_lock_init ();
   
  logmsg (LOG_NOTICE, _("wydawca (%s) started"), PACKAGE_STRING);

  if (!daemon_mode)
    {
      scan_all_spools (uidc, uidv);
      logstats ();
    }
  else
    wydawca_daemon ();
  
  logmsg (LOG_NOTICE, _("wydawca (%s) finished"), PACKAGE_STRING);
  
  mail_finish ();

  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.