2 Unix SMB/CIFS implementation.
4 Winbind daemon - cached credentials functions
6 Copyright (C) Robert O'Callahan 2006
7 Copyright (C) Jeremy Allison 2006 (minor fixes to fit into Samba and
8 protect against integer wrap).
9 Copyright (C) Andrew Bartlett 2011
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "auth/gensec/gensec.h"
28 #include "auth_generic.h"
29 #include "lib/util/string_wrappers.h"
32 #define DBGC_CLASS DBGC_WINBIND
34 static bool client_can_access_ccache_entry(uid_t client_uid
,
35 struct WINBINDD_MEMORY_CREDS
*entry
)
37 if (client_uid
== entry
->uid
|| client_uid
== 0) {
38 DEBUG(10, ("Access granted to uid %u\n", (unsigned int)client_uid
));
42 DEBUG(1, ("Access denied to uid %u (expected %u)\n",
43 (unsigned int)client_uid
, (unsigned int)entry
->uid
));
47 static NTSTATUS
do_ntlm_auth_with_stored_pw(const char *namespace,
51 const DATA_BLOB initial_msg
,
52 const DATA_BLOB challenge_msg
,
55 uint8_t session_key
[16],
59 struct auth_generic_state
*auth_generic_state
= NULL
;
60 DATA_BLOB reply
, session_key_blob
;
62 status
= auth_generic_client_prepare(mem_ctx
, &auth_generic_state
);
64 if (!NT_STATUS_IS_OK(status
)) {
65 DEBUG(1, ("Could not start NTLMSSP client: %s\n",
70 status
= auth_generic_set_username(auth_generic_state
, username
);
72 if (!NT_STATUS_IS_OK(status
)) {
73 DEBUG(1, ("Could not set username: %s\n",
78 status
= auth_generic_set_domain(auth_generic_state
, domain
);
80 if (!NT_STATUS_IS_OK(status
)) {
81 DEBUG(1, ("Could not set domain: %s\n",
86 status
= auth_generic_set_password(auth_generic_state
, password
);
88 if (!NT_STATUS_IS_OK(status
)) {
89 DEBUG(1, ("Could not set password: %s\n",
94 if (initial_msg
.length
== 0) {
95 gensec_want_feature(auth_generic_state
->gensec_security
,
96 GENSEC_FEATURE_SESSION_KEY
);
99 status
= auth_generic_client_start_by_name(auth_generic_state
,
100 "ntlmssp_resume_ccache");
101 if (!NT_STATUS_IS_OK(status
)) {
102 DEBUG(1, ("Could not start NTLMSSP resume mech: %s\n",
108 * We inject the initial NEGOTIATE message our caller used
109 * in order to get the state machine into the correct position.
111 reply
= data_blob_null
;
112 status
= gensec_update(auth_generic_state
->gensec_security
,
113 talloc_tos(), initial_msg
, &reply
);
114 data_blob_free(&reply
);
116 if (!NT_STATUS_EQUAL(status
, NT_STATUS_MORE_PROCESSING_REQUIRED
)) {
117 DEBUG(1, ("Failed to create initial message! [%s]\n",
122 /* Now we are ready to handle the server's actual response. */
123 status
= gensec_update(auth_generic_state
->gensec_security
,
124 mem_ctx
, challenge_msg
, &reply
);
125 if (!NT_STATUS_EQUAL(status
, NT_STATUS_OK
)) {
126 DEBUG(1, ("We didn't get a response to the challenge! [%s]\n",
128 data_blob_free(&reply
);
132 status
= gensec_session_key(auth_generic_state
->gensec_security
,
133 talloc_tos(), &session_key_blob
);
134 if (!NT_STATUS_EQUAL(status
, NT_STATUS_OK
)) {
135 DEBUG(1, ("We didn't get the session key we requested! [%s]\n",
137 data_blob_free(&reply
);
141 if (session_key_blob
.length
!= 16) {
142 DEBUG(1, ("invalid session key length %d\n",
143 (int)session_key_blob
.length
));
144 data_blob_free(&reply
);
147 memcpy(session_key
, session_key_blob
.data
, 16);
148 data_blob_free(&session_key_blob
);
150 *new_spnego
= gensec_have_feature(auth_generic_state
->gensec_security
,
151 GENSEC_FEATURE_NEW_SPNEGO
);
152 status
= NT_STATUS_OK
;
155 TALLOC_FREE(auth_generic_state
);
159 static bool check_client_uid(struct winbindd_cli_state
*state
, uid_t uid
)
167 ret
= getpeereid(state
->sock
, &ret_uid
, &ret_gid
);
169 DEBUG(1, ("check_client_uid: Could not get socket peer uid: %s; "
170 "denying access\n", strerror(errno
)));
174 if (uid
!= ret_uid
&& ret_uid
!= sec_initial_uid()) {
175 DEBUG(1, ("check_client_uid: Client lied about its uid: said %u, "
176 "actually was %u; denying access\n",
177 (unsigned int)uid
, (unsigned int)ret_uid
));
184 bool winbindd_ccache_ntlm_auth(struct winbindd_cli_state
*state
)
186 struct winbindd_domain
*domain
;
187 char *name_namespace
= NULL
;
188 char *name_domain
= NULL
;
189 char *name_user
= NULL
;
190 char *auth_user
= NULL
;
191 NTSTATUS result
= NT_STATUS_NOT_SUPPORTED
;
192 struct WINBINDD_MEMORY_CREDS
*entry
;
193 DATA_BLOB initial
, challenge
, auth
;
194 uint32_t initial_blob_len
, challenge_blob_len
, extra_len
;
197 /* Ensure null termination */
198 state
->request
->data
.ccache_ntlm_auth
.user
[
199 sizeof(state
->request
->data
.ccache_ntlm_auth
.user
)-1]='\0';
201 DEBUG(3, ("[%5lu]: perform NTLM auth on behalf of user %s\n", (unsigned long)state
->pid
,
202 state
->request
->data
.ccache_ntlm_auth
.user
));
204 /* Parse domain and username */
206 auth_user
= state
->request
->data
.ccache_ntlm_auth
.user
;
207 ok
= canonicalize_username(state
,
213 DEBUG(5,("winbindd_ccache_ntlm_auth: cannot parse domain and user from name [%s]\n",
214 state
->request
->data
.ccache_ntlm_auth
.user
));
218 fstrcpy(state
->request
->data
.ccache_ntlm_auth
.user
, auth_user
);
219 TALLOC_FREE(auth_user
);
221 domain
= find_auth_domain(state
->request
->flags
, name_domain
);
223 if (domain
== NULL
) {
224 DEBUG(5,("winbindd_ccache_ntlm_auth: can't get domain [%s]\n",
229 if (!check_client_uid(state
, state
->request
->data
.ccache_ntlm_auth
.uid
)) {
233 /* validate blob lengths */
234 initial_blob_len
= state
->request
->data
.ccache_ntlm_auth
.initial_blob_len
;
235 challenge_blob_len
= state
->request
->data
.ccache_ntlm_auth
.challenge_blob_len
;
236 extra_len
= state
->request
->extra_len
;
238 if (initial_blob_len
> extra_len
|| challenge_blob_len
> extra_len
||
239 initial_blob_len
+ challenge_blob_len
> extra_len
||
240 initial_blob_len
+ challenge_blob_len
< initial_blob_len
||
241 initial_blob_len
+ challenge_blob_len
< challenge_blob_len
) {
243 DEBUG(10,("winbindd_dual_ccache_ntlm_auth: blob lengths overrun "
244 "or wrap. Buffer [%d+%d > %d]\n",
251 TALLOC_FREE(name_namespace
);
252 TALLOC_FREE(name_domain
);
253 TALLOC_FREE(name_user
);
254 /* Parse domain and username */
255 ok
= parse_domain_user(state
,
256 state
->request
->data
.ccache_ntlm_auth
.user
,
261 DEBUG(10,("winbindd_dual_ccache_ntlm_auth: cannot parse "
262 "domain and user from name [%s]\n",
263 state
->request
->data
.ccache_ntlm_auth
.user
));
267 entry
= find_memory_creds_by_name(state
->request
->data
.ccache_ntlm_auth
.user
);
268 if (entry
== NULL
|| entry
->nt_hash
== NULL
|| entry
->lm_hash
== NULL
) {
269 DEBUG(10,("winbindd_dual_ccache_ntlm_auth: could not find "
270 "credentials for user %s\n",
271 state
->request
->data
.ccache_ntlm_auth
.user
));
275 DEBUG(10,("winbindd_dual_ccache_ntlm_auth: found ccache [%s]\n", entry
->username
));
277 if (!client_can_access_ccache_entry(state
->request
->data
.ccache_ntlm_auth
.uid
, entry
)) {
281 if (initial_blob_len
== 0 && challenge_blob_len
== 0) {
282 /* this is just a probe to see if credentials are available. */
283 result
= NT_STATUS_OK
;
284 state
->response
->data
.ccache_ntlm_auth
.auth_blob_len
= 0;
288 initial
= data_blob_const(state
->request
->extra_data
.data
,
290 challenge
= data_blob_const(
291 state
->request
->extra_data
.data
+ initial_blob_len
,
292 state
->request
->data
.ccache_ntlm_auth
.challenge_blob_len
);
294 result
= do_ntlm_auth_with_stored_pw(
303 state
->response
->data
.ccache_ntlm_auth
.session_key
,
304 &state
->response
->data
.ccache_ntlm_auth
.new_spnego
);
306 if (!NT_STATUS_IS_OK(result
)) {
310 state
->response
->extra_data
.data
= talloc_memdup(
311 state
->mem_ctx
, auth
.data
, auth
.length
);
312 if (!state
->response
->extra_data
.data
) {
313 result
= NT_STATUS_NO_MEMORY
;
316 state
->response
->length
+= auth
.length
;
317 state
->response
->data
.ccache_ntlm_auth
.auth_blob_len
= auth
.length
;
319 data_blob_free(&auth
);
322 TALLOC_FREE(name_namespace
);
323 TALLOC_FREE(name_domain
);
324 TALLOC_FREE(name_user
);
325 return NT_STATUS_IS_OK(result
);
328 bool winbindd_ccache_save(struct winbindd_cli_state
*state
)
330 struct winbindd_domain
*domain
;
331 char *name_namespace
= NULL
;
332 char *name_domain
= NULL
;
333 char *name_user
= NULL
;
334 char *save_user
= NULL
;
338 /* Ensure null termination */
339 state
->request
->data
.ccache_save
.user
[
340 sizeof(state
->request
->data
.ccache_save
.user
)-1]='\0';
341 state
->request
->data
.ccache_save
.pass
[
342 sizeof(state
->request
->data
.ccache_save
.pass
)-1]='\0';
344 DEBUG(3, ("[%5lu]: save password of user %s\n",
345 (unsigned long)state
->pid
,
346 state
->request
->data
.ccache_save
.user
));
348 /* Parse domain and username */
351 save_user
= state
->request
->data
.ccache_save
.user
;
352 ok
= canonicalize_username(state
,
358 DEBUG(5,("winbindd_ccache_save: cannot parse domain and user "
360 state
->request
->data
.ccache_save
.user
));
364 fstrcpy(state
->request
->data
.ccache_save
.user
, save_user
);
367 * The domain is checked here only for compatibility
368 * reasons. We used to do the winbindd memory ccache for
369 * ntlm_auth in the domain child. With that code, we had to
370 * make sure that we do have a domain around to send this
371 * to. Now we do the memory cache in the parent winbindd,
372 * where it would not matter if we have a domain or not.
375 domain
= find_auth_domain(state
->request
->flags
, name_domain
);
376 if (domain
== NULL
) {
377 DEBUG(5, ("winbindd_ccache_save: can't get domain [%s]\n",
382 if (!check_client_uid(state
, state
->request
->data
.ccache_save
.uid
)) {
386 status
= winbindd_add_memory_creds(
387 state
->request
->data
.ccache_save
.user
,
388 state
->request
->data
.ccache_save
.uid
,
389 state
->request
->data
.ccache_save
.pass
);
391 if (!NT_STATUS_IS_OK(status
)) {
392 DEBUG(1, ("winbindd_add_memory_creds failed %s\n",