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