annotate src/sm-archive.cpp @ 21:09564d4acd9e stable-1-0-8

patches from Marco d'Itri for postfix
author Carl Byington <carl@five-ten-sg.com>
date Fri, 24 Dec 2010 15:13:18 -0800
parents b24369330483
children 9298f8b00db2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
616666e2f34c initial version
carl
parents:
diff changeset
1 /*
616666e2f34c initial version
carl
parents:
diff changeset
2
13
75e1a9bcbc2e gpl3, add removal option for original recipients
carl
parents: 10
diff changeset
3 Copyright (c) 2007 Carl Byington - 510 Software Group, released under
75e1a9bcbc2e gpl3, add removal option for original recipients
carl
parents: 10
diff changeset
4 the GPL version 3 or any later version at your choice available at
75e1a9bcbc2e gpl3, add removal option for original recipients
carl
parents: 10
diff changeset
5 http://www.gnu.org/licenses/gpl-3.0.txt
0
616666e2f34c initial version
carl
parents:
diff changeset
6
616666e2f34c initial version
carl
parents:
diff changeset
7 Based on a sample milter Copyright (c) 2000-2003 Sendmail, Inc. and its
13
75e1a9bcbc2e gpl3, add removal option for original recipients
carl
parents: 10
diff changeset
8 suppliers.
0
616666e2f34c initial version
carl
parents:
diff changeset
9
616666e2f34c initial version
carl
parents:
diff changeset
10 -p port The port through which the MTA will connect to this milter.
616666e2f34c initial version
carl
parents:
diff changeset
11 -t sec The timeout value.
616666e2f34c initial version
carl
parents:
diff changeset
12 -c Check the config, and print a copy to stdout. Don't start the
616666e2f34c initial version
carl
parents:
diff changeset
13 milter or do anything with the socket.
616666e2f34c initial version
carl
parents:
diff changeset
14 -d increase debug level
616666e2f34c initial version
carl
parents:
diff changeset
15
616666e2f34c initial version
carl
parents:
diff changeset
16 */
616666e2f34c initial version
carl
parents:
diff changeset
17
616666e2f34c initial version
carl
parents:
diff changeset
18
616666e2f34c initial version
carl
parents:
diff changeset
19 // from sendmail sample
616666e2f34c initial version
carl
parents:
diff changeset
20 #include <sys/types.h>
616666e2f34c initial version
carl
parents:
diff changeset
21 #include <sys/stat.h>
616666e2f34c initial version
carl
parents:
diff changeset
22 #include <errno.h>
616666e2f34c initial version
carl
parents:
diff changeset
23 #include <sysexits.h>
616666e2f34c initial version
carl
parents:
diff changeset
24 #include <unistd.h>
616666e2f34c initial version
carl
parents:
diff changeset
25
616666e2f34c initial version
carl
parents:
diff changeset
26 // needed for thread
616666e2f34c initial version
carl
parents:
diff changeset
27 #include <pthread.h>
616666e2f34c initial version
carl
parents:
diff changeset
28
616666e2f34c initial version
carl
parents:
diff changeset
29 // needed for std c++ collections
616666e2f34c initial version
carl
parents:
diff changeset
30 #include <set>
616666e2f34c initial version
carl
parents:
diff changeset
31 #include <map>
616666e2f34c initial version
carl
parents:
diff changeset
32 #include <list>
616666e2f34c initial version
carl
parents:
diff changeset
33
616666e2f34c initial version
carl
parents:
diff changeset
34 // misc stuff needed here
616666e2f34c initial version
carl
parents:
diff changeset
35 #include <ctype.h>
616666e2f34c initial version
carl
parents:
diff changeset
36 #include <syslog.h>
616666e2f34c initial version
carl
parents:
diff changeset
37 #include <pwd.h>
616666e2f34c initial version
carl
parents:
diff changeset
38 #include <sys/wait.h> /* header for waitpid() and various macros */
616666e2f34c initial version
carl
parents:
diff changeset
39 #include <signal.h> /* header for signal functions */
616666e2f34c initial version
carl
parents:
diff changeset
40
616666e2f34c initial version
carl
parents:
diff changeset
41 #include "includes.h"
616666e2f34c initial version
carl
parents:
diff changeset
42
9
48880e61ce5e better autoconf, changes for solaris portability
carl
parents: 8
diff changeset
43 #ifndef HAVE_DAEMON
48880e61ce5e better autoconf, changes for solaris portability
carl
parents: 8
diff changeset
44 #include "daemon.h"
48880e61ce5e better autoconf, changes for solaris portability
carl
parents: 8
diff changeset
45 #include "daemon.c"
48880e61ce5e better autoconf, changes for solaris portability
carl
parents: 8
diff changeset
46 #endif
48880e61ce5e better autoconf, changes for solaris portability
carl
parents: 8
diff changeset
47
0
616666e2f34c initial version
carl
parents:
diff changeset
48 extern "C" {
9
48880e61ce5e better autoconf, changes for solaris portability
carl
parents: 8
diff changeset
49 #include <libmilter/mfapi.h>
0
616666e2f34c initial version
carl
parents:
diff changeset
50 sfsistat mlfi_connect(SMFICTX *ctx, char *hostname, _SOCK_ADDR *hostaddr);
616666e2f34c initial version
carl
parents:
diff changeset
51 sfsistat mlfi_envfrom(SMFICTX *ctx, char **argv);
616666e2f34c initial version
carl
parents:
diff changeset
52 sfsistat mlfi_envrcpt(SMFICTX *ctx, char **argv);
616666e2f34c initial version
carl
parents:
diff changeset
53 sfsistat mlfi_eom(SMFICTX *ctx);
616666e2f34c initial version
carl
parents:
diff changeset
54 sfsistat mlfi_abort(SMFICTX *ctx);
616666e2f34c initial version
carl
parents:
diff changeset
55 sfsistat mlfi_close(SMFICTX *ctx);
616666e2f34c initial version
carl
parents:
diff changeset
56 void sig_chld(int signo);
616666e2f34c initial version
carl
parents:
diff changeset
57 }
616666e2f34c initial version
carl
parents:
diff changeset
58
616666e2f34c initial version
carl
parents:
diff changeset
59 int debug_syslog = 0;
616666e2f34c initial version
carl
parents:
diff changeset
60 bool syslog_opened = false;
616666e2f34c initial version
carl
parents:
diff changeset
61 bool use_syslog = true; // false to printf
616666e2f34c initial version
carl
parents:
diff changeset
62 bool loader_run = true; // used to stop the config loader thread
616666e2f34c initial version
carl
parents:
diff changeset
63 CONFIG *config = NULL; // protected by the config_mutex
616666e2f34c initial version
carl
parents:
diff changeset
64 int generation = 0; // protected by the config_mutex
616666e2f34c initial version
carl
parents:
diff changeset
65 const int maxlen = 1000; // used for snprintf buffers
616666e2f34c initial version
carl
parents:
diff changeset
66
616666e2f34c initial version
carl
parents:
diff changeset
67 pthread_mutex_t config_mutex;
616666e2f34c initial version
carl
parents:
diff changeset
68 pthread_mutex_t syslog_mutex;
616666e2f34c initial version
carl
parents:
diff changeset
69
616666e2f34c initial version
carl
parents:
diff changeset
70
616666e2f34c initial version
carl
parents:
diff changeset
71 mlfiPriv::mlfiPriv() {
616666e2f34c initial version
carl
parents:
diff changeset
72 pthread_mutex_lock(&config_mutex);
616666e2f34c initial version
carl
parents:
diff changeset
73 pc = config;
616666e2f34c initial version
carl
parents:
diff changeset
74 pc->reference_count++;
616666e2f34c initial version
carl
parents:
diff changeset
75 pthread_mutex_unlock(&config_mutex);
8
ed73e6361570 initial version
carl
parents: 7
diff changeset
76 mailaddr = NULL;
ed73e6361570 initial version
carl
parents: 7
diff changeset
77 queueid = NULL;
0
616666e2f34c initial version
carl
parents:
diff changeset
78 }
616666e2f34c initial version
carl
parents:
diff changeset
79
616666e2f34c initial version
carl
parents:
diff changeset
80 mlfiPriv::~mlfiPriv() {
616666e2f34c initial version
carl
parents:
diff changeset
81 pthread_mutex_lock(&config_mutex);
616666e2f34c initial version
carl
parents:
diff changeset
82 pc->reference_count--;
616666e2f34c initial version
carl
parents:
diff changeset
83 pthread_mutex_unlock(&config_mutex);
616666e2f34c initial version
carl
parents:
diff changeset
84 reset(true);
616666e2f34c initial version
carl
parents:
diff changeset
85 }
616666e2f34c initial version
carl
parents:
diff changeset
86
616666e2f34c initial version
carl
parents:
diff changeset
87 void mlfiPriv::reset(bool final) {
8
ed73e6361570 initial version
carl
parents: 7
diff changeset
88 targets.clear();
13
75e1a9bcbc2e gpl3, add removal option for original recipients
carl
parents: 10
diff changeset
89 for (string_set::iterator i=removal.begin(); i!=removal.end(); i++) {
19
b24369330483 Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 17
diff changeset
90 const char *remove = (*i);
b24369330483 Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 17
diff changeset
91 free((void*)remove);
13
75e1a9bcbc2e gpl3, add removal option for original recipients
carl
parents: 10
diff changeset
92 }
75e1a9bcbc2e gpl3, add removal option for original recipients
carl
parents: 10
diff changeset
93 removal.clear();
19
b24369330483 Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 17
diff changeset
94 if (mailaddr) free((void*)mailaddr);
b24369330483 Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 17
diff changeset
95 if (queueid) free((void*)queueid);
0
616666e2f34c initial version
carl
parents:
diff changeset
96 if (!final) {
616666e2f34c initial version
carl
parents:
diff changeset
97 mailaddr = NULL;
616666e2f34c initial version
carl
parents:
diff changeset
98 queueid = NULL;
616666e2f34c initial version
carl
parents:
diff changeset
99 }
616666e2f34c initial version
carl
parents:
diff changeset
100 }
616666e2f34c initial version
carl
parents:
diff changeset
101
616666e2f34c initial version
carl
parents:
diff changeset
102 #define MLFIPRIV ((struct mlfiPriv *) smfi_getpriv(ctx))
616666e2f34c initial version
carl
parents:
diff changeset
103
616666e2f34c initial version
carl
parents:
diff changeset
104
616666e2f34c initial version
carl
parents:
diff changeset
105 ////////////////////////////////////////////////
616666e2f34c initial version
carl
parents:
diff changeset
106 // syslog a message
616666e2f34c initial version
carl
parents:
diff changeset
107 //
19
b24369330483 Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 17
diff changeset
108 void my_syslog(mlfiPriv *priv, const char *text) {
0
616666e2f34c initial version
carl
parents:
diff changeset
109 char buf[maxlen];
616666e2f34c initial version
carl
parents:
diff changeset
110 if (priv) {
21
09564d4acd9e patches from Marco d'Itri for postfix
Carl Byington <carl@five-ten-sg.com>
parents: 19
diff changeset
111 snprintf(buf, sizeof(buf), "%s: %s",
09564d4acd9e patches from Marco d'Itri for postfix
Carl Byington <carl@five-ten-sg.com>
parents: 19
diff changeset
112 priv->queueid ? priv->queueid : "NOQUEUE", text);
0
616666e2f34c initial version
carl
parents:
diff changeset
113 text = buf;
616666e2f34c initial version
carl
parents:
diff changeset
114 }
616666e2f34c initial version
carl
parents:
diff changeset
115 if (use_syslog) {
616666e2f34c initial version
carl
parents:
diff changeset
116 pthread_mutex_lock(&syslog_mutex);
616666e2f34c initial version
carl
parents:
diff changeset
117 if (!syslog_opened) {
2
32b57406b656 initial version
carl
parents: 0
diff changeset
118 openlog("sm-archive", LOG_PID, LOG_MAIL);
0
616666e2f34c initial version
carl
parents:
diff changeset
119 syslog_opened = true;
616666e2f34c initial version
carl
parents:
diff changeset
120 }
616666e2f34c initial version
carl
parents:
diff changeset
121 syslog(LOG_NOTICE, "%s", text);
616666e2f34c initial version
carl
parents:
diff changeset
122 pthread_mutex_unlock(&syslog_mutex);
616666e2f34c initial version
carl
parents:
diff changeset
123 }
616666e2f34c initial version
carl
parents:
diff changeset
124 else {
616666e2f34c initial version
carl
parents:
diff changeset
125 printf("%s \n", text);
616666e2f34c initial version
carl
parents:
diff changeset
126 }
616666e2f34c initial version
carl
parents:
diff changeset
127 }
616666e2f34c initial version
carl
parents:
diff changeset
128
19
b24369330483 Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 17
diff changeset
129 void my_syslog(const char *text) {
0
616666e2f34c initial version
carl
parents:
diff changeset
130 my_syslog(NULL, text);
616666e2f34c initial version
carl
parents:
diff changeset
131 }
616666e2f34c initial version
carl
parents:
diff changeset
132
616666e2f34c initial version
carl
parents:
diff changeset
133
616666e2f34c initial version
carl
parents:
diff changeset
134 ////////////////////////////////////////////////
616666e2f34c initial version
carl
parents:
diff changeset
135 // this email address is passed in from sendmail, and will
616666e2f34c initial version
carl
parents:
diff changeset
136 // always be enclosed in <>. It may have mixed case, just
616666e2f34c initial version
carl
parents:
diff changeset
137 // as the mail client sent it. We dup the string and convert
616666e2f34c initial version
carl
parents:
diff changeset
138 // the duplicate to lower case.
21
09564d4acd9e patches from Marco d'Itri for postfix
Carl Byington <carl@five-ten-sg.com>
parents: 19
diff changeset
139 // Postfix will return addresses without <> if they have been provided
09564d4acd9e patches from Marco d'Itri for postfix
Carl Byington <carl@five-ten-sg.com>
parents: 19
diff changeset
140 // this way in the SMTP dialog.
0
616666e2f34c initial version
carl
parents:
diff changeset
141 //
19
b24369330483 Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 17
diff changeset
142 const char *to_lower_string(const char *email);
b24369330483 Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 17
diff changeset
143 const char *to_lower_string(const char *email) {
21
09564d4acd9e patches from Marco d'Itri for postfix
Carl Byington <carl@five-ten-sg.com>
parents: 19
diff changeset
144 char *key, *p;
09564d4acd9e patches from Marco d'Itri for postfix
Carl Byington <carl@five-ten-sg.com>
parents: 19
diff changeset
145 int i;
09564d4acd9e patches from Marco d'Itri for postfix
Carl Byington <carl@five-ten-sg.com>
parents: 19
diff changeset
146
09564d4acd9e patches from Marco d'Itri for postfix
Carl Byington <carl@five-ten-sg.com>
parents: 19
diff changeset
147 if (strcmp(email, "<>") == 0) return strdup(email);
09564d4acd9e patches from Marco d'Itri for postfix
Carl Byington <carl@five-ten-sg.com>
parents: 19
diff changeset
148 if (email[0] == '<') p = (char *) email + 1;
09564d4acd9e patches from Marco d'Itri for postfix
Carl Byington <carl@five-ten-sg.com>
parents: 19
diff changeset
149 else p = (char *) email;
09564d4acd9e patches from Marco d'Itri for postfix
Carl Byington <carl@five-ten-sg.com>
parents: 19
diff changeset
150 key = (char *) malloc(strlen(p) + 1);
09564d4acd9e patches from Marco d'Itri for postfix
Carl Byington <carl@five-ten-sg.com>
parents: 19
diff changeset
151 for (i = 0; p[i] != '\0'; i++) key[i] = tolower(p[i]);
09564d4acd9e patches from Marco d'Itri for postfix
Carl Byington <carl@five-ten-sg.com>
parents: 19
diff changeset
152 if (p[i - 1] == '>') i--;
09564d4acd9e patches from Marco d'Itri for postfix
Carl Byington <carl@five-ten-sg.com>
parents: 19
diff changeset
153 key[i] = '\0';
0
616666e2f34c initial version
carl
parents:
diff changeset
154 return key;
616666e2f34c initial version
carl
parents:
diff changeset
155 }
616666e2f34c initial version
carl
parents:
diff changeset
156
616666e2f34c initial version
carl
parents:
diff changeset
157
616666e2f34c initial version
carl
parents:
diff changeset
158 ////////////////////////////////////////////////
616666e2f34c initial version
carl
parents:
diff changeset
159 // start of sendmail milter interfaces
616666e2f34c initial version
carl
parents:
diff changeset
160 //
616666e2f34c initial version
carl
parents:
diff changeset
161 sfsistat mlfi_connect(SMFICTX *ctx, char *hostname, _SOCK_ADDR *hostaddr)
616666e2f34c initial version
carl
parents:
diff changeset
162 {
616666e2f34c initial version
carl
parents:
diff changeset
163 // allocate some private memory
616666e2f34c initial version
carl
parents:
diff changeset
164 mlfiPriv *priv = new mlfiPriv;
616666e2f34c initial version
carl
parents:
diff changeset
165 // save the private data
616666e2f34c initial version
carl
parents:
diff changeset
166 smfi_setpriv(ctx, (void*)priv);
616666e2f34c initial version
carl
parents:
diff changeset
167 // continue processing
616666e2f34c initial version
carl
parents:
diff changeset
168 return SMFIS_CONTINUE;
616666e2f34c initial version
carl
parents:
diff changeset
169 }
616666e2f34c initial version
carl
parents:
diff changeset
170
616666e2f34c initial version
carl
parents:
diff changeset
171 sfsistat mlfi_envfrom(SMFICTX *ctx, char **from)
616666e2f34c initial version
carl
parents:
diff changeset
172 {
616666e2f34c initial version
carl
parents:
diff changeset
173 mlfiPriv &priv = *MLFIPRIV;
616666e2f34c initial version
carl
parents:
diff changeset
174 priv.mailaddr = to_lower_string(from[0]);
616666e2f34c initial version
carl
parents:
diff changeset
175 return SMFIS_CONTINUE;
616666e2f34c initial version
carl
parents:
diff changeset
176 }
616666e2f34c initial version
carl
parents:
diff changeset
177
19
b24369330483 Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 17
diff changeset
178 void add_target(mlfiPriv &priv, const char *target);
b24369330483 Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 17
diff changeset
179 void add_target(mlfiPriv &priv, const char *target)
7
5241a079fe03 initial version
carl
parents: 3
diff changeset
180 {
5241a079fe03 initial version
carl
parents: 3
diff changeset
181 if (target) {
8
ed73e6361570 initial version
carl
parents: 7
diff changeset
182 string_set::iterator i = priv.targets.find(target);
ed73e6361570 initial version
carl
parents: 7
diff changeset
183 if (i == priv.targets.end()) priv.targets.insert(target);
7
5241a079fe03 initial version
carl
parents: 3
diff changeset
184 }
5241a079fe03 initial version
carl
parents: 3
diff changeset
185 }
5241a079fe03 initial version
carl
parents: 3
diff changeset
186
19
b24369330483 Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 17
diff changeset
187 void add_remove(mlfiPriv &priv, const char *remove);
b24369330483 Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 17
diff changeset
188 void add_remove(mlfiPriv &priv, const char *remove)
13
75e1a9bcbc2e gpl3, add removal option for original recipients
carl
parents: 10
diff changeset
189 {
75e1a9bcbc2e gpl3, add removal option for original recipients
carl
parents: 10
diff changeset
190 if (remove) {
75e1a9bcbc2e gpl3, add removal option for original recipients
carl
parents: 10
diff changeset
191 string_set::iterator i = priv.removal.find(remove);
75e1a9bcbc2e gpl3, add removal option for original recipients
carl
parents: 10
diff changeset
192 if (i == priv.removal.end()) priv.removal.insert(remove);
75e1a9bcbc2e gpl3, add removal option for original recipients
carl
parents: 10
diff changeset
193 }
75e1a9bcbc2e gpl3, add removal option for original recipients
carl
parents: 10
diff changeset
194 }
75e1a9bcbc2e gpl3, add removal option for original recipients
carl
parents: 10
diff changeset
195
0
616666e2f34c initial version
carl
parents:
diff changeset
196 sfsistat mlfi_envrcpt(SMFICTX *ctx, char **rcpt)
616666e2f34c initial version
carl
parents:
diff changeset
197 {
616666e2f34c initial version
carl
parents:
diff changeset
198 mlfiPriv &priv = *MLFIPRIV;
616666e2f34c initial version
carl
parents:
diff changeset
199 CONFIG &dc = *priv.pc;
21
09564d4acd9e patches from Marco d'Itri for postfix
Carl Byington <carl@five-ten-sg.com>
parents: 19
diff changeset
200 const char *i_macro = smfi_getsymval(ctx, (char*)"i");
09564d4acd9e patches from Marco d'Itri for postfix
Carl Byington <carl@five-ten-sg.com>
parents: 19
diff changeset
201 if (!priv.queueid && i_macro) priv.queueid = strdup(i_macro);
19
b24369330483 Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 17
diff changeset
202 const char *rcptaddr = to_lower_string(rcpt[0]);
0
616666e2f34c initial version
carl
parents:
diff changeset
203 if (debug_syslog > 1) {
616666e2f34c initial version
carl
parents:
diff changeset
204 char msg[maxlen];
616666e2f34c initial version
carl
parents:
diff changeset
205 snprintf(msg, sizeof(msg), "from <%s> to <%s>", priv.mailaddr, rcptaddr);
616666e2f34c initial version
carl
parents:
diff changeset
206 my_syslog(&priv, msg);
616666e2f34c initial version
carl
parents:
diff changeset
207 }
19
b24369330483 Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 17
diff changeset
208 const char *target = dc.find_to(rcptaddr);
13
75e1a9bcbc2e gpl3, add removal option for original recipients
carl
parents: 10
diff changeset
209 add_target(priv, target);
75e1a9bcbc2e gpl3, add removal option for original recipients
carl
parents: 10
diff changeset
210 bool remove = dc.find_remove(rcptaddr);
75e1a9bcbc2e gpl3, add removal option for original recipients
carl
parents: 10
diff changeset
211 if (remove) add_remove(priv, strdup(rcptaddr));
19
b24369330483 Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 17
diff changeset
212 free((void*)rcptaddr);
0
616666e2f34c initial version
carl
parents:
diff changeset
213 return SMFIS_CONTINUE;
616666e2f34c initial version
carl
parents:
diff changeset
214 }
616666e2f34c initial version
carl
parents:
diff changeset
215
616666e2f34c initial version
carl
parents:
diff changeset
216 sfsistat mlfi_eom(SMFICTX *ctx)
616666e2f34c initial version
carl
parents:
diff changeset
217 {
8
ed73e6361570 initial version
carl
parents: 7
diff changeset
218 mlfiPriv &priv = *MLFIPRIV;
ed73e6361570 initial version
carl
parents: 7
diff changeset
219 CONFIG &dc = *priv.pc;
21
09564d4acd9e patches from Marco d'Itri for postfix
Carl Byington <carl@five-ten-sg.com>
parents: 19
diff changeset
220 const char *i_macro = smfi_getsymval(ctx, (char*)"i");
09564d4acd9e patches from Marco d'Itri for postfix
Carl Byington <carl@five-ten-sg.com>
parents: 19
diff changeset
221 if (!priv.queueid && i_macro) priv.queueid = strdup(i_macro);
19
b24369330483 Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 17
diff changeset
222 const char *target = dc.find_from(priv.mailaddr);
8
ed73e6361570 initial version
carl
parents: 7
diff changeset
223 add_target(priv, target);
ed73e6361570 initial version
carl
parents: 7
diff changeset
224 for (string_set::iterator i=priv.targets.begin(); i!=priv.targets.end(); i++) {
ed73e6361570 initial version
carl
parents: 7
diff changeset
225 target = (*i);
19
b24369330483 Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 17
diff changeset
226 smfi_addrcpt(ctx, (char*)target);
8
ed73e6361570 initial version
carl
parents: 7
diff changeset
227 if (debug_syslog > 1) {
ed73e6361570 initial version
carl
parents: 7
diff changeset
228 char msg[maxlen];
ed73e6361570 initial version
carl
parents: 7
diff changeset
229 snprintf(msg, sizeof(msg), "adding recipient <%s>", target);
ed73e6361570 initial version
carl
parents: 7
diff changeset
230 my_syslog(&priv, msg);
ed73e6361570 initial version
carl
parents: 7
diff changeset
231 }
ed73e6361570 initial version
carl
parents: 7
diff changeset
232 }
13
75e1a9bcbc2e gpl3, add removal option for original recipients
carl
parents: 10
diff changeset
233 for (string_set::iterator i=priv.removal.begin(); i!=priv.removal.end(); i++) {
19
b24369330483 Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 17
diff changeset
234 const char *remove = (*i);
b24369330483 Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 17
diff changeset
235 smfi_delrcpt(ctx, (char*)remove);
13
75e1a9bcbc2e gpl3, add removal option for original recipients
carl
parents: 10
diff changeset
236 if (debug_syslog > 1) {
75e1a9bcbc2e gpl3, add removal option for original recipients
carl
parents: 10
diff changeset
237 char msg[maxlen];
75e1a9bcbc2e gpl3, add removal option for original recipients
carl
parents: 10
diff changeset
238 snprintf(msg, sizeof(msg), "removing recipient <%s>", remove);
75e1a9bcbc2e gpl3, add removal option for original recipients
carl
parents: 10
diff changeset
239 my_syslog(&priv, msg);
75e1a9bcbc2e gpl3, add removal option for original recipients
carl
parents: 10
diff changeset
240 }
75e1a9bcbc2e gpl3, add removal option for original recipients
carl
parents: 10
diff changeset
241 }
0
616666e2f34c initial version
carl
parents:
diff changeset
242 // reset for a new message on the same connection
616666e2f34c initial version
carl
parents:
diff changeset
243 mlfi_abort(ctx);
616666e2f34c initial version
carl
parents:
diff changeset
244 return SMFIS_CONTINUE;
616666e2f34c initial version
carl
parents:
diff changeset
245 }
616666e2f34c initial version
carl
parents:
diff changeset
246
616666e2f34c initial version
carl
parents:
diff changeset
247 sfsistat mlfi_abort(SMFICTX *ctx)
616666e2f34c initial version
carl
parents:
diff changeset
248 {
616666e2f34c initial version
carl
parents:
diff changeset
249 mlfiPriv &priv = *MLFIPRIV;
616666e2f34c initial version
carl
parents:
diff changeset
250 priv.reset();
616666e2f34c initial version
carl
parents:
diff changeset
251 return SMFIS_CONTINUE;
616666e2f34c initial version
carl
parents:
diff changeset
252 }
616666e2f34c initial version
carl
parents:
diff changeset
253
616666e2f34c initial version
carl
parents:
diff changeset
254 sfsistat mlfi_close(SMFICTX *ctx)
616666e2f34c initial version
carl
parents:
diff changeset
255 {
616666e2f34c initial version
carl
parents:
diff changeset
256 mlfiPriv *priv = MLFIPRIV;
616666e2f34c initial version
carl
parents:
diff changeset
257 if (!priv) return SMFIS_CONTINUE;
616666e2f34c initial version
carl
parents:
diff changeset
258 delete priv;
616666e2f34c initial version
carl
parents:
diff changeset
259 smfi_setpriv(ctx, NULL);
616666e2f34c initial version
carl
parents:
diff changeset
260 return SMFIS_CONTINUE;
616666e2f34c initial version
carl
parents:
diff changeset
261 }
616666e2f34c initial version
carl
parents:
diff changeset
262
616666e2f34c initial version
carl
parents:
diff changeset
263 struct smfiDesc smfilter =
616666e2f34c initial version
carl
parents:
diff changeset
264 {
19
b24369330483 Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 17
diff changeset
265 (char*)"SM-ARCHIVE",// filter name
0
616666e2f34c initial version
carl
parents:
diff changeset
266 SMFI_VERSION, // version code -- do not change
13
75e1a9bcbc2e gpl3, add removal option for original recipients
carl
parents: 10
diff changeset
267 SMFIF_ADDRCPT | \
75e1a9bcbc2e gpl3, add removal option for original recipients
carl
parents: 10
diff changeset
268 SMFIF_DELRCPT, // flags
0
616666e2f34c initial version
carl
parents:
diff changeset
269 mlfi_connect, // connection info filter
616666e2f34c initial version
carl
parents:
diff changeset
270 NULL, // SMTP HELO command filter
616666e2f34c initial version
carl
parents:
diff changeset
271 mlfi_envfrom, // envelope sender filter
616666e2f34c initial version
carl
parents:
diff changeset
272 mlfi_envrcpt, // envelope recipient filter
616666e2f34c initial version
carl
parents:
diff changeset
273 NULL, // header filter
616666e2f34c initial version
carl
parents:
diff changeset
274 NULL, // end of header
616666e2f34c initial version
carl
parents:
diff changeset
275 NULL, // body block filter
616666e2f34c initial version
carl
parents:
diff changeset
276 mlfi_eom, // end of message
616666e2f34c initial version
carl
parents:
diff changeset
277 mlfi_abort, // message aborted
616666e2f34c initial version
carl
parents:
diff changeset
278 mlfi_close, // connection cleanup
616666e2f34c initial version
carl
parents:
diff changeset
279 };
616666e2f34c initial version
carl
parents:
diff changeset
280
616666e2f34c initial version
carl
parents:
diff changeset
281
616666e2f34c initial version
carl
parents:
diff changeset
282 ////////////////////////////////////////////////
616666e2f34c initial version
carl
parents:
diff changeset
283 // reload the config
616666e2f34c initial version
carl
parents:
diff changeset
284 //
616666e2f34c initial version
carl
parents:
diff changeset
285 CONFIG* new_conf();
616666e2f34c initial version
carl
parents:
diff changeset
286 CONFIG* new_conf() {
616666e2f34c initial version
carl
parents:
diff changeset
287 CONFIG *newc = new CONFIG;
616666e2f34c initial version
carl
parents:
diff changeset
288 pthread_mutex_lock(&config_mutex);
616666e2f34c initial version
carl
parents:
diff changeset
289 newc->generation = generation++;
616666e2f34c initial version
carl
parents:
diff changeset
290 pthread_mutex_unlock(&config_mutex);
616666e2f34c initial version
carl
parents:
diff changeset
291 if (debug_syslog) {
616666e2f34c initial version
carl
parents:
diff changeset
292 char buf[maxlen];
616666e2f34c initial version
carl
parents:
diff changeset
293 snprintf(buf, sizeof(buf), "loading configuration generation %d", newc->generation);
616666e2f34c initial version
carl
parents:
diff changeset
294 my_syslog(buf);
616666e2f34c initial version
carl
parents:
diff changeset
295 }
2
32b57406b656 initial version
carl
parents: 0
diff changeset
296 if (load_conf(*newc, "sm-archive.conf")) {
0
616666e2f34c initial version
carl
parents:
diff changeset
297 newc->load_time = time(NULL);
616666e2f34c initial version
carl
parents:
diff changeset
298 return newc;
616666e2f34c initial version
carl
parents:
diff changeset
299 }
616666e2f34c initial version
carl
parents:
diff changeset
300 delete newc;
616666e2f34c initial version
carl
parents:
diff changeset
301 return NULL;
616666e2f34c initial version
carl
parents:
diff changeset
302 }
616666e2f34c initial version
carl
parents:
diff changeset
303
616666e2f34c initial version
carl
parents:
diff changeset
304
616666e2f34c initial version
carl
parents:
diff changeset
305 ////////////////////////////////////////////////
616666e2f34c initial version
carl
parents:
diff changeset
306 // thread to watch the old config files for changes
616666e2f34c initial version
carl
parents:
diff changeset
307 // and reload when needed. we also cleanup old
616666e2f34c initial version
carl
parents:
diff changeset
308 // configs whose reference count has gone to zero.
616666e2f34c initial version
carl
parents:
diff changeset
309 //
10
f9e8bbf33a2a more fixes for solaris
carl
parents: 9
diff changeset
310 extern "C" {void* config_loader(void *arg);}
0
616666e2f34c initial version
carl
parents:
diff changeset
311 void* config_loader(void *arg) {
616666e2f34c initial version
carl
parents:
diff changeset
312 while (loader_run) {
616666e2f34c initial version
carl
parents:
diff changeset
313 sleep(180); // look for modifications every 3 minutes
616666e2f34c initial version
carl
parents:
diff changeset
314 if (!loader_run) break;
616666e2f34c initial version
carl
parents:
diff changeset
315 CONFIG &dc = *config;
616666e2f34c initial version
carl
parents:
diff changeset
316 time_t then = dc.load_time;
616666e2f34c initial version
carl
parents:
diff changeset
317 struct stat st;
616666e2f34c initial version
carl
parents:
diff changeset
318 bool reload = false;
616666e2f34c initial version
carl
parents:
diff changeset
319 for (string_set::iterator i=dc.config_files.begin(); i!=dc.config_files.end(); i++) {
19
b24369330483 Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 17
diff changeset
320 const char *fn = *i;
0
616666e2f34c initial version
carl
parents:
diff changeset
321 if (stat(fn, &st)) reload = true; // file disappeared
616666e2f34c initial version
carl
parents:
diff changeset
322 else if (st.st_mtime > then) reload = true; // file modified
616666e2f34c initial version
carl
parents:
diff changeset
323 if (reload) break;
616666e2f34c initial version
carl
parents:
diff changeset
324 }
616666e2f34c initial version
carl
parents:
diff changeset
325 if (reload) {
616666e2f34c initial version
carl
parents:
diff changeset
326 CONFIG *newc = new_conf();
616666e2f34c initial version
carl
parents:
diff changeset
327 if (newc) {
616666e2f34c initial version
carl
parents:
diff changeset
328 // replace the global config pointer
616666e2f34c initial version
carl
parents:
diff changeset
329 pthread_mutex_lock(&config_mutex);
616666e2f34c initial version
carl
parents:
diff changeset
330 CONFIG *old = config;
13
75e1a9bcbc2e gpl3, add removal option for original recipients
carl
parents: 10
diff changeset
331 bool last = old && (!old->reference_count);
0
616666e2f34c initial version
carl
parents:
diff changeset
332 config = newc;
616666e2f34c initial version
carl
parents:
diff changeset
333 pthread_mutex_unlock(&config_mutex);
13
75e1a9bcbc2e gpl3, add removal option for original recipients
carl
parents: 10
diff changeset
334 if (last) delete old; // there were no references to this config
0
616666e2f34c initial version
carl
parents:
diff changeset
335 }
616666e2f34c initial version
carl
parents:
diff changeset
336 else {
616666e2f34c initial version
carl
parents:
diff changeset
337 // failed to load new config
616666e2f34c initial version
carl
parents:
diff changeset
338 my_syslog("failed to load new configuration");
2
32b57406b656 initial version
carl
parents: 0
diff changeset
339 system("echo 'failed to load new sm-archive configuration from /etc/sm-archive' | mail -s 'error in /etc/sm-archive configuration' root");
0
616666e2f34c initial version
carl
parents:
diff changeset
340 // update the load time on the current config to prevent complaining every 3 minutes
616666e2f34c initial version
carl
parents:
diff changeset
341 dc.load_time = time(NULL);
616666e2f34c initial version
carl
parents:
diff changeset
342 }
616666e2f34c initial version
carl
parents:
diff changeset
343 }
616666e2f34c initial version
carl
parents:
diff changeset
344 }
616666e2f34c initial version
carl
parents:
diff changeset
345 return NULL;
616666e2f34c initial version
carl
parents:
diff changeset
346 }
616666e2f34c initial version
carl
parents:
diff changeset
347
616666e2f34c initial version
carl
parents:
diff changeset
348
19
b24369330483 Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 17
diff changeset
349 void usage(const char *prog);
b24369330483 Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 17
diff changeset
350 void usage(const char *prog)
0
616666e2f34c initial version
carl
parents:
diff changeset
351 {
616666e2f34c initial version
carl
parents:
diff changeset
352 fprintf(stderr, "Usage: %s [-d [level]] [-c] -p sm-sock-addr [-t timeout]\n", prog);
616666e2f34c initial version
carl
parents:
diff changeset
353 fprintf(stderr, "where sm-sock-addr is for the connection to sendmail\n");
616666e2f34c initial version
carl
parents:
diff changeset
354 fprintf(stderr, " and should be one of\n");
616666e2f34c initial version
carl
parents:
diff changeset
355 fprintf(stderr, " inet:port@ip-address\n");
616666e2f34c initial version
carl
parents:
diff changeset
356 fprintf(stderr, " local:local-domain-socket-file-name\n");
616666e2f34c initial version
carl
parents:
diff changeset
357 fprintf(stderr, "-c will load and dump the config to stdout\n");
616666e2f34c initial version
carl
parents:
diff changeset
358 fprintf(stderr, "-d will set the syslog message level, currently 0 to 3\n");
616666e2f34c initial version
carl
parents:
diff changeset
359 }
616666e2f34c initial version
carl
parents:
diff changeset
360
616666e2f34c initial version
carl
parents:
diff changeset
361
616666e2f34c initial version
carl
parents:
diff changeset
362
19
b24369330483 Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 17
diff changeset
363 void setup_socket(const char *sock);
b24369330483 Fedora 9 compile and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents: 17
diff changeset
364 void setup_socket(const char *sock) {
0
616666e2f34c initial version
carl
parents:
diff changeset
365 unlink(sock);
616666e2f34c initial version
carl
parents:
diff changeset
366 // sockaddr_un addr;
616666e2f34c initial version
carl
parents:
diff changeset
367 // memset(&addr, '\0', sizeof addr);
616666e2f34c initial version
carl
parents:
diff changeset
368 // addr.sun_family = AF_UNIX;
616666e2f34c initial version
carl
parents:
diff changeset
369 // strncpy(addr.sun_path, sock, sizeof(addr.sun_path)-1);
616666e2f34c initial version
carl
parents:
diff changeset
370 // int s = socket(AF_UNIX, SOCK_STREAM, 0);
616666e2f34c initial version
carl
parents:
diff changeset
371 // bind(s, (sockaddr*)&addr, sizeof(addr));
616666e2f34c initial version
carl
parents:
diff changeset
372 // close(s);
616666e2f34c initial version
carl
parents:
diff changeset
373 }
616666e2f34c initial version
carl
parents:
diff changeset
374
616666e2f34c initial version
carl
parents:
diff changeset
375
616666e2f34c initial version
carl
parents:
diff changeset
376 /*
616666e2f34c initial version
carl
parents:
diff changeset
377 * The signal handler function -- only gets called when a SIGCHLD
616666e2f34c initial version
carl
parents:
diff changeset
378 * is received, ie when a child terminates
616666e2f34c initial version
carl
parents:
diff changeset
379 */
616666e2f34c initial version
carl
parents:
diff changeset
380 void sig_chld(int signo)
616666e2f34c initial version
carl
parents:
diff changeset
381 {
616666e2f34c initial version
carl
parents:
diff changeset
382 int status;
616666e2f34c initial version
carl
parents:
diff changeset
383 /* Wait for any child without blocking */
616666e2f34c initial version
carl
parents:
diff changeset
384 while (waitpid(-1, &status, WNOHANG) > 0) {
616666e2f34c initial version
carl
parents:
diff changeset
385 // ignore child exit status, we only do this to cleanup zombies
616666e2f34c initial version
carl
parents:
diff changeset
386 }
616666e2f34c initial version
carl
parents:
diff changeset
387 }
616666e2f34c initial version
carl
parents:
diff changeset
388
616666e2f34c initial version
carl
parents:
diff changeset
389
616666e2f34c initial version
carl
parents:
diff changeset
390 int main(int argc, char**argv)
616666e2f34c initial version
carl
parents:
diff changeset
391 {
616666e2f34c initial version
carl
parents:
diff changeset
392 token_init();
616666e2f34c initial version
carl
parents:
diff changeset
393 bool check = false;
616666e2f34c initial version
carl
parents:
diff changeset
394 bool setconn = false;
616666e2f34c initial version
carl
parents:
diff changeset
395 int c;
616666e2f34c initial version
carl
parents:
diff changeset
396 const char *args = "p:t:d:ch";
616666e2f34c initial version
carl
parents:
diff changeset
397 extern char *optarg;
616666e2f34c initial version
carl
parents:
diff changeset
398
616666e2f34c initial version
carl
parents:
diff changeset
399 // Process command line options
616666e2f34c initial version
carl
parents:
diff changeset
400 while ((c = getopt(argc, argv, args)) != -1) {
616666e2f34c initial version
carl
parents:
diff changeset
401 switch (c) {
616666e2f34c initial version
carl
parents:
diff changeset
402 case 'p':
616666e2f34c initial version
carl
parents:
diff changeset
403 if (optarg == NULL || *optarg == '\0') {
616666e2f34c initial version
carl
parents:
diff changeset
404 fprintf(stderr, "Illegal sendmail socket: %s\n", optarg);
616666e2f34c initial version
carl
parents:
diff changeset
405 exit(EX_USAGE);
616666e2f34c initial version
carl
parents:
diff changeset
406 }
616666e2f34c initial version
carl
parents:
diff changeset
407 if (smfi_setconn(optarg) == MI_FAILURE) {
616666e2f34c initial version
carl
parents:
diff changeset
408 fprintf(stderr, "smfi_setconn failed\n");
616666e2f34c initial version
carl
parents:
diff changeset
409 exit(EX_SOFTWARE);
616666e2f34c initial version
carl
parents:
diff changeset
410 }
616666e2f34c initial version
carl
parents:
diff changeset
411 if (strncasecmp(optarg, "unix:", 5) == 0) setup_socket(optarg + 5);
616666e2f34c initial version
carl
parents:
diff changeset
412 else if (strncasecmp(optarg, "local:", 6) == 0) setup_socket(optarg + 6);
616666e2f34c initial version
carl
parents:
diff changeset
413 setconn = true;
616666e2f34c initial version
carl
parents:
diff changeset
414 break;
616666e2f34c initial version
carl
parents:
diff changeset
415
616666e2f34c initial version
carl
parents:
diff changeset
416 case 't':
616666e2f34c initial version
carl
parents:
diff changeset
417 if (optarg == NULL || *optarg == '\0') {
616666e2f34c initial version
carl
parents:
diff changeset
418 fprintf(stderr, "Illegal timeout: %s\n", optarg);
616666e2f34c initial version
carl
parents:
diff changeset
419 exit(EX_USAGE);
616666e2f34c initial version
carl
parents:
diff changeset
420 }
616666e2f34c initial version
carl
parents:
diff changeset
421 if (smfi_settimeout(atoi(optarg)) == MI_FAILURE) {
616666e2f34c initial version
carl
parents:
diff changeset
422 fprintf(stderr, "smfi_settimeout failed\n");
616666e2f34c initial version
carl
parents:
diff changeset
423 exit(EX_SOFTWARE);
616666e2f34c initial version
carl
parents:
diff changeset
424 }
616666e2f34c initial version
carl
parents:
diff changeset
425 break;
616666e2f34c initial version
carl
parents:
diff changeset
426
616666e2f34c initial version
carl
parents:
diff changeset
427 case 'c':
616666e2f34c initial version
carl
parents:
diff changeset
428 check = true;
616666e2f34c initial version
carl
parents:
diff changeset
429 break;
616666e2f34c initial version
carl
parents:
diff changeset
430
616666e2f34c initial version
carl
parents:
diff changeset
431 case 'd':
616666e2f34c initial version
carl
parents:
diff changeset
432 if (optarg == NULL || *optarg == '\0') debug_syslog = 1;
616666e2f34c initial version
carl
parents:
diff changeset
433 else debug_syslog = atoi(optarg);
616666e2f34c initial version
carl
parents:
diff changeset
434 break;
616666e2f34c initial version
carl
parents:
diff changeset
435
616666e2f34c initial version
carl
parents:
diff changeset
436 case 'h':
616666e2f34c initial version
carl
parents:
diff changeset
437 default:
616666e2f34c initial version
carl
parents:
diff changeset
438 usage(argv[0]);
616666e2f34c initial version
carl
parents:
diff changeset
439 exit(EX_USAGE);
616666e2f34c initial version
carl
parents:
diff changeset
440 }
616666e2f34c initial version
carl
parents:
diff changeset
441 }
616666e2f34c initial version
carl
parents:
diff changeset
442
616666e2f34c initial version
carl
parents:
diff changeset
443 if (check) {
616666e2f34c initial version
carl
parents:
diff changeset
444 use_syslog = false;
616666e2f34c initial version
carl
parents:
diff changeset
445 debug_syslog = 10;
616666e2f34c initial version
carl
parents:
diff changeset
446 CONFIG *conf = new_conf();
616666e2f34c initial version
carl
parents:
diff changeset
447 if (conf) {
616666e2f34c initial version
carl
parents:
diff changeset
448 conf->dump();
616666e2f34c initial version
carl
parents:
diff changeset
449 delete conf;
13
75e1a9bcbc2e gpl3, add removal option for original recipients
carl
parents: 10
diff changeset
450 clear_strings(); // for valgrind checking
0
616666e2f34c initial version
carl
parents:
diff changeset
451 return 0;
616666e2f34c initial version
carl
parents:
diff changeset
452 }
616666e2f34c initial version
carl
parents:
diff changeset
453 else {
616666e2f34c initial version
carl
parents:
diff changeset
454 return 1; // config failed to load
616666e2f34c initial version
carl
parents:
diff changeset
455 }
616666e2f34c initial version
carl
parents:
diff changeset
456 }
616666e2f34c initial version
carl
parents:
diff changeset
457
616666e2f34c initial version
carl
parents:
diff changeset
458 if (!setconn) {
616666e2f34c initial version
carl
parents:
diff changeset
459 fprintf(stderr, "%s: Missing required -p argument\n", argv[0]);
616666e2f34c initial version
carl
parents:
diff changeset
460 usage(argv[0]);
616666e2f34c initial version
carl
parents:
diff changeset
461 exit(EX_USAGE);
616666e2f34c initial version
carl
parents:
diff changeset
462 }
616666e2f34c initial version
carl
parents:
diff changeset
463
616666e2f34c initial version
carl
parents:
diff changeset
464 if (smfi_register(smfilter) == MI_FAILURE) {
616666e2f34c initial version
carl
parents:
diff changeset
465 fprintf(stderr, "smfi_register failed\n");
616666e2f34c initial version
carl
parents:
diff changeset
466 exit(EX_UNAVAILABLE);
616666e2f34c initial version
carl
parents:
diff changeset
467 }
616666e2f34c initial version
carl
parents:
diff changeset
468
616666e2f34c initial version
carl
parents:
diff changeset
469 // switch to background mode
616666e2f34c initial version
carl
parents:
diff changeset
470 if (daemon(1,0) < 0) {
616666e2f34c initial version
carl
parents:
diff changeset
471 fprintf(stderr, "daemon() call failed\n");
616666e2f34c initial version
carl
parents:
diff changeset
472 exit(EX_UNAVAILABLE);
616666e2f34c initial version
carl
parents:
diff changeset
473 }
616666e2f34c initial version
carl
parents:
diff changeset
474
616666e2f34c initial version
carl
parents:
diff changeset
475 // write the pid
2
32b57406b656 initial version
carl
parents: 0
diff changeset
476 const char *pidpath = "/var/run/sm-archive.pid";
0
616666e2f34c initial version
carl
parents:
diff changeset
477 unlink(pidpath);
616666e2f34c initial version
carl
parents:
diff changeset
478 FILE *f = fopen(pidpath, "w");
616666e2f34c initial version
carl
parents:
diff changeset
479 if (f) {
616666e2f34c initial version
carl
parents:
diff changeset
480 #ifdef linux
616666e2f34c initial version
carl
parents:
diff changeset
481 // from a comment in the DCC source code:
616666e2f34c initial version
carl
parents:
diff changeset
482 // Linux threads are broken. Signals given the
616666e2f34c initial version
carl
parents:
diff changeset
483 // original process are delivered to only the
616666e2f34c initial version
carl
parents:
diff changeset
484 // thread that happens to have that PID. The
616666e2f34c initial version
carl
parents:
diff changeset
485 // sendmail libmilter thread that needs to hear
616666e2f34c initial version
carl
parents:
diff changeset
486 // SIGINT and other signals does not, and that breaks
616666e2f34c initial version
carl
parents:
diff changeset
487 // scripts that need to stop milters.
616666e2f34c initial version
carl
parents:
diff changeset
488 // However, signaling the process group works.
616666e2f34c initial version
carl
parents:
diff changeset
489 fprintf(f, "-%d\n", (u_int)getpgrp());
616666e2f34c initial version
carl
parents:
diff changeset
490 #else
616666e2f34c initial version
carl
parents:
diff changeset
491 fprintf(f, "%d\n", (u_int)getpid());
616666e2f34c initial version
carl
parents:
diff changeset
492 #endif
616666e2f34c initial version
carl
parents:
diff changeset
493 fclose(f);
616666e2f34c initial version
carl
parents:
diff changeset
494 }
616666e2f34c initial version
carl
parents:
diff changeset
495
616666e2f34c initial version
carl
parents:
diff changeset
496 // initialize the thread sync objects
616666e2f34c initial version
carl
parents:
diff changeset
497 pthread_mutex_init(&config_mutex, 0);
616666e2f34c initial version
carl
parents:
diff changeset
498 pthread_mutex_init(&syslog_mutex, 0);
616666e2f34c initial version
carl
parents:
diff changeset
499
616666e2f34c initial version
carl
parents:
diff changeset
500 // drop root privs
616666e2f34c initial version
carl
parents:
diff changeset
501 struct passwd *pw = getpwnam("sm-archive");
616666e2f34c initial version
carl
parents:
diff changeset
502 if (pw) {
616666e2f34c initial version
carl
parents:
diff changeset
503 if (setgid(pw->pw_gid) == -1) {
2
32b57406b656 initial version
carl
parents: 0
diff changeset
504 my_syslog("failed to switch to group sm-archive");
0
616666e2f34c initial version
carl
parents:
diff changeset
505 }
616666e2f34c initial version
carl
parents:
diff changeset
506 if (setuid(pw->pw_uid) == -1) {
2
32b57406b656 initial version
carl
parents: 0
diff changeset
507 my_syslog("failed to switch to user sm-archive");
0
616666e2f34c initial version
carl
parents:
diff changeset
508 }
616666e2f34c initial version
carl
parents:
diff changeset
509 }
616666e2f34c initial version
carl
parents:
diff changeset
510
616666e2f34c initial version
carl
parents:
diff changeset
511 // load the initial config
616666e2f34c initial version
carl
parents:
diff changeset
512 config = new_conf();
616666e2f34c initial version
carl
parents:
diff changeset
513 if (!config) {
616666e2f34c initial version
carl
parents:
diff changeset
514 my_syslog("failed to load initial configuration, quitting");
616666e2f34c initial version
carl
parents:
diff changeset
515 exit(1);
616666e2f34c initial version
carl
parents:
diff changeset
516 }
616666e2f34c initial version
carl
parents:
diff changeset
517
616666e2f34c initial version
carl
parents:
diff changeset
518 // only create threads after the fork() in daemon
616666e2f34c initial version
carl
parents:
diff changeset
519 pthread_t tid;
616666e2f34c initial version
carl
parents:
diff changeset
520 if (pthread_create(&tid, 0, config_loader, 0))
616666e2f34c initial version
carl
parents:
diff changeset
521 my_syslog("failed to create config loader thread");
616666e2f34c initial version
carl
parents:
diff changeset
522 if (pthread_detach(tid))
616666e2f34c initial version
carl
parents:
diff changeset
523 my_syslog("failed to detach config loader thread");
616666e2f34c initial version
carl
parents:
diff changeset
524
616666e2f34c initial version
carl
parents:
diff changeset
525 time_t starting = time(NULL);
616666e2f34c initial version
carl
parents:
diff changeset
526 int rc = smfi_main();
616666e2f34c initial version
carl
parents:
diff changeset
527 if ((rc != MI_SUCCESS) && (time(NULL) > starting+5*60)) {
616666e2f34c initial version
carl
parents:
diff changeset
528 my_syslog("trying to restart after smfi_main()");
616666e2f34c initial version
carl
parents:
diff changeset
529 loader_run = false; // eventually the config loader thread will terminate
616666e2f34c initial version
carl
parents:
diff changeset
530 execvp(argv[0], argv);
616666e2f34c initial version
carl
parents:
diff changeset
531 }
616666e2f34c initial version
carl
parents:
diff changeset
532 exit((rc == MI_SUCCESS) ? 0 : EX_UNAVAILABLE);
616666e2f34c initial version
carl
parents:
diff changeset
533 }
616666e2f34c initial version
carl
parents:
diff changeset
534