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