UNPKG

mecano

Version:

Common functions for system deployment.

188 lines (182 loc) 6.41 kB
// Generated by CoffeeScript 1.9.1 var each, ldap, misc, string; module.exports = function(options, callback) { var modified; if (options.acls == null) { options.acls = [{}]; } modified = false; return each(options.acls).run((function(_this) { return function(acl, callback) { var do_diff, do_end, do_getacls, do_getdn, do_save; do_getdn = function() { if (options.hdb_dn) { return do_getacls(); } if (typeof options.log === "function") { options.log("mecano `ldap_acl`: get DN of the HDB to modify"); } return _this.execute({ cmd: "ldapsearch -LLL -Y EXTERNAL -H ldapi:/// -b cn=config \"(olcSuffix= " + options.suffix + ")\" dn 2>/dev/null | egrep '^dn' | sed -e 's/^dn:\\s*olcDatabase=\\(.*\\)$/\\1/g'" }, function(err, _, hdb_dn) { if (err) { return callback(err); } options.hdb_dn = hdb_dn.trim(); return do_getacls(); }); }; do_getacls = function() { if (typeof options.log === "function") { options.log("mecano `ldap_acl`: list all ACL of the directory"); } return _this.execute({ cmd: "ldapsearch -LLL -Y EXTERNAL -H ldapi:/// -b olcDatabase=" + options.hdb_dn + " \"(olcAccess=*)\" olcAccess" }, function(err, _, stdout) { var current, j, len, line, match, olcAccesses, ref; if (err) { return callback(err); } current = null; olcAccesses = []; ref = string.lines(stdout); for (j = 0, len = ref.length; j < len; j++) { line = ref[j]; if (match = /^olcAccess: (.*)$/.exec(line)) { if (current != null) { olcAccesses.push(current); } current = match[1]; } else if (current != null) { if (/^ /.test(line)) { current += line.substr(1); } else { olcAccesses.push(current); current = null; } } } return do_diff(ldap.acl.parse(olcAccesses)); }); }; do_diff = function(olcAccesses) { var access, access_by, acl_by, found, i, index, is_perfect_match, j, k, l, len, len1, len2, len3, len4, len5, m, n, not_found_acl, o, olcAccess, ref, ref1, ref2; olcAccess = null; for (i = j = 0, len = olcAccesses.length; j < len; i = ++j) { access = olcAccesses[i]; if (acl.to === access.to) { olcAccess = misc.object.clone(access); olcAccess.old = access; break; } } if (olcAccess) { is_perfect_match = true; not_found_acl = []; if (acl.by.length !== olcAccess.by.length) { is_perfect_match = false; } else { ref = acl.by; for (i = k = 0, len1 = ref.length; k < len1; i = ++k) { acl_by = ref[i]; if (acl_by !== olcAccess.by[i]) { is_perfect_match = false; } found = true; ref1 = olcAccess.by; for (l = 0, len2 = ref1.length; l < len2; l++) { access_by = ref1[l]; if (acl_by !== access_by) { found = false; } } if (!found) { not_found_acl.push(acl_by); } } } if (is_perfect_match) { if (typeof options.log === "function") { options.log('mecano `ldap_acl`: no modification to apply'); } return do_end(); } if (not_found_acl.length) { if (typeof options.log === "function") { options.log('mecano `ldap_acl`: modify access after undefined acl'); } ref2 = olcAccess.by; for (m = 0, len3 = ref2.length; m < len3; m++) { access_by = ref2[m]; not_found_acl.push(access_by); } olcAccess.by = not_found_acl; } else { if (typeof options.log === "function") { options.log('mecano `ldap_acl`: modify access after reorder'); } olcAccess.by = acl.by; } } else { if (typeof options.log === "function") { options.log('mecano `ldap_acl`: insert a new access'); } index = olcAccesses.length; if (acl.before) { for (i = n = 0, len4 = olcAccesses.length; n < len4; i = ++n) { access = olcAccesses[i]; if (access.to === acl.before) { index = i; } } } else if (acl.after) { for (i = o = 0, len5 = olcAccesses.length; o < len5; i = ++o) { access = olcAccesses[i]; if (access.to === options.after) { index = i + 1; } } } olcAccess = { index: index, to: acl.to, by: acl.by, add: true }; } return do_save(olcAccess); }; do_save = function(olcAccess) { var cmd, old; if (olcAccess.old) { old = ldap.acl.stringify(olcAccess.old); } olcAccess = ldap.acl.stringify(olcAccess); if (old) { cmd = "ldapadd -Y EXTERNAL -H ldapi:/// <<-EOF\ndn: olcDatabase=" + options.hdb_dn + "\nchangetype: modify\ndelete: olcAccess\nolcAccess: " + old + "\n-\nadd: olcAccess\nolcAccess: " + olcAccess + "\nEOF"; } else { cmd = "ldapadd -Y EXTERNAL -H ldapi:/// <<-EOF\ndn: olcDatabase=" + options.hdb_dn + "\nchangetype: modify\nadd: olcAccess\nolcAccess: " + olcAccess + "\nEOF"; } return _this.execute({ cmd: cmd }, function(err, _, hdb_dn) { if (err) { return callback(err); } modified = true; return do_end(); }); }; do_end = function() { return callback(); }; return do_getdn(); }; })(this)).then(function(err) { return callback(err, modified); }); }; each = require('each'); misc = require('./misc'); ldap = require('./misc/ldap'); string = require('./misc/string');