s/qmail 4.3.20
Next generation secure email transport
Loading...
Searching...
No Matches
setmaillist.c
Go to the documentation of this file.
1#include <unistd.h>
2#include <sys/stat.h>
3#include "buffer.h"
4#include "logmsg.h"
5#include "stralloc.h"
6#include "getln.h"
7#include "open.h"
8#include "byte.h"
9#include "qmail.h"
10
11#define WHO "setmaillist"
12
13int rename(const char *,const char *); // stdio.h
14
15void usage()
16{
17 logmsg(WHO,100,USAGE,"setmaillist list.bin list.tmp");
18}
19
20stralloc line = {0};
22
23char *fnbin;
24char *fntmp;
25int fd;
27buffer bo;
28
30{
31 logmsg(WHO,111,FATAL,B("unable to write to: ",fntmp));
32}
33
34static void out(char *s,int len)
35{
36 if (buffer_put(&bo,s,len) == -1) writeerr();
37}
38
39int main(int argc,char * const *argv)
40{
41 umask(033);
42
43 fnbin = argv[1]; if (!fnbin) usage();
44 fntmp = argv[2]; if (!fntmp) usage();
45
46 fd = open_trunc(fntmp);
47 if (fd == -1)
48 logmsg(WHO,111,FATAL,B("unable to create: ",fntmp));
49
50 buffer_init(&bo,buffer_unixwrite,fd,buf,sizeof(buf));
51
52
53 do {
54 if (getln(buffer_0small,&line,&match,'\n') == -1)
55 logmsg(WHO,111,FATAL,"unable to read input: ");
56
57 while (line.len) {
58 if (line.s[line.len - 1] != '\n')
59 if (line.s[line.len - 1] != ' ')
60 if (line.s[line.len - 1] != '\t')
61 break;
62 --line.len;
63 }
64
65 if (byte_chr(line.s,line.len,'\0') != line.len)
66 logmsg(WHO,111,FATAL,"NUL in input");
67
68 if (line.len)
69 if (line.s[0] != '#') {
70 if ((line.s[0] == '.') || (line.s[0] == '/')) {
71 out(line.s,line.len);
72 out("",1);
73 }
74 else {
75 if (line.len > 800)
76 logmsg(WHO,111,FATAL,"addresses must be under 800 bytes");
77 if (line.s[0] != '&')
78 out("&",1);
79 out(line.s,line.len);
80 out("",1);
81 }
82 }
83
84 } while (match);
85
86 if (buffer_flush(&bo) == -1) writeerr();
87 if (fsync(fd) == -1) writeerr();
88 if (close(fd) == -1) writeerr(); /* NFS stupidity */
89
90 if (rename(fntmp,fnbin) == -1)
91 logmsg(WHO,111,FATAL,B("unable to move ",fntmp," to: ",fnbin));
92
93 _exit(0);
94}
int main()
Definition: chkshsgr.c:6
stralloc out
Definition: dnscname.c:12
void _exit(int)
#define BUFSIZE_LINE
Definition: qmail.h:8
char * fntmp
Definition: setmaillist.c:24
void usage()
Definition: setmaillist.c:15
char * fnbin
Definition: setmaillist.c:23
int rename(const char *, const char *)
int fd
Definition: setmaillist.c:25
char buf[BUFSIZE_LINE]
Definition: setmaillist.c:26
stralloc line
Definition: setmaillist.c:20
void writeerr()
Definition: setmaillist.c:29
int match
Definition: setmaillist.c:21
buffer bo
Definition: setmaillist.c:27
#define WHO
Definition: setmaillist.c:11