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); |
Ed Tanous | fcc5aa6 | 2022-01-07 09:40:43 -0800 | [diff] [blame] | 60 | // NOLINTNEXTLINE(cppcoreguidelines-no-malloc) |
Ed Tanous | 46ff87b | 2022-01-07 09:25:51 -0800 | [diff] [blame] | 61 | void* ptr = calloc(numMsgSize, sizeof(struct pam_response)); |
P Dheeraj Srujan Kumar | ba95fcc | 2021-07-12 21:47:59 +0530 | [diff] [blame] | 62 | if (ptr == nullptr) |
| 63 | { |
Ed Tanous | fcc5aa6 | 2022-01-07 09:40:43 -0800 | [diff] [blame] | 64 | // NOLINTNEXTLINE(cppcoreguidelines-no-malloc) |
P Dheeraj Srujan Kumar | ba95fcc | 2021-07-12 21:47:59 +0530 | [diff] [blame] | 65 | free(pass); |
| 66 | return PAM_BUF_ERR; |
| 67 | } |
| 68 | |
Ed Tanous | 46ff87b | 2022-01-07 09:25:51 -0800 | [diff] [blame] | 69 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
P Dheeraj Srujan Kumar | ba95fcc | 2021-07-12 21:47:59 +0530 | [diff] [blame] | 70 | *resp = reinterpret_cast<pam_response*>(ptr); |
Ed Tanous | 46ff87b | 2022-01-07 09:25:51 -0800 | [diff] [blame] | 71 | |
Ed Tanous | ca45aa3 | 2022-01-07 09:28:45 -0800 | [diff] [blame] | 72 | // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 73 | resp[i]->resp = pass; |
P Dheeraj Srujan Kumar | ba95fcc | 2021-07-12 21:47:59 +0530 | [diff] [blame] | 74 | |
| 75 | return PAM_SUCCESS; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 76 | } |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 77 | |
P Dheeraj Srujan Kumar | ba95fcc | 2021-07-12 21:47:59 +0530 | [diff] [blame] | 78 | return PAM_CONV_ERR; |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 79 | } |
| 80 | |
Joseph Reynolds | d887fff | 2020-01-14 16:34:09 -0600 | [diff] [blame] | 81 | /** |
| 82 | * @brief Attempt username/password authentication via PAM. |
| 83 | * @param username The provided username aka account name. |
| 84 | * @param password The provided password. |
| 85 | * @returns PAM error code or PAM_SUCCESS for success. */ |
| 86 | inline int pamAuthenticateUser(const std::string_view username, |
| 87 | const std::string_view password) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 88 | { |
| 89 | std::string userStr(username); |
| 90 | std::string passStr(password); |
Ed Tanous | 4ecc618 | 2022-01-07 09:36:26 -0800 | [diff] [blame] | 91 | |
| 92 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) |
| 93 | char* passStrNoConst = const_cast<char*>(passStr.c_str()); |
| 94 | const struct pam_conv localConversation = {pamFunctionConversation, |
| 95 | passStrNoConst}; |
Ed Tanous | 99131cd | 2019-10-24 11:12:47 -0700 | [diff] [blame] | 96 | pam_handle_t* localAuthHandle = nullptr; // this gets set by pam_start |
Ed Tanous | f3d847c | 2017-06-12 16:01:42 -0700 | [diff] [blame] | 97 | |
Joseph Reynolds | d887fff | 2020-01-14 16:34:09 -0600 | [diff] [blame] | 98 | int retval = pam_start("webserver", userStr.c_str(), &localConversation, |
| 99 | &localAuthHandle); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 100 | if (retval != PAM_SUCCESS) |
| 101 | { |
Joseph Reynolds | d887fff | 2020-01-14 16:34:09 -0600 | [diff] [blame] | 102 | return retval; |
| 103 | } |
| 104 | |
| 105 | retval = pam_authenticate(localAuthHandle, |
| 106 | PAM_SILENT | PAM_DISALLOW_NULL_AUTHTOK); |
| 107 | if (retval != PAM_SUCCESS) |
| 108 | { |
| 109 | pam_end(localAuthHandle, PAM_SUCCESS); // ignore retval |
| 110 | return retval; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 111 | } |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 112 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 113 | /* check that the account is healthy */ |
Joseph Reynolds | d887fff | 2020-01-14 16:34:09 -0600 | [diff] [blame] | 114 | retval = pam_acct_mgmt(localAuthHandle, PAM_DISALLOW_NULL_AUTHTOK); |
| 115 | if (retval != PAM_SUCCESS) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 116 | { |
Joseph Reynolds | d887fff | 2020-01-14 16:34:09 -0600 | [diff] [blame] | 117 | pam_end(localAuthHandle, PAM_SUCCESS); // ignore retval |
| 118 | return retval; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 119 | } |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 120 | |
Joseph Reynolds | d887fff | 2020-01-14 16:34:09 -0600 | [diff] [blame] | 121 | return pam_end(localAuthHandle, PAM_SUCCESS); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 122 | } |
Ed Tanous | a840879 | 2018-09-05 16:08:38 -0700 | [diff] [blame] | 123 | |
jayaprakash Mutyala | 66b5ca7 | 2019-08-07 20:26:37 +0000 | [diff] [blame] | 124 | inline int pamUpdatePassword(const std::string& username, |
| 125 | const std::string& password) |
Ed Tanous | a840879 | 2018-09-05 16:08:38 -0700 | [diff] [blame] | 126 | { |
Ed Tanous | 4ecc618 | 2022-01-07 09:36:26 -0800 | [diff] [blame] | 127 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast) |
| 128 | char* passStrNoConst = const_cast<char*>(password.c_str()); |
| 129 | const struct pam_conv localConversation = {pamFunctionConversation, |
| 130 | passStrNoConst}; |
Ed Tanous | 99131cd | 2019-10-24 11:12:47 -0700 | [diff] [blame] | 131 | pam_handle_t* localAuthHandle = nullptr; // this gets set by pam_start |
Ed Tanous | a840879 | 2018-09-05 16:08:38 -0700 | [diff] [blame] | 132 | |
Joseph Reynolds | 96b39e0 | 2019-12-05 17:53:35 -0600 | [diff] [blame] | 133 | int retval = pam_start("webserver", username.c_str(), &localConversation, |
jayaprakash Mutyala | 66b5ca7 | 2019-08-07 20:26:37 +0000 | [diff] [blame] | 134 | &localAuthHandle); |
Ed Tanous | a840879 | 2018-09-05 16:08:38 -0700 | [diff] [blame] | 135 | |
| 136 | if (retval != PAM_SUCCESS) |
| 137 | { |
jayaprakash Mutyala | 66b5ca7 | 2019-08-07 20:26:37 +0000 | [diff] [blame] | 138 | return retval; |
Ed Tanous | a840879 | 2018-09-05 16:08:38 -0700 | [diff] [blame] | 139 | } |
| 140 | |
jayaprakash Mutyala | 66b5ca7 | 2019-08-07 20:26:37 +0000 | [diff] [blame] | 141 | retval = pam_chauthtok(localAuthHandle, PAM_SILENT); |
| 142 | if (retval != PAM_SUCCESS) |
Ed Tanous | a840879 | 2018-09-05 16:08:38 -0700 | [diff] [blame] | 143 | { |
jayaprakash Mutyala | 66b5ca7 | 2019-08-07 20:26:37 +0000 | [diff] [blame] | 144 | pam_end(localAuthHandle, PAM_SUCCESS); |
| 145 | return retval; |
Ed Tanous | a840879 | 2018-09-05 16:08:38 -0700 | [diff] [blame] | 146 | } |
| 147 | |
jayaprakash Mutyala | 66b5ca7 | 2019-08-07 20:26:37 +0000 | [diff] [blame] | 148 | return pam_end(localAuthHandle, PAM_SUCCESS); |
Ed Tanous | a840879 | 2018-09-05 16:08:38 -0700 | [diff] [blame] | 149 | } |