Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / runtime / kioslave / smb / kio_smb_auth.cpp
blobf6aeee5db843cc12854209e717e4cb7c8db32226
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 SMBSlave *theSlave = (SMBSlave*)smbc_option_get(context, "user_data");
51 theSlave->auth_smbc_get_data(server, share,
52 workgroup,wgmaxlen,
53 username, unmaxlen,
54 password, pwmaxlen);
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";
71 return;
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);
85 KIO::AuthInfo info;
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();
104 else
106 // user defined a default username/password in kcontrol; try this
107 info.username = m_default_user;
108 info.password = m_default_password;
111 } else
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;
122 KIO::AuthInfo info;
123 info.url = KUrl("smb:///");
124 info.url.setHost(url.host());
126 QString share = url.path();
127 int index = share.indexOf('/', 1);
128 if (index > 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() )
136 info.prompt = i18n(
137 "<qt>Please enter authentication information for <b>%1</b></qt>" ,
138 url.host() );
139 else
140 info.prompt = i18n(
141 "Please enter authentication information for:\n"
142 "Server = %1\n"
143 "Share = %2" ,
144 url.host() ,
145 share );
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);
153 return true;
155 kDebug(KIO_SMB) << "no value from openPasswordDialog\n";
156 return false;
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 );
174 #if 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"));
180 return false;
182 #endif
183 smb_context = smbc_new_context();
184 if (smb_context == NULL) {
185 SlaveBase::error(ERR_INTERNAL, i18n("libsmbclient failed to create context"));
186 return false;
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);
196 smb_context = NULL;
197 SlaveBase::error(ERR_INTERNAL, i18n("libsmbclient failed to initialize context"));
198 return false;
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;
203 #endif
205 smbc_set_context(smb_context);
207 m_initialized_smbc = true;
210 return true;