summaryrefslogtreecommitdiff
path: root/src/qmail-newu.c
blob: db410c5ef68bce0d7f4f10a33ab324f0367f7c02 (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
#include <unistd.h>
#include <sys/stat.h>
#include <stdio.h> // rename
#include "stralloc.h"
#include "getln.h"
#include "buffer.h"
#include "cdbmake.h"
#include "exit.h"
#include "open.h"
#include "logmsg.h"
#include "case.h"
#include "byte.h"
#include "auto_qmail.h"
#include "qmail.h"

#define WHO "qmail-newu"

int rename(const char *,const char *); // stdio.h

void die_chdir()
{
  logmsg(WHO,110,ERROR,"unable to chdir");
}
void die_nomem()
{
  logmsg(WHO,111,FATAL,"fatal: out of memory");
}
void die_opena()
{
  logmsg(WHO,112,ERROR,"unable to open users/assign");
}
void die_reada()
{
  logmsg(WHO,110,ERROR,"unable to read users/assign");
}
void die_format()
{
  logmsg(WHO,112,ERROR,"bad format in users/assign");
}
void die_opent()
{
  logmsg(WHO,112,ERROR,"unable to open users/assign.cdb.tmp");
}
void die_writet()
{
  logmsg(WHO,112,ERROR,"unable to write users/assign.cdb.tmp");
}
void die_rename()
{
  logmsg(WHO,112,ERROR,"unable to move users/cdb.tmp to users/assign.cdb");
}

struct cdb_make cdb;
stralloc key = {0};
stralloc data = {0};

char inbuf[BUFSIZE_LINE];
buffer bi;

int fd;
int fdtemp;

stralloc line = {0};
int match;

stralloc wildchars = {0};

int main()
{
  int i;
  int numcolons;

  umask(033);
  if (chdir(auto_qmail) == -1) die_chdir();

  fd = open_read("users/assign");
  if (fd == -1) die_opena();

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

  fdtemp = open_trunc("users/assign.cdb.tmp");
  if (fdtemp == -1) die_opent();

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

  if (!stralloc_copys(&wildchars,"")) die_nomem();

  for (;;) {
    if (getln(&bi,&line,&match,'\n') != 0) die_reada();
    if (line.len && (line.s[0] == '.')) break;
    if (!match) die_format();

    if (byte_chr(line.s,line.len,'\0') < line.len) die_format();
    i = byte_chr(line.s,line.len,':');
    if (i == line.len) die_format();
    if (i == 0) die_format();
    if (!stralloc_copys(&key,"!")) die_nomem();
    if (line.s[0] == '+') {
      if (!stralloc_catb(&key,line.s + 1,i - 1)) die_nomem();
      case_lowerb(key.s,key.len);
      if (i >= 2)
        if (byte_chr(wildchars.s,wildchars.len,line.s[i - 1]) == wildchars.len)
      if (!stralloc_append(&wildchars,line.s + i - 1)) die_nomem();
    }
    else {
      if (!stralloc_catb(&key,line.s + 1,i - 1)) die_nomem();
      if (!stralloc_0(&key)) die_nomem();
      case_lowerb(key.s,key.len);
    }

    if (!stralloc_copyb(&data,line.s + i + 1,line.len - i - 1)) die_nomem();

    numcolons = 0;
    for (i = 0; i < data.len; ++i)
      if (data.s[i] == ':') {
        data.s[i] = 0;
        if (++numcolons == 6) break;
      }
    if (numcolons < 6) die_format();
    data.len = i;

    if (cdb_make_add(&cdb,key.s,key.len,data.s,data.len) == -1) die_writet();
  }

  if (cdb_make_add(&cdb,"",0,wildchars.s,wildchars.len) == -1) die_writet();

  if (cdb_make_finish(&cdb) == -1) die_writet();
  if (fsync(fdtemp) == -1) die_writet();
  if (close(fdtemp) == -1) die_writet(); /* NFS stupidity */
  if (rename("users/assign.cdb.tmp","users/assign.cdb") == -1) die_rename();

  _exit(0);
}