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