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