mecano
Version:
Common functions for system deployment.
155 lines (151 loc) • 5.12 kB
JavaScript
// Generated by CoffeeScript 1.9.1
var misc, string;
module.exports = function(options, callback) {
var do_chown, do_end, do_get, do_ktadd, do_ktremove, status;
if (!options.principal) {
return callback(new Error('Property principal is required'));
}
if (!options.keytab) {
return callback(new Error('Property keytab is required'));
}
if (/^\S+@\S+$/.test(options.kadmin_principal)) {
if (options.realm == null) {
options.realm = options.kadmin_principal.split('@')[1];
}
} else {
if (!options.realm) {
throw Error('Property "realm" is required unless present in principal');
}
options.principal = options.principal + "@" + options.realm;
}
status = false;
do_get = (function(_this) {
return function() {
if (!options.keytab) {
return do_end();
}
return _this.execute({
cmd: "export TZ=GMT; klist -kt " + options.keytab,
code_skipped: 1,
shy: true
}, function(err, exists, stdout, stderr) {
var _, i, keytab, kvno, len, line, match, mdate, principal, ref;
if (err) {
return callback(err);
}
if (!exists) {
if (typeof options.log === "function") {
options.log('Mecano `krb5_ktadd`: keytab does not yet exists');
}
return do_ktadd();
}
keytab = {};
ref = string.lines(stdout);
for (i = 0, len = ref.length; i < len; i++) {
line = ref[i];
if (match = /^\s*(\d+)\s+([\d\/:]+\s+[\d\/:]+)\s+(.*)\s*$/.exec(line)) {
_ = match[0], kvno = match[1], mdate = match[2], principal = match[3];
kvno = parseInt(kvno, 10);
mdate = Date.parse(mdate + " GMT");
if (!keytab[principal] || keytab[principal].kvno < kvno) {
keytab[principal] = {
kvno: kvno,
mdate: mdate
};
}
}
}
if (keytab[options.principal] == null) {
if (typeof options.log === "function") {
options.log('Mecano `krb5_ktadd`: Principal is not listed inside the keytab');
}
return do_ktadd();
}
return this.execute({
cmd: misc.kadmin(options, "getprinc -terse " + options.principal),
shy: true
}, function(err, exists, stdout, stderr) {
var ref1, ref2, ref3, values;
if (err) {
return err;
}
values = string.lines(stdout)[1];
if (!values) {
return callback(Error("Principal does not exist: '" + options.principal + "'"));
}
values = values.split('\t');
mdate = parseInt(values[2], 10) * 1000;
kvno = parseInt(values[8], 10);
if (typeof options.log === "function") {
options.log("Mecano `krb5_ktadd`: keytab kvno '" + ((ref1 = keytab[principal]) != null ? ref1.kvno : void 0) + "', principal kvno '" + kvno + "'");
}
if (typeof options.log === "function") {
options.log("Mecano `krb5_ktadd`: keytab mdate '" + (new Date((ref2 = keytab[principal]) != null ? ref2.mdate : void 0)) + "', principal mdate '" + (new Date(mdate)) + "'");
}
if (((ref3 = keytab[principal]) != null ? ref3.kvno : void 0) === kvno && keytab[principal].mdate === mdate) {
if (typeof options.log === "function") {
options.log('Mecano `krb5_ktadd`: kvno and mdate are ok, continue with changing the keytab');
}
return do_chown();
}
return do_ktremove();
});
});
};
})(this);
do_ktremove = (function(_this) {
return function() {
return _this.execute({
cmd: misc.kadmin(options, "ktremove -k " + options.keytab + " " + options.principal)
}, function(err, exists, stdout, stderr) {
if (err) {
return callback(err);
}
return do_ktadd();
});
};
})(this);
do_ktadd = (function(_this) {
return function() {
return _this.execute({
cmd: misc.kadmin(options, "ktadd -k " + options.keytab + " " + options.principal)
}, function(err, ktadded) {
if (err) {
return callback(err);
}
status = true;
return do_chown();
});
};
})(this);
do_chown = (function(_this) {
return function() {
return _this.chown({
destination: options.keytab,
uid: options.uid,
gid: options.gid,
"if": (options.uid != null) || (options.gid != null)
}).chmod({
destination: options.keytab,
mode: options.mode,
"if": options.mode != null
}).then(function(err, changed) {
if (err) {
return callback(err);
}
if (changed) {
status = changed;
}
return do_end();
});
};
})(this);
do_end = (function(_this) {
return function() {
return callback(null, status);
};
})(this);
return do_get();
};
misc = require('./misc');
string = require('./misc/string');