r25789: print out what error happened...
[kai/samba.git] / source4 / kdc / pac-glue.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    PAC Glue between Samba and the KDC
5    
6    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
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 3 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    
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "kdc/kdc.h"
25 #include "dsdb/common/flags.h"
26 #include "lib/ldb/include/ldb.h"
27 #include "librpc/gen_ndr/ndr_krb5pac.h"
28 #include "librpc/gen_ndr/krb5pac.h"
29 #include "auth/auth.h"
30 #include "auth/auth_sam.h"
31
32 struct krb5_dh_moduli;
33 struct _krb5_krb_auth_data;
34
35 krb5_error_code samba_kdc_plugin_init(krb5_context context, void **ptr) 
36 {
37         *ptr = NULL;
38         return 0;
39 }
40
41 void    samba_kdc_plugin_fini(void *ptr) 
42 {
43         return;
44 }
45
46 static krb5_error_code make_pac(krb5_context context,
47                                 TALLOC_CTX *mem_ctx, 
48                                 struct auth_serversupplied_info *server_info,
49                                 krb5_pac *pac) 
50 {
51         struct PAC_LOGON_INFO_CTR logon_info;
52         struct netr_SamInfo3 *info3;
53         krb5_data pac_data;
54         NTSTATUS nt_status;
55         DATA_BLOB pac_out;
56         krb5_error_code ret;
57
58         ZERO_STRUCT(logon_info);
59
60         nt_status = auth_convert_server_info_saminfo3(mem_ctx, server_info, &info3);
61         if (!NT_STATUS_IS_OK(nt_status)) {
62                 DEBUG(1, ("Getting Samba info failed: %s\n", nt_errstr(nt_status)));
63                 return EINVAL;
64         }
65
66         logon_info.info = talloc_zero(mem_ctx, struct PAC_LOGON_INFO);
67         if (!mem_ctx) {
68                 return ENOMEM;
69         }
70
71         logon_info.info->info3 = *info3;
72
73         nt_status = ndr_push_struct_blob(&pac_out, mem_ctx, &logon_info,
74                                          (ndr_push_flags_fn_t)ndr_push_PAC_LOGON_INFO_CTR);
75         if (!NT_STATUS_IS_OK(nt_status)) {
76                 DEBUG(1, ("PAC (presig) push failed: %s\n", nt_errstr(nt_status)));
77                 return EINVAL;
78         }
79
80         ret = krb5_data_copy(&pac_data, pac_out.data, pac_out.length);
81         if (ret != 0) {
82                 return ret;
83         }
84
85         ret = krb5_pac_init(context, pac);
86         if (ret != 0) {
87                 krb5_data_free(&pac_data);
88                 return ret;
89         }
90
91         ret = krb5_pac_add_buffer(context, *pac, PAC_TYPE_LOGON_INFO, &pac_data);
92         krb5_data_free(&pac_data);
93         if (ret != 0) {
94                 return ret;
95         }
96
97         return ret;
98 }
99
100 /* Given the right private pointer from hdb_ldb, get a PAC from the attached ldb messages */
101 krb5_error_code samba_kdc_get_pac(void *priv,
102                                   krb5_context context, 
103                                   struct hdb_entry_ex *client,
104                                   krb5_pac *pac)
105 {
106         krb5_error_code ret;
107         NTSTATUS nt_status;
108         struct auth_serversupplied_info *server_info;
109         struct hdb_ldb_private *private = talloc_get_type(client->ctx, struct hdb_ldb_private);
110         TALLOC_CTX *mem_ctx = talloc_named(private, 0, "samba_get_pac context");
111         unsigned int userAccountControl;
112
113         if (!mem_ctx) {
114                 return ENOMEM;
115         }
116
117         /* The user account may be set not to want the PAC */
118         userAccountControl = ldb_msg_find_attr_as_uint(private->msg, "userAccountControl", 0);
119         if (userAccountControl & UF_NO_AUTH_DATA_REQUIRED) {
120                 *pac = NULL;
121                 return 0;
122         }
123
124         nt_status = authsam_make_server_info(mem_ctx, private->samdb, 
125                                              private->msg, 
126                                              private->realm_ref_msg,
127                                              data_blob(NULL, 0),
128                                              data_blob(NULL, 0),
129                                              &server_info);
130         if (!NT_STATUS_IS_OK(nt_status)) {
131                 DEBUG(0, ("Getting user info for PAC failed: %s\n",
132                           nt_errstr(nt_status)));
133                 return ENOMEM;
134         }
135
136         ret = make_pac(context, mem_ctx, server_info, pac);
137
138         talloc_free(mem_ctx);
139         return ret;
140 }
141
142 /* Resign (and reform, including possibly new groups) a PAC */
143
144 krb5_error_code samba_kdc_reget_pac(void *priv, krb5_context context,
145                                 const krb5_principal client_principal,
146                                 struct hdb_entry_ex *client,  
147                                 struct hdb_entry_ex *server, krb5_pac *pac)
148 {
149         NTSTATUS nt_status;
150         krb5_error_code ret;
151
152         unsigned int userAccountControl;
153
154         struct hdb_ldb_private *private = talloc_get_type(server->ctx, struct hdb_ldb_private);
155         krb5_data k5pac_in;
156         DATA_BLOB pac_in;
157
158         struct PAC_LOGON_INFO_CTR logon_info;
159         union netr_Validation validation;
160         struct auth_serversupplied_info *server_info_out;
161
162         TALLOC_CTX *mem_ctx = talloc_named(private, 0, "samba_get_pac context");
163         
164         if (!mem_ctx) {
165                 return ENOMEM;
166         }
167
168         /* The service account may be set not to want the PAC */
169         userAccountControl = ldb_msg_find_attr_as_uint(private->msg, "userAccountControl", 0);
170         if (userAccountControl & UF_NO_AUTH_DATA_REQUIRED) {
171                 *pac = NULL;
172                 return 0;
173         }
174
175         ret = krb5_pac_get_buffer(context, *pac, PAC_TYPE_LOGON_INFO, &k5pac_in);
176         if (ret != 0) {
177                 return ret;
178         }
179
180         pac_in = data_blob_talloc(mem_ctx, k5pac_in.data, k5pac_in.length);
181         krb5_data_free(&k5pac_in);
182         if (!pac_in.data) {
183                 talloc_free(mem_ctx);
184                 return ENOMEM;
185         }
186                 
187         nt_status = ndr_pull_struct_blob(&pac_in, mem_ctx, &logon_info, 
188                                       (ndr_pull_flags_fn_t)ndr_pull_PAC_LOGON_INFO_CTR);
189         if (!NT_STATUS_IS_OK(nt_status) || !logon_info.info) {
190                 DEBUG(0,("can't parse the PAC LOGON_INFO: %s\n", nt_errstr(nt_status)));
191                 talloc_free(mem_ctx);
192                 return EINVAL;
193         }
194
195         /* Pull this right into the normal auth sysstem structures */
196         validation.sam3 = &logon_info.info->info3;
197         nt_status = make_server_info_netlogon_validation(mem_ctx,
198                                                          "",
199                                                          3, &validation,
200                                                          &server_info_out); 
201         if (!NT_STATUS_IS_OK(nt_status)) {
202                 talloc_free(mem_ctx);
203                 return ENOMEM;
204         }
205
206         /* We will compleatly regenerate this pac */
207         krb5_pac_free(context, *pac);
208
209         ret = make_pac(context, mem_ctx, server_info_out, pac);
210
211         talloc_free(mem_ctx);
212         return ret;
213 }
214
215 /* Given an hdb entry (and in particular it's private member), consult
216  * the account_ok routine in auth/auth_sam.c for consistancy */
217
218
219 krb5_error_code samba_kdc_check_client_access(void *priv, 
220                                               krb5_context context, hdb_entry_ex *entry_ex, 
221                                               KDC_REQ *req)
222 {
223         krb5_error_code ret;
224         NTSTATUS nt_status;
225         TALLOC_CTX *tmp_ctx = talloc_new(entry_ex->ctx);
226         struct hdb_ldb_private *private = talloc_get_type(entry_ex->ctx, struct hdb_ldb_private);
227         char *name, *workstation = NULL;
228         HostAddresses *addresses = req->req_body.addresses;
229         int i;
230
231         if (!tmp_ctx) {
232                 return ENOMEM;
233         }
234         
235         ret = krb5_unparse_name(context, entry_ex->entry.principal, &name);
236         if (ret != 0) {
237                 talloc_free(tmp_ctx);
238                 return ret;
239         }
240
241         if (addresses) {
242                 for (i=0; i < addresses->len; i++) {
243                         if (addresses->val->addr_type == KRB5_ADDRESS_NETBIOS) {
244                                 workstation = talloc_strndup(tmp_ctx, addresses->val->address.data, MIN(addresses->val->address.length, 15));
245                                 if (workstation) {
246                                         break;
247                                 }
248                         }
249                 }
250         }
251
252         /* Strip space padding */
253         if (workstation) {
254                 i = MIN(strlen(workstation), 15);
255                 for (; i > 0 && workstation[i - 1] == ' '; i--) {
256                         workstation[i - 1] = '\0';
257                 }
258         }
259
260         nt_status = authsam_account_ok(tmp_ctx, 
261                                        private->samdb, 
262                                        MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT | MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT,
263                                        private->msg,
264                                        private->realm_ref_msg,
265                                        workstation,
266                                        name);
267         free(name);
268
269         /* TODO:  Need a more complete mapping of NTSTATUS to krb5kdc errors */
270
271         if (!NT_STATUS_IS_OK(nt_status)) {
272                 return KRB5KDC_ERR_POLICY;
273         }
274         return 0;
275 }
276