1 /////////////////////////////////////////////////////////////////////////////
3 // Project: SMB kioslave for KDE2
5 // File: kio_smb_auth.cpp
7 // Abstract: member function implementations for SMBSlave that deal with
8 // SMB directory access
10 // Author(s): Matthew Peterson <mpeterson@caldera.com>
12 //---------------------------------------------------------------------------
14 // Copyright (c) 2000 Caldera Systems, Inc.
16 // This program is free software; you can redistribute it and/or modify it
17 // under the terms of the GNU General Public License as published by the
18 // Free Software Foundation; either version 2.1 of the License, or
19 // (at your option) any later version.
21 // This program is distributed in the hope that it will be useful,
22 // but WITHOUT ANY WARRANTY; without even the implied warranty of
23 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 // GNU Lesser General Public License for more details.
26 // You should have received a copy of the GNU General Public License
27 // along with this program; see the file COPYING. If not, please obtain
28 // a copy from http://www.gnu.org/copyleft/gpl.html
30 /////////////////////////////////////////////////////////////////////////////
33 #include "kio_smb_internal.h"
36 #include <kconfiggroup.h>
40 // call for libsmbclient
41 //==========================================================================
42 void auth_smbc_get_data(SMBCCTX
* context
,
43 const char *server
,const char *share
,
44 char *workgroup
, int wgmaxlen
,
45 char *username
, int unmaxlen
,
46 char *password
, int pwmaxlen
)
47 //==========================================================================
49 if (context
!= NULL
) {
50 SMBSlave
*theSlave
= (SMBSlave
*)smbc_option_get(context
, "user_data");
51 theSlave
->auth_smbc_get_data(server
, share
,
59 //--------------------------------------------------------------------------
60 void SMBSlave::auth_smbc_get_data(const char *server
,const char *share
,
61 char *workgroup
, int wgmaxlen
,
62 char *username
, int unmaxlen
,
63 char *password
, int pwmaxlen
)
64 //--------------------------------------------------------------------------
66 //check this to see if we "really" need to authenticate...
67 SMBUrlType t
= m_current_url
.getType();
68 if( t
== SMBURLTYPE_ENTIRE_NETWORK
)
70 kDebug(KIO_SMB
) << "we don't really need to authenticate for this top level url, returning";
73 kDebug(KIO_SMB
) << "AAAAAAAAAAAAAA auth_smbc_get_dat: set user=" << username
<< ", workgroup=" << workgroup
74 << " server=" << server
<< ", share=" << share
<< endl
;
76 QString s_server
= QString::fromUtf8(server
);
77 QString s_share
= QString::fromUtf8(share
);
78 workgroup
[wgmaxlen
- 1] = 0;
79 QString s_workgroup
= QString::fromUtf8(workgroup
);
80 username
[unmaxlen
- 1] = 0;
81 QString s_username
= QString::fromUtf8(username
);
82 password
[pwmaxlen
- 1] = 0;
83 QString s_password
= QString::fromUtf8(password
);
86 info
.url
= KUrl("smb:///");
87 info
.url
.setHost(s_server
);
88 info
.url
.setPath('/' + s_share
);
90 info
.username
= s_username
;
91 info
.password
= s_password
;
92 info
.verifyPath
= true;
94 kDebug(KIO_SMB
) << "libsmb-auth-callback URL:" << info
.url
;
96 if ( !checkCachedAuthentication( info
) )
98 if ( m_default_user
.isEmpty() )
100 // ok, we do not know the password. Let's try anonymous before we try for real
101 info
.username
= "anonymous";
102 info
.password
.clear();
106 // user defined a default username/password in kcontrol; try this
107 info
.username
= m_default_user
;
108 info
.password
= m_default_password
;
112 kDebug(KIO_SMB
) << "got password through cache";
114 strncpy(username
, info
.username
.toUtf8(), unmaxlen
- 1);
115 strncpy(password
, info
.password
.toUtf8(), pwmaxlen
- 1);
118 bool SMBSlave::checkPassword(SMBUrl
&url
)
120 kDebug(KIO_SMB
) << "checkPassword for " << url
;
123 info
.url
= KUrl("smb:///");
124 info
.url
.setHost(url
.host());
126 QString share
= url
.path();
127 int index
= share
.indexOf('/', 1);
129 share
= share
.left(index
);
130 if (share
.at(0) == '/')
131 share
= share
.mid(1);
132 info
.url
.setPath('/' + share
);
133 info
.verifyPath
= true;
135 if ( share
.isEmpty() )
137 "<qt>Please enter authentication information for <b>%1</b></qt>" ,
141 "Please enter authentication information for:\n"
147 info
.username
= url
.user();
148 kDebug(KIO_SMB
) << "call openPasswordDialog for " << info
.url
;
150 if ( openPasswordDialog(info
) ) {
151 kDebug(KIO_SMB
) << "openPasswordDialog returned " << info
.username
;
152 url
.setUser(info
.username
);
155 kDebug(KIO_SMB
) << "no value from openPasswordDialog\n";
159 //--------------------------------------------------------------------------
160 // Initalizes the smbclient library
162 // Returns: 0 on success -1 with errno set on error
163 bool SMBSlave::auth_initialize_smbc()
165 SMBCCTX
*smb_context
= NULL
;
167 kDebug(KIO_SMB
) << "auth_initialize_smbc ";
168 if(m_initialized_smbc
== false)
170 kDebug(KIO_SMB
) << "smbc_init call";
171 KConfig
cfg( "kioslaverc", KConfig::SimpleConfig
);
172 int debug_level
= cfg
.group( "SMB" ).readEntry( "DebugLevel", 0 );
175 /* old API initialisation routine does not allow to set flags */
177 if(smbc_init(::auth_smbc_get_data
,debug_level
) == -1)
179 SlaveBase::error(ERR_INTERNAL
, i18n("libsmbclient failed to initialize"));
183 smb_context
= smbc_new_context();
184 if (smb_context
== NULL
) {
185 SlaveBase::error(ERR_INTERNAL
, i18n("libsmbclient failed to create context"));
189 smb_context
->debug
= debug_level
;
190 smb_context
->callbacks
.auth_fn
= NULL
;
191 smbc_option_set(smb_context
, "auth_function", (void*)::auth_smbc_get_data
);
192 smbc_option_set(smb_context
, "user_data", this);
194 if (!smbc_init_context(smb_context
)) {
195 smbc_free_context(smb_context
, false);
197 SlaveBase::error(ERR_INTERNAL
, i18n("libsmbclient failed to initialize context"));
201 #if defined(SMB_CTX_FLAG_USE_KERBEROS) && defined(SMB_CTX_FLAG_FALLBACK_AFTER_KERBEROS)
202 smb_context
->flags
|= SMB_CTX_FLAG_USE_KERBEROS
| SMB_CTX_FLAG_FALLBACK_AFTER_KERBEROS
;
205 smbc_set_context(smb_context
);
207 m_initialized_smbc
= true;