r15: syncing for 3.0.3pre2
[Samba.git] / source / libads / kerberos.c
blob70f6f3386c70ff3d09db8a6265ef94cdba3f331a
1 /*
2 Unix SMB/CIFS implementation.
3 kerberos utility library
4 Copyright (C) Andrew Tridgell 2001
5 Copyright (C) Remus Koos 2001
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.
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.
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.
23 #include "includes.h"
25 #ifdef HAVE_KRB5
28 we use a prompter to avoid a crash bug in the kerberos libs when
29 dealing with empty passwords
30 this prompter is just a string copy ...
32 static krb5_error_code
33 kerb_prompter(krb5_context ctx, void *data,
34 const char *name,
35 const char *banner,
36 int num_prompts,
37 krb5_prompt prompts[])
39 if (num_prompts == 0) return 0;
41 memset(prompts[0].reply->data, 0, prompts[0].reply->length);
42 if (prompts[0].reply->length > 0) {
43 if (data) {
44 strncpy(prompts[0].reply->data, data, prompts[0].reply->length-1);
45 prompts[0].reply->length = strlen(prompts[0].reply->data);
46 } else {
47 prompts[0].reply->length = 0;
50 return 0;
54 simulate a kinit, putting the tgt in the default cache location
55 remus@snapserver.com
57 int kerberos_kinit_password(const char *principal, const char *password, int time_offset, time_t *expire_time)
59 krb5_context ctx;
60 krb5_error_code code = 0;
61 krb5_ccache cc;
62 krb5_principal me;
63 krb5_creds my_creds;
65 if ((code = krb5_init_context(&ctx)))
66 return code;
68 if (time_offset != 0) {
69 krb5_set_real_time(ctx, time(NULL) + time_offset, 0);
72 if ((code = krb5_cc_default(ctx, &cc))) {
73 krb5_free_context(ctx);
74 return code;
77 if ((code = krb5_parse_name(ctx, principal, &me))) {
78 krb5_free_context(ctx);
79 return code;
82 if ((code = krb5_get_init_creds_password(ctx, &my_creds, me, NULL,
83 kerb_prompter,
84 password, 0, NULL, NULL))) {
85 krb5_free_principal(ctx, me);
86 krb5_free_context(ctx);
87 return code;
90 if ((code = krb5_cc_initialize(ctx, cc, me))) {
91 krb5_free_cred_contents(ctx, &my_creds);
92 krb5_free_principal(ctx, me);
93 krb5_free_context(ctx);
94 return code;
97 if ((code = krb5_cc_store_cred(ctx, cc, &my_creds))) {
98 krb5_cc_close(ctx, cc);
99 krb5_free_cred_contents(ctx, &my_creds);
100 krb5_free_principal(ctx, me);
101 krb5_free_context(ctx);
102 return code;
105 if (expire_time)
106 *expire_time = (time_t) my_creds.times.endtime;
108 krb5_cc_close(ctx, cc);
109 krb5_free_cred_contents(ctx, &my_creds);
110 krb5_free_principal(ctx, me);
111 krb5_free_context(ctx);
113 return 0;
118 /* run kinit to setup our ccache */
119 int ads_kinit_password(ADS_STRUCT *ads)
121 char *s;
122 int ret;
124 if (asprintf(&s, "%s@%s", ads->auth.user_name, ads->auth.realm) == -1) {
125 return KRB5_CC_NOMEM;
128 if (!ads->auth.password) {
129 return KRB5_LIBOS_CANTREADPWD;
132 ret = kerberos_kinit_password(s, ads->auth.password, ads->auth.time_offset, &ads->auth.expire);
134 if (ret) {
135 DEBUG(0,("kerberos_kinit_password %s failed: %s\n",
136 s, error_message(ret)));
138 free(s);
139 return ret;
142 int ads_kdestroy(const char *cc_name)
144 krb5_error_code code;
145 krb5_context ctx;
146 krb5_ccache cc;
148 if ((code = krb5_init_context (&ctx))) {
149 DEBUG(3, ("ads_kdestroy: kdb5_init_context rc=%d\n", code));
150 return code;
153 if (!cc_name) {
154 if ((code = krb5_cc_default(ctx, &cc))) {
155 krb5_free_context(ctx);
156 return code;
158 } else {
159 if ((code = krb5_cc_resolve(ctx, cc_name, &cc))) {
160 DEBUG(3, ("ads_kdestroy: krb5_cc_resolve rc=%d\n",
161 code));
162 krb5_free_context(ctx);
163 return code;
167 if ((code = krb5_cc_destroy (ctx, cc))) {
168 DEBUG(3, ("ads_kdestroy: krb5_cc_destroy rc=%d\n", code));
171 krb5_free_context (ctx);
172 return code;
175 #endif