annotate src/sm-archive.cpp @ 8:ed73e6361570 stable-1-0

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