summaryrefslogtreecommitdiff
path: root/sqmail-4.3.07/src/newinclude.c
blob: 746b6a40ad6a26508cdb07afdc80f8e03356ca33 (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
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h> // rename
#include "buffer.h"
#include "logmsg.h"
#include "genalloc.h"
#include "stralloc.h"
#include "getln.h"
#include "open.h"
#include "token822.h"
#include "control.h"
#include "auto_qmail.h"
#include "byte.h"
#include "env.h"

#define WHO "newinclude"

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

void nomem()
{
  logmsg(WHO,111,FATAL,"out of memory");
}
void usage()
{
  logmsg(WHO,100,USAGE,"newinclude list");
}

char *fnlist;
char listbuf[1024];
buffer bi;

stralloc bin = {0};
#define fnbin bin.s
stralloc tmp = {0};
#define fntmp tmp.s
char tmpbuf[1024];
buffer bt;


void readerr()
{
  logmsg(WHO,111,FATAL,B("unable to read: ",fnlist));
}
void writeerr()
{
  logmsg(WHO,111,FATAL,B("unable to write to ",fntmp));
}

static void out(char *s,int len)
{
  if (buffer_put(&bt,s,len) == -1) writeerr();
}

void doincl(buf,len)
char *buf;
int len;
{
  if (!len)
    logmsg(WHO,111,FATAL,"empty :include: filenames not permitted");
  if (byte_chr(buf,len,'\n') != len)
    logmsg(WHO,111,FATAL,"newlines not permitted in :include: filenames");
  if (byte_chr(buf,len,'\0') != len)
    logmsg(WHO,111,FATAL,"NUL not permitted in :include: filenames");
  if ((buf[0] != '.') && (buf[0] != '/'))
    out("./",2);
  out(buf,len);
  out("",1);
}

void dorecip(buf,len)
char *buf;
int len;
{
  if (!len)
    logmsg(WHO,111,FATAL,"empty recipient addresses not permitted");
  if (byte_chr(buf,len,'\n') != len)
    logmsg(WHO,111,FATAL,"newlines not permitted in recipient addresses");
  if (byte_chr(buf,len,'\0') != len)
    logmsg(WHO,111,FATAL,"NUL not permitted in recipient addresses");
  if (len > 800)
    logmsg(WHO,111,FATAL,"addresses must be under 800 bytes");
  if ((buf[len - 1] == ' ') || (buf[len - 1] == '\t'))
    logmsg(WHO,111,FATAL,"spaces and tabs not permitted at ends of addresses");
  out("&",1);
  out(buf,len);
  out("",1);
}


void die_control()
{
  logmsg(WHO,111,FATAL,"unable to read controls");
}

stralloc me = {0};
stralloc defaulthost = {0};
stralloc defaultdomain = {0};
stralloc plusdomain = {0};

void readcontrols()
{
  int r;
  int fddir;
  char *x;

  fddir = open_read(".");
  if (fddir == -1)
    logmsg(WHO,111,FATAL,"unable to open current directory");

  if (chdir(auto_qmail) == -1)
    logmsg(WHO,111,FATAL,B("unable to chdir to: ",auto_qmail));

  r = control_readline(&me,"control/me");
  if (r == -1) die_control();
  if (!r) if (!stralloc_copys(&me,"me")) nomem();

  r = control_readline(&defaultdomain,"control/defaultdomain");
  if (r == -1) die_control();
  if (!r) if (!stralloc_copy(&defaultdomain,&me)) nomem();
  x = env_get("QMAILDEFAULTDOMAIN");
  if (x) if (!stralloc_copys(&defaultdomain,x)) nomem();

  r = control_readline(&defaulthost,"control/defaulthost");
  if (r == -1) die_control();
  if (!r) if (!stralloc_copy(&defaulthost,&me)) nomem();
  x = env_get("QMAILDEFAULTHOST");
  if (x) if (!stralloc_copys(&defaulthost,x)) nomem();

  r = control_readline(&plusdomain,"control/plusdomain");
  if (r == -1) die_control();
  if (!r) if (!stralloc_copy(&plusdomain,&me)) nomem();
  x = env_get("QMAILPLUSDOMAIN");
  if (x) if (!stralloc_copys(&plusdomain,x)) nomem();

  if (fchdir(fddir) == -1)
    logmsg(WHO,111,FATAL,"unable to set current directory");
}

stralloc cbuf = {0};
token822_alloc toks = {0};
token822_alloc tokaddr = {0};
stralloc address = {0};

void gotincl()
{
  token822_reverse(&tokaddr);
  if (token822_unquote(&address,&tokaddr) != 1) nomem();
  tokaddr.len = 0;
  doincl(address.s,address.len);
}

void gotaddr()
{
  int i;
  int j;
  int flaghasat;
 
  token822_reverse(&tokaddr);
  if (token822_unquote(&address,&tokaddr) != 1) nomem();
 
  flaghasat = 0;
  for (i = 0;i < tokaddr.len;++i)
    if (tokaddr.t[i].type == TOKEN822_AT)
      flaghasat = 1;
 
  tokaddr.len = 0;

  if (!address.len) return;

  if (!flaghasat)
    if (address.s[0] == '/') {
      if (!stralloc_0(&address)) nomem();
      logmsg(WHO,111,FATAL,B("file delivery for ",address.s," not supported"));
    }
  if (!flaghasat)
    if (address.s[0] == '|') {
      if (!stralloc_0(&address)) nomem();
      logmsg(WHO,111,FATAL,B("program delivery for ",address.s," not supported"));
    }

  if (!flaghasat) {
    if (!stralloc_cats(&address,"@")) nomem();
    if (!stralloc_cat(&address,&defaulthost)) nomem();
  }
  if (address.s[address.len - 1] == '+') {
    address.s[address.len - 1] = '.';
    if (!stralloc_cat(&address,&plusdomain)) nomem();
  }
  j = 0;
  for (i = 0;i < address.len;++i) if (address.s[i] == '@') j = i;
  for (i = j;i < address.len;++i) if (address.s[i] == '.') break;
  if (i == address.len) {
    if (!stralloc_cats(&address,".")) nomem();
    if (!stralloc_cat(&address,&defaultdomain)) nomem();
  }

  dorecip(address.s,address.len);
}


stralloc line = {0};
int match;

void parseerr()
{
  if (!stralloc_0(&line)) nomem();
  logmsg(WHO,111,FATAL,B("unable to parse this line: ",line.s));
}

void parseline()
{
  int wordok;
  struct token822 *t;
  struct token822 *beginning;
 
  switch (token822_parse(&toks,&line,&cbuf)) {
    case -1: nomem();
    case 0: parseerr();
  }

  beginning = toks.t;
  t = toks.t + toks.len;
  wordok = 1;
 
  if (!token822_readyplus(&tokaddr,1)) nomem();
  tokaddr.len = 0;
 
  while (t > beginning)
    switch ((--t)->type) {
      case TOKEN822_SEMI:
        break; /*XXX*/
      case TOKEN822_COLON:
        if (t >= beginning + 2)
          if (t[-2].type == TOKEN822_COLON)
            if (t[-1].type == TOKEN822_ATOM)
              if (t[-1].slen == 7)
                if (!byte_diff(t[-1].s,7,"include")) {
                  gotincl();
                  t -= 2;
                }
        break; /*XXX*/
      case TOKEN822_RIGHT:
        if (tokaddr.len) gotaddr();
        while ((t > beginning) && (t[-1].type != TOKEN822_LEFT))
          if (!token822_append(&tokaddr,--t)) nomem();
        gotaddr();
        if (t <= beginning) parseerr();
        --t;
        while ((t > beginning) && ((t[-1].type == TOKEN822_COMMENT) || (t[-1].type == TOKEN822_ATOM) || (t[-1].type == TOKEN822_QUOTE) || (t[-1].type == TOKEN822_AT) || (t[-1].type == TOKEN822_DOT)))
          --t;
        wordok = 0;
        continue;
      case TOKEN822_ATOM: case TOKEN822_QUOTE: case TOKEN822_LITERAL:
        if (!wordok) if (tokaddr.len) gotaddr();
        wordok = 0;
        if (!token822_append(&tokaddr,t)) nomem();
        continue;
      case TOKEN822_COMMENT:
        /* comment is lexically a space; shouldn't affect wordok */
        break;
      case TOKEN822_COMMA:
        if (tokaddr.len) gotaddr();
        wordok = 1;
        break;
      default:
        wordok = 1;
        if (!token822_append(&tokaddr,t)) nomem();
        continue;
    }
  if (tokaddr.len) gotaddr();
}

 
int main(argc,argv)
int argc;
char **argv;
{
  int fd;

  umask(033);
  readcontrols();

  fnlist = argv[1]; if (!fnlist) usage();

  if (!stralloc_copys(&bin,fnlist)) nomem();
  if (!stralloc_cats(&bin,".bin")) nomem();
  if (!stralloc_0(&bin)) nomem();

  if (!stralloc_copys(&tmp,fnlist)) nomem();
  if (!stralloc_cats(&tmp,".tmp")) nomem();
  if (!stralloc_0(&tmp)) nomem();

  fd = open_read(fnlist);
  if (fd == -1) readerr();
  buffer_init(&bi,read,fd,listbuf,sizeof(listbuf));

  fd = open_trunc(fntmp);
  if (fd == -1) writeerr();
  buffer_init(&bt,write,fd,tmpbuf,sizeof(tmpbuf));

  for (;;) {
    if (getln(&bi,&line,&match,'\n') == -1) readerr();
    if (!line.len) break;
    if (line.s[0] != '#') parseline();
    if (!match) break;
  }

  if (buffer_flush(&bt) == -1) writeerr();
  if (fsync(fd) == -1) writeerr();
  if (close(fd) == -1) writeerr(); /* NFS stupidity */

  if (rename(fntmp,fnbin) == -1)
    logmsg(WHO,111,FATAL,B("unable to move ",fntmp," to: ",fnbin));
  
  _exit(0);
}