Files
cloudron-box/src/mailboxdb.js

409 lines
17 KiB
JavaScript
Raw Normal View History

2016-05-26 22:34:04 -07:00
'use strict';
exports = module.exports = {
2020-07-15 15:33:53 -07:00
addMailbox,
addList,
updateMailbox,
2020-07-15 15:33:53 -07:00
updateList,
del,
2016-09-25 23:21:55 -07:00
2020-07-15 15:33:53 -07:00
getMailboxCount,
listMailboxes,
getLists,
2016-09-25 23:21:55 -07:00
2020-07-15 15:33:53 -07:00
listAllMailboxes,
2020-07-15 15:33:53 -07:00
get,
getMailbox,
getList,
getAlias,
2016-09-25 23:21:55 -07:00
2020-07-15 15:33:53 -07:00
getAliasesForName,
setAliasesForName,
2016-09-25 23:21:55 -07:00
2020-07-15 15:33:53 -07:00
getByOwnerId,
delByOwnerId,
delByDomain,
2016-05-26 22:34:04 -07:00
2020-07-15 15:33:53 -07:00
updateName,
2016-12-15 16:13:16 +01:00
_clear: clear,
2018-04-07 19:12:07 -07:00
TYPE_MAILBOX: 'mailbox',
TYPE_LIST: 'list',
rework how app mailboxes are allocated Our current setup had a mailbox allocated for an app during app install (into the mailboxes table). This has many issues: * When set to a custom mailbox location, there was no way to access this mailbox even via IMAP. Even when using app credentials, we cannot use IMAP since the ldap logic was testing on the addon type (most of our apps only use sendmail addon and thus cannot recvmail). * The mailboxes table was being used to add hidden 'app' type entries. This made it very hard for the user to understand why a mailbox conflicts. For example, if you set an app to use custom mailbox 'blog', this is hidden from all views. The solution is to let an app send email as whatever mailbox name is allocated to it (which we now track in the apps table. the default is in the db already so that REST response contains it). When not using Cloudron email, it will just send mail as that mailbox and the auth checks the "app password" in the addons table. Any replies to that mailbox will end up in the domain's mail server (not our problem). When using cloudron email, the app can send mail like above. Any responses will not end anywhere and bounce since there is no 'mailbox'. This is the expected behavior. If user wants to access this mailbox name, he can create a concrete mailbox and set himself as owner OR set this as an alias. For apps using the recvmail addon, the workflow is to actually create a mailbox at some point. Currently, we have no UI for this 'flow'. It's fine because we have only meemo using it. Intuitive much!
2018-12-06 21:08:19 -08:00
TYPE_ALIAS: 'alias'
2016-05-26 22:34:04 -07:00
};
var assert = require('assert'),
2019-10-24 13:34:14 -07:00
BoxError = require('./boxerror.js'),
2016-05-26 22:34:04 -07:00
database = require('./database.js'),
mysql = require('mysql'),
safe = require('safetydance'),
util = require('util');
2016-05-26 22:34:04 -07:00
var MAILBOX_FIELDS = [ 'name', 'type', 'ownerId', 'ownerType', 'aliasName', 'aliasDomain', 'creationTime', 'membersJson', 'membersOnly', 'domain', 'active' ].join(',');
function postProcess(data) {
data.members = safe.JSON.parse(data.membersJson) || [ ];
delete data.membersJson;
2020-04-17 16:55:23 -07:00
data.membersOnly = !!data.membersOnly;
data.active = !!data.active;
2020-04-17 16:55:23 -07:00
return data;
}
2016-05-26 22:34:04 -07:00
function postProcessAliases(data) {
const aliasNames = JSON.parse(data.aliasNames), aliasDomains = JSON.parse(data.aliasDomains);
delete data.aliasNames;
delete data.aliasDomains;
data.aliases = [];
for (let i = 0; i < aliasNames.length; i++) { // NOTE: aliasNames is [ null ] when no aliases
if (aliasNames[i]) data.aliases[i] = { name: aliasNames[i], domain: aliasDomains[i] };
}
}
function addMailbox(name, domain, data, callback) {
assert.strictEqual(typeof name, 'string');
assert.strictEqual(typeof domain, 'string');
assert.strictEqual(typeof data, 'object');
assert.strictEqual(typeof callback, 'function');
const { ownerId, ownerType, active } = data;
assert.strictEqual(typeof ownerId, 'string');
2020-11-12 23:25:33 -08:00
assert.strictEqual(typeof ownerType, 'string');
assert.strictEqual(typeof active, 'boolean');
database.query('INSERT INTO mailboxes (name, type, domain, ownerId, ownerType, active) VALUES (?, ?, ?, ?, ?, ?)', [ name, exports.TYPE_MAILBOX, domain, ownerId, ownerType, active ], function (error) {
2019-10-24 13:34:14 -07:00
if (error && error.code === 'ER_DUP_ENTRY') return callback(new BoxError(BoxError.ALREADY_EXISTS, 'mailbox already exists'));
if (error) return callback(new BoxError(BoxError.DATABASE_ERROR, error));
callback(null);
});
}
function updateMailbox(name, domain, data, callback) {
assert.strictEqual(typeof name, 'string');
assert.strictEqual(typeof domain, 'string');
assert.strictEqual(typeof data, 'object');
assert.strictEqual(typeof callback, 'function');
const { ownerId, ownerType, active } = data;
assert.strictEqual(typeof ownerId, 'string');
2020-11-12 23:25:33 -08:00
assert.strictEqual(typeof ownerType, 'string');
assert.strictEqual(typeof active, 'boolean');
database.query('UPDATE mailboxes SET ownerId = ?, ownerType = ?, active = ? WHERE name = ? AND domain = ?', [ ownerId, ownerType, active, name, domain ], function (error, result) {
2019-10-24 13:34:14 -07:00
if (error) return callback(new BoxError(BoxError.DATABASE_ERROR, error));
2019-10-24 20:48:38 -07:00
if (result.affectedRows === 0) return callback(new BoxError(BoxError.NOT_FOUND, 'Mailbox not found'));
callback(null);
});
}
function addList(name, domain, data, callback) {
2016-05-26 22:34:04 -07:00
assert.strictEqual(typeof name, 'string');
2017-11-09 23:45:29 +01:00
assert.strictEqual(typeof domain, 'string');
assert.strictEqual(typeof data, 'object');
assert.strictEqual(typeof callback, 'function');
const { members, membersOnly, active } = data;
assert(Array.isArray(members));
2020-04-17 16:55:23 -07:00
assert.strictEqual(typeof membersOnly, 'boolean');
assert.strictEqual(typeof active, 'boolean');
2016-05-26 22:34:04 -07:00
database.query('INSERT INTO mailboxes (name, type, domain, ownerId, ownerType, membersJson, membersOnly, active) VALUES (?, ?, ?, ?, ?, ?, ?, ?)',
[ name, exports.TYPE_LIST, domain, 'admin', 'user', JSON.stringify(members), membersOnly, active ], function (error) {
2019-10-24 13:34:14 -07:00
if (error && error.code === 'ER_DUP_ENTRY') return callback(new BoxError(BoxError.ALREADY_EXISTS, 'mailbox already exists'));
if (error) return callback(new BoxError(BoxError.DATABASE_ERROR, error));
callback(null);
});
}
function updateList(name, domain, data, callback) {
assert.strictEqual(typeof name, 'string');
assert.strictEqual(typeof domain, 'string');
assert.strictEqual(typeof data, 'object');
assert.strictEqual(typeof callback, 'function');
const { members, membersOnly, active } = data;
assert(Array.isArray(members));
2020-04-17 16:55:23 -07:00
assert.strictEqual(typeof membersOnly, 'boolean');
assert.strictEqual(typeof active, 'boolean');
database.query('UPDATE mailboxes SET membersJson = ?, membersOnly = ?, active = ? WHERE name = ? AND domain = ?',
[ JSON.stringify(members), membersOnly, active, name, domain ], function (error, result) {
2019-10-24 13:34:14 -07:00
if (error) return callback(new BoxError(BoxError.DATABASE_ERROR, error));
2019-10-24 20:48:38 -07:00
if (result.affectedRows === 0) return callback(new BoxError(BoxError.NOT_FOUND, 'Mailbox not found'));
callback(null);
});
}
2016-05-26 22:34:04 -07:00
function clear(callback) {
assert.strictEqual(typeof callback, 'function');
database.query('TRUNCATE TABLE mailboxes', [], function (error) {
2019-10-24 13:34:14 -07:00
if (error) return callback(new BoxError(BoxError.DATABASE_ERROR, error));
2016-05-26 22:34:04 -07:00
callback(null);
});
}
2017-11-09 23:45:29 +01:00
function del(name, domain, callback) {
assert.strictEqual(typeof name, 'string');
2017-11-09 23:45:29 +01:00
assert.strictEqual(typeof domain, 'string');
2016-05-26 22:34:04 -07:00
assert.strictEqual(typeof callback, 'function');
// deletes aliases as well
database.query('DELETE FROM mailboxes WHERE ((name=? AND domain=?) OR (aliasName = ? AND aliasDomain=?))', [ name, domain, name, domain ], function (error, result) {
2019-10-24 13:34:14 -07:00
if (error) return callback(new BoxError(BoxError.DATABASE_ERROR, error));
2019-10-24 20:48:38 -07:00
if (result.affectedRows === 0) return callback(new BoxError(BoxError.NOT_FOUND, 'Mailbox not found'));
2016-05-26 22:34:04 -07:00
callback(null);
});
}
2018-02-11 01:18:29 -08:00
function delByDomain(domain, callback) {
assert.strictEqual(typeof domain, 'string');
assert.strictEqual(typeof callback, 'function');
database.query('DELETE FROM mailboxes WHERE domain = ?', [ domain ], function (error) {
2019-10-24 13:34:14 -07:00
if (error) return callback(new BoxError(BoxError.DATABASE_ERROR, error));
2018-02-11 01:18:29 -08:00
callback(null);
});
}
function delByOwnerId(id, callback) {
assert.strictEqual(typeof id, 'string');
assert.strictEqual(typeof callback, 'function');
2016-09-25 23:21:55 -07:00
database.query('DELETE FROM mailboxes WHERE ownerId=?', [ id ], function (error) {
2019-10-24 13:34:14 -07:00
if (error) return callback(new BoxError(BoxError.DATABASE_ERROR, error));
callback(null);
});
}
2017-11-09 23:45:29 +01:00
function updateName(oldName, oldDomain, newName, newDomain, callback) {
2016-12-15 16:13:16 +01:00
assert.strictEqual(typeof oldName, 'string');
2017-11-09 23:45:29 +01:00
assert.strictEqual(typeof oldDomain, 'string');
2016-12-15 16:13:16 +01:00
assert.strictEqual(typeof newName, 'string');
2017-11-09 23:45:29 +01:00
assert.strictEqual(typeof newDomain, 'string');
2016-12-15 16:13:16 +01:00
assert.strictEqual(typeof callback, 'function');
// skip if no changes
2017-11-09 23:45:29 +01:00
if (oldName === newName && oldDomain === newDomain) return callback(null);
2017-11-09 23:45:29 +01:00
database.query('UPDATE mailboxes SET name=?, domain=? WHERE name=? AND domain = ?', [ newName, newDomain, oldName, oldDomain ], function (error, result) {
2019-10-24 13:34:14 -07:00
if (error && error.code === 'ER_DUP_ENTRY') return callback(new BoxError(BoxError.ALREADY_EXISTS, 'mailbox already exists'));
if (error) return callback(new BoxError(BoxError.DATABASE_ERROR, error));
2019-10-24 20:48:38 -07:00
if (result.affectedRows !== 1) return callback(new BoxError(BoxError.NOT_FOUND, 'Mailbox not found'));
2016-12-15 16:13:16 +01:00
callback(null);
});
}
2019-11-06 16:45:44 -08:00
function get(name, domain, callback) {
assert.strictEqual(typeof name, 'string');
assert.strictEqual(typeof domain, 'string');
assert.strictEqual(typeof callback, 'function');
database.query('SELECT ' + MAILBOX_FIELDS + ' FROM mailboxes WHERE name = ? AND domain = ?',
[ name, domain ], function (error, results) {
if (error) return callback(new BoxError(BoxError.DATABASE_ERROR, error));
if (results.length === 0) return callback(new BoxError(BoxError.NOT_FOUND, 'Mailbox not found'));
callback(null, postProcess(results[0]));
});
}
2017-11-09 23:45:29 +01:00
function getMailbox(name, domain, callback) {
2016-09-25 18:59:11 -07:00
assert.strictEqual(typeof name, 'string');
2017-11-09 23:45:29 +01:00
assert.strictEqual(typeof domain, 'string');
2016-09-25 18:59:11 -07:00
assert.strictEqual(typeof callback, 'function');
2018-04-07 19:12:07 -07:00
database.query('SELECT ' + MAILBOX_FIELDS + ' FROM mailboxes WHERE name = ? AND type = ? AND domain = ?',
[ name, exports.TYPE_MAILBOX, domain ], function (error, results) {
2019-10-24 13:34:14 -07:00
if (error) return callback(new BoxError(BoxError.DATABASE_ERROR, error));
2019-10-24 20:48:38 -07:00
if (results.length === 0) return callback(new BoxError(BoxError.NOT_FOUND, 'Mailbox not found'));
2016-09-25 18:59:11 -07:00
2018-04-07 19:12:07 -07:00
callback(null, postProcess(results[0]));
});
2016-09-25 18:59:11 -07:00
}
2020-07-15 15:33:53 -07:00
function getMailboxCount(domain, callback) {
assert.strictEqual(typeof domain, 'string');
assert.strictEqual(typeof callback, 'function');
database.query('SELECT COUNT(*) AS total FROM mailboxes WHERE type = ? AND domain = ?', [ exports.TYPE_MAILBOX, domain ], function (error, results) {
if (error) return callback(new BoxError(BoxError.DATABASE_ERROR, error));
callback(null, results[0].total);
});
}
function listMailboxes(domain, search, page, perPage, callback) {
2017-11-09 23:45:29 +01:00
assert.strictEqual(typeof domain, 'string');
assert(typeof search === 'string' || search === null);
2019-10-22 10:11:35 -07:00
assert.strictEqual(typeof page, 'number');
assert.strictEqual(typeof perPage, 'number');
2016-09-25 18:59:11 -07:00
assert.strictEqual(typeof callback, 'function');
const escapedSearch = mysql.escape('%' + search + '%'); // this also quotes the string
const searchQuery = search ? ` HAVING (name LIKE ${escapedSearch} OR aliasNames LIKE ${escapedSearch} OR aliasDomains LIKE ${escapedSearch})` : ''; // having instead of where because of aggregated columns use
const query = 'SELECT m1.name AS name, m1.domain AS domain, m1.ownerId AS ownerId, m1.ownerType as ownerType, m1.active as active, JSON_ARRAYAGG(m2.name) AS aliasNames, JSON_ARRAYAGG(m2.domain) AS aliasDomains '
+ ` FROM (SELECT * FROM mailboxes WHERE type='${exports.TYPE_MAILBOX}') AS m1`
+ ` LEFT JOIN (SELECT * FROM mailboxes WHERE type='${exports.TYPE_ALIAS}') AS m2`
+ ' ON m1.name=m2.aliasName AND m1.domain=m2.aliasDomain AND m1.ownerId=m2.ownerId'
+ ' WHERE m1.domain = ?'
+ ' GROUP BY m1.name, m1.domain, m1.ownerId'
+ searchQuery
+ ' ORDER BY name LIMIT ?,?';
2016-09-25 18:59:11 -07:00
database.query(query, [ domain, (page-1)*perPage, perPage ], function (error, results) {
if (error) return callback(new BoxError(BoxError.DATABASE_ERROR, error));
results.forEach(postProcess);
results.forEach(postProcessAliases);
callback(null, results);
});
2016-09-25 18:59:11 -07:00
}
function listAllMailboxes(page, perPage, callback) {
assert.strictEqual(typeof page, 'number');
assert.strictEqual(typeof perPage, 'number');
assert.strictEqual(typeof callback, 'function');
const query = 'SELECT m1.name AS name, m1.domain AS domain, m1.ownerId AS ownerId, m1.ownerType as ownerType, m1.active as active, JSON_ARRAYAGG(m2.name) AS aliasNames, JSON_ARRAYAGG(m2.domain) AS aliasDomains '
+ ` FROM (SELECT * FROM mailboxes WHERE type='${exports.TYPE_MAILBOX}') AS m1`
+ ` LEFT JOIN (SELECT * FROM mailboxes WHERE type='${exports.TYPE_ALIAS}') AS m2`
+ ' ON m1.name=m2.aliasName AND m1.domain=m2.aliasDomain AND m1.ownerId=m2.ownerId'
+ ' GROUP BY m1.name, m1.domain, m1.ownerId'
+ ' ORDER BY name LIMIT ?,?';
database.query(query, [ (page-1)*perPage, perPage ], function (error, results) {
if (error) return callback(new BoxError(BoxError.DATABASE_ERROR, error));
results.forEach(postProcess);
results.forEach(postProcessAliases);
callback(null, results);
});
}
function getLists(domain, search, page, perPage, callback) {
2018-01-26 10:22:50 +01:00
assert.strictEqual(typeof domain, 'string');
assert(typeof search === 'string' || search === null);
assert.strictEqual(typeof page, 'number');
assert.strictEqual(typeof perPage, 'number');
2018-01-26 10:22:50 +01:00
assert.strictEqual(typeof callback, 'function');
let query = `SELECT ${MAILBOX_FIELDS} FROM mailboxes WHERE type = ? AND domain = ?`;
2020-07-05 11:44:46 -07:00
if (search) query += ' AND (name LIKE ' + mysql.escape('%' + search + '%') + ' OR membersJson LIKE ' + mysql.escape('%' + search + '%') + ')';
query += 'ORDER BY name LIMIT ?,?';
2018-01-26 10:22:50 +01:00
database.query(query, [ exports.TYPE_LIST, domain, (page-1)*perPage, perPage ], function (error, results) {
if (error) return callback(new BoxError(BoxError.DATABASE_ERROR, error));
results.forEach(function (result) { postProcess(result); });
callback(null, results);
});
2018-01-26 10:22:50 +01:00
}
2019-08-23 15:09:06 -07:00
function getList(name, domain, callback) {
2016-09-25 18:59:11 -07:00
assert.strictEqual(typeof name, 'string');
2017-11-09 23:45:29 +01:00
assert.strictEqual(typeof domain, 'string');
2016-09-25 18:59:11 -07:00
assert.strictEqual(typeof callback, 'function');
2018-04-07 19:12:07 -07:00
database.query('SELECT ' + MAILBOX_FIELDS + ' FROM mailboxes WHERE type = ? AND name = ? AND domain = ?',
[ exports.TYPE_LIST, name, domain ], function (error, results) {
2019-10-24 13:34:14 -07:00
if (error) return callback(new BoxError(BoxError.DATABASE_ERROR, error));
2019-10-24 20:48:38 -07:00
if (results.length === 0) return callback(new BoxError(BoxError.NOT_FOUND, 'Mailbox not found'));
2016-09-25 18:59:11 -07:00
2018-04-07 19:12:07 -07:00
callback(null, postProcess(results[0]));
});
2016-09-25 18:59:11 -07:00
}
2016-09-23 17:35:48 -07:00
function getByOwnerId(ownerId, callback) {
assert.strictEqual(typeof ownerId, 'string');
assert.strictEqual(typeof callback, 'function');
2016-09-25 23:21:55 -07:00
database.query('SELECT ' + MAILBOX_FIELDS + ' FROM mailboxes WHERE ownerId = ? ORDER BY name', [ ownerId ], function (error, results) {
2019-10-24 13:34:14 -07:00
if (error) return callback(new BoxError(BoxError.DATABASE_ERROR, error));
2019-10-24 20:48:38 -07:00
if (results.length === 0) return callback(new BoxError(BoxError.NOT_FOUND, 'Mailbox not found'));
2016-09-23 17:35:48 -07:00
results.forEach(function (result) { postProcess(result); });
2016-09-25 23:21:55 -07:00
callback(null, results);
2016-09-23 17:35:48 -07:00
});
}
2017-11-09 23:45:29 +01:00
function setAliasesForName(name, domain, aliases, callback) {
assert.strictEqual(typeof name, 'string');
2017-11-09 23:45:29 +01:00
assert.strictEqual(typeof domain, 'string');
2021-05-02 11:26:08 -07:00
assert(Array.isArray(aliases));
assert.strictEqual(typeof callback, 'function');
2017-11-09 23:45:29 +01:00
database.query('SELECT ' + MAILBOX_FIELDS + ' FROM mailboxes WHERE name = ? AND domain = ?', [ name, domain ], function (error, results) {
2019-10-24 13:34:14 -07:00
if (error) return callback(new BoxError(BoxError.DATABASE_ERROR, error));
2019-10-24 20:48:38 -07:00
if (results.length === 0) return callback(new BoxError(BoxError.NOT_FOUND, 'Mailbox not found'));
2016-09-25 23:21:55 -07:00
var queries = [];
2018-04-07 19:12:07 -07:00
// clear existing aliases
queries.push({ query: 'DELETE FROM mailboxes WHERE aliasName = ? AND aliasDomain = ? AND type = ?', args: [ name, domain, exports.TYPE_ALIAS ] });
2016-09-25 23:21:55 -07:00
aliases.forEach(function (alias) {
2020-11-12 23:25:33 -08:00
queries.push({ query: 'INSERT INTO mailboxes (name, domain, type, aliasName, aliasDomain, ownerId, ownerType) VALUES (?, ?, ?, ?, ?, ?, ?)',
args: [ alias.name, alias.domain, exports.TYPE_ALIAS, name, domain, results[0].ownerId, results[0].ownerType ] });
2016-09-25 23:21:55 -07:00
});
database.transaction(queries, function (error) {
2019-10-24 20:48:38 -07:00
if (error && error.code === 'ER_DUP_ENTRY' && error.message.indexOf('mailboxes_name_domain_unique_index') !== -1) {
var aliasMatch = error.message.match(new RegExp(`^ER_DUP_ENTRY: Duplicate entry '(.*)-${domain}' for key 'mailboxes_name_domain_unique_index'$`));
if (!aliasMatch) return callback(new BoxError(BoxError.ALREADY_EXISTS, error));
return callback(new BoxError(BoxError.ALREADY_EXISTS, `Mailbox, mailinglist or alias for ${aliasMatch[1]} already exists`));
}
2019-10-24 13:34:14 -07:00
if (error) return callback(new BoxError(BoxError.DATABASE_ERROR, error));
2016-09-25 23:21:55 -07:00
callback(null);
});
});
}
2017-11-09 23:45:29 +01:00
function getAliasesForName(name, domain, callback) {
assert.strictEqual(typeof name, 'string');
2017-11-09 23:45:29 +01:00
assert.strictEqual(typeof domain, 'string');
assert.strictEqual(typeof callback, 'function');
database.query('SELECT name, domain FROM mailboxes WHERE type = ? AND aliasName = ? AND aliasDomain = ? ORDER BY name',
2018-04-07 19:12:07 -07:00
[ exports.TYPE_ALIAS, name, domain ], function (error, results) {
2019-10-24 13:34:14 -07:00
if (error) return callback(new BoxError(BoxError.DATABASE_ERROR, error));
2016-05-26 22:34:04 -07:00
2018-04-07 19:12:07 -07:00
callback(null, results);
});
2016-05-26 22:34:04 -07:00
}
2016-09-25 18:59:11 -07:00
2017-11-09 23:45:29 +01:00
function getAlias(name, domain, callback) {
2016-09-25 18:59:11 -07:00
assert.strictEqual(typeof name, 'string');
2017-11-09 23:45:29 +01:00
assert.strictEqual(typeof domain, 'string');
2016-09-25 18:59:11 -07:00
assert.strictEqual(typeof callback, 'function');
2018-04-07 19:12:07 -07:00
database.query('SELECT ' + MAILBOX_FIELDS + ' FROM mailboxes WHERE name = ? AND type = ? AND domain = ?',
[ name, exports.TYPE_ALIAS, domain ], function (error, results) {
2019-10-24 13:34:14 -07:00
if (error) return callback(new BoxError(BoxError.DATABASE_ERROR, error));
2019-10-24 20:48:38 -07:00
if (results.length === 0) return callback(new BoxError(BoxError.NOT_FOUND, 'Mailbox not found'));
2016-09-25 18:59:11 -07:00
2018-04-07 19:12:07 -07:00
results.forEach(function (result) { postProcess(result); });
2018-04-07 19:12:07 -07:00
callback(null, results[0]);
});
2016-09-25 18:59:11 -07:00
}