2 Unix SMB/CIFS implementation.
3 Wrapper for krb5_init_context
5 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "system/kerberos.h"
24 #include "auth/kerberos/kerberos.h"
26 static int smb_krb5_context_destroy_1(void *ptr)
28 struct smb_krb5_context *ctx = ptr;
29 krb5_free_context(ctx->krb5_context);
33 static int smb_krb5_context_destroy_2(void *ptr)
35 struct smb_krb5_context *ctx = ptr;
37 /* Otherwise krb5_free_context will try and close what we have already free()ed */
38 krb5_set_warn_dest(ctx->krb5_context, NULL);
39 krb5_closelog(ctx->krb5_context, ctx->logf);
40 smb_krb5_context_destroy_1(ptr);
44 /* We never close down the DEBUG system, and no need to unreference the use */
45 static void smb_krb5_debug_close(void *private) {
49 static void smb_krb5_debug_wrapper(const char *timestr, const char *msg, void *private)
51 DEBUG(3, ("Kerberos: %s\n", msg));
54 krb5_error_code smb_krb5_init_context(void *parent_ctx,
55 struct smb_krb5_context **smb_krb5_context)
60 initialize_krb5_error_table();
62 tmp_ctx = talloc_new(parent_ctx);
63 *smb_krb5_context = talloc(tmp_ctx, struct smb_krb5_context);
65 if (!*smb_krb5_context || !tmp_ctx) {
66 talloc_free(*smb_krb5_context);
71 ret = krb5_init_context(&(*smb_krb5_context)->krb5_context);
73 DEBUG(1,("krb5_init_context failed (%s)\n",
78 talloc_set_destructor(*smb_krb5_context, smb_krb5_context_destroy_1);
80 if (lp_realm() && *lp_realm()) {
81 char *upper_realm = strupper_talloc(tmp_ctx, lp_realm());
83 DEBUG(1,("gensec_krb5_start: could not uppercase realm: %s\n", lp_realm()));
87 ret = krb5_set_default_realm((*smb_krb5_context)->krb5_context, upper_realm);
89 DEBUG(1,("krb5_set_default_realm failed (%s)\n",
90 smb_get_krb5_error_message((*smb_krb5_context)->krb5_context, ret, tmp_ctx)));
96 /* TODO: Should we have a different name here? */
97 ret = krb5_initlog((*smb_krb5_context)->krb5_context, "Samba", &(*smb_krb5_context)->logf);
100 DEBUG(1,("krb5_initlog failed (%s)\n",
101 smb_get_krb5_error_message((*smb_krb5_context)->krb5_context, ret, tmp_ctx)));
102 talloc_free(tmp_ctx);
106 talloc_set_destructor(*smb_krb5_context, smb_krb5_context_destroy_2);
108 ret = krb5_addlog_func((*smb_krb5_context)->krb5_context, (*smb_krb5_context)->logf, 0 /* min */, -1 /* max */,
109 smb_krb5_debug_wrapper, smb_krb5_debug_close, NULL);
111 DEBUG(1,("krb5_addlog_func failed (%s)\n",
112 smb_get_krb5_error_message((*smb_krb5_context)->krb5_context, ret, tmp_ctx)));
113 talloc_free(tmp_ctx);
116 krb5_set_warn_dest((*smb_krb5_context)->krb5_context, (*smb_krb5_context)->logf);
118 talloc_steal(parent_ctx, *smb_krb5_context);
119 talloc_free(tmp_ctx);
121 /* Set options in kerberos */
123 (*smb_krb5_context)->krb5_context->fdns = FALSE;
128 void smb_krb5_free_context(struct smb_krb5_context *smb_krb5_context)
130 talloc_free(smb_krb5_context);