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 9b36ee94f4afefdf8fbc7cc307a0e4aafc78513b..7fcc8a6048d70efd3175d6638edfa1cc92953e91 100644 (file)
@@ -62,8 +62,6 @@ static krb5_error_code smb_krb5_context_destroy(struct smb_krb5_context *ctx)
                krb5_closelog(ctx->krb5_context,
                                (krb5_log_facility *)ctx->pvt_log_data);
        }
-#else
-       krb5_set_trace_callback(ctx->krb5_context, NULL, NULL);
 #endif
        krb5_free_context(ctx->krb5_context);
        return 0;
@@ -81,13 +79,6 @@ static void smb_krb5_debug_wrapper(const char *timestr, const char *msg, void *p
 {
        DEBUG(3, ("Kerberos: %s\n", msg));
 }
-#else
-static void smb_krb5_debug_wrapper(krb5_context context,
-                                  const struct krb5_trace_info *info,
-                                  void *cb_data)
-{
-       DEBUG(3, ("Kerberos: %s\n", info->message));
-}
 #endif
 
 #ifdef SAMBA4_USES_HEIMDAL
@@ -219,51 +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;
        const char *name;
-       struct addrinfo *ai, *a;
+       struct addrinfo *a;
        struct smb_krb5_socket *smb_krb5;
 
        DATA_BLOB send_blob;
 
-       struct tevent_context *ev;
-       TALLOC_CTX *tmp_ctx = talloc_new(NULL);
-       if (!tmp_ctx) {
+       TALLOC_CTX *frame = talloc_stackframe();
+       if (frame == NULL) {
                return ENOMEM;
        }
 
-       if (!data) {
-               /* If no event context was available, then create one for this loop */
-               ev = tevent_context_init(tmp_ctx);
-               if (!ev) {
-                       talloc_free(tmp_ctx);
-                       return ENOMEM;
-               }
-       } else {
-               ev = talloc_get_type_abort(data, struct tevent_context);
-       }
-
        send_blob = data_blob_const(send_buf->data, send_buf->length);
 
-       ret = krb5_krbhst_get_addrinfo(context, hi, &ai);
-       if (ret) {
-               talloc_free(tmp_ctx);
-               return ret;
-       }
-
        for (a = ai; a; a = a->ai_next) {
                struct socket_address *remote_addr;
-               smb_krb5 = talloc(tmp_ctx, struct smb_krb5_socket);
+               smb_krb5 = talloc(frame, struct smb_krb5_socket);
                if (!smb_krb5) {
-                       talloc_free(tmp_ctx);
+                       TALLOC_FREE(frame);
                        return ENOMEM;
                }
                smb_krb5->hi = hi;
@@ -278,7 +254,7 @@ krb5_error_code smb_krb5_send_and_recv_func(krb5_context context,
                        break;
 #endif
                default:
-                       talloc_free(tmp_ctx);
+                       TALLOC_FREE(frame);
                        return EINVAL;
                }
 
@@ -291,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(tmp_ctx);
+                       TALLOC_FREE(frame);
                        return EINVAL;
                }
                if (!NT_STATUS_IS_OK(status)) {
@@ -359,27 +335,29 @@ 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(tmp_ctx);
+                       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(tmp_ctx);
+                               TALLOC_FREE(frame);
                                return EINVAL;
                        }
 
-                       /* 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,
-                                                       smb_krb5_send_and_recv_func,
-                                                       data);
-                       if (ret != 0) {
-                               talloc_free(tmp_ctx);
-                               return ret;
+                        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)) {
@@ -403,19 +381,87 @@ krb5_error_code smb_krb5_send_and_recv_func(krb5_context context,
 
                ret = krb5_data_copy(recv_buf, smb_krb5->reply.data, smb_krb5->reply.length);
                if (ret) {
-                       talloc_free(tmp_ctx);
+                       TALLOC_FREE(frame);
                        return ret;
                }
                talloc_free(smb_krb5);
 
                break;
        }
-       talloc_free(tmp_ctx);
+       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
@@ -483,7 +529,6 @@ smb_krb5_init_context_basic(TALLOC_CTX *tmp_ctx,
 }
 
 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)
 {
@@ -538,30 +583,11 @@ krb5_error_code smb_krb5_init_context(void *parent_ctx,
        }
        krb5_set_warn_dest(kctx, logf);
 
-       /* Set use of our socket lib */
-       if (ev) {
-               struct tevent_context *previous_ev;
-               ret = smb_krb5_context_set_event_ctx(*smb_krb5_context,
-                                                    ev, &previous_ev);
-               if (ret) {
-                       talloc_free(tmp_ctx);
-                       return ret;
-               }
-       }
-
        /* Set options in kerberos */
 
        krb5_set_dns_canonicalize_hostname(kctx,
                        lpcfg_parm_bool(lp_ctx, NULL, "krb5",
                                        "set_dns_canonicalize", false));
-#else
-       ret = krb5_set_trace_callback(kctx, smb_krb5_debug_wrapper, NULL);
-       if (ret && ret != KRB5_TRACE_NOSUPP) {
-               DEBUG(1, ("krb5_set_trace_callback failed (%s)\n",
-                         smb_get_krb5_error_message(kctx, ret, tmp_ctx)));
-               talloc_free(tmp_ctx);
-               return ret;
-       }
 #endif
        talloc_steal(parent_ctx, *smb_krb5_context);
        talloc_free(tmp_ctx);