Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 3 | #include <security/pam_appl.h> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 4 | |
| 5 | #include <boost/utility/string_view.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 6 | |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 7 | #include <cstring> |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 8 | #include <memory> |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 9 | |
| 10 | // function used to get user input |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 11 | inline int pamFunctionConversation(int numMsg, const struct pam_message** msg, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 12 | struct pam_response** resp, void* appdataPtr) |
| 13 | { |
| 14 | if (appdataPtr == nullptr) |
| 15 | { |
P Dheeraj Srujan Kumar | ba95fcc | 2021-07-12 21:47:59 +0530 | [diff] [blame] | 16 | return PAM_CONV_ERR; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 17 | } |
P Dheeraj Srujan Kumar | ba95fcc | 2021-07-12 21:47:59 +0530 | [diff] [blame] | 18 | |
| 19 | if (numMsg <= 0 || numMsg >= PAM_MAX_NUM_MSG) |
Ed Tanous | f1eebf0 | 2019-03-04 15:57:09 -0800 | [diff] [blame] | 20 | { |
P Dheeraj Srujan Kumar | ba95fcc | 2021-07-12 21:47:59 +0530 | [diff] [blame] | 21 | return PAM_CONV_ERR; |
Ed Tanous | f1eebf0 | 2019-03-04 15:57:09 -0800 | [diff] [blame] | 22 | } |
| 23 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 24 | for (int i = 0; i < numMsg; ++i) |
| 25 | { |
| 26 | /* Ignore all PAM messages except prompting for hidden input */ |
Ed Tanous | ca45aa3 | 2022-01-07 09:28:45 -0800 | [diff] [blame] | 27 | // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 28 | if (msg[i]->msg_style != PAM_PROMPT_ECHO_OFF) |
| 29 | { |
| 30 | continue; |
| 31 | } |
| 32 | |
| 33 | /* Assume PAM is only prompting for the password as hidden input */ |
P Dheeraj Srujan Kumar | ba95fcc | 2021-07-12 21:47:59 +0530 | [diff] [blame] | 34 | /* Allocate memory only when PAM_PROMPT_ECHO_OFF is encounterred */ |
| 35 | |
Ed Tanous | 46ff87b | 2022-01-07 09:25:51 -0800 | [diff] [blame] | 36 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
P Dheeraj Srujan Kumar | ba95fcc | 2021-07-12 21:47:59 +0530 | [diff] [blame] | 37 | char* appPass = reinterpret_cast<char*>(appdataPtr); |
| 38 | size_t appPassSize = std::strlen(appPass); |
| 39 | |
| 40 | if ((appPassSize + 1) > PAM_MAX_RESP_SIZE) |
| 41 | { |
| 42 | return PAM_CONV_ERR; |
| 43 | } |
Ed Tanous | 46ff87b | 2022-01-07 09:25:51 -0800 | [diff] [blame] | 44 | // IDeally we'd like to avoid using malloc here, but because we're |
| 45 | // passing off ownership of this to a C application, there aren't a lot |
| 46 | // of sane ways to avoid it. |
P Dheeraj Srujan Kumar | ba95fcc | 2021-07-12 21:47:59 +0530 | [diff] [blame] | 47 | |
Ed Tanous | 46ff87b | 2022-01-07 09:25:51 -0800 | [diff] [blame] | 48 | // NOLINTNEXTLINE(cppcoreguidelines-no-malloc)' |
| 49 | void* passPtr = malloc(appPassSize + 1); |
| 50 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
| 51 | char* pass = reinterpret_cast<char*>(passPtr); |
P Dheeraj Srujan Kumar | ba95fcc | 2021-07-12 21:47:59 +0530 | [diff] [blame] | 52 | if (pass == nullptr) |
| 53 | { |
| 54 | return PAM_BUF_ERR; |
| 55 | } |
| 56 | |
| 57 | std::strncpy(pass, appPass, appPassSize + 1); |
| 58 | |
Ed Tanous | 46ff87b | 2022-01-07 09:25:51 -0800 | [diff] [blame] | 59 | size_t numMsgSize = static_cast<size_t>(numMsg); |
| 60 | void* ptr = calloc(numMsgSize, sizeof(struct pam_response)); |
P Dheeraj Srujan Kumar | ba95fcc | 2021-07-12 21:47:59 +0530 | [diff] [blame] | 61 | if (ptr == nullptr) |
| 62 | { |
| 63 | free(pass); |
| 64 | return PAM_BUF_ERR; |
| 65 | } |
| 66 | |
Ed Tanous | 46ff87b | 2022-01-07 09:25:51 -0800 | [diff] [blame] | 67 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
P Dheeraj Srujan Kumar | ba95fcc | 2021-07-12 21:47:59 +0530 | [diff] [blame] | 68 | *resp = reinterpret_cast<pam_response*>(ptr); |
Ed Tanous | 46ff87b | 2022-01-07 09:25:51 -0800 | [diff] [blame] | 69 | |
Ed Tanous | ca45aa3 | 2022-01-07 09:28:45 -0800 | [diff] [blame] | 70 | // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 71 | resp[i]->resp = pass; |
P Dheeraj Srujan Kumar | ba95fcc | 2021-07-12 21:47:59 +0530 | [diff] [blame] | 72 | |
| 73 | return PAM_SUCCESS; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 74 | } |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 75 | |
P Dheeraj Srujan Kumar | ba95fcc | 2021-07-12 21:47:59 +0530 | [diff] [blame] | 76 | return PAM_CONV_ERR; |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Joseph Reynolds | d887fff | 2020-01-14 16:34:09 -0600 | [diff] [blame] | 79 | /** |
| 80 | * @brief Attempt username/password authentication via PAM. |
| 81 | * @param username The provided username aka account name. |
| 82 | * @param password The provided password. |
| 83 | * @returns PAM error code or PAM_SUCCESS for success. */ |
| 84 | inline int pamAuthenticateUser(const std::string_view username, |
| 85 | const std::string_view password) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 86 | { |
| 87 | std::string userStr(username); |
| 88 | std::string passStr(password); |
Ed Tanous | 4ecc618 | 2022-01-07 09:36:26 -0800 | [diff] [blame^] | 89 | |
| 90 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) |
| 91 | char* passStrNoConst = const_cast<char*>(passStr.c_str()); |
| 92 | const struct pam_conv localConversation = {pamFunctionConversation, |
| 93 | passStrNoConst}; |
Ed Tanous | 99131cd | 2019-10-24 11:12:47 -0700 | [diff] [blame] | 94 | pam_handle_t* localAuthHandle = nullptr; // this gets set by pam_start |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 95 | |
Joseph Reynolds | d887fff | 2020-01-14 16:34:09 -0600 | [diff] [blame] | 96 | int retval = pam_start("webserver", userStr.c_str(), &localConversation, |
| 97 | &localAuthHandle); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 98 | if (retval != PAM_SUCCESS) |
| 99 | { |
Joseph Reynolds | d887fff | 2020-01-14 16:34:09 -0600 | [diff] [blame] | 100 | return retval; |
| 101 | } |
| 102 | |
| 103 | retval = pam_authenticate(localAuthHandle, |
| 104 | PAM_SILENT | PAM_DISALLOW_NULL_AUTHTOK); |
| 105 | if (retval != PAM_SUCCESS) |
| 106 | { |
| 107 | pam_end(localAuthHandle, PAM_SUCCESS); // ignore retval |
| 108 | return retval; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 109 | } |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 110 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 111 | /* check that the account is healthy */ |
Joseph Reynolds | d887fff | 2020-01-14 16:34:09 -0600 | [diff] [blame] | 112 | retval = pam_acct_mgmt(localAuthHandle, PAM_DISALLOW_NULL_AUTHTOK); |
| 113 | if (retval != PAM_SUCCESS) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 114 | { |
Joseph Reynolds | d887fff | 2020-01-14 16:34:09 -0600 | [diff] [blame] | 115 | pam_end(localAuthHandle, PAM_SUCCESS); // ignore retval |
| 116 | return retval; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 117 | } |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 118 | |
Joseph Reynolds | d887fff | 2020-01-14 16:34:09 -0600 | [diff] [blame] | 119 | return pam_end(localAuthHandle, PAM_SUCCESS); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 120 | } |
Ed Tanous | a840879 | 2018-09-05 16:08:38 -0700 | [diff] [blame] | 121 | |
jayaprakash Mutyala | 66b5ca7 | 2019-08-07 20:26:37 +0000 | [diff] [blame] | 122 | inline int pamUpdatePassword(const std::string& username, |
| 123 | const std::string& password) |
Ed Tanous | a840879 | 2018-09-05 16:08:38 -0700 | [diff] [blame] | 124 | { |
Ed Tanous | 4ecc618 | 2022-01-07 09:36:26 -0800 | [diff] [blame^] | 125 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) |
| 126 | char* passStrNoConst = const_cast<char*>(password.c_str()); |
| 127 | const struct pam_conv localConversation = {pamFunctionConversation, |
| 128 | passStrNoConst}; |
Ed Tanous | 99131cd | 2019-10-24 11:12:47 -0700 | [diff] [blame] | 129 | pam_handle_t* localAuthHandle = nullptr; // this gets set by pam_start |
Ed Tanous | a840879 | 2018-09-05 16:08:38 -0700 | [diff] [blame] | 130 | |
Joseph Reynolds | 96b39e0 | 2019-12-05 17:53:35 -0600 | [diff] [blame] | 131 | int retval = pam_start("webserver", username.c_str(), &localConversation, |
jayaprakash Mutyala | 66b5ca7 | 2019-08-07 20:26:37 +0000 | [diff] [blame] | 132 | &localAuthHandle); |
Ed Tanous | a840879 | 2018-09-05 16:08:38 -0700 | [diff] [blame] | 133 | |
| 134 | if (retval != PAM_SUCCESS) |
| 135 | { |
jayaprakash Mutyala | 66b5ca7 | 2019-08-07 20:26:37 +0000 | [diff] [blame] | 136 | return retval; |
Ed Tanous | a840879 | 2018-09-05 16:08:38 -0700 | [diff] [blame] | 137 | } |
| 138 | |
jayaprakash Mutyala | 66b5ca7 | 2019-08-07 20:26:37 +0000 | [diff] [blame] | 139 | retval = pam_chauthtok(localAuthHandle, PAM_SILENT); |
| 140 | if (retval != PAM_SUCCESS) |
Ed Tanous | a840879 | 2018-09-05 16:08:38 -0700 | [diff] [blame] | 141 | { |
jayaprakash Mutyala | 66b5ca7 | 2019-08-07 20:26:37 +0000 | [diff] [blame] | 142 | pam_end(localAuthHandle, PAM_SUCCESS); |
| 143 | return retval; |
Ed Tanous | a840879 | 2018-09-05 16:08:38 -0700 | [diff] [blame] | 144 | } |
| 145 | |
jayaprakash Mutyala | 66b5ca7 | 2019-08-07 20:26:37 +0000 | [diff] [blame] | 146 | return pam_end(localAuthHandle, PAM_SUCCESS); |
Ed Tanous | a840879 | 2018-09-05 16:08:38 -0700 | [diff] [blame] | 147 | } |