smbd: Add a paranoia check for leases
[Samba.git] / source4 / kdc / kpasswd-service-mit.c
blob1546b16b3697af54bcc7de4d8c4fdcea3f8517b0
1 /*
2 Unix SMB/CIFS implementation.
4 Samba kpasswd implementation
6 Copyright (c) 2016 Andreas Schneider <asn@samba.org>
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.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "smbd/service_task.h"
24 #include "param/param.h"
25 #include "auth/auth.h"
26 #include "auth/gensec/gensec.h"
27 #include "kdc/kdc-server.h"
28 #include "kdc/kpasswd_glue.h"
29 #include "kdc/kpasswd-service.h"
30 #include "kdc/kpasswd-helper.h"
32 #define RFC3244_VERSION 0xff80
34 krb5_error_code decode_krb5_setpw_req(const krb5_data *code,
35 krb5_data **password_out,
36 krb5_principal *target_out);
38 static krb5_error_code kpasswd_change_password(struct kdc_server *kdc,
39 TALLOC_CTX *mem_ctx,
40 struct auth_session_info *session_info,
41 DATA_BLOB *password,
42 DATA_BLOB *kpasswd_reply,
43 const char **error_string)
45 NTSTATUS status;
46 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
47 enum samPwdChangeReason reject_reason;
48 const char *reject_string = NULL;
49 struct samr_DomInfo1 *dominfo;
50 bool ok;
52 status = samdb_kpasswd_change_password(mem_ctx,
53 kdc->task->lp_ctx,
54 kdc->task->event_ctx,
55 kdc->samdb,
56 session_info,
57 password,
58 &reject_reason,
59 &dominfo,
60 &reject_string,
61 &result);
62 if (!NT_STATUS_IS_OK(status)) {
63 ok = kpasswd_make_error_reply(mem_ctx,
64 KRB5_KPASSWD_ACCESSDENIED,
65 reject_string,
66 kpasswd_reply);
67 if (!ok) {
68 *error_string = "Failed to create reply";
69 return KRB5_KPASSWD_HARDERROR;
71 /* We want to send an an authenticated packet. */
72 return 0;
75 ok = kpasswd_make_pwchange_reply(mem_ctx,
76 result,
77 reject_reason,
78 dominfo,
79 kpasswd_reply);
80 if (!ok) {
81 *error_string = "Failed to create reply";
82 return KRB5_KPASSWD_HARDERROR;
85 return 0;
88 static krb5_error_code kpasswd_set_password(struct kdc_server *kdc,
89 TALLOC_CTX *mem_ctx,
90 struct auth_session_info *session_info,
91 DATA_BLOB *decoded_data,
92 DATA_BLOB *kpasswd_reply,
93 const char **error_string)
95 krb5_context context = kdc->smb_krb5_context->krb5_context;
96 krb5_data k_dec_data;
97 krb5_data *k_clear_data;
98 krb5_principal target_principal;
99 krb5_error_code code;
100 DATA_BLOB password;
101 char *target_realm = NULL;
102 char *target_name = NULL;
103 char *target_principal_string = NULL;
104 bool is_service_principal = false;
105 bool ok;
106 size_t num_components;
107 enum samPwdChangeReason reject_reason = SAM_PWD_CHANGE_NO_ERROR;
108 struct samr_DomInfo1 *dominfo = NULL;
109 NTSTATUS status;
111 k_dec_data.length = decoded_data->length;
112 k_dec_data.data = (char *)decoded_data->data;
114 code = decode_krb5_setpw_req(&k_dec_data,
115 &k_clear_data,
116 &target_principal);
117 if (code != 0) {
118 DBG_WARNING("decode_krb5_setpw_req failed: %s\n",
119 error_message(code));
120 ok = kpasswd_make_error_reply(mem_ctx,
121 KRB5_KPASSWD_MALFORMED,
122 "Failed to decode packet",
123 kpasswd_reply);
124 if (!ok) {
125 *error_string = "Failed to create reply";
126 return KRB5_KPASSWD_HARDERROR;
128 return 0;
131 ok = convert_string_talloc_handle(mem_ctx,
132 lpcfg_iconv_handle(kdc->task->lp_ctx),
133 CH_UTF8,
134 CH_UTF16,
135 (const char *)k_clear_data->data,
136 k_clear_data->length,
137 (void **)&password.data,
138 &password.length);
139 krb5_free_data(context, k_clear_data);
140 if (!ok) {
141 DBG_WARNING("String conversion failed\n");
142 *error_string = "String conversion failed";
143 return KRB5_KPASSWD_HARDERROR;
146 target_realm = smb_krb5_principal_get_realm(context, target_principal);
147 code = krb5_unparse_name_flags(context,
148 target_principal,
149 KRB5_PRINCIPAL_UNPARSE_NO_REALM,
150 &target_name);
151 if (code != 0) {
152 DBG_WARNING("Failed to parse principal\n");
153 *error_string = "String conversion failed";
154 return KRB5_KPASSWD_HARDERROR;
157 if ((target_name != NULL && target_realm == NULL) ||
158 (target_name == NULL && target_realm != NULL)) {
159 krb5_free_principal(context, target_principal);
160 SAFE_FREE(target_realm);
161 SAFE_FREE(target_name);
163 ok = kpasswd_make_error_reply(mem_ctx,
164 KRB5_KPASSWD_MALFORMED,
165 "Realm and principal must be "
166 "both present, or neither "
167 "present",
168 kpasswd_reply);
169 if (!ok) {
170 *error_string = "Failed to create reply";
171 return KRB5_KPASSWD_HARDERROR;
173 return 0;
176 if (target_name != NULL && target_realm != NULL) {
177 SAFE_FREE(target_realm);
178 SAFE_FREE(target_name);
179 } else {
180 krb5_free_principal(context, target_principal);
181 SAFE_FREE(target_realm);
182 SAFE_FREE(target_name);
184 return kpasswd_change_password(kdc,
185 mem_ctx,
186 session_info,
187 &password,
188 kpasswd_reply,
189 error_string);
192 num_components = krb5_princ_size(context, target_principal);
193 if (num_components >= 2) {
194 is_service_principal = true;
195 code = krb5_unparse_name_flags(context,
196 target_principal,
197 KRB5_PRINCIPAL_UNPARSE_SHORT,
198 &target_principal_string);
199 } else {
200 code = krb5_unparse_name(context,
201 target_principal,
202 &target_principal_string);
204 krb5_free_principal(context, target_principal);
205 if (code != 0) {
206 ok = kpasswd_make_error_reply(mem_ctx,
207 KRB5_KPASSWD_MALFORMED,
208 "Failed to parse principal",
209 kpasswd_reply);
210 if (!ok) {
211 *error_string = "Failed to create reply";
212 return KRB5_KPASSWD_HARDERROR;
216 status = kpasswd_samdb_set_password(mem_ctx,
217 kdc->task->event_ctx,
218 kdc->task->lp_ctx,
219 session_info,
220 is_service_principal,
221 target_principal_string,
222 &password,
223 &reject_reason,
224 &dominfo);
225 if (!NT_STATUS_IS_OK(status)) {
226 DBG_ERR("kpasswd_samdb_set_password failed - %s\n",
227 nt_errstr(status));
230 ok = kpasswd_make_pwchange_reply(mem_ctx,
231 status,
232 reject_reason,
233 dominfo,
234 kpasswd_reply);
235 if (!ok) {
236 *error_string = "Failed to create reply";
237 return KRB5_KPASSWD_HARDERROR;
240 return 0;
243 krb5_error_code kpasswd_handle_request(struct kdc_server *kdc,
244 TALLOC_CTX *mem_ctx,
245 struct gensec_security *gensec_security,
246 uint16_t verno,
247 DATA_BLOB *decoded_data,
248 DATA_BLOB *kpasswd_reply,
249 const char **error_string)
251 struct auth_session_info *session_info;
252 NTSTATUS status;
254 status = gensec_session_info(gensec_security,
255 mem_ctx,
256 &session_info);
257 if (!NT_STATUS_IS_OK(status)) {
258 *error_string = talloc_asprintf(mem_ctx,
259 "gensec_session_info failed - "
260 "%s",
261 nt_errstr(status));
262 return KRB5_KPASSWD_HARDERROR;
265 switch(verno) {
266 case 1: {
267 DATA_BLOB password;
268 bool ok;
270 ok = convert_string_talloc_handle(mem_ctx,
271 lpcfg_iconv_handle(kdc->task->lp_ctx),
272 CH_UTF8,
273 CH_UTF16,
274 (const char *)decoded_data->data,
275 decoded_data->length,
276 (void **)&password.data,
277 &password.length);
278 if (!ok) {
279 *error_string = "String conversion failed!";
280 DBG_WARNING("%s\n", *error_string);
281 return KRB5_KPASSWD_HARDERROR;
284 return kpasswd_change_password(kdc,
285 mem_ctx,
286 session_info,
287 &password,
288 kpasswd_reply,
289 error_string);
291 case RFC3244_VERSION: {
292 return kpasswd_set_password(kdc,
293 mem_ctx,
294 session_info,
295 decoded_data,
296 kpasswd_reply,
297 error_string);
299 default:
300 return KRB5_KPASSWD_BAD_VERSION;
303 return 0;