annotate src/sm-archive.cpp @ 27:9298f8b00db2 stable-1-0-10

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