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/>.
24 #include "smbd/service_task.h"
25 #include "lib/events/events.h"
26 #include "lib/socket/socket.h"
27 #include "system/network.h"
28 #include "../lib/util/dlinklist.h"
29 #include "lib/ldb/include/ldb.h"
30 #include "auth/gensec/gensec.h"
31 #include "auth/credentials/credentials.h"
32 #include "auth/credentials/credentials_krb5.h"
33 #include "auth/auth.h"
34 #include "dsdb/samdb/samdb.h"
35 #include "rpc_server/dcerpc_server.h"
36 #include "rpc_server/samr/proto.h"
37 #include "libcli/security/security.h"
38 #include "param/param.h"
41 /* TODO: remove all SAMBA4_INTERNAL_HEIMDAL stuff from this file */
42 #ifdef SAMBA4_INTERNAL_HEIMDAL
43 #include "heimdal_build/kpasswdd-glue.h"
46 /* hold information about one kdc socket */
47 struct kpasswd_socket {
48 struct socket_context *sock;
49 struct kdc_server *kdc;
50 struct tevent_fd *fde;
52 /* a queue of outgoing replies that have been deferred */
53 struct kdc_reply *send_queue;
56 /* Return true if there is a valid error packet formed in the error_blob */
57 static bool kpasswdd_make_error_reply(struct kdc_server *kdc,
60 const char *error_string,
61 DATA_BLOB *error_blob)
63 char *error_string_utf8;
66 DEBUG(result_code ? 3 : 10, ("kpasswdd: %s\n", error_string));
68 len = push_utf8_talloc(mem_ctx, &error_string_utf8, error_string);
73 *error_blob = data_blob_talloc(mem_ctx, NULL, 2 + len + 1);
74 if (!error_blob->data) {
77 RSSVAL(error_blob->data, 0, result_code);
78 memcpy(error_blob->data + 2, error_string_utf8, len + 1);
82 /* Return true if there is a valid error packet formed in the error_blob */
83 static bool kpasswdd_make_unauth_error_reply(struct kdc_server *kdc,
86 const char *error_string,
87 DATA_BLOB *error_blob)
91 DATA_BLOB error_bytes;
92 krb5_data k5_error_bytes, k5_error_blob;
93 ret = kpasswdd_make_error_reply(kdc, mem_ctx, result_code, error_string,
98 k5_error_bytes.data = error_bytes.data;
99 k5_error_bytes.length = error_bytes.length;
100 kret = krb5_mk_error(kdc->smb_krb5_context->krb5_context,
101 result_code, NULL, &k5_error_bytes,
102 NULL, NULL, NULL, NULL, &k5_error_blob);
106 *error_blob = data_blob_talloc(mem_ctx, k5_error_blob.data, k5_error_blob.length);
107 krb5_data_free(&k5_error_blob);
108 if (!error_blob->data) {
114 static bool kpasswd_make_pwchange_reply(struct kdc_server *kdc,
117 enum samr_RejectReason reject_reason,
118 struct samr_DomInfo1 *dominfo,
119 DATA_BLOB *error_blob)
121 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
122 return kpasswdd_make_error_reply(kdc, mem_ctx,
123 KRB5_KPASSWD_ACCESSDENIED,
124 "No such user when changing password",
127 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
128 return kpasswdd_make_error_reply(kdc, mem_ctx,
129 KRB5_KPASSWD_ACCESSDENIED,
130 "Not permitted to change password",
133 if (dominfo && NT_STATUS_EQUAL(status, NT_STATUS_PASSWORD_RESTRICTION)) {
134 const char *reject_string;
135 switch (reject_reason) {
136 case SAMR_REJECT_TOO_SHORT:
137 reject_string = talloc_asprintf(mem_ctx, "Password too short, password must be at least %d characters long",
138 dominfo->min_password_length);
140 case SAMR_REJECT_COMPLEXITY:
141 reject_string = "Password does not meet complexity requirements";
143 case SAMR_REJECT_IN_HISTORY:
144 reject_string = "Password is already in password history";
146 case SAMR_REJECT_OTHER:
148 reject_string = talloc_asprintf(mem_ctx, "Password must be at least %d characters long, and cannot match any of your %d previous passwords",
149 dominfo->min_password_length, dominfo->password_history_length);
152 return kpasswdd_make_error_reply(kdc, mem_ctx,
153 KRB5_KPASSWD_SOFTERROR,
157 if (!NT_STATUS_IS_OK(status)) {
158 return kpasswdd_make_error_reply(kdc, mem_ctx,
159 KRB5_KPASSWD_HARDERROR,
160 talloc_asprintf(mem_ctx, "failed to set password: %s", nt_errstr(status)),
164 return kpasswdd_make_error_reply(kdc, mem_ctx, KRB5_KPASSWD_SUCCESS,
170 A user password change
172 Return true if there is a valid error packet (or sucess) formed in
175 static bool kpasswdd_change_password(struct kdc_server *kdc,
177 struct auth_session_info *session_info,
178 const DATA_BLOB *password,
182 enum samr_RejectReason reject_reason;
183 struct samr_DomInfo1 *dominfo;
184 struct ldb_context *samdb;
186 samdb = samdb_connect(mem_ctx, kdc->task->event_ctx, kdc->task->lp_ctx, system_session(mem_ctx, kdc->task->lp_ctx));
188 return kpasswdd_make_error_reply(kdc, mem_ctx,
189 KRB5_KPASSWD_HARDERROR,
190 "Failed to open samdb",
194 DEBUG(3, ("Changing password of %s\\%s (%s)\n",
195 session_info->server_info->domain_name,
196 session_info->server_info->account_name,
197 dom_sid_string(mem_ctx, session_info->security_token->user_sid)));
199 /* User password change */
200 status = samdb_set_password_sid(samdb, mem_ctx,
201 session_info->security_token->user_sid,
202 password, NULL, NULL,
203 true, /* this is a user password change */
206 return kpasswd_make_pwchange_reply(kdc, mem_ctx,
214 static bool kpasswd_process_request(struct kdc_server *kdc,
216 struct gensec_security *gensec_security,
221 struct auth_session_info *session_info;
224 if (!NT_STATUS_IS_OK(gensec_session_info(gensec_security,
226 return kpasswdd_make_error_reply(kdc, mem_ctx,
227 KRB5_KPASSWD_HARDERROR,
228 "gensec_session_info failed!",
233 case KRB5_KPASSWD_VERS_CHANGEPW:
236 if (!convert_string_talloc_convenience(mem_ctx, lp_iconv_convenience(kdc->task->lp_ctx),
238 (const char *)input->data,
240 (void **)&password.data, &pw_len, false)) {
243 password.length = pw_len;
245 return kpasswdd_change_password(kdc, mem_ctx, session_info,
249 case KRB5_KPASSWD_VERS_SETPW:
252 enum samr_RejectReason reject_reason = SAMR_REJECT_OTHER;
253 struct samr_DomInfo1 *dominfo = NULL;
254 struct ldb_context *samdb;
255 struct ldb_message *msg;
256 krb5_context context = kdc->smb_krb5_context->krb5_context;
258 ChangePasswdDataMS chpw;
261 krb5_principal principal;
262 char *set_password_on_princ;
263 struct ldb_dn *set_password_on_dn;
268 msg = ldb_msg_new(mem_ctx);
273 ret = decode_ChangePasswdDataMS(input->data, input->length,
276 return kpasswdd_make_error_reply(kdc, mem_ctx,
277 KRB5_KPASSWD_MALFORMED,
278 "failed to decode password change structure",
282 if (!convert_string_talloc_convenience(mem_ctx, lp_iconv_convenience(kdc->task->lp_ctx),
284 (const char *)chpw.newpasswd.data,
285 chpw.newpasswd.length,
286 (void **)&password.data, &pw_len, false)) {
287 free_ChangePasswdDataMS(&chpw);
291 password.length = pw_len;
293 if ((chpw.targname && !chpw.targrealm)
294 || (!chpw.targname && chpw.targrealm)) {
295 return kpasswdd_make_error_reply(kdc, mem_ctx,
296 KRB5_KPASSWD_MALFORMED,
297 "Realm and principal must be both present, or neither present",
300 if (chpw.targname && chpw.targrealm) {
301 #ifdef SAMBA4_INTERNAL_HEIMDAL
302 if (_krb5_principalname2krb5_principal(kdc->smb_krb5_context->krb5_context,
303 &principal, *chpw.targname,
304 *chpw.targrealm) != 0) {
305 free_ChangePasswdDataMS(&chpw);
306 return kpasswdd_make_error_reply(kdc, mem_ctx,
307 KRB5_KPASSWD_MALFORMED,
308 "failed to extract principal to set",
312 #else /* SAMBA4_INTERNAL_HEIMDAL */
313 return kpasswdd_make_error_reply(kdc, mem_ctx,
314 KRB5_KPASSWD_BAD_VERSION,
315 "Operation Not Implemented",
317 #endif /* SAMBA4_INTERNAL_HEIMDAL */
319 free_ChangePasswdDataMS(&chpw);
320 return kpasswdd_change_password(kdc, mem_ctx, session_info,
323 free_ChangePasswdDataMS(&chpw);
325 if (krb5_unparse_name(context, principal, &set_password_on_princ) != 0) {
326 krb5_free_principal(context, principal);
327 return kpasswdd_make_error_reply(kdc, mem_ctx,
328 KRB5_KPASSWD_MALFORMED,
329 "krb5_unparse_name failed!",
333 krb5_free_principal(context, principal);
335 samdb = samdb_connect(mem_ctx, kdc->task->event_ctx, kdc->task->lp_ctx, session_info);
337 return kpasswdd_make_error_reply(kdc, mem_ctx,
338 KRB5_KPASSWD_HARDERROR,
339 "Unable to open database!",
343 DEBUG(3, ("%s\\%s (%s) is changing password of %s\n",
344 session_info->server_info->domain_name,
345 session_info->server_info->account_name,
346 dom_sid_string(mem_ctx, session_info->security_token->user_sid),
347 set_password_on_princ));
348 ret = ldb_transaction_start(samdb);
350 status = NT_STATUS_TRANSACTION_ABORTED;
351 return kpasswd_make_pwchange_reply(kdc, mem_ctx,
358 status = crack_user_principal_name(samdb, mem_ctx,
359 set_password_on_princ,
360 &set_password_on_dn, NULL);
361 free(set_password_on_princ);
362 if (!NT_STATUS_IS_OK(status)) {
363 ldb_transaction_cancel(samdb);
364 return kpasswd_make_pwchange_reply(kdc, mem_ctx,
371 msg = ldb_msg_new(mem_ctx);
373 ldb_transaction_cancel(samdb);
374 status = NT_STATUS_NO_MEMORY;
376 msg->dn = ldb_dn_copy(msg, set_password_on_dn);
378 status = NT_STATUS_NO_MEMORY;
382 if (NT_STATUS_IS_OK(status)) {
383 /* Admin password set */
384 status = samdb_set_password(samdb, mem_ctx,
385 set_password_on_dn, NULL,
386 msg, &password, NULL, NULL,
387 false, /* this is not a user password change */
388 &reject_reason, &dominfo);
391 if (NT_STATUS_IS_OK(status)) {
392 /* modify the samdb record */
393 ret = samdb_replace(samdb, mem_ctx, msg);
395 DEBUG(2,("Failed to modify record to set password on %s: %s\n",
396 ldb_dn_get_linearized(msg->dn),
397 ldb_errstring(samdb)));
398 status = NT_STATUS_ACCESS_DENIED;
401 if (NT_STATUS_IS_OK(status)) {
402 ret = ldb_transaction_commit(samdb);
404 DEBUG(1,("Failed to commit transaction to set password on %s: %s\n",
405 ldb_dn_get_linearized(msg->dn),
406 ldb_errstring(samdb)));
407 status = NT_STATUS_TRANSACTION_ABORTED;
410 ldb_transaction_cancel(samdb);
412 return kpasswd_make_pwchange_reply(kdc, mem_ctx,
419 return kpasswdd_make_error_reply(kdc, mem_ctx,
420 KRB5_KPASSWD_BAD_VERSION,
421 talloc_asprintf(mem_ctx,
422 "Protocol version %u not supported",
429 bool kpasswdd_process(struct kdc_server *kdc,
433 struct socket_address *peer_addr,
434 struct socket_address *my_addr,
438 const uint16_t header_len = 6;
441 uint16_t krb_priv_len;
444 DATA_BLOB ap_req, krb_priv_req;
445 DATA_BLOB krb_priv_rep = data_blob(NULL, 0);
446 DATA_BLOB ap_rep = data_blob(NULL, 0);
447 DATA_BLOB kpasswd_req, kpasswd_rep;
448 struct cli_credentials *server_credentials;
449 struct gensec_security *gensec_security;
450 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
456 /* Be parinoid. We need to ensure we don't just let the
457 * caller lead us into a buffer overflow */
458 if (input->length <= header_len) {
459 talloc_free(tmp_ctx);
463 len = RSVAL(input->data, 0);
464 if (input->length != len) {
465 talloc_free(tmp_ctx);
469 /* There are two different versions of this protocol so far,
470 * plus others in the standards pipe. Fortunetly they all
471 * take a very similar framing */
472 version = RSVAL(input->data, 2);
473 ap_req_len = RSVAL(input->data, 4);
474 if ((ap_req_len >= len) || (ap_req_len + header_len) >= len) {
475 talloc_free(tmp_ctx);
479 krb_priv_len = len - ap_req_len;
480 ap_req = data_blob_const(&input->data[header_len], ap_req_len);
481 krb_priv_req = data_blob_const(&input->data[header_len + ap_req_len], krb_priv_len);
483 server_credentials = cli_credentials_init(tmp_ctx);
484 if (!server_credentials) {
485 DEBUG(1, ("Failed to init server credentials\n"));
489 /* We want the credentials subsystem to use the krb5 context
490 * we already have, rather than a new context */
491 cli_credentials_set_krb5_context(server_credentials, kdc->smb_krb5_context);
492 cli_credentials_set_conf(server_credentials, kdc->task->lp_ctx);
493 nt_status = cli_credentials_set_stored_principal(server_credentials, kdc->task->event_ctx, kdc->task->lp_ctx, "kadmin/changepw");
494 if (!NT_STATUS_IS_OK(nt_status)) {
495 ret = kpasswdd_make_unauth_error_reply(kdc, mem_ctx,
496 KRB5_KPASSWD_HARDERROR,
497 talloc_asprintf(mem_ctx,
498 "Failed to obtain server credentials for kadmin/changepw: %s\n",
499 nt_errstr(nt_status)),
505 talloc_free(tmp_ctx);
509 /* We don't strictly need to call this wrapper, and could call
510 * gensec_server_start directly, as we have no need for NTLM
511 * and we have a PAC, but this ensures that the wrapper can be
512 * safely extended for other helpful things in future */
513 nt_status = samba_server_gensec_start(tmp_ctx, kdc->task->event_ctx,
519 if (!NT_STATUS_IS_OK(nt_status)) {
520 talloc_free(tmp_ctx);
524 /* The kerberos PRIV packets include these addresses. MIT
525 * clients check that they are present */
526 nt_status = gensec_set_peer_addr(gensec_security, peer_addr);
527 if (!NT_STATUS_IS_OK(nt_status)) {
528 talloc_free(tmp_ctx);
531 nt_status = gensec_set_my_addr(gensec_security, my_addr);
532 if (!NT_STATUS_IS_OK(nt_status)) {
533 talloc_free(tmp_ctx);
537 /* We want the GENSEC wrap calls to generate PRIV tokens */
538 gensec_want_feature(gensec_security, GENSEC_FEATURE_SEAL);
540 nt_status = gensec_start_mech_by_name(gensec_security, "krb5");
541 if (!NT_STATUS_IS_OK(nt_status)) {
542 talloc_free(tmp_ctx);
546 /* Accept the AP-REQ and generate teh AP-REP we need for the reply */
547 nt_status = gensec_update(gensec_security, tmp_ctx, ap_req, &ap_rep);
548 if (!NT_STATUS_IS_OK(nt_status) && !NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
550 ret = kpasswdd_make_unauth_error_reply(kdc, mem_ctx,
551 KRB5_KPASSWD_HARDERROR,
552 talloc_asprintf(mem_ctx,
553 "gensec_update failed: %s",
554 nt_errstr(nt_status)),
560 talloc_free(tmp_ctx);
564 /* Extract the data from the KRB-PRIV half of the message */
565 nt_status = gensec_unwrap(gensec_security, tmp_ctx, &krb_priv_req, &kpasswd_req);
566 if (!NT_STATUS_IS_OK(nt_status)) {
567 ret = kpasswdd_make_unauth_error_reply(kdc, mem_ctx,
568 KRB5_KPASSWD_HARDERROR,
569 talloc_asprintf(mem_ctx,
570 "gensec_unwrap failed: %s",
571 nt_errstr(nt_status)),
577 talloc_free(tmp_ctx);
581 /* Figure out something to do with it (probably changing a password...) */
582 ret = kpasswd_process_request(kdc, tmp_ctx,
585 &kpasswd_req, &kpasswd_rep);
591 /* And wrap up the reply: This ensures that the error message
592 * or success can be verified by the client */
593 nt_status = gensec_wrap(gensec_security, tmp_ctx,
594 &kpasswd_rep, &krb_priv_rep);
595 if (!NT_STATUS_IS_OK(nt_status)) {
596 ret = kpasswdd_make_unauth_error_reply(kdc, mem_ctx,
597 KRB5_KPASSWD_HARDERROR,
598 talloc_asprintf(mem_ctx,
599 "gensec_wrap failed: %s",
600 nt_errstr(nt_status)),
606 talloc_free(tmp_ctx);
611 *reply = data_blob_talloc(mem_ctx, NULL, krb_priv_rep.length + ap_rep.length + header_len);
616 RSSVAL(reply->data, 0, reply->length);
617 RSSVAL(reply->data, 2, 1); /* This is a version 1 reply, MS change/set or otherwise */
618 RSSVAL(reply->data, 4, ap_rep.length);
619 memcpy(reply->data + header_len,
622 memcpy(reply->data + header_len + ap_rep.length,
624 krb_priv_rep.length);
626 talloc_free(tmp_ctx);