kio_smb.h 8.25 KB
Newer Older
1
2
3
4
5
/*
    SPDX-License-Identifier: GPL-2.0-or-later
    SPDX-FileCopyrightText: 2000 Caldera Systems, Inc.
    SPDX-FileContributor: Matthew Peterson <mpeterson@caldera.com>
*/
6
7
8
9

#ifndef KIO_SMB_H_INCLUDED
#define KIO_SMB_H_INCLUDED

10
#include <config-smb.h>
11

Méven Car's avatar
Méven Car committed
12
13
#include "smb-logsettings.h"

14
15
16
17
18
19
20
21
22
//--------------
// KDE includes
//--------------
#include <kio/global.h>
#include <kio/slavebase.h>

//-----------------------------
// Standard C library includes
//-----------------------------
23
24
25
26
27
#include <arpa/inet.h>
#include <errno.h>
#include <net/if.h>
#include <netinet/in.h>
#include <stdio.h>
28
29
30
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
31
#include <sys/stat.h>
32
#include <time.h>
33
34
35
36
37

//-----------------------------
// Qt includes
//-----------------------------

38
#include <QLoggingCategory>
39
#include <QObject>
40
#include <QUrl>
41
42

//-------------------------------
43
// Samba client library includes
44
//-------------------------------
45
extern "C" {
46
47
#include <libsmbclient.h>
}
Stephan Kulow's avatar
Stephan Kulow committed
48

49
50
51
//---------------------------
// kio_smb internal includes
//---------------------------
52
#include "smburl.h"
53
54
#include "smbauthenticator.h"
#include "smbcontext.h"
55

Stephan Kulow's avatar
Stephan Kulow committed
56
using namespace KIO;
57
58
59
60
61
62
63
64
65
66
class SMBSlave;

class SlaveFrontend : public SMBAbstractFrontend
{
public:
    SlaveFrontend(SMBSlave &slave);
    bool checkCachedAuthentication(AuthInfo &info) override;
private:
    SMBSlave &m_slave;
};
67

68
class SMBSlave : public QObject, public KIO::SlaveBase
69
{
70
    Q_OBJECT
Harald Sitter's avatar
Harald Sitter committed
71
    friend class SMBCDiscoverer;
72
73
    SlaveFrontend m_frontend { *this };
    SMBContext m_context { new SMBAuthenticator(m_frontend) };
74

75
private:
76

77
78
    class SMBError
    {
79
80
81
82
83
    public:
        int kioErrorId;
        QString errorString;
    };

Frank Schwanz's avatar
Frank Schwanz committed
84
    /**
Toshitaka Fujioka's avatar
i18n    
Toshitaka Fujioka committed
85
     * we store the current url, it's needed for
John Layt's avatar
John Layt committed
86
     * callback authorization method
Frank Schwanz's avatar
Frank Schwanz committed
87
     */
88
    SMBUrl m_current_url;
Frank Schwanz's avatar
Frank Schwanz committed
89
90

    /**
Toshitaka Fujioka's avatar
i18n    
Toshitaka Fujioka committed
91
     * From Controlcenter, show SHARE$ or not
Frank Schwanz's avatar
Frank Schwanz committed
92
     */
93
    //    bool m_showHiddenShares;     //currently unused, Alex <neundorf@kde.org>
94

Frank Schwanz's avatar
Frank Schwanz committed
95
96
97
    /**
     * libsmbclient need global variables to store in,
     * else it crashes on exit next method after use cache_stat,
Stephan Kulow's avatar
Stephan Kulow committed
98
     * looks like gcc (C/C++) failure
Frank Schwanz's avatar
Frank Schwanz committed
99
     */
100
101
    struct stat st {
    };
102

103
104
protected:
    //---------------------------------------------
Stephan Kulow's avatar
Stephan Kulow committed
105
    // Authentication functions (kio_smb_auth.cpp)
106
107
    //---------------------------------------------
    // (please prefix functions with auth)
Frank Schwanz's avatar
Frank Schwanz committed
108
109

    /**
John Layt's avatar
John Layt committed
110
     * Description :   Initializes the libsmbclient
111
     * Return :        true on success false with errno set on error
Frank Schwanz's avatar
Frank Schwanz committed
112
     */
113
    bool auth_initialize_smbc();
114

115
    int checkPassword(SMBUrl &url);
116

117
118
119
120
    //---------------------------------------------
    // Cache functions (kio_smb_auth.cpp)
    //---------------------------------------------

121
    // Stat methods
Frank Schwanz's avatar
Frank Schwanz committed
122

123
    //-----------------------------------------
Stephan Kulow's avatar
Stephan Kulow committed
124
    // Browsing functions (kio_smb_browse.cpp)
125
126
    //-----------------------------------------
    // (please prefix functions with browse)
Frank Schwanz's avatar
Frank Schwanz committed
127
128

    /**
Stephan Kulow's avatar
Stephan Kulow committed
129
     * Description :  Return a stat of given SMBUrl. Calls cache_stat and
Frank Schwanz's avatar
Frank Schwanz committed
130
131
     *                pack it in UDSEntry. UDSEntry will not be cleared
     * Parameter :    SMBUrl the url to stat
132
     * Return :       cache_stat() return code
Frank Schwanz's avatar
Frank Schwanz committed
133
     */
134
    int browse_stat_path(const SMBUrl &url, UDSEntry &udsentry);
Stephan Kulow's avatar
Stephan Kulow committed
135

136
137
138
139
140
141
142
143
144
    /**
     * Description :  call smbc_stat and return stats of the url
     * Parameter :    SMBUrl the url to stat
     * Return :       stat* of the url
     * Note :         it has some problems with stat in method, looks like
     *                something leave(or removed) on the stack. If your
     *                method segfault on returning try to change the stat*
     *                variable
     */
Harald Sitter's avatar
style++    
Harald Sitter committed
145
    static int cache_stat(const SMBUrl &url, struct stat *st);
146

147
    //---------------------------------------------
Stephan Kulow's avatar
Stephan Kulow committed
148
    // Configuration functions (kio_smb_config.cpp)
149
150
    //---------------------------------------------
    // (please prefix functions with config)
Stephan Kulow's avatar
Stephan Kulow committed
151

152
    //---------------------------------------
Stephan Kulow's avatar
Stephan Kulow committed
153
    // Directory functions (kio_smb_dir.cpp)
154
155
156
157
    //---------------------------------------
    // (please prefix functions with dir)

    //--------------------------------------
Stephan Kulow's avatar
Stephan Kulow committed
158
    // File IO functions (kio_smb_file.cpp)
159
160
161
162
163
164
    //--------------------------------------
    // (please prefix functions with file)

    //----------------------------
    // Misc functions (this file)
    //----------------------------
Frank Schwanz's avatar
Frank Schwanz committed
165
166
167
168

    /**
     * Description :  correct a given URL
     *                valid URL's are
Stephan Kulow's avatar
Stephan Kulow committed
169
     *
Frank Schwanz's avatar
Frank Schwanz committed
170
171
172
173
174
175
176
177
178
     *                smb://[[domain;]user[:password]@]server[:port][/share[/path[/file]]]
     *                smb:/[[domain;]user[:password]@][group/[server[/share[/path[/file]]]]]
     *                domain   = workgroup(domain) of the user
     *                user     = username
     *                password = password of useraccount
     *                group    = workgroup(domain) of server
     *                server   = host to connect
     *                share    = a share of the server (host)
     *                path     = a path of the share
179
180
     * Parameter :    QUrl the url to check
     * Return :       new QUrl if it is corrected. else the same QUrl
Frank Schwanz's avatar
Frank Schwanz committed
181
     */
182
    QUrl checkURL(const QUrl &kurl) const;
183

184
185
    void reportError(const SMBUrl &url, const int errNum);
    void reportWarning(const SMBUrl &url, const int errNum);
186

187
188
189
public:
    //-----------------------------------------------------------------------
    // Overwritten functions from the base class that define the operation of
Stephan Kulow's avatar
Stephan Kulow committed
190
    // this slave. (See the base class headerfile slavebase.h for more
191
192
193
194
    // details)
    //-----------------------------------------------------------------------

    // Functions overwritten in kio_smb.cpp
195
    SMBSlave(const QByteArray &pool, const QByteArray &app);
Friedrich W. H. Kossebau's avatar
Friedrich W. H. Kossebau committed
196
    ~SMBSlave() override;
Stephan Kulow's avatar
Stephan Kulow committed
197

198
    // Functions overwritten in kio_smb_browse.cpp
199
200
    void listDir(const QUrl &url) override;
    void stat(const QUrl &url) override;
201
202

    // Functions overwritten in kio_smb_config.cpp
Friedrich W. H. Kossebau's avatar
Friedrich W. H. Kossebau committed
203
    void reparseConfiguration() override;
204
205

    // Functions overwritten in kio_smb_dir.cpp
206
207
208
209
    void copy(const QUrl &src, const QUrl &dst, int permissions, KIO::JobFlags flags) override;
    void del(const QUrl &kurl, bool isfile) override;
    void mkdir(const QUrl &kurl, int permissions) override;
    void rename(const QUrl &src, const QUrl &dest, KIO::JobFlags flags) override;
210
211

    // Functions overwritten in kio_smb_file.cpp
212
213
214
215
216
217
218
    void get(const QUrl &kurl) override;
    void put(const QUrl &kurl, int permissions, KIO::JobFlags flags) override;
    void open(const QUrl &kurl, QIODevice::OpenMode mode) override;
    void read(KIO::filesize_t bytesRequested) override;
    void write(const QByteArray &fileData) override;
    void seek(KIO::filesize_t offset) override;
    void truncate(KIO::filesize_t length);
Friedrich W. H. Kossebau's avatar
Friedrich W. H. Kossebau committed
219
    void close() override;
220
221

    // Functions not implemented  (yet)
222
223
224
225
226
    // virtual void setHost(const QString& host, int port, const QString& user, const QString& pass);
    // virtual void openConnection();
    // virtual void closeConnection();
    // virtual void slave_status();
    void special(const QByteArray &) override;
227

228
protected:
Kevin Funk's avatar
Kevin Funk committed
229
    void virtual_hook(int id, void *data) override;
230

231
private:
232
233
234
235
    SMBError errnumToKioError(const SMBUrl &url, const int errNum);
    void smbCopy(const QUrl &src, const QUrl &dst, int permissions, KIO::JobFlags flags);
    void smbCopyGet(const QUrl &ksrc, const QUrl &kdst, int permissions, KIO::JobFlags flags);
    void smbCopyPut(const QUrl &ksrc, const QUrl &kdst, int permissions, KIO::JobFlags flags);
236
    bool workaroundEEXIST(const int errNum) const;
237
    int statToUDSEntry(const QUrl &url, const struct stat &st, KIO::UDSEntry &udsentry);
238
239
    void fileSystemFreeSpace(const QUrl &url);

240
    /**
241
242
243
244
245
246
     * Used in open(), read(), write(), and close()
     * FIXME Placing these in the private section above causes m_openUrl = kurl
     * to fail in SMBSlave::open. Need to find out why this is.
     */
    int m_openFd;
    SMBUrl m_openUrl;
247
248

    const bool m_enableEEXISTWorkaround; /* Enables a workaround for some broken libsmbclient versions */
249
250
    // Close without calling finish(). Use this to close after error.
    void closeWithoutFinish();
251
252
253
254
};

//===========================================================================
// Main slave entrypoint (see kio_smb.cpp)
255
256
extern "C" {
int kdemain(int argc, char **argv);
257
258
}

259
#endif //#endif KIO_SMB_H_INCLUDED