Advertisement
Guest User

kwalletd main.cpp patch

a guest
Feb 19th, 2020
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 5.96 KB | None | 0 0
  1. --- main_old.cpp    2020-02-19 23:09:00.260522578 +0100
  2. +++ main.cpp    2020-02-19 22:49:59.085331739 +0100
  3. @@ -38,12 +38,16 @@
  4.  #ifndef Q_OS_WIN
  5.  #include <sys/types.h>
  6.  #include <sys/socket.h>
  7. +#include <sys/syslog.h>
  8.  #include <sys/un.h>
  9. +#include <sys/stat.h>
  10. +#include <fcntl.h>
  11.  #include <unistd.h>
  12. +#include <string.h>
  13.  
  14.  #define BSIZE 1000
  15.  static int pipefd = 0;
  16. -static int socketfd = 0;
  17. +char* socketPath = nullptr;
  18.  #endif
  19.  
  20.  static bool isWalletEnabled()
  21. @@ -57,7 +61,7 @@
  22.  //Waits until the PAM_MODULE sends the hash
  23.  static char *waitForHash()
  24.  {
  25. -    printf("kwalletd5: Waiting for hash on %d-\n", pipefd);
  26. +    syslog(LOG_DEBUG, "kwalletd5: Waiting for hash on %d-\n", pipefd);
  27.      int totalRead = 0;
  28.      int readBytes = 0;
  29.      int attempts = 0;
  30. @@ -74,58 +78,72 @@
  31.      }
  32.  
  33.      close(pipefd);
  34. +    syslog(LOG_DEBUG, "kwalletd5: hash obtained\n");
  35.      return buf;
  36.  }
  37.  
  38. +int mult_putenv(char* str) {
  39. +    if(!str) return 0;
  40. +    while((str = strtok(str, "\n"))) {
  41. +        if(putenv(str)) return -1;
  42. +        else str = NULL;
  43. +    }
  44. +    return 0;
  45. +}
  46. +
  47.  //Waits until startkde sends the environment variables
  48.  static int waitForEnvironment()
  49.  {
  50. -    printf("kwalletd5: waitingForEnvironment on: %d\n", socketfd);
  51. +    syslog(LOG_DEBUG, "kwalletd5: waitingForEnvironment on: %s\n", socketPath);
  52.  
  53. -    int s2;
  54. -    struct sockaddr_un remote;
  55. -    socklen_t t = sizeof(remote);
  56. -    if ((s2 = accept(socketfd, (struct sockaddr *)&remote, &t)) == -1) {
  57. -        fprintf(stdout, "kwalletd5: Couldn't accept incoming connection\n");
  58. +    int socketfd;
  59. +    if((socketfd = open(socketPath, O_RDONLY)) == -1) {  //Blocking here!!!
  60. +        syslog(LOG_DEBUG, "kwalletd5: couldn't open env named pipe; errno: %d", errno);
  61.          return -1;
  62.      }
  63. -    printf("kwalletd5: client connected\n");
  64. -
  65. -    char str[BSIZE] = {'\0'};
  66. -
  67. -    int chop = 0;
  68. -    FILE *s3 = fdopen(dup(s2), "r");
  69. -    while(!feof(s3)) {
  70. -        if (fgets(str, BSIZE, s3)) {
  71. -            chop = strlen(str) - 1;
  72. -            if (str[chop] == '\n') {
  73. -                str[chop] = '\0';
  74. -            }
  75. -            putenv(strdup(str));
  76. +    syslog(LOG_DEBUG, "kwalletd5: receiving env from fd: %d\n", socketfd);
  77. +    int byteCount = 0;
  78. +    int totBytes = 0;
  79. +    char *envBuf = (char*)malloc(BSIZE*sizeof(char));
  80. +    size_t curSize = BSIZE;
  81. +    do {
  82. +        if(!(curSize - totBytes)) {
  83. +            envBuf = (char*)realloc((void*)envBuf, curSize+=BSIZE);
  84.          }
  85. +        totBytes += (byteCount = read(socketfd, envBuf + totBytes, curSize - totBytes));
  86. +    } while(byteCount > 0);
  87. +    if(byteCount == -1) {
  88. +        syslog(LOG_DEBUG, "kwalletd5: couldn't read env from fifo; errno: %d\n", errno);
  89. +        return -1;
  90.      }
  91. -    fclose(s3);
  92. -
  93. -    printf("kwalletd5: client disconnected\n");
  94.      close(socketfd);
  95. +    syslog(LOG_DEBUG, "kwalletd5: client disconnected; %d bytes read\n", totBytes);
  96. +    //Shrink buffer
  97. +    envBuf = (char*)realloc((void*)envBuf, totBytes);
  98. +    //Add string terminator (it replaces the newline character at the end of the string)
  99. +    envBuf[totBytes-1] = '\0';
  100. +    //Set env vars
  101. +    mult_putenv(envBuf);
  102. +    //Do not free envBuf!!! It is now part of the environment
  103. +
  104.      return 1;
  105.  }
  106.  
  107.  char* checkPamModule(int argc, char **argv)
  108.  {
  109. -    printf("kwalletd5: Checking for pam module\n");
  110. +    syslog(LOG_DEBUG, "kwalletd5: Checking for pam module\n");
  111.      char *hash = nullptr;
  112.      int x = 1;
  113.      for (; x < argc; ++x) {
  114.          if (strcmp(argv[x], "--pam-login") != 0) {
  115.              continue;
  116.          }
  117. -        printf("kwalletd5: Got pam-login param\n");
  118. +        syslog(LOG_DEBUG, "kwalletd5: Got pam-login param\n");
  119.          argv[x] = nullptr;
  120.          x++;
  121.          //We need at least 2 extra arguments after --pam-login
  122.          if (x + 1 > argc) {
  123. -            printf("kwalletd5: Invalid arguments (less than needed)\n");
  124. +            syslog(LOG_DEBUG, "kwalletd5: Invalid arguments (less than needed)\n");
  125.              return nullptr;
  126.          }
  127.  
  128. @@ -134,20 +152,20 @@
  129.          argv[x] = nullptr;
  130.          x++;
  131.          //second socket for environment, comes from a localsocket
  132. -        socketfd = atoi(argv[x]);
  133. +        socketPath = argv[x];
  134.          argv[x] = nullptr;
  135.          break;
  136.      }
  137.  
  138. -    if (!pipefd || !socketfd) {
  139. -        printf("Lacking a socket, pipe: %d, env:%d\n", pipefd, socketfd);
  140. +    if (!pipefd || !socketPath) {
  141. +        syslog(LOG_DEBUG, "Lacking a socket, pipe: %d, env:%s\n", pipefd, socketPath);
  142.          return nullptr;
  143.      }
  144.  
  145.      hash = waitForHash();
  146.  
  147.      if (hash == nullptr || waitForEnvironment() == -1) {
  148. -        printf("kwalletd5: Hash or environment not received\n");
  149. +        syslog(LOG_DEBUG, "kwalletd5: Hash or environment not received\n");
  150.          free(hash);
  151.          return nullptr;
  152.      }
  153. @@ -167,6 +185,9 @@
  154.      if (getenv("PAM_KWALLET5_LOGIN")) {
  155.          hash = checkPamModule(argc, argv);
  156.      }
  157. +    if(!hash) {
  158. +        printf("kwalletd5: error: PAM_KWALLET5_LOGIN is set but the program couldn't retrieve the hash; see the syslog for more details.\n");
  159. +    }
  160.  #endif
  161.  
  162.      QApplication app(argc, argv);
  163. @@ -213,15 +234,19 @@
  164.  
  165.      KWalletD walletd;
  166.      qCDebug(KWALLETD_LOG) << "kwalletd5 started";
  167. +    syslog(LOG_DEBUG, "kwalletd5: kwalletd5 started");
  168.  
  169.  #ifndef Q_OS_WIN
  170.      if (hash) {
  171. +        syslog(LOG_DEBUG, "kwalletd5: opening wallet...\n");
  172.          QByteArray passHash(hash, PBKDF2_SHA512_KEYSIZE);
  173.          int wallet = walletd.pamOpen(KWallet::Wallet::LocalWallet(), passHash, 0);
  174.          if (wallet < 0) {
  175.              qWarning() << "Wallet failed to get opened by PAM, error code is" << wallet;
  176. +            syslog(LOG_DEBUG, "kwalletd5: Wallet failed to get opened by PAM, error code is %d\n", wallet);
  177.          } else {
  178.              qCDebug(KWALLETD_LOG) << "Wallet opened by PAM";
  179. +            syslog(LOG_DEBUG, "kwalletd5: Wallet opened by PAM");
  180.          }
  181.          free(hash);
  182.      }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement