-
Notifications
You must be signed in to change notification settings - Fork 0
/
emails.c
219 lines (173 loc) · 4.61 KB
/
emails.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
/*
* The Minger Email Address Verification Daemon
* Copyright (C) 2012 David De Grave <[email protected]>
*
* http://sourceforge.net/projects/mingerd
*
* 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 3 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, see http://www.gnu.org/licenses/.
*/
#include "emails.h"
#include "common.h"
#include "mingerd.h"
#include "log.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
typedef struct cache {
char *cache;
char *cursor;
int size;
int uselen;
time_t last; } CACHE;
static CACHE sysuser,
aliases;
static int increase_cache_buffer(CACHE *c)
{
char *t;
if ( (t = realloc(c->cache, c->size + CACHE_EXT)) == NULL ) {
printlog(stdlog, PL_ERR, "Error allocating memory: %m");
return -1;
}
c->cache = t;
c->cursor = c->cache + c->uselen;
c->size += CACHE_EXT;
return 0;
}
static int reset_cache(CACHE *c)
{
if ( ! c->size && increase_cache_buffer(c) < 0 )
return -1;
c->cache[0] = '|';
c->cursor = c->cache + 1;
c->uselen = 1;
c->last = 0;
return 0;
}
static int add_to_cache(CACHE *c, const char *u)
{
int len;
len = c->uselen + strlen(u) + 1 /*'|'*/ + 1 /*'\0'*/;
while ( len > c->size )
if ( increase_cache_buffer(c) < 0 ) return -1;
len = sprintf(c->cursor, "%s|", u);
c->cursor += len;
c->uselen += len;
return 0;
}
int load_sysuser_cache(const char *fpasswd)
{
FILE *f;
char b[256], *u, *i;
if ( (f = fopen(fpasswd, "rt")) == NULL ) {
printlog(stdlog, PL_ERR, "Error opening file %s for reading: %m", fpasswd);
return -1;
}
if ( reset_cache(&sysuser) < 0 ) {
fclose(f);
return -1;
}
while ( fgets(b, sizeof(b), f) ) {
trim(b);
if ( ! b[0] || b[0] == '#' ) continue; /* Skip comments & empty lines */
/* username:x:510:510::/home/username:/bin/bash */
u = strtok(b, ":"); /* user */
strtok(NULL, ":"); /* skipped field */
i = strtok(NULL, ":"); /* we take only users >= 500 */
if ( i == NULL ) {
printlog(stdlog, PL_CRIT, "Parsing error on file %s !", fpasswd);
return -1;
}
if ( atoi(i) >= 500 && add_to_cache(&sysuser, u) < 0 ) return -1;
}
fclose(f);
return 0;
}
int load_aliases_cache(const char *faliases)
{
FILE *f;
char b[256], *u;
if ( (f = fopen(faliases, "rt")) == NULL ) {
printlog(stdlog, PL_ERR, "Error opening file %s for reading: %m", faliases);
return -1;
}
if ( reset_cache(&aliases) < 0 ) {
fclose(f);
return -1;
}
while ( fgets(b, sizeof(b), f) ) {
trim(b);
if ( ! b[0] || b[0] == '#' ) continue; /* Skip comments & empty lines */
/* useralias: username */
u = strtok(b, ":");
if ( u == NULL ) {
printlog(stdlog, PL_ERR, "Parsing error on file %s !", faliases);
return -1;
}
if ( add_to_cache(&aliases, trim(u)) < 0 ) return -1;
}
fclose(f);
return 0;
}
static int search_user(const char *user)
{
char b[256];
sprintf(b,"|%s|", user);
if ( strstr(sysuser.cache, b)
|| strstr(aliases.cache, b) )
return ST_SUCCESS;
return ST_EMAIL_NOT_FOUND;
}
bool check_email(const char *em)
{
if ( ! em )
return false;
if ( strcmp(em, "*") == 0 ) /* Special test case */
return true;
if ( strlen(em) >= 5 && ! strchr(em, ' ') && strchr(em, '@') && strchr(em, '.') )
return true;
return false;
}
int check_email_active(const char *em)
{
struct stat s;
char user[32];
int l;
if ( strcmp(em, "*") == 0 ) /* Special test case */
return ST_SUCCESS;
if ( stat(PASSWDF, &s) < 0 ) {
printlog(stdlog, PL_ERR, "Cannot access %s: %m", PASSWDF);
return -1;
}
if ( s.st_mtime > sysuser.last ) {
printlog(stdlog, PL_INFO, "System user database change detected. Reloading ...");
if ( load_sysuser_cache(PASSWDF) < 0 ) return -1;
sysuser.last = s.st_mtime;
}
if ( stat(ALIASESF, &s) < 0 ) {
printlog(stdlog, PL_ERR, "Cannot access %s: %m", ALIASESF);
return -1;
}
if ( s.st_mtime > aliases.last ) {
printlog(stdlog, PL_INFO, "User alias database change detected. Reloading ...");
if ( load_aliases_cache(ALIASESF) < 0 ) return -1;
aliases.last = s.st_mtime;
}
l = strchr(em, '@') - em;
strncpy(user, em, l);
user[l] = 0;
return search_user(strlwr(user));
}