forked from brunonymous/vpopmail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vdeloldusers.c
203 lines (173 loc) · 4.78 KB
/
vdeloldusers.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
/*
* 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 <time.h>
#include "config.h"
#include "vpopmail.h"
#include "vauth.h"
#ifdef ENABLE_AUTH_LOGGING
#define DEFAULT_AGE 180
#define TOKENS ":\n"
char Domain[MAX_BUFF];
char SqlBuf[MAX_BUFF];
int Age;
int EveryDomain;
int Verbose;
int Delete;
int UsersToDelete = 0;
void usage();
void get_options(int argc,char **argv);
void process_all_domains(time_t nowt);
void deloldusers(char *Domain, time_t nowt);
int main(int argc, char *argv[])
{
time_t nowt;
if( vauth_open( 1 )) {
vexiterror( stderr, "Initial open." );
}
get_options(argc,argv);
/* get the time */
nowt = time(NULL);
/* subtract the age */
nowt = nowt - (86400*Age);
if(EveryDomain == 0) {
deloldusers(Domain,nowt);
} else {
process_all_domains(nowt);
}
if( ! UsersToDelete ) {
printf("no old users found\n");
}
if( ! Delete && UsersToDelete) {
printf(" ** no users were deleted **\n");
printf(" ** use -D to delete users **\n");
}
return(vexit(0));
}
void usage()
{
printf("vdeloldusers: usage: [options]\n");
printf("options: -a age_in_days (will delete accounts older than this date)\n");
printf(" (default is 6 months or 180 days)\n");
printf(" -v (print version number and exit)\n");
printf(" -d [domain] (process only [domain])\n");
printf(" -e (process every domain)\n");
printf(" -D (actually delete users. no users are deleted without this option)\n");
printf(" -V (verbose -- print old users that will be deleted)\n");
}
void get_options(int argc,char **argv)
{
int c;
int errflag;
memset(Domain, 0, sizeof(Domain));
Age = DEFAULT_AGE;
EveryDomain = 0;
Verbose = 0;
Delete = 0;
errflag = 0;
while( !errflag && (c=getopt(argc,argv,"vVDd:a:e")) != -1 ) {
switch(c) {
case 'e':
EveryDomain = 1;
break;
case 'D':
Delete = 1;
break;
case 'V':
Verbose = 1;
break;
case 'd':
snprintf(Domain, sizeof(Domain), "%s", optarg);
EveryDomain = 0;
break;
case 'a':
Age = atoi(optarg);
break;
case 'v':
printf("version: %s\n", VERSION);
vexit(-1);
break;
default:
errflag = 1;
break;
}
}
if (argc <= 1 ) {
usage();
vexit(-1);
}
if ( ! EveryDomain && strlen(Domain) == 0) {
printf("error: you must supply either the -e or -d [domain] options\n");
vexit(-1);
}
}
void deloldusers(char *Domain, time_t nowt)
{
time_t mytime;
static struct vqpasswd *mypw;
int first = 1;
while( (mypw = vauth_getall(Domain, first, 0)) != NULL ) {
first = 0;
if ( strcmp(mypw->pw_name, "postmaster") == 0 ) {
if ( Verbose) {
printf("skipping postmaster@%s\n", Domain);
}
} else {
mytime = vget_lastauth(mypw, Domain);
if ( mytime != 0 ) {
if(mytime < nowt) {
UsersToDelete = 1;
if ( Verbose) {
printf("%s@%s\n", mypw->pw_name, Domain);
}
if( Delete ) {
vdeluser(mypw->pw_name, Domain);
}
}
}
}
}
}
void process_all_domains(time_t nowt)
{
domain_entry *entry;
entry = get_domain_entries( Domain );
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 ) {
deloldusers(entry->domain,nowt);
entry = get_domain_entries(NULL);
}
}
#else
int main()
{
printf("auth logging was not enabled, reconfigure with --enable-auth-logging=y\n");
return(vexit(-1));
}
#endif /* ENABLE_AUTH_LOGGING */