added "net join" command
[bbaumbach/samba-autobuild/.git] / source3 / 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   verify an incoming ticket and parse out the principal name and 
28   authorization_data if available 
29 */
30 NTSTATUS ads_verify_ticket(const DATA_BLOB *ticket, 
31                            char **principal, DATA_BLOB *auth_data)
32 {
33         krb5_context context;
34         krb5_auth_context auth_context = NULL;
35         krb5_keytab keytab = NULL;
36         krb5_data packet;
37         krb5_ticket *tkt = NULL;
38         krb5_data salt;
39         krb5_encrypt_block eblock;
40         int ret;
41         krb5_keyblock * key;
42         krb5_principal host_princ;
43         char *host_princ_s;
44         extern pstring global_myname;
45         fstring myname;
46         char *password_s;
47         krb5_data password;
48
49         if (!secrets_init()) {
50                 DEBUG(1,("secrets_init failed\n"));
51                 return NT_STATUS_LOGON_FAILURE;
52         }
53
54         password_s = secrets_fetch_machine_password();
55         if (!password_s) {
56                 DEBUG(1,("failed to fetch machine password\n"));
57                 return NT_STATUS_LOGON_FAILURE;
58         }
59
60         password.data = password_s;
61         password.length = strlen(password_s);
62
63         ret = krb5_init_context(&context);
64         if (ret) {
65                 DEBUG(1,("krb5_init_context failed (%s)\n", error_message(ret)));
66                 return NT_STATUS_LOGON_FAILURE;
67         }
68
69         ret = krb5_set_default_realm(context, lp_realm());
70         if (ret) {
71                 DEBUG(1,("krb5_set_default_realm failed (%s)\n", error_message(ret)));
72                 return NT_STATUS_LOGON_FAILURE;
73         }
74
75         /* this whole process is far more complex than I would
76            like. We have to go through all this to allow us to store
77            the secret internally, instead of using /etc/krb5.keytab */
78         ret = krb5_auth_con_init(context, &auth_context);
79         if (ret) {
80                 DEBUG(1,("krb5_auth_con_init failed (%s)\n", error_message(ret)));
81                 return NT_STATUS_LOGON_FAILURE;
82         }
83
84         fstrcpy(myname, global_myname);
85         strlower(myname);
86         asprintf(&host_princ_s, "HOST/%s@%s", myname, lp_realm());
87         ret = krb5_parse_name(context, host_princ_s, &host_princ);
88         if (ret) {
89                 DEBUG(1,("krb5_parse_name(%s) failed (%s)\n", host_princ_s, error_message(ret)));
90                 return NT_STATUS_LOGON_FAILURE;
91         }
92
93         ret = krb5_principal2salt(context, host_princ, &salt);
94         if (ret) {
95                 DEBUG(1,("krb5_principal2salt failed (%s)\n", error_message(ret)));
96                 return NT_STATUS_LOGON_FAILURE;
97         }
98     
99         if (!(key = (krb5_keyblock *)malloc(sizeof(*key)))) {
100                 return NT_STATUS_NO_MEMORY;
101         }
102         
103         krb5_use_enctype(context, &eblock, ENCTYPE_DES_CBC_MD5);
104         
105         ret = krb5_string_to_key(context, &eblock, key, &password, &salt);
106         if (ret) {
107                 DEBUG(1,("krb5_string_to_key failed (%s)\n", error_message(ret)));
108                 return NT_STATUS_LOGON_FAILURE;
109         }
110
111         krb5_auth_con_setuseruserkey(context, auth_context, key);
112
113         packet.length = ticket->length;
114         packet.data = (krb5_pointer)ticket->data;
115
116 #if 0
117         file_save("/tmp/ticket.dat", ticket->data, ticket->length);
118 #endif
119
120         if ((ret = krb5_rd_req(context, &auth_context, &packet, 
121                                NULL, keytab, NULL, &tkt))) {
122                 DEBUG(3,("krb5_rd_req with auth failed (%s)\n", 
123                          error_message(ret)));
124                 return NT_STATUS_LOGON_FAILURE;
125         }
126
127         if (tkt->enc_part2) {
128                 *auth_data = data_blob(tkt->enc_part2->authorization_data[0]->contents,
129                                        tkt->enc_part2->authorization_data[0]->length);
130         }
131
132 #if 0
133         if (tkt->enc_part2) {
134                 file_save("/tmp/authdata.dat", 
135                           tkt->enc_part2->authorization_data[0]->contents,
136                           tkt->enc_part2->authorization_data[0]->length);
137         }
138 #endif
139
140         if ((ret = krb5_unparse_name(context, tkt->enc_part2->client, principal))) {
141                 DEBUG(3,("krb5_unparse_name failed (%s)\n", 
142                          error_message(ret)));
143                 return NT_STATUS_LOGON_FAILURE;
144         }
145
146         return NT_STATUS_OK;
147 }
148
149 #endif