r20034: Start using ldb_search_exp_fmt()
[jelmer/samba4-debian.git] / source / 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/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 #include "heimdal/lib/krb5/krb5_locl.h"
36
37 /* Given the right private pointer from hdb_ldb, get a PAC from the attached ldb messages */
38 static krb5_error_code samba_get_pac(krb5_context context, 
39                                      struct hdb_ldb_private *private,
40                                      krb5_principal client, 
41                                      const krb5_keyblock *krbtgt_keyblock, 
42                                      const krb5_keyblock *server_keyblock, 
43                                      time_t tgs_authtime,
44                                      krb5_data *pac)
45 {
46         krb5_error_code ret;
47         NTSTATUS nt_status;
48         struct auth_serversupplied_info *server_info;
49         DATA_BLOB tmp_blob;
50         TALLOC_CTX *mem_ctx = talloc_named(private, 0, "samba_get_pac context");
51
52         if (!mem_ctx) {
53                 return ENOMEM;
54         }
55
56         nt_status = authsam_make_server_info(mem_ctx, private->samdb, 
57                                              private->msg, 
58                                              private->realm_ref_msg,
59                                              data_blob(NULL, 0),
60                                              data_blob(NULL, 0),
61                                              &server_info);
62         if (!NT_STATUS_IS_OK(nt_status)) {
63                 DEBUG(0, ("Getting user info for PAC failed: %s\n",
64                           nt_errstr(nt_status)));
65                 return ENOMEM;
66         }
67
68         ret = kerberos_create_pac(mem_ctx, server_info, 
69                                   context, 
70                                   krbtgt_keyblock,
71                                   server_keyblock,
72                                   client,
73                                   tgs_authtime,
74                                   &tmp_blob);
75
76         if (ret) {
77                 DEBUG(1, ("PAC encoding failed: %s\n", 
78                           smb_get_krb5_error_message(context, ret, mem_ctx)));
79                 talloc_free(mem_ctx);
80                 return ret;
81         }
82
83         ret = krb5_data_copy(pac, tmp_blob.data, tmp_blob.length);
84         talloc_free(mem_ctx);
85         return ret;
86 }
87
88 /* Wrap the PAC in the right ASN.1.  Will always free 'pac', on success or failure */
89 static krb5_error_code wrap_pac(krb5_context context, krb5_data *pac, AuthorizationData **out) 
90 {
91         krb5_error_code ret;
92
93         unsigned char *buf;
94         size_t buf_size;
95         size_t len;
96         
97         AD_IF_RELEVANT if_relevant;
98         AuthorizationData *auth_data;
99
100         if_relevant.len = 1;
101         if_relevant.val = malloc(sizeof(*if_relevant.val));
102         if (!if_relevant.val) {
103                 krb5_data_free(pac);
104                 *out = NULL;
105                 return ENOMEM;
106         }
107
108         if_relevant.val[0].ad_type = KRB5_AUTHDATA_WIN2K_PAC;
109         if_relevant.val[0].ad_data.data = NULL;
110         if_relevant.val[0].ad_data.length = 0;
111         
112         /* pac.data will be freed with this */
113         if_relevant.val[0].ad_data.data = pac->data;
114         if_relevant.val[0].ad_data.length = pac->length;
115         
116         ASN1_MALLOC_ENCODE(AuthorizationData, buf, buf_size, &if_relevant, &len, ret);
117         free_AuthorizationData(&if_relevant);
118         if (ret) {
119                 *out = NULL;
120                 return ret;
121         }               
122         
123         auth_data = malloc(sizeof(*auth_data));
124         if (!auth_data) {
125                 free(buf);
126                 *out = NULL;
127                 return ret;
128         }               
129         auth_data->len = 1;
130         auth_data->val = malloc(sizeof(*auth_data->val));
131         if (!auth_data->val) {
132                 free(buf);
133                 free(auth_data);
134                 *out = NULL;
135                 return ret;
136         }
137         auth_data->val[0].ad_type = KRB5_AUTHDATA_IF_RELEVANT;
138         auth_data->val[0].ad_data.length = len;
139         auth_data->val[0].ad_data.data = buf;
140
141         *out = auth_data;
142         return 0;
143 }
144
145
146 /* Given a hdb_entry, create a PAC out of the private data 
147
148    Don't create it if the client has the UF_NO_AUTH_DATA_REQUIRED bit
149    set, or if they specificaly asked not to get it.
150 */
151
152 krb5_error_code hdb_ldb_authz_data_as_req(krb5_context context, struct hdb_entry_ex *entry_ex, 
153                                           METHOD_DATA* pa_data_seq,
154                                           time_t authtime,
155                                           const EncryptionKey *tgtkey,
156                                           const EncryptionKey *sessionkey,
157                                           AuthorizationData **out)
158 {
159         krb5_error_code ret;
160         int i;
161         krb5_data pac;
162         krb5_boolean pac_wanted = TRUE;
163         unsigned int userAccountControl;
164         struct PA_PAC_REQUEST pac_request;
165         struct hdb_ldb_private *private = talloc_get_type(entry_ex->ctx, struct hdb_ldb_private);
166         
167         /* The user account may be set not to want the PAC */
168         userAccountControl = ldb_msg_find_attr_as_uint(private->msg, "userAccountControl", 0);
169         if (userAccountControl & UF_NO_AUTH_DATA_REQUIRED) {
170                 *out = NULL;
171                 return 0;
172         }
173
174         /* The user may not want a PAC */
175         for (i=0; i<pa_data_seq->len; i++) {
176                 if (pa_data_seq->val[i].padata_type == KRB5_PADATA_PA_PAC_REQUEST) {
177                         ret = decode_PA_PAC_REQUEST(pa_data_seq->val[i].padata_value.data, 
178                                                     pa_data_seq->val[i].padata_value.length, 
179                                                     &pac_request, NULL);
180                         if (ret == 0) {
181                                 pac_wanted = !!pac_request.include_pac;
182                         }
183                         free_PA_PAC_REQUEST(&pac_request);
184                         break;
185                 }
186         }
187
188         if (!pac_wanted) {
189                 *out = NULL;
190                 return 0;
191         }       
192
193         /* Get PAC from Samba */
194         ret = samba_get_pac(context,
195                             private,
196                             entry_ex->entry.principal,
197                             tgtkey,
198                             tgtkey,
199                             authtime,
200                             &pac);
201
202         if (ret) {
203                 *out = NULL;
204                 return ret;
205         }
206         
207         return wrap_pac(context, &pac, out);
208 }
209
210 /* Resign (and reform, including possibly new groups) a PAC */
211
212 krb5_error_code hdb_ldb_authz_data_tgs_req(krb5_context context, struct hdb_entry_ex *entry_ex, 
213                                            krb5_principal client, 
214                                            AuthorizationData *in, 
215                                            time_t authtime,
216                                            const EncryptionKey *tgtkey,
217                                            const EncryptionKey *servicekey,
218                                            const EncryptionKey *sessionkey,
219                                            AuthorizationData **out)
220 {
221         NTSTATUS nt_status;
222         krb5_error_code ret;
223
224         unsigned int userAccountControl;
225
226         struct hdb_ldb_private *private = talloc_get_type(entry_ex->ctx, struct hdb_ldb_private);
227         krb5_data k5pac_in, k5pac_out;
228         DATA_BLOB pac_in, pac_out;
229
230         struct PAC_LOGON_INFO *logon_info;
231         union netr_Validation validation;
232         struct auth_serversupplied_info *server_info_out;
233
234         krb5_boolean found = FALSE;
235         TALLOC_CTX *mem_ctx;
236         
237         /* The service account may be set not to want the PAC */
238         userAccountControl = ldb_msg_find_attr_as_uint(private->msg, "userAccountControl", 0);
239         if (userAccountControl & UF_NO_AUTH_DATA_REQUIRED) {
240                 *out = NULL;
241                 return 0;
242         }
243
244         ret = _krb5_find_type_in_ad(context, KRB5_AUTHDATA_WIN2K_PAC,
245                                     &k5pac_in, &found, sessionkey, in);
246         if (ret || !found) {
247                 *out = NULL;
248                 return 0;
249         }
250
251         mem_ctx = talloc_new(private);
252         if (!mem_ctx) {
253                 krb5_data_free(&k5pac_in);
254                 *out = NULL;
255                 return ENOMEM;
256         }
257
258         pac_in = data_blob_talloc(mem_ctx, k5pac_in.data, k5pac_in.length);
259         krb5_data_free(&k5pac_in);
260         if (!pac_in.data) {
261                 talloc_free(mem_ctx);
262                 *out = NULL;
263                 return ENOMEM;
264         }
265                 
266         /* Parse the PAC again, for the logon info */
267         nt_status = kerberos_pac_logon_info(mem_ctx, &logon_info,
268                                             pac_in,
269                                             context,
270                                             tgtkey, 
271                                             tgtkey, 
272                                             client, authtime, 
273                                             &ret);
274
275         if (!NT_STATUS_IS_OK(nt_status)) {
276                 DEBUG(1, ("Failed to parse PAC in TGT: %s/%s\n", 
277                           nt_errstr(nt_status), error_message(ret)));
278                 talloc_free(mem_ctx);
279                 *out = NULL;
280                 return ret;
281         }
282
283         /* Pull this right into the normal auth sysstem structures */
284         validation.sam3 = &logon_info->info3;
285         nt_status = make_server_info_netlogon_validation(mem_ctx,
286                                                          "",
287                                                          3, &validation,
288                                                          &server_info_out); 
289         if (!NT_STATUS_IS_OK(nt_status)) {
290                 talloc_free(mem_ctx);
291                 *out = NULL;
292                 return ENOMEM;
293         }
294
295         /* And make a new PAC, possibly containing new groups */
296         ret = kerberos_create_pac(mem_ctx, 
297                                   server_info_out,
298                                   context,
299                                   tgtkey,
300                                   servicekey,
301                                   client,
302                                   authtime,
303                                   &pac_out);
304
305         if (ret != 0) {
306                 talloc_free(mem_ctx);
307                 *out = NULL;
308                 return ret;
309         }
310
311         ret = krb5_data_copy(&k5pac_out, pac_out.data, pac_out.length);
312         if (ret != 0) {
313                 talloc_free(mem_ctx);
314                 *out = NULL;
315                 return ret;
316         }
317
318         return wrap_pac(context, &k5pac_out, out);
319 }
320
321 /* Given an hdb entry (and in particular it's private member), consult
322  * the account_ok routine in auth/auth_sam.c for consistancy */
323
324 krb5_error_code hdb_ldb_check_client_access(krb5_context context, hdb_entry_ex *entry_ex, 
325                                             HostAddresses *addresses)
326 {
327         krb5_error_code ret;
328         NTSTATUS nt_status;
329         TALLOC_CTX *tmp_ctx = talloc_new(entry_ex->ctx);
330         struct hdb_ldb_private *private = talloc_get_type(entry_ex->ctx, struct hdb_ldb_private);
331         char *name, *workstation = NULL;
332         int i;
333
334         if (!tmp_ctx) {
335                 return ENOMEM;
336         }
337         
338         ret = krb5_unparse_name(context, entry_ex->entry.principal, &name);
339         if (ret != 0) {
340                 talloc_free(tmp_ctx);
341                 return ret;
342         }
343
344         if (addresses) {
345                 for (i=0; i < addresses->len; i++) {
346                         if (addresses->val->addr_type == KRB5_ADDRESS_NETBIOS) {
347                                 workstation = talloc_strndup(tmp_ctx, addresses->val->address.data, MIN(addresses->val->address.length, 15));
348                                 if (workstation) {
349                                         break;
350                                 }
351                         }
352                 }
353         }
354
355         /* Strip space padding */
356         if (workstation) {
357                 i = MIN(strlen(workstation), 15);
358                 for (; i > 0 && workstation[i - 1] == ' '; i--) {
359                         workstation[i - 1] = '\0';
360                 }
361         }
362
363         nt_status = authsam_account_ok(tmp_ctx, 
364                                        private->samdb, 
365                                        MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT | MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT,
366                                        private->msg,
367                                        private->realm_ref_msg,
368                                        workstation,
369                                        name);
370         free(name);
371
372         /* TODO:  Need a more complete mapping of NTSTATUS to krb5kdc errors */
373
374         if (!NT_STATUS_IS_OK(nt_status)) {
375                 return KRB5KDC_ERR_POLICY;
376         }
377         return 0;
378 }
379