Repository navigation
Expand file tree
/
Copy pathnewaliases.c
More file actions
198 lines (166 loc) · 3.7 KB
/
Copy pathnewaliases.c
File metadata and controls
198 lines (166 loc) · 3.7 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
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
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sysexits.h>
#include <ctype.h>
#include <sys/mman.h>
#if HAVE_MALLOC_H
# include <malloc.h>
#endif
#if HAVE_ALLOCA_H
# include <alloca.h>
#endif
#include "dbif.h"
#include "aliases.h"
#include "env.h"
#include "domain.h"
#include "mymalloc.h"
static char*
token(char *p, char sep, char **next)
{
char *ret;
char *q;
int i;
ret = 0;
while (isspace(*p))
++p;
for (q = p; *q != '\n' && *q != sep; q++)
;
if (*q == sep) {
*next = q;
/* possibly good */
while ( (--q >= p) && isspace(*q) )
;
if (q >= p) {
if ( (ret = malloc(2 + (q-p))) != 0 ) {
for (i=0; (*p != sep) && (p <= q); p++, i++) {
if ( isascii(*p) && isupper(*p) )
ret[i] = tolower(*p);
else
ret[i] = *p;
}
ret[i++] = 0;
}
}
}
return ret;
}
static void
rebuild_db(char *domain)
{
char *atemp, *alias;
int fd;
char *atext;
size_t asize;
int nraliases = 0;
int longest = 0;
int total = 0;
int rv;
char *key;
DBhandle aliasdb;
char *q;
struct domain *dom = getdomain(domain);
if ( domain && (dom == 0) ) {
fprintf(stderr, "%s: not a known virtual domain\n", domain);
return;
}
alias = aliasfile(dom);
if ( (atemp = alloca(strlen(alias) + 10)) == 0 ) {
perror(alias);
exit(EX_TEMPFAIL);
}
sprintf(atemp, "%sXXXXXX", alias);
if ( ! mktemp(atemp) ) {
perror(atemp);
exit(EX_TEMPFAIL);
}
aliasdb = dbif_open(atemp, DBIF_WRITER|DBIF_CREAT, 0644);
if (aliasdb == 0) {
perror(atemp);
exit(EX_NOPERM);
}
if ( (fd = open(alias, O_RDONLY)) != -1 ) {
if (atext = mapfd(fd, &asize)) {
char *p, *nl, *end;
int size;
char *value;
for (p = atext, end = atext+asize; p && (p < end); p = 1+nl) {
if ( (nl = memchr(p, '\n', (end-p))) == 0 )
break;
key = token(p, ':', &p);
if ( key && (key[0] != '#') ) {
/* slurp in continuation lines */
while ( (nl < end-1) && isspace(nl[1]) ) {
if ( (q = memchr(nl+1,'\n',(end-nl)-1)) == 0 )
nl = end-1;
else
nl = q;
}
/* trim leading whitespace */
do { ++p; } while (isspace(*p) && *p != '\n');
/* trim trailing whitespace */
for (q = nl; (q > p) && isspace(*q); --q)
;
size = (q-p)+2;
if ( (q > p) && (value = malloc(size)) != 0 ) {
memcpy(value, p, size);
value[size-1] = 0;
rv = dbif_put(aliasdb,key,value,DBIF_INSERT);
if (rv < 0) {
fprintf(stderr, "alias %s", key);
if (errno)
fprintf(stderr, " : %s\n", strerror(errno));
else
fprintf(stderr, " : db error %d\n", rv);
unlink(atemp);
exit(EX_IOERR);
}
nraliases++;
if (size-1 > longest)
longest = size-1;
/* don't count null bytes in the total size */
total += (size + strlen(key) - 1);
free(value);
}
}
if (key) free(key);
}
munmap(atext, asize);
}
close(fd);
}
/* drop in a sendmail-compatable magic cookie */
dbif_put(aliasdb, "@", "@", DBIF_INSERT);
dbif_close(aliasdb);
if (dbif_rename(atemp, alias) != 0) {
perror(alias);
exit(EX_IOERR);
}
fprintf(stderr, "%s: %d alias%s",
alias,
nraliases, (nraliases != 1)?"es":"");
if (nraliases > 1)
fprintf(stderr, ", longest is %d byte%s", longest, (longest!=1)?"s":"");
fprintf(stderr, ", %d byte%s total.\n", total, (total!=1)?"s":"");
}
void
newaliases(int argc, char **argv)
{
int i;
if (argc == 0)
rebuild_db(0);
else for (i=0; i< argc; i++)
rebuild_db(argv[i]);
}
#ifdef DEBUG
main()
{
newaliases(0,0);
}
#endif