-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
137 lines (121 loc) · 3.62 KB
/
index.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
'use strict';
exports.register = function () {
const plugin = this;
try {
plugin.imap = require('imap');
}
catch (ignore) {}
if (!plugin.imap) {
plugin.logerror('imap library not found, try \'npm -g install imap\' or \'npm install imap\' in your configuration directory to install it');
return;
}
plugin.inherits('auth/auth_base');
plugin.load_imap_ini();
};
exports.load_imap_ini = function () {
const plugin = this;
plugin.cfg = plugin.config.get('auth_imap.ini', {
booleans: [
'-main.tls',
'-main.rejectUnauthorized'
],
},
function () {
plugin.load_imap_ini();
});
};
exports.hook_capabilities = function (next, connection) {
// Don't offer AUTH capabilities by default unless session is encrypted
if (connection.tls.enabled) {
const methods = ['PLAIN', 'LOGIN'];
connection.capabilities.push(`AUTH ${ methods.join(' ')}`);
connection.notes.allowed_auth_methods = methods;
}
next();
};
const ca_cache = {}
exports.check_plain_passwd = function (connection, user, passwd, cb) {
const plugin = this;
let trace_imap = false;
const domain = (user.split('@'))[1];
let sect = plugin.cfg.main;
let section_name = 'main';
if (domain && plugin.cfg[domain]) {
sect = plugin.cfg[domain];
section_name = domain;
}
const config = {
user,
password: passwd,
host: 'localhost',
port: 143,
tls: sect.tls,
tlsOptions: {
rejectUnauthorized: sect.rejectUnauthorized
}
};
if (sect.trace_imap == 'true') {
trace_imap = true;
config.debug = function (info) {
connection.logdebug(plugin, info);
}
}
if (sect.host) {
config.host = sect.host;
}
if (sect.port) {
config.port = sect.port;
}
if (sect.ca) {
if (!ca_cache[section_name]) {
ca_cache[section_name] = require('fs').readFileSync(sect.ca);
}
config.tlsOptions.ca = [ca_cache[section_name]];
}
if (sect.connTimeout) {
config.connTimeout = parseInt(sect.connTimeout, 10);
}
if (sect.authTimeout) {
config.authTimeout = parseInt(sect.authTimeout, 10);
}
if (sect.users) {
if (sect.users.split(/\s*,\s*/).indexOf((user.split('@'))[0]) < 0) {
connection.loginfo(plugin, `AUTH user="${ user
}" is not allowed to authenticate by imap`
);
return cb(false);
}
}
const client = new plugin.imap(config);
let message = `section="${ section_name }" host="${
config.host }" port="${ config.port }" tls=${ config.tls}`;
if (config.tlsOptions) {
message += ` rejectUnauthorized=${ config.tlsOptions
.rejectUnauthorized}`;
}
if (config.connTimeout) {
message += ` connTimeout=${ config.connTimeout}`;
}
if (config.authTimeout) {
message += ` authTimeout=${ config.authTimeout}`;
}
connection.logdebug(plugin, message);
client.once('ready', function () {
connection.loginfo(plugin, `AUTH user="${ user
}" success=true`);
if (trace_imap) {
connection.logdebug(plugin, client);
}
client.end();
return cb(true);
});
client.once('error', function (err) {
connection.loginfo(plugin, `AUTH user="${ user
}" success=false error="${ err.message }"`);
if (trace_imap) {
connection.logdebug(plugin, client);
}
return cb(false);
});
client.connect();
};