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