Having gone to all the effort to uppercase the realm, actually set the
[samba-svnmirror.git] / source / 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    
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.
11    
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.
16    
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.
20 */
21
22 #include "includes.h"
23 #include "system/kerberos.h"
24 #include "auth/kerberos/kerberos.h"
25
26 static int smb_krb5_context_destroy_1(void *ptr) 
27 {
28         struct smb_krb5_context *ctx = ptr;
29         krb5_free_context(ctx->krb5_context); 
30         return 0;
31 }
32
33 static int smb_krb5_context_destroy_2(void *ptr) 
34 {
35         struct smb_krb5_context *ctx = ptr;
36
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);
41         return 0;
42 }
43
44 /* We never close down the DEBUG system, and no need to unreference the use */
45 static void smb_krb5_debug_close(void *private) {
46         return;
47 }
48
49 static void smb_krb5_debug_wrapper(const char *timestr, const char *msg, void *private) 
50 {
51         DEBUG(3, ("Kerberos: %s\n", msg));
52 }
53
54  krb5_error_code smb_krb5_init_context(void *parent_ctx, 
55                                        struct smb_krb5_context **smb_krb5_context) 
56 {
57         krb5_error_code ret;
58         TALLOC_CTX *tmp_ctx;
59         
60         initialize_krb5_error_table();
61         
62         tmp_ctx = talloc_new(parent_ctx);
63         *smb_krb5_context = talloc(tmp_ctx, struct smb_krb5_context);
64
65         if (!*smb_krb5_context || !tmp_ctx) {
66                 talloc_free(*smb_krb5_context);
67                 talloc_free(tmp_ctx);
68                 return ENOMEM;
69         }
70
71         ret = krb5_init_context(&(*smb_krb5_context)->krb5_context);
72         if (ret) {
73                 DEBUG(1,("krb5_init_context failed (%s)\n", 
74                          error_message(ret)));
75                 return ret;
76         }
77
78         talloc_set_destructor(*smb_krb5_context, smb_krb5_context_destroy_1);
79
80         if (lp_realm() && *lp_realm()) {
81                 char *upper_realm = strupper_talloc(tmp_ctx, lp_realm());
82                 if (!upper_realm) {
83                         DEBUG(1,("gensec_krb5_start: could not uppercase realm: %s\n", lp_realm()));
84                         talloc_free(tmp_ctx);
85                         return ENOMEM;
86                 }
87                 ret = krb5_set_default_realm((*smb_krb5_context)->krb5_context, upper_realm);
88                 if (ret) {
89                         DEBUG(1,("krb5_set_default_realm failed (%s)\n", 
90                                  smb_get_krb5_error_message((*smb_krb5_context)->krb5_context, ret, tmp_ctx)));
91                         talloc_free(tmp_ctx);
92                         return ret;
93                 }
94         }
95
96         /* TODO: Should we have a different name here? */
97         ret = krb5_initlog((*smb_krb5_context)->krb5_context, "Samba", &(*smb_krb5_context)->logf);
98         
99         if (ret) {
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);
103                 return ret;
104         }
105
106         talloc_set_destructor(*smb_krb5_context, smb_krb5_context_destroy_2);
107
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);
110         if (ret) {
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);
114                 return ret;
115         }
116         krb5_set_warn_dest((*smb_krb5_context)->krb5_context, (*smb_krb5_context)->logf);
117
118         talloc_steal(parent_ctx, *smb_krb5_context);
119         talloc_free(tmp_ctx);
120
121         /* Set options in kerberos */
122
123         (*smb_krb5_context)->krb5_context->fdns = FALSE;
124         
125         return 0;
126 }
127
128  void smb_krb5_free_context(struct smb_krb5_context *smb_krb5_context) 
129 {
130         talloc_free(smb_krb5_context);
131 }