|
| 1 | +// relay |
| 2 | +// |
| 3 | +// documentation via: haraka -h relay |
| 4 | + |
| 5 | +var ipaddr = require('ipaddr.js'), |
| 6 | + net = require('net'); |
| 7 | + |
| 8 | +exports.register = function() { |
| 9 | + var plugin = this; |
| 10 | + plugin.refresh_config(); |
| 11 | + if (plugin.cfg.relay.acl ) { plugin.register_hook('connect', 'acl' ); } |
| 12 | + if (plugin.cfg.relay.dest_domains ) { plugin.register_hook('rcpt', 'dest_domains'); } |
| 13 | + if (plugin.cfg.relay.all ) { plugin.register_hook('rcpt', 'all' ); } |
| 14 | + if (plugin.cfg.relay.force_routing) { plugin.register_hook('get_mx', 'force_routing'); } |
| 15 | +}; |
| 16 | + |
| 17 | +exports.refresh_config = function() { |
| 18 | + var plugin = this; |
| 19 | + |
| 20 | + var load_relay_ini = function () { |
| 21 | + plugin.cfg = plugin.config.get('relay.ini', { |
| 22 | + booleans: [ |
| 23 | + '+relay.acl', |
| 24 | + '+relay.force_routing', |
| 25 | + '-relay.all', |
| 26 | + '-relay.dest_domains', |
| 27 | + ], |
| 28 | + }, function () { |
| 29 | + load_relay_ini(); |
| 30 | + }); |
| 31 | + }; |
| 32 | + |
| 33 | + var load_dest_domains = function () { |
| 34 | + plugin.loginfo(plugin, "loading relay_dest_domain.ini"); |
| 35 | + plugin.dest = plugin.config.get('relay_dest_domains.ini', 'ini', function() { |
| 36 | + load_dest_domains(); |
| 37 | + }); |
| 38 | + }; |
| 39 | + |
| 40 | + var load_acls = function () { |
| 41 | + var file_name = 'relay_acl_allow'; |
| 42 | + plugin.loginfo(plugin, "loading " + file_name); |
| 43 | + |
| 44 | + // load with a self-referential callback |
| 45 | + plugin.acl_allow = plugin.config.get(file_name, 'list', function () { |
| 46 | + load_acls(); |
| 47 | + }); |
| 48 | + |
| 49 | + for (var i=0; i<plugin.acl_allow.length; i++) { |
| 50 | + var cidr = plugin.acl_allow[i].split('/'); |
| 51 | + if (!net.isIP(cidr[0])) { |
| 52 | + plugin.logerror(plugin, "invalid entry in " + file_name + ": " + cidr[0]); |
| 53 | + } |
| 54 | + if (!cidr[1]) { |
| 55 | + plugin.logerror(plugin, "appending missing CIDR suffix in: " + file_name); |
| 56 | + plugin.acl_allow[i] = cidr[0] + '/32'; |
| 57 | + } |
| 58 | + } |
| 59 | + }; |
| 60 | + |
| 61 | + load_relay_ini(); // plugin.cfg = { } |
| 62 | + |
| 63 | + if (plugin.cfg.relay.acl) { |
| 64 | + load_acls(); // plugin.acl_allow = [..] |
| 65 | + } |
| 66 | + |
| 67 | + if (plugin.cfg.relay.force_routing || plugin.cfg.relay.dest_domains) { |
| 68 | + load_dest_domains(); // plugin.dest.domains = { } |
| 69 | + } |
| 70 | +}; |
| 71 | + |
| 72 | +exports.acl = function (next, connection) { |
| 73 | + var plugin = this; |
| 74 | + if (!plugin.cfg.relay.acl) { return next(); } |
| 75 | + |
| 76 | + connection.logdebug(this, 'checking ' + connection.remote_ip + ' in relay_acl_allow'); |
| 77 | + |
| 78 | + if (!plugin.is_acl_allowed(connection)) { |
| 79 | + connection.results.add(plugin, {skip: 'acl(unlisted)'}); |
| 80 | + return next(); |
| 81 | + } |
| 82 | + |
| 83 | + connection.results.add(plugin, {pass: 'acl'}); |
| 84 | + connection.relaying = true; |
| 85 | + return next(OK); |
| 86 | +}; |
| 87 | + |
| 88 | +exports.is_acl_allowed = function (connection) { |
| 89 | + var plugin = this; |
| 90 | + if (!plugin.acl_allow) { return false; } |
| 91 | + if (!plugin.acl_allow.length) { return false; } |
| 92 | + |
| 93 | + var ip = connection.remote_ip; |
| 94 | + |
| 95 | + for (var i=0; i < plugin.acl_allow.length; i++) { |
| 96 | + var item = plugin.acl_allow[i]; |
| 97 | + connection.logdebug(plugin, 'checking if ' + ip + ' is in ' + item); |
| 98 | + var cidr = plugin.acl_allow[i].split('/'); |
| 99 | + var c_net = cidr[0]; |
| 100 | + var c_mask = cidr[1] || 32; |
| 101 | + |
| 102 | + if (!net.isIP(c_net)) continue; // bad config entry |
| 103 | + if (net.isIPv4(ip) && net.isIPv6(c_net)) continue; |
| 104 | + if (net.isIPv6(ip) && net.isIPv4(c_net)) continue; |
| 105 | + |
| 106 | + if (ipaddr.parse(ip).match(ipaddr.parse(c_net), c_mask)) { |
| 107 | + connection.logdebug(plugin, 'checking if ' + ip + ' is in ' + item + ": yes"); |
| 108 | + return true; |
| 109 | + } |
| 110 | + } |
| 111 | + return false; |
| 112 | +}; |
| 113 | + |
| 114 | +exports.dest_domains = function (next, connection, params) { |
| 115 | + var plugin = this; |
| 116 | + if (!plugin.cfg.relay.dest_domains) { return next(); } |
| 117 | + var transaction = connection.transaction; |
| 118 | + |
| 119 | + // Skip this if the host is already allowed to relay |
| 120 | + if (connection.relaying) { |
| 121 | + transaction.results.add(plugin, {skip: 'relay_dest_domain(relay)'}); |
| 122 | + return next(); |
| 123 | + } |
| 124 | + |
| 125 | + if (!plugin.dest) { |
| 126 | + transaction.results.add(plugin, {err: 'relay_dest_domain(no config!)'}); |
| 127 | + return next(); |
| 128 | + } |
| 129 | + |
| 130 | + if (!plugin.dest.domains) { |
| 131 | + transaction.results.add(plugin, {skip: 'relay_dest_domain(config)'}); |
| 132 | + return next(); |
| 133 | + } |
| 134 | + |
| 135 | + var dest_domain = params[0].host; |
| 136 | + connection.logdebug(plugin, 'dest_domain = ' + dest_domain); |
| 137 | + |
| 138 | + var dst_cfg = plugin.dest.domains[dest_domain]; |
| 139 | + if (!dst_cfg) { |
| 140 | + transaction.results.add(plugin, {fail: 'relay_dest_domain'}); |
| 141 | + return next(DENY, "You are not allowed to relay"); |
| 142 | + } |
| 143 | + |
| 144 | + var action = JSON.parse(dst_cfg).action; |
| 145 | + connection.logdebug(plugin, 'found config for ' + dest_domain + ': ' + action); |
| 146 | + |
| 147 | + switch(action) { |
| 148 | + case "accept": |
| 149 | + // why enable relaying here? Returning next(OK) will allow the |
| 150 | + // address to be considered 'local'. What advantage does relaying |
| 151 | + // bring? |
| 152 | + connection.relaying = true; |
| 153 | + transaction.results.add(plugin, {pass: 'relay_dest_domain'}); |
| 154 | + return next(OK); |
| 155 | + case "continue": |
| 156 | + // why oh why? Only reason I can think of is to enable outbound. |
| 157 | + connection.relaying = true; |
| 158 | + transaction.results.add(plugin, {pass: 'relay_dest_domain'}); |
| 159 | + return next(CONT); // same as next() |
| 160 | + case "deny": |
| 161 | + transaction.results.add(plugin, {fail: 'relay_dest_domain'}); |
| 162 | + return next(DENY, "You are not allowed to relay"); |
| 163 | + } |
| 164 | + |
| 165 | + transaction.results.add(plugin, {fail: 'relay_dest_domain'}); |
| 166 | + return next(DENY, "Mail for that recipient is not accepted here."); |
| 167 | +}; |
| 168 | + |
| 169 | +exports.force_routing = function (next, hmail, domain) { |
| 170 | + var plugin = this; |
| 171 | + if (!plugin.cfg.relay.force_routing) { return next(); } |
| 172 | + if (!plugin.dest) { return next(); } |
| 173 | + if (!plugin.dest.domains) { return next(); } |
| 174 | + var route = plugin.dest.domains[domain]; |
| 175 | + |
| 176 | + if (!route) { |
| 177 | + plugin.logdebug(plugin, 'using normal MX lookup for: ' + domain); |
| 178 | + return next(); |
| 179 | + } |
| 180 | + |
| 181 | + var c = JSON.parse(route); |
| 182 | + var nexthop = JSON.parse(route).nexthop; |
| 183 | + if (!nexthop) { |
| 184 | + plugin.logdebug(plugin, 'using normal MX lookup for: ' + domain); |
| 185 | + return next(); |
| 186 | + } |
| 187 | + |
| 188 | + plugin.logdebug(plugin, 'using ' + nexthop + ' for: ' + domain); |
| 189 | + return next(OK, nexthop); |
| 190 | +}; |
| 191 | + |
| 192 | +exports.all = function(next, connection, params) { |
| 193 | +// relay everything - could be useful for a spamtrap |
| 194 | + var plugin = this; |
| 195 | + if (!plugin.cfg.relay.all) { return next(); } |
| 196 | +// TODO: This looks like a bug (shortening the recipient array) |
| 197 | + var recipient = params.shift(); |
| 198 | + connection.loginfo(plugin, "confirming recipient " + recipient); |
| 199 | + connection.relaying = true; |
| 200 | + next(OK); |
| 201 | +}; |
| 202 | + |
0 commit comments