-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathdockerMailserver.js
More file actions
595 lines (520 loc) · 17.4 KB
/
dockerMailserver.js
File metadata and controls
595 lines (520 loc) · 17.4 KB
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
const Docker = require('dockerode');
const docker = new Docker({ socketPath: '/var/run/docker.sock' });
// Docker container name for docker-mailserver
const DOCKER_CONTAINER = process.env.DOCKER_CONTAINER || 'mailserver';
const OPENDKIM_KEYS_PATH =
process.env.OPENDKIM_KEYS_PATH || '/tmp/docker-mailserver/opendkim/keys';
// Debug flag
const DEBUG = process.env.DEBUG_DOCKER === 'true';
/**
* Debug logger that only logs if DEBUG is true
* @param {string} message - Message to log
* @param {any} data - Optional data to log
*/
function debugLog(message, data = null) {
if (DEBUG) {
if (data) {
console.log(`[DOCKER-DEBUG] ${message}`, data);
} else {
console.log(`[DOCKER-DEBUG] ${message}`);
}
}
}
/**
* Escapes a string for safe use in shell commands by wrapping it in single quotes
* and escaping any single quotes within the string
* @param {string} arg - Argument to escape
* @return {string} Escaped argument safe for shell execution
*/
function escapeShellArg(arg) {
// Replace single quotes with '\'' (end quote, escaped quote, start quote)
// Then wrap the entire string in single quotes
return `'${arg.replace(/'/g, "'\\''")}'`;
}
/**
* Normalizes a domain to lowercase and trims surrounding whitespace.
* @param {string} domain - Domain candidate
* @return {string|null} Normalized domain or null if invalid
*/
function normalizeDomain(domain) {
if (!domain || typeof domain !== 'string') {
return null;
}
const normalized = domain.trim().toLowerCase();
if (!normalized || !/^[a-z0-9.-]+$/.test(normalized)) {
return null;
}
return normalized;
}
/**
* Extracts a domain from an email address.
* @param {string} email - Email address
* @return {string|null} Extracted domain or null when invalid
*/
function extractDomainFromEmail(email) {
if (!email || typeof email !== 'string') {
return null;
}
const parts = email.trim().split('@');
if (parts.length !== 2 || !parts[1]) {
return null;
}
return normalizeDomain(parts[1]);
}
/**
* Executes a command in the docker-mailserver container
* @param {string} command Command to execute
* @return {Promise<string>} stdout from the command
*/
async function execInContainer(command) {
try {
debugLog(`Executing command in container ${DOCKER_CONTAINER}: ${command}`);
// Get container instance
const container = docker.getContainer(DOCKER_CONTAINER);
// Create exec instance
const exec = await container.exec({
Cmd: ['sh', '-c', command],
AttachStdout: true,
AttachStderr: true,
});
// Start exec instance
const stream = await exec.start();
// Collect output
return new Promise((resolve, reject) => {
let stdoutData = '';
let stderrData = '';
stream.on('data', (chunk) => {
// Docker multiplexes stdout/stderr in the same stream
// First 8 bytes contain header, actual data starts at 8th byte
stdoutData += chunk.slice(8).toString();
});
stream.on('end', () => {
debugLog(`Command completed. Output:`, stdoutData);
resolve(stdoutData);
});
stream.on('error', (err) => {
debugLog(`Command error:`, err);
reject(err);
});
});
} catch (error) {
console.error(`Error executing command in container: ${command}`, error);
debugLog(`Execution error:`, error);
throw error;
}
}
/**
* Executes a setup.sh command in the docker-mailserver container
* @param {string} setupCommand Command to pass to setup.sh
* @return {Promise<string>} stdout from the command
*/
async function execSetup(setupCommand) {
// The setup.sh script is usually located at /usr/local/bin/setup.sh or /usr/local/bin/setup in docker-mailserver
debugLog(`Executing setup command: ${setupCommand}`);
return execInContainer(`/usr/local/bin/setup ${setupCommand}`);
}
/**
* Reads immediate child directories in OPENDKIM keys path (domain folders).
* @return {Promise<string[]>} Domain names discovered from folder structure
*/
async function getDomainsFromOpendkimKeysPath() {
const escapedPath = escapeShellArg(OPENDKIM_KEYS_PATH);
const stdout = await execInContainer(
`if [ -d ${escapedPath} ]; then ls -1 ${escapedPath}; fi`
);
return stdout
.split('\n')
.map((line) => line.replace(/[\x00-\x1F\x7F-\x9F]/g, '').trim())
.filter((line) => line.length > 0)
.map((line) => normalizeDomain(line))
.filter(Boolean);
}
/**
* Parses OpenDKIM TXT file content and extracts DNS-ready fields.
* @param {string} rawDkimTxt - Raw content of mail.txt
* @return {{recordName: string, recordType: string, recordValue: string, raw: string}}
*/
function parseDkimTxt(rawDkimTxt) {
const cleanedRaw = rawDkimTxt
.replace(/[\x00-\x1F\x7F-\x9F]/g, '')
.replace(/\s+/g, ' ')
.trim();
const nameMatch = cleanedRaw.match(/^([^\s]+)\s+IN\s+TXT/i);
const recordName = nameMatch ? nameMatch[1] : 'mail._domainkey';
const quotedParts = [...cleanedRaw.matchAll(/"([^"]+)"/g)].map(
(match) => match[1]
);
const recordValue = quotedParts.join('').replace(/\s+/g, ' ').trim();
return {
recordName,
recordType: 'TXT',
recordValue,
raw: cleanedRaw,
};
}
/**
* Reads DKIM TXT record from mail.txt for a specific domain.
* @param {string} domain - Domain name
* @return {Promise<{configured: boolean, selector: string, recordName: string|null, recordType: string, recordValue: string|null, raw: string|null}>}
*/
async function getDomainDkim(domain) {
const normalizedDomain = normalizeDomain(domain);
if (!normalizedDomain) {
return {
configured: false,
selector: 'mail',
recordName: null,
recordType: 'TXT',
recordValue: null,
raw: null,
};
}
const dkimFilePath = `${OPENDKIM_KEYS_PATH}/${normalizedDomain}/mail.txt`;
const escapedFilePath = escapeShellArg(dkimFilePath);
const stdout = await execInContainer(
`if [ -f ${escapedFilePath} ]; then cat ${escapedFilePath}; fi`
);
const rawDkimTxt = stdout.trim();
if (!rawDkimTxt) {
return {
configured: false,
selector: 'mail',
recordName: null,
recordType: 'TXT',
recordValue: null,
raw: null,
};
}
const parsed = parseDkimTxt(rawDkimTxt);
return {
configured: true,
selector: 'mail',
recordName: parsed.recordName,
recordType: parsed.recordType,
recordValue: parsed.recordValue,
raw: parsed.raw,
};
}
/**
* Returns domain overview with DKIM, SPF, and DMARC DNS data.
* @return {Promise<Array>} Domain overview list
*/
async function getDomainsOverview() {
try {
const [accounts, aliases, keyPathDomains] = await Promise.all([
getAccounts(),
getAliases(),
getDomainsFromOpendkimKeysPath(),
]);
const domainsSet = new Set();
accounts.forEach((account) => {
const domain = extractDomainFromEmail(account.email);
if (domain) {
domainsSet.add(domain);
}
});
aliases.forEach((alias) => {
const sourceDomain = extractDomainFromEmail(alias.source);
const destinationDomain = extractDomainFromEmail(alias.destination);
if (sourceDomain) {
domainsSet.add(sourceDomain);
}
if (destinationDomain) {
domainsSet.add(destinationDomain);
}
});
keyPathDomains.forEach((domain) => domainsSet.add(domain));
const domains = Array.from(domainsSet).sort((a, b) => a.localeCompare(b));
const domainsWithDns = await Promise.all(
domains.map(async (domain) => {
const dkim = await getDomainDkim(domain);
return {
domain,
dkim,
spf: {
recordName: '@',
recordType: 'TXT',
recordValue: 'v=spf1 mx -all',
explanation:
'Allow mail delivery from this domain hosts (mx) and reject other senders.',
},
dmarc: {
recordName: `_dmarc.${domain}`,
recordType: 'TXT',
recordValue: `v=DMARC1; p=none; rua=mailto:postmaster@${domain}; fo=1; adkim=s; aspf=s`,
explanation:
'Start with p=none for monitoring, then tighten policy to quarantine or reject after validation.',
},
};
})
);
return domainsWithDns;
} catch (error) {
console.error('Error retrieving domains overview:', error);
debugLog('Domains overview error:', error);
throw new Error('Unable to retrieve domains overview');
}
}
/**
* Runs docker-mailserver DKIM configuration command.
* @return {Promise<{success: boolean, command: string}>}
*/
async function configureDkim() {
try {
await execSetup('config dkim');
return {
success: true,
command: 'setup config dkim',
};
} catch (error) {
console.error('Error configuring DKIM:', error);
debugLog('DKIM configuration error:', error);
throw new Error('Unable to configure DKIM');
}
}
// Function to retrieve email accounts
async function getAccounts() {
try {
debugLog('Getting email accounts list');
const stdout = await execSetup('email list');
// Parse multiline output with regex to extract email and size information
const accounts = [];
const accountLineRegex =
/\* ([\w\-\.@]+) \( ([\w\.\~]+) \/ ([\w\.\~]+) \) \[(\d+)%\](.*)$/;
// Process each line individually
const lines = stdout.split('\n').filter((line) => line.trim().length > 0);
debugLog('Raw email list response:', lines);
for (let i = 0; i < lines.length; i++) {
// Clean the line from binary control characters
const line = lines[i].replace(/[\x00-\x1F\x7F-\x9F]/g, '').trim();
// Check if line contains * which indicates an account entry
if (line.includes('*')) {
const match = line.match(accountLineRegex);
if (match) {
const email = match[1];
const usedSpace = match[2];
const totalSpace = match[3] === '~' ? 'unlimited' : match[3];
const usagePercent = match[4];
debugLog(
`Parsed account: ${email}, Storage: ${usedSpace}/${totalSpace} [${usagePercent}%]`
);
accounts.push({
email,
storage: {
used: usedSpace,
total: totalSpace,
percent: usagePercent + '%',
},
});
} else {
debugLog(`Failed to parse account line: ${line}`);
}
}
}
debugLog(`Found ${accounts.length} accounts`);
return accounts;
} catch (error) {
console.error('Error retrieving accounts:', error);
debugLog('Account retrieval error:', error);
throw new Error('Unable to retrieve account list');
}
}
// Function to add a new email account
async function addAccount(email, password) {
try {
debugLog(`Adding new email account: ${email}`);
await execSetup(
`email add ${escapeShellArg(email)} ${escapeShellArg(password)}`
);
debugLog(`Account created: ${email}`);
return { success: true, email };
} catch (error) {
console.error('Error adding account:', error);
debugLog('Account creation error:', error);
throw new Error('Unable to add email account');
}
}
// Function to update an email account password
async function updateAccountPassword(email, password) {
try {
debugLog(`Updating password for account: ${email}`);
await execSetup(
`email update ${escapeShellArg(email)} ${escapeShellArg(password)}`
);
debugLog(`Password updated for account: ${email}`);
return { success: true, email };
} catch (error) {
console.error('Error updating account password:', error);
debugLog('Account password update error:', error);
throw new Error('Unable to update email account password');
}
}
// Function to update an email account quota
async function updateAccountQuota(email, quota) {
try {
debugLog(`Updating quota for account: ${email} -> ${quota}`);
await execSetup(
`quota set ${escapeShellArg(email)} ${escapeShellArg(quota)}`
);
debugLog(`Quota updated for account: ${email}`);
return { success: true, email, quota };
} catch (error) {
console.error('Error updating account quota:', error);
debugLog('Account quota update error:', error);
throw new Error('Unable to update email account quota');
}
}
// Function to delete an email account
async function deleteAccount(email) {
try {
debugLog(`Deleting email account: ${email}`);
await execSetup(`email del ${escapeShellArg(email)}`);
debugLog(`Account deleted: ${email}`);
return { success: true, email };
} catch (error) {
console.error('Error deleting account:', error);
debugLog('Account deletion error:', error);
throw new Error('Unable to delete email account');
}
}
// Function to retrieve aliases
async function getAliases() {
try {
debugLog('Getting aliases list');
const stdout = await execSetup('alias list');
const aliases = [];
// Parse each line in the format "* source destination"
const lines = stdout.split('\n').filter((line) => line.trim().length > 0);
debugLog('Raw alias list response:', lines);
// Modified regex to be more tolerant of control characters that might appear in the output
const aliasRegex = /\* ([\w\-\.@]+) ([\w\-\.@]+)$/;
for (let i = 0; i < lines.length; i++) {
// Clean the line from binary control characters
const line = lines[i].replace(/[\x00-\x1F\x7F-\x9F]/g, '').trim();
if (line.includes('*')) {
const match = line.match(aliasRegex);
if (match) {
const source = match[1];
const destination = match[2];
debugLog(`Parsed alias: ${source} -> ${destination}`);
aliases.push({
source,
destination,
});
} else {
debugLog(`Failed to parse alias line: ${line}`);
}
}
}
debugLog(`Found ${aliases.length} aliases`);
return aliases;
} catch (error) {
console.error('Error retrieving aliases:', error);
debugLog('Alias retrieval error:', error);
throw new Error('Unable to retrieve alias list');
}
}
// Function to add an alias
async function addAlias(source, destination) {
try {
debugLog(`Adding new alias: ${source} -> ${destination}`);
await execSetup(
`alias add ${escapeShellArg(source)} ${escapeShellArg(destination)}`
);
debugLog(`Alias created: ${source} -> ${destination}`);
return { success: true, source, destination };
} catch (error) {
console.error('Error adding alias:', error);
debugLog('Alias creation error:', error);
throw new Error('Unable to add alias');
}
}
// Function to delete an alias
async function deleteAlias(source, destination) {
try {
debugLog(`Deleting alias: ${source} => ${destination}`);
await execSetup(
`alias del ${escapeShellArg(source)} ${escapeShellArg(destination)}`
);
debugLog(`Alias deleted: ${source} => ${destination}`);
return { success: true, source, destination };
} catch (error) {
console.error('Error deleting alias:', error);
debugLog('Alias deletion error:', error);
throw new Error('Unable to delete alias');
}
}
// Function to check server status
async function getServerStatus() {
try {
debugLog('Getting server status');
// Get container info
const container = docker.getContainer(DOCKER_CONTAINER);
const containerInfo = await container.inspect();
// Check if container is running
const isRunning = containerInfo.State.Running === true;
debugLog(`Container running: ${isRunning}`);
let diskUsage = '0%';
let cpuUsage = '0%';
let memoryUsage = '0MB';
if (isRunning) {
// Get container stats
debugLog('Getting container stats');
const stats = await container.stats({ stream: false });
// Calculate CPU usage percentage
const cpuDelta =
stats.cpu_stats.cpu_usage.total_usage -
stats.precpu_stats.cpu_usage.total_usage;
const systemCpuDelta =
stats.cpu_stats.system_cpu_usage - stats.precpu_stats.system_cpu_usage;
const cpuPercent =
(cpuDelta / systemCpuDelta) * stats.cpu_stats.online_cpus * 100;
cpuUsage = `${cpuPercent.toFixed(2)}%`;
// Calculate memory usage
const memoryUsageBytes = stats.memory_stats.usage;
memoryUsage = formatMemorySize(memoryUsageBytes);
debugLog(`Resources - CPU: ${cpuUsage}, Memory: ${memoryUsage}`);
// For disk usage, we would need to run a command inside the container
// This could be a more complex operation involving checking specific directories
// For simplicity, we'll set this to "N/A" or implement a basic check
diskUsage = 'N/A';
}
const result = {
status: isRunning ? 'running' : 'stopped',
resources: {
cpu: cpuUsage,
memory: memoryUsage,
disk: diskUsage,
},
};
debugLog('Server status result:', result);
return result;
} catch (error) {
console.error('Error checking server status:', error);
debugLog('Server status error:', error);
return {
status: 'unknown',
error: error.message,
};
}
}
// Helper function to format memory size
function formatMemorySize(bytes) {
if (bytes === 0) return '0B';
const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
const i = Math.floor(Math.log(bytes) / Math.log(1024));
return parseFloat((bytes / Math.pow(1024, i)).toFixed(2)) + sizes[i];
}
module.exports = {
getAccounts,
addAccount,
updateAccountPassword,
updateAccountQuota,
deleteAccount,
getAliases,
addAlias,
deleteAlias,
getDomainsOverview,
configureDkim,
getServerStatus,
};