Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(pam): use environ variable when getenv doesn't work #903

Open
wants to merge 1 commit into
base: beta
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ To install them on Debian/Ubuntu for example:
sudo apt-get update && sudo apt-get install -y \
python3 python3-pip python3-setuptools python3-wheel \
cmake make build-essential \
libpam0g-dev libinih-dev libevdev-dev \
libpam0g-dev libinih-dev libevdev-dev python3-opencv \
python3-dev libopencv-dev
```

Expand Down
16 changes: 4 additions & 12 deletions howdy/src/pam/main.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <cerrno>
#include <csignal>
#include <cstdlib>
#include <ostream>

#include <glob.h>
#include <libintl.h>
Expand All @@ -16,22 +15,15 @@
#include <syslog.h>
#include <unistd.h>

#include <atomic>
#include <chrono>
#include <condition_variable>
#include <cstring>
#include <fstream>
#include <functional>
#include <future>
#include <iostream>
#include <iterator>
#include <memory>
#include <mutex>
#include <string>
#include <system_error>
#include <thread>
#include <tuple>
#include <vector>

#include <INIReader.h>

Expand All @@ -42,7 +34,7 @@
#include "enter_device.hh"
#include "main.hh"
#include "optional_task.hh"
#include "paths.hh"
#include <paths.hh>

const auto DEFAULT_TIMEOUT =
std::chrono::duration<int, std::chrono::milliseconds::period>(100);
Expand Down Expand Up @@ -139,7 +131,7 @@ auto howdy_status(char *username, int status, const INIReader &config,
* @return Returns PAM_AUTHINFO_UNAVAIL if it shouldn't be enabled,
* PAM_SUCCESS otherwise
*/
auto check_enabled(const INIReader &config, const char* username) -> int {
auto check_enabled(const INIReader &config, const char *username) -> int {
// Stop executing if Howdy has been disabled in the config
if (config.GetBoolean("core", "disabled", false)) {
syslog(LOG_INFO, "Skipped authentication, Howdy is disabled");
Expand All @@ -148,8 +140,8 @@ auto check_enabled(const INIReader &config, const char* username) -> int {

// Stop if we're in a remote shell and configured to exit
if (config.GetBoolean("core", "abort_if_ssh", true)) {
if (getenv("SSH_CONNECTION") != nullptr ||
getenv("SSH_CLIENT") != nullptr || getenv("SSHD_OPTS") != nullptr) {
if (checkenv("SSH_CONNECTION") || checkenv("SSH_CLIENT") ||
checkenv("SSH_TTY") || checkenv("SSHD_OPTS")) {
syslog(LOG_INFO, "Skipped authentication, SSH session detected");
return PAM_AUTHINFO_UNAVAIL;
}
Expand Down
27 changes: 27 additions & 0 deletions howdy/src/pam/main.hh
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#ifndef MAIN_H_
#define MAIN_H_

#include <cstring>
#include <string>
#include <unistd.h>

enum class ConfirmationType { Unset, Howdy, Pam };

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

/**
* Check if an environment variable exists either in the environ array or using
* getenv.
* @param name The name of the environment variable.
* @return The value of the environment variable or nullptr if it doesn't exist
* or environ is nullptr.
* @note This function was created because `getenv` wasn't working properly in
* some contexts (like sudo).
*/
auto checkenv(const char *name) -> bool {
if (std::getenv(name) != nullptr) {
return true;
}

auto len = strlen(name);

for (char **env = environ; *env != nullptr; env++) {
if (strncmp(*env, name, len) == 0) {
return true;
}
}

return false;
}

#endif // MAIN_H_