Skip to content

Commit 85534ba

Browse files
authored
Merge pull request #903 from saidsay-so/ssh_fix
fix(pam): use environ variable when getenv doesn't work
2 parents b52ec0f + 906a8f7 commit 85534ba

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ To install them on Debian/Ubuntu for example:
9292
sudo apt-get update && sudo apt-get install -y \
9393
python3 python3-pip python3-setuptools python3-wheel \
9494
cmake make build-essential \
95-
libpam0g-dev libinih-dev libevdev-dev \
95+
libpam0g-dev libinih-dev libevdev-dev python3-opencv \
9696
python3-dev libopencv-dev
9797
```
9898

howdy/src/pam/main.cc

+4-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include <cerrno>
22
#include <csignal>
33
#include <cstdlib>
4-
#include <ostream>
54

65
#include <glob.h>
76
#include <libintl.h>
@@ -16,22 +15,15 @@
1615
#include <syslog.h>
1716
#include <unistd.h>
1817

19-
#include <atomic>
2018
#include <chrono>
2119
#include <condition_variable>
2220
#include <cstring>
2321
#include <fstream>
2422
#include <functional>
2523
#include <future>
26-
#include <iostream>
27-
#include <iterator>
28-
#include <memory>
2924
#include <mutex>
3025
#include <string>
31-
#include <system_error>
32-
#include <thread>
3326
#include <tuple>
34-
#include <vector>
3527

3628
#include <INIReader.h>
3729

@@ -42,7 +34,7 @@
4234
#include "enter_device.hh"
4335
#include "main.hh"
4436
#include "optional_task.hh"
45-
#include "paths.hh"
37+
#include <paths.hh>
4638

4739
const auto DEFAULT_TIMEOUT =
4840
std::chrono::duration<int, std::chrono::milliseconds::period>(100);
@@ -138,7 +130,7 @@ auto howdy_status(char *username, int status, const INIReader &config,
138130
* @return Returns PAM_AUTHINFO_UNAVAIL if it shouldn't be enabled,
139131
* PAM_SUCCESS otherwise
140132
*/
141-
auto check_enabled(const INIReader &config, const char* username) -> int {
133+
auto check_enabled(const INIReader &config, const char *username) -> int {
142134
// Stop executing if Howdy has been disabled in the config
143135
if (config.GetBoolean("core", "disabled", false)) {
144136
syslog(LOG_INFO, "Skipped authentication, Howdy is disabled");
@@ -147,8 +139,8 @@ auto check_enabled(const INIReader &config, const char* username) -> int {
147139

148140
// Stop if we're in a remote shell and configured to exit
149141
if (config.GetBoolean("core", "abort_if_ssh", true)) {
150-
if (getenv("SSH_CONNECTION") != nullptr ||
151-
getenv("SSH_CLIENT") != nullptr || getenv("SSHD_OPTS") != nullptr) {
142+
if (checkenv("SSH_CONNECTION") || checkenv("SSH_CLIENT") ||
143+
checkenv("SSH_TTY") || checkenv("SSHD_OPTS")) {
152144
syslog(LOG_INFO, "Skipped authentication, SSH session detected");
153145
return PAM_AUTHINFO_UNAVAIL;
154146
}

howdy/src/pam/main.hh

+27
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#ifndef MAIN_H_
22
#define MAIN_H_
33

4+
#include <cstring>
45
#include <string>
6+
#include <unistd.h>
57

68
enum class ConfirmationType { Unset, Howdy, Pam };
79

@@ -29,4 +31,29 @@ inline auto get_workaround(const std::string &workaround) -> Workaround {
2931
return Workaround::Off;
3032
}
3133

34+
/**
35+
* Check if an environment variable exists either in the environ array or using
36+
* getenv.
37+
* @param name The name of the environment variable.
38+
* @return The value of the environment variable or nullptr if it doesn't exist
39+
* or environ is nullptr.
40+
* @note This function was created because `getenv` wasn't working properly in
41+
* some contexts (like sudo).
42+
*/
43+
auto checkenv(const char *name) -> bool {
44+
if (std::getenv(name) != nullptr) {
45+
return true;
46+
}
47+
48+
auto len = strlen(name);
49+
50+
for (char **env = environ; *env != nullptr; env++) {
51+
if (strncmp(*env, name, len) == 0) {
52+
return true;
53+
}
54+
}
55+
56+
return false;
57+
}
58+
3259
#endif // MAIN_H_

0 commit comments

Comments
 (0)