add more spacing
[personal-kdebase.git] / runtime / kioslave / smb / kio_smb_auth.cpp
blob29afeea5f4f5024e4c39dad82583f16ef5c799e7
1 /////////////////////////////////////////////////////////////////////////////
2 //
3 // Project: SMB kioslave for KDE2
4 //
5 // File: kio_smb_auth.cpp
6 //
7 // Abstract: member function implementations for SMBSlave that deal with
8 // SMB directory access
9 //
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 /////////////////////////////////////////////////////////////////////////////
32 #include "kio_smb.h"
33 #include "kio_smb_internal.h"
35 #include <kconfig.h>
36 #include <kconfiggroup.h>
37 #include <QDir>
38 #include <stdlib.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 #ifdef DEPRECATED_SMBC_INTERFACE
51 SMBSlave *theSlave = (SMBSlave*) smbc_getOptionUserData(context);
52 #else
53 SMBSlave *theSlave = (SMBSlave*)smbc_option_get(context, "user_data");
54 #endif
55 theSlave->auth_smbc_get_data(server, share,
56 workgroup,wgmaxlen,
57 username, unmaxlen,
58 password, pwmaxlen);
63 //--------------------------------------------------------------------------
64 void SMBSlave::auth_smbc_get_data(const char *server,const char *share,
65 char *workgroup, int wgmaxlen,
66 char *username, int unmaxlen,
67 char *password, int pwmaxlen)
68 //--------------------------------------------------------------------------
70 //check this to see if we "really" need to authenticate...
71 SMBUrlType t = m_current_url.getType();
72 if( t == SMBURLTYPE_ENTIRE_NETWORK )
74 kDebug(KIO_SMB) << "we don't really need to authenticate for this top level url, returning";
75 return;
77 kDebug(KIO_SMB) << "AAAAAAAAAAAAAA auth_smbc_get_dat: set user=" << username << ", workgroup=" << workgroup
78 << " server=" << server << ", share=" << share << endl;
80 QString s_server = QString::fromUtf8(server);
81 QString s_share = QString::fromUtf8(share);
82 workgroup[wgmaxlen - 1] = 0;
83 QString s_workgroup = QString::fromUtf8(workgroup);
84 username[unmaxlen - 1] = 0;
85 QString s_username = QString::fromUtf8(username);
86 password[pwmaxlen - 1] = 0;
87 QString s_password = QString::fromUtf8(password);
89 KIO::AuthInfo info;
90 info.url = KUrl("smb:///");
91 info.url.setHost(s_server);
92 info.url.setPath('/' + s_share);
94 info.username = s_username;
95 info.password = s_password;
96 info.verifyPath = true;
98 kDebug(KIO_SMB) << "libsmb-auth-callback URL:" << info.url;
100 if ( !checkCachedAuthentication( info ) )
102 if ( m_default_user.isEmpty() )
104 // ok, we do not know the password. Let's try anonymous before we try for real
105 info.username = "anonymous";
106 info.password.clear();
108 else
110 // user defined a default username/password in kcontrol; try this
111 info.username = m_default_user;
112 info.password = m_default_password;
115 } else
116 kDebug(KIO_SMB) << "got password through cache";
118 strncpy(username, info.username.toUtf8(), unmaxlen - 1);
119 strncpy(password, info.password.toUtf8(), pwmaxlen - 1);
122 bool SMBSlave::checkPassword(SMBUrl &url)
124 kDebug(KIO_SMB) << "checkPassword for " << url;
126 KIO::AuthInfo info;
127 info.url = KUrl("smb:///");
128 info.url.setHost(url.host());
130 QString share = url.path();
131 int index = share.indexOf('/', 1);
132 if (index > 1)
133 share = share.left(index);
134 if (share.at(0) == '/')
135 share = share.mid(1);
136 info.url.setPath('/' + share);
137 info.verifyPath = true;
139 if ( share.isEmpty() )
140 info.prompt = i18n(
141 "<qt>Please enter authentication information for <b>%1</b></qt>" ,
142 url.host() );
143 else
144 info.prompt = i18n(
145 "Please enter authentication information for:\n"
146 "Server = %1\n"
147 "Share = %2" ,
148 url.host() ,
149 share );
151 info.username = url.user();
152 kDebug(KIO_SMB) << "call openPasswordDialog for " << info.url;
154 if ( openPasswordDialog(info) ) {
155 kDebug(KIO_SMB) << "openPasswordDialog returned " << info.username;
156 url.setUser(info.username);
157 return true;
159 kDebug(KIO_SMB) << "no value from openPasswordDialog\n";
160 return false;
163 //--------------------------------------------------------------------------
164 // Initalizes the smbclient library
166 // Returns: 0 on success -1 with errno set on error
167 bool SMBSlave::auth_initialize_smbc()
169 SMBCCTX *smb_context = NULL;
171 kDebug(KIO_SMB) << "auth_initialize_smbc ";
172 if(m_initialized_smbc == false)
174 kDebug(KIO_SMB) << "smbc_init call";
175 KConfig cfg( "kioslaverc", KConfig::SimpleConfig);
176 int debug_level = cfg.group( "SMB" ).readEntry( "DebugLevel", 0 );
178 smb_context = smbc_new_context();
179 if (smb_context == NULL) {
180 SlaveBase::error(ERR_INTERNAL, i18n("libsmbclient failed to create context"));
181 return false;
184 #ifdef DEPRECATED_SMBC_INTERFACE // defined by libsmbclient.h of Samba 3.2
186 /* New libsmbclient interface of Samba 3.2 */
187 smbc_setDebug(smb_context, debug_level);
188 smbc_setFunctionAuthDataWithContext(smb_context, ::auth_smbc_get_data);
189 smbc_setOptionUserData(smb_context, this);
191 /* Enable Kerberos support */
192 smbc_setOptionUseKerberos(smb_context, 1);
193 smbc_setOptionFallbackAfterKerberos(smb_context, 1);
194 #else
195 smb_context->debug = debug_level;
196 smb_context->callbacks.auth_fn = NULL;
197 smbc_option_set(smb_context, "auth_function", (void*)::auth_smbc_get_data);
198 smbc_option_set(smb_context, "user_data", this);
200 #if defined(SMB_CTX_FLAG_USE_KERBEROS) && defined(SMB_CTX_FLAG_FALLBACK_AFTER_KERBEROS)
201 smb_context->flags |= SMB_CTX_FLAG_USE_KERBEROS | SMB_CTX_FLAG_FALLBACK_AFTER_KERBEROS;
202 #endif
203 #endif /* DEPRECATED_SMBC_INTERFACE */
205 if (!smbc_init_context(smb_context)) {
206 smbc_free_context(smb_context, 0);
207 smb_context = NULL;
208 SlaveBase::error(ERR_INTERNAL, i18n("libsmbclient failed to initialize context"));
209 return false;
212 smbc_set_context(smb_context);
214 m_initialized_smbc = true;
217 return true;