| 1 | /* Copyright (c) 2020, 2026, Oracle and/or its affiliates. |
| 2 | |
| 3 | This program is free software; you can redistribute it and/or modify |
| 4 | it under the terms of the GNU General Public License, version 2.0, |
| 5 | as published by the Free Software Foundation. |
| 6 | |
| 7 | This program is designed to work with certain software (including |
| 8 | but not limited to OpenSSL) that is licensed under separate terms, |
| 9 | as designated in a particular file or component or in included license |
| 10 | documentation. The authors of MySQL hereby grant you an additional |
| 11 | permission to link the program and your derivative works with the |
| 12 | separately licensed software that they have either included with |
| 13 | the program or referenced in the documentation. |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License, version 2.0, for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
| 21 | along with this program; if not, write to the Free Software |
| 22 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
| 23 | |
| 24 | #include "my_config.h" |
| 25 | |
| 26 | #include "auth_ldap_sasl_mechanism.h" |
| 27 | |
| 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| 31 | #ifndef _WIN32 |
| 32 | #include <lber.h> |
| 33 | #include <sasl/sasl.h> |
| 34 | #endif |
| 35 | |
| 36 | #if defined(KERBEROS_LIB_CONFIGURED) |
| 37 | extern Ldap_logger *g_logger_client; |
| 38 | using namespace auth_ldap_client_kerberos_context; |
| 39 | #else |
| 40 | Ldap_logger *g_logger_client = NULL; |
| 41 | #endif |
| 42 | |
| 43 | #if defined(KERBEROS_LIB_CONFIGURED) |
| 44 | Sasl_mechanism_kerberos::Sasl_mechanism_kerberos() = default; |
| 45 | |
| 46 | Sasl_mechanism_kerberos::~Sasl_mechanism_kerberos() = default; |
| 47 | |
| 48 | bool Sasl_mechanism_kerberos::pre_authentication() { |
| 49 | m_kerberos = std::unique_ptr<Kerberos>( |
| 50 | new Kerberos(m_user.c_str(), m_password.c_str())); |
| 51 | /** |
| 52 | Both user name and password are empty. |
| 53 | Existing TGT will be used for authentication. |
| 54 | Main user case. |
| 55 | */ |
| 56 | if (m_user.empty() && m_password.empty()) { |
| 57 | log_dbg( |
| 58 | "MySQL user name and password are empty. Existing TGT will be used for " |
| 59 | "authentication."); |
| 60 | return true; |
| 61 | } |
| 62 | /** |
| 63 | User name and password are not empty, Obtaining TGT from kerberos. |
| 64 | First time use case. |
| 65 | */ |
| 66 | if (!m_user.empty() && !m_password.empty()) { |
| 67 | log_dbg("Obtaining TGT from kerberos."); |
| 68 | return m_kerberos->obtain_store_credentials(); |
| 69 | } |
| 70 | /** |
| 71 | User name is not empty. |
| 72 | */ |
| 73 | if (!m_user.empty()) { |
| 74 | std::string user_name; |
| 75 | m_kerberos->get_user_name(&user_name); |
| 76 | /** |
| 77 | MySQL user name and kerberos default principle name is same. |
| 78 | Existing TGT will be used for authentication. |
| 79 | */ |
| 80 | if (user_name == m_user) { |
| 81 | log_dbg( |
| 82 | "MySQL user name and kerberos default principle name is same. " |
| 83 | "Existing TGT will be used for authentication."); |
| 84 | return true; |
| 85 | } else { |
| 86 | bool ret_val = false; |
| 87 | log_dbg( |
| 88 | "MySQL user name and kerberos default principle name is different. " |
| 89 | "Authentication will be aborted. "); |
| 90 | return ret_val; |
| 91 | } |
| 92 | } |
| 93 | /** |
| 94 | User has provided password but not user name, returning error. |
| 95 | */ |
| 96 | log_dbg( |
| 97 | "MySQL user name is empty but password is not empty. Authentication will " |
| 98 | "be aborted. "); |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | void Sasl_mechanism_kerberos::get_ldap_host(std::string &host) { |
| 103 | log_dbg("Sasl_mechanism_kerberos::get_ldap_host"); |
| 104 | if (!m_kerberos) return; |
| 105 | m_kerberos->get_ldap_host(host); |
| 106 | } |
| 107 | |
| 108 | #endif |
| 109 | |
| 110 | void Sasl_mechanism::set_user_info(std::string user, std::string password) { |
| 111 | m_user = user; |
| 112 | m_password = password; |
| 113 | } |
| 114 | |
| 115 | Sasl_mechanism::Sasl_mechanism() = default; |
| 116 | |
| 117 | Sasl_mechanism::~Sasl_mechanism() = default; |
| 118 | |
| 119 | bool Sasl_mechanism::pre_authentication() { return true; } |
| 120 | |
| 121 | void Sasl_mechanism::get_ldap_host(std::string &host) { host = ""; } |