Make krb5 wrapper library common so they can be used all over
[idra/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 "../libcli/auth/spnego.h"
34 #include "auth/kerberos/pac_utils.h"
35
36 #ifdef HAVE_KRB5
37
38 struct smb_krb5_context;
39
40 /****************************************************************
41 Callback to get the PAC_LOGON_INFO from the blob
42 ****************************************************************/
43 static NTSTATUS kerberos_fetch_pac(struct auth4_context *auth_ctx,
44                                    TALLOC_CTX *mem_ctx,
45                                    struct smb_krb5_context *smb_krb5_context,
46                                    DATA_BLOB *pac_blob,
47                                    const char *princ_name,
48                                    const struct tsocket_address *remote_address,
49                                    uint32_t session_info_flags,
50                                    struct auth_session_info **session_info)
51 {
52         TALLOC_CTX *tmp_ctx;
53         struct PAC_DATA *pac_data = NULL;
54         struct PAC_LOGON_INFO *logon_info = NULL;
55         unsigned int i;
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_decode_pac(tmp_ctx,
65                                      *pac_blob,
66                                      NULL, NULL, NULL, NULL, 0, &pac_data);
67                 if (!NT_STATUS_IS_OK(status)) {
68                         goto done;
69                 }
70
71                 /* get logon name and logon info */
72                 for (i = 0; i < pac_data->num_buffers; i++) {
73                         struct PAC_BUFFER *data_buf = &pac_data->buffers[i];
74
75                         switch (data_buf->type) {
76                         case PAC_TYPE_LOGON_INFO:
77                                 if (!data_buf->info) {
78                                         break;
79                                 }
80                                 logon_info = data_buf->info->logon_info.info;
81                                 break;
82                         default:
83                                 break;
84                         }
85                 }
86                 if (!logon_info) {
87                         DEBUG(1, ("Invalid PAC data, missing logon info!\n"));
88                         status = NT_STATUS_NOT_FOUND;
89                         goto done;
90                 }
91         }
92         talloc_set_name_const(logon_info, "struct PAC_LOGON_INFO");
93
94         auth_ctx->private_data = talloc_steal(auth_ctx, logon_info);
95         *session_info = talloc_zero(mem_ctx, struct auth_session_info);
96         if (!*session_info) {
97                 status = NT_STATUS_NO_MEMORY;
98                 goto done;
99         }
100         status = NT_STATUS_OK;
101
102 done:
103         TALLOC_FREE(tmp_ctx);
104
105         return status;
106 }
107
108 /*
109  * Given the username/password, do a kinit, store the ticket in
110  * cache_name if specified, and return the PAC_LOGON_INFO (the
111  * structure containing the important user information such as
112  * groups).
113  */
114 NTSTATUS kerberos_return_pac(TALLOC_CTX *mem_ctx,
115                              const char *name,
116                              const char *pass,
117                              time_t time_offset,
118                              time_t *expire_time,
119                              time_t *renew_till_time,
120                              const char *cache_name,
121                              bool request_pac,
122                              bool add_netbios_addr,
123                              time_t renewable_time,
124                              const char *impersonate_princ_s,
125                              struct PAC_LOGON_INFO **_logon_info)
126 {
127         krb5_error_code ret;
128         NTSTATUS status = NT_STATUS_INVALID_PARAMETER;
129         DATA_BLOB tkt, tkt_wrapped, ap_rep, sesskey1;
130         const char *auth_princ = NULL;
131         const char *local_service = NULL;
132         const char *cc = "MEMORY:kerberos_return_pac";
133         struct auth_session_info *session_info;
134         struct gensec_security *gensec_server_context;
135
136         struct gensec_settings *gensec_settings;
137         size_t idx = 0;
138         struct auth4_context *auth_context;
139         struct loadparm_context *lp_ctx;
140         struct PAC_LOGON_INFO *logon_info = NULL;
141
142         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
143         NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
144
145         ZERO_STRUCT(tkt);
146         ZERO_STRUCT(ap_rep);
147         ZERO_STRUCT(sesskey1);
148
149         if (!name || !pass) {
150                 return NT_STATUS_INVALID_PARAMETER;
151         }
152
153         if (cache_name) {
154                 cc = cache_name;
155         }
156
157         if (!strchr_m(name, '@')) {
158                 auth_princ = talloc_asprintf(mem_ctx, "%s@%s", name,
159                         lp_realm());
160         } else {
161                 auth_princ = name;
162         }
163         NT_STATUS_HAVE_NO_MEMORY(auth_princ);
164
165         local_service = talloc_asprintf(mem_ctx, "%s$@%s",
166                                         lp_netbios_name(), lp_realm());
167         NT_STATUS_HAVE_NO_MEMORY(local_service);
168
169         ret = kerberos_kinit_password_ext(auth_princ,
170                                           pass,
171                                           time_offset,
172                                           expire_time,
173                                           renew_till_time,
174                                           cc,
175                                           request_pac,
176                                           add_netbios_addr,
177                                           renewable_time,
178                                           &status);
179         if (ret) {
180                 DEBUG(1,("kinit failed for '%s' with: %s (%d)\n",
181                         auth_princ, error_message(ret), ret));
182                 /* status already set */
183                 goto out;
184         }
185
186         DEBUG(10,("got TGT for %s in %s\n", auth_princ, cc));
187         if (expire_time) {
188                 DEBUGADD(10,("\tvalid until: %s (%d)\n",
189                         http_timestring(talloc_tos(), *expire_time),
190                         (int)*expire_time));
191         }
192         if (renew_till_time) {
193                 DEBUGADD(10,("\trenewable till: %s (%d)\n",
194                         http_timestring(talloc_tos(), *renew_till_time),
195                         (int)*renew_till_time));
196         }
197
198         /* we cannot continue with krb5 when UF_DONT_REQUIRE_PREAUTH is set,
199          * in that case fallback to NTLM - gd */
200
201         if (expire_time && renew_till_time &&
202             (*expire_time == 0) && (*renew_till_time == 0)) {
203                 return NT_STATUS_INVALID_LOGON_TYPE;
204         }
205
206         ret = cli_krb5_get_ticket(mem_ctx,
207                                   local_service,
208                                   time_offset,
209                                   &tkt,
210                                   &sesskey1,
211                                   0,
212                                   cc,
213                                   NULL,
214                                   impersonate_princ_s);
215         if (ret) {
216                 DEBUG(1,("failed to get ticket for %s: %s\n",
217                         local_service, error_message(ret)));
218                 if (impersonate_princ_s) {
219                         DEBUGADD(1,("tried S4U2SELF impersonation as: %s\n",
220                                 impersonate_princ_s));
221                 }
222                 status = krb5_to_nt_status(ret);
223                 goto out;
224         }
225
226         /* wrap that up in a nice GSS-API wrapping */
227         tkt_wrapped = spnego_gen_krb5_wrap(tmp_ctx, tkt, TOK_ID_KRB_AP_REQ);
228         if (tkt_wrapped.data == NULL) {
229                 status = NT_STATUS_NO_MEMORY;
230                 goto out;
231         }
232
233         auth_context = talloc_zero(tmp_ctx, struct auth4_context);
234         if (auth_context == NULL) {
235                 status = NT_STATUS_NO_MEMORY;
236                 goto out;
237         }
238         auth_context->generate_session_info_pac = kerberos_fetch_pac;
239
240         lp_ctx = loadparm_init_s3(tmp_ctx, loadparm_s3_context());
241         if (lp_ctx == NULL) {
242                 status = NT_STATUS_INVALID_SERVER_STATE;
243                 DEBUG(10, ("loadparm_init_s3 failed\n"));
244                 goto out;
245         }
246
247         gensec_settings = lpcfg_gensec_settings(tmp_ctx, lp_ctx);
248         if (lp_ctx == NULL) {
249                 status = NT_STATUS_NO_MEMORY;
250                 DEBUG(10, ("lpcfg_gensec_settings failed\n"));
251                 goto out;
252         }
253
254         gensec_settings->backends = talloc_zero_array(gensec_settings,
255                                                       struct gensec_security_ops *, 2);
256         if (gensec_settings->backends == NULL) {
257                 status = NT_STATUS_NO_MEMORY;
258                 goto out;
259         }
260
261         gensec_init();
262
263         gensec_settings->backends[idx++] = &gensec_gse_krb5_security_ops;
264
265         status = gensec_server_start(tmp_ctx, gensec_settings,
266                                         auth_context, &gensec_server_context);
267
268         if (!NT_STATUS_IS_OK(status)) {
269                 DEBUG(1, (__location__ "Failed to start server-side GENSEC to validate a Kerberos ticket: %s\n", nt_errstr(status)));
270                 goto out;
271         }
272
273         talloc_unlink(tmp_ctx, lp_ctx);
274         talloc_unlink(tmp_ctx, gensec_settings);
275         talloc_unlink(tmp_ctx, auth_context);
276
277         status = gensec_start_mech_by_oid(gensec_server_context, GENSEC_OID_KERBEROS5);
278         if (!NT_STATUS_IS_OK(status)) {
279                 DEBUG(1, (__location__ "Failed to start server-side GENSEC krb5 to validate a Kerberos ticket: %s\n", nt_errstr(status)));
280                 goto out;
281         }
282
283         /* Do a client-server update dance */
284         status = gensec_update(gensec_server_context, tmp_ctx, NULL, tkt_wrapped, &ap_rep);
285         if (!NT_STATUS_IS_OK(status)) {
286                 DEBUG(1, ("gensec_update() failed: %s\n", nt_errstr(status)));
287                 goto out;
288         }
289
290         /* Now return the PAC information to the callers.  We ingore
291          * the session_info and instead pick out the PAC via the
292          * private_data on the auth_context */
293         status = gensec_session_info(gensec_server_context, tmp_ctx, &session_info);
294         if (!NT_STATUS_IS_OK(status)) {
295                 DEBUG(1, ("Unable to obtain PAC via gensec_session_info\n"));
296                 goto out;
297         }
298
299         logon_info = talloc_get_type_abort(gensec_server_context->auth_context->private_data,
300                                            struct PAC_LOGON_INFO);
301         if (logon_info == NULL) {
302                 DEBUG(1,("no PAC\n"));
303                 status = NT_STATUS_INVALID_PARAMETER;
304                 goto out;
305         }
306
307         *_logon_info = talloc_move(mem_ctx, &logon_info);
308
309 out:
310         talloc_free(tmp_ctx);
311         if (cc != cache_name) {
312                 ads_kdestroy(cc);
313         }
314
315         data_blob_free(&tkt);
316         data_blob_free(&ap_rep);
317         data_blob_free(&sesskey1);
318
319         return status;
320 }
321
322 #endif