s4:sec_descriptor - fix constant
[Samba/ekacnet.git] / source4 / kdc / pac-glue.c
blob3d542d9a942e1c28cff459556f90e6167cdd7409
1 /*
2 Unix SMB/CIFS implementation.
4 PAC Glue between Samba and the KDC
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005-2009
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.
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.
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/>.
23 #include "includes.h"
24 #include "../libds/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 "kdc/kdc.h"
32 #include "param/param.h"
34 struct krb5_dh_moduli;
35 struct _krb5_krb_auth_data;
37 static krb5_error_code samba_kdc_plugin_init(krb5_context context, void **ptr)
39 *ptr = NULL;
40 return 0;
43 static void samba_kdc_plugin_fini(void *ptr)
45 return;
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)
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;
62 ZERO_STRUCT(info);
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;
70 info.logon_info.info = talloc_zero(mem_ctx, struct PAC_LOGON_INFO);
71 if (!mem_ctx) {
72 return ENOMEM;
75 info.logon_info.info->info3 = *info3;
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;
86 ret = krb5_data_copy(&pac_data, pac_out.data, pac_out.length);
87 if (ret != 0) {
88 return ret;
91 ret = krb5_pac_init(context, pac);
92 if (ret != 0) {
93 krb5_data_free(&pac_data);
94 return ret;
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;
103 return ret;
106 /* Given the right private pointer from hdb_samba4, get a PAC from the attached ldb messages */
107 static krb5_error_code samba_kdc_get_pac(void *priv,
108 krb5_context context,
109 struct hdb_entry_ex *client,
110 krb5_pac *pac)
112 krb5_error_code ret;
113 NTSTATUS nt_status;
114 struct auth_serversupplied_info *server_info;
115 struct hdb_samba4_private *p = talloc_get_type(client->ctx, struct hdb_samba4_private);
116 TALLOC_CTX *mem_ctx = talloc_named(p, 0, "samba_get_pac context");
117 unsigned int userAccountControl;
119 if (!mem_ctx) {
120 return ENOMEM;
123 /* The user account may be set not to want the PAC */
124 userAccountControl = ldb_msg_find_attr_as_uint(p->msg, "userAccountControl", 0);
125 if (userAccountControl & UF_NO_AUTH_DATA_REQUIRED) {
126 *pac = NULL;
127 return 0;
130 nt_status = authsam_make_server_info(mem_ctx, p->samdb,
131 lp_netbios_name(p->lp_ctx),
132 lp_sam_name(p->lp_ctx),
133 p->realm_dn,
134 p->msg,
135 data_blob(NULL, 0),
136 data_blob(NULL, 0),
137 &server_info);
138 if (!NT_STATUS_IS_OK(nt_status)) {
139 DEBUG(0, ("Getting user info for PAC failed: %s\n",
140 nt_errstr(nt_status)));
141 return ENOMEM;
144 ret = make_pac(context, mem_ctx, p->iconv_convenience, server_info, pac);
146 talloc_free(mem_ctx);
147 return ret;
150 /* Resign (and reform, including possibly new groups) a PAC */
152 static krb5_error_code samba_kdc_reget_pac(void *priv, krb5_context context,
153 const krb5_principal client_principal,
154 struct hdb_entry_ex *client,
155 struct hdb_entry_ex *server, krb5_pac *pac)
157 krb5_error_code ret;
159 unsigned int userAccountControl;
161 struct hdb_samba4_private *p = talloc_get_type(server->ctx, struct hdb_samba4_private);
163 struct auth_serversupplied_info *server_info_out;
165 TALLOC_CTX *mem_ctx = talloc_named(p, 0, "samba_get_pac context");
167 if (!mem_ctx) {
168 return ENOMEM;
171 /* The service account may be set not to want the PAC */
172 userAccountControl = ldb_msg_find_attr_as_uint(p->msg, "userAccountControl", 0);
173 if (userAccountControl & UF_NO_AUTH_DATA_REQUIRED) {
174 talloc_free(mem_ctx);
175 *pac = NULL;
176 return 0;
179 ret = kerberos_pac_to_server_info(mem_ctx, p->iconv_convenience,
180 *pac, context, &server_info_out);
182 /* We will compleatly regenerate this pac */
183 krb5_pac_free(context, *pac);
185 if (ret) {
186 talloc_free(mem_ctx);
187 return ret;
190 ret = make_pac(context, mem_ctx, p->iconv_convenience, server_info_out, pac);
192 talloc_free(mem_ctx);
193 return ret;
196 static void samba_kdc_build_edata_reply(TALLOC_CTX *tmp_ctx, krb5_data *e_data,
197 NTSTATUS nt_status)
199 PA_DATA pa;
200 unsigned char *buf;
201 size_t len;
202 krb5_error_code ret = 0;
204 if (!e_data)
205 return;
207 pa.padata_type = KRB5_PADATA_PW_SALT;
208 pa.padata_value.length = 12;
209 pa.padata_value.data = malloc(pa.padata_value.length);
210 if (!pa.padata_value.data) {
211 e_data->length = 0;
212 e_data->data = NULL;
213 return;
216 SIVAL(pa.padata_value.data, 0, NT_STATUS_V(nt_status));
217 SIVAL(pa.padata_value.data, 4, 0);
218 SIVAL(pa.padata_value.data, 8, 1);
220 ASN1_MALLOC_ENCODE(PA_DATA, buf, len, &pa, &len, ret);
221 free(pa.padata_value.data);
223 e_data->data = buf;
224 e_data->length = len;
226 return;
229 /* Given an hdb entry (and in particular it's private member), consult
230 * the account_ok routine in auth/auth_sam.c for consistancy */
233 static krb5_error_code samba_kdc_check_client_access(void *priv,
234 krb5_context context,
235 krb5_kdc_configuration *config,
236 hdb_entry_ex *client_ex, const char *client_name,
237 hdb_entry_ex *server_ex, const char *server_name,
238 KDC_REQ *req,
239 krb5_data *e_data)
241 krb5_error_code ret;
242 NTSTATUS nt_status;
243 TALLOC_CTX *tmp_ctx;
244 struct hdb_samba4_private *p;
245 char *workstation = NULL;
246 HostAddresses *addresses = req->req_body.addresses;
247 int i;
248 bool password_change;
250 tmp_ctx = talloc_new(client_ex->ctx);
251 p = talloc_get_type(client_ex->ctx, struct hdb_samba4_private);
253 if (!tmp_ctx) {
254 return ENOMEM;
257 if (addresses) {
258 for (i=0; i < addresses->len; i++) {
259 if (addresses->val->addr_type == KRB5_ADDRESS_NETBIOS) {
260 workstation = talloc_strndup(tmp_ctx, addresses->val->address.data, MIN(addresses->val->address.length, 15));
261 if (workstation) {
262 break;
268 /* Strip space padding */
269 if (workstation) {
270 i = MIN(strlen(workstation), 15);
271 for (; i > 0 && workstation[i - 1] == ' '; i--) {
272 workstation[i - 1] = '\0';
276 password_change = (server_ex && server_ex->entry.flags.change_pw);
278 /* we allow all kinds of trusts here */
279 nt_status = authsam_account_ok(tmp_ctx,
280 p->samdb,
281 MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT | MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT,
282 p->realm_dn,
283 p->msg,
284 workstation,
285 client_name, true, password_change);
287 if (NT_STATUS_IS_OK(nt_status)) {
288 /* Now do the standard Heimdal check */
289 ret = kdc_check_flags(context, config,
290 client_ex, client_name,
291 server_ex, server_name,
292 req->msg_type == krb_as_req);
293 } else {
294 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_PASSWORD_MUST_CHANGE))
295 ret = KRB5KDC_ERR_KEY_EXPIRED;
296 else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_PASSWORD_EXPIRED))
297 ret = KRB5KDC_ERR_KEY_EXPIRED;
298 else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_EXPIRED))
299 ret = KRB5KDC_ERR_CLIENT_REVOKED;
300 else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_DISABLED))
301 ret = KRB5KDC_ERR_CLIENT_REVOKED;
302 else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_LOGON_HOURS))
303 ret = KRB5KDC_ERR_CLIENT_REVOKED;
304 else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_LOCKED_OUT))
305 ret = KRB5KDC_ERR_CLIENT_REVOKED;
306 else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_WORKSTATION))
307 ret = KRB5KDC_ERR_POLICY;
308 else
309 ret = KRB5KDC_ERR_POLICY;
311 samba_kdc_build_edata_reply(tmp_ctx, e_data, nt_status);
314 return ret;
317 struct krb5plugin_windc_ftable windc_plugin_table = {
318 .minor_version = KRB5_WINDC_PLUGING_MINOR,
319 .init = samba_kdc_plugin_init,
320 .fini = samba_kdc_plugin_fini,
321 .pac_generate = samba_kdc_get_pac,
322 .pac_verify = samba_kdc_reget_pac,
323 .client_access = samba_kdc_check_client_access,