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