File: 03_syslog.diff

package info (click to toggle)
nullmailer 1%3A1.13-1%2Bdeb8u1
  • links: PTS
  • area: main
  • in suites: jessie
  • size: 1,736 kB
  • ctags: 1,204
  • sloc: cpp: 5,438; sh: 1,693; makefile: 205; perl: 184
file content (327 lines) | stat: -rw-r--r-- 10,574 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#! /bin/sh -e
if [ $# -ne 1 ]; then 
    echo >&2 "`basename $0`: script expects -patch|-unpatch as argument"
    exit 1  
fi
case "$1" in 
    -patch) patch -f --no-backup-if-mismatch -p1 < $0;;
    -unpatch) patch -f --no-backup-if-mismatch -R -p1 < $0;;
    *)
        echo >&2 "`basename $0`: script expects -patch|-unpatch as argument"
        exit 1;;
esac

exit 0

@DPATCH@
Index: nullmailer-1.13/doc/nullmailer-send.8
===================================================================
--- nullmailer-1.13.orig/doc/nullmailer-send.8	2014-07-31 21:25:35.000000000 +0100
+++ nullmailer-1.13/doc/nullmailer-send.8	2014-07-31 21:25:36.000000000 +0100
@@ -3,6 +3,7 @@
 nullmailer-send \- Send queued messages
 .SH SYNOPSIS
 .B nullmailer-send
+.RB [ \-\-daemon ]\ [ \-\-syslog ]
 .SH DESCRIPTION
 This program is responsible for coordinating the transmission of
 messages that have been queued by
@@ -42,6 +43,15 @@
 sleeps for a number of seconds specified by
 .B pausetime
 before retrying sending the contents of the queue.
+.SH OPTIONS
+.TP
+.BR \-d ,\  \-\-daemon
+Fork into the background, implies \-\-syslog.
+.TP
+.BR \-s ,\  \-\-syslog
+Use syslog for error and log messages.
+With \-\-daemon, syslog will be used exclusively, without \-\-daemon,
+syslog will be used additionally.
 .SH CONTROL FILES
 All the control files are reread each time the queue is run.
 .TP
Index: nullmailer-1.13/protocols/protocol.cc
===================================================================
--- nullmailer-1.13.orig/protocols/protocol.cc	2014-07-31 21:25:15.000000000 +0100
+++ nullmailer-1.13/protocols/protocol.cc	2014-07-31 21:25:36.000000000 +0100
@@ -22,11 +22,15 @@
 #include <config.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <sys/syslog.h>
 #include "connect.h"
 #include "errcodes.h"
 #include "protocol.h"
 #include "cli++.h"
 
+static int use_syslog = 0;
+static int daemonize  = 0;
+
 const char* user = 0;
 const char* pass = 0;
 int port = 0;
@@ -44,6 +48,8 @@
     "Set the user name for authentication", 0 },
   { 0, "pass", cli_option::string, 0, &pass,
     "Set the password for authentication", 0 },
+  { 'd', "daemon", cli_option::flag, 1, &daemonize,  "use syslog exclusively ", 0 },
+  { 's', "syslog", cli_option::flag, 1, &use_syslog, "use syslog additionally", 0 },
   { 0, "auth-login", cli_option::flag, AUTH_LOGIN, &auth_method,
     "Use AUTH LOGIN instead of auto-detecting in SMTP", 0 },
 #ifdef HAVE_TLS
@@ -67,13 +73,19 @@
 
 void protocol_fail(int e, const char* msg)
 {
-  ferr << cli_program << ": Failed: " << msg << endl;
+  if (use_syslog)
+    syslog(LOG_ERR, "%s: Failed: %s", cli_program, msg);
+  if (!daemonize)
+    ferr << cli_program << ": Failed: " << msg << endl;
   exit(e);
 }
 
 void protocol_succ(const char* msg)
 {
-  ferr << cli_program << ": Succeeded: " << msg << endl;
+  if (use_syslog)
+    syslog(LOG_INFO, "%s: Succeeded: %s", cli_program, msg);
+  if (!daemonize)
+    ferr << cli_program << ": Succeeded: " << msg << endl;
   exit(0);
 }
 
@@ -93,6 +105,10 @@
 
 int cli_main(int, char* argv[])
 {
+  if (daemonize)
+    use_syslog = 1;
+  if (use_syslog)
+    openlog("nullmailer", LOG_CONS | LOG_PID, LOG_MAIL);
   const char* remote = argv[0];
   if (port == 0)
     port = use_ssl ? default_ssl_port : default_port;
Index: nullmailer-1.13/src/Makefile.in
===================================================================
--- nullmailer-1.13.orig/src/Makefile.in	2014-07-31 21:25:15.000000000 +0100
+++ nullmailer-1.13/src/Makefile.in	2014-07-31 21:25:36.000000000 +0100
@@ -61,7 +61,7 @@
 nullmailer_queue_DEPENDENCIES = ../lib/libnullmailer.a
 am_nullmailer_send_OBJECTS = send.$(OBJEXT)
 nullmailer_send_OBJECTS = $(am_nullmailer_send_OBJECTS)
-nullmailer_send_DEPENDENCIES = ../lib/libnullmailer.a
+nullmailer_send_DEPENDENCIES = ../lib/cli++/libcli++.a ../lib/libnullmailer.a
 am_nullmailer_smtpd_OBJECTS = smtpd.$(OBJEXT)
 nullmailer_smtpd_OBJECTS = $(am_nullmailer_smtpd_OBJECTS)
 nullmailer_smtpd_DEPENDENCIES = ../lib/libnullmailer.a
@@ -182,13 +182,13 @@
 #noinst_PROGRAMS = address
 INCLUDES = -I../lib -I../lib/cli++
 mailq_SOURCES = mailq.cc
-mailq_LDADD = ../lib/libnullmailer.a
+mailq_LDADD = ../lib/cli++/libcli++.a ../lib/libnullmailer.a
 nullmailer_inject_SOURCES = inject.cc
 nullmailer_inject_LDADD = ../lib/cli++/libcli++.a ../lib/libnullmailer.a
 nullmailer_queue_SOURCES = queue.cc
 nullmailer_queue_LDADD = ../lib/libnullmailer.a
 nullmailer_send_SOURCES = send.cc
-nullmailer_send_LDADD = ../lib/libnullmailer.a
+nullmailer_send_LDADD = ../lib/cli++/libcli++.a ../lib/libnullmailer.a
 nullmailer_smtpd_SOURCES = smtpd.cc
 nullmailer_smtpd_LDADD = ../lib/libnullmailer.a
 sendmail_SOURCES = sendmail.cc
Index: nullmailer-1.13/src/send.cc
===================================================================
--- nullmailer-1.13.orig/src/send.cc	2014-07-31 21:25:15.000000000 +0100
+++ nullmailer-1.13/src/send.cc	2014-07-31 21:25:36.000000000 +0100
@@ -27,6 +27,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <sys/stat.h>
+#include <sys/syslog.h>
 #include <sys/types.h>
 #include <sys/wait.h>
 #include <unistd.h>
@@ -40,14 +41,30 @@
 #include "list.h"
 #include "selfpipe.h"
 #include "setenv.h"
+#include "cli++/cli++.h"
 
 selfpipe selfpipe;
 
 typedef list<mystring> slist;
 
-#define fail(MSG) do { fout << MSG << endl; return false; } while(0)
-#define fail2(MSG1,MSG2) do{ fout << MSG1 << MSG2 << endl; return false; }while(0)
-#define fail_sys(MSG) do{ fout << MSG << strerror(errno) << endl; return false; }while(0)
+static int use_syslog = 0;
+static int daemonize  = 0;
+
+const char* cli_program     = "nullmailer-send";
+const char* cli_help_prefix = "nullmailer daemon\n";
+const char* cli_help_suffix = "";
+const char* cli_args_usage  = "";
+const int cli_args_min = 0;
+const int cli_args_max = 0;
+cli_option cli_options[] = {
+  { 'd', "daemon", cli_option::flag, 1, &daemonize,  "daemonize , implies --syslog", 0 },
+  { 's', "syslog", cli_option::flag, 1, &use_syslog, "use syslog", 0 },
+  { 0, 0, cli_option::flag, 0, 0, 0, 0 }
+};
+
+#define fail(MSG) do { if (use_syslog) syslog(LOG_ERR, "%s", MSG); if (!daemonize) ferr << MSG << endl; return false; } while (0)
+#define fail2(MSG1,MSG2) do { if (use_syslog) syslog(LOG_ERR, "%s %s", MSG1, MSG2); if (!daemonize) fout << MSG1 << MSG2 << endl; return false; } while (0)
+#define fail_sys(MSG) do { if (use_syslog) syslog(LOG_ERR, "%s %s", MSG, strerror(errno)); if (!daemonize) ferr << MSG << strerror(errno) << endl; return false; } while (0)
 
 struct remote
 {
@@ -162,7 +179,10 @@
 bool load_files()
 {
   reload_files = false;
-  fout << "Rescanning queue." << endl;
+  if (use_syslog)
+    syslog(LOG_INFO, "Rescanning queue.");
+  if (!daemonize)
+    fout << "Rescanning queue." << endl;
   DIR* dir = opendir(".");
   if(!dir)
     fail_sys("Cannot open queue directory: ");
@@ -180,12 +200,19 @@
 
 void exec_protocol(int fd, remote& remote)
 {
-  if(close(0) == -1 || dup2(fd, 0) == -1 || close(fd) == -1)
+  if (!daemonize && close(STDIN_FILENO) < 0)
     return;
+  if (fd != STDIN_FILENO)
+    if (dup2(fd, STDIN_FILENO) < 0 || close(fd) < 0)
+      return;
   mystring program = PROTOCOL_DIR + remote.proto;
-  const char* args[3+remote.options.count()];
+  const char* args[5+remote.options.count()];
   unsigned i = 0;
   args[i++] = program.c_str();
+  if (daemonize)
+    args[i++] = "-d";
+  if (use_syslog)
+    args[i++] = "-s";
   for(slist::const_iter opt(remote.options); opt; opt++)
     args[i++] = strdup((*opt).c_str());
   args[i++] = remote.host.c_str();
@@ -222,7 +249,10 @@
       if(status)
 	fail2("Sending failed: ", errorstr(status));
       else {
-	fout << "Sent file." << endl;
+        if (use_syslog)
+          syslog(LOG_INFO, "Sent file.");
+        if (!daemonize)
+             fout << "Sent file." << endl;
 	return true;
       }
     }
@@ -235,10 +265,20 @@
 {
   int fd = open(filename.c_str(), O_RDONLY);
   if(fd == -1) {
-    fout << "Can't open file '" << filename << "'" << endl;
+    if (use_syslog)
+      syslog(LOG_ERR, "Can't open file '%s'", filename.c_str());
+    if (!daemonize)
+      fout << "Can't open file '" << filename << "'" << endl;
     return false;
   }
-  fout << "Starting delivery: protocol: " << remote.proto
+  if (use_syslog)
+    syslog(LOG_INFO, "Starting delivery: protocol: %s host: %s file: %s",
+      remote.proto.c_str(), remote.host.c_str(), filename.c_str());
+  if (!daemonize)
+  if (use_syslog)
+    syslog(LOG_INFO, "Starting delivery, %d message(s) in queue.", files.count());
+  if (!daemonize)
+    fout << "Starting delivery: protocol: " << remote.proto
        << " host: " << remote.host
        << " file: " << filename << endl;
   pid_t pid = fork();
@@ -277,7 +317,10 @@
 	file++;
     }
   }
-  fout << "Delivery complete, "
+  if (use_syslog)
+    syslog(LOG_INFO, "Delivery complete, %d message(s) remain.", files.count());
+  if (!daemonize)
+    fout << "Delivery complete, "
        << itoa(files.count()) << " message(s) remain." << endl;
   return true;
 }
@@ -329,7 +372,10 @@
 
   int s = select(trigger+1, &readfds, 0, 0, &timeout);
   if(s == 1) {
-    fout << "Trigger pulled." << endl;
+    if (use_syslog)
+      syslog(LOG_INFO, "Trigger pulled.");
+    if (!daemonize)
+      fout << "Trigger pulled." << endl;
     read_trigger();
     reload_files = true;
     pausetime = minpause;
@@ -343,8 +389,14 @@
   return true;
 }
 
-int main(int, char*[])
+int cli_main(int, char*[])
 {
+  pid_t pid;
+
+  if (daemonize)
+    use_syslog = 1;
+  if (use_syslog)
+    openlog("nullmailer", LOG_CONS | LOG_PID, LOG_MAIL);
   read_hostnames();
 
   if(!selfpipe) {
@@ -353,13 +405,34 @@
   }
   selfpipe.catchsig(SIGCHLD);
   
-  if(!open_trigger())
+  if(!open_trigger()) {
+    if (use_syslog)
+      syslog(LOG_CRIT, "Could not open trigger.");
+    if (!daemonize)
+      ferr << "Could not open trigger." << endl;
     return 1;
+  }
   if(chdir(QUEUE_MSG_DIR) == -1) {
     fout << "Could not chdir to queue message directory." << endl;
+    if (use_syslog)
+      syslog(LOG_CRIT, "Could not chdir to queue message directory.");
+    if (!daemonize)
+      ferr << "Could not chdir to queue message directory." << endl;
     return 1;
   }
-  
+
+  if (daemonize) {
+    if ((pid = fork()) < 0) {
+      syslog(LOG_CRIT, "Could not fork.");
+      return 1;
+    }
+    if (pid)
+      return 0;
+    close(STDIN_FILENO);
+    close(STDOUT_FILENO);
+    close(STDERR_FILENO);
+  }
+
   signal(SIGALRM, catch_alrm);
   signal(SIGHUP, SIG_IGN);
   load_config();