Merge branch 'v4-0-test' of ssh://git.samba.org/data/git/samba into v4-0-gmake3
[idra/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 #include "auth/auth_sam_reply.h"
32 #include "param/param.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         struct PAC_LOGON_INFO_CTR logon_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(logon_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         logon_info.info = talloc_zero(mem_ctx, struct PAC_LOGON_INFO);
71         if (!mem_ctx) {
72                 return ENOMEM;
73         }
74
75         logon_info.info->info3 = *info3;
76
77         ndr_err = ndr_push_struct_blob(&pac_out, mem_ctx, iconv_convenience, &logon_info,
78                                        (ndr_push_flags_fn_t)ndr_push_PAC_LOGON_INFO_CTR);
79         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
80                 nt_status = ndr_map_error2ntstatus(ndr_err);
81                 DEBUG(1, ("PAC (presig) push failed: %s\n", nt_errstr(nt_status)));
82                 return EINVAL;
83         }
84
85         ret = krb5_data_copy(&pac_data, pac_out.data, pac_out.length);
86         if (ret != 0) {
87                 return ret;
88         }
89
90         ret = krb5_pac_init(context, pac);
91         if (ret != 0) {
92                 krb5_data_free(&pac_data);
93                 return ret;
94         }
95
96         ret = krb5_pac_add_buffer(context, *pac, PAC_TYPE_LOGON_INFO, &pac_data);
97         krb5_data_free(&pac_data);
98         if (ret != 0) {
99                 return ret;
100         }
101
102         return ret;
103 }
104
105 /* Given the right private pointer from hdb_ldb, get a PAC from the attached ldb messages */
106 krb5_error_code samba_kdc_get_pac(void *priv,
107                                   krb5_context context, 
108                                   struct hdb_entry_ex *client,
109                                   krb5_pac *pac)
110 {
111         krb5_error_code ret;
112         NTSTATUS nt_status;
113         struct auth_serversupplied_info *server_info;
114         struct hdb_ldb_private *private = talloc_get_type(client->ctx, struct hdb_ldb_private);
115         TALLOC_CTX *mem_ctx = talloc_named(private, 0, "samba_get_pac context");
116         unsigned int userAccountControl;
117
118         if (!mem_ctx) {
119                 return ENOMEM;
120         }
121
122         /* The user account may be set not to want the PAC */
123         userAccountControl = ldb_msg_find_attr_as_uint(private->msg, "userAccountControl", 0);
124         if (userAccountControl & UF_NO_AUTH_DATA_REQUIRED) {
125                 *pac = NULL;
126                 return 0;
127         }
128
129         nt_status = authsam_make_server_info(mem_ctx, private->samdb, 
130                                              private->netbios_name,
131                                              private->msg, 
132                                              private->realm_ref_msg,
133                                              data_blob(NULL, 0),
134                                              data_blob(NULL, 0),
135                                              &server_info);
136         if (!NT_STATUS_IS_OK(nt_status)) {
137                 DEBUG(0, ("Getting user info for PAC failed: %s\n",
138                           nt_errstr(nt_status)));
139                 return ENOMEM;
140         }
141
142         ret = make_pac(context, mem_ctx, private->iconv_convenience, server_info, pac);
143
144         talloc_free(mem_ctx);
145         return ret;
146 }
147
148 /* Resign (and reform, including possibly new groups) a PAC */
149
150 krb5_error_code samba_kdc_reget_pac(void *priv, krb5_context context,
151                                 const krb5_principal client_principal,
152                                 struct hdb_entry_ex *client,  
153                                 struct hdb_entry_ex *server, krb5_pac *pac)
154 {
155         NTSTATUS nt_status;
156         enum ndr_err_code ndr_err;
157         krb5_error_code ret;
158
159         unsigned int userAccountControl;
160
161         struct hdb_ldb_private *private = talloc_get_type(server->ctx, struct hdb_ldb_private);
162         krb5_data k5pac_in;
163         DATA_BLOB pac_in;
164
165         struct PAC_LOGON_INFO_CTR logon_info;
166         union netr_Validation validation;
167         struct auth_serversupplied_info *server_info_out;
168
169         TALLOC_CTX *mem_ctx = talloc_named(private, 0, "samba_get_pac context");
170         
171         if (!mem_ctx) {
172                 return ENOMEM;
173         }
174
175         /* The service account may be set not to want the PAC */
176         userAccountControl = ldb_msg_find_attr_as_uint(private->msg, "userAccountControl", 0);
177         if (userAccountControl & UF_NO_AUTH_DATA_REQUIRED) {
178                 *pac = NULL;
179                 return 0;
180         }
181
182         ret = krb5_pac_get_buffer(context, *pac, PAC_TYPE_LOGON_INFO, &k5pac_in);
183         if (ret != 0) {
184                 return ret;
185         }
186
187         pac_in = data_blob_talloc(mem_ctx, k5pac_in.data, k5pac_in.length);
188         krb5_data_free(&k5pac_in);
189         if (!pac_in.data) {
190                 talloc_free(mem_ctx);
191                 return ENOMEM;
192         }
193                 
194         ndr_err = ndr_pull_struct_blob(&pac_in, mem_ctx, private->iconv_convenience, &logon_info,
195                                        (ndr_pull_flags_fn_t)ndr_pull_PAC_LOGON_INFO_CTR);
196         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err) || !logon_info.info) {
197                 nt_status = ndr_map_error2ntstatus(ndr_err);
198                 DEBUG(0,("can't parse the PAC LOGON_INFO: %s\n", nt_errstr(nt_status)));
199                 talloc_free(mem_ctx);
200                 return EINVAL;
201         }
202
203         /* Pull this right into the normal auth sysstem structures */
204         validation.sam3 = &logon_info.info->info3;
205         nt_status = make_server_info_netlogon_validation(mem_ctx,
206                                                          "",
207                                                          3, &validation,
208                                                          &server_info_out); 
209         if (!NT_STATUS_IS_OK(nt_status)) {
210                 talloc_free(mem_ctx);
211                 return ENOMEM;
212         }
213
214         /* We will compleatly regenerate this pac */
215         krb5_pac_free(context, *pac);
216
217         ret = make_pac(context, mem_ctx, private->iconv_convenience, server_info_out, pac);
218
219         talloc_free(mem_ctx);
220         return ret;
221 }
222
223 /* Given an hdb entry (and in particular it's private member), consult
224  * the account_ok routine in auth/auth_sam.c for consistancy */
225
226
227 krb5_error_code samba_kdc_check_client_access(void *priv, 
228                                               krb5_context context, hdb_entry_ex *entry_ex, 
229                                               KDC_REQ *req)
230 {
231         krb5_error_code ret;
232         NTSTATUS nt_status;
233         TALLOC_CTX *tmp_ctx = talloc_new(entry_ex->ctx);
234         struct hdb_ldb_private *private = talloc_get_type(entry_ex->ctx, struct hdb_ldb_private);
235         char *name, *workstation = NULL;
236         HostAddresses *addresses = req->req_body.addresses;
237         int i;
238
239         if (!tmp_ctx) {
240                 return ENOMEM;
241         }
242         
243         ret = krb5_unparse_name(context, entry_ex->entry.principal, &name);
244         if (ret != 0) {
245                 talloc_free(tmp_ctx);
246                 return ret;
247         }
248
249         if (addresses) {
250                 for (i=0; i < addresses->len; i++) {
251                         if (addresses->val->addr_type == KRB5_ADDRESS_NETBIOS) {
252                                 workstation = talloc_strndup(tmp_ctx, addresses->val->address.data, MIN(addresses->val->address.length, 15));
253                                 if (workstation) {
254                                         break;
255                                 }
256                         }
257                 }
258         }
259
260         /* Strip space padding */
261         if (workstation) {
262                 i = MIN(strlen(workstation), 15);
263                 for (; i > 0 && workstation[i - 1] == ' '; i--) {
264                         workstation[i - 1] = '\0';
265                 }
266         }
267
268         nt_status = authsam_account_ok(tmp_ctx, 
269                                        private->samdb, 
270                                        MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT | MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT,
271                                        private->msg,
272                                        private->realm_ref_msg,
273                                        workstation,
274                                        name);
275         free(name);
276
277         /* TODO:  Need a more complete mapping of NTSTATUS to krb5kdc errors */
278
279         /* TODO:  Also need to add the appropriate e-data struct of type
280          * PA-PW-SALT (3) that includes the NT_STATUS code, which gives Windows
281          * the information it needs to display the appropriate dialog. */
282
283         if (NT_STATUS_EQUAL(nt_status, NT_STATUS_PASSWORD_MUST_CHANGE))
284                 return KRB5KDC_ERR_KEY_EXPIRED;
285         else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_PASSWORD_EXPIRED))
286                 return KRB5KDC_ERR_KEY_EXPIRED;
287         else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_EXPIRED))
288                 return KRB5KDC_ERR_CLIENT_REVOKED;
289         else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_DISABLED))
290                 return KRB5KDC_ERR_CLIENT_REVOKED;
291         else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_LOGON_HOURS))
292                 return KRB5KDC_ERR_CLIENT_REVOKED;
293         else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_LOCKED_OUT))
294                 return KRB5KDC_ERR_CLIENT_REVOKED;
295         else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_WORKSTATION))
296                 return KRB5KDC_ERR_POLICY;
297         else if (!NT_STATUS_IS_OK(nt_status)) {
298                 return KRB5KDC_ERR_POLICY;
299         }
300
301         return 0;
302 }
303