summaryrefslogtreecommitdiff
path: root/src/qmail-badloadertypes.c
blob: 84e5b51844e7b93afb75e0dc7e320988a7628985 (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
#include <sys/stat.h>
#include <unistd.h>

#include <stdio.h>
#include <stdlib.h>

#include "buffer.h"
#include "cdbmake.h"
#include "exit.h"
#include "getln.h"
#include "logmsg.h"
#include "open.h"
#include "stralloc.h"

#ifdef USE_CONFIG
  #include "fehsqm-config.h"
#else
  #include "auto_qmail.h"
#endif

#define WHO        "qmail-badloadertypes"
#define LOADER_LEN 5

static void die_read()
{
  logmsg(WHO, 111, FATAL, "unable to read control/badloadertypes");
}

static void die_write()
{
  logmsg(WHO, 111, FATAL, "unable to write to control/badloadertypes.tmp");
}

char inbuf[1024];
buffer b;

int fd;
int fdtemp;

struct cdb_make cdb;
stralloc line = {0};
int match;

int main()
{
  umask(033);
  if (chdir(auto_qmail) == -1) logmsg(WHO, 111, FATAL, B("unable to chdir to: ", auto_qmail));

  fd = open_read("control/badloadertypes");
  if (fd == -1) die_read();

  buffer_init(&b, read, fd, inbuf, sizeof(inbuf));

  fdtemp = open_trunc("control/badloadertypes.tmp");
  if (fdtemp == -1) die_write();

  if (cdb_make_start(&cdb, fdtemp) == -1) die_write();

  for (;;) {
    if (getln(&b, &line, &match, '\n') != 0) die_read();
    if (line.s[0] != '#' && line.len > LOADER_LEN)
      if (cdb_make_add(&cdb, line.s, LOADER_LEN, "", 0) == -1) die_write();
    if (!match) break;
  }

  if (cdb_make_finish(&cdb) == -1) die_write();
  if (fsync(fdtemp) == -1) die_write();
  if (close(fdtemp) == -1) die_write(); /* NFS stupidity */
  if (rename("control/badloadertypes.tmp", "control/badloadertypes.cdb") == -1)
    logmsg(WHO, 111, FATAL, "unable to move control/badloadertypes.tmp to control/badloadertypes.cdb");

  _exit(0);
}