auth/kerberos: Use talloc_stackframe to avoid memory and FD leak of event context
[kai/samba-autobuild/.git] / source4 / auth / kerberos / krb5_init_context.c
index ff356aa86f1b111880afc55e1fd6e95c6b511e1b..7fcc8a6048d70efd3175d6638edfa1cc92953e91 100644 (file)
@@ -22,6 +22,7 @@
 
 #include "includes.h"
 #include "system/kerberos.h"
+#include "system/gssapi.h"
 #include <tevent.h>
 #include "auth/kerberos/kerberos.h"
 #include "lib/socket/socket.h"
@@ -29,7 +30,8 @@
 #include "system/network.h"
 #include "param/param.h"
 #include "libcli/resolve/resolve.h"
-
+#include "../lib/tsocket/tsocket.h"
+#include "krb5_init_context.h"
 /*
   context structure for operations on cldap packets
 */
@@ -45,29 +47,41 @@ struct smb_krb5_socket {
        struct packet_context *packet;
 
        size_t partial_read;
-
+#ifdef SAMBA4_USES_HEIMDAL
        krb5_krbhst_info *hi;
+#endif
 };
 
 static krb5_error_code smb_krb5_context_destroy(struct smb_krb5_context *ctx)
 {
-       /* Otherwise krb5_free_context will try and close what we have already free()ed */
-       krb5_set_warn_dest(ctx->krb5_context, NULL);
-       krb5_closelog(ctx->krb5_context, ctx->logf);
+#ifdef SAMBA4_USES_HEIMDAL
+       if (ctx->pvt_log_data) {
+               /* Otherwise krb5_free_context will try and close what we
+                * have already free()ed */
+               krb5_set_warn_dest(ctx->krb5_context, NULL);
+               krb5_closelog(ctx->krb5_context,
+                               (krb5_log_facility *)ctx->pvt_log_data);
+       }
+#endif
        krb5_free_context(ctx->krb5_context);
        return 0;
 }
 
+#ifdef SAMBA4_USES_HEIMDAL
 /* We never close down the DEBUG system, and no need to unreference the use */
 static void smb_krb5_debug_close(void *private_data) {
        return;
 }
+#endif
 
+#ifdef SAMBA4_USES_HEIMDAL
 static void smb_krb5_debug_wrapper(const char *timestr, const char *msg, void *private_data)
 {
        DEBUG(3, ("Kerberos: %s\n", msg));
 }
+#endif
 
+#ifdef SAMBA4_USES_HEIMDAL
 /*
   handle recv events on a smb_krb5 socket
 */
@@ -103,7 +117,7 @@ static void smb_krb5_socket_recv(struct smb_krb5_socket *smb_krb5)
                return;
        }
 
-       DEBUG(2,("Received smb_krb5 packet of length %d\n",
+       DEBUG(4,("Received smb_krb5 packet of length %d\n",
                 (int)blob.length));
 
        talloc_steal(smb_krb5, blob.data);
@@ -196,33 +210,36 @@ static void smb_krb5_socket_handler(struct tevent_context *ev, struct tevent_fd
        }
 }
 
-
-krb5_error_code smb_krb5_send_and_recv_func(krb5_context context,
-                                           void *data,
-                                           krb5_krbhst_info *hi,
-                                           time_t timeout,
-                                           const krb5_data *send_buf,
-                                           krb5_data *recv_buf)
+static krb5_error_code smb_krb5_send_and_recv_func_int(krb5_context context,
+                                                      struct tevent_context *ev,
+                                                      krb5_krbhst_info *hi,
+                                                      struct addrinfo *ai,
+                                                      krb5_send_to_kdc_func func,
+                                                      void *data,
+                                                      time_t timeout,
+                                                      const krb5_data *send_buf,
+                                                      krb5_data *recv_buf)
 {
        krb5_error_code ret;
        NTSTATUS status;
-       struct socket_address *remote_addr;
        const char *name;
-       struct addrinfo *ai, *a;
+       struct addrinfo *a;
        struct smb_krb5_socket *smb_krb5;
 
-       struct tevent_context *ev = talloc_get_type(data, struct tevent_context);
-
-       DATA_BLOB send_blob = data_blob_const(send_buf->data, send_buf->length);
+       DATA_BLOB send_blob;
 
-       ret = krb5_krbhst_get_addrinfo(context, hi, &ai);
-       if (ret) {
-               return ret;
+       TALLOC_CTX *frame = talloc_stackframe();
+       if (frame == NULL) {
+               return ENOMEM;
        }
 
-       for (a = ai; a; a = ai->ai_next) {
-               smb_krb5 = talloc(NULL, struct smb_krb5_socket);
+       send_blob = data_blob_const(send_buf->data, send_buf->length);
+
+       for (a = ai; a; a = a->ai_next) {
+               struct socket_address *remote_addr;
+               smb_krb5 = talloc(frame, struct smb_krb5_socket);
                if (!smb_krb5) {
+                       TALLOC_FREE(frame);
                        return ENOMEM;
                }
                smb_krb5->hi = hi;
@@ -237,7 +254,7 @@ krb5_error_code smb_krb5_send_and_recv_func(krb5_context context,
                        break;
 #endif
                default:
-                       talloc_free(smb_krb5);
+                       TALLOC_FREE(frame);
                        return EINVAL;
                }
 
@@ -250,7 +267,7 @@ krb5_error_code smb_krb5_send_and_recv_func(krb5_context context,
                        status = socket_create(name, SOCKET_TYPE_STREAM, &smb_krb5->sock, 0);
                        break;
                case KRB5_KRBHST_HTTP:
-                       talloc_free(smb_krb5);
+                       TALLOC_FREE(frame);
                        return EINVAL;
                }
                if (!NT_STATUS_IS_OK(status)) {
@@ -271,7 +288,6 @@ krb5_error_code smb_krb5_send_and_recv_func(krb5_context context,
                        talloc_free(smb_krb5);
                        continue;
                }
-               talloc_free(remote_addr);
 
                /* Setup the FDE, start listening for read events
                 * from the start (otherwise we may miss a socket
@@ -319,14 +335,30 @@ krb5_error_code smb_krb5_send_and_recv_func(krb5_context context,
                        packet_send(smb_krb5->packet, smb_krb5->request);
                        break;
                case KRB5_KRBHST_HTTP:
-                       talloc_free(smb_krb5);
+                       TALLOC_FREE(frame);
                        return EINVAL;
                }
                while ((NT_STATUS_IS_OK(smb_krb5->status)) && !smb_krb5->reply.length) {
                        if (tevent_loop_once(ev) != 0) {
-                               talloc_free(smb_krb5);
+                               TALLOC_FREE(frame);
                                return EINVAL;
                        }
+
+                        if (func) {
+                               /* After each and every event loop, reset the
+                                * send_to_kdc pointers to what they were when
+                                * we entered this loop.  That way, if a
+                                * nested event has invalidated them, we put
+                                * it back before we return to the heimdal
+                                * code */
+                               ret = krb5_set_send_to_kdc_func(context,
+                                                               func,
+                                                               data);
+                               if (ret != 0) {
+                                       TALLOC_FREE(frame);
+                                       return ret;
+                               }
+                       }
                }
                if (NT_STATUS_EQUAL(smb_krb5->status, NT_STATUS_IO_TIMEOUT)) {
                        talloc_free(smb_krb5);
@@ -334,35 +366,114 @@ krb5_error_code smb_krb5_send_and_recv_func(krb5_context context,
                }
 
                if (!NT_STATUS_IS_OK(smb_krb5->status)) {
-                       DEBUG(2,("Error reading smb_krb5 reply packet: %s\n", nt_errstr(smb_krb5->status)));
+                       struct tsocket_address *addr = socket_address_to_tsocket_address(smb_krb5, remote_addr);
+                       const char *addr_string = NULL;
+                       if (addr) {
+                               addr_string = tsocket_address_inet_addr_string(addr, smb_krb5);
+                       } else {
+                               addr_string = NULL;
+                       }
+                       DEBUG(2,("Error reading smb_krb5 reply packet: %s from %s\n", nt_errstr(smb_krb5->status),
+                                addr_string));
                        talloc_free(smb_krb5);
                        continue;
                }
 
                ret = krb5_data_copy(recv_buf, smb_krb5->reply.data, smb_krb5->reply.length);
                if (ret) {
-                       talloc_free(smb_krb5);
+                       TALLOC_FREE(frame);
                        return ret;
                }
                talloc_free(smb_krb5);
 
                break;
        }
+       TALLOC_FREE(frame);
        if (a) {
                return 0;
        }
        return KRB5_KDC_UNREACH;
 }
 
+krb5_error_code smb_krb5_send_and_recv_func(krb5_context context,
+                                           void *data,
+                                           krb5_krbhst_info *hi,
+                                           time_t timeout,
+                                           const krb5_data *send_buf,
+                                           krb5_data *recv_buf)
+{
+       krb5_error_code ret;
+       struct addrinfo *ai;
+
+       struct tevent_context *ev;
+       TALLOC_CTX *frame = talloc_stackframe();
+       if (frame == NULL) {
+               return ENOMEM;
+       }
+
+       if (data == NULL) {
+               /* If no event context was available, then create one for this loop */
+               ev = samba_tevent_context_init(frame);
+               if (ev == NULL) {
+                       TALLOC_FREE(frame);
+                       return ENOMEM;
+               }
+       } else {
+               ev = talloc_get_type_abort(data, struct tevent_context);
+       }
+
+       ret = krb5_krbhst_get_addrinfo(context, hi, &ai);
+       if (ret) {
+               TALLOC_FREE(frame);
+               return ret;
+       }
+
+       ret = smb_krb5_send_and_recv_func_int(context, ev, hi, ai, smb_krb5_send_and_recv_func, data, timeout, send_buf, recv_buf);
+       TALLOC_FREE(frame);
+       return ret;
+}
+
+krb5_error_code smb_krb5_send_and_recv_func_forced(krb5_context context,
+                                                  void *data, /* struct addrinfo */
+                                                  krb5_krbhst_info *hi,
+                                                  time_t timeout,
+                                                  const krb5_data *send_buf,
+                                                  krb5_data *recv_buf)
+{
+       krb5_error_code k5ret;
+       struct addrinfo *ai = data;
+
+       struct tevent_context *ev;
+       TALLOC_CTX *frame = talloc_stackframe();
+       if (frame == NULL) {
+               return ENOMEM;
+       }
+
+       /* no event context is passed in, create one for this loop */
+       ev = samba_tevent_context_init(frame);
+       if (ev == NULL) {
+               TALLOC_FREE(frame);
+               return ENOMEM;
+       }
+
+       /* No need to pass in send_and_recv functions, we won't nest on this private event loop */
+       k5ret = smb_krb5_send_and_recv_func_int(context, ev, hi, ai, NULL, NULL,
+                                               timeout, send_buf, recv_buf);
+       TALLOC_FREE(frame);
+       return k5ret;
+}
+#endif
+
 krb5_error_code
 smb_krb5_init_context_basic(TALLOC_CTX *tmp_ctx,
-                           struct tevent_context *ev,
                            struct loadparm_context *lp_ctx,
                            krb5_context *_krb5_context)
 {
        krb5_error_code ret;
+#ifdef SAMBA4_USES_HEIMDAL
        char **config_files;
        const char *config_file, *realm;
+#endif
        krb5_context krb5_ctx;
 
        initialize_krb5_error_table();
@@ -374,14 +485,18 @@ smb_krb5_init_context_basic(TALLOC_CTX *tmp_ctx,
                return ret;
        }
 
-       config_file = config_path(tmp_ctx, lp_ctx, "krb5.conf");
+       /* The MIT Kerberos build relies on using the system krb5.conf file.
+        * If you really want to use another file please set KRB5_CONFIG
+        * accordingly. */
+#ifdef SAMBA4_USES_HEIMDAL
+       config_file = lpcfg_config_path(tmp_ctx, lp_ctx, "krb5.conf");
        if (!config_file) {
                krb5_free_context(krb5_ctx);
                return ENOMEM;
        }
 
        /* Use our local krb5.conf file by default */
-       ret = krb5_prepend_config_files_default(config_file == NULL?"":config_file, &config_files);
+       ret = krb5_prepend_config_files_default(config_file, &config_files);
        if (ret) {
                DEBUG(1,("krb5_prepend_config_files_default failed (%s)\n",
                         smb_get_krb5_error_message(krb5_ctx, ret, tmp_ctx)));
@@ -408,80 +523,130 @@ smb_krb5_init_context_basic(TALLOC_CTX *tmp_ctx,
                        return ret;
                }
        }
-
+#endif
        *_krb5_context = krb5_ctx;
        return 0;
 }
 
 krb5_error_code smb_krb5_init_context(void *parent_ctx,
-                                     struct tevent_context *ev,
                                      struct loadparm_context *lp_ctx,
-                                      struct smb_krb5_context **smb_krb5_context)
+                                     struct smb_krb5_context **smb_krb5_context)
 {
        krb5_error_code ret;
        TALLOC_CTX *tmp_ctx;
+       krb5_context kctx;
+#ifdef SAMBA4_USES_HEIMDAL
+       krb5_log_facility *logf;
+#endif
 
        initialize_krb5_error_table();
 
        tmp_ctx = talloc_new(parent_ctx);
-       *smb_krb5_context = talloc(tmp_ctx, struct smb_krb5_context);
+       *smb_krb5_context = talloc_zero(tmp_ctx, struct smb_krb5_context);
 
        if (!*smb_krb5_context || !tmp_ctx) {
                talloc_free(tmp_ctx);
                return ENOMEM;
        }
 
-       ret = smb_krb5_init_context_basic(tmp_ctx, ev, lp_ctx,
-                                         &(*smb_krb5_context)->krb5_context);
+       ret = smb_krb5_init_context_basic(tmp_ctx, lp_ctx, &kctx);
        if (ret) {
                DEBUG(1,("smb_krb5_context_init_basic failed (%s)\n",
                         error_message(ret)));
                talloc_free(tmp_ctx);
                return ret;
        }
+       (*smb_krb5_context)->krb5_context = kctx;
+
+       talloc_set_destructor(*smb_krb5_context, smb_krb5_context_destroy);
 
+#ifdef SAMBA4_USES_HEIMDAL
        /* TODO: Should we have a different name here? */
-       ret = krb5_initlog((*smb_krb5_context)->krb5_context, "Samba", &(*smb_krb5_context)->logf);
+       ret = krb5_initlog(kctx, "Samba", &logf);
 
        if (ret) {
                DEBUG(1,("krb5_initlog failed (%s)\n",
-                        smb_get_krb5_error_message((*smb_krb5_context)->krb5_context, ret, tmp_ctx)));
-               krb5_free_context((*smb_krb5_context)->krb5_context);
+                        smb_get_krb5_error_message(kctx, ret, tmp_ctx)));
                talloc_free(tmp_ctx);
                return ret;
        }
+       (*smb_krb5_context)->pvt_log_data = logf;
 
-       talloc_set_destructor(*smb_krb5_context, smb_krb5_context_destroy);
-
-       ret = krb5_addlog_func((*smb_krb5_context)->krb5_context, (*smb_krb5_context)->logf, 0 /* min */, -1 /* max */,
-                              smb_krb5_debug_wrapper, smb_krb5_debug_close, NULL);
+       ret = krb5_addlog_func(kctx, logf, 0 /* min */, -1 /* max */,
+                              smb_krb5_debug_wrapper,
+                               smb_krb5_debug_close, NULL);
        if (ret) {
                DEBUG(1,("krb5_addlog_func failed (%s)\n",
-                        smb_get_krb5_error_message((*smb_krb5_context)->krb5_context, ret, tmp_ctx)));
+                        smb_get_krb5_error_message(kctx, ret, tmp_ctx)));
                talloc_free(tmp_ctx);
                return ret;
        }
-       krb5_set_warn_dest((*smb_krb5_context)->krb5_context, (*smb_krb5_context)->logf);
+       krb5_set_warn_dest(kctx, logf);
+
+       /* Set options in kerberos */
+
+       krb5_set_dns_canonicalize_hostname(kctx,
+                       lpcfg_parm_bool(lp_ctx, NULL, "krb5",
+                                       "set_dns_canonicalize", false));
+#endif
+       talloc_steal(parent_ctx, *smb_krb5_context);
+       talloc_free(tmp_ctx);
+
+       return 0;
+}
+
+#ifdef SAMBA4_USES_HEIMDAL
+krb5_error_code smb_krb5_context_set_event_ctx(struct smb_krb5_context *smb_krb5_context,
+                                              struct tevent_context *ev,
+                                              struct tevent_context **previous_ev)
+{
+       int ret;
+       if (!ev) {
+               return EINVAL;
+       }
+
+       *previous_ev = smb_krb5_context->current_ev;
+
+       smb_krb5_context->current_ev = talloc_reference(smb_krb5_context, ev);
+       if (!smb_krb5_context->current_ev) {
+               return ENOMEM;
+       }
 
        /* Set use of our socket lib */
-       ret = krb5_set_send_to_kdc_func((*smb_krb5_context)->krb5_context,
+       ret = krb5_set_send_to_kdc_func(smb_krb5_context->krb5_context,
                                        smb_krb5_send_and_recv_func,
                                        ev);
        if (ret) {
+               TALLOC_CTX *tmp_ctx = talloc_new(NULL);
                DEBUG(1,("krb5_set_send_recv_func failed (%s)\n",
-                        smb_get_krb5_error_message((*smb_krb5_context)->krb5_context, ret, tmp_ctx)));
+                        smb_get_krb5_error_message(smb_krb5_context->krb5_context, ret, tmp_ctx)));
                talloc_free(tmp_ctx);
+               talloc_unlink(smb_krb5_context, smb_krb5_context->current_ev);
+               smb_krb5_context->current_ev = NULL;
                return ret;
        }
-
-       talloc_steal(parent_ctx, *smb_krb5_context);
-       talloc_free(tmp_ctx);
-
-       /* Set options in kerberos */
-
-       krb5_set_dns_canonicalize_hostname((*smb_krb5_context)->krb5_context,
-                                          lpcfg_parm_bool(lp_ctx, NULL, "krb5", "set_dns_canonicalize", false));
-
        return 0;
 }
 
+krb5_error_code smb_krb5_context_remove_event_ctx(struct smb_krb5_context *smb_krb5_context,
+                                                 struct tevent_context *previous_ev,
+                                                 struct tevent_context *ev)
+{
+       int ret;
+       talloc_unlink(smb_krb5_context, ev);
+       /* If there was a mismatch with things happening on a stack, then don't wipe things */
+       smb_krb5_context->current_ev = previous_ev;
+       /* Set use of our socket lib */
+       ret = krb5_set_send_to_kdc_func(smb_krb5_context->krb5_context,
+                                       smb_krb5_send_and_recv_func,
+                                       previous_ev);
+       if (ret) {
+               TALLOC_CTX *tmp_ctx = talloc_new(NULL);
+               DEBUG(1,("krb5_set_send_recv_func failed (%s)\n",
+                        smb_get_krb5_error_message(smb_krb5_context->krb5_context, ret, tmp_ctx)));
+               talloc_free(tmp_ctx);
+               return ret;
+       }
+       return 0;
+}
+#endif