forked from haraka/Haraka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
309 lines (266 loc) · 8.95 KB
/
server.js
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
'use strict';
// smtp network server
var net = require('./tls_socket');
var logger = require('./logger');
var config = require('./config');
var conn = require('./connection');
var out = require('./outbound');
var plugins = require('./plugins');
var constants = require('./constants');
var os = require('os');
var cluster = require('cluster');
var async = require('async');
var daemon = require('daemon');
// Need these here so we can run hooks
logger.add_log_methods(exports, 'server');
var Server = exports;
Server.load_smtp_ini = function () {
Server.cfg = config.get('smtp.ini', {
booleans: [
'-main.daemonize',
],
}, function () {
Server.load_smtp_ini();
});
var defaults = {
inactivity_timeout: 600,
daemon_log_file: '/var/log/haraka.log',
daemon_pid_file: '/var/run/haraka.pid'
};
for (var key in defaults) {
if (Server.cfg.main[key] !== undefined) continue;
Server.cfg.main[key] = defaults[key];
}
};
Server.load_smtp_ini();
Server.daemonize = function (config_data) {
var c = this.cfg.main;
if (!c.daemonize) return;
if (!process.env.__daemon) {
// Remove process.on('exit') listeners otherwise
// we get a spurious 'Exiting' log entry.
process.removeAllListeners('exit');
logger.lognotice('Daemonizing...');
}
var log_fd = require('fs').openSync(c.daemon_log_file, 'a');
daemon({stdout: log_fd});
// We are the daemon from here on...
var npid = require('npid');
try {
npid.create(c.daemon_pid_file).removeOnExit();
}
catch (err) {
logger.logerror(err.message);
process.exit(1);
}
};
Server.flushQueue = function () {
if (!Server.cluster) {
out.flush_queue();
return;
}
for (var id in cluster.workers) {
cluster.workers[id].send({event: 'outbound.flush_queue'});
}
};
Server.get_listen_addrs = function (cfg, port) {
if (!port) port = 25;
var listeners = [];
if (cfg && cfg.listen) {
listeners = cfg.listen.split(/\s*,\s*/);
if (listeners[0] === '') listeners = [];
for (var i=0; i < listeners.length; i++) {
if (/:[0-9]{1,5}$/.test(listeners[i])) continue;
listeners[i] = listeners[i] + ':' + port;
}
}
if (cfg.port) {
var host = cfg.listen_host;
if (!host) {
host = '[::0]';
Server.default_host = true;
}
listeners.unshift(host + ':' + cfg.port);
}
if (listeners.length) return listeners;
Server.default_host = true;
listeners.push('[::0]:' + port);
return listeners;
};
Server.createServer = function (params) {
var c = Server.cfg.main;
for (var key in params) {
if (typeof params[key] === 'function') continue;
c[key] = params[key];
}
Server.notes = {};
plugins.server = Server;
plugins.load_plugins();
var inactivity_timeout = (c.inactivity_timeout || 300) * 1000;
if (!cluster || !c.nodes) {
Server.daemonize(c);
Server.setup_smtp_listeners(plugins, 'master', inactivity_timeout);
return;
}
// Cluster
Server.cluster = cluster;
if (!cluster.isMaster) { // Workers
Server.setup_smtp_listeners(plugins, "child", inactivity_timeout);
return;
}
// Master
out.scan_queue_pids(function (err, pids) {
if (err) {
Server.logcrit("Scanning queue failed. Shutting down.");
process.exit(1);
}
Server.daemonize();
// Fork workers
var workers = (c.nodes === 'cpus') ? os.cpus().length : c.nodes;
var new_workers = [];
for (var i=0; i<workers; i++) {
new_workers.push(cluster.fork({ CLUSTER_MASTER_PID: process.pid }));
}
for (var j=0; j<pids.length; j++) {
new_workers[j % new_workers.length].send({event: 'outbound.load_pid_queue', data: pids[j]});
}
cluster.on('online', function (worker) {
logger.lognotice('worker ' + worker.id + ' started pid=' + worker.process.pid);
});
cluster.on('listening', function (worker, address) {
logger.lognotice('worker ' + worker.id + ' listening on ' + address.address + ':' + address.port);
});
cluster.on('exit', function (worker, code, signal) {
if (signal) {
logger.lognotice('worker ' + worker.id + ' killed by signal ' + signal);
}
else if (code !== 0) {
logger.lognotice('worker ' + worker.id + ' exited with error code: ' + code);
}
if (signal || code !== 0) {
// Restart worker
var new_worker = cluster.fork({ CLUSTER_MASTER_PID: process.pid });
new_worker.send({event: 'outbound.load_pid_queue', data: worker.process.pid});
}
});
plugins.run_hooks('init_master', Server);
});
};
Server.get_smtp_server = function (host, port, inactivity_timeout) {
var server;
var conn_cb = function (client) {
client.setTimeout(inactivity_timeout);
conn.createConnection(client, server);
};
if (port != 465) {
server = net.createServer(conn_cb);
return server;
}
var options = {
key: config.get('tls_key.pem', 'binary'),
cert: config.get('tls_cert.pem', 'binary'),
};
if (!options.key) {
logger.logerror("Missing tls_key.pem for port 465");
return;
}
if (!options.cert) {
logger.logerror("Missing tls_cert.pem for port 465");
return;
}
logger.logdebug("Creating TLS server on " + host + ':' + port);
server = require('tls').createServer(options, conn_cb);
server.has_tls=true;
return server;
};
Server.setup_smtp_listeners = function (plugins, type, inactivity_timeout) {
var listeners = Server.get_listen_addrs(Server.cfg.main);
var runInitHooks = function (err) {
if (err) {
logger.logerror("Failed to setup listeners: " + err.message);
return process.exit(-1);
}
Server.listening();
plugins.run_hooks('init_' + type, Server);
};
var setupListener = function (host_port, cb) {
var hp = /^\[?([^\]]+)\]?:(\d+)$/.exec(host_port);
if (!hp) {
return cb(new Error("Invalid format for listen parameter in smtp.ini"));
}
var host = hp[1];
var port = hp[2];
var server = Server.get_smtp_server(host, port, inactivity_timeout);
if (!server) return cb();
server.notes = Server.notes;
if (Server.cluster) server.cluster = Server.cluster;
server.on('listening', function () {
var addr = this.address();
logger.lognotice("Listening on " + addr.address + ':' + addr.port);
cb();
});
// Fallback from IPv6 to IPv4 if not supported
// But only if we supplied the default of [::0]:25
server.on('error', function (e) {
if (e.code === 'EAFNOSUPPORT' && /^::0/.test(host) && Server.default_host) {
server.listen(port, '0.0.0.0');
}
else {
// Pass error to callback
cb(e);
}
});
server.listen(port, host);
};
async.each(listeners, setupListener, runInitHooks);
};
Server.init_master_respond = function (retval, msg) {
Server.ready = 1;
switch(retval) {
case constants.ok:
case constants.cont:
// Load the queue if we're just one process
if (!(cluster && config.get('smtp.ini').main.nodes)) {
out.load_queue();
}
break;
default:
Server.logerror("init_master returned error" + ((msg) ? ': ' + msg : ''));
process.exit(1);
}
};
Server.init_child_respond = function (retval, msg) {
switch(retval) {
case constants.ok:
case constants.cont:
break;
default:
var pid = process.env.CLUSTER_MASTER_PID;
Server.logerror("init_child returned error" + ((msg) ? ': ' + msg : ''));
try {
if (pid) {
process.kill(pid);
Server.logerror('Killing master (pid=' + pid + ')');
}
}
catch (err) {
Server.logerror('Terminating child');
}
process.exit(1);
}
};
Server.listening = function () {
var c = Server.cfg.main;
// Drop privileges
if (c.group) {
Server.lognotice('Switching from current gid: ' + process.getgid());
process.setgid(c.group);
Server.lognotice('New gid: ' + process.getgid());
}
if (c.user) {
Server.lognotice('Switching from current uid: ' + process.getuid());
process.setuid(c.user);
Server.lognotice('New uid: ' + process.getuid());
}
Server.ready = 1;
};