dd80dc24e44171bc7202632fae9fc9fa44ca7ef9
[samba.git] / source3 / libads / authdata.c
1 /*
2    Unix SMB/CIFS implementation.
3    kerberos authorization data (PAC) utility library
4    Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2003
5    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2004-2005
6    Copyright (C) Andrew Tridgell 2001
7    Copyright (C) Luke Howard 2002-2003
8    Copyright (C) Stefan Metzmacher 2004-2005
9    Copyright (C) Guenther Deschner 2005,2007,2008
10
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 3 of the License, or
14    (at your option) any later version.
15
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with this program.  If not, see <http://www.gnu.org/licenses/>.
23 */
24
25 #include "includes.h"
26 #include "librpc/gen_ndr/ndr_krb5pac.h"
27 #include "smb_krb5.h"
28 #include "libads/kerberos_proto.h"
29 #include "auth/common_auth.h"
30 #include "lib/param/param.h"
31 #include "librpc/crypto/gse.h"
32 #include "auth/gensec/gensec.h"
33 #include "auth/gensec/gensec_internal.h" /* TODO: remove this */
34 #include "../libcli/auth/spnego.h"
35
36 #ifdef HAVE_KRB5
37
38 #include "auth/kerberos/pac_utils.h"
39
40 struct smb_krb5_context;
41
42 /****************************************************************
43 Callback to get the PAC_LOGON_INFO from the blob
44 ****************************************************************/
45 static NTSTATUS kerberos_fetch_pac(struct auth4_context *auth_ctx,
46                                    TALLOC_CTX *mem_ctx,
47                                    struct smb_krb5_context *smb_krb5_context,
48                                    DATA_BLOB *pac_blob,
49                                    const char *princ_name,
50                                    const struct tsocket_address *remote_address,
51                                    uint32_t session_info_flags,
52                                    struct auth_session_info **session_info)
53 {
54         TALLOC_CTX *tmp_ctx;
55         struct PAC_LOGON_INFO *logon_info = NULL;
56         NTSTATUS status = NT_STATUS_INTERNAL_ERROR;
57
58         tmp_ctx = talloc_new(mem_ctx);
59         if (!tmp_ctx) {
60                 return NT_STATUS_NO_MEMORY;
61         }
62
63         if (pac_blob) {
64                 status = kerberos_pac_logon_info(tmp_ctx, *pac_blob, NULL, NULL,
65                                                  NULL, NULL, 0, &logon_info);
66                 if (!NT_STATUS_IS_OK(status)) {
67                         goto done;
68                 }
69         }
70
71         talloc_set_name_const(logon_info, "struct PAC_LOGON_INFO");
72
73         auth_ctx->private_data = talloc_steal(auth_ctx, logon_info);
74         *session_info = talloc_zero(mem_ctx, struct auth_session_info);
75         if (!*session_info) {
76                 status = NT_STATUS_NO_MEMORY;
77                 goto done;
78         }
79         status = NT_STATUS_OK;
80
81 done:
82         TALLOC_FREE(tmp_ctx);
83
84         return status;
85 }
86
87 /*
88  * Given the username/password, do a kinit, store the ticket in
89  * cache_name if specified, and return the PAC_LOGON_INFO (the
90  * structure containing the important user information such as
91  * groups).
92  */
93 NTSTATUS kerberos_return_pac(TALLOC_CTX *mem_ctx,
94                              const char *name,
95                              const char *pass,
96                              time_t time_offset,
97                              time_t *expire_time,
98                              time_t *renew_till_time,
99                              const char *cache_name,
100                              bool request_pac,
101                              bool add_netbios_addr,
102                              time_t renewable_time,
103                              const char *impersonate_princ_s,
104                              const char *local_service,
105                              struct PAC_LOGON_INFO **_logon_info)
106 {
107         krb5_error_code ret;
108         NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
109         DATA_BLOB tkt, tkt_wrapped, ap_rep, sesskey1;
110         const char *auth_princ = NULL;
111         const char *cc = "MEMORY:kerberos_return_pac";
112         struct auth_session_info *session_info;
113         struct gensec_security *gensec_server_context;
114         const struct gensec_security_ops **backends;
115         struct gensec_settings *gensec_settings;
116         size_t idx = 0;
117         struct auth4_context *auth_context;
118         struct loadparm_context *lp_ctx;
119         struct PAC_LOGON_INFO *logon_info = NULL;
120
121         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
122         NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
123
124         ZERO_STRUCT(tkt);
125         ZERO_STRUCT(ap_rep);
126         ZERO_STRUCT(sesskey1);
127
128         if (!name || !pass) {
129                 return NT_STATUS_INVALID_PARAMETER;
130         }
131
132         if (cache_name) {
133                 cc = cache_name;
134         }
135
136         if (!strchr_m(name, '@')) {
137                 auth_princ = talloc_asprintf(mem_ctx, "%s@%s", name,
138                         lp_realm());
139         } else {
140                 auth_princ = name;
141         }
142         NT_STATUS_HAVE_NO_MEMORY(auth_princ);
143
144         ret = kerberos_kinit_password_ext(auth_princ,
145                                           pass,
146                                           time_offset,
147                                           expire_time,
148                                           renew_till_time,
149                                           cc,
150                                           request_pac,
151                                           add_netbios_addr,
152                                           renewable_time,
153                                           &status);
154         if (ret) {
155                 DEBUG(1,("kinit failed for '%s' with: %s (%d)\n",
156                         auth_princ, error_message(ret), ret));
157                 /* status already set */
158                 goto out;
159         }
160
161         DEBUG(10,("got TGT for %s in %s\n", auth_princ, cc));
162         if (expire_time) {
163                 DEBUGADD(10,("\tvalid until: %s (%d)\n",
164                         http_timestring(talloc_tos(), *expire_time),
165                         (int)*expire_time));
166         }
167         if (renew_till_time) {
168                 DEBUGADD(10,("\trenewable till: %s (%d)\n",
169                         http_timestring(talloc_tos(), *renew_till_time),
170                         (int)*renew_till_time));
171         }
172
173         /* we cannot continue with krb5 when UF_DONT_REQUIRE_PREAUTH is set,
174          * in that case fallback to NTLM - gd */
175
176         if (expire_time && renew_till_time &&
177             (*expire_time == 0) && (*renew_till_time == 0)) {
178                 return NT_STATUS_INVALID_LOGON_TYPE;
179         }
180
181         ret = cli_krb5_get_ticket(mem_ctx,
182                                   local_service,
183                                   time_offset,
184                                   &tkt,
185                                   &sesskey1,
186                                   0,
187                                   cc,
188                                   NULL,
189                                   impersonate_princ_s);
190         if (ret) {
191                 DEBUG(1,("failed to get ticket for %s: %s\n",
192                         local_service, error_message(ret)));
193                 if (impersonate_princ_s) {
194                         DEBUGADD(1,("tried S4U2SELF impersonation as: %s\n",
195                                 impersonate_princ_s));
196                 }
197                 status = krb5_to_nt_status(ret);
198                 goto out;
199         }
200
201         /* wrap that up in a nice GSS-API wrapping */
202         tkt_wrapped = spnego_gen_krb5_wrap(tmp_ctx, tkt, TOK_ID_KRB_AP_REQ);
203         if (tkt_wrapped.data == NULL) {
204                 status = NT_STATUS_NO_MEMORY;
205                 goto out;
206         }
207
208         auth_context = talloc_zero(tmp_ctx, struct auth4_context);
209         if (auth_context == NULL) {
210                 status = NT_STATUS_NO_MEMORY;
211                 goto out;
212         }
213         auth_context->generate_session_info_pac = kerberos_fetch_pac;
214
215         lp_ctx = loadparm_init_s3(tmp_ctx, loadparm_s3_helpers());
216         if (lp_ctx == NULL) {
217                 status = NT_STATUS_INVALID_SERVER_STATE;
218                 DEBUG(10, ("loadparm_init_s3 failed\n"));
219                 goto out;
220         }
221
222         gensec_settings = lpcfg_gensec_settings(tmp_ctx, lp_ctx);
223         if (lp_ctx == NULL) {
224                 status = NT_STATUS_NO_MEMORY;
225                 DEBUG(10, ("lpcfg_gensec_settings failed\n"));
226                 goto out;
227         }
228
229         backends = talloc_zero_array(gensec_settings,
230                                      const struct gensec_security_ops *, 2);
231         if (backends == NULL) {
232                 status = NT_STATUS_NO_MEMORY;
233                 goto out;
234         }
235         gensec_settings->backends = backends;
236
237         gensec_init();
238
239         backends[idx++] = &gensec_gse_krb5_security_ops;
240
241         status = gensec_server_start(tmp_ctx, gensec_settings,
242                                         auth_context, &gensec_server_context);
243
244         if (!NT_STATUS_IS_OK(status)) {
245                 DEBUG(1, (__location__ "Failed to start server-side GENSEC to validate a Kerberos ticket: %s\n", nt_errstr(status)));
246                 goto out;
247         }
248
249         talloc_unlink(tmp_ctx, lp_ctx);
250         talloc_unlink(tmp_ctx, gensec_settings);
251         talloc_unlink(tmp_ctx, auth_context);
252
253         status = gensec_start_mech_by_oid(gensec_server_context, GENSEC_OID_KERBEROS5);
254         if (!NT_STATUS_IS_OK(status)) {
255                 DEBUG(1, (__location__ "Failed to start server-side GENSEC krb5 to validate a Kerberos ticket: %s\n", nt_errstr(status)));
256                 goto out;
257         }
258
259         /* Do a client-server update dance */
260         status = gensec_update(gensec_server_context, tmp_ctx, NULL, tkt_wrapped, &ap_rep);
261         if (!NT_STATUS_IS_OK(status)) {
262                 DEBUG(1, ("gensec_update() failed: %s\n", nt_errstr(status)));
263                 goto out;
264         }
265
266         /* Now return the PAC information to the callers.  We ingore
267          * the session_info and instead pick out the PAC via the
268          * private_data on the auth_context */
269         status = gensec_session_info(gensec_server_context, tmp_ctx, &session_info);
270         if (!NT_STATUS_IS_OK(status)) {
271                 DEBUG(1, ("Unable to obtain PAC via gensec_session_info\n"));
272                 goto out;
273         }
274
275         logon_info = talloc_get_type_abort(gensec_server_context->auth_context->private_data,
276                                            struct PAC_LOGON_INFO);
277         if (logon_info == NULL) {
278                 DEBUG(1,("no PAC\n"));
279                 status = NT_STATUS_INVALID_PARAMETER;
280                 goto out;
281         }
282
283         *_logon_info = talloc_move(mem_ctx, &logon_info);
284
285 out:
286         talloc_free(tmp_ctx);
287         if (cc != cache_name) {
288                 ads_kdestroy(cc);
289         }
290
291         data_blob_free(&tkt);
292         data_blob_free(&ap_rep);
293         data_blob_free(&sesskey1);
294
295         return status;
296 }
297
298 #endif