Finish removal of iconv_convenience in public API's.
[amitay/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-2009
7    Copyright (C) Simo Sorce <idra@samba.org> 2010
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "includes.h"
25 #include "../libds/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 "kdc/kdc.h"
33 #include "param/param.h"
34
35 static
36 NTSTATUS samba_get_logon_info_pac_blob(TALLOC_CTX *mem_ctx,
37                                        struct auth_serversupplied_info *info,
38                                        DATA_BLOB *pac_data)
39 {
40         struct netr_SamInfo3 *info3;
41         union PAC_INFO pac_info;
42         enum ndr_err_code ndr_err;
43         NTSTATUS nt_status;
44
45         ZERO_STRUCT(pac_info);
46
47         nt_status = auth_convert_server_info_saminfo3(mem_ctx, info, &info3);
48         if (!NT_STATUS_IS_OK(nt_status)) {
49                 DEBUG(1, ("Getting Samba info failed: %s\n",
50                           nt_errstr(nt_status)));
51                 return nt_status;
52         }
53
54         pac_info.logon_info.info = talloc_zero(mem_ctx, struct PAC_LOGON_INFO);
55         if (!mem_ctx) {
56                 return NT_STATUS_NO_MEMORY;
57         }
58
59         pac_info.logon_info.info->info3 = *info3;
60
61         ndr_err = ndr_push_union_blob(pac_data, mem_ctx, &pac_info,
62                                       PAC_TYPE_LOGON_INFO,
63                                       (ndr_push_flags_fn_t)ndr_push_PAC_INFO);
64         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
65                 nt_status = ndr_map_error2ntstatus(ndr_err);
66                 DEBUG(1, ("PAC (presig) push failed: %s\n",
67                           nt_errstr(nt_status)));
68                 return nt_status;
69         }
70
71         return NT_STATUS_OK;
72 }
73
74 krb5_error_code samba_make_krb5_pac(krb5_context context,
75                                     DATA_BLOB *pac_blob,
76                                     krb5_pac *pac)
77 {
78         krb5_data pac_data;
79         krb5_error_code ret;
80
81         /* The user account may be set not to want the PAC */
82         if (!pac_blob) {
83                 return 0;
84         }
85
86         ret = krb5_data_copy(&pac_data, pac_blob->data, pac_blob->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 bool samba_princ_needs_pac(struct hdb_entry_ex *princ)
107 {
108
109         struct samba_kdc_entry *p = talloc_get_type(princ->ctx, struct samba_kdc_entry);
110         unsigned int userAccountControl;
111
112
113         /* The service account may be set not to want the PAC */
114         userAccountControl = ldb_msg_find_attr_as_uint(p->msg, "userAccountControl", 0);
115         if (userAccountControl & UF_NO_AUTH_DATA_REQUIRED) {
116                 return false;
117         }
118
119         return true;
120 }
121
122 NTSTATUS samba_kdc_get_pac_blob(TALLOC_CTX *mem_ctx,
123                                 struct hdb_entry_ex *client,
124                                 DATA_BLOB **_pac_blob)
125 {
126         struct samba_kdc_entry *p = talloc_get_type(client->ctx, struct samba_kdc_entry);
127         struct auth_serversupplied_info *server_info;
128         DATA_BLOB *pac_blob;
129         NTSTATUS nt_status;
130
131         /* The user account may be set not to want the PAC */
132         if ( ! samba_princ_needs_pac(client)) {
133                 *_pac_blob = NULL;
134                 return NT_STATUS_OK;
135         }
136
137         pac_blob = talloc_zero(mem_ctx, DATA_BLOB);
138         if (!pac_blob) {
139                 return NT_STATUS_NO_MEMORY;
140         }
141
142         nt_status = authsam_make_server_info(mem_ctx, p->kdc_db_ctx->samdb,
143                                              lp_netbios_name(p->kdc_db_ctx->lp_ctx),
144                                              lp_sam_name(p->kdc_db_ctx->lp_ctx),
145                                              p->realm_dn,
146                                              p->msg,
147                                              data_blob(NULL, 0),
148                                              data_blob(NULL, 0),
149                                              &server_info);
150         if (!NT_STATUS_IS_OK(nt_status)) {
151                 DEBUG(0, ("Getting user info for PAC failed: %s\n",
152                           nt_errstr(nt_status)));
153                 return nt_status;
154         }
155
156         nt_status = samba_get_logon_info_pac_blob(mem_ctx, server_info, pac_blob);
157         if (!NT_STATUS_IS_OK(nt_status)) {
158                 DEBUG(0, ("Building PAC failed: %s\n",
159                           nt_errstr(nt_status)));
160                 return nt_status;
161         }
162
163         *_pac_blob = pac_blob;
164         return NT_STATUS_OK;
165 }
166
167 NTSTATUS samba_kdc_update_pac_blob(TALLOC_CTX *mem_ctx,
168                                    krb5_context context,
169                                    krb5_pac *pac, DATA_BLOB *pac_blob)
170 {
171         struct auth_serversupplied_info *server_info;
172         krb5_error_code ret;
173         NTSTATUS nt_status;
174
175         ret = kerberos_pac_to_server_info(mem_ctx, *pac,
176                                           context, &server_info);
177         if (ret) {
178                 return NT_STATUS_UNSUCCESSFUL;
179         }
180
181         nt_status = samba_get_logon_info_pac_blob(mem_ctx, 
182                                                   server_info, pac_blob);
183
184         return nt_status;
185 }
186
187 /* this function allocates 'data' using malloc.
188  * The caller is responsible for freeing it */
189 void samba_kdc_build_edata_reply(NTSTATUS nt_status, DATA_BLOB *e_data)
190 {
191         PA_DATA pa;
192         unsigned char *buf;
193         size_t len;
194         krb5_error_code ret = 0;
195
196         if (!e_data)
197                 return;
198
199         pa.padata_type          = KRB5_PADATA_PW_SALT;
200         pa.padata_value.length  = 12;
201         pa.padata_value.data    = malloc(pa.padata_value.length);
202         if (!pa.padata_value.data) {
203                 e_data->length = 0;
204                 e_data->data = NULL;
205                 return;
206         }
207
208         SIVAL(pa.padata_value.data, 0, NT_STATUS_V(nt_status));
209         SIVAL(pa.padata_value.data, 4, 0);
210         SIVAL(pa.padata_value.data, 8, 1);
211
212         ASN1_MALLOC_ENCODE(PA_DATA, buf, len, &pa, &len, ret);
213         free(pa.padata_value.data);
214
215         e_data->data   = buf;
216         e_data->length = len;
217
218         return;
219 }
220
221 /* function to map policy errors */
222 krb5_error_code samba_kdc_map_policy_err(NTSTATUS nt_status)
223 {
224         krb5_error_code ret;
225
226         if (NT_STATUS_EQUAL(nt_status, NT_STATUS_PASSWORD_MUST_CHANGE))
227                 ret = KRB5KDC_ERR_KEY_EXPIRED;
228         else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_PASSWORD_EXPIRED))
229                 ret = KRB5KDC_ERR_KEY_EXPIRED;
230         else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_EXPIRED))
231                 ret = KRB5KDC_ERR_CLIENT_REVOKED;
232         else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_DISABLED))
233                 ret = KRB5KDC_ERR_CLIENT_REVOKED;
234         else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_LOGON_HOURS))
235                 ret = KRB5KDC_ERR_CLIENT_REVOKED;
236         else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_ACCOUNT_LOCKED_OUT))
237                 ret = KRB5KDC_ERR_CLIENT_REVOKED;
238         else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_INVALID_WORKSTATION))
239                 ret = KRB5KDC_ERR_POLICY;
240         else
241                 ret = KRB5KDC_ERR_POLICY;
242
243         return ret;
244 }
245
246 /* Given a kdc entry, consult the account_ok routine in auth/auth_sam.c
247  * for consistency */
248 NTSTATUS samba_kdc_check_client_access(struct samba_kdc_entry *kdc_entry,
249                                        const char *client_name,
250                                        const char *workstation,
251                                        bool password_change)
252 {
253         TALLOC_CTX *tmp_ctx;
254         NTSTATUS nt_status;
255
256         tmp_ctx = talloc_named(NULL, 0, "samba_kdc_check_client_access");
257         if (!tmp_ctx) {
258                 return NT_STATUS_NO_MEMORY;
259         }
260
261         /* we allow all kinds of trusts here */
262         nt_status = authsam_account_ok(tmp_ctx,
263                                        kdc_entry->kdc_db_ctx->samdb,
264                                        MSV1_0_ALLOW_SERVER_TRUST_ACCOUNT |
265                                        MSV1_0_ALLOW_WORKSTATION_TRUST_ACCOUNT,
266                                        kdc_entry->realm_dn, kdc_entry->msg,
267                                        workstation, client_name,
268                                        true, password_change);
269
270         talloc_free(tmp_ctx);
271         return nt_status;
272 }
273