source: vendor/current/source4/auth/session.c

Last change on this file was 988, checked in by Silvan Scherrer, 9 years ago

Samba Server: update vendor to version 4.4.3

File size: 12.0 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3 Authentication utility functions
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Andrew Bartlett 2001-2010
6 Copyright (C) Jeremy Allison 2000-2001
7 Copyright (C) Rafal Szczesniak 2002
8 Copyright (C) Stefan Metzmacher 2005
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
22*/
23
24#include "includes.h"
25#include "auth/auth.h"
26#include "auth/auth_sam.h"
27#include "auth/credentials/credentials.h"
28#include "auth/credentials/credentials_krb5.h"
29#include "libcli/security/security.h"
30#include "libcli/auth/libcli_auth.h"
31#include "dsdb/samdb/samdb.h"
32#include "auth/session_proto.h"
33#include "system/kerberos.h"
34#include <gssapi/gssapi.h>
35#include "libcli/wbclient/wbclient.h"
36
37_PUBLIC_ struct auth_session_info *anonymous_session(TALLOC_CTX *mem_ctx,
38 struct loadparm_context *lp_ctx)
39{
40 NTSTATUS nt_status;
41 struct auth_session_info *session_info = NULL;
42 nt_status = auth_anonymous_session_info(mem_ctx, lp_ctx, &session_info);
43 if (!NT_STATUS_IS_OK(nt_status)) {
44 return NULL;
45 }
46 return session_info;
47}
48
49_PUBLIC_ NTSTATUS auth_generate_session_info(TALLOC_CTX *mem_ctx,
50 struct loadparm_context *lp_ctx, /* Optional, if you don't want privilages */
51 struct ldb_context *sam_ctx, /* Optional, if you don't want local groups */
52 struct auth_user_info_dc *user_info_dc,
53 uint32_t session_info_flags,
54 struct auth_session_info **_session_info)
55{
56 struct auth_session_info *session_info;
57 NTSTATUS nt_status;
58 unsigned int i, num_sids = 0;
59
60 const char *filter;
61
62 struct dom_sid *sids = NULL;
63 const struct dom_sid *anonymous_sid, *system_sid;
64
65 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
66 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
67
68 session_info = talloc_zero(tmp_ctx, struct auth_session_info);
69 if (session_info == NULL) {
70 TALLOC_FREE(tmp_ctx);
71 return NT_STATUS_NO_MEMORY;
72 }
73
74 session_info->info = talloc_reference(session_info, user_info_dc->info);
75
76 session_info->torture = talloc_zero(session_info, struct auth_user_info_torture);
77 if (session_info->torture == NULL) {
78 TALLOC_FREE(tmp_ctx);
79 return NT_STATUS_NO_MEMORY;
80 }
81 session_info->torture->num_dc_sids = user_info_dc->num_sids;
82 session_info->torture->dc_sids = talloc_reference(session_info, user_info_dc->sids);
83 if (session_info->torture->dc_sids == NULL) {
84 TALLOC_FREE(tmp_ctx);
85 return NT_STATUS_NO_MEMORY;
86 }
87
88 /* unless set otherwise, the session key is the user session
89 * key from the auth subsystem */
90 session_info->session_key = data_blob_talloc(session_info, user_info_dc->user_session_key.data, user_info_dc->user_session_key.length);
91 if (!session_info->session_key.data && session_info->session_key.length) {
92 if (session_info->session_key.data == NULL) {
93 TALLOC_FREE(tmp_ctx);
94 return NT_STATUS_NO_MEMORY;
95 }
96 }
97
98 anonymous_sid = dom_sid_parse_talloc(tmp_ctx, SID_NT_ANONYMOUS);
99 if (anonymous_sid == NULL) {
100 TALLOC_FREE(tmp_ctx);
101 return NT_STATUS_NO_MEMORY;
102 }
103
104 system_sid = dom_sid_parse_talloc(tmp_ctx, SID_NT_SYSTEM);
105 if (system_sid == NULL) {
106 TALLOC_FREE(tmp_ctx);
107 return NT_STATUS_NO_MEMORY;
108 }
109
110 sids = talloc_array(tmp_ctx, struct dom_sid, user_info_dc->num_sids);
111 if (sids == NULL) {
112 TALLOC_FREE(tmp_ctx);
113 return NT_STATUS_NO_MEMORY;
114 }
115 if (!sids) {
116 talloc_free(tmp_ctx);
117 return NT_STATUS_NO_MEMORY;
118 }
119
120 num_sids = user_info_dc->num_sids;
121
122 for (i=0; i < user_info_dc->num_sids; i++) {
123 sids[i] = user_info_dc->sids[i];
124 }
125
126 /*
127 * Finally add the "standard" sids.
128 * The only difference between guest and "anonymous"
129 * is the addition of Authenticated_Users.
130 */
131
132 if (session_info_flags & AUTH_SESSION_INFO_DEFAULT_GROUPS) {
133 sids = talloc_realloc(tmp_ctx, sids, struct dom_sid, num_sids + 2);
134 NT_STATUS_HAVE_NO_MEMORY(sids);
135
136 if (!dom_sid_parse(SID_WORLD, &sids[num_sids])) {
137 return NT_STATUS_INTERNAL_ERROR;
138 }
139 num_sids++;
140
141 if (!dom_sid_parse(SID_NT_NETWORK, &sids[num_sids])) {
142 return NT_STATUS_INTERNAL_ERROR;
143 }
144 num_sids++;
145 }
146
147 if (session_info_flags & AUTH_SESSION_INFO_AUTHENTICATED) {
148 sids = talloc_realloc(tmp_ctx, sids, struct dom_sid, num_sids + 1);
149 NT_STATUS_HAVE_NO_MEMORY(sids);
150
151 if (!dom_sid_parse(SID_NT_AUTHENTICATED_USERS, &sids[num_sids])) {
152 return NT_STATUS_INTERNAL_ERROR;
153 }
154 num_sids++;
155 }
156
157
158
159 if (num_sids > PRIMARY_USER_SID_INDEX && dom_sid_equal(anonymous_sid, &sids[PRIMARY_USER_SID_INDEX])) {
160 /* Don't expand nested groups of system, anonymous etc*/
161 } else if (num_sids > PRIMARY_USER_SID_INDEX && dom_sid_equal(system_sid, &sids[PRIMARY_USER_SID_INDEX])) {
162 /* Don't expand nested groups of system, anonymous etc*/
163 } else if (sam_ctx) {
164 filter = talloc_asprintf(tmp_ctx, "(&(objectClass=group)(groupType:1.2.840.113556.1.4.803:=%u))",
165 GROUP_TYPE_BUILTIN_LOCAL_GROUP);
166
167 /* Search for each group in the token */
168 for (i = 0; i < num_sids; i++) {
169 char *sid_string;
170 const char *sid_dn;
171 DATA_BLOB sid_blob;
172
173 sid_string = dom_sid_string(tmp_ctx,
174 &sids[i]);
175 if (sid_string == NULL) {
176 TALLOC_FREE(user_info_dc);
177 return NT_STATUS_NO_MEMORY;
178 }
179
180 sid_dn = talloc_asprintf(tmp_ctx, "<SID=%s>", sid_string);
181 talloc_free(sid_string);
182 if (sid_dn == NULL) {
183 TALLOC_FREE(user_info_dc);
184 return NT_STATUS_NO_MEMORY;
185 }
186 sid_blob = data_blob_string_const(sid_dn);
187
188 /* This function takes in memberOf values and expands
189 * them, as long as they meet the filter - so only
190 * builtin groups
191 *
192 * We already have the SID in the token, so set
193 * 'only childs' flag to true */
194 nt_status = dsdb_expand_nested_groups(sam_ctx, &sid_blob, true, filter,
195 tmp_ctx, &sids, &num_sids);
196 if (!NT_STATUS_IS_OK(nt_status)) {
197 talloc_free(tmp_ctx);
198 return nt_status;
199 }
200 }
201 }
202
203 nt_status = security_token_create(session_info,
204 lp_ctx,
205 num_sids,
206 sids,
207 session_info_flags,
208 &session_info->security_token);
209 if (!NT_STATUS_IS_OK(nt_status)) {
210 TALLOC_FREE(tmp_ctx);
211 return nt_status;
212 }
213
214 session_info->credentials = NULL;
215
216 talloc_steal(mem_ctx, session_info);
217 *_session_info = session_info;
218 talloc_free(tmp_ctx);
219 return NT_STATUS_OK;
220}
221
222
223/* Fill out the auth_session_info with a cli_credentials based on the
224 * auth_session_info we were forwarded over named pipe forwarding.
225 *
226 * NOTE: The stucture members of session_info_transport are stolen
227 * with talloc_move() into auth_session_info for long term use
228 */
229struct auth_session_info *auth_session_info_from_transport(TALLOC_CTX *mem_ctx,
230 struct auth_session_info_transport *session_info_transport,
231 struct loadparm_context *lp_ctx,
232 const char **reason)
233{
234 struct auth_session_info *session_info;
235 session_info = talloc_steal(mem_ctx, session_info_transport->session_info);
236 /*
237 * This is to allow us to check the type of this pointer using
238 * talloc_get_type()
239 */
240 talloc_set_name(session_info, "struct auth_session_info");
241#ifdef HAVE_GSS_IMPORT_CRED
242 if (session_info_transport->exported_gssapi_credentials.length) {
243 struct cli_credentials *creds;
244 OM_uint32 minor_status;
245 gss_buffer_desc cred_token;
246 gss_cred_id_t cred_handle;
247 const char *error_string;
248 int ret;
249
250 DEBUG(10, ("Delegated credentials supplied by client\n"));
251
252 cred_token.value = session_info_transport->exported_gssapi_credentials.data;
253 cred_token.length = session_info_transport->exported_gssapi_credentials.length;
254
255 ret = gss_import_cred(&minor_status,
256 &cred_token,
257 &cred_handle);
258 if (ret != GSS_S_COMPLETE) {
259 *reason = "Internal error in gss_import_cred()";
260 return NULL;
261 }
262
263 creds = cli_credentials_init(session_info);
264 if (!creds) {
265 *reason = "Out of memory in cli_credentials_init()";
266 return NULL;
267 }
268 session_info->credentials = creds;
269
270 cli_credentials_set_conf(creds, lp_ctx);
271 /* Just so we don't segfault trying to get at a username */
272 cli_credentials_set_anonymous(creds);
273
274 ret = cli_credentials_set_client_gss_creds(creds,
275 lp_ctx,
276 cred_handle,
277 CRED_SPECIFIED,
278 &error_string);
279 if (ret) {
280 *reason = talloc_asprintf(mem_ctx,
281 "Failed to set pipe forwarded"
282 "creds: %s\n", error_string);
283 return NULL;
284 }
285
286 /* This credential handle isn't useful for password
287 * authentication, so ensure nobody tries to do that */
288 cli_credentials_set_kerberos_state(creds,
289 CRED_MUST_USE_KERBEROS);
290
291 }
292#endif
293 return session_info;
294}
295
296
297/* Create a auth_session_info_transport from an auth_session_info.
298 *
299 * NOTE: Members of the auth_session_info_transport structure are
300 * talloc_referenced() into this structure, and should not be changed.
301 */
302NTSTATUS auth_session_info_transport_from_session(TALLOC_CTX *mem_ctx,
303 struct auth_session_info *session_info,
304 struct tevent_context *event_ctx,
305 struct loadparm_context *lp_ctx,
306 struct auth_session_info_transport **transport_out)
307{
308
309 struct auth_session_info_transport *session_info_transport
310 = talloc_zero(mem_ctx, struct auth_session_info_transport);
311 if (!session_info_transport) {
312 return NT_STATUS_NO_MEMORY;
313 };
314 session_info_transport->session_info = talloc_reference(session_info_transport, session_info);
315 if (!session_info_transport->session_info) {
316 return NT_STATUS_NO_MEMORY;
317 };
318#ifdef HAVE_GSS_EXPORT_CRED
319 if (session_info->credentials) {
320 struct gssapi_creds_container *gcc;
321 OM_uint32 gret;
322 OM_uint32 minor_status;
323 gss_buffer_desc cred_token;
324 const char *error_string;
325 int ret;
326
327 ret = cli_credentials_get_client_gss_creds(session_info->credentials,
328 event_ctx,
329 lp_ctx,
330 &gcc, &error_string);
331 if (ret != 0) {
332 *transport_out = session_info_transport;
333 return NT_STATUS_OK;
334 }
335
336 gret = gss_export_cred(&minor_status,
337 gcc->creds,
338 &cred_token);
339 if (gret != GSS_S_COMPLETE) {
340 return NT_STATUS_INTERNAL_ERROR;
341 }
342
343 if (cred_token.length) {
344 session_info_transport->exported_gssapi_credentials
345 = data_blob_talloc(session_info_transport,
346 cred_token.value,
347 cred_token.length);
348 gss_release_buffer(&minor_status, &cred_token);
349 NT_STATUS_HAVE_NO_MEMORY(session_info_transport->exported_gssapi_credentials.data);
350 }
351 }
352#endif
353 *transport_out = session_info_transport;
354 return NT_STATUS_OK;
355}
356
357
358/* Produce a session_info for an arbitary DN or principal in the local
359 * DB, assuming the local DB holds all the groups
360 *
361 * Supply either a principal or a DN
362 */
363NTSTATUS authsam_get_session_info_principal(TALLOC_CTX *mem_ctx,
364 struct loadparm_context *lp_ctx,
365 struct ldb_context *sam_ctx,
366 const char *principal,
367 struct ldb_dn *user_dn,
368 uint32_t session_info_flags,
369 struct auth_session_info **session_info)
370{
371 NTSTATUS nt_status;
372 struct auth_user_info_dc *user_info_dc;
373 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
374 if (!tmp_ctx) {
375 return NT_STATUS_NO_MEMORY;
376 }
377 nt_status = authsam_get_user_info_dc_principal(tmp_ctx, lp_ctx, sam_ctx,
378 principal, user_dn,
379 &user_info_dc);
380 if (!NT_STATUS_IS_OK(nt_status)) {
381 talloc_free(tmp_ctx);
382 return nt_status;
383 }
384
385 nt_status = auth_generate_session_info(tmp_ctx, lp_ctx, sam_ctx,
386 user_info_dc, session_info_flags,
387 session_info);
388
389 if (NT_STATUS_IS_OK(nt_status)) {
390 talloc_steal(mem_ctx, *session_info);
391 }
392 talloc_free(tmp_ctx);
393 return nt_status;
394}
395
396/**
397 * prints a struct auth_session_info security token to debug output.
398 */
399void auth_session_info_debug(int dbg_lev,
400 const struct auth_session_info *session_info)
401{
402 if (!session_info) {
403 DEBUG(dbg_lev, ("Session Info: (NULL)\n"));
404 return;
405 }
406
407 security_token_debug(0, dbg_lev, session_info->security_token);
408}
Note: See TracBrowser for help on using the repository browser.