-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
362 lines (323 loc) · 12.1 KB
/
main.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
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
/*
* signing-milter - main.c
* Copyright (C) 2010-2020 Andreas Schulze
*
* 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; only version 2 of the License is applicable.
*
* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Authors:
* Andreas Schulze <signing-milter at andreasschulze.de>
*
*/
#include "main.h"
/* Standardwerte setzen */
char* opt_clientgroup = NULL;
int opt_loglevel = LOG_NOTICE;
int opt_logdest = LOG_DEST_SYSLOG;
char* opt_group = "signing-milter";
char* opt_keepdir = NULL;
char* opt_signingtable = "/etc/signing-milter/signingtable.cdb";
char* opt_modetable = NULL;
char* opt_miltersocket = "inet6:30053@[::1]";
int opt_timeout = 600;
char* opt_user = "signing-milter";
int opt_addxheader = 0;
int opt_signerfromheader = 0;
/* globale Variablen */
struct DICT dict_signingtable = {
"signingtable", /* name */
DICT_FLAG_TRY0NULL, /* flags */
0, /* stat_fd */
0, /* mtime */
CDB_STATIC_INIT, /* cdb */
NULL, /* buffer */
NULL, /* result */
0 /* result_len */
};
struct DICT dict_modetable = {
"modetable", /* name */
DICT_FLAG_TRY0NULL, /* flags */
0, /* stat_fd */
0, /* mtime */
CDB_STATIC_INIT, /* cdb */
NULL, /* buffer */
NULL, /* result */
0 /* result_len */
};
int main(int argc, char** argv) {
int c;
char* p;
uid_t uid, gid, client_gid, root_gid;
struct passwd* pw;
struct group* gr;
struct stat st;
int localsocket = 1;
/*
* compilerwarnung: "client_gid may be used uninitialized"
* sowas will man ja nun wirklich nicht ...
*/
uid = gid = client_gid = root_gid = 0;
while ((c = getopt(argc, argv, "bc:d:hfg:k:lm:n:s:t:u:vx")) > 0) {
switch (c) {
case 'b': /* break contentheader */
logmsg(LOG_INFO, "option -b is ignored for compatibily reasons, you may remove it safely");
break;
case 'c': /* clientgroup */
opt_clientgroup = optarg;
if ((gr = getgrnam(opt_clientgroup)) == NULL) {
logmsg(LOG_ERR, "unknown clientgroup: getgrnam(%s) failed", opt_group);
exit(EX_DATAERR);
}
client_gid = gr->gr_gid;
break;
case 'd': /* Loglevel */
opt_loglevel = (int) strtoul(optarg, &p, 10);
if (p != NULL && *p != '\0') {
printf("debug-level is not valid integer: %s\n", optarg);
exit(EX_DATAERR);
}
p = NULL;
if (opt_loglevel < 0 || opt_loglevel > 7) {
printf("loglevel out of range 0..7: %i\n", opt_loglevel);
exit(EX_DATAERR);
}
break;
case 'f': /* signer from header, not from envelope */
opt_signerfromheader = 1;
break;
case 'g': /* group */
opt_group = optarg;
if ((gr = getgrnam(opt_group)) == NULL) {
printf("unknown group: getgrnam(%s) failed", opt_group);
exit(EX_DATAERR);
}
break;
case 'k': /* keepdir */
opt_keepdir = optarg;
if (stat(opt_keepdir, &st) < 0) {
printf("directory to keep data: %s: %s", opt_keepdir, strerror(errno));
exit(EX_DATAERR);
}
if (!S_ISDIR(st.st_mode)) {
printf("directory to keep data: %s is not a directory", opt_keepdir);
exit(EX_DATAERR);
}
/* Zugriffsrechte werden spaeter geprueft, wenn zur richtigen uid gewechselt wurde */
break;
case '?': /* help */
case 'h':
usage();
exit(EX_OK);
case 'l': /* switch log destination from SYSLOG (default) to STDOUT */
opt_logdest = LOG_DEST_STDOUT;
break;
case 'm': /* Signingtable cdbfilename */
opt_signingtable = optarg;
break;
case 'n': /* Modetable cdbfilename */
opt_modetable = optarg;
break;
case 's': /* Miltersocket */
opt_miltersocket = optarg;
break;
case 't': /* Timeout */
opt_timeout = (int) strtoul(optarg, &p, 10);
if (p != NULL && *p != '\0') {
printf("timeout is not valid integer: %s\n", optarg);
exit(EX_DATAERR);
}
p = NULL;
if (opt_timeout < 0 ) {
printf("negative milter connection timeout: %i\n", opt_timeout);
exit(EX_DATAERR);
}
break;
case 'u': /* user */
opt_user = optarg;
/* get passwd/group entries for opt_user and opt_group */
if ((pw = getpwnam(opt_user)) == NULL) {
logmsg(LOG_ERR, "unknown user: getpwnam(%s) failed", opt_user);
exit(EX_DATAERR);
}
break;
case 'v': /* Version */
version();
exit(EX_OK);
case 'x': /* add X-Header */
opt_addxheader = (int) !opt_addxheader;
break;
default:
usage();
exit(EX_USAGE);
}
}
/* open syslog */
if (LOG_DEST_SYSLOG == opt_logdest)
openlog(STR_PROGNAME, LOG_PID, LOG_MAIL);
/* say helo */
logmsg(LOG_NOTICE, "starting %s %s listening on %s, loglevel %i",
STR_PROGNAME, STR_PROGVERSION, opt_miltersocket, opt_loglevel);
/* force a new processgroup */
if ((setsid() == -1))
logmsg(LOG_DEBUG, "ignoring that setsid() failed");
if (opt_timeout > 0 && smfi_settimeout(opt_timeout) != MI_SUCCESS) {
logmsg(LOG_ERR, "could not set milter timeout");
exit(EX_SOFTWARE);
}
logmsg(LOG_INFO, "miltertimeout set to %i", opt_timeout);
if (smfi_setconn(opt_miltersocket) != MI_SUCCESS) {
logmsg(LOG_ERR, "could not set milter socket");
exit(EX_SOFTWARE);
}
if (smfi_register(callbacks) != MI_SUCCESS) {
logmsg(LOG_ERR, "could not register milter");
exit(EX_SOFTWARE);
}
/*
* User- und Gruppennamen stehen nun fest. Testen, ob es diese gibt
* und uid / gid ermitteln
*/
if ((pw = getpwnam(opt_user)) == NULL) {
logmsg(LOG_ERR, "unknown user: getpwnam(%s) failed", opt_user);
exit(EX_DATAERR);
}
uid = pw->pw_uid;
if ((gr = getgrnam(opt_group)) == NULL) {
logmsg(LOG_ERR, "unknown group: getgrnam(%s) failed", opt_group);
exit(EX_SOFTWARE);
}
gid = gr->gr_gid;
/* wenn nicht als Parameter angegeben, gehört ein Unix-Socket erstmal der gleichen Gruppe */
if (opt_clientgroup == NULL)
client_gid = gid;
/* wenn inet in optarg gefunden wird *und* das auch noch direkt am Anfang
* dann ist's kein lokaler socket */
if (((p = strstr(opt_miltersocket, "inet")) != NULL) && opt_miltersocket == p)
localsocket = 0;
if (localsocket == 1) {
/* den Socket oeffnen */
if (smfi_opensocket(REMOVE_EXISTING_SOCKETS) != MI_SUCCESS) {
logmsg(LOG_ERR, "could not open milter socket %s", opt_miltersocket);
exit(EX_SOFTWARE);
}
/* testen, ob's den Socket nun gibt */
p = opt_miltersocket + strlen("local:");
if (stat(p, &st) < 0) {
p = opt_miltersocket + strlen("unix:");
if (stat(p, &st) < 0) {
logmsg(LOG_ERR, "miltersocket does not exist: %m", strerror(errno));
exit(EX_DATAERR);
}
}
/* gid der Gruppe root */
if ((gr = getgrnam("root")) == NULL) {
logmsg(LOG_ERR, "unknown rootgroup: getgrnam(root) failed");
exit(EX_SOFTWARE);
}
root_gid = gr->gr_gid;
/* clientgroup muss != root und != opt_group sein */
if ((client_gid == gid) || (client_gid == root_gid)) {
logmsg(LOG_ERR, "clientgroup %s must be neither %s nor %s", opt_clientgroup, "root", opt_group);
exit(EX_DATAERR);
}
/* nun die Rechte setzen */
if (chown(p, uid, client_gid) != 0) {
logmsg(LOG_ERR, "chown(%s, %i, %i) failed: %m", p, uid, client_gid, strerror(errno));
exit(EX_SOFTWARE);
}
if (chmod(p, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP) != 0) {
logmsg(LOG_ERR, "chmod(%s, 0660) failed: %m", p, strerror(errno));
exit(EX_SOFTWARE);
}
logmsg(LOG_INFO, "changed socket %s to owner/group: %i/%i, mode: 0660", opt_miltersocket, uid, client_gid);
}
/* gid/uid setzen */
if (setgid(gid) != 0) {
logmsg(LOG_ERR, "setgid(%i) failed: %s", gr->gr_gid, strerror(errno));
exit(EX_SOFTWARE);
}
if (setuid(uid) != 0) {
logmsg(LOG_ERR, "setuid(%i) failed: %s", pw->pw_uid, strerror(errno));
exit(EX_SOFTWARE);
}
/* aktuelle uid/gid pruefen und loggen */
uid = getuid();
gid = getgid();
if (uid == 0 || gid == 0) {
logmsg(LOG_ERR, "too much privileges, %s will not start under root", STR_PROGNAME);
exit(EX_DATAERR);
}
logmsg(LOG_INFO, "running as uid: %i, gid: %i", (int) uid, (int) gid);
if (opt_keepdir != NULL) {
if (S_IRWXO & st.st_mode) {
logmsg(LOG_ERR, "directory to keep data: %s: permissions too open: remove any access for other", opt_keepdir);
exit(EX_DATAERR);
}
if (access(opt_keepdir, R_OK) < 0 && errno == EACCES) {
logmsg(LOG_ERR, "directory to keep data: %s: permissions too strong: no read access", opt_keepdir);
exit(EX_DATAERR);
}
if (access(opt_keepdir, W_OK) < 0 && errno == EACCES) {
logmsg(LOG_ERR, "directory to keep data: %s: permissions too strong: no write access", opt_keepdir);
exit(EX_DATAERR);
}
if (access(opt_keepdir, X_OK) < 0 && errno == EACCES) {
logmsg(LOG_ERR, "directory to keep data: %s: permissions too strong: no execute access", opt_keepdir);
exit(EX_DATAERR);
}
logmsg(LOG_INFO, "directory to keep data: %s", opt_keepdir);
}
dict_open(opt_signingtable, &dict_signingtable);
if (opt_modetable)
dict_open(opt_modetable, &dict_modetable);
/* initialize OpenSSL */
SSL_library_init();
OpenSSL_add_all_algorithms();
/* get meaningful error messages */
SSL_load_error_strings();
ERR_load_crypto_strings();
/* Statistik initialisieren */
init_stats();
/* Signal-Handler fuer SIGALRM */
signal(SIGALRM, sig_handler);
/* Run milter */
if ((c = smfi_main()) != MI_SUCCESS)
logmsg(LOG_ERR, "Milter startup failed");
else
logmsg(LOG_NOTICE, "stopping %s %s listening on %s", STR_PROGNAME, STR_PROGVERSION, opt_miltersocket);
dict_close(&dict_signingtable);
if (opt_modetable)
dict_close(&dict_modetable);
/* cleanup OpenSSL */
ERR_free_strings();
EVP_cleanup();
output_stats();
#ifdef DMALLOC
dmalloc_log_stats();
dmalloc_log_unfreed();
dmalloc_shutdown();
#endif
exit(c);
}
/* under Linux a sighandler must reenable itself */
void sig_handler(int sig) {
logmsg(LOG_INFO, "%s: signal %i received", STR_PROGNAME, sig);
signal(sig, sig_handler);
output_stats();
reset_stats();
#ifdef DMALLOC
logmsg(LOG_DEBUG, "%s: dumping dmalloc statistics", STR_PROGNAME);
dmalloc_log_stats();
dmalloc_log_unfreed();
#endif
}