9510aaa7fb413bbb4080a7ffdfaa5ba2ea2a782b
[samba.git] / source4 / libcli / auth / kerberos.c
1 /* 
2    Unix SMB/CIFS implementation.
3    kerberos utility library
4    Copyright (C) Andrew Tridgell 2001
5    Copyright (C) Remus Koos 2001
6    
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "system/kerberos.h"
25 #include "libcli/auth/kerberos.h"
26 #include "system/time.h"
27
28 #ifdef HAVE_KRB5
29
30 /*
31   we use a prompter to avoid a crash bug in the kerberos libs when 
32   dealing with empty passwords
33   this prompter is just a string copy ...
34 */
35 static krb5_error_code 
36 kerb_prompter(krb5_context ctx, void *data,
37                const char *name,
38                const char *banner,
39                int num_prompts,
40                krb5_prompt prompts[])
41 {
42         if (num_prompts == 0) return 0;
43
44         memset(prompts[0].reply->data, 0, prompts[0].reply->length);
45         if (prompts[0].reply->length > 0) {
46                 if (data) {
47                         strncpy(prompts[0].reply->data, data, prompts[0].reply->length-1);
48                         prompts[0].reply->length = strlen(prompts[0].reply->data);
49                 } else {
50                         prompts[0].reply->length = 0;
51                 }
52         }
53         return 0;
54 }
55
56 /*
57   simulate a kinit, putting the tgt in the default cache location
58   remus@snapserver.com
59 */
60  int kerberos_kinit_password_cc(krb5_context ctx, krb5_ccache cc, const char *principal, const char *password, time_t *expire_time, time_t *kdc_time)
61 {
62         krb5_error_code code = 0;
63         krb5_principal me;
64         krb5_creds my_creds;
65         krb5_get_init_creds_opt options;
66
67         if ((code = krb5_parse_name(ctx, principal, &me))) {
68                 return code;
69         }
70
71         krb5_get_init_creds_opt_init(&options);
72
73         if ((code = krb5_get_init_creds_password(ctx, &my_creds, me, password, 
74                                                  kerb_prompter, 
75                                                  NULL, 0, NULL, &options))) {
76                 krb5_free_principal(ctx, me);
77                 return code;
78         }
79         
80         if ((code = krb5_cc_initialize(ctx, cc, me))) {
81                 krb5_free_cred_contents(ctx, &my_creds);
82                 krb5_free_principal(ctx, me);
83                 return code;
84         }
85         
86         if ((code = krb5_cc_store_cred(ctx, cc, &my_creds))) {
87                 krb5_free_cred_contents(ctx, &my_creds);
88                 krb5_free_principal(ctx, me);
89                 return code;
90         }
91         
92         if (expire_time) {
93                 *expire_time = (time_t) my_creds.times.endtime;
94         }
95
96         if (kdc_time) {
97                 *kdc_time = (time_t) my_creds.times.starttime;
98         }
99
100         krb5_free_cred_contents(ctx, &my_creds);
101         krb5_free_principal(ctx, me);
102         
103         return 0;
104 }
105
106
107 /*
108   simulate a kinit, putting the tgt in the default cache location
109   remus@snapserver.com
110 */
111 int kerberos_kinit_password(const char *principal, const char *password, int time_offset, time_t *expire_time, time_t *kdc_time)
112 {
113         krb5_context ctx = NULL;
114         krb5_error_code code = 0;
115         krb5_ccache cc = NULL;
116
117         if ((code = krb5_init_context(&ctx)))
118                 return code;
119
120         if (time_offset != 0) {
121                 krb5_set_real_time(ctx, time(NULL) + time_offset, 0);
122         }
123         
124         if ((code = krb5_cc_default(ctx, &cc))) {
125                 krb5_free_context(ctx);
126                 return code;
127         }
128
129         if ((code = kerberos_kinit_password_cc(ctx, cc, principal, password, expire_time, kdc_time))) {
130                 krb5_cc_close(ctx, cc);
131                 krb5_free_context(ctx);
132                 return code;
133         }
134         
135         return 0;
136 }
137
138
139
140 /* run kinit to setup our ccache */
141 int ads_kinit_password(ADS_STRUCT *ads)
142 {
143         char *s;
144         int ret;
145
146         if (asprintf(&s, "%s@%s", ads->auth.user_name, ads->auth.realm) == -1) {
147                 return KRB5_CC_NOMEM;
148         }
149
150         if (!ads->auth.password) {
151                 return KRB5_LIBOS_CANTREADPWD;
152         }
153         
154         ret = kerberos_kinit_password(s, ads->auth.password, ads->auth.time_offset, &ads->auth.expire, NULL);
155
156         if (ret) {
157                 DEBUG(0,("kerberos_kinit_password %s failed: %s\n", 
158                          s, error_message(ret)));
159         }
160         free(s);
161         return ret;
162 }
163
164 int ads_kdestroy(const char *cc_name)
165 {
166         krb5_error_code code;
167         krb5_context ctx = NULL;
168         krb5_ccache cc = NULL;
169
170         if ((code = krb5_init_context (&ctx))) {
171                 DEBUG(3, ("ads_kdestroy: kdb5_init_context rc=%d\n", code));
172                 return code;
173         }
174   
175         if (!cc_name) {
176                 if ((code = krb5_cc_default(ctx, &cc))) {
177                         krb5_free_context(ctx);
178                         return code;
179                 }
180         } else {
181                 if ((code = krb5_cc_resolve(ctx, cc_name, &cc))) {
182                         DEBUG(3, ("ads_kdestroy: krb5_cc_resolve rc=%d\n",
183                                   code));
184                         krb5_free_context(ctx);
185                         return code;
186                 }
187         }
188
189         if ((code = krb5_cc_destroy (ctx, cc))) {
190                 DEBUG(3, ("ads_kdestroy: krb5_cc_destroy rc=%d\n", code));
191         }
192
193         krb5_free_context (ctx);
194         return code;
195 }
196
197 #endif