put the winbindd krb5 credentials cache in the lock directory
[jerry/samba.git] / source / libads / kerberos.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    kerberos utility library
5    Copyright (C) Andrew Tridgell 2001
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
24 #ifdef HAVE_KRB5
25
26 /*
27   simulate a kinit, putting the tgt in the default cache location
28   remus@snapserver.com
29 */
30 int kerberos_kinit_password(const char *principal, const char *password)
31 {
32         krb5_context ctx;
33         krb5_error_code code = 0;
34         krb5_ccache cc;
35         krb5_principal me;
36         krb5_creds my_creds;
37         
38         if ((code = krb5_init_context(&ctx)))
39                 return code;
40         
41         if ((code = krb5_cc_default(ctx, &cc))) {
42                 krb5_free_context(ctx);
43                 return code;
44         }
45         
46         if ((code = krb5_parse_name(ctx, principal, &me))) {
47                 krb5_free_context(ctx); 
48                 return code;
49         }
50         
51         if ((code = krb5_get_init_creds_password(ctx, &my_creds, me, password, NULL, 
52                                                 NULL, 0, NULL, NULL))) {
53                 krb5_free_principal(ctx, me);
54                 krb5_free_context(ctx);         
55                 return code;
56         }
57         
58         if ((code = krb5_cc_initialize(ctx, cc, me))) {
59                 krb5_free_cred_contents(ctx, &my_creds);
60                 krb5_free_principal(ctx, me);
61                 krb5_free_context(ctx);         
62                 return code;
63         }
64         
65         if ((code = krb5_cc_store_cred(ctx, cc, &my_creds))) {
66                 krb5_cc_close(ctx, cc);
67                 krb5_free_cred_contents(ctx, &my_creds);
68                 krb5_free_principal(ctx, me);
69                 krb5_free_context(ctx);         
70                 return code;
71         }
72         
73         krb5_cc_close(ctx, cc);
74         krb5_free_cred_contents(ctx, &my_creds);
75         krb5_free_principal(ctx, me);
76         krb5_free_context(ctx);         
77         
78         return 0;
79 }
80
81
82
83 /* run kinit to setup our ccache */
84 int ads_kinit_password(ADS_STRUCT *ads)
85 {
86         char *s;
87         int ret;
88         extern pstring global_myname;
89         fstring myname;
90
91         /* we don't want this to affect the users ccache */
92         setenv("KRB5CCNAME", lock_path("winbindd_ccache"), 1);
93
94         fstrcpy(myname, global_myname);
95         strlower(myname);
96         asprintf(&s, "HOST/%s@%s", global_myname, ads->realm);
97         ret = kerberos_kinit_password(s, ads->password);
98         free(s);
99         if (ret) {
100                 DEBUG(1,("kerberos_kinit_password failed: %s\n", error_message(ret)));
101         }
102         return ret;
103 }
104
105 /*
106   verify an incoming ticket and parse out the principal name and 
107   authorization_data if available 
108 */
109 NTSTATUS ads_verify_ticket(ADS_STRUCT *ads, const DATA_BLOB *ticket, 
110                            char **principal, DATA_BLOB *auth_data)
111 {
112         krb5_context context;
113         krb5_auth_context auth_context = NULL;
114         krb5_keytab keytab = NULL;
115         krb5_data packet;
116         krb5_ticket *tkt = NULL;
117         krb5_data salt;
118         krb5_encrypt_block eblock;
119         int ret;
120         krb5_keyblock * key;
121         krb5_principal host_princ;
122         char *host_princ_s;
123         extern pstring global_myname;
124         fstring myname;
125         char *password_s;
126         krb5_data password;
127
128         if (!secrets_init()) {
129                 DEBUG(1,("secrets_init failed\n"));
130                 return NT_STATUS_LOGON_FAILURE;
131         }
132
133         password_s = secrets_fetch_machine_password();
134         if (!password_s) {
135                 DEBUG(1,("failed to fetch machine password\n"));
136                 return NT_STATUS_LOGON_FAILURE;
137         }
138
139         password.data = password_s;
140         password.length = strlen(password_s);
141
142         ret = krb5_init_context(&context);
143         if (ret) {
144                 DEBUG(1,("krb5_init_context failed (%s)\n", error_message(ret)));
145                 return NT_STATUS_LOGON_FAILURE;
146         }
147
148         ret = krb5_set_default_realm(context, ads->realm);
149         if (ret) {
150                 DEBUG(1,("krb5_set_default_realm failed (%s)\n", error_message(ret)));
151                 ads_destroy(&ads);
152                 return NT_STATUS_LOGON_FAILURE;
153         }
154
155         /* this whole process is far more complex than I would
156            like. We have to go through all this to allow us to store
157            the secret internally, instead of using /etc/krb5.keytab */
158         ret = krb5_auth_con_init(context, &auth_context);
159         if (ret) {
160                 DEBUG(1,("krb5_auth_con_init failed (%s)\n", error_message(ret)));
161                 return NT_STATUS_LOGON_FAILURE;
162         }
163
164         fstrcpy(myname, global_myname);
165         strlower(myname);
166         asprintf(&host_princ_s, "HOST/%s@%s", myname, lp_realm());
167         ret = krb5_parse_name(context, host_princ_s, &host_princ);
168         if (ret) {
169                 DEBUG(1,("krb5_parse_name(%s) failed (%s)\n", host_princ_s, error_message(ret)));
170                 return NT_STATUS_LOGON_FAILURE;
171         }
172
173         ret = krb5_principal2salt(context, host_princ, &salt);
174         if (ret) {
175                 DEBUG(1,("krb5_principal2salt failed (%s)\n", error_message(ret)));
176                 return NT_STATUS_LOGON_FAILURE;
177         }
178     
179         if (!(key = (krb5_keyblock *)malloc(sizeof(*key)))) {
180                 return NT_STATUS_NO_MEMORY;
181         }
182         
183         krb5_use_enctype(context, &eblock, ENCTYPE_DES_CBC_MD5);
184         
185         ret = krb5_string_to_key(context, &eblock, key, &password, &salt);
186         if (ret) {
187                 DEBUG(1,("krb5_string_to_key failed (%s)\n", error_message(ret)));
188                 return NT_STATUS_LOGON_FAILURE;
189         }
190
191         krb5_auth_con_setuseruserkey(context, auth_context, key);
192
193         packet.length = ticket->length;
194         packet.data = (krb5_pointer)ticket->data;
195
196 #if 0
197         file_save("/tmp/ticket.dat", ticket->data, ticket->length);
198 #endif
199
200         if ((ret = krb5_rd_req(context, &auth_context, &packet, 
201                                NULL, keytab, NULL, &tkt))) {
202                 DEBUG(3,("krb5_rd_req with auth failed (%s)\n", 
203                          error_message(ret)));
204                 return NT_STATUS_LOGON_FAILURE;
205         }
206
207         if (tkt->enc_part2) {
208                 *auth_data = data_blob(tkt->enc_part2->authorization_data[0]->contents,
209                                        tkt->enc_part2->authorization_data[0]->length);
210         }
211
212 #if 0
213         if (tkt->enc_part2) {
214                 file_save("/tmp/authdata.dat", 
215                           tkt->enc_part2->authorization_data[0]->contents,
216                           tkt->enc_part2->authorization_data[0]->length);
217         }
218 #endif
219
220         if ((ret = krb5_unparse_name(context, tkt->enc_part2->client, principal))) {
221                 DEBUG(3,("krb5_unparse_name failed (%s)\n", 
222                          error_message(ret)));
223                 return NT_STATUS_LOGON_FAILURE;
224         }
225
226         return NT_STATUS_OK;
227 }
228
229 #endif