nfs4acls: Use talloc_realloc()
[Samba.git] / source4 / kdc / kpasswdd.c
blobb686e2be3b81b5004c47d8b288d4d6413b48f3ef
1 /*
2 Unix SMB/CIFS implementation.
4 kpasswd Server implementation
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005
7 Copyright (C) Andrew Tridgell 2005
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.
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.
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 "smbd/service_task.h"
25 #include "auth/gensec/gensec.h"
26 #include "auth/credentials/credentials.h"
27 #include "auth/auth.h"
28 #include "dsdb/samdb/samdb.h"
29 #include "../lib/util/util_ldb.h"
30 #include "libcli/security/security.h"
31 #include "param/param.h"
32 #include "kdc/kdc-glue.h"
33 #include "dsdb/common/util.h"
34 #include "kdc/kpasswd_glue.h"
36 /* Return true if there is a valid error packet formed in the error_blob */
37 static bool kpasswdd_make_error_reply(struct kdc_server *kdc,
38 TALLOC_CTX *mem_ctx,
39 uint16_t result_code,
40 const char *error_string,
41 DATA_BLOB *error_blob)
43 char *error_string_utf8;
44 size_t len;
46 DEBUG(result_code ? 3 : 10, ("kpasswdd: %s\n", error_string));
48 if (!push_utf8_talloc(mem_ctx, &error_string_utf8, error_string, &len)) {
49 return false;
52 *error_blob = data_blob_talloc(mem_ctx, NULL, 2 + len + 1);
53 if (!error_blob->data) {
54 return false;
56 RSSVAL(error_blob->data, 0, result_code);
57 memcpy(error_blob->data + 2, error_string_utf8, len + 1);
58 return true;
61 /* Return true if there is a valid error packet formed in the error_blob */
62 static bool kpasswdd_make_unauth_error_reply(struct kdc_server *kdc,
63 TALLOC_CTX *mem_ctx,
64 uint16_t result_code,
65 const char *error_string,
66 DATA_BLOB *error_blob)
68 bool ret;
69 int kret;
70 DATA_BLOB error_bytes;
71 krb5_data k5_error_bytes, k5_error_blob;
72 ret = kpasswdd_make_error_reply(kdc, mem_ctx, result_code, error_string,
73 &error_bytes);
74 if (!ret) {
75 return false;
77 k5_error_bytes.data = error_bytes.data;
78 k5_error_bytes.length = error_bytes.length;
79 kret = krb5_mk_error(kdc->smb_krb5_context->krb5_context,
80 result_code, NULL, &k5_error_bytes,
81 NULL, NULL, NULL, NULL, &k5_error_blob);
82 if (kret) {
83 return false;
85 *error_blob = data_blob_talloc(mem_ctx, k5_error_blob.data, k5_error_blob.length);
86 krb5_data_free(&k5_error_blob);
87 if (!error_blob->data) {
88 return false;
90 return true;
93 static bool kpasswd_make_pwchange_reply(struct kdc_server *kdc,
94 TALLOC_CTX *mem_ctx,
95 NTSTATUS status,
96 enum samPwdChangeReason reject_reason,
97 struct samr_DomInfo1 *dominfo,
98 DATA_BLOB *error_blob)
100 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
101 return kpasswdd_make_error_reply(kdc, mem_ctx,
102 KRB5_KPASSWD_ACCESSDENIED,
103 "No such user when changing password",
104 error_blob);
106 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
107 return kpasswdd_make_error_reply(kdc, mem_ctx,
108 KRB5_KPASSWD_ACCESSDENIED,
109 "Not permitted to change password",
110 error_blob);
112 if (dominfo && NT_STATUS_EQUAL(status, NT_STATUS_PASSWORD_RESTRICTION)) {
113 const char *reject_string;
114 switch (reject_reason) {
115 case SAM_PWD_CHANGE_PASSWORD_TOO_SHORT:
116 reject_string = talloc_asprintf(mem_ctx, "Password too short, password must be at least %d characters long.",
117 dominfo->min_password_length);
118 break;
119 case SAM_PWD_CHANGE_NOT_COMPLEX:
120 reject_string = "Password does not meet complexity requirements";
121 break;
122 case SAM_PWD_CHANGE_PWD_IN_HISTORY:
123 reject_string = talloc_asprintf(mem_ctx, "Password is already in password history. New password must not match any of your %d previous passwords.",
124 dominfo->password_history_length);
125 break;
126 default:
127 reject_string = "Password change rejected, password changes may not be permitted on this account, or the minimum password age may not have elapsed.";
128 break;
130 return kpasswdd_make_error_reply(kdc, mem_ctx,
131 KRB5_KPASSWD_SOFTERROR,
132 reject_string,
133 error_blob);
135 if (!NT_STATUS_IS_OK(status)) {
136 return kpasswdd_make_error_reply(kdc, mem_ctx,
137 KRB5_KPASSWD_HARDERROR,
138 talloc_asprintf(mem_ctx, "failed to set password: %s", nt_errstr(status)),
139 error_blob);
142 return kpasswdd_make_error_reply(kdc, mem_ctx, KRB5_KPASSWD_SUCCESS,
143 "Password changed",
144 error_blob);
148 A user password change
150 Return true if there is a valid error packet (or success) formed in
151 the error_blob
153 static bool kpasswdd_change_password(struct kdc_server *kdc,
154 TALLOC_CTX *mem_ctx,
155 struct auth_session_info *session_info,
156 const DATA_BLOB *password,
157 DATA_BLOB *reply)
159 NTSTATUS status;
160 NTSTATUS result = NT_STATUS_UNSUCCESSFUL;
161 enum samPwdChangeReason reject_reason;
162 struct samr_DomInfo1 *dominfo;
163 const char *error_string;
165 status = samdb_kpasswd_change_password(mem_ctx,
166 kdc->task->lp_ctx,
167 kdc->task->event_ctx,
168 kdc->samdb,
169 session_info,
170 password,
171 &reject_reason,
172 &dominfo,
173 &error_string,
174 &result);
175 if (!NT_STATUS_IS_OK(status)) {
176 return kpasswdd_make_error_reply(kdc,
177 mem_ctx,
178 KRB5_KPASSWD_ACCESSDENIED,
179 error_string,
180 reply);
183 return kpasswd_make_pwchange_reply(kdc,
184 mem_ctx,
185 result,
186 reject_reason,
187 dominfo,
188 reply);
191 static bool kpasswd_process_request(struct kdc_server *kdc,
192 TALLOC_CTX *mem_ctx,
193 struct gensec_security *gensec_security,
194 uint16_t version,
195 DATA_BLOB *input,
196 DATA_BLOB *reply)
198 struct auth_session_info *session_info;
199 size_t pw_len;
201 if (!NT_STATUS_IS_OK(gensec_session_info(gensec_security,
202 mem_ctx,
203 &session_info))) {
204 return kpasswdd_make_error_reply(kdc, mem_ctx,
205 KRB5_KPASSWD_HARDERROR,
206 "gensec_session_info failed!",
207 reply);
210 switch (version) {
211 case KRB5_KPASSWD_VERS_CHANGEPW:
213 DATA_BLOB password;
214 if (!convert_string_talloc_handle(mem_ctx, lpcfg_iconv_handle(kdc->task->lp_ctx),
215 CH_UTF8, CH_UTF16,
216 (const char *)input->data,
217 input->length,
218 (void **)&password.data, &pw_len)) {
219 return false;
221 password.length = pw_len;
223 return kpasswdd_change_password(kdc, mem_ctx, session_info,
224 &password, reply);
226 case KRB5_KPASSWD_VERS_SETPW:
228 NTSTATUS status;
229 enum samPwdChangeReason reject_reason = SAM_PWD_CHANGE_NO_ERROR;
230 struct samr_DomInfo1 *dominfo = NULL;
231 struct ldb_context *samdb;
232 krb5_context context = kdc->smb_krb5_context->krb5_context;
234 ChangePasswdDataMS chpw;
235 DATA_BLOB password;
237 krb5_principal principal;
238 char *set_password_on_princ;
239 struct ldb_dn *set_password_on_dn;
240 bool service_principal_name = false;
242 size_t len;
243 int ret;
245 ret = decode_ChangePasswdDataMS(input->data, input->length,
246 &chpw, &len);
247 if (ret) {
248 return kpasswdd_make_error_reply(kdc, mem_ctx,
249 KRB5_KPASSWD_MALFORMED,
250 "failed to decode password change structure",
251 reply);
254 if (!convert_string_talloc_handle(mem_ctx, lpcfg_iconv_handle(kdc->task->lp_ctx),
255 CH_UTF8, CH_UTF16,
256 (const char *)chpw.newpasswd.data,
257 chpw.newpasswd.length,
258 (void **)&password.data, &pw_len)) {
259 free_ChangePasswdDataMS(&chpw);
260 return false;
263 password.length = pw_len;
265 if ((chpw.targname && !chpw.targrealm)
266 || (!chpw.targname && chpw.targrealm)) {
267 free_ChangePasswdDataMS(&chpw);
268 return kpasswdd_make_error_reply(kdc, mem_ctx,
269 KRB5_KPASSWD_MALFORMED,
270 "Realm and principal must be both present, or neither present",
271 reply);
273 if (chpw.targname && chpw.targrealm) {
274 ret = krb5_build_principal_ext(kdc->smb_krb5_context->krb5_context,
275 &principal,
276 strlen(*chpw.targrealm),
277 *chpw.targrealm, 0);
278 if (ret) {
279 free_ChangePasswdDataMS(&chpw);
280 return kpasswdd_make_error_reply(kdc, mem_ctx,
281 KRB5_KPASSWD_MALFORMED,
282 "failed to get principal",
283 reply);
285 if (copy_PrincipalName(chpw.targname, &principal->name)) {
286 free_ChangePasswdDataMS(&chpw);
287 krb5_free_principal(context, principal);
288 return kpasswdd_make_error_reply(kdc, mem_ctx,
289 KRB5_KPASSWD_MALFORMED,
290 "failed to extract principal to set",
291 reply);
293 } else {
294 free_ChangePasswdDataMS(&chpw);
295 return kpasswdd_change_password(kdc, mem_ctx, session_info,
296 &password, reply);
298 free_ChangePasswdDataMS(&chpw);
300 if (principal->name.name_string.len >= 2) {
301 service_principal_name = true;
303 /* We use this, rather than 'no realm' flag,
304 * as we don't want to accept a password
305 * change on a principal from another realm */
307 if (krb5_unparse_name_short(context, principal, &set_password_on_princ) != 0) {
308 krb5_free_principal(context, principal);
309 return kpasswdd_make_error_reply(kdc, mem_ctx,
310 KRB5_KPASSWD_MALFORMED,
311 "krb5_unparse_name failed!",
312 reply);
314 } else {
315 if (krb5_unparse_name(context, principal, &set_password_on_princ) != 0) {
316 krb5_free_principal(context, principal);
317 return kpasswdd_make_error_reply(kdc, mem_ctx,
318 KRB5_KPASSWD_MALFORMED,
319 "krb5_unparse_name failed!",
320 reply);
323 krb5_free_principal(context, principal);
325 samdb = samdb_connect(mem_ctx, kdc->task->event_ctx, kdc->task->lp_ctx, session_info, 0);
326 if (!samdb) {
327 free(set_password_on_princ);
328 return kpasswdd_make_error_reply(kdc, mem_ctx,
329 KRB5_KPASSWD_HARDERROR,
330 "Unable to open database!",
331 reply);
334 DEBUG(3, ("%s\\%s (%s) is changing password of %s\n",
335 session_info->info->domain_name,
336 session_info->info->account_name,
337 dom_sid_string(mem_ctx, &session_info->security_token->sids[PRIMARY_USER_SID_INDEX]),
338 set_password_on_princ));
339 ret = ldb_transaction_start(samdb);
340 if (ret != LDB_SUCCESS) {
341 free(set_password_on_princ);
342 status = NT_STATUS_TRANSACTION_ABORTED;
343 return kpasswd_make_pwchange_reply(kdc, mem_ctx,
344 status,
345 SAM_PWD_CHANGE_NO_ERROR,
346 NULL,
347 reply);
350 if (service_principal_name) {
351 status = crack_service_principal_name(samdb, mem_ctx,
352 set_password_on_princ,
353 &set_password_on_dn, NULL);
354 } else {
355 status = crack_user_principal_name(samdb, mem_ctx,
356 set_password_on_princ,
357 &set_password_on_dn, NULL);
359 free(set_password_on_princ);
360 if (!NT_STATUS_IS_OK(status)) {
361 ldb_transaction_cancel(samdb);
362 return kpasswd_make_pwchange_reply(kdc, mem_ctx,
363 status,
364 SAM_PWD_CHANGE_NO_ERROR,
365 NULL,
366 reply);
369 if (NT_STATUS_IS_OK(status)) {
370 /* Admin password set */
371 status = samdb_set_password(samdb, mem_ctx,
372 set_password_on_dn, NULL,
373 &password, NULL, NULL,
374 NULL, NULL, /* this is not a user password change */
375 &reject_reason, &dominfo);
378 if (NT_STATUS_IS_OK(status)) {
379 ret = ldb_transaction_commit(samdb);
380 if (ret != LDB_SUCCESS) {
381 DEBUG(1,("Failed to commit transaction to set password on %s: %s\n",
382 ldb_dn_get_linearized(set_password_on_dn),
383 ldb_errstring(samdb)));
384 status = NT_STATUS_TRANSACTION_ABORTED;
386 } else {
387 ldb_transaction_cancel(samdb);
389 return kpasswd_make_pwchange_reply(kdc, mem_ctx,
390 status,
391 reject_reason,
392 dominfo,
393 reply);
395 default:
396 return kpasswdd_make_error_reply(kdc, mem_ctx,
397 KRB5_KPASSWD_BAD_VERSION,
398 talloc_asprintf(mem_ctx,
399 "Protocol version %u not supported",
400 version),
401 reply);
405 enum kdc_process_ret kpasswdd_process(struct kdc_server *kdc,
406 TALLOC_CTX *mem_ctx,
407 DATA_BLOB *input,
408 DATA_BLOB *reply,
409 struct tsocket_address *peer_addr,
410 struct tsocket_address *my_addr,
411 int datagram_reply)
413 bool ret;
414 const uint16_t header_len = 6;
415 uint16_t len;
416 uint16_t ap_req_len;
417 uint16_t krb_priv_len;
418 uint16_t version;
419 NTSTATUS nt_status;
420 DATA_BLOB ap_req, krb_priv_req;
421 DATA_BLOB krb_priv_rep = data_blob(NULL, 0);
422 DATA_BLOB ap_rep = data_blob(NULL, 0);
423 DATA_BLOB kpasswd_req, kpasswd_rep;
424 struct cli_credentials *server_credentials;
425 struct gensec_security *gensec_security;
426 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
428 char *keytab_name;
430 if (!tmp_ctx) {
431 return KDC_PROCESS_FAILED;
434 if (kdc->am_rodc) {
435 talloc_free(tmp_ctx);
436 return KDC_PROCESS_PROXY;
439 /* Be parinoid. We need to ensure we don't just let the
440 * caller lead us into a buffer overflow */
441 if (input->length <= header_len) {
442 talloc_free(tmp_ctx);
443 return KDC_PROCESS_FAILED;
446 len = RSVAL(input->data, 0);
447 if (input->length != len) {
448 talloc_free(tmp_ctx);
449 return KDC_PROCESS_FAILED;
452 /* There are two different versions of this protocol so far,
453 * plus others in the standards pipe. Fortunetly they all
454 * take a very similar framing */
455 version = RSVAL(input->data, 2);
456 ap_req_len = RSVAL(input->data, 4);
457 if ((ap_req_len >= len) || (ap_req_len + header_len) >= len) {
458 talloc_free(tmp_ctx);
459 return KDC_PROCESS_FAILED;
462 krb_priv_len = len - ap_req_len;
463 ap_req = data_blob_const(&input->data[header_len], ap_req_len);
464 krb_priv_req = data_blob_const(&input->data[header_len + ap_req_len], krb_priv_len);
466 server_credentials = cli_credentials_init(tmp_ctx);
467 if (!server_credentials) {
468 DEBUG(1, ("Failed to init server credentials\n"));
469 talloc_free(tmp_ctx);
470 return KDC_PROCESS_FAILED;
473 /* We want the credentials subsystem to use the krb5 context
474 * we already have, rather than a new context */
475 cli_credentials_set_krb5_context(server_credentials, kdc->smb_krb5_context);
476 cli_credentials_set_conf(server_credentials, kdc->task->lp_ctx);
478 keytab_name = talloc_asprintf(server_credentials, "HDB:samba4&%p", kdc->base_ctx);
480 cli_credentials_set_username(server_credentials, "kadmin/changepw", CRED_SPECIFIED);
481 ret = cli_credentials_set_keytab_name(server_credentials, kdc->task->lp_ctx, keytab_name, CRED_SPECIFIED);
482 if (ret != 0) {
483 ret = kpasswdd_make_unauth_error_reply(kdc, mem_ctx,
484 KRB5_KPASSWD_HARDERROR,
485 talloc_asprintf(mem_ctx,
486 "Failed to obtain server credentials for kadmin/changepw!"),
487 &krb_priv_rep);
488 ap_rep.length = 0;
489 if (ret) {
490 goto reply;
492 talloc_free(tmp_ctx);
493 return ret;
496 /* We don't strictly need to call this wrapper, and could call
497 * gensec_server_start directly, as we have no need for NTLM
498 * and we have a PAC, but this ensures that the wrapper can be
499 * safely extended for other helpful things in future */
500 nt_status = samba_server_gensec_start(tmp_ctx, kdc->task->event_ctx,
501 kdc->task->msg_ctx,
502 kdc->task->lp_ctx,
503 server_credentials,
504 "kpasswd",
505 &gensec_security);
506 if (!NT_STATUS_IS_OK(nt_status)) {
507 talloc_free(tmp_ctx);
508 return KDC_PROCESS_FAILED;
511 /* The kerberos PRIV packets include these addresses. MIT
512 * clients check that they are present */
513 #if 0
514 /* Skip this part for now, it breaks with a NetAPP filer and
515 * in any case where the client address is behind NAT. If
516 * older MIT clients need this, we might have to insert more
517 * complex code */
519 nt_status = gensec_set_remote_address(gensec_security, peer_addr);
520 if (!NT_STATUS_IS_OK(nt_status)) {
521 talloc_free(tmp_ctx);
522 return KDC_PROCESS_FAILED;
524 #endif
526 nt_status = gensec_set_local_address(gensec_security, my_addr);
527 if (!NT_STATUS_IS_OK(nt_status)) {
528 talloc_free(tmp_ctx);
529 return KDC_PROCESS_FAILED;
532 /* We want the GENSEC wrap calls to generate PRIV tokens */
533 gensec_want_feature(gensec_security, GENSEC_FEATURE_SEAL);
535 nt_status = gensec_start_mech_by_name(gensec_security, "krb5");
536 if (!NT_STATUS_IS_OK(nt_status)) {
537 talloc_free(tmp_ctx);
538 return KDC_PROCESS_FAILED;
541 /* Accept the AP-REQ and generate teh AP-REP we need for the reply */
542 nt_status = gensec_update_ev(gensec_security, tmp_ctx, kdc->task->event_ctx, ap_req, &ap_rep);
543 if (!NT_STATUS_IS_OK(nt_status) && !NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
545 ret = kpasswdd_make_unauth_error_reply(kdc, mem_ctx,
546 KRB5_KPASSWD_HARDERROR,
547 talloc_asprintf(mem_ctx,
548 "gensec_update failed: %s",
549 nt_errstr(nt_status)),
550 &krb_priv_rep);
551 ap_rep.length = 0;
552 if (ret) {
553 goto reply;
555 talloc_free(tmp_ctx);
556 return KDC_PROCESS_FAILED;
559 /* Extract the data from the KRB-PRIV half of the message */
560 nt_status = gensec_unwrap(gensec_security, tmp_ctx, &krb_priv_req, &kpasswd_req);
561 if (!NT_STATUS_IS_OK(nt_status)) {
562 ret = kpasswdd_make_unauth_error_reply(kdc, mem_ctx,
563 KRB5_KPASSWD_HARDERROR,
564 talloc_asprintf(mem_ctx,
565 "gensec_unwrap failed: %s",
566 nt_errstr(nt_status)),
567 &krb_priv_rep);
568 ap_rep.length = 0;
569 if (ret) {
570 goto reply;
572 talloc_free(tmp_ctx);
573 return KDC_PROCESS_FAILED;
576 /* Figure out something to do with it (probably changing a password...) */
577 ret = kpasswd_process_request(kdc, tmp_ctx,
578 gensec_security,
579 version,
580 &kpasswd_req, &kpasswd_rep);
581 if (!ret) {
582 /* Argh! */
583 talloc_free(tmp_ctx);
584 return KDC_PROCESS_FAILED;
587 /* And wrap up the reply: This ensures that the error message
588 * or success can be verified by the client */
589 nt_status = gensec_wrap(gensec_security, tmp_ctx,
590 &kpasswd_rep, &krb_priv_rep);
591 if (!NT_STATUS_IS_OK(nt_status)) {
592 ret = kpasswdd_make_unauth_error_reply(kdc, mem_ctx,
593 KRB5_KPASSWD_HARDERROR,
594 talloc_asprintf(mem_ctx,
595 "gensec_wrap failed: %s",
596 nt_errstr(nt_status)),
597 &krb_priv_rep);
598 ap_rep.length = 0;
599 if (ret) {
600 goto reply;
602 talloc_free(tmp_ctx);
603 return KDC_PROCESS_FAILED;
606 reply:
607 *reply = data_blob_talloc(mem_ctx, NULL, krb_priv_rep.length + ap_rep.length + header_len);
608 if (!reply->data) {
609 talloc_free(tmp_ctx);
610 return KDC_PROCESS_FAILED;
613 RSSVAL(reply->data, 0, reply->length);
614 RSSVAL(reply->data, 2, 1); /* This is a version 1 reply, MS change/set or otherwise */
615 RSSVAL(reply->data, 4, ap_rep.length);
616 memcpy(reply->data + header_len,
617 ap_rep.data,
618 ap_rep.length);
619 memcpy(reply->data + header_len + ap_rep.length,
620 krb_priv_rep.data,
621 krb_priv_rep.length);
623 talloc_free(tmp_ctx);
624 return KDC_PROCESS_OK;