auth4: Remove an unused variable
[mat/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 _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         NT_STATUS_HAVE_NO_MEMORY_AND_FREE(session_info, tmp_ctx);
70
71         session_info->info = talloc_reference(session_info, user_info_dc->info);
72
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);
78
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);
84         }
85
86         anonymous_sid = dom_sid_parse_talloc(tmp_ctx, SID_NT_ANONYMOUS);
87         NT_STATUS_HAVE_NO_MEMORY_AND_FREE(anonymous_sid, tmp_ctx);
88
89         system_sid = dom_sid_parse_talloc(tmp_ctx, SID_NT_SYSTEM);
90         NT_STATUS_HAVE_NO_MEMORY_AND_FREE(system_sid, tmp_ctx);
91
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;
97         }
98
99         num_sids = user_info_dc->num_sids;
100
101         for (i=0; i < user_info_dc->num_sids; i++) {
102                 sids[i] = user_info_dc->sids[i];
103         }
104
105         /*
106          * Finally add the "standard" sids.
107          * The only difference between guest and "anonymous"
108          * is the addition of Authenticated_Users.
109          */
110
111         if (session_info_flags & AUTH_SESSION_INFO_DEFAULT_GROUPS) {
112                 sids = talloc_realloc(tmp_ctx, sids, struct dom_sid, num_sids + 2);
113                 NT_STATUS_HAVE_NO_MEMORY(sids);
114
115                 if (!dom_sid_parse(SID_WORLD, &sids[num_sids])) {
116                         return NT_STATUS_INTERNAL_ERROR;
117                 }
118                 num_sids++;
119
120                 if (!dom_sid_parse(SID_NT_NETWORK, &sids[num_sids])) {
121                         return NT_STATUS_INTERNAL_ERROR;
122                 }
123                 num_sids++;
124         }
125
126         if (session_info_flags & AUTH_SESSION_INFO_AUTHENTICATED) {
127                 sids = talloc_realloc(tmp_ctx, sids, struct dom_sid, num_sids + 1);
128                 NT_STATUS_HAVE_NO_MEMORY(sids);
129
130                 if (!dom_sid_parse(SID_NT_AUTHENTICATED_USERS, &sids[num_sids])) {
131                         return NT_STATUS_INTERNAL_ERROR;
132                 }
133                 num_sids++;
134         }
135
136
137
138         if (num_sids > PRIMARY_USER_SID_INDEX && dom_sid_equal(anonymous_sid, &sids[PRIMARY_USER_SID_INDEX])) {
139                 /* Don't expand nested groups of system, anonymous etc*/
140         } else if (num_sids > PRIMARY_USER_SID_INDEX && dom_sid_equal(system_sid, &sids[PRIMARY_USER_SID_INDEX])) {
141                 /* Don't expand nested groups of system, anonymous etc*/
142         } else if (sam_ctx) {
143                 filter = talloc_asprintf(tmp_ctx, "(&(objectClass=group)(groupType:1.2.840.113556.1.4.803:=%u))",
144                                          GROUP_TYPE_BUILTIN_LOCAL_GROUP);
145
146                 /* Search for each group in the token */
147                 for (i = 0; i < num_sids; i++) {
148                         char *sid_string;
149                         const char *sid_dn;
150                         DATA_BLOB sid_blob;
151
152                         sid_string = dom_sid_string(tmp_ctx,
153                                                       &sids[i]);
154                         NT_STATUS_HAVE_NO_MEMORY_AND_FREE(sid_string, user_info_dc);
155                         
156                         sid_dn = talloc_asprintf(tmp_ctx, "<SID=%s>", sid_string);
157                         talloc_free(sid_string);
158                         NT_STATUS_HAVE_NO_MEMORY_AND_FREE(sid_dn, user_info_dc);
159                         sid_blob = data_blob_string_const(sid_dn);
160                         
161                         /* This function takes in memberOf values and expands
162                          * them, as long as they meet the filter - so only
163                          * builtin groups
164                          *
165                          * We already have the SID in the token, so set
166                          * 'only childs' flag to true */
167                         nt_status = dsdb_expand_nested_groups(sam_ctx, &sid_blob, true, filter,
168                                                               tmp_ctx, &sids, &num_sids);
169                         if (!NT_STATUS_IS_OK(nt_status)) {
170                                 talloc_free(tmp_ctx);
171                                 return nt_status;
172                         }
173                 }
174         }
175
176         nt_status = security_token_create(session_info,
177                                           lp_ctx,
178                                           num_sids,
179                                           sids,
180                                           session_info_flags,
181                                           &session_info->security_token);
182         NT_STATUS_NOT_OK_RETURN_AND_FREE(nt_status, tmp_ctx);
183
184         session_info->credentials = NULL;
185
186         talloc_steal(mem_ctx, session_info);
187         *_session_info = session_info;
188         talloc_free(tmp_ctx);
189         return NT_STATUS_OK;
190 }
191
192
193 /* Fill out the auth_session_info with a cli_credentials based on the
194  * auth_session_info we were forwarded over named pipe forwarding.
195  *
196  * NOTE: The stucture members of session_info_transport are stolen
197  * with talloc_move() into auth_session_info for long term use
198  */
199 struct auth_session_info *auth_session_info_from_transport(TALLOC_CTX *mem_ctx,
200                                                            struct auth_session_info_transport *session_info_transport,
201                                                            struct loadparm_context *lp_ctx,
202                                                            const char **reason)
203 {
204         struct auth_session_info *session_info;
205         session_info = talloc_steal(mem_ctx, session_info_transport->session_info);
206 #ifdef HAVE_GSS_IMPORT_CRED
207         if (session_info_transport->exported_gssapi_credentials.length) {
208                 struct cli_credentials *creds;
209                 OM_uint32 minor_status;
210                 gss_buffer_desc cred_token;
211                 gss_cred_id_t cred_handle;
212                 const char *error_string;
213                 int ret;
214
215                 DEBUG(10, ("Delegated credentials supplied by client\n"));
216
217                 cred_token.value = session_info_transport->exported_gssapi_credentials.data;
218                 cred_token.length = session_info_transport->exported_gssapi_credentials.length;
219
220                 ret = gss_import_cred(&minor_status,
221                                       &cred_token,
222                                       &cred_handle);
223                 if (ret != GSS_S_COMPLETE) {
224                         *reason = "Internal error in gss_import_cred()";
225                         return NULL;
226                 }
227
228                 creds = cli_credentials_init(session_info);
229                 if (!creds) {
230                         *reason = "Out of memory in cli_credentials_init()";
231                         return NULL;
232                 }
233                 session_info->credentials = creds;
234
235                 cli_credentials_set_conf(creds, lp_ctx);
236                 /* Just so we don't segfault trying to get at a username */
237                 cli_credentials_set_anonymous(creds);
238
239                 ret = cli_credentials_set_client_gss_creds(creds,
240                                                            lp_ctx,
241                                                            cred_handle,
242                                                            CRED_SPECIFIED,
243                                                            &error_string);
244                 if (ret) {
245                         *reason = talloc_asprintf(mem_ctx,
246                                                   "Failed to set pipe forwarded"
247                                                   "creds: %s\n", error_string);
248                         return NULL;
249                 }
250
251                 /* This credential handle isn't useful for password
252                  * authentication, so ensure nobody tries to do that */
253                 cli_credentials_set_kerberos_state(creds,
254                                                    CRED_MUST_USE_KERBEROS);
255
256         }
257 #endif
258         return session_info;
259 }
260
261
262 /* Create a auth_session_info_transport from an auth_session_info.
263  *
264  * NOTE: Members of the auth_session_info_transport structure are
265  * talloc_referenced() into this structure, and should not be changed.
266  */
267 NTSTATUS auth_session_info_transport_from_session(TALLOC_CTX *mem_ctx,
268                                                   struct auth_session_info *session_info,
269                                                   struct tevent_context *event_ctx,
270                                                   struct loadparm_context *lp_ctx,
271                                                   struct auth_session_info_transport **transport_out)
272 {
273
274         struct auth_session_info_transport *session_info_transport
275                 = talloc_zero(mem_ctx, struct auth_session_info_transport);
276         if (!session_info_transport) {
277                 return NT_STATUS_NO_MEMORY;
278         };
279         session_info_transport->session_info = talloc_reference(session_info_transport, session_info);
280         if (!session_info_transport->session_info) {
281                 return NT_STATUS_NO_MEMORY;
282         };
283 #ifdef HAVE_GSS_EXPORT_CRED
284         if (session_info->credentials) {
285                 struct gssapi_creds_container *gcc;
286                 OM_uint32 gret;
287                 OM_uint32 minor_status;
288                 gss_buffer_desc cred_token;
289                 const char *error_string;
290                 int ret;
291
292                 ret = cli_credentials_get_client_gss_creds(session_info->credentials,
293                                                            event_ctx,
294                                                            lp_ctx,
295                                                            &gcc, &error_string);
296                 if (ret != 0) {
297                         *transport_out = session_info_transport;
298                         return NT_STATUS_OK;
299                 }
300
301                 gret = gss_export_cred(&minor_status,
302                                        gcc->creds,
303                                        &cred_token);
304                 if (gret != GSS_S_COMPLETE) {
305                         return NT_STATUS_INTERNAL_ERROR;
306                 }
307
308                 if (cred_token.length) {
309                         session_info_transport->exported_gssapi_credentials
310                                 = data_blob_talloc(session_info_transport,
311                                                    cred_token.value,
312                                                    cred_token.length);
313                         gss_release_buffer(&minor_status, &cred_token);
314                         NT_STATUS_HAVE_NO_MEMORY(session_info_transport->exported_gssapi_credentials.data);
315                 }
316         }
317 #endif
318         *transport_out = session_info_transport;
319         return NT_STATUS_OK;
320 }
321
322
323 /* Produce a session_info for an arbitary DN or principal in the local
324  * DB, assuming the local DB holds all the groups
325  *
326  * Supply either a principal or a DN
327  */
328 NTSTATUS authsam_get_session_info_principal(TALLOC_CTX *mem_ctx,
329                                             struct loadparm_context *lp_ctx,
330                                             struct ldb_context *sam_ctx,
331                                             const char *principal,
332                                             struct ldb_dn *user_dn,
333                                             uint32_t session_info_flags,
334                                             struct auth_session_info **session_info)
335 {
336         NTSTATUS nt_status;
337         struct auth_user_info_dc *user_info_dc;
338         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
339         if (!tmp_ctx) {
340                 return NT_STATUS_NO_MEMORY;
341         }
342         nt_status = authsam_get_user_info_dc_principal(tmp_ctx, lp_ctx, sam_ctx,
343                                                       principal, user_dn,
344                                                       &user_info_dc);
345         if (!NT_STATUS_IS_OK(nt_status)) {
346                 talloc_free(tmp_ctx);
347                 return nt_status;
348         }
349
350         nt_status = auth_generate_session_info(tmp_ctx, lp_ctx, sam_ctx,
351                                                user_info_dc, session_info_flags,
352                                                session_info);
353
354         if (NT_STATUS_IS_OK(nt_status)) {
355                 talloc_steal(mem_ctx, *session_info);
356         }
357         talloc_free(tmp_ctx);
358         return nt_status;
359 }
360
361 /**
362  * prints a struct auth_session_info security token to debug output.
363  */
364 void auth_session_info_debug(int dbg_lev, 
365                              const struct auth_session_info *session_info)
366 {
367         if (!session_info) {
368                 DEBUG(dbg_lev, ("Session Info: (NULL)\n"));
369                 return; 
370         }
371
372         security_token_debug(0, dbg_lev, session_info->security_token);
373 }