forked from brunonymous/vpopmail
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvpopbull.c
More file actions
416 lines (365 loc) · 11.2 KB
/
Copy pathvpopbull.c
File metadata and controls
416 lines (365 loc) · 11.2 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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
* Copyright (C) 1999-2009 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 <strings.h>
#include <time.h>
#include <pwd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include "config.h"
#include "vpopmail.h"
#include "vauth.h"
#define MSG_BUF_SIZE 32768
#define TOKENS ":\r\n"
static char EmailFile[MAX_BUFF];
static char CurDir[MAX_BUFF];
static char ExcludeFile[MAX_BUFF];
static char Domain[MAX_BUFF];
static char TmpBuf[MAX_BUFF];
static char MsgBuf[MSG_BUF_SIZE];
static int Verbose;
static int DoNothing;
#define COPY_IT 0
#define HARD_LINK_IT 1
#define SYMBOLIC_LINK_IT 2
static int DeliveryMethod = COPY_IT;
int InsertDate = 1;
int EmailFileFlag = 0;
int ExcludeFileFlag = 0;
int process_domain(char *, FILE *, FILE *);
int copy_email( FILE *, char *, char *, struct vqpasswd *);
int in_exclude_list( FILE *, char *, char *);
void get_options(int argc,char **argv);
void usage();
int main(int argc, char *argv[])
{
FILE *fsi = NULL;
FILE *fsx = NULL;
char *domain;
char *domain_dir = NULL;
static struct stat statbuf;
domain_entry *entry;
memset(TmpBuf,0,sizeof(TmpBuf));
memset(MsgBuf,0,sizeof(MsgBuf));
Verbose = 0;
DoNothing = 0;
if ( argc == 1 ) {
usage();
vexit(-1);
}
if( vauth_open( 1 )) {
vexiterror( stderr, "Initial open." );
}
get_options(argc,argv);
getcwd(CurDir,sizeof(CurDir));
if ( EmailFileFlag == 1 ) {
if ( (fsi = fopen(EmailFile, "r")) == NULL ) {
fprintf(stderr, "Could not open file %s\n", EmailFile);
vexit(-1);
} else {
/* make sure the file size is not 0 */
stat(EmailFile, &statbuf);
if(statbuf.st_size == 0) {
fprintf(stderr, "Error: %s is empty\n", EmailFile);
vexit(-1);
}
/* check for existing date header */
while (fgets (TmpBuf, sizeof(TmpBuf), fsi) != NULL) {
/* check for end of headers (blank line) */
if (*TmpBuf == '\n') break;
if (strncasecmp ("Date: ", TmpBuf, 6) == 0) {
InsertDate = 0;
break;
}
}
rewind(fsi);
}
} else if (! DoNothing) {
/* require -f [email_file] */
fprintf(stderr, "Error: email_file not specified\n");
usage();
vexit(-1);
}
if ( ExcludeFileFlag == 1 ) {
if ( (fsx = fopen(ExcludeFile, "r")) == NULL ) {
fprintf(stderr, "Could not open file %s\n", ExcludeFile);
vexit(-1);
}
}
if (( EmailFile[0] != 0 || DoNothing == 1) && Domain[0] != 0 ) {
/* Process list of domains */
domain = strtok(Domain, " ");
while (domain != NULL ) {
if((vget_assign(domain, domain_dir, sizeof(domain_dir), NULL, NULL)) != NULL) {
process_domain(domain, fsi, fsx );
} else {
fprintf(stderr, "Error: domain %s does not exist\n", domain);
}
domain = strtok(NULL, " ");
}
vexit(0);
} else if ( (EmailFile[0] != 0 || DoNothing == 1) && Domain[0] == 0 ) {
entry = get_domain_entries( "" );
if (entry==NULL) {
if( verrori ) {
printf("Can't get domain entries - %s\n", verror( verrori ));
vexit(-1);
} else {
printf("What now - %s\n", verror( verrori ));
vexit(0);
}
}
while( entry ) {
if (strcmp (entry->domain, entry->realdomain) != 0) {
if (Verbose) {
fprintf (stderr, "skipping %s (alias of %s)\n",
entry->domain, entry->realdomain);
}
} else {
chdir(entry->path);
process_domain(entry->realdomain, fsi, fsx );
}
entry = get_domain_entries(NULL);
}
}
return(vexit(0));
}
int process_domain(char *domain, FILE *fsi, FILE *fsx)
{
char filename[MAX_BUFF+50];
char hostname[MAX_BUFF];
static struct vqpasswd *pwent;
time_t tm;
int pid;
int first = 1;
gethostname(hostname,sizeof(hostname));
pid=getpid();
time (&tm);
snprintf(filename, sizeof(filename), "%lu.%lu.%s",(long unsigned)tm,
(long unsigned)pid,hostname);
first = 1;
while( (pwent = vauth_getall(domain, first, 1)) != NULL ) {
first = 0;
if ( !in_exclude_list( fsx, domain, pwent->pw_name) ) {
if (DoNothing) {
printf("%s@%s\n", pwent->pw_name, domain);
} else {
if(copy_email( fsi, filename, domain, pwent) != 0) {
fprintf(stderr, "%s@%s: ERROR COPYING TO %s\n", pwent->pw_name,
domain, pwent->pw_dir);
} else if (Verbose) {
printf("%s@%s\n", pwent->pw_name, domain);
}
}
}
}
return(0);
}
int copy_email(FILE *fs_file, char *filename, char *domain, struct vqpasswd *pwent)
{
static char tmpbuf[MAX_BUFF];
static char tmpbuf1[MAX_BUFF];
FILE *fs;
int count;
struct stat mystatbuf;
uid_t uid;
gid_t gid;
int r;
/* At this point, we know that the user exists in the auth backend.
* Now we need to run some other checks before we can copy the
* bulletin into their maildir...
*/
/* test to see if the user has been allocated a userdir yet.
* Some of the auth backends (eg MySQL) allow users to be inserted
* into authsystem while leaving their dir blank. The idea here is
* that this information will get allocated/updated the first time
* the user does an AUTH or receives a msg.
*/
if ( pwent->pw_dir == NULL || pwent->pw_dir[0]==0 ) {
/* A dir hasnt been allocated to this user yet.
* Try and allocate one now
*/
/* retrieve the domain's uid/gid
* (required for the call to make_user_dir()
*/
if (vget_assign(domain, NULL, 0, &uid, &gid) == NULL) {
fprintf(stderr, "Failed to vget_assign() for %s\n", domain);
return (-1);
}
if ( make_user_dir(pwent->pw_name, domain, uid, gid) == NULL) {
fprintf(stderr, "Auto creation of maildir failed for %s@%s\n", pwent->pw_name, domain);
return(-1);
}
/* Re-read pwent, because we need to lookup the newly created
* pw_dir entry
*/
if ((pwent=vauth_getpw(pwent->pw_name, domain)) == NULL ) {
fprintf(stderr, "Failed to vauth_getpw() for %s@%s\n", pwent->pw_name, domain);
return(-1);
}
}
/* At this point, a dir must have been allocated to the user
* in the auth backend. So the next thing to do is test to see
* if the dir exists on the filesystem or not. If the dir doesnt
* exist, then we will try and create it
*/
if ( stat(pwent->pw_dir, &mystatbuf ) == -1 ) {
if ( vmake_maildir(domain, pwent->pw_dir )!= VA_SUCCESS ) {
fprintf(stderr, "Auto creation of maildir failed for %s@%s\n", pwent->pw_name, domain);
return(-1);
}
}
snprintf(tmpbuf, sizeof(tmpbuf), "%s/Maildir/new/%s", pwent->pw_dir, filename );
if ( DeliveryMethod == COPY_IT ) {
rewind(fs_file);
if ( (fs = fopen(tmpbuf, "w+")) == NULL ) {
return(-1);
}
fprintf(fs, "To: %s@%s\n", pwent->pw_name, domain);
if (InsertDate) fprintf(fs, "%s", date_header());
while((count=fread(MsgBuf,sizeof(char),MSG_BUF_SIZE,fs_file))
!= 0 ) {
fwrite( MsgBuf, sizeof(char), count, fs );
}
fclose(fs);
} else if ( DeliveryMethod == HARD_LINK_IT ) {
if (*EmailFile == '/')
r = snprintf(tmpbuf1, sizeof(tmpbuf1), "%s", EmailFile);
else
r = snprintf(tmpbuf1, sizeof(tmpbuf1), "%s/%s", CurDir, EmailFile);
if (r == -1) {
fprintf(stderr, "path too long\n");
return -1;
}
if ( link( tmpbuf1, tmpbuf) < 0 ) {
perror("link");
}
} else if ( DeliveryMethod == SYMBOLIC_LINK_IT ) {
if (*EmailFile == '/')
r = snprintf(tmpbuf1, sizeof(tmpbuf1), "%s", EmailFile);
else
r = snprintf(tmpbuf1, sizeof(tmpbuf1), "%s/%s", CurDir, EmailFile);
if (r == -1) {
fprintf(stderr, "path too long\n");
return -1;
}
if ( symlink( tmpbuf1, tmpbuf) < 0 ) {
perror("symlink");
}
} else {
fprintf(stderr, "no delivery method set\n");
return -1;
}
/* fix permissions */
chown(tmpbuf, VPOPMAILUID, VPOPMAILGID);
chmod(tmpbuf, 0600);
return(0);
}
int in_exclude_list( FILE *fsx, char *domain, char *user )
{
static char tmpbuf[MAX_BUFF];
static char emailaddr[MAX_BUFF];
int i;
if ( fsx == NULL ) {
return(0);
}
rewind(fsx);
snprintf(emailaddr, sizeof(emailaddr), "%s@%s", user, domain);
while (fgets(tmpbuf, sizeof(tmpbuf), fsx) != NULL) {
for(i=0;tmpbuf[i]!=0;++i) if (tmpbuf[i]=='\n') tmpbuf[i]=0;
if ( strcmp( tmpbuf, emailaddr ) == 0 ) {
return(1);
}
}
return(0);
}
void get_options(int argc, char **argv)
{
int n = 0;
int c;
int errflag;
extern char *optarg;
extern int optind;
memset(Domain, 0, sizeof(Domain));
memset(EmailFile, 0, sizeof(EmailFile));
memset(ExcludeFile, 0, sizeof(ExcludeFile));
errflag = 0;
EmailFileFlag = 0;
ExcludeFileFlag = 0;
while( !errflag && (c=getopt(argc,argv,"Vvcshnf:e:")) != -1 ) {
switch(c) {
case 'v':
printf("version: %s\n", VERSION);
break;
case 'V':
Verbose = 1;
break;
case 's':
DeliveryMethod = SYMBOLIC_LINK_IT;
break;
case 'c':
DeliveryMethod = COPY_IT;
break;
case 'f':
EmailFileFlag = 1;
snprintf(EmailFile, sizeof(EmailFile), "%s", optarg);
break;
case 'e':
ExcludeFileFlag = 1;
snprintf(ExcludeFile, sizeof(ExcludeFile), "%s", optarg);
break;
case 'h':
DeliveryMethod = HARD_LINK_IT;
break;
case 'n':
DoNothing = 1;
break;
default:
errflag = 1;
break;
}
}
if ( errflag > 0 ) {
usage();
vexit(-1);
}
n = 0;
while ( optind < argc ) {
if((n=1)) strncat(Domain, " ", sizeof(Domain)-strlen(Domain)-1);
strncat(Domain, argv[optind], sizeof(Domain)-strlen(Domain)-1);
n = 1;
++optind;
}
}
void usage()
{
printf("usage: vpopbull [options] -f [email_file] [virtual_domain] [...]\n");
printf(" -v (print version number)\n");
printf(" -V (verbose)\n");
printf(" -f email_file (file with message contents)\n");
printf(" -e exclude_email_addr_file (list of addresses to exclude)\n");
printf(" -n (don't mail. Use with -V to list accounts)\n");
printf(" -c (default, copy file)\n");
printf(" -h (use hard links)\n");
printf(" -s (use symbolic links)\n");
}