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