s4:kerberos: adjust smb_krb5_debug_wrapper() to embedded heimdal
[samba.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 #ifdef SAMBA4_USES_HEIMDAL
50         krb5_krbhst_info *hi;
51 #endif
52 };
53
54 static krb5_error_code smb_krb5_context_destroy(struct smb_krb5_context *ctx)
55 {
56 #ifdef SAMBA4_USES_HEIMDAL
57         if (ctx->pvt_log_data) {
58                 /* Otherwise krb5_free_context will try and close what we
59                  * have already free()ed */
60                 krb5_set_warn_dest(ctx->krb5_context, NULL);
61                 krb5_closelog(ctx->krb5_context,
62                                 (krb5_log_facility *)ctx->pvt_log_data);
63         }
64 #endif
65         krb5_free_context(ctx->krb5_context);
66         return 0;
67 }
68
69 #ifdef SAMBA4_USES_HEIMDAL
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 #endif
75
76 #ifdef SAMBA4_USES_HEIMDAL
77 static void smb_krb5_debug_wrapper(
78 #ifdef HAVE_KRB5_ADDLOG_FUNC_NEED_CONTEXT
79                 krb5_context ctx,
80 #endif /* HAVE_KRB5_ADDLOG_FUNC_NEED_CONTEXT */
81                 const char *timestr, const char *msg, void *private_data)
82 {
83         DEBUGC(DBGC_KERBEROS, 3, ("Kerberos: %s\n", msg));
84 }
85 #endif
86
87 #ifdef SAMBA4_USES_HEIMDAL
88 /*
89   handle recv events on a smb_krb5 socket
90 */
91 static void smb_krb5_socket_recv(struct smb_krb5_socket *smb_krb5)
92 {
93         TALLOC_CTX *tmp_ctx = talloc_new(smb_krb5);
94         DATA_BLOB blob;
95         size_t nread, dsize;
96
97         smb_krb5->status = socket_pending(smb_krb5->sock, &dsize);
98         if (!NT_STATUS_IS_OK(smb_krb5->status)) {
99                 talloc_free(tmp_ctx);
100                 return;
101         }
102
103         blob = data_blob_talloc(tmp_ctx, NULL, dsize);
104         if (blob.data == NULL && dsize != 0) {
105                 smb_krb5->status = NT_STATUS_NO_MEMORY;
106                 talloc_free(tmp_ctx);
107                 return;
108         }
109
110         smb_krb5->status = socket_recv(smb_krb5->sock, blob.data, blob.length, &nread);
111         if (!NT_STATUS_IS_OK(smb_krb5->status)) {
112                 talloc_free(tmp_ctx);
113                 return;
114         }
115         blob.length = nread;
116
117         if (nread == 0) {
118                 smb_krb5->status = NT_STATUS_UNEXPECTED_NETWORK_ERROR;
119                 talloc_free(tmp_ctx);
120                 return;
121         }
122
123         DEBUG(4,("Received smb_krb5 packet of length %d\n",
124                  (int)blob.length));
125
126         talloc_steal(smb_krb5, blob.data);
127         smb_krb5->reply = blob;
128         talloc_free(tmp_ctx);
129 }
130
131 static NTSTATUS smb_krb5_full_packet(void *private_data, DATA_BLOB data)
132 {
133         struct smb_krb5_socket *smb_krb5 = talloc_get_type(private_data, struct smb_krb5_socket);
134         talloc_steal(smb_krb5, data.data);
135         smb_krb5->reply = data;
136         smb_krb5->reply.length -= 4;
137         smb_krb5->reply.data += 4;
138         return NT_STATUS_OK;
139 }
140
141 /*
142   handle request timeouts
143 */
144 static void smb_krb5_request_timeout(struct tevent_context *event_ctx,
145                                   struct tevent_timer *te, struct timeval t,
146                                   void *private_data)
147 {
148         struct smb_krb5_socket *smb_krb5 = talloc_get_type(private_data, struct smb_krb5_socket);
149         DEBUG(5,("Timed out smb_krb5 packet\n"));
150         smb_krb5->status = NT_STATUS_IO_TIMEOUT;
151 }
152
153 static void smb_krb5_error_handler(void *private_data, NTSTATUS status)
154 {
155         struct smb_krb5_socket *smb_krb5 = talloc_get_type(private_data, struct smb_krb5_socket);
156         smb_krb5->status = status;
157 }
158
159 /*
160   handle send events on a smb_krb5 socket
161 */
162 static void smb_krb5_socket_send(struct smb_krb5_socket *smb_krb5)
163 {
164         NTSTATUS status;
165
166         size_t len;
167
168         len = smb_krb5->request.length;
169         status = socket_send(smb_krb5->sock, &smb_krb5->request, &len);
170
171         if (!NT_STATUS_IS_OK(status)) return;
172
173         TEVENT_FD_READABLE(smb_krb5->fde);
174
175         TEVENT_FD_NOT_WRITEABLE(smb_krb5->fde);
176         return;
177 }
178
179
180 /*
181   handle fd events on a smb_krb5_socket
182 */
183 static void smb_krb5_socket_handler(struct tevent_context *ev, struct tevent_fd *fde,
184                                  uint16_t flags, void *private_data)
185 {
186         struct smb_krb5_socket *smb_krb5 = talloc_get_type(private_data, struct smb_krb5_socket);
187         switch (smb_krb5->hi->proto) {
188         case KRB5_KRBHST_UDP:
189                 if (flags & TEVENT_FD_READ) {
190                         smb_krb5_socket_recv(smb_krb5);
191                         return;
192                 }
193                 if (flags & TEVENT_FD_WRITE) {
194                         smb_krb5_socket_send(smb_krb5);
195                         return;
196                 }
197                 /* not reached */
198                 return;
199         case KRB5_KRBHST_TCP:
200                 if (flags & TEVENT_FD_READ) {
201                         packet_recv(smb_krb5->packet);
202                         return;
203                 }
204                 if (flags & TEVENT_FD_WRITE) {
205                         packet_queue_run(smb_krb5->packet);
206                         return;
207                 }
208                 /* not reached */
209                 return;
210         case KRB5_KRBHST_HTTP:
211                 /* can't happen */
212                 break;
213         }
214 }
215
216 static krb5_error_code smb_krb5_send_and_recv_func_int(krb5_context context,
217                                                        struct tevent_context *ev,
218                                                        krb5_krbhst_info *hi,
219                                                        struct addrinfo *ai,
220                                                        krb5_send_to_kdc_func func,
221                                                        void *data,
222                                                        time_t timeout,
223                                                        const krb5_data *send_buf,
224                                                        krb5_data *recv_buf)
225 {
226         krb5_error_code ret;
227         NTSTATUS status;
228         const char *name;
229         struct addrinfo *a;
230         struct smb_krb5_socket *smb_krb5;
231
232         DATA_BLOB send_blob;
233
234         TALLOC_CTX *frame = talloc_stackframe();
235         if (frame == NULL) {
236                 return ENOMEM;
237         }
238
239         send_blob = data_blob_const(send_buf->data, send_buf->length);
240
241         for (a = ai; a; a = a->ai_next) {
242                 struct socket_address *remote_addr;
243                 smb_krb5 = talloc(frame, struct smb_krb5_socket);
244                 if (!smb_krb5) {
245                         TALLOC_FREE(frame);
246                         return ENOMEM;
247                 }
248                 smb_krb5->hi = hi;
249
250                 switch (a->ai_family) {
251                 case PF_INET:
252                         name = "ipv4";
253                         break;
254 #ifdef HAVE_IPV6
255                 case PF_INET6:
256                         name = "ipv6";
257                         break;
258 #endif
259                 default:
260                         TALLOC_FREE(frame);
261                         return EINVAL;
262                 }
263
264                 status = NT_STATUS_INVALID_PARAMETER;
265                 switch (hi->proto) {
266                 case KRB5_KRBHST_UDP:
267                         status = socket_create(smb_krb5, name,
268                                                SOCKET_TYPE_DGRAM,
269                                                &smb_krb5->sock, 0);
270                         break;
271                 case KRB5_KRBHST_TCP:
272                         status = socket_create(smb_krb5, name,
273                                                SOCKET_TYPE_STREAM,
274                                                &smb_krb5->sock, 0);
275                         break;
276                 case KRB5_KRBHST_HTTP:
277                         TALLOC_FREE(frame);
278                         return EINVAL;
279                 }
280                 if (!NT_STATUS_IS_OK(status)) {
281                         talloc_free(smb_krb5);
282                         continue;
283                 }
284
285                 remote_addr = socket_address_from_sockaddr(smb_krb5, a->ai_addr, a->ai_addrlen);
286                 if (!remote_addr) {
287                         talloc_free(smb_krb5);
288                         continue;
289                 }
290
291                 status = socket_connect_ev(smb_krb5->sock, NULL, remote_addr, 0, ev);
292                 if (!NT_STATUS_IS_OK(status)) {
293                         talloc_free(smb_krb5);
294                         continue;
295                 }
296
297                 /* Setup the FDE, start listening for read events
298                  * from the start (otherwise we may miss a socket
299                  * drop) and mark as AUTOCLOSE along with the fde */
300
301                 /* Ths is equivilant to EVENT_FD_READABLE(smb_krb5->fde) */
302                 smb_krb5->fde = tevent_add_fd(ev, smb_krb5->sock,
303                                               socket_get_fd(smb_krb5->sock),
304                                               TEVENT_FD_READ,
305                                               smb_krb5_socket_handler, smb_krb5);
306                 /* its now the job of the event layer to close the socket */
307                 tevent_fd_set_close_fn(smb_krb5->fde, socket_tevent_fd_close_fn);
308                 socket_set_flags(smb_krb5->sock, SOCKET_FLAG_NOCLOSE);
309
310                 tevent_add_timer(ev, smb_krb5,
311                                  timeval_current_ofs(timeout, 0),
312                                  smb_krb5_request_timeout, smb_krb5);
313
314                 smb_krb5->status = NT_STATUS_OK;
315                 smb_krb5->reply = data_blob(NULL, 0);
316
317                 switch (hi->proto) {
318                 case KRB5_KRBHST_UDP:
319                         TEVENT_FD_WRITEABLE(smb_krb5->fde);
320                         smb_krb5->request = send_blob;
321                         break;
322                 case KRB5_KRBHST_TCP:
323
324                         smb_krb5->packet = packet_init(smb_krb5);
325                         if (smb_krb5->packet == NULL) {
326                                 talloc_free(smb_krb5);
327                                 return ENOMEM;
328                         }
329                         packet_set_private(smb_krb5->packet, smb_krb5);
330                         packet_set_socket(smb_krb5->packet, smb_krb5->sock);
331                         packet_set_callback(smb_krb5->packet, smb_krb5_full_packet);
332                         packet_set_full_request(smb_krb5->packet, packet_full_request_u32);
333                         packet_set_error_handler(smb_krb5->packet, smb_krb5_error_handler);
334                         packet_set_event_context(smb_krb5->packet, ev);
335                         packet_set_fde(smb_krb5->packet, smb_krb5->fde);
336
337                         smb_krb5->request = data_blob_talloc(smb_krb5, NULL, send_blob.length + 4);
338                         RSIVAL(smb_krb5->request.data, 0, send_blob.length);
339                         memcpy(smb_krb5->request.data+4, send_blob.data, send_blob.length);
340                         packet_send(smb_krb5->packet, smb_krb5->request);
341                         break;
342                 case KRB5_KRBHST_HTTP:
343                         TALLOC_FREE(frame);
344                         return EINVAL;
345                 }
346                 while ((NT_STATUS_IS_OK(smb_krb5->status)) && !smb_krb5->reply.length) {
347                         if (tevent_loop_once(ev) != 0) {
348                                 TALLOC_FREE(frame);
349                                 return EINVAL;
350                         }
351
352                         if (func) {
353                                 /* After each and every event loop, reset the
354                                  * send_to_kdc pointers to what they were when
355                                  * we entered this loop.  That way, if a
356                                  * nested event has invalidated them, we put
357                                  * it back before we return to the heimdal
358                                  * code */
359                                 ret = krb5_set_send_to_kdc_func(context,
360                                                                 func,
361                                                                 data);
362                                 if (ret != 0) {
363                                         TALLOC_FREE(frame);
364                                         return ret;
365                                 }
366                         }
367                 }
368                 if (NT_STATUS_EQUAL(smb_krb5->status, NT_STATUS_IO_TIMEOUT)) {
369                         talloc_free(smb_krb5);
370                         continue;
371                 }
372
373                 if (!NT_STATUS_IS_OK(smb_krb5->status)) {
374                         struct tsocket_address *addr = socket_address_to_tsocket_address(smb_krb5, remote_addr);
375                         const char *addr_string = NULL;
376                         if (addr) {
377                                 addr_string = tsocket_address_inet_addr_string(addr, smb_krb5);
378                         } else {
379                                 addr_string = NULL;
380                         }
381                         DEBUG(2,("Error reading smb_krb5 reply packet: %s from %s\n", nt_errstr(smb_krb5->status),
382                                  addr_string));
383                         talloc_free(smb_krb5);
384                         continue;
385                 }
386
387                 ret = krb5_data_copy(recv_buf, smb_krb5->reply.data, smb_krb5->reply.length);
388                 if (ret) {
389                         TALLOC_FREE(frame);
390                         return ret;
391                 }
392                 talloc_free(smb_krb5);
393
394                 break;
395         }
396         TALLOC_FREE(frame);
397         if (a) {
398                 return 0;
399         }
400         return KRB5_KDC_UNREACH;
401 }
402
403 krb5_error_code smb_krb5_send_and_recv_func(krb5_context context,
404                                             void *data,
405                                             krb5_krbhst_info *hi,
406                                             time_t timeout,
407                                             const krb5_data *send_buf,
408                                             krb5_data *recv_buf)
409 {
410         krb5_error_code ret;
411         struct addrinfo *ai;
412
413         struct tevent_context *ev;
414         TALLOC_CTX *frame = talloc_stackframe();
415         if (frame == NULL) {
416                 return ENOMEM;
417         }
418
419         if (data == NULL) {
420                 /* If no event context was available, then create one for this loop */
421                 ev = samba_tevent_context_init(frame);
422                 if (ev == NULL) {
423                         TALLOC_FREE(frame);
424                         return ENOMEM;
425                 }
426         } else {
427                 ev = talloc_get_type_abort(data, struct tevent_context);
428         }
429
430         ret = krb5_krbhst_get_addrinfo(context, hi, &ai);
431         if (ret) {
432                 TALLOC_FREE(frame);
433                 return ret;
434         }
435
436         ret = smb_krb5_send_and_recv_func_int(context, ev, hi, ai, smb_krb5_send_and_recv_func, data, timeout, send_buf, recv_buf);
437         TALLOC_FREE(frame);
438         return ret;
439 }
440
441 krb5_error_code smb_krb5_send_and_recv_func_forced(krb5_context context,
442                                                    void *data, /* struct addrinfo */
443                                                    krb5_krbhst_info *hi,
444                                                    time_t timeout,
445                                                    const krb5_data *send_buf,
446                                                    krb5_data *recv_buf)
447 {
448         krb5_error_code k5ret;
449         struct addrinfo *ai = data;
450
451         struct tevent_context *ev;
452         TALLOC_CTX *frame = talloc_stackframe();
453         if (frame == NULL) {
454                 return ENOMEM;
455         }
456
457         /* no event context is passed in, create one for this loop */
458         ev = samba_tevent_context_init(frame);
459         if (ev == NULL) {
460                 TALLOC_FREE(frame);
461                 return ENOMEM;
462         }
463
464         /* No need to pass in send_and_recv functions, we won't nest on this private event loop */
465         k5ret = smb_krb5_send_and_recv_func_int(context, ev, hi, ai, NULL, NULL,
466                                                 timeout, send_buf, recv_buf);
467         TALLOC_FREE(frame);
468         return k5ret;
469 }
470 #endif
471
472 krb5_error_code
473 smb_krb5_init_context_basic(TALLOC_CTX *tmp_ctx,
474                             struct loadparm_context *lp_ctx,
475                             krb5_context *_krb5_context)
476 {
477         krb5_error_code ret;
478 #ifdef SAMBA4_USES_HEIMDAL
479         char **config_files;
480         const char *config_file, *realm;
481 #endif
482         krb5_context krb5_ctx;
483
484         ret = smb_krb5_init_context_common(&krb5_ctx);
485         if (ret) {
486                 return ret;
487         }
488
489         /* The MIT Kerberos build relies on using the system krb5.conf file.
490          * If you really want to use another file please set KRB5_CONFIG
491          * accordingly. */
492 #ifdef SAMBA4_USES_HEIMDAL
493         config_file = lpcfg_config_path(tmp_ctx, lp_ctx, "krb5.conf");
494         if (!config_file) {
495                 krb5_free_context(krb5_ctx);
496                 return ENOMEM;
497         }
498
499         /* Use our local krb5.conf file by default */
500         ret = krb5_prepend_config_files_default(config_file, &config_files);
501         if (ret) {
502                 DEBUG(1,("krb5_prepend_config_files_default failed (%s)\n",
503                          smb_get_krb5_error_message(krb5_ctx, ret, tmp_ctx)));
504                 krb5_free_context(krb5_ctx);
505                 return ret;
506         }
507
508         ret = krb5_set_config_files(krb5_ctx, config_files);
509         krb5_free_config_files(config_files);
510         if (ret) {
511                 DEBUG(1,("krb5_set_config_files failed (%s)\n",
512                          smb_get_krb5_error_message(krb5_ctx, ret, tmp_ctx)));
513                 krb5_free_context(krb5_ctx);
514                 return ret;
515         }
516
517         /*
518          * This is already called in smb_krb5_init_context_common(),
519          * but krb5_set_config_files() may resets it.
520          */
521         krb5_set_dns_canonicalize_hostname(krb5_ctx, false);
522
523         realm = lpcfg_realm(lp_ctx);
524         if (realm != NULL) {
525                 ret = krb5_set_default_realm(krb5_ctx, realm);
526                 if (ret) {
527                         DEBUG(1,("krb5_set_default_realm failed (%s)\n",
528                                  smb_get_krb5_error_message(krb5_ctx, ret, tmp_ctx)));
529                         krb5_free_context(krb5_ctx);
530                         return ret;
531                 }
532         }
533 #endif
534         *_krb5_context = krb5_ctx;
535         return 0;
536 }
537
538 krb5_error_code smb_krb5_init_context(void *parent_ctx,
539                                       struct loadparm_context *lp_ctx,
540                                       struct smb_krb5_context **smb_krb5_context)
541 {
542         krb5_error_code ret;
543         TALLOC_CTX *tmp_ctx;
544         krb5_context kctx;
545 #ifdef SAMBA4_USES_HEIMDAL
546         krb5_log_facility *logf;
547 #endif
548
549         tmp_ctx = talloc_new(parent_ctx);
550         *smb_krb5_context = talloc_zero(tmp_ctx, struct smb_krb5_context);
551
552         if (!*smb_krb5_context || !tmp_ctx) {
553                 talloc_free(tmp_ctx);
554                 return ENOMEM;
555         }
556
557         ret = smb_krb5_init_context_basic(tmp_ctx, lp_ctx, &kctx);
558         if (ret) {
559                 DEBUG(1,("smb_krb5_context_init_basic failed (%s)\n",
560                          error_message(ret)));
561                 talloc_free(tmp_ctx);
562                 return ret;
563         }
564         (*smb_krb5_context)->krb5_context = kctx;
565
566         talloc_set_destructor(*smb_krb5_context, smb_krb5_context_destroy);
567
568 #ifdef SAMBA4_USES_HEIMDAL
569         /* TODO: Should we have a different name here? */
570         ret = krb5_initlog(kctx, "Samba", &logf);
571
572         if (ret) {
573                 DEBUG(1,("krb5_initlog failed (%s)\n",
574                          smb_get_krb5_error_message(kctx, ret, tmp_ctx)));
575                 talloc_free(tmp_ctx);
576                 return ret;
577         }
578         (*smb_krb5_context)->pvt_log_data = logf;
579
580         ret = krb5_addlog_func(kctx, logf, 0 /* min */, -1 /* max */,
581                                smb_krb5_debug_wrapper,
582                                 smb_krb5_debug_close, NULL);
583         if (ret) {
584                 DEBUG(1,("krb5_addlog_func failed (%s)\n",
585                          smb_get_krb5_error_message(kctx, ret, tmp_ctx)));
586                 talloc_free(tmp_ctx);
587                 return ret;
588         }
589         krb5_set_warn_dest(kctx, logf);
590 #endif
591         talloc_steal(parent_ctx, *smb_krb5_context);
592         talloc_free(tmp_ctx);
593
594         return 0;
595 }
596
597 #ifdef SAMBA4_USES_HEIMDAL
598 krb5_error_code smb_krb5_context_set_event_ctx(struct smb_krb5_context *smb_krb5_context,
599                                                struct tevent_context *ev,
600                                                struct tevent_context **previous_ev)
601 {
602         int ret;
603         if (!ev) {
604                 return EINVAL;
605         }
606
607         *previous_ev = smb_krb5_context->current_ev;
608
609         smb_krb5_context->current_ev = talloc_reference(smb_krb5_context, ev);
610         if (!smb_krb5_context->current_ev) {
611                 return ENOMEM;
612         }
613
614         /* Set use of our socket lib */
615         ret = krb5_set_send_to_kdc_func(smb_krb5_context->krb5_context,
616                                         smb_krb5_send_and_recv_func,
617                                         ev);
618         if (ret) {
619                 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
620                 DEBUG(1,("krb5_set_send_recv_func failed (%s)\n",
621                          smb_get_krb5_error_message(smb_krb5_context->krb5_context, ret, tmp_ctx)));
622                 talloc_free(tmp_ctx);
623                 talloc_unlink(smb_krb5_context, smb_krb5_context->current_ev);
624                 smb_krb5_context->current_ev = NULL;
625                 return ret;
626         }
627         return 0;
628 }
629
630 krb5_error_code smb_krb5_context_remove_event_ctx(struct smb_krb5_context *smb_krb5_context,
631                                                   struct tevent_context *previous_ev,
632                                                   struct tevent_context *ev)
633 {
634         int ret;
635         talloc_unlink(smb_krb5_context, ev);
636         /* If there was a mismatch with things happening on a stack, then don't wipe things */
637         smb_krb5_context->current_ev = previous_ev;
638         /* Set use of our socket lib */
639         ret = krb5_set_send_to_kdc_func(smb_krb5_context->krb5_context,
640                                         smb_krb5_send_and_recv_func,
641                                         previous_ev);
642         if (ret) {
643                 TALLOC_CTX *tmp_ctx = talloc_new(NULL);
644                 DEBUG(1,("krb5_set_send_recv_func failed (%s)\n",
645                          smb_get_krb5_error_message(smb_krb5_context->krb5_context, ret, tmp_ctx)));
646                 talloc_free(tmp_ctx);
647                 return ret;
648         }
649         return 0;
650 }
651 #endif