For now just disable this Heindal specific stuff in the MIT build
[kai/samba-autobuild/.git] / source4 / auth / kerberos / krb5_init_context.c
1 /*
2    Unix SMB/CIFS implementation.
3    Wrapper for krb5_init_context
4
5    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
6    Copyright (C) Andrew Tridgell 2005
7    Copyright (C) Stefan Metzmacher 2004
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "system/kerberos.h"
25 #include <tevent.h>
26 #include "auth/kerberos/kerberos.h"
27 #include "lib/socket/socket.h"
28 #include "lib/stream/packet.h"
29 #include "system/network.h"
30 #include "param/param.h"
31 #include "libcli/resolve/resolve.h"
32 #include "../lib/tsocket/tsocket.h"
33 #include "krb5_init_context.h"
34 /*
35   context structure for operations on cldap packets
36 */
37 struct smb_krb5_socket {
38         struct socket_context *sock;
39
40         /* the fd event */
41         struct tevent_fd *fde;
42
43         NTSTATUS status;
44         DATA_BLOB request, reply;
45
46         struct packet_context *packet;
47
48         size_t partial_read;
49
50         krb5_krbhst_info *hi;
51 };
52
53 static krb5_error_code smb_krb5_context_destroy(struct smb_krb5_context *ctx)
54 {
55 #ifdef SAMBA4_USES_HEIMDAL
56         if (ctx->pvt_log_data) {
57                 /* Otherwise krb5_free_context will try and close what we
58                  * have already free()ed */
59                 krb5_set_warn_dest(ctx->krb5_context, NULL);
60                 krb5_closelog(ctx->krb5_context,
61                                 (krb5_log_facility *)ctx->pvt_log_data);
62         }
63 #else
64         krb5_set_trace_callback(ctx->krb5_context, NULL, NULL);
65 #endif
66         krb5_free_context(ctx->krb5_context);
67         return 0;
68 }
69
70 /* We never close down the DEBUG system, and no need to unreference the use */
71 static void smb_krb5_debug_close(void *private_data) {
72         return;
73 }
74
75 #ifdef SAMBA4_USES_HEIMDAL
76 static void smb_krb5_debug_wrapper(const char *timestr, const char *msg, void *private_data)
77 {
78         DEBUG(3, ("Kerberos: %s\n", msg));
79 }
80 #else
81 static void smb_krb5_debug_wrapper(krb5_context context,
82                                    const struct krb5_trace_info *info,
83                                    void *cb_data)
84 {
85         DEBUG(3, ("Kerberos: %s\n", info->message));
86 }
87 #endif
88
89 #ifdef SAMBA4_USES_HEIMDAL
90 /*
91   handle recv events on a smb_krb5 socket
92 */
93 static void smb_krb5_socket_recv(struct smb_krb5_socket *smb_krb5)
94 {
95         TALLOC_CTX *tmp_ctx = talloc_new(smb_krb5);
96         DATA_BLOB blob;
97         size_t nread, dsize;
98
99         smb_krb5->status = socket_pending(smb_krb5->sock, &dsize);
100         if (!NT_STATUS_IS_OK(smb_krb5->status)) {
101                 talloc_free(tmp_ctx);
102                 return;
103         }
104
105         blob = data_blob_talloc(tmp_ctx, NULL, dsize);
106         if (blob.data == NULL && dsize != 0) {
107                 smb_krb5->status = NT_STATUS_NO_MEMORY;
108                 talloc_free(tmp_ctx);
109                 return;
110         }
111
112         smb_krb5->status = socket_recv(smb_krb5->sock, blob.data, blob.length, &nread);
113         if (!NT_STATUS_IS_OK(smb_krb5->status)) {
114                 talloc_free(tmp_ctx);
115                 return;
116         }
117         blob.length = nread;
118
119         if (nread == 0) {
120                 smb_krb5->status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
121                 talloc_free(tmp_ctx);
122                 return;
123         }
124
125         DEBUG(4,("Received smb_krb5 packet of length %d\n",
126                  (int)blob.length));
127
128         talloc_steal(smb_krb5, blob.data);
129         smb_krb5->reply = blob;
130         talloc_free(tmp_ctx);
131 }
132
133 static NTSTATUS smb_krb5_full_packet(void *private_data, DATA_BLOB data)
134 {
135         struct smb_krb5_socket *smb_krb5 = talloc_get_type(private_data, struct smb_krb5_socket);
136         talloc_steal(smb_krb5, data.data);
137         smb_krb5->reply = data;
138         smb_krb5->reply.length -= 4;
139         smb_krb5->reply.data += 4;
140         return NT_STATUS_OK;
141 }
142
143 /*
144   handle request timeouts
145 */
146 static void smb_krb5_request_timeout(struct tevent_context *event_ctx,
147                                   struct tevent_timer *te, struct timeval t,
148                                   void *private_data)
149 {
150         struct smb_krb5_socket *smb_krb5 = talloc_get_type(private_data, struct smb_krb5_socket);
151         DEBUG(5,("Timed out smb_krb5 packet\n"));
152         smb_krb5->status = NT_STATUS_IO_TIMEOUT;
153 }
154
155 static void smb_krb5_error_handler(void *private_data, NTSTATUS status)
156 {
157         struct smb_krb5_socket *smb_krb5 = talloc_get_type(private_data, struct smb_krb5_socket);
158         smb_krb5->status = status;
159 }
160
161 /*
162   handle send events on a smb_krb5 socket
163 */
164 static void smb_krb5_socket_send(struct smb_krb5_socket *smb_krb5)
165 {
166         NTSTATUS status;
167
168         size_t len;
169
170         len = smb_krb5->request.length;
171         status = socket_send(smb_krb5->sock, &smb_krb5->request, &len);
172
173         if (!NT_STATUS_IS_OK(status)) return;
174
175         TEVENT_FD_READABLE(smb_krb5->fde);
176
177         TEVENT_FD_NOT_WRITEABLE(smb_krb5->fde);
178         return;
179 }
180
181
182 /*
183   handle fd events on a smb_krb5_socket
184 */
185 static void smb_krb5_socket_handler(struct tevent_context *ev, struct tevent_fd *fde,
186                                  uint16_t flags, void *private_data)
187 {
188         struct smb_krb5_socket *smb_krb5 = talloc_get_type(private_data, struct smb_krb5_socket);
189         switch (smb_krb5->hi->proto) {
190         case KRB5_KRBHST_UDP:
191                 if (flags & TEVENT_FD_READ) {
192                         smb_krb5_socket_recv(smb_krb5);
193                         return;
194                 }
195                 if (flags & TEVENT_FD_WRITE) {
196                         smb_krb5_socket_send(smb_krb5);
197                         return;
198                 }
199                 /* not reached */
200                 return;
201         case KRB5_KRBHST_TCP:
202                 if (flags & TEVENT_FD_READ) {
203                         packet_recv(smb_krb5->packet);
204                         return;
205                 }
206                 if (flags & TEVENT_FD_WRITE) {
207                         packet_queue_run(smb_krb5->packet);
208                         return;
209                 }
210                 /* not reached */
211                 return;
212         case KRB5_KRBHST_HTTP:
213                 /* can't happen */
214                 break;
215         }
216 }
217
218 krb5_error_code smb_krb5_send_and_recv_func(krb5_context context,
219                                             void *data,
220                                             krb5_krbhst_info *hi,
221                                             time_t timeout,
222                                             const krb5_data *send_buf,
223                                             krb5_data *recv_buf)
224 {
225         krb5_error_code ret;
226         NTSTATUS status;
227         const char *name;
228         struct addrinfo *ai, *a;
229         struct smb_krb5_socket *smb_krb5;
230
231         DATA_BLOB send_blob;
232
233         struct tevent_context *ev;
234         TALLOC_CTX *tmp_ctx = talloc_new(NULL);
235         if (!tmp_ctx) {
236                 return ENOMEM;
237         }
238
239         if (!data) {
240                 /* If no event context was available, then create one for this loop */
241                 ev = tevent_context_init(tmp_ctx);
242                 if (!ev) {
243                         talloc_free(tmp_ctx);
244                         return ENOMEM;
245                 }
246         } else {
247                 ev = talloc_get_type_abort(data, struct tevent_context);
248         }
249
250         send_blob = data_blob_const(send_buf->data, send_buf->length);
251
252         ret = krb5_krbhst_get_addrinfo(context, hi, &ai);
253         if (ret) {
254                 talloc_free(tmp_ctx);
255                 return ret;
256         }
257
258         for (a = ai; a; a = a->ai_next) {
259                 struct socket_address *remote_addr;
260                 smb_krb5 = talloc(tmp_ctx, struct smb_krb5_socket);
261                 if (!smb_krb5) {
262                         talloc_free(tmp_ctx);
263                         return ENOMEM;
264                 }
265                 smb_krb5->hi = hi;
266
267                 switch (a->ai_family) {
268                 case PF_INET:
269                         name = "ipv4";
270                         break;
271 #ifdef HAVE_IPV6
272                 case PF_INET6:
273                         name = "ipv6";
274                         break;
275 #endif
276                 default:
277                         talloc_free(tmp_ctx);
278                         return EINVAL;
279                 }
280
281                 status = NT_STATUS_INVALID_PARAMETER;
282                 switch (hi->proto) {
283                 case KRB5_KRBHST_UDP:
284                         status = socket_create(name, SOCKET_TYPE_DGRAM, &smb_krb5->sock, 0);
285                         break;
286                 case KRB5_KRBHST_TCP:
287                         status = socket_create(name, SOCKET_TYPE_STREAM, &smb_krb5->sock, 0);
288                         break;
289                 case KRB5_KRBHST_HTTP:
290                         talloc_free(tmp_ctx);
291                         return EINVAL;
292                 }
293                 if (!NT_STATUS_IS_OK(status)) {
294                         talloc_free(smb_krb5);
295                         continue;
296                 }
297
298                 talloc_steal(smb_krb5, smb_krb5->sock);
299
300                 remote_addr = socket_address_from_sockaddr(smb_krb5, a->ai_addr, a->ai_addrlen);
301                 if (!remote_addr) {
302                         talloc_free(smb_krb5);
303                         continue;
304                 }
305
306                 status = socket_connect_ev(smb_krb5->sock, NULL, remote_addr, 0, ev);
307                 if (!NT_STATUS_IS_OK(status)) {
308                         talloc_free(smb_krb5);
309                         continue;
310                 }
311
312                 /* Setup the FDE, start listening for read events
313                  * from the start (otherwise we may miss a socket
314                  * drop) and mark as AUTOCLOSE along with the fde */
315
316                 /* Ths is equivilant to EVENT_FD_READABLE(smb_krb5->fde) */
317                 smb_krb5->fde = tevent_add_fd(ev, smb_krb5->sock,
318                                               socket_get_fd(smb_krb5->sock),
319                                               TEVENT_FD_READ,
320                                               smb_krb5_socket_handler, smb_krb5);
321                 /* its now the job of the event layer to close the socket */
322                 tevent_fd_set_close_fn(smb_krb5->fde, socket_tevent_fd_close_fn);
323                 socket_set_flags(smb_krb5->sock, SOCKET_FLAG_NOCLOSE);
324
325                 tevent_add_timer(ev, smb_krb5,
326                                  timeval_current_ofs(timeout, 0),
327                                  smb_krb5_request_timeout, smb_krb5);
328
329                 smb_krb5->status = NT_STATUS_OK;
330                 smb_krb5->reply = data_blob(NULL, 0);
331
332                 switch (hi->proto) {
333                 case KRB5_KRBHST_UDP:
334                         TEVENT_FD_WRITEABLE(smb_krb5->fde);
335                         smb_krb5->request = send_blob;
336                         break;
337                 case KRB5_KRBHST_TCP:
338
339                         smb_krb5->packet = packet_init(smb_krb5);
340                         if (smb_krb5->packet == NULL) {
341                                 talloc_free(smb_krb5);
342                                 return ENOMEM;
343                         }
344                         packet_set_private(smb_krb5->packet, smb_krb5);
345                         packet_set_socket(smb_krb5->packet, smb_krb5->sock);
346                         packet_set_callback(smb_krb5->packet, smb_krb5_full_packet);
347                         packet_set_full_request(smb_krb5->packet, packet_full_request_u32);
348                         packet_set_error_handler(smb_krb5->packet, smb_krb5_error_handler);
349                         packet_set_event_context(smb_krb5->packet, ev);
350                         packet_set_fde(smb_krb5->packet, smb_krb5->fde);
351
352                         smb_krb5->request = data_blob_talloc(smb_krb5, NULL, send_blob.length + 4);
353                         RSIVAL(smb_krb5->request.data, 0, send_blob.length);
354                         memcpy(smb_krb5->request.data+4, send_blob.data, send_blob.length);
355                         packet_send(smb_krb5->packet, smb_krb5->request);
356                         break;
357                 case KRB5_KRBHST_HTTP:
358                         talloc_free(tmp_ctx);
359                         return EINVAL;
360                 }
361                 while ((NT_STATUS_IS_OK(smb_krb5->status)) && !smb_krb5->reply.length) {
362                         if (tevent_loop_once(ev) != 0) {
363                                 talloc_free(tmp_ctx);
364                                 return EINVAL;
365                         }
366
367                         /* After each and every event loop, reset the
368                          * send_to_kdc pointers to what they were when
369                          * we entered this loop.  That way, if a
370                          * nested event has invalidated them, we put
371                          * it back before we return to the heimdal
372                          * code */
373                         ret = krb5_set_send_to_kdc_func(context,
374                                                         smb_krb5_send_and_recv_func,
375                                                         data);
376                         if (ret != 0) {
377                                 talloc_free(tmp_ctx);
378                                 return ret;
379                         }
380                 }
381                 if (NT_STATUS_EQUAL(smb_krb5->status, NT_STATUS_IO_TIMEOUT)) {
382                         talloc_free(smb_krb5);
383                         continue;
384                 }
385
386                 if (!NT_STATUS_IS_OK(smb_krb5->status)) {
387                         struct tsocket_address *addr = socket_address_to_tsocket_address(smb_krb5, remote_addr);
388                         const char *addr_string = NULL;
389                         if (addr) {
390                                 addr_string = tsocket_address_inet_addr_string(addr, smb_krb5);
391                         } else {
392                                 addr_string = NULL;
393                         }
394                         DEBUG(2,("Error reading smb_krb5 reply packet: %s from %s\n", nt_errstr(smb_krb5->status),
395                                  addr_string));
396                         talloc_free(smb_krb5);
397                         continue;
398                 }
399
400                 ret = krb5_data_copy(recv_buf, smb_krb5->reply.data, smb_krb5->reply.length);
401                 if (ret) {
402                         talloc_free(tmp_ctx);
403                         return ret;
404                 }
405                 talloc_free(smb_krb5);
406
407                 break;
408         }
409         talloc_free(tmp_ctx);
410         if (a) {
411                 return 0;
412         }
413         return KRB5_KDC_UNREACH;
414 }
415 #endif
416
417 krb5_error_code
418 smb_krb5_init_context_basic(TALLOC_CTX *tmp_ctx,
419                             struct loadparm_context *lp_ctx,
420                             krb5_context *_krb5_context)
421 {
422         krb5_error_code ret;
423         char **config_files;
424         const char *config_file, *realm;
425         krb5_context krb5_ctx;
426
427         initialize_krb5_error_table();
428
429         ret = krb5_init_context(&krb5_ctx);
430         if (ret) {
431                 DEBUG(1,("krb5_init_context failed (%s)\n",
432                          error_message(ret)));
433                 return ret;
434         }
435
436         config_file = lpcfg_config_path(tmp_ctx, lp_ctx, "krb5.conf");
437         if (!config_file) {
438                 krb5_free_context(krb5_ctx);
439                 return ENOMEM;
440         }
441
442         /* Use our local krb5.conf file by default */
443         ret = krb5_prepend_config_files_default(config_file == NULL?"":config_file, &config_files);
444         if (ret) {
445                 DEBUG(1,("krb5_prepend_config_files_default failed (%s)\n",
446                          smb_get_krb5_error_message(krb5_ctx, ret, tmp_ctx)));
447                 krb5_free_context(krb5_ctx);
448                 return ret;
449         }
450
451         ret = krb5_set_config_files(krb5_ctx, config_files);
452         krb5_free_config_files(config_files);
453         if (ret) {
454                 DEBUG(1,("krb5_set_config_files failed (%s)\n",
455                          smb_get_krb5_error_message(krb5_ctx, ret, tmp_ctx)));
456                 krb5_free_context(krb5_ctx);
457                 return ret;
458         }
459
460         realm = lpcfg_realm(lp_ctx);
461         if (realm != NULL) {
462                 ret = krb5_set_default_realm(krb5_ctx, realm);
463                 if (ret) {
464                         DEBUG(1,("krb5_set_default_realm failed (%s)\n",
465                                  smb_get_krb5_error_message(krb5_ctx, ret, tmp_ctx)));
466                         krb5_free_context(krb5_ctx);
467                         return ret;
468                 }
469         }
470
471         *_krb5_context = krb5_ctx;
472         return 0;
473 }
474
475 krb5_error_code smb_krb5_init_context(void *parent_ctx,
476                                       struct tevent_context *ev,
477                                       struct loadparm_context *lp_ctx,
478                                       struct smb_krb5_context **smb_krb5_context)
479 {
480         krb5_error_code ret;
481         TALLOC_CTX *tmp_ctx;
482         krb5_context kctx;
483 #ifdef SAMBA4_USES_HEIMDAL
484         krb5_log_facility *logf;
485 #endif
486
487         initialize_krb5_error_table();
488
489         tmp_ctx = talloc_new(parent_ctx);
490         *smb_krb5_context = talloc_zero(tmp_ctx, struct smb_krb5_context);
491
492         if (!*smb_krb5_context || !tmp_ctx) {
493                 talloc_free(tmp_ctx);
494                 return ENOMEM;
495         }
496
497         ret = smb_krb5_init_context_basic(tmp_ctx, lp_ctx, &kctx);
498         if (ret) {
499                 DEBUG(1,("smb_krb5_context_init_basic failed (%s)\n",
500                          error_message(ret)));
501                 talloc_free(tmp_ctx);
502                 return ret;
503         }
504         (*smb_krb5_context)->krb5_context = kctx;
505
506         talloc_set_destructor(*smb_krb5_context, smb_krb5_context_destroy);
507
508 #ifdef SAMBA4_USES_HEIMDAL
509         /* TODO: Should we have a different name here? */
510         ret = krb5_initlog(kctx, "Samba", &logf);
511
512         if (ret) {
513                 DEBUG(1,("krb5_initlog failed (%s)\n",
514                          smb_get_krb5_error_message(kctx, ret, tmp_ctx)));
515                 talloc_free(tmp_ctx);
516                 return ret;
517         }
518         (*smb_krb5_context)->pvt_log_data = logf;
519
520         ret = krb5_addlog_func(kctx, logf, 0 /* min */, -1 /* max */,
521                                smb_krb5_debug_wrapper,
522                                 smb_krb5_debug_close, NULL);
523         if (ret) {
524                 DEBUG(1,("krb5_addlog_func failed (%s)\n",
525                          smb_get_krb5_error_message(kctx, ret, tmp_ctx)));
526                 talloc_free(tmp_ctx);
527                 return ret;
528         }
529         krb5_set_warn_dest(kctx, logf);
530
531         /* Set use of our socket lib */
532         if (ev) {
533                 struct tevent_context *previous_ev;
534                 ret = smb_krb5_context_set_event_ctx(*smb_krb5_context,
535                                                      ev, &previous_ev);
536                 if (ret) {
537                         talloc_free(tmp_ctx);
538                         return ret;
539                 }
540         }
541
542         /* Set options in kerberos */
543
544         krb5_set_dns_canonicalize_hostname(kctx,
545                         lpcfg_parm_bool(lp_ctx, NULL, "krb5",
546                                         "set_dns_canonicalize", false));
547 #else
548         ret = krb5_set_trace_callback(kctx, smb_krb5_debug_wrapper, NULL);
549         if (ret && ret != KRB5_TRACE_NOSUPP) {
550                 DEBUG(1, ("krb5_set_trace_callback failed (%s)\n"
551                           smb_get_krb5_error_message(kctx, ret, tmp_ctx)));
552                 talloc_free(tmp_ctx);
553                 return ret;
554         }
555 #endif
556         talloc_steal(parent_ctx, *smb_krb5_context);
557         talloc_free(tmp_ctx);
558
559         return 0;
560 }
561
562 #ifdef SAMBA4_USES_HEIMDAL
563 krb5_error_code smb_krb5_context_set_event_ctx(struct smb_krb5_context *smb_krb5_context,
564                                                struct tevent_context *ev,
565                                                struct tevent_context **previous_ev)
566 {
567         int ret;
568         if (!ev) {
569                 return EINVAL;
570         }
571
572         *previous_ev = smb_krb5_context->current_ev;
573
574         smb_krb5_context->current_ev = talloc_reference(smb_krb5_context, ev);
575         if (!smb_krb5_context->current_ev) {
576                 return ENOMEM;
577         }
578
579         /* Set use of our socket lib */
580         ret = krb5_set_send_to_kdc_func(smb_krb5_context->krb5_context,
581                                         smb_krb5_send_and_recv_func,
582                                         ev);
583         if (ret) {
584                 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
585                 DEBUG(1,("krb5_set_send_recv_func failed (%s)\n",
586                          smb_get_krb5_error_message(smb_krb5_context->krb5_context, ret, tmp_ctx)));
587                 talloc_free(tmp_ctx);
588                 talloc_unlink(smb_krb5_context, smb_krb5_context->current_ev);
589                 smb_krb5_context->current_ev = NULL;
590                 return ret;
591         }
592         return 0;
593 }
594
595 krb5_error_code smb_krb5_context_remove_event_ctx(struct smb_krb5_context *smb_krb5_context,
596                                                   struct tevent_context *previous_ev,
597                                                   struct tevent_context *ev)
598 {
599         int ret;
600         talloc_unlink(smb_krb5_context, ev);
601         /* If there was a mismatch with things happening on a stack, then don't wipe things */
602         smb_krb5_context->current_ev = previous_ev;
603         /* Set use of our socket lib */
604         ret = krb5_set_send_to_kdc_func(smb_krb5_context->krb5_context,
605                                         smb_krb5_send_and_recv_func,
606                                         previous_ev);
607         if (ret) {
608                 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
609                 DEBUG(1,("krb5_set_send_recv_func failed (%s)\n",
610                          smb_get_krb5_error_message(smb_krb5_context->krb5_context, ret, tmp_ctx)));
611                 talloc_free(tmp_ctx);
612                 return ret;
613         }
614         return 0;
615 }
616 #endif