Only allow the trust in the correct direction (per the flags).
[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 "dsdb/common/flags.h"
25 #include "lib/ldb/include/ldb.h"
26 #include "librpc/gen_ndr/ndr_krb5pac.h"
27 #include "librpc/gen_ndr/krb5pac.h"
28 #include "auth/auth.h"
29 #include "auth/auth_sam.h"
30 #include "auth/auth_sam_reply.h"
31 #include "param/param.h"
32 #include "kdc/kdc.h"
33
34 struct krb5_dh_moduli;
35 struct _krb5_krb_auth_data;
36
37 krb5_error_code samba_kdc_plugin_init(krb5_context context, void **ptr) 
38 {
39         *ptr = NULL;
40         return 0;
41 }
42
43 void    samba_kdc_plugin_fini(void *ptr) 
44 {
45         return;
46 }
47
48 static krb5_error_code make_pac(krb5_context context,
49                                 TALLOC_CTX *mem_ctx, 
50                                 struct smb_iconv_convenience *iconv_convenience,
51                                 struct auth_serversupplied_info *server_info,
52                                 krb5_pac *pac) 
53 {
54         union PAC_INFO info;
55         struct netr_SamInfo3 *info3;
56         krb5_data pac_data;
57         NTSTATUS nt_status;
58         enum ndr_err_code ndr_err;
59         DATA_BLOB pac_out;
60         krb5_error_code ret;
61
62         ZERO_STRUCT(info);
63
64         nt_status = auth_convert_server_info_saminfo3(mem_ctx, server_info, &info3);
65         if (!NT_STATUS_IS_OK(nt_status)) {
66                 DEBUG(1, ("Getting Samba info failed: %s\n", nt_errstr(nt_status)));
67                 return EINVAL;
68         }
69
70         info.logon_info.info = talloc_zero(mem_ctx, struct PAC_LOGON_INFO);
71         if (!mem_ctx) {
72                 return ENOMEM;
73         }
74
75         info.logon_info.info->info3 = *info3;
76
77         ndr_err = ndr_push_union_blob(&pac_out, mem_ctx, iconv_convenience, &info,
78                                       PAC_TYPE_LOGON_INFO,
79                                       (ndr_push_flags_fn_t)ndr_push_PAC_INFO);
80         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
81                 nt_status = ndr_map_error2ntstatus(ndr_err);
82                 DEBUG(1, ("PAC (presig) push failed: %s\n", nt_errstr(nt_status)));
83                 return EINVAL;
84         }
85
86         ret = krb5_data_copy(&pac_data, pac_out.data, pac_out.length);
87         if (ret != 0) {
88                 return ret;
89         }
90
91         ret = krb5_pac_init(context, pac);
92         if (ret != 0) {
93                 krb5_data_free(&pac_data);
94                 return ret;
95         }
96
97         ret = krb5_pac_add_buffer(context, *pac, PAC_TYPE_LOGON_INFO, &pac_data);
98         krb5_data_free(&pac_data);
99         if (ret != 0) {
100                 return ret;
101         }
102
103         return ret;
104 }
105
106 /* Given the right private pointer from hdb_ldb, get a PAC from the attached ldb messages */
107 krb5_error_code samba_kdc_get_pac(void *priv,
108                                   krb5_context context, 
109                                   struct hdb_entry_ex *client,
110                                   krb5_pac *pac)
111 {
112         krb5_error_code ret;
113         NTSTATUS nt_status;
114         struct auth_serversupplied_info *server_info;
115         struct hdb_ldb_private *private = talloc_get_type(client->ctx, struct hdb_ldb_private);
116         TALLOC_CTX *mem_ctx = talloc_named(private, 0, "samba_get_pac context");
117         unsigned int userAccountControl;
118
119         if (!mem_ctx) {
120                 return ENOMEM;
121         }
122
123         /* The user account may be set not to want the PAC */
124         userAccountControl = ldb_msg_find_attr_as_uint(private->msg, "userAccountControl", 0);
125         if (userAccountControl & UF_NO_AUTH_DATA_REQUIRED) {
126                 *pac = NULL;
127                 return 0;
128         }
129
130         nt_status = authsam_make_server_info(mem_ctx, private->samdb, 
131                                              private->netbios_name,
132                                              private->msg, 
133                                              private->realm_ref_msg,
134                                              data_blob(NULL, 0),
135                                              data_blob(NULL, 0),
136                                              &server_info);
137         if (!NT_STATUS_IS_OK(nt_status)) {
138                 DEBUG(0, ("Getting user info for PAC failed: %s\n",
139                           nt_errstr(nt_status)));
140                 return ENOMEM;
141         }
142
143         ret = make_pac(context, mem_ctx, private->iconv_convenience, server_info, pac);
144
145         talloc_free(mem_ctx);
146         return ret;
147 }
148
149 /* Resign (and reform, including possibly new groups) a PAC */
150
151 krb5_error_code samba_kdc_reget_pac(void *priv, krb5_context context,
152                                 const krb5_principal client_principal,
153                                 struct hdb_entry_ex *client,  
154                                 struct hdb_entry_ex *server, krb5_pac *pac)
155 {
156         NTSTATUS nt_status;
157         enum ndr_err_code ndr_err;
158         krb5_error_code ret;
159
160         unsigned int userAccountControl;
161
162         struct hdb_ldb_private *private = talloc_get_type(server->ctx, struct hdb_ldb_private);
163         krb5_data k5pac_in;
164         DATA_BLOB pac_in;
165
166         union PAC_INFO info;
167         union netr_Validation validation;
168         struct auth_serversupplied_info *server_info_out;
169
170         TALLOC_CTX *mem_ctx = talloc_named(private, 0, "samba_get_pac context");
171         
172         if (!mem_ctx) {
173                 return ENOMEM;
174         }
175
176         /* The service account may be set not to want the PAC */
177         userAccountControl = ldb_msg_find_attr_as_uint(private->msg, "userAccountControl", 0);
178         if (userAccountControl & UF_NO_AUTH_DATA_REQUIRED) {
179                 *pac = NULL;
180                 return 0;
181         }
182
183         ret = krb5_pac_get_buffer(context, *pac, PAC_TYPE_LOGON_INFO, &k5pac_in);
184         if (ret != 0) {
185                 return ret;
186         }
187
188         pac_in = data_blob_talloc(mem_ctx, k5pac_in.data, k5pac_in.length);
189         krb5_data_free(&k5pac_in);
190         if (!pac_in.data) {
191                 talloc_free(mem_ctx);
192                 return ENOMEM;
193         }
194                 
195         ndr_err = ndr_pull_union_blob(&pac_in, mem_ctx, private->iconv_convenience, &info,
196                                       PAC_TYPE_LOGON_INFO,
197                                       (ndr_pull_flags_fn_t)ndr_pull_PAC_INFO);
198         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err) || !info.logon_info.info) {
199                 nt_status = ndr_map_error2ntstatus(ndr_err);
200                 DEBUG(0,("can't parse the PAC LOGON_INFO: %s\n", nt_errstr(nt_status)));
201                 talloc_free(mem_ctx);
202                 return EINVAL;
203         }
204
205         /* Pull this right into the normal auth sysstem structures */
206         validation.sam3 = &info.logon_info.info->info3;
207         nt_status = make_server_info_netlogon_validation(mem_ctx,
208                                                          "",
209                                                          3, &validation,
210                                                          &server_info_out); 
211         if (!NT_STATUS_IS_OK(nt_status)) {
212                 talloc_free(mem_ctx);
213                 return ENOMEM;
214         }
215
216         /* We will compleatly regenerate this pac */
217         krb5_pac_free(context, *pac);
218
219         ret = make_pac(context, mem_ctx, private->iconv_convenience, server_info_out, pac);
220
221         talloc_free(mem_ctx);
222         return ret;
223 }
224
225 static void samba_kdc_build_edata_reply(TALLOC_CTX *tmp_ctx, krb5_data *e_data,
226                                        NTSTATUS nt_status)
227 {
228         PA_DATA pa;
229         unsigned char *buf;
230         size_t len;
231         krb5_error_code ret = 0;
232
233         if (!e_data)
234                 return;
235
236         pa.padata_type          = KRB5_PADATA_PW_SALT;
237         pa.padata_value.length  = 12;
238         pa.padata_value.data    = malloc(pa.padata_value.length);
239         if (!pa.padata_value.data) {
240                 e_data->length = 0;
241                 e_data->data = NULL;
242                 return;
243         }
244
245         SIVAL(pa.padata_value.data, 0, NT_STATUS_V(nt_status));
246         SIVAL(pa.padata_value.data, 4, 0);
247         SIVAL(pa.padata_value.data, 8, 1);
248
249         ASN1_MALLOC_ENCODE(PA_DATA, buf, len, &pa, &len, ret);
250         free(pa.padata_value.data);
251
252         e_data->data   = buf;
253         e_data->length = len;
254
255         return;
256 }
257
258 /* Given an hdb entry (and in particular it's private member), consult
259  * the account_ok routine in auth/auth_sam.c for consistancy */
260
261
262 krb5_error_code samba_kdc_check_client_access(void *priv, 
263                                               krb5_context context, hdb_entry_ex *entry_ex, 
264                                               KDC_REQ *req,
265                                               krb5_data *e_data)
266 {
267         krb5_error_code ret;
268         NTSTATUS nt_status;
269         TALLOC_CTX *tmp_ctx = talloc_new(entry_ex->ctx);
270         struct hdb_ldb_private *private = talloc_get_type(entry_ex->ctx, struct hdb_ldb_private);
271         char *name, *workstation = NULL;
272         HostAddresses *addresses = req->req_body.addresses;
273         int i;
274
275         if (!tmp_ctx) {
276                 return ENOMEM;
277         }
278         
279         ret = krb5_unparse_name(context, entry_ex->entry.principal, &name);
280         if (ret != 0) {
281                 talloc_free(tmp_ctx);
282                 return ret;
283         }
284
285         if (addresses) {
286                 for (i=0; i < addresses->len; i++) {
287                         if (addresses->val->addr_type == KRB5_ADDRESS_NETBIOS) {
288                                 workstation = talloc_strndup(tmp_ctx, addresses->val->address.data, MIN(addresses->val->address.length, 15));
289                                 if (workstation) {
290                                         break;
291                                 }
292                         }
293                 }
294         }
295
296         /* Strip space padding */
297         if (workstation) {
298                 i = MIN(strlen(workstation), 15);
299                 for (; i > 0 && workstation[i - 1] == ' '; i--) {
300                         workstation[i - 1] = '\0';
301                 }
302         }
303
304         nt_status = authsam_account_ok(tmp_ctx, 
305                                        private->samdb, 
306                                        MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT | MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT,
307                                        private->msg,
308                                        private->realm_ref_msg,
309                                        workstation,
310                                        name);
311         free(name);
312
313         if (NT_STATUS_IS_OK(nt_status))
314                 return 0;
315
316         if (NT_STATUS_EQUAL(nt_status, NT_STATUS_PASSWORD_MUST_CHANGE))
317                 ret = KRB5KDC_ERR_KEY_EXPIRED;
318         else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_PASSWORD_EXPIRED))
319                 ret = KRB5KDC_ERR_KEY_EXPIRED;
320         else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_EXPIRED))
321                 ret = KRB5KDC_ERR_CLIENT_REVOKED;
322         else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_DISABLED))
323                 ret = KRB5KDC_ERR_CLIENT_REVOKED;
324         else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_LOGON_HOURS))
325                 ret = KRB5KDC_ERR_CLIENT_REVOKED;
326         else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_LOCKED_OUT))
327                 ret = KRB5KDC_ERR_CLIENT_REVOKED;
328         else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_WORKSTATION))
329                 ret = KRB5KDC_ERR_POLICY;
330         else
331                 ret = KRB5KDC_ERR_POLICY;
332
333         samba_kdc_build_edata_reply(tmp_ctx, e_data, nt_status);
334
335         return ret;
336 }
337