Skip to content

Commit 906a8f7

Browse files
committed
fix(pam): use environ variable when getenv doesn't work
On some programs like sudo, getenv doesn't return the environment variable value.
1 parent 344eb34 commit 906a8f7

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

README.md

Lines changed: 1 addition & 1 deletion
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

Lines changed: 4 additions & 12 deletions
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);
@@ -139,7 +131,7 @@ auto howdy_status(char *username, int status, const INIReader &config,
139131
* @return Returns PAM_AUTHINFO_UNAVAIL if it shouldn't be enabled,
140132
* PAM_SUCCESS otherwise
141133
*/
142-
auto check_enabled(const INIReader &config, const char* username) -> int {
134+
auto check_enabled(const INIReader &config, const char *username) -> int {
143135
// Stop executing if Howdy has been disabled in the config
144136
if (config.GetBoolean("core", "disabled", false)) {
145137
syslog(LOG_INFO, "Skipped authentication, Howdy is disabled");
@@ -148,8 +140,8 @@ auto check_enabled(const INIReader &config, const char* username) -> int {
148140

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

howdy/src/pam/main.hh

Lines changed: 27 additions & 0 deletions
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)