Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --- main_old.cpp 2020-02-19 23:09:00.260522578 +0100
- +++ main.cpp 2020-02-19 22:49:59.085331739 +0100
- @@ -38,12 +38,16 @@
- #ifndef Q_OS_WIN
- #include <sys/types.h>
- #include <sys/socket.h>
- +#include <sys/syslog.h>
- #include <sys/un.h>
- +#include <sys/stat.h>
- +#include <fcntl.h>
- #include <unistd.h>
- +#include <string.h>
- #define BSIZE 1000
- static int pipefd = 0;
- -static int socketfd = 0;
- +char* socketPath = nullptr;
- #endif
- static bool isWalletEnabled()
- @@ -57,7 +61,7 @@
- //Waits until the PAM_MODULE sends the hash
- static char *waitForHash()
- {
- - printf("kwalletd5: Waiting for hash on %d-\n", pipefd);
- + syslog(LOG_DEBUG, "kwalletd5: Waiting for hash on %d-\n", pipefd);
- int totalRead = 0;
- int readBytes = 0;
- int attempts = 0;
- @@ -74,58 +78,72 @@
- }
- close(pipefd);
- + syslog(LOG_DEBUG, "kwalletd5: hash obtained\n");
- return buf;
- }
- +int mult_putenv(char* str) {
- + if(!str) return 0;
- + while((str = strtok(str, "\n"))) {
- + if(putenv(str)) return -1;
- + else str = NULL;
- + }
- + return 0;
- +}
- +
- //Waits until startkde sends the environment variables
- static int waitForEnvironment()
- {
- - printf("kwalletd5: waitingForEnvironment on: %d\n", socketfd);
- + syslog(LOG_DEBUG, "kwalletd5: waitingForEnvironment on: %s\n", socketPath);
- - int s2;
- - struct sockaddr_un remote;
- - socklen_t t = sizeof(remote);
- - if ((s2 = accept(socketfd, (struct sockaddr *)&remote, &t)) == -1) {
- - fprintf(stdout, "kwalletd5: Couldn't accept incoming connection\n");
- + int socketfd;
- + if((socketfd = open(socketPath, O_RDONLY)) == -1) { //Blocking here!!!
- + syslog(LOG_DEBUG, "kwalletd5: couldn't open env named pipe; errno: %d", errno);
- return -1;
- }
- - printf("kwalletd5: client connected\n");
- -
- - char str[BSIZE] = {'\0'};
- -
- - int chop = 0;
- - FILE *s3 = fdopen(dup(s2), "r");
- - while(!feof(s3)) {
- - if (fgets(str, BSIZE, s3)) {
- - chop = strlen(str) - 1;
- - if (str[chop] == '\n') {
- - str[chop] = '\0';
- - }
- - putenv(strdup(str));
- + syslog(LOG_DEBUG, "kwalletd5: receiving env from fd: %d\n", socketfd);
- + int byteCount = 0;
- + int totBytes = 0;
- + char *envBuf = (char*)malloc(BSIZE*sizeof(char));
- + size_t curSize = BSIZE;
- + do {
- + if(!(curSize - totBytes)) {
- + envBuf = (char*)realloc((void*)envBuf, curSize+=BSIZE);
- }
- + totBytes += (byteCount = read(socketfd, envBuf + totBytes, curSize - totBytes));
- + } while(byteCount > 0);
- + if(byteCount == -1) {
- + syslog(LOG_DEBUG, "kwalletd5: couldn't read env from fifo; errno: %d\n", errno);
- + return -1;
- }
- - fclose(s3);
- -
- - printf("kwalletd5: client disconnected\n");
- close(socketfd);
- + syslog(LOG_DEBUG, "kwalletd5: client disconnected; %d bytes read\n", totBytes);
- + //Shrink buffer
- + envBuf = (char*)realloc((void*)envBuf, totBytes);
- + //Add string terminator (it replaces the newline character at the end of the string)
- + envBuf[totBytes-1] = '\0';
- + //Set env vars
- + mult_putenv(envBuf);
- + //Do not free envBuf!!! It is now part of the environment
- +
- return 1;
- }
- char* checkPamModule(int argc, char **argv)
- {
- - printf("kwalletd5: Checking for pam module\n");
- + syslog(LOG_DEBUG, "kwalletd5: Checking for pam module\n");
- char *hash = nullptr;
- int x = 1;
- for (; x < argc; ++x) {
- if (strcmp(argv[x], "--pam-login") != 0) {
- continue;
- }
- - printf("kwalletd5: Got pam-login param\n");
- + syslog(LOG_DEBUG, "kwalletd5: Got pam-login param\n");
- argv[x] = nullptr;
- x++;
- //We need at least 2 extra arguments after --pam-login
- if (x + 1 > argc) {
- - printf("kwalletd5: Invalid arguments (less than needed)\n");
- + syslog(LOG_DEBUG, "kwalletd5: Invalid arguments (less than needed)\n");
- return nullptr;
- }
- @@ -134,20 +152,20 @@
- argv[x] = nullptr;
- x++;
- //second socket for environment, comes from a localsocket
- - socketfd = atoi(argv[x]);
- + socketPath = argv[x];
- argv[x] = nullptr;
- break;
- }
- - if (!pipefd || !socketfd) {
- - printf("Lacking a socket, pipe: %d, env:%d\n", pipefd, socketfd);
- + if (!pipefd || !socketPath) {
- + syslog(LOG_DEBUG, "Lacking a socket, pipe: %d, env:%s\n", pipefd, socketPath);
- return nullptr;
- }
- hash = waitForHash();
- if (hash == nullptr || waitForEnvironment() == -1) {
- - printf("kwalletd5: Hash or environment not received\n");
- + syslog(LOG_DEBUG, "kwalletd5: Hash or environment not received\n");
- free(hash);
- return nullptr;
- }
- @@ -167,6 +185,9 @@
- if (getenv("PAM_KWALLET5_LOGIN")) {
- hash = checkPamModule(argc, argv);
- }
- + if(!hash) {
- + printf("kwalletd5: error: PAM_KWALLET5_LOGIN is set but the program couldn't retrieve the hash; see the syslog for more details.\n");
- + }
- #endif
- QApplication app(argc, argv);
- @@ -213,15 +234,19 @@
- KWalletD walletd;
- qCDebug(KWALLETD_LOG) << "kwalletd5 started";
- + syslog(LOG_DEBUG, "kwalletd5: kwalletd5 started");
- #ifndef Q_OS_WIN
- if (hash) {
- + syslog(LOG_DEBUG, "kwalletd5: opening wallet...\n");
- QByteArray passHash(hash, PBKDF2_SHA512_KEYSIZE);
- int wallet = walletd.pamOpen(KWallet::Wallet::LocalWallet(), passHash, 0);
- if (wallet < 0) {
- qWarning() << "Wallet failed to get opened by PAM, error code is" << wallet;
- + syslog(LOG_DEBUG, "kwalletd5: Wallet failed to get opened by PAM, error code is %d\n", wallet);
- } else {
- qCDebug(KWALLETD_LOG) << "Wallet opened by PAM";
- + syslog(LOG_DEBUG, "kwalletd5: Wallet opened by PAM");
- }
- free(hash);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement