forked from qmail/qmailadmin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
dotqmail.c
169 lines (145 loc) · 3.55 KB
/
dotqmail.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
/*
* $Id: dotqmail.c,v 1.2.2.3 2004-11-20 01:10:41 tomcollins Exp $
* Copyright (C) 1999-2004 Inter7 Internet Technologies
*
* 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 <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <vpopmail.h>
#include <vpopmail_config.h>
/* undef some macros that get redefined in config.h below */
#undef PACKAGE
#undef VERSION
#undef PACKAGE_NAME
#undef PACKAGE_STRING
#undef PACKAGE_TARNAME
#undef PACKAGE_VERSION
#undef QMAILDIR
#include "config.h"
#include "dotqmail.h"
#include "qmailadmin.h"
#include "qmailadminx.h"
int dotqmail_delete_files(char *user)
{
return (! valias_delete (user, Domain));
}
int dotqmail_add_line(char *user, char *line)
{
return (valias_insert (user, Domain, line));
}
#ifdef VALIAS
int dotqmail_del_line(char *user, char *line)
{
return (valias_remove (user, Domain, line));
}
#else
#define _(String) gettext(String)
#define MAX_LINE 512
FILE *fr, *fw;
int lr,lw;
char LineBuf[MAX_LINE];
char *mydotqmail_name;
char *dotqmail_bak_name;
int dotqmail_open_files(char *user)
{
int i,j;
lr = 0;
lw = 0;
if (!user || !strlen(user)) return(1);
i = strlen(user);
mydotqmail_name = strcat(strcpy(malloc(8 + i), ".qmail-"), user);
for(j=8;mydotqmail_name[j]!=0;j++) {
if (mydotqmail_name[j]=='.') {
mydotqmail_name[j] = ':';
}
}
dotqmail_bak_name = strcat(strcpy(malloc(8 + i + 4),
mydotqmail_name), ".new");
fr = fopen(mydotqmail_name, "r");
if (!(fw = fopen(dotqmail_bak_name, "w"))) {
free(mydotqmail_name);
free(dotqmail_bak_name);
fclose(fr);
return(1);
}
return(0);
}
void dotqmail_close_files(char *user, int keep)
{
if (fr) fclose(fr);
if (fw) fclose(fw);
if (!keep) {
rename(dotqmail_bak_name, mydotqmail_name);
lr = lw;
} else {
unlink(dotqmail_bak_name);
}
if (!lr) unlink(mydotqmail_name);
free(mydotqmail_name);
free(dotqmail_bak_name);
}
int dotqmail_del_line(char *user, char *line)
{
int exist = 1;
int i;
if (dotqmail_open_files(user)) return(1);
if (fr) {
while (fgets(LineBuf, sizeof(LineBuf), fr)) {
i = strlen(line);
if (!strncmp(LineBuf, line, i)) {
exist = 0;
} else {
fputs(LineBuf, fw);
lw++;
}
lr++;
}
}
dotqmail_close_files(user, exist);
return(0);
}
int dotqmail_cleanup(char *user, char *line)
{
int exist = 0;
if (dotqmail_open_files(user)) return(1);
if (fr) {
while (fgets(LineBuf, sizeof(LineBuf), fr)) {
if (strncmp(LineBuf, line, sizeof(LineBuf))) {
exist = 1;
lw++;
fputs(LineBuf, fw);
}
lr++;
}
}
dotqmail_close_files(user, exist);
return(0);
}
int dotqmail_count(char *user)
{
if (dotqmail_open_files(user)) return(1);
if (fr) {
while (fgets(LineBuf, sizeof(LineBuf), fr)) {
lr++;
}
}
dotqmail_close_files(user, 1);
return(lr);
}
#endif