forked from brunonymous/vpopmail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackfill.c
271 lines (263 loc) · 6.31 KB
/
backfill.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
/*
* Copyright (C) 2009 Inter7 Internet Technologies, Inc.
*
* Revision 2.1 2009-01-12 10:38:56+05:30 Cprogrammer
* function to backfill empty slots in dir_control
*
*/
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "vpopmail.h"
#include "file_lock.h"
#include "vauth.h"
/*
* Generic remove a line from a file utility
* input: template to search for
* file to search inside
*
* output: less than zero on failure
* 0 if successful
* 1 if match found
*/
int
remove_line(char *template, char *filename, mode_t mode, int once_only)
{
int found;
char bak_file[MAX_BUFF], tmpbuf[MAX_BUFF];
struct stat statbuf;
char *ptr;
FILE *fs1, *fs2;
int fd;
#ifdef FILE_LOCKING
int lockfd;
char lockfile[MAX_BUFF];
#endif
if (stat(filename, &statbuf))
{
fprintf(stderr, "%s: %s\n", filename, strerror(errno));
return (-1);
}
#ifdef FILE_LOCKING
snprintf(lockfile, sizeof(lockfile), "%s.lock", filename);
if ((lockfd = open(lockfile, O_WRONLY | O_CREAT, S_IRUSR|S_IWUSR)) < 0)
{
fprintf(stderr, "could not open lock file %s: %s\n", lockfile, strerror(errno));
return(-1);
}
if (get_write_lock(lockfd) < 0 )
return(-1);
#endif
/*- format a new string */
snprintf(bak_file, sizeof(bak_file), "%s.bak", filename);
if (rename(filename, bak_file))
{
fprintf(stderr, "rename %s->%s: %s\n", filename, bak_file, strerror(errno));
#ifdef FILE_LOCKING
unlock_lock(lockfd, 0, SEEK_SET, 0);
close(lockfd);
#endif
return(-1);
}
/*- open the file and check for error */
if (!(fs1 = fopen(filename, "w+")))
{
rename(bak_file, filename);
#ifdef FILE_LOCKING
unlock_lock(lockfd, 0, SEEK_SET, 0);
close(lockfd);
#endif
fprintf(stderr, "fopen(%s, w+: %s\n", filename, strerror(errno));
return (-1);
}
fd = fileno(fs1);
if (fchmod(fd, mode) || fchown(fd, statbuf.st_uid, statbuf.st_gid))
{
rename(bak_file, filename);
#ifdef FILE_LOCKING
unlock_lock(lockfd, 0, SEEK_SET, 0);
close(lockfd);
#endif
fprintf(stderr, "chmod(%s, %d, %d, %o): %s\n", filename, statbuf.st_uid, statbuf.st_gid, mode,
strerror(errno));
return (-1);
}
/*- open in read mode and check for error */
if (!(fs2 = fopen(bak_file, "r+")))
{
rename(bak_file, filename);
#ifdef FILE_LOCKING
unlock_lock(lockfd, 0, SEEK_SET, 0);
close(lockfd);
#endif
fprintf(stderr, "fopen(%s, r+): %s\n", filename, strerror(errno));
fclose(fs1);
return (-1);
}
/*- pound away on the files run the search algorythm */
for (found = 0;;)
{
if (!fgets(tmpbuf, MAX_BUFF, fs2))
break;
if ((ptr = strchr(tmpbuf, '\n')) != NULL)
*ptr = 0;
if (once_only & found)
{
fprintf(fs1, "%s\n", tmpbuf);
continue;
}
if (strncmp(template, tmpbuf, strlen(template)))
fprintf(fs1, "%s\n", tmpbuf);
else
found++;
}
fclose(fs1);
fclose(fs2);
unlink(bak_file);
#ifdef FILE_LOCKING
unlock_lock(lockfd, 0, SEEK_SET, 0);
close(lockfd);
#endif
/*
* return 0 = everything went okay, but we didn't find it
* 1 = everything went okay and we found a match
*/
return (found);
}
/* Find the last occurrence of the substring needle in the string haystack */
char *strrstr(const char *haystack, const char *needle)
{
char *ptr = NULL;
char *last = NULL;
/* find the first occurrence of needle */
ptr = strstr(haystack, needle);
if (ptr == NULL) {
/* needle not found */
return NULL;
}
/* Keep searching forward for needle */
do {
last = ptr;
ptr++;
} while (ptr = strstr(ptr, needle));
/* The last known good location is in last */
return last;
}
char *
backfill(char *username, char *domain, char *path, int operation)
{
vdir_type vdir;
char *ptr = (char *) 0;
char filename[MAX_BUFF];
static char tmpbuf[MAX_BUFF];
int count, len;
#ifdef FILE_LOCKING
char lockfile[MAX_BUFF];
int lockfd;
#endif
uid_t uid;
gid_t gid;
FILE *fp;
int r;
if (!domain || !*domain)
return ((char *) 0);
if (!(ptr = vget_assign(domain, NULL, 0, &uid, &gid)))
{
fprintf(stderr, "%s: No such domain\n", domain);
return((char *) 0);
}
snprintf(filename, sizeof(filename), "%s/.dir_control_free", ptr);
if (operation == 1) /*- Delete */
{
if (!(fp = fopen(filename, "r")))
return ((char *) 0);
for (count = 1;;count++)
{
if (!fgets(tmpbuf, MAX_BUFF - 2, fp))
{
fclose(fp);
return ((char *) 0);
}
if (tmpbuf[(len = strlen(tmpbuf)) - 1] != '\n')
{
fprintf(stderr, "Line No %d in %s Exceeds %d chars\n", count, filename, MAX_BUFF - 2);
fclose(fp);
return ((char *) 0);
}
if ((ptr = strchr(tmpbuf, '#')))
*ptr = '\0';
for (ptr = tmpbuf; *ptr && isspace((int) *ptr); ptr++);
if (!*ptr)
continue;
tmpbuf[len - 1] = 0;
break;
}
fclose(fp);
if (remove_line(ptr, filename, VPOPMAIL_QMAIL_MODE, 1) == 1)
{
vread_dir_control(&vdir, domain, uid, gid);
if (vdir.cur_users)
++vdir.cur_users;
vwrite_dir_control(&vdir, domain, uid, gid);
return (ptr);
}
} else
if (operation == 2) /*- add */
{
snprintf(tmpbuf, sizeof(tmpbuf), "%s", path);
if ((ptr = strrstr(tmpbuf, username)))
{
if (ptr != tmpbuf)
ptr--;
if (*ptr == '/')
*ptr = 0;
} else {
/* if the username isn't in the path, something is clearly wrong */
return((char *) 0);
}
if ((ptr = strrstr(tmpbuf, domain)))
{
ptr += strlen(domain);
if (*ptr == '/')
ptr++;
if (ptr && *ptr)
{
#ifdef FILE_LOCKING
r = snprintf(lockfile, sizeof(lockfile), "%s.lock", filename);
if (r == -1) {
fprintf(stderr, "file name too long\n");
return((char *) 0);
}
if ((lockfd = open(lockfile, O_WRONLY | O_CREAT, S_IRUSR|S_IWUSR)) < 0)
{
fprintf(stderr, "could not open lock file %s: %s\n", lockfile, strerror(errno));
return((char *) 0);
}
if (get_write_lock(lockfd) < 0 )
return((char *) 0);
#endif
if (!(fp = fopen(filename, "a")))
{
#ifdef FILE_LOCKING
unlock_lock(lockfd, 0, SEEK_SET, 0);
close(lockfd);
#endif
return((char *) 0);
}
fprintf(fp, "%s\n", ptr);
fclose(fp);
#ifdef FILE_LOCKING
unlock_lock(lockfd, 0, SEEK_SET, 0);
close(lockfd);
#endif
return(ptr);
}
}
}
return((char *) 0);
}