-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathqmail-newmrh.c
More file actions
84 lines (71 loc) · 2.01 KB
/
qmail-newmrh.c
File metadata and controls
84 lines (71 loc) · 2.01 KB
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
/*
* Revision 20171130, Kai Peter
* - changed folder name 'control' to 'etc'
* Revision 20170317, Kai Peter
* - switched to 'buffer'
* - added 'rename.h'
* Revision 20170306, Kai Peter
* - updated to new(er) cdb lib from ucspi-tcp-0.88
* - changed return type of main to int
* - added '<sys/stat.h> to prevent compiler warnings
*/
#include <unistd.h>
#include <sys/stat.h>
#include "getln.h"
#include "strerr.h"
#include "stralloc.h"
#include "open.h"
#include "auto_qmail.h"
#include "cdbmake.h"
#include "case.h"
#include "rename.h"
#define FATL "qmail-newmrh: fatal: "
void die_read()
{
strerr_die2sys(111,FATL,"unable to read control/morercpthosts: ");
}
void die_write()
{
strerr_die2sys(111,FATL,"unable to write to control/morercpthosts.tmp: ");
}
char inbuf[1024];
buffer ssin;
int fd;
int fdtemp;
struct cdb_make c;
stralloc line = {0};
int match;
int main()
{
umask(033);
if (chdir(auto_qmail) == -1)
strerr_die4sys(111,FATL,"unable to chdir to ",auto_qmail,": ");
fd = open_read("etc/morercpthosts");
if (fd == -1) die_read();
buffer_init(&ssin,read,fd,inbuf,sizeof inbuf);
fdtemp = open_trunc("etc/morercpthosts.tmp");
if (fdtemp == -1) die_write();
if (cdb_make_start(&c,fdtemp) == -1) die_write();
for (;;) {
if (getln(&ssin,&line,&match,'\n') != 0) die_read();
case_lowerb(line.s,line.len);
while (line.len) {
if (line.s[line.len - 1] == ' ') { --line.len; continue; }
if (line.s[line.len - 1] == '\n') { --line.len; continue; }
if (line.s[line.len - 1] == '\t') { --line.len; continue; }
if (line.s[0] != '#')
if (cdb_make_add(&c,line.s,line.len,"",0) == -1)
{
die_write();
break;
}
}
if (!match) break;
}
if (cdb_make_finish(&c) == -1) die_write();
if (fsync(fdtemp) == -1) die_write();
if (close(fdtemp) == -1) die_write(); /* NFS stupidity */
if (rename("etc/morercpthosts.tmp","etc/morercpthosts.cdb") == -1)
strerr_die2sys(111,FATL,"unable to move etc/morercpthosts.tmp to etc/morercpthosts.cdb");
_exit(0);
}