passwordfd.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Author: Arvin Schnell <arvin@suse.de>
  3. *
  4. * This plugin let's you pass the password to the pppd via
  5. * a file descriptor. That's easy and secure - no fiddling
  6. * with pap- and chap-secrets files.
  7. */
  8. #include <stdio.h>
  9. #include <errno.h>
  10. #include <string.h>
  11. #include <unistd.h>
  12. #include "pppd.h"
  13. char pppd_version[] = VERSION;
  14. static int passwdfd = -1;
  15. static char save_passwd[MAXSECRETLEN];
  16. static option_t options[] = {
  17. { "passwordfd", o_int, &passwdfd,
  18. "Receive password on this file descriptor" },
  19. { NULL }
  20. };
  21. static int pwfd_check (void)
  22. {
  23. return 1;
  24. }
  25. static int pwfd_passwd (char *user, char *passwd)
  26. {
  27. int readgood, red;
  28. if (passwdfd == -1)
  29. return -1;
  30. if (passwd == NULL)
  31. return 1;
  32. if (passwdfd == -2) {
  33. strcpy (passwd, save_passwd);
  34. return 1;
  35. }
  36. readgood = 0;
  37. do {
  38. red = read (passwdfd, passwd + readgood, MAXSECRETLEN - 1 - readgood);
  39. if (red == 0)
  40. break;
  41. if (red < 0) {
  42. error ("Can't read secret from fd\n");
  43. readgood = -1;
  44. break;
  45. }
  46. readgood += red;
  47. } while (readgood < MAXSECRETLEN - 1);
  48. close (passwdfd);
  49. if (readgood < 0)
  50. return 0;
  51. passwd[readgood] = 0;
  52. strcpy (save_passwd, passwd);
  53. passwdfd = -2;
  54. return 1;
  55. }
  56. void plugin_init (void)
  57. {
  58. add_options (options);
  59. pap_check_hook = pwfd_check;
  60. pap_passwd_hook = pwfd_passwd;
  61. chap_check_hook = pwfd_check;
  62. chap_passwd_hook = pwfd_passwd;
  63. }