forked from brunonymous/vpopmail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dotqmail2valias.c
292 lines (259 loc) · 7.85 KB
/
dotqmail2valias.c
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
/*
* Copyright (C) 2004-2009 Inter7 Internet Technologies, Inc.
*
* Copyright (C) 2003-2004 Tom Collins
* Initial version of this program sponsored by ACIS Pty Ltd.
*
* Based on "vconvert.c,v 1.2 2003/10/20 18:59:57" in vpopmail 5.4.
* Copyright (C) 1999-2003 Inter7 Internet Technologies, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include "config.h"
#include "vpopmail.h"
#include "vauth.h"
/* Default behavior is to do nothing if there are already valias table
* entries for a dotqmail file to be processed. Define one of the
* following settings to do something instead of nothing.
*/
/* To delete the existing valias table entries and replace them
* with contents of current .qmail files, define
* DELETE_OLD_VALIAS_ENTRIES.
*/
/* #define DELETE_OLD_VALIAS_ENTRIES */
/* To delete .qmail files that have matching valias table
* entries (instead of attempting to convert them),
* define DELETE_OLD_DOTQMAIL_FILES.
*/
/* #define DELETE_OLD_DOTQMAIL_FILES */
int Debug = 0;
int AllDomains = 0;
int do_all_domains();
int conv_domain( char *);
void usage();
void get_options(int argc, char **argv);
/* replace all occurrences of c1 in s with c2 */
void strreplace (char *s, char c1, char c2)
{
char *p;
for (p = s; *p != '\0'; p++) if (*p == c1) *p = c2;
}
int main(int argc, char *argv[])
{
#ifndef VALIAS
fprintf (stderr, "You must enable valiases (./configure --enable-valias) to use this program.\n");
return -1;
#endif
if( vauth_open( 1 )) {
vexiterror( stderr, "Initial open." );
}
get_options(argc,argv);
if ( optind == argc ) {
if (AllDomains)
do_all_domains();
else {
usage();
return -1;
}
} else {
for(;optind<argc;++optind){
lowerit(argv[optind]);
printf("converting %s\n", argv[optind]);
if ( conv_domain( argv[optind] ) != 0 ) {
fprintf(stderr, "conversion of %s failed\n", argv[optind]);
/* should exit -1 here? */
}
}
}
return(vexit(0));
}
int do_all_domains()
{
FILE *fs;
char assign_file[MAX_BUFF];
static char tmpbuf[MAX_BUFF];
int i;
int r;
r = snprintf(assign_file, sizeof(assign_file), "%s/users/assign", QMAILDIR);
if (r == -1) {
perror("temporary string buffer too short\n");
vexit(-1);
}
if ( (fs=fopen(assign_file, "r"))==NULL ) {
r = snprintf(tmpbuf, sizeof(tmpbuf), "could not open qmail assign file at %s\n", assign_file);
if (r == -1) {
perror("temporary string buffer too short\n");
}
perror(tmpbuf);
vexit(-1);
}
while ( fgets(tmpbuf, sizeof(tmpbuf),fs) != NULL ) {
if (*tmpbuf != '+') continue; /* ignore non-domain entries */
for(i=1;tmpbuf[i]!=':';++i);
tmpbuf[i-1] = 0;
/* ignore non-domain entries */
if (strchr (tmpbuf, '.') == NULL) continue;
if ( tmpbuf[1] != '\n' ) {
printf("converting %s\n", &tmpbuf[1] );
if ( conv_domain( &tmpbuf[1] ) != 0 ) {
printf("conversion of %s failed\n", &tmpbuf[1]);
/* should vexit -1 here? */
}
}
}
fclose(fs);
return(0);
}
int conv_domain( char *domain )
{
char domcopy[MAX_BUFF];
char domainpath[MAX_BUFF];
char username[MAX_BUFF];
char alias_line[MAX_ALIAS_LINE];
char *p;
int r;
DIR *domaindir;
struct dirent *direntry;
char dotqmail_fn[MAX_BUFF];
FILE *dotqmail_fs;
int notlist;
int linecount;
snprintf (domcopy, sizeof(domcopy), "%s", domain);
if (vget_assign (domcopy, domainpath, sizeof(domainpath), NULL, NULL) == NULL) {
fprintf (stderr, "Error, domain %s not found in users/cdb.\n", domain);
return -1;
}
if (strcmp (domain, domcopy) != 0) {
fprintf (stderr, "Skipping %s (alias of %s).\n", domain, domcopy);
return -1;
}
if ((domaindir = opendir (domainpath)) == NULL) {
fprintf (stderr, "Error, couldn't open %s.\n", domainpath);
return -1;
}
while ((direntry = readdir(domaindir)) != NULL) {
/* don't process .qmail-default */
if (strcmp (".qmail-default", direntry->d_name) == 0) continue;
/* process all other files starting with ".qmail-" */
if (strncmp (".qmail-", direntry->d_name, 7) == 0) {
snprintf (username, sizeof(username), "%s", &direntry->d_name[7]);
r = snprintf (dotqmail_fn, sizeof(dotqmail_fn), "%s/%s", domainpath, direntry->d_name);
if (r == -1) {
fprintf(stderr, "Temporary string buffe too short\n");
return -1;
}
/* convert to email address (change ':' to '.') */
strreplace (username, ':', '.');
printf (" checking %s@%s...", username, domain);
/* Does a VALIAS already exist for the file? If so, throw an error. */
if (valias_select (username, domain) != NULL) {
#ifdef DELETE_OLD_VALIAS_ENTRIES
valias_delete (username, domain);
#else
#ifdef DELETE_OLD_DOTQMAIL_FILES
printf ("valias already exists, deleting %s.\n", direntry->d_name);
unlink (dotqmail_fn);
continue;
#else
printf ("valias already exists, skipping.\n");
continue;
#endif
#endif
}
/* Open the .qmail-alias file */
if ((dotqmail_fs = fopen (dotqmail_fn, "r")) == NULL ) {
printf ("Error opening %s, skipping.\n", dotqmail_fn);
continue;
}
/* if first line matches "|[^ ]+/ezmlm-", then it's a list and shouldn't get converted */
/* ! note that .qmail-list-owner has ezmlm-warn on second line ! */
notlist = 1;
linecount = 0;
while (notlist && (fgets (alias_line, sizeof(alias_line), dotqmail_fs) != NULL)) {
linecount++;
/* Determine if this is an ezmlm list (which shouldn't be converted to valias) */
/* if line is program delivery, and contains string "/ezmlm-" before the first " ",
then it is a list. */
if (alias_line[0] == '|') {
char *p1, *p2;
p1 = strstr (alias_line, "/ezmlm-");
p2 = strchr (alias_line, ' ');
if ( (p1 != NULL) && (p2 == NULL || p1 < p2) ) {
printf ("mailing list, skipping.\n");
notlist = 0;
}
}
/* strip trailing newline (if present) */
p = strchr (alias_line, '\n');
if (p) *p = '\0';
valias_insert (username, domain, alias_line);
}
fclose (dotqmail_fs);
if (notlist) {
printf (" converted %u entries.\n", linecount);
unlink (dotqmail_fn);
} else {
/* it's a list, so remove the valias entries we created */
valias_delete (username, domain);
}
}
}
closedir (domaindir);
return 0;
}
void usage()
{
fprintf(stdout, "usage: dotqmail2valias [options] [domainlist]\n");
fprintf(stdout, " Converts .qmail-alias files for listed domains to valias format.\n");
fprintf(stdout, " Options:\n");
fprintf(stdout, " -a = convert all domains\n");
fprintf(stdout, " -v = version\n");
fprintf(stdout, " -d = debug info\n");
}
void get_options(int argc, char **argv)
{
int c;
int errflag;
extern char *optarg;
extern int optind;
errflag = 0;
Debug = 0;
while( !errflag && (c=getopt(argc,argv,":avd")) != -1 ) {
switch(c) {
case 'a':
AllDomains = 1;
break;
case 'd':
Debug = 1;
break;
case 'v':
printf("version: %s\n", VERSION);
break;
default:
errflag = 1;
break;
}
}
if (errflag > 0) {
usage();
vexit(-1);
}
}