debug: new debug class for kerberos
[kai/samba-autobuild/.git] / source4 / auth / kerberos / krb5_init_context.c
index 4125f395486357dd71488e24d56cd8e2b91b0b6f..e2c837abdced251489e86abfc5b3aee7ac1554b5 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"
@@ -46,8 +47,9 @@ 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)
@@ -60,29 +62,22 @@ 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;
 }
 
+#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));
-}
-#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));
+       DEBUGC(DBGC_KERBEROS, 3, ("Kerberos: %s\n", msg));
 }
 #endif
 
@@ -215,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;
@@ -274,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;
                }
 
@@ -287,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)) {
@@ -355,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)) {
@@ -399,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
@@ -420,8 +470,10 @@ smb_krb5_init_context_basic(TALLOC_CTX *tmp_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();
@@ -433,6 +485,10 @@ smb_krb5_init_context_basic(TALLOC_CTX *tmp_ctx,
                return ret;
        }
 
+       /* 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);
@@ -440,7 +496,7 @@ smb_krb5_init_context_basic(TALLOC_CTX *tmp_ctx,
        }
 
        /* 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)));
@@ -467,13 +523,12 @@ 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)
 {
@@ -528,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);