Changeset View
Changeset View
Standalone View
Standalone View
smb/kio_smb_auth.cpp
Show First 20 Lines • Show All 89 Lines • ▼ Show 20 Line(s) | 69 | { | |||
---|---|---|---|---|---|
90 | info.url = QUrl("smb:///"); | 90 | info.url = QUrl("smb:///"); | ||
91 | info.url.setHost(s_server); | 91 | info.url.setHost(s_server); | ||
92 | info.url.setPath('/' + s_share); | 92 | info.url.setPath('/' + s_share); | ||
93 | 93 | | |||
94 | info.username = s_username; | 94 | info.username = s_username; | ||
95 | info.password = s_password; | 95 | info.password = s_password; | ||
96 | info.verifyPath = true; | 96 | info.verifyPath = true; | ||
97 | 97 | | |||
98 | info.setExtraField("domain", s_workgroup); | ||||
99 | | ||||
98 | qCDebug(KIO_SMB) << "libsmb-auth-callback URL:" << info.url; | 100 | qCDebug(KIO_SMB) << "libsmb-auth-callback URL:" << info.url; | ||
99 | 101 | | |||
100 | if ( !checkCachedAuthentication( info ) ) | 102 | if ( !checkCachedAuthentication( info ) ) | ||
101 | { | 103 | { | ||
102 | if ( m_default_user.isEmpty() ) | 104 | if ( m_default_user.isEmpty() ) | ||
103 | { | 105 | { | ||
104 | // ok, we do not know the password. Let's try anonymous before we try for real | 106 | // ok, we do not know the password. Let's try anonymous before we try for real | ||
105 | info.username = "anonymous"; | 107 | info.username = "anonymous"; | ||
106 | info.password.clear(); | 108 | info.password.clear(); | ||
107 | } | 109 | } | ||
108 | else | 110 | else | ||
109 | { | 111 | { | ||
110 | // user defined a default username/password in kcontrol; try this | 112 | // user defined a default username/password in kcontrol; try this | ||
111 | info.username = m_default_user; | 113 | info.username = m_default_user; | ||
112 | info.password = m_default_password; | 114 | info.password = m_default_password; | ||
113 | } | 115 | } | ||
114 | 116 | | |||
115 | } else | 117 | } else | ||
116 | qCDebug(KIO_SMB) << "got password through cache"; | 118 | qCDebug(KIO_SMB) << "got password through cache"; | ||
117 | 119 | | |||
118 | strncpy(username, info.username.toUtf8(), unmaxlen - 1); | 120 | // Make sure it'll be safe to cast to size_t (unsigned) | ||
119 | strncpy(password, info.password.toUtf8(), pwmaxlen - 1); | 121 | Q_ASSERT(unmaxlen > 0); | ||
122 | Q_ASSERT(pwmaxlen > 0); | ||||
123 | Q_ASSERT(wgmaxlen > 0); | ||||
124 | | ||||
125 | strncpy(username, info.username.toUtf8(), static_cast<size_t>(unmaxlen - 1)); | ||||
126 | strncpy(password, info.password.toUtf8(), static_cast<size_t>(pwmaxlen - 1)); | ||||
127 | // TODO: isEmpty guard can be removed in 20.08+ | ||||
128 | // It is only here to prevent us setting an empty work group if a user updates | ||||
129 | // but doesn't restart so kiod5 could hold an old cache without domain | ||||
130 | // field. In that event we'll leave the input workgroup as-is. | ||||
131 | const QString domain = info.getExtraField("domain").toString(); | ||||
132 | if (!domain.isEmpty()) { | ||||
133 | strncpy(workgroup, domain.toUtf8(), static_cast<size_t>(wgmaxlen - 1)); | ||||
134 | } | ||||
120 | } | 135 | } | ||
121 | 136 | | |||
122 | int SMBSlave::checkPassword(SMBUrl &url) | 137 | int SMBSlave::checkPassword(SMBUrl &url) | ||
123 | { | 138 | { | ||
124 | qCDebug(KIO_SMB) << "checkPassword for " << url; | 139 | qCDebug(KIO_SMB) << "checkPassword for " << url; | ||
125 | 140 | | |||
126 | KIO::AuthInfo info; | 141 | KIO::AuthInfo info; | ||
127 | info.url = QUrl("smb:///"); | 142 | info.url = QUrl("smb:///"); | ||
128 | info.url.setHost(url.host()); | 143 | info.url.setHost(url.host()); | ||
129 | 144 | | |||
130 | QString share = url.path(); | 145 | QString share = url.path(); | ||
131 | int index = share.indexOf('/', 1); | 146 | int index = share.indexOf('/', 1); | ||
132 | if (index > 1) | 147 | if (index > 1) | ||
133 | share = share.left(index); | 148 | share = share.left(index); | ||
134 | if (share.at(0) == '/') | 149 | if (share.at(0) == '/') | ||
135 | share = share.mid(1); | 150 | share = share.mid(1); | ||
136 | info.url.setPath('/' + share); | 151 | info.url.setPath('/' + share); | ||
137 | info.verifyPath = true; | 152 | info.verifyPath = true; | ||
138 | info.keepPassword = true; | 153 | info.keepPassword = true; | ||
139 | 154 | | |||
155 | info.setExtraField("anonymous", true); // arbitrary default for dialog | ||||
156 | info.setExtraField("domain", m_default_workgroup); | ||||
157 | | ||||
140 | if ( share.isEmpty() ) | 158 | if ( share.isEmpty() ) | ||
141 | info.prompt = i18n( | 159 | info.prompt = i18n( | ||
142 | "<qt>Please enter authentication information for <b>%1</b></qt>" , | 160 | "<qt>Please enter authentication information for <b>%1</b></qt>" , | ||
143 | url.host() ); | 161 | url.host() ); | ||
144 | else | 162 | else | ||
145 | info.prompt = i18n( | 163 | info.prompt = i18n( | ||
146 | "Please enter authentication information for:\n" | 164 | "Please enter authentication information for:\n" | ||
147 | "Server = %1\n" | 165 | "Server = %1\n" | ||
▲ Show 20 Lines • Show All 67 Lines • ▼ Show 20 Line(s) | 232 | if (!smbc_init_context(smb_context)) { | |||
215 | smbc_free_context(smb_context, 0); | 233 | smbc_free_context(smb_context, 0); | ||
216 | smb_context = nullptr; | 234 | smb_context = nullptr; | ||
217 | SlaveBase::error(ERR_INTERNAL, i18n("libsmbclient failed to initialize context")); | 235 | SlaveBase::error(ERR_INTERNAL, i18n("libsmbclient failed to initialize context")); | ||
218 | return false; | 236 | return false; | ||
219 | } | 237 | } | ||
220 | 238 | | |||
221 | smbc_set_context(smb_context); | 239 | smbc_set_context(smb_context); | ||
222 | 240 | | |||
241 | // TODO: refactor; checkPassword should query this on | ||||
242 | // demand to not run into situations where we may have cached | ||||
243 | // the workgroup early on and it changed since. Needs context | ||||
244 | // being held in the slave though, which opens us up to nullptr | ||||
245 | // problems should checkPassword be called without init first. | ||||
246 | m_default_workgroup = smbc_getWorkgroup(smb_context); | ||||
247 | | ||||
223 | m_initialized_smbc = true; | 248 | m_initialized_smbc = true; | ||
224 | } | 249 | } | ||
225 | 250 | | |||
226 | return true; | 251 | return true; | ||
227 | } | 252 | } | ||
228 | 253 | |