waf: add test for HAVE_DQB_FSOFTLIMIT
[Samba.git] / source4 / auth / session.c
blobde417cc54b110c83f53ed651bb5e8595d0dfa6d3
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
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.
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.
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/>.
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"
37 _PUBLIC_ struct auth_session_info *anonymous_session(TALLOC_CTX *mem_ctx,
38 struct loadparm_context *lp_ctx)
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;
46 return session_info;
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)
56 struct auth_session_info *session_info;
57 NTSTATUS nt_status;
58 unsigned int i, num_sids = 0;
60 const char *filter;
62 struct dom_sid *sids = NULL;
63 const struct dom_sid *anonymous_sid, *system_sid;
65 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
66 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
68 session_info = talloc_zero(tmp_ctx, struct auth_session_info);
69 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(session_info, tmp_ctx);
71 session_info->info = talloc_reference(session_info, user_info_dc->info);
73 session_info->torture = talloc_zero(session_info, struct auth_user_info_torture);
74 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(session_info->torture, tmp_ctx);
75 session_info->torture->num_dc_sids = user_info_dc->num_sids;
76 session_info->torture->dc_sids = talloc_reference(session_info, user_info_dc->sids);
77 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(session_info->torture->dc_sids, tmp_ctx);
79 /* unless set otherwise, the session key is the user session
80 * key from the auth subsystem */
81 session_info->session_key = data_blob_talloc(session_info, user_info_dc->user_session_key.data, user_info_dc->user_session_key.length);
82 if (!session_info->session_key.data && session_info->session_key.length) {
83 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(session_info->session_key.data, tmp_ctx);
86 anonymous_sid = dom_sid_parse_talloc(tmp_ctx, SID_NT_ANONYMOUS);
87 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(anonymous_sid, tmp_ctx);
89 system_sid = dom_sid_parse_talloc(tmp_ctx, SID_NT_SYSTEM);
90 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(system_sid, tmp_ctx);
92 sids = talloc_array(tmp_ctx, struct dom_sid, user_info_dc->num_sids);
93 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(sids, tmp_ctx);
94 if (!sids) {
95 talloc_free(tmp_ctx);
96 return NT_STATUS_NO_MEMORY;
99 num_sids = user_info_dc->num_sids;
101 for (i=0; i < user_info_dc->num_sids; i++) {
102 sids[i] = user_info_dc->sids[i];
105 if (user_info_dc->num_sids > PRIMARY_USER_SID_INDEX && dom_sid_equal(anonymous_sid, &user_info_dc->sids[PRIMARY_USER_SID_INDEX])) {
106 /* Don't expand nested groups of system, anonymous etc*/
107 } else if (user_info_dc->num_sids > PRIMARY_USER_SID_INDEX && dom_sid_equal(system_sid, &user_info_dc->sids[PRIMARY_USER_SID_INDEX])) {
108 /* Don't expand nested groups of system, anonymous etc*/
109 } else if (sam_ctx) {
110 filter = talloc_asprintf(tmp_ctx, "(&(objectClass=group)(groupType:1.2.840.113556.1.4.803:=%u))",
111 GROUP_TYPE_BUILTIN_LOCAL_GROUP);
113 /* Search for each group in the token */
114 for (i = 0; i < user_info_dc->num_sids; i++) {
115 char *sid_string;
116 const char *sid_dn;
117 DATA_BLOB sid_blob;
119 sid_string = dom_sid_string(tmp_ctx,
120 &user_info_dc->sids[i]);
121 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(sid_string, user_info_dc);
123 sid_dn = talloc_asprintf(tmp_ctx, "<SID=%s>", sid_string);
124 talloc_free(sid_string);
125 NT_STATUS_HAVE_NO_MEMORY_AND_FREE(sid_dn, user_info_dc);
126 sid_blob = data_blob_string_const(sid_dn);
128 /* This function takes in memberOf values and expands
129 * them, as long as they meet the filter - so only
130 * builtin groups
132 * We already have the SID in the token, so set
133 * 'only childs' flag to true */
134 nt_status = dsdb_expand_nested_groups(sam_ctx, &sid_blob, true, filter,
135 tmp_ctx, &sids, &num_sids);
136 if (!NT_STATUS_IS_OK(nt_status)) {
137 talloc_free(tmp_ctx);
138 return nt_status;
143 nt_status = security_token_create(session_info,
144 lp_ctx,
145 num_sids,
146 sids,
147 session_info_flags,
148 &session_info->security_token);
149 NT_STATUS_NOT_OK_RETURN_AND_FREE(nt_status, tmp_ctx);
151 session_info->credentials = NULL;
153 talloc_steal(mem_ctx, session_info);
154 *_session_info = session_info;
155 talloc_free(tmp_ctx);
156 return NT_STATUS_OK;
160 /* Fill out the auth_session_info with a cli_credentials based on the
161 * auth_session_info we were forwarded over named pipe forwarding.
163 * NOTE: The stucture members of session_info_transport are stolen
164 * with talloc_move() into auth_session_info for long term use
166 struct auth_session_info *auth_session_info_from_transport(TALLOC_CTX *mem_ctx,
167 struct auth_session_info_transport *session_info_transport,
168 struct loadparm_context *lp_ctx,
169 const char **reason)
171 struct auth_session_info *session_info;
172 session_info = talloc_steal(mem_ctx, session_info_transport->session_info);
173 #ifdef HAVE_GSS_IMPORT_CRED
174 if (session_info_transport->exported_gssapi_credentials.length) {
175 struct cli_credentials *creds;
176 OM_uint32 minor_status;
177 gss_buffer_desc cred_token;
178 gss_cred_id_t cred_handle;
179 const char *error_string;
180 int ret;
182 DEBUG(10, ("Delegated credentials supplied by client\n"));
184 cred_token.value = session_info_transport->exported_gssapi_credentials.data;
185 cred_token.length = session_info_transport->exported_gssapi_credentials.length;
187 ret = gss_import_cred(&minor_status,
188 &cred_token,
189 &cred_handle);
190 if (ret != GSS_S_COMPLETE) {
191 *reason = "Internal error in gss_import_cred()";
192 return NULL;
195 creds = cli_credentials_init(session_info);
196 if (!creds) {
197 *reason = "Out of memory in cli_credentials_init()";
198 return NULL;
200 session_info->credentials = creds;
202 cli_credentials_set_conf(creds, lp_ctx);
203 /* Just so we don't segfault trying to get at a username */
204 cli_credentials_set_anonymous(creds);
206 ret = cli_credentials_set_client_gss_creds(creds,
207 lp_ctx,
208 cred_handle,
209 CRED_SPECIFIED,
210 &error_string);
211 if (ret) {
212 *reason = talloc_asprintf(mem_ctx,
213 "Failed to set pipe forwarded"
214 "creds: %s\n", error_string);
215 return NULL;
218 /* This credential handle isn't useful for password
219 * authentication, so ensure nobody tries to do that */
220 cli_credentials_set_kerberos_state(creds,
221 CRED_MUST_USE_KERBEROS);
224 #endif
225 return session_info;
229 /* Create a auth_session_info_transport from an auth_session_info.
231 * NOTE: Members of the auth_session_info_transport structure are
232 * talloc_referenced() into this structure, and should not be changed.
234 NTSTATUS auth_session_info_transport_from_session(TALLOC_CTX *mem_ctx,
235 struct auth_session_info *session_info,
236 struct tevent_context *event_ctx,
237 struct loadparm_context *lp_ctx,
238 struct auth_session_info_transport **transport_out)
241 struct auth_session_info_transport *session_info_transport
242 = talloc_zero(mem_ctx, struct auth_session_info_transport);
243 if (!session_info_transport) {
244 return NT_STATUS_NO_MEMORY;
246 session_info_transport->session_info = talloc_reference(session_info_transport, session_info);
247 if (!session_info_transport->session_info) {
248 return NT_STATUS_NO_MEMORY;
250 #ifdef HAVE_GSS_EXPORT_CRED
251 if (session_info->credentials) {
252 struct gssapi_creds_container *gcc;
253 OM_uint32 gret;
254 OM_uint32 minor_status;
255 gss_buffer_desc cred_token;
256 const char *error_string;
257 int ret;
259 ret = cli_credentials_get_client_gss_creds(session_info->credentials,
260 event_ctx,
261 lp_ctx,
262 &gcc, &error_string);
263 if (ret != 0) {
264 *transport_out = session_info_transport;
265 return NT_STATUS_OK;
268 gret = gss_export_cred(&minor_status,
269 gcc->creds,
270 &cred_token);
271 if (gret != GSS_S_COMPLETE) {
272 return NT_STATUS_INTERNAL_ERROR;
275 if (cred_token.length) {
276 session_info_transport->exported_gssapi_credentials
277 = data_blob_talloc(session_info_transport,
278 cred_token.value,
279 cred_token.length);
280 gss_release_buffer(&minor_status, &cred_token);
281 NT_STATUS_HAVE_NO_MEMORY(session_info_transport->exported_gssapi_credentials.data);
284 #endif
285 *transport_out = session_info_transport;
286 return NT_STATUS_OK;
290 /* Produce a session_info for an arbitary DN or principal in the local
291 * DB, assuming the local DB holds all the groups
293 * Supply either a principal or a DN
295 NTSTATUS authsam_get_session_info_principal(TALLOC_CTX *mem_ctx,
296 struct loadparm_context *lp_ctx,
297 struct ldb_context *sam_ctx,
298 const char *principal,
299 struct ldb_dn *user_dn,
300 uint32_t session_info_flags,
301 struct auth_session_info **session_info)
303 NTSTATUS nt_status;
304 struct auth_user_info_dc *user_info_dc;
305 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
306 if (!tmp_ctx) {
307 return NT_STATUS_NO_MEMORY;
309 nt_status = authsam_get_user_info_dc_principal(tmp_ctx, lp_ctx, sam_ctx,
310 principal, user_dn,
311 &user_info_dc);
312 if (!NT_STATUS_IS_OK(nt_status)) {
313 talloc_free(tmp_ctx);
314 return nt_status;
317 nt_status = auth_generate_session_info(tmp_ctx, lp_ctx, sam_ctx,
318 user_info_dc, session_info_flags,
319 session_info);
321 if (NT_STATUS_IS_OK(nt_status)) {
322 talloc_steal(mem_ctx, *session_info);
324 talloc_free(tmp_ctx);
325 return nt_status;
329 * prints a struct auth_session_info security token to debug output.
331 void auth_session_info_debug(int dbg_lev,
332 const struct auth_session_info *session_info)
334 if (!session_info) {
335 DEBUG(dbg_lev, ("Session Info: (NULL)\n"));
336 return;
339 security_token_debug(0, dbg_lev, session_info->security_token);