0009-passwordfd-read-early.patch 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. From b67eb5595b232c0d936d38a595544a298f99517f Mon Sep 17 00:00:00 2001
  2. From: Mike Frysinger <vapier@gentoo.org>
  3. Date: Fri, 3 Jan 2020 17:19:09 +0100
  4. Subject: [PATCH 09/14] passwordfd: read early
  5. ---
  6. pppd/plugins/passwordfd.c | 54 ++++++++++++++++++---------------------
  7. 1 file changed, 25 insertions(+), 29 deletions(-)
  8. diff --git a/pppd/plugins/passwordfd.c b/pppd/plugins/passwordfd.c
  9. index c3f9793..48d04b2 100644
  10. --- a/pppd/plugins/passwordfd.c
  11. +++ b/pppd/plugins/passwordfd.c
  12. @@ -16,11 +16,11 @@
  13. char pppd_version[] = VERSION;
  14. -static int passwdfd = -1;
  15. static char save_passwd[MAXSECRETLEN];
  16. +static int readpassword (char **);
  17. static option_t options[] = {
  18. - { "passwordfd", o_int, &passwdfd,
  19. + { "passwordfd", o_special, (void *)readpassword,
  20. "Receive password on this file descriptor" },
  21. { NULL }
  22. };
  23. @@ -30,43 +30,39 @@ static int pwfd_check (void)
  24. return 1;
  25. }
  26. -static int pwfd_passwd (char *user, char *passwd)
  27. +static int readpassword(char **argv)
  28. {
  29. - int readgood, red;
  30. -
  31. - if (passwdfd == -1)
  32. - return -1;
  33. + char *arg = *argv;
  34. + int passwdfd = -1;
  35. + int chunk, len;
  36. - if (passwd == NULL)
  37. - return 1;
  38. -
  39. - if (passwdfd == -2) {
  40. - strcpy (passwd, save_passwd);
  41. - return 1;
  42. + if (sscanf(arg, "%d", &passwdfd) != 1 || passwdfd < 0)
  43. + {
  44. + error ("\"%s\" is not a valid file descriptor number", arg);
  45. + return 0;
  46. }
  47. - readgood = 0;
  48. + len = 0;
  49. do {
  50. - red = read (passwdfd, passwd + readgood, MAXSECRETLEN - 1 - readgood);
  51. - if (red == 0)
  52. - break;
  53. - if (red < 0) {
  54. - error ("Can't read secret from fd\n");
  55. - readgood = -1;
  56. + chunk = read (passwdfd, save_passwd + len, MAXSECRETLEN - 1 - len);
  57. + if (chunk == 0)
  58. break;
  59. + if (chunk < 0) {
  60. + error ("Can't read secret from fd %d", passwdfd);
  61. + return 0;
  62. }
  63. - readgood += red;
  64. - } while (readgood < MAXSECRETLEN - 1);
  65. -
  66. + len += chunk;
  67. + } while (len < MAXSECRETLEN - 1);
  68. + save_passwd[len] = 0;
  69. close (passwdfd);
  70. - if (readgood < 0)
  71. - return 0;
  72. -
  73. - passwd[readgood] = 0;
  74. - strcpy (save_passwd, passwd);
  75. - passwdfd = -2;
  76. + return 1;
  77. +}
  78. +static int pwfd_passwd (char *user, char *passwd)
  79. +{
  80. + if (passwd != NULL)
  81. + strcpy (passwd, save_passwd);
  82. return 1;
  83. }
  84. --
  85. 2.30.0