This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.
[sfrench/samba-autobuild/.git] / source / libads / 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
25 #ifdef HAVE_KRB5
26
27 /*
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 ...
31 */
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[])
38 {
39         if (num_prompts == 0) return 0;
40
41         memset(prompts[0].reply->data, 0, prompts[0].reply->length);
42         if (prompts[0].reply->length > 0) {
43                 strncpy(prompts[0].reply->data, data, prompts[0].reply->length-1);
44                 prompts[0].reply->length = strlen(prompts[0].reply->data);
45         }
46         return 0;
47 }
48
49 /*
50   simulate a kinit, putting the tgt in the default cache location
51   remus@snapserver.com
52 */
53 int kerberos_kinit_password(const char *principal, const char *password, int time_offset)
54 {
55         krb5_context ctx;
56         krb5_error_code code = 0;
57         krb5_ccache cc;
58         krb5_principal me;
59         krb5_creds my_creds;
60
61         if ((code = krb5_init_context(&ctx)))
62                 return code;
63
64         if (time_offset != 0) {
65                 krb5_set_real_time(ctx, time(NULL) + time_offset, 0);
66         }
67         
68         if ((code = krb5_cc_default(ctx, &cc))) {
69                 krb5_free_context(ctx);
70                 return code;
71         }
72         
73         if ((code = krb5_parse_name(ctx, principal, &me))) {
74                 krb5_free_context(ctx); 
75                 return code;
76         }
77         
78         if ((code = krb5_get_init_creds_password(ctx, &my_creds, me, NULL, 
79                                                  kerb_prompter, 
80                                                  password, 0, NULL, NULL))) {
81                 krb5_free_principal(ctx, me);
82                 krb5_free_context(ctx);         
83                 return code;
84         }
85         
86         if ((code = krb5_cc_initialize(ctx, cc, me))) {
87                 krb5_free_cred_contents(ctx, &my_creds);
88                 krb5_free_principal(ctx, me);
89                 krb5_free_context(ctx);         
90                 return code;
91         }
92         
93         if ((code = krb5_cc_store_cred(ctx, cc, &my_creds))) {
94                 krb5_cc_close(ctx, cc);
95                 krb5_free_cred_contents(ctx, &my_creds);
96                 krb5_free_principal(ctx, me);
97                 krb5_free_context(ctx);         
98                 return code;
99         }
100         
101         krb5_cc_close(ctx, cc);
102         krb5_free_cred_contents(ctx, &my_creds);
103         krb5_free_principal(ctx, me);
104         krb5_free_context(ctx);         
105         
106         return 0;
107 }
108
109
110
111 /* run kinit to setup our ccache */
112 int ads_kinit_password(ADS_STRUCT *ads)
113 {
114         char *s;
115         int ret;
116
117         asprintf(&s, "%s@%s", ads->auth.user_name, ads->auth.realm);
118         ret = kerberos_kinit_password(s, ads->auth.password, ads->auth.time_offset);
119
120         if (ret) {
121                 DEBUG(0,("kerberos_kinit_password %s failed: %s\n", 
122                          s, error_message(ret)));
123         }
124         free(s);
125         return ret;
126 }
127
128
129 #endif