2020-11-12 22:20:25 +00:00
|
|
|
|
|
|
|
const app = require('vn-loopback/server/server');
|
|
|
|
const ldap = require('../util/ldapjs-extra');
|
|
|
|
const crypto = require('crypto');
|
|
|
|
const nthash = require('smbhash').nthash;
|
2024-05-21 13:11:32 +00:00
|
|
|
const isProduction = require('vn-loopback/server/boot/isProduction');
|
2020-11-12 22:20:25 +00:00
|
|
|
|
|
|
|
module.exports = Self => {
|
2024-05-21 11:50:45 +00:00
|
|
|
const shouldSync = isProduction();
|
2022-11-02 09:18:38 +00:00
|
|
|
|
2023-11-20 17:39:37 +00:00
|
|
|
Self.getLinker = async function() {
|
2020-11-12 22:20:25 +00:00
|
|
|
return await Self.findOne({
|
|
|
|
fields: [
|
|
|
|
'server',
|
|
|
|
'rdn',
|
|
|
|
'password',
|
|
|
|
'userDn',
|
|
|
|
'groupDn'
|
|
|
|
]
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Object.assign(Self.prototype, {
|
|
|
|
async init() {
|
|
|
|
this.client = ldap.createClient({
|
|
|
|
url: this.server
|
|
|
|
});
|
2023-11-20 17:39:37 +00:00
|
|
|
this.client.on('error', () => {});
|
2020-11-12 22:20:25 +00:00
|
|
|
await this.client.bind(this.rdn, this.password);
|
|
|
|
},
|
|
|
|
|
|
|
|
async deinit() {
|
|
|
|
await this.client.unbind();
|
|
|
|
},
|
|
|
|
|
|
|
|
async syncUser(userName, info, password) {
|
|
|
|
let {
|
|
|
|
client,
|
|
|
|
accountConfig
|
|
|
|
} = this;
|
|
|
|
|
2020-12-09 11:28:18 +00:00
|
|
|
let dn = `uid=${userName},${this.userDn}`;
|
2020-11-12 22:20:25 +00:00
|
|
|
|
2020-11-13 12:20:37 +00:00
|
|
|
if (info.hasAccount) {
|
|
|
|
let {user} = info;
|
2020-11-12 22:20:25 +00:00
|
|
|
|
2020-11-13 12:20:37 +00:00
|
|
|
let oldUser = await client.searchOne(this.userDn, {
|
|
|
|
scope: 'sub',
|
|
|
|
filter: `&(uid=${userName})`
|
|
|
|
});
|
2020-11-12 22:20:25 +00:00
|
|
|
|
2020-11-13 12:20:37 +00:00
|
|
|
let nickname = user.nickname || userName;
|
|
|
|
let nameArgs = nickname.trim().split(' ');
|
|
|
|
let sn = nameArgs.length > 1
|
|
|
|
? nameArgs.splice(1).join(' ')
|
|
|
|
: '-';
|
|
|
|
|
2020-12-09 11:28:18 +00:00
|
|
|
let newEntry = {
|
2020-11-13 12:20:37 +00:00
|
|
|
uid: userName,
|
|
|
|
objectClass: [
|
|
|
|
'inetOrgPerson',
|
|
|
|
'posixAccount',
|
|
|
|
'sambaSamAccount'
|
|
|
|
],
|
|
|
|
cn: nickname,
|
|
|
|
displayName: nickname,
|
|
|
|
givenName: nameArgs[0],
|
|
|
|
sn,
|
|
|
|
mail: info.corporateMail,
|
|
|
|
preferredLanguage: user.lang || 'en',
|
|
|
|
homeDirectory: `${accountConfig.homedir}/${userName}`,
|
|
|
|
loginShell: accountConfig.shell,
|
|
|
|
uidNumber: info.uidNumber,
|
|
|
|
gidNumber: accountConfig.idBase + user.roleFk,
|
|
|
|
sambaSID: '-'
|
|
|
|
};
|
|
|
|
|
|
|
|
if (password) {
|
|
|
|
let salt = crypto
|
|
|
|
.randomBytes(8)
|
|
|
|
.toString('base64');
|
|
|
|
|
|
|
|
let hash = crypto.createHash('sha1');
|
|
|
|
hash.update(password);
|
|
|
|
hash.update(salt, 'binary');
|
|
|
|
let digest = hash.digest('binary');
|
|
|
|
|
|
|
|
let ssha = Buffer
|
|
|
|
.from(digest + salt, 'binary')
|
|
|
|
.toString('base64');
|
|
|
|
|
|
|
|
Object.assign(newEntry, {
|
|
|
|
userPassword: `{SSHA}${ssha}`,
|
|
|
|
sambaNTPassword: nthash(password)
|
|
|
|
});
|
|
|
|
} else if (oldUser) {
|
|
|
|
Object.assign(newEntry, {
|
|
|
|
userPassword: oldUser.userPassword,
|
|
|
|
sambaNTPassword: oldUser.sambaNTPassword
|
|
|
|
});
|
|
|
|
}
|
2020-11-12 22:20:25 +00:00
|
|
|
|
2020-11-13 12:20:37 +00:00
|
|
|
for (let prop in newEntry) {
|
|
|
|
if (newEntry[prop] == null)
|
|
|
|
delete newEntry[prop];
|
|
|
|
}
|
2020-11-12 22:20:25 +00:00
|
|
|
|
2020-12-09 11:28:18 +00:00
|
|
|
if (oldUser) {
|
|
|
|
let changes = [];
|
|
|
|
let skipProps = new Set([
|
|
|
|
'dn',
|
|
|
|
'controls'
|
|
|
|
]);
|
|
|
|
|
|
|
|
for (let prop in oldUser) {
|
|
|
|
let deleteProp = !skipProps.has(prop)
|
|
|
|
&& !newEntry.hasOwnProperty(prop);
|
|
|
|
if (!deleteProp) continue;
|
|
|
|
changes.push(new ldap.Change({
|
|
|
|
operation: 'delete',
|
|
|
|
modification: {
|
|
|
|
[prop]: oldUser[prop]
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
for (let prop in newEntry) {
|
|
|
|
if (this.isEqual(oldUser[prop], newEntry[prop]))
|
|
|
|
continue;
|
|
|
|
changes.push(new ldap.Change({
|
|
|
|
operation: 'replace',
|
|
|
|
modification: {
|
|
|
|
[prop]: newEntry[prop]
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2022-11-02 09:18:38 +00:00
|
|
|
if (shouldSync && changes.length)
|
2020-12-09 11:28:18 +00:00
|
|
|
await client.modify(dn, changes);
|
2022-11-02 09:18:38 +00:00
|
|
|
} else if (shouldSync)
|
2020-12-09 11:28:18 +00:00
|
|
|
await client.add(dn, newEntry);
|
|
|
|
} else {
|
|
|
|
try {
|
2022-11-02 09:18:38 +00:00
|
|
|
if (shouldSync)
|
|
|
|
await client.del(dn);
|
2023-11-16 22:07:26 +00:00
|
|
|
// eslint-disable-next-line no-console
|
2020-12-09 11:28:18 +00:00
|
|
|
console.log(` -> User '${userName}' removed from LDAP`);
|
|
|
|
} catch (e) {
|
|
|
|
if (e.name !== 'NoSuchObjectError') throw e;
|
|
|
|
}
|
2020-11-12 22:20:25 +00:00
|
|
|
}
|
2020-12-09 11:28:18 +00:00
|
|
|
},
|
2020-11-12 22:20:25 +00:00
|
|
|
|
2020-12-09 11:28:18 +00:00
|
|
|
isEqual(a, b) {
|
|
|
|
if (Array.isArray(a) && Array.isArray(b)) {
|
|
|
|
if (a.length !== b.length)
|
|
|
|
return false;
|
|
|
|
for (let element of a) {
|
|
|
|
if (b.indexOf(element) === -1)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} else
|
|
|
|
return a == b;
|
2020-11-12 22:20:25 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
async syncUserGroups(userName, info) {
|
|
|
|
let {client} = this;
|
2020-12-09 11:28:18 +00:00
|
|
|
let {user} = info;
|
|
|
|
let groupDn = this.groupDn;
|
2020-11-12 22:20:25 +00:00
|
|
|
|
2020-11-13 12:20:37 +00:00
|
|
|
let opts = {
|
2020-11-12 22:20:25 +00:00
|
|
|
scope: 'sub',
|
2020-12-09 11:28:18 +00:00
|
|
|
attributes: ['dn', 'cn'],
|
2020-11-12 22:20:25 +00:00
|
|
|
filter: `&(memberUid=${userName})(objectClass=posixGroup)`
|
2020-11-13 12:20:37 +00:00
|
|
|
};
|
2020-12-09 11:28:18 +00:00
|
|
|
let oldGroups = await client.searchAll(groupDn, opts);
|
2020-11-12 22:20:25 +00:00
|
|
|
|
2020-12-09 11:28:18 +00:00
|
|
|
let deleteGroups = [];
|
|
|
|
let addGroups = [];
|
2020-11-12 22:20:25 +00:00
|
|
|
|
2020-12-09 11:28:18 +00:00
|
|
|
if (info.hasAccount) {
|
|
|
|
let oldSet = new Set();
|
|
|
|
oldGroups.forEach(e => oldSet.add(e.cn));
|
2020-11-12 22:20:25 +00:00
|
|
|
|
2020-12-09 11:28:18 +00:00
|
|
|
let newSet = new Set();
|
|
|
|
user.roles().forEach(e => newSet.add(e.inherits().name));
|
2020-11-30 17:50:04 +00:00
|
|
|
|
2020-12-09 11:28:18 +00:00
|
|
|
for (let group of oldGroups) {
|
|
|
|
if (!newSet.has(group.cn))
|
|
|
|
deleteGroups.push(group.cn);
|
|
|
|
}
|
|
|
|
for (let role of user.roles()) {
|
|
|
|
if (!oldSet.has(role.inherits().name))
|
|
|
|
addGroups.push(role.inherits().name);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (let group of oldGroups)
|
|
|
|
deleteGroups.push(group.cn);
|
|
|
|
}
|
2020-11-30 17:50:04 +00:00
|
|
|
|
2020-12-09 11:28:18 +00:00
|
|
|
async function applyOperations(groups, operation) {
|
|
|
|
for (let group of groups) {
|
|
|
|
try {
|
|
|
|
let dn = `cn=${group},${groupDn}`;
|
2022-11-02 09:18:38 +00:00
|
|
|
if (shouldSync) {
|
|
|
|
await client.modify(dn, new ldap.Change({
|
|
|
|
operation,
|
|
|
|
modification: {memberUid: userName}
|
|
|
|
}));
|
|
|
|
}
|
2020-12-09 11:28:18 +00:00
|
|
|
} catch (err) {
|
|
|
|
if (err.name !== 'NoSuchObjectError')
|
|
|
|
throw err;
|
|
|
|
}
|
2020-11-30 17:50:04 +00:00
|
|
|
}
|
2020-11-12 22:20:25 +00:00
|
|
|
}
|
2023-04-14 23:51:38 +00:00
|
|
|
|
2020-12-09 11:28:18 +00:00
|
|
|
await applyOperations(deleteGroups, 'delete');
|
|
|
|
await applyOperations(addGroups, 'add');
|
2020-11-12 22:20:25 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
async getUsers(usersToSync) {
|
|
|
|
let {client} = this;
|
|
|
|
|
2020-11-13 12:20:37 +00:00
|
|
|
let opts = {
|
2020-11-12 22:20:25 +00:00
|
|
|
scope: 'sub',
|
|
|
|
attributes: ['uid'],
|
|
|
|
filter: `uid=*`
|
2020-11-13 12:20:37 +00:00
|
|
|
};
|
|
|
|
await client.searchForeach(this.userDn, opts,
|
|
|
|
o => usersToSync.add(o.uid));
|
2020-11-12 22:20:25 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
async syncRoles() {
|
|
|
|
let $ = app.models;
|
|
|
|
let {
|
|
|
|
client,
|
|
|
|
accountConfig
|
|
|
|
} = this;
|
|
|
|
|
2020-11-13 12:20:37 +00:00
|
|
|
// Prepare data
|
2020-11-12 22:20:25 +00:00
|
|
|
|
2023-12-05 06:16:12 +00:00
|
|
|
let roles = await $.VnRole.find({
|
2020-11-12 22:20:25 +00:00
|
|
|
fields: ['id', 'name', 'description']
|
|
|
|
});
|
|
|
|
let roleRoles = await $.RoleRole.find({
|
|
|
|
fields: ['role', 'inheritsFrom']
|
|
|
|
});
|
|
|
|
let roleMap = toMap(roleRoles, e => {
|
|
|
|
return {key: e.inheritsFrom, val: e.role};
|
|
|
|
});
|
|
|
|
|
2023-01-31 13:57:24 +00:00
|
|
|
let accounts = await $.Account.find({
|
2020-11-12 22:20:25 +00:00
|
|
|
fields: ['id'],
|
|
|
|
include: {
|
|
|
|
relation: 'user',
|
|
|
|
scope: {
|
|
|
|
fields: ['name', 'roleFk'],
|
|
|
|
where: {active: true}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
let accountMap = toMap(accounts, e => {
|
|
|
|
let user = e.user();
|
|
|
|
if (!user) return;
|
|
|
|
return {key: user.roleFk, val: user.name};
|
|
|
|
});
|
|
|
|
|
2020-11-13 12:20:37 +00:00
|
|
|
// Delete roles
|
|
|
|
|
|
|
|
let opts = {
|
|
|
|
scope: 'sub',
|
|
|
|
attributes: ['dn'],
|
|
|
|
filter: 'objectClass=posixGroup'
|
|
|
|
};
|
|
|
|
let reqs = [];
|
2022-11-02 09:18:38 +00:00
|
|
|
await client.searchForeach(this.groupDn, opts, object => {
|
|
|
|
if (shouldSync)
|
|
|
|
reqs.push(client.del(object.dn));
|
|
|
|
});
|
2020-11-13 12:20:37 +00:00
|
|
|
await Promise.all(reqs);
|
|
|
|
|
|
|
|
// Recreate roles
|
|
|
|
|
2020-11-12 22:20:25 +00:00
|
|
|
reqs = [];
|
|
|
|
for (let role of roles) {
|
|
|
|
let newEntry = {
|
|
|
|
objectClass: ['top', 'posixGroup'],
|
|
|
|
cn: role.name,
|
|
|
|
description: role.description,
|
|
|
|
gidNumber: accountConfig.idBase + role.id
|
|
|
|
};
|
|
|
|
|
|
|
|
let memberUid = [];
|
|
|
|
for (let subrole of roleMap.get(role.id) || [])
|
|
|
|
memberUid = memberUid.concat(accountMap.get(subrole) || []);
|
|
|
|
|
|
|
|
if (memberUid.length) {
|
|
|
|
memberUid.sort((a, b) => a.localeCompare(b));
|
|
|
|
newEntry.memberUid = memberUid;
|
|
|
|
}
|
|
|
|
|
|
|
|
let dn = `cn=${role.name},${this.groupDn}`;
|
2022-11-02 09:18:38 +00:00
|
|
|
if (shouldSync)
|
|
|
|
reqs.push(client.add(dn, newEntry));
|
2020-11-12 22:20:25 +00:00
|
|
|
}
|
|
|
|
await Promise.all(reqs);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2020-11-13 12:20:37 +00:00
|
|
|
|
|
|
|
function toMap(array, fn) {
|
|
|
|
let map = new Map();
|
|
|
|
for (let item of array) {
|
|
|
|
let keyVal = fn(item);
|
|
|
|
if (!keyVal) continue;
|
|
|
|
let key = keyVal.key;
|
|
|
|
if (!map.has(key)) map.set(key, []);
|
|
|
|
map.get(key).push(keyVal.val);
|
|
|
|
}
|
|
|
|
return map;
|
|
|
|
}
|