when a new activity is added, don't move more than necessary to show it
[kdebase.git] / runtime / kioslave / smb / kio_smb_auth.cpp
blob599ba9f4e083fc17ae2869906192a1319bf8172f
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 // FIXME deprecated use smbc_getOption*() functions instead
51 SMBSlave *theSlave = (SMBSlave*)smbc_option_get(context, "user_data");
52 theSlave->auth_smbc_get_data(server, share,
53 workgroup,wgmaxlen,
54 username, unmaxlen,
55 password, pwmaxlen);
60 //--------------------------------------------------------------------------
61 void SMBSlave::auth_smbc_get_data(const char *server,const char *share,
62 char *workgroup, int wgmaxlen,
63 char *username, int unmaxlen,
64 char *password, int pwmaxlen)
65 //--------------------------------------------------------------------------
67 //check this to see if we "really" need to authenticate...
68 SMBUrlType t = m_current_url.getType();
69 if( t == SMBURLTYPE_ENTIRE_NETWORK )
71 kDebug(KIO_SMB) << "we don't really need to authenticate for this top level url, returning";
72 return;
74 kDebug(KIO_SMB) << "AAAAAAAAAAAAAA auth_smbc_get_dat: set user=" << username << ", workgroup=" << workgroup
75 << " server=" << server << ", share=" << share << endl;
77 QString s_server = QString::fromUtf8(server);
78 QString s_share = QString::fromUtf8(share);
79 workgroup[wgmaxlen - 1] = 0;
80 QString s_workgroup = QString::fromUtf8(workgroup);
81 username[unmaxlen - 1] = 0;
82 QString s_username = QString::fromUtf8(username);
83 password[pwmaxlen - 1] = 0;
84 QString s_password = QString::fromUtf8(password);
86 KIO::AuthInfo info;
87 info.url = KUrl("smb:///");
88 info.url.setHost(s_server);
89 info.url.setPath('/' + s_share);
91 info.username = s_username;
92 info.password = s_password;
93 info.verifyPath = true;
95 kDebug(KIO_SMB) << "libsmb-auth-callback URL:" << info.url;
97 if ( !checkCachedAuthentication( info ) )
99 if ( m_default_user.isEmpty() )
101 // ok, we do not know the password. Let's try anonymous before we try for real
102 info.username = "anonymous";
103 info.password.clear();
105 else
107 // user defined a default username/password in kcontrol; try this
108 info.username = m_default_user;
109 info.password = m_default_password;
112 } else
113 kDebug(KIO_SMB) << "got password through cache";
115 strncpy(username, info.username.toUtf8(), unmaxlen - 1);
116 strncpy(password, info.password.toUtf8(), pwmaxlen - 1);
119 bool SMBSlave::checkPassword(SMBUrl &url)
121 kDebug(KIO_SMB) << "checkPassword for " << url;
123 KIO::AuthInfo info;
124 info.url = KUrl("smb:///");
125 info.url.setHost(url.host());
127 QString share = url.path();
128 int index = share.indexOf('/', 1);
129 if (index > 1)
130 share = share.left(index);
131 if (share.at(0) == '/')
132 share = share.mid(1);
133 info.url.setPath('/' + share);
134 info.verifyPath = true;
136 if ( share.isEmpty() )
137 info.prompt = i18n(
138 "<qt>Please enter authentication information for <b>%1</b></qt>" ,
139 url.host() );
140 else
141 info.prompt = i18n(
142 "Please enter authentication information for:\n"
143 "Server = %1\n"
144 "Share = %2" ,
145 url.host() ,
146 share );
148 info.username = url.user();
149 kDebug(KIO_SMB) << "call openPasswordDialog for " << info.url;
151 if ( openPasswordDialog(info) ) {
152 kDebug(KIO_SMB) << "openPasswordDialog returned " << info.username;
153 url.setUser(info.username);
154 return true;
156 kDebug(KIO_SMB) << "no value from openPasswordDialog\n";
157 return false;
160 //--------------------------------------------------------------------------
161 // Initalizes the smbclient library
163 // Returns: 0 on success -1 with errno set on error
164 bool SMBSlave::auth_initialize_smbc()
166 SMBCCTX *smb_context = NULL;
168 kDebug(KIO_SMB) << "auth_initialize_smbc ";
169 if(m_initialized_smbc == false)
171 kDebug(KIO_SMB) << "smbc_init call";
172 KConfig cfg( "kioslaverc", KConfig::SimpleConfig);
173 int debug_level = cfg.group( "SMB" ).readEntry( "DebugLevel", 0 );
175 #if 0
176 /* old API initialisation routine does not allow to set flags */
178 if(smbc_init(::auth_smbc_get_data,debug_level) == -1)
180 SlaveBase::error(ERR_INTERNAL, i18n("libsmbclient failed to initialize"));
181 return false;
183 #endif
184 smb_context = smbc_new_context();
185 if (smb_context == NULL) {
186 SlaveBase::error(ERR_INTERNAL, i18n("libsmbclient failed to create context"));
187 return false;
190 smb_context->debug = debug_level;
191 smb_context->callbacks.auth_fn = NULL;
192 // FIXME deprecated use smbc_setOption*() functions instead.
193 smbc_option_set(smb_context, "auth_function", (void*)::auth_smbc_get_data);
194 smbc_option_set(smb_context, "user_data", this);
196 if (!smbc_init_context(smb_context)) {
197 smbc_free_context(smb_context, 0);
198 smb_context = NULL;
199 SlaveBase::error(ERR_INTERNAL, i18n("libsmbclient failed to initialize context"));
200 return false;
203 #if defined(SMB_CTX_FLAG_USE_KERBEROS) && defined(SMB_CTX_FLAG_FALLBACK_AFTER_KERBEROS)
204 smb_context->flags |= SMB_CTX_FLAG_USE_KERBEROS | SMB_CTX_FLAG_FALLBACK_AFTER_KERBEROS;
205 #endif
207 smbc_set_context(smb_context);
209 m_initialized_smbc = true;
212 return true;