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

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 "kio_smb.h"
0009 #include "smburl.h"
0010 
0011 #include <KIO/AuthInfo>
0012 #include <KLocalizedString>
0013 #include <cstdlib>
0014 
0015 int SMBWorker::checkPassword(SMBUrl &url)
0016 {
0017     qCDebug(KIO_SMB_LOG) << "checkPassword for " << url;
0018 
0019     KIO::AuthInfo info;
0020     info.url = QUrl("smb:///");
0021     info.url.setHost(url.host());
0022 
0023     QString share = url.path();
0024     int index = share.indexOf('/', 1);
0025     if (index > 1) {
0026         share = share.left(index);
0027     }
0028     if (share.at(0) == '/') {
0029         share = share.mid(1);
0030     }
0031     info.url.setPath('/' + share);
0032     info.verifyPath = true;
0033     info.keepPassword = true;
0034 
0035     // By suggestion from upstream we do not split login variants through the UI but rather expect different
0036     // username inputs. This is also more in line with how logon works in windows.
0037     // https://bugzilla.samba.org/show_bug.cgi?id=14326
0038     // https://docs.microsoft.com/en-us/windows/win32/secauthn/user-name-formats
0039     info.setExtraField(
0040         QStringLiteral("username-context-help"),
0041         xi18nc("@info:whatsthis",
0042                "<para>There are various options for authenticating on SMB shares.</para>"
0043                "<para><placeholder>username</placeholder>: When authenticating within a home network the username on the server is sufficient</para>"
0044                "<para><placeholder>username@domain.com</placeholder>: Modern corporate logon names are formed like e-mail addresses</para>"
0045                "<para><placeholder>DOMAIN\\username</placeholder>: For ancient corporate networks or workgroups you may need to prefix the NetBIOS domain name "
0046                "(pre-Windows 2000)</para>"
0047                "<para><placeholder>anonymous</placeholder>: Anonymous logins can be attempted using empty username and password. Depending on server "
0048                "configuration non-empty usernames may be required</para>"));
0049 
0050     if (share.isEmpty()) {
0051         info.prompt = i18n("<qt>Please enter authentication information for <b>%1</b></qt>", url.host());
0052     } else {
0053         info.prompt = i18n(
0054             "Please enter authentication information for:\n"
0055             "Server = %1\n"
0056             "Share = %2",
0057             url.host(),
0058             share);
0059     }
0060 
0061     info.username = url.userName();
0062     qCDebug(KIO_SMB_LOG) << "call openPasswordDialog for " << info.url;
0063 
0064     const int passwordDialogErrorCode = openPasswordDialog(info);
0065     if (passwordDialogErrorCode == KJob::NoError) {
0066         qCDebug(KIO_SMB_LOG) << "openPasswordDialog returned " << info.username;
0067         url.setUser(info.username);
0068 
0069         if (info.keepPassword) {
0070             qCDebug(KIO_SMB_LOG) << "Caching info.username = " << info.username << ", info.url = " << info.url.toDisplayString();
0071             cacheAuthentication(info);
0072         }
0073 
0074         return KJob::NoError;
0075     }
0076     qCDebug(KIO_SMB_LOG) << "no value from openPasswordDialog; error:" << passwordDialogErrorCode;
0077     return passwordDialogErrorCode;
0078 }