pam-password-auth.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. const PluginAuth = require('./plugin-auth');
  2. /**
  3. * Use PAM authentication
  4. */
  5. class PamPasswordAuth extends PluginAuth {
  6. constructor(packSeq, compressPackSeq, pluginData, resolve, reject, multiAuthResolver) {
  7. super(resolve, reject, multiAuthResolver);
  8. this.pluginData = pluginData;
  9. this.sequenceNo = packSeq;
  10. this.counter = 0;
  11. }
  12. start(out, opts, info) {
  13. this.exchange(this.pluginData, out, opts, info);
  14. this.onPacketReceive = this.response;
  15. }
  16. exchange(buffer, out, opts, info) {
  17. //conversation is :
  18. // - first byte is information tell if question is a password (4) or clear text (2).
  19. // - other bytes are the question to user
  20. out.startPacket(this);
  21. let pwd;
  22. if (Array.isArray(opts.password)) {
  23. pwd = opts.password[this.counter];
  24. this.counter++;
  25. } else {
  26. pwd = opts.password;
  27. }
  28. if (pwd) out.writeString(pwd);
  29. out.writeInt8(0);
  30. out.flushBuffer(true);
  31. }
  32. response(packet, out, opts, info) {
  33. const marker = packet.peek();
  34. switch (marker) {
  35. //*********************************************************************************************************
  36. //* OK_Packet and Err_Packet ending packet
  37. //*********************************************************************************************************
  38. case 0x00:
  39. case 0xff:
  40. this.emit('send_end');
  41. return this.successSend(packet, out, opts, info);
  42. default:
  43. let promptData = packet.readBuffer();
  44. this.exchange(promptData, out, opts, info);
  45. this.onPacketReceive = this.response;
  46. }
  47. }
  48. }
  49. module.exports = PamPasswordAuth;