1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
/*
SPDX-License-Identifier: GPL-2.0-or-later
SPDX-FileCopyrightText: 2000 Caldera Systems Inc.
SPDX-FileCopyrightText: 2020-2021 Harald Sitter <sitter@kde.org>
SPDX-FileContributor: Matthew Peterson <mpeterson@caldera.com>
*/
#include "smburl.h"
#include "smb-logsettings.h"
#include <KConfig>
#include <KIO/Global>
#include <QDir>
#include <QHostAddress>
#include <QUrlQuery>
SMBUrl::SMBUrl(const QUrl &kurl)
: QUrl(kurl)
{
// We treat cifs as an alias but need to translate it to smb.
// https://bugs.kde.org/show_bug.cgi?id=327295
// It's not IANA registered and also libsmbc internally expects
// smb URIs so we do very broadly coerce cifs to smb.
// Also see SMBWorker::checkURL.
if (scheme() == "cifs") {
setScheme("smb");
}
updateCache();
}
SMBUrl::SMBUrl() = default;
SMBUrl::SMBUrl(const SMBUrl &other) = default;
SMBUrl::~SMBUrl() = default;
SMBUrl &SMBUrl::operator=(const SMBUrl &) = default;
void SMBUrl::addPath(const QString &filedir)
{
if (path().length() > 0 && path().at(path().length() - 1) != QLatin1Char('/')) {
QUrl::setPath(path() + QLatin1Char('/') + filedir);
} else {
QUrl::setPath(path() + filedir);
}
updateCache();
}
void SMBUrl::cdUp()
{
setUrl(KIO::upUrl(*this).url());
updateCache();
}
void SMBUrl::updateCache()
{
QUrl::setPath(QDir::cleanPath(path()));
// SMB URLs are UTF-8 encoded
qCDebug(KIO_SMB_LOG) << "updateCache " << QUrl::path();
QUrl sambaUrl(*this);
const QHostAddress address(sambaUrl.host());
switch (address.protocol()) {
case QAbstractSocket::IPv6Protocol: {
// Convert to Windows IPv6 literal to bypass limitations in samba.
// https://bugzilla.samba.org/show_bug.cgi?id=14297
// https://docs.microsoft.com/en-us/windows/win32/api/winnetwk/nf-winnetwk-wnetaddconnection2a
// https://devblogs.microsoft.com/oldnewthing/20100915-00/?p=12863
// https://www.samba.org/~idra/code/nss-ipv6literal/README.html
// https://ipv6-literal.com
QString literal = address.toString();
literal.replace(':', '-'); // address
literal.replace('%', 's'); // scope
if (literal.front() == '-') {
// Special prefix for [::f] so it doesn't start with a dash.
literal.prepend('0');
}
if (literal.back() == '-') {
// Special suffix, also cannot end with a dash.
literal.append('0');
}
literal += ".ipv6-literal.net"; // reserved host host
qCDebug(KIO_SMB_LOG) << "converting IPv6 to literal " << host() << literal;
sambaUrl.setHost(literal);
break;
}
case QAbstractSocket::IPv4Protocol:
case QAbstractSocket::AnyIPProtocol:
case QAbstractSocket::UnknownNetworkLayerProtocol:
break;
}
// NetBios workgroup names may contain characters that QUrl will not
// allow in a host. Yet the SMB URI requires us to have the workgroup
// in the host field when browsing a workgroup.
// As a hacky workaround we'll not set a host but use a query param
// when encountering a workgroup that causes QUrl to error out.
// For libsmbc we then need to translate the query back to SMB URI.
// Since this is super daft string construction it will doubltlessly
// be imperfect and so we do still prefer deferring the string
// construction to QUrl whenever possible.
// https://support.microsoft.com/en-gb/help/909264/naming-conventions-in-active-directory-for-computers-domains-sites-and
// https://bugs.kde.org/show_bug.cgi?id=204423
//
// Should we ever stop supporting workgroup browsing this entire
// hack can be removed.
QUrlQuery query(sambaUrl);
const QString workgroup = query.queryItemValue("kio-workgroup");
if (workgroup.isEmpty()) {
// If we don't have a hack to apply we can simply defer to QUrl
if (sambaUrl.url() == "smb:/") {
m_surl = "smb://";
} else {
m_surl = sambaUrl.toString(QUrl::PrettyDecoded).toUtf8();
}
} else {
// If we have a workgroup hack to apply we need to manually construct
// the stringy URI.
query.removeQueryItem("kio-workgroup");
sambaUrl.setQuery(query);
QString url;
url = "smb://";
if (!sambaUrl.userInfo().isEmpty()) {
url += sambaUrl.userInfo() + "@";
}
url += workgroup;
// Workgroups can have ports per the IANA definition of smb.
if (sambaUrl.port() != -1) {
url += ':' + QString::number(sambaUrl.port());
}
// Make sure to only use clear paths. libsmbc is allergic to excess slashes.
QString path('/');
if (!sambaUrl.host().isEmpty()) {
path += sambaUrl.host();
}
if (!sambaUrl.path().isEmpty()) {
path += sambaUrl.path();
}
url += QDir::cleanPath(path);
if (!sambaUrl.query().isEmpty()) {
url += '?' + sambaUrl.query();
}
if (!sambaUrl.fragment().isEmpty()) {
url += '#' + sambaUrl.fragment();
}
m_surl = url.toUtf8();
}
m_type = SMBURLTYPE_UNKNOWN;
// update m_type
(void)getType();
}
SMBUrlType SMBUrl::getType() const
{
if (m_type != SMBURLTYPE_UNKNOWN)
return m_type;
if (scheme() != "smb") {
m_type = SMBURLTYPE_UNKNOWN;
return m_type;
}
if (QUrlQuery query(*this); query.queryItemValue("kio-printer") == "true") {
return m_type = SMBURLTYPE_PRINTER;
}
if (path().isEmpty() || path(QUrl::FullyDecoded) == "/") {
if (host().isEmpty() && !query().contains("kio-workgroup"))
m_type = SMBURLTYPE_ENTIRE_NETWORK;
else
m_type = SMBURLTYPE_WORKGROUP_OR_SERVER;
return m_type;
}
// Check for the path if we get this far
m_type = SMBURLTYPE_SHARE_OR_PATH;
return m_type;
}
SMBUrl SMBUrl::partUrl() const
{
const bool isRemoteFile = m_type == SMBURLTYPE_SHARE_OR_PATH && !fileName().isEmpty();
const bool isLocalFile = scheme() == QLatin1String("file") && !fileName().isEmpty();
// Mind that filename doesn't necessarily mean it is a file.
if (isRemoteFile || isLocalFile) {
SMBUrl url(*this);
url.setPath(path() + QLatin1String(".part"));
return url;
}
return SMBUrl();
}
|