summaryrefslogtreecommitdiffstats
path: root/smb4k/core/smb4kpasswordhandler.h
blob: e73caa7bbd1998ff7a6116bed954a4dc7e758f13 (plain)
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/***************************************************************************
    smb4kpasswordhandler  -  This class handles the passwords for Smb4K.
                             -------------------
    begin                : So Jan 16 2005
    copyright            : (C) 2005-2007 by Alexander Reinholdt
    email                : dustpuppy@users.berlios.de
 ***************************************************************************/

/***************************************************************************
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful, but   *
 *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
 *   General Public License for more details.                              *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,   *
 *   MA  02110-1301 USA                                                    *
 ***************************************************************************/

#ifndef SMB4KPASSWORDHANDLER_H
#define SMB4KPASSWORDHANDLER_H

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

// TQt includes
#include <tqobject.h>
#include <tqcheckbox.h>

// KDE includes
#include <tdewallet.h>
#include <kdialogbase.h>
#include <kprocess.h>

// forward declarations
#ifndef __FreeBSD__
class Smb4KAuthInfo;
#endif

// application specific includes
#include "smb4khomesshareshandler.h"
#ifdef __FreeBSD__
#include "smb4ksambaoptionshandler.h"
#include "smb4kauthinfo.h"
#endif



/**
 * This class handles the passwords used by Smb4K to authenticate to
 * the network shares.
 *
 * @author Alexander Reinholdt <dustpuppy@users.berlios.de>
 */

class Smb4KPasswordHandler : public TQObject
{
  Q_OBJECT
  

  public:
#ifndef __FreeBSD__
    /**
     * The constructor.
     *
     * @param config        The TDEConfig object that should be used
     *
     * @param handler       The Smb4KHomesSharesHandler object that is needed to retrieve
     *                      infos in case we need to look up authentication information for
     *                      'homes' shares.
     *
     * @param parent        The parent object.
     *
     * @param name          The name this class should carry.
     */
    Smb4KPasswordHandler( Smb4KHomesSharesHandler *handler,
                          TQObject *parent = 0,
                          const char *name = 0 );
#else
    /**
     * The constructor (FreeBSD).
     *
     * @param s_handler     The Smb4KHomesSharesHandler object that is needed to retrieve
     *                      infos in case we need to look up authentication information for
     *                      'homes' shares.
     *
     * @param o_handler     The Smb4KSambaOptionsHandler object. It is needed to write the
     *                      ~/.nsmbrc file correctly.
     *
     * @param parent        The parent object.
     *
     * @param name          The name this class should carry.
     */
    Smb4KPasswordHandler( Smb4KHomesSharesHandler *s_handler,
                          Smb4KSambaOptionsHandler *o_handler,
                          TQObject *parent = 0,
                          const char *name = 0 );
#endif

    /**
     * The destructor.
     */
    ~Smb4KPasswordHandler();

    /**
     * Opens the askpass dialog.
     *
     * @param workgroup     The workgroup where the share is located in (may be empty).
     *
     * @param host          The host the share belongs to.
     *
     * @param name          The name of the share.
     *
     * @param desc          Determines which descriptive label the askpass dialog should carry.
     *                      Values are taken from the Smb4KPasswordHandler::AskPass::Description
     *                      enumeration.
     *
     * @param parent        The parent of this dialog.
     *
     * @param name          The name of this dialog.
     *
     * @returns             TRUE if a new password has been supplied and FALSE otherwise.
     */
    bool askpass( const TQString &workgroup, const TQString &host, const TQString &share, int desc, TQWidget *parent = 0, const char *name = 0 );

    /**
     * Returns the authentication data that was found for the defined share. You need to
     * provide the workgroup, host name, and share name with @p authInfo.
     *
     * @param authInfo      The authentication information object.
     *
     * @returns             a pointer to @p authInfo. This may be NULL if @p authInfo was
     * also NULL.
     */
    Smb4KAuthInfo *readAuth( Smb4KAuthInfo *authInfo );

    /**
     * Write the provided authentication data to the wallet.
     *
     * @param authInfo      The Smb4KAuthInfo object that holds the authentication information.
     */
    void writeAuth( Smb4KAuthInfo *authInfo );

    /**
     * Read the default authentication from the wallet.
     *
     * @param authInfo      The authentication information object.
     *
     * @returns             pointer to the authentication data object if a wallet is used or NULL otherwise
     */
    Smb4KAuthInfo *readDefaultAuth( Smb4KAuthInfo *authInfo );

    /**
     * Writes the default authentication data to the wallet. This function seems only to
     * be useful for the configuration dialog.
     *
     * @param authInfo      The authentication data for the default login if a wallet is
     *                      used or NULL otherwise.
     */
    void writeDefaultAuth( Smb4KAuthInfo *authInfo );

    /**
     * This enumeration destermines which destriptive label the askpass
     * dialog should carry.
     */
    enum Description{ NewData, AccessDenied, BadPassword, PermDenied, AuthError, LogonFailure, None };

    /**
     * This function returns TRUE if the wallet it open at the moment and FALSE
     * if it is closed or not used.
     *
     * @returns TRUE if the wallet is open.
     */
    bool walletIsOpen() { return (m_wallet && m_wallet->isOpen()); }

    /**
     * This function returns TRUE if the digital wallet support has been
     * disabled. This happens when the wallet could not be opened.
     *
     * @returns TRUE if the digital wallet support has been disabled.
     */
    bool walletSupportIsDisabled() { return m_wallet_support_disabled; }

  protected slots:
    /**
     * Get the password for a specific share and user name.
     */
    void slotGetPassword( const TQString &username );

    /**
     * This slot is used to enable the OK button of the askpass
     * dialog when text has been entered in the text boxes and/or the
     * combo box.
     *
     * @param text          The text that has been entered.
     */
    void slotEnableOKButton( const TQString &text );

    /**
     * FreeBSD specific: This slot receives output from the process that encrypts
     * the passwords.
     *
     * @param proc          The process that sends the output
     *
     * @param buffer        The buffer that contains the output
     *
     * @param buflen        The length of the buffer
     */
    void slotReceivePassword( TDEProcess *proc, char *buffer, int buflen );

    /**
     * FreeBSD specific: This slot is invoked if the process exited.
     *
     * @param proc          The process that exited
     */
    void slotWritePassword( TDEProcess *proc );

  private:
    /**
     * This function opens the wallet in which the passwords should be
     * stored.
     */
    void open_close_wallet();

    /**
     * This function is used to convert old wallet entries to the new
     * map based ones.
     */
    void convert_old_entries();

    /**
     * The TDEWallet object.
     */
    TDEWallet::Wallet *m_wallet;

    /**
     * The Smb4KAuthInfo object. For use with the askpass dialog.
     */
    Smb4KAuthInfo *m_auth;

    /**
     * The askpass dialog.
     */
    KDialogBase *m_dlg;

    /**
     * This list holds the authentication data the user supplied if he/she
     * does not want to use TDEWallet.
     */
    TQValueList<Smb4KAuthInfo *> m_auth_list;

    /**
     * If the user neither wants to store the passwords in a wallet nor in a
     * temporary list, the authentication data received from the askpass dialog
     * will the stored in this object.
     */
    Smb4KAuthInfo *m_temp_auth;

    /**
     * The Smb4KHomesSharesHandler object
     */
    Smb4KHomesSharesHandler *m_handler;

    /**
     * Is the wallet support disabled?
     */
    bool m_wallet_support_disabled;

#ifdef __FreeBSD__

    /**
     * FreeBSD specific: This function writes authentication information
     * to the ~/.nsmbrc file, which is used by mount_smbfs.
     *
     * @param authInfo      The authentication information that should be written
     *                      to ~/.nsmbrc file.
     */
    void writeToSMBConfFile( Smb4KAuthInfo *authInfo );

    /**
     * FreeBSD specific: Holds the authentication information that should
     * be written to ~/.nsmbrc.
     */
    Smb4KAuthInfo m_nsmbrc_auth;

    /**
     * The buffer for the process that encrypts the password.
     */
    TQString m_buffer;

    /**
     * The Smb4KSambaOptionsHandler object.
     */
    Smb4KSambaOptionsHandler *m_options_handler;

#endif
};

#endif