File indexing completed on 2024-04-28 04:58:01

0001 /*
0002     SPDX-License-Identifier: GPL-2.0-or-later
0003     SPDX-FileCopyrightText: 2000 Caldera Systems Inc.
0004     SPDX-FileCopyrightText: 2020 Harald Sitter <sitter@kde.org>
0005     SPDX-FileContributor: Matthew Peterson <mpeterson@caldera.com>
0006 */
0007 
0008 #include "smbauthenticator.h"
0009 
0010 #include <KConfig>
0011 #include <KConfigGroup>
0012 #include <KIO/AuthInfo>
0013 #include <KLocalizedString>
0014 #include <QTextCodec>
0015 
0016 #include "smb-logsettings.h"
0017 #include "smburl.h"
0018 
0019 SMBAuthenticator::SMBAuthenticator(SMBAbstractFrontend &frontend)
0020     : m_frontend(frontend)
0021 {
0022 }
0023 
0024 QString SMBAuthenticator::defaultWorkgroup() const
0025 {
0026     return m_defaultWorkgroup;
0027 }
0028 
0029 void SMBAuthenticator::setDefaultWorkgroup(const QString &workGroup)
0030 {
0031     m_defaultWorkgroup = workGroup;
0032 }
0033 
0034 void SMBAuthenticator::auth(SMBCCTX *context,
0035                             const char *server,
0036                             const char *share,
0037                             char *workgroup,
0038                             int wgmaxlen,
0039                             char *username,
0040                             int unmaxlen,
0041                             char *password,
0042                             int pwmaxlen)
0043 {
0044     qCDebug(KIO_SMB_LOG) << "auth_smbc_get_dat: set user=" << username << ", workgroup=" << workgroup << " server=" << server << ", share=" << share;
0045 
0046     QString s_server = QString::fromUtf8(server);
0047     QString s_share = QString::fromUtf8(share);
0048     workgroup[wgmaxlen - 1] = 0;
0049     QString s_workgroup = QString::fromUtf8(workgroup);
0050     username[unmaxlen - 1] = 0;
0051     QString s_username = QString::fromUtf8(username);
0052     password[pwmaxlen - 1] = 0;
0053     QString s_password = QString::fromUtf8(password);
0054 
0055     KIO::AuthInfo info;
0056     info.url = QUrl("smb:///");
0057     info.url.setHost(s_server);
0058     info.url.setPath('/' + s_share);
0059 
0060     // check this to see if we "really" need to authenticate...
0061     if (SMBUrl(info.url).getType() == SMBURLTYPE_ENTIRE_NETWORK) {
0062         qCDebug(KIO_SMB_LOG) << "we don't really need to authenticate for this top level url, returning";
0063         return;
0064     }
0065 
0066     info.username = s_username;
0067     info.password = s_password;
0068     info.verifyPath = true;
0069 
0070     qCDebug(KIO_SMB_LOG) << "libsmb-auth-callback URL:" << info.url;
0071 
0072     // NOTE: By suggestion from upstream we do not default to any amount of
0073     //   anonymous/guest logins as it's not safe to do in many environments:
0074     //   https://bugzilla.samba.org/show_bug.cgi?id=14326
0075 
0076     if (m_frontend.checkCachedAuthentication(info)) {
0077         qCDebug(KIO_SMB_LOG) << "got password through cache" << info.username;
0078     } else if (!m_defaultUser.isEmpty()) {
0079         // user defined a default username/password in kcontrol; try this
0080         info.username = m_defaultUser;
0081         info.password = m_defaultPassword;
0082         qCDebug(KIO_SMB_LOG) << "trying defaults for user" << info.username;
0083     }
0084 
0085     // Make sure it'll be safe to cast to size_t (unsigned)
0086     Q_ASSERT(unmaxlen > 0);
0087     Q_ASSERT(pwmaxlen > 0);
0088 
0089     strncpy(username, info.username.toUtf8(), static_cast<size_t>(unmaxlen - 1));
0090     strncpy(password, info.password.toUtf8(), static_cast<size_t>(pwmaxlen - 1));
0091 
0092     smbc_set_credentials_with_fallback(context, workgroup, username, password);
0093 }