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 2 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, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "smbd/service_task.h"
26 #include "lib/events/events.h"
27 #include "lib/socket/socket.h"
29 #include "system/network.h"
30 #include "dlinklist.h"
31 #include "lib/ldb/include/ldb.h"
32 #include "heimdal/lib/krb5/krb5-private.h"
33 #include "auth/auth.h"
34 #include "dsdb/samdb/samdb.h"
36 /* hold information about one kdc socket */
37 struct kpasswd_socket {
38 struct socket_context *sock;
39 struct kdc_server *kdc;
42 /* a queue of outgoing replies that have been deferred */
43 struct kdc_reply *send_queue;
46 /* Return true if there is a valid error packet formed in the error_blob */
47 static BOOL kpasswdd_make_error_reply(struct kdc_server *kdc,
50 const char *error_string,
51 DATA_BLOB *error_blob)
53 char *error_string_utf8;
56 DEBUG(result_code ? 3 : 10, ("kpasswdd: %s\n", error_string));
58 len = push_utf8_talloc(mem_ctx, &error_string_utf8, error_string);
63 *error_blob = data_blob_talloc(mem_ctx, NULL, 2 + len + 1);
64 if (!error_blob->data) {
67 RSSVAL(error_blob->data, 0, result_code);
68 memcpy(error_blob->data + 2, error_string_utf8, len + 1);
72 /* Return true if there is a valid error packet formed in the error_blob */
73 static BOOL kpasswdd_make_unauth_error_reply(struct kdc_server *kdc,
76 const char *error_string,
77 DATA_BLOB *error_blob)
81 DATA_BLOB error_bytes;
82 krb5_data k5_error_bytes, k5_error_blob;
83 ret = kpasswdd_make_error_reply(kdc, mem_ctx, result_code, error_string,
88 k5_error_bytes.data = error_bytes.data;
89 k5_error_bytes.length = error_bytes.length;
90 kret = krb5_mk_error(kdc->smb_krb5_context->krb5_context,
91 result_code, NULL, &k5_error_bytes,
92 NULL, NULL, NULL, NULL, &k5_error_blob);
96 *error_blob = data_blob_talloc(mem_ctx, k5_error_blob.data, k5_error_blob.length);
97 krb5_data_free(&k5_error_blob);
98 if (!error_blob->data) {
104 static BOOL kpasswd_make_pwchange_reply(struct kdc_server *kdc,
107 enum samr_RejectReason reject_reason,
108 struct samr_DomInfo1 *dominfo,
109 DATA_BLOB *error_blob)
111 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
112 return kpasswdd_make_error_reply(kdc, mem_ctx,
113 KRB5_KPASSWD_ACCESSDENIED,
114 "No such user when changing password",
117 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
118 return kpasswdd_make_error_reply(kdc, mem_ctx,
119 KRB5_KPASSWD_ACCESSDENIED,
120 "Not permitted to change password",
123 if (NT_STATUS_EQUAL(status, NT_STATUS_PASSWORD_RESTRICTION)) {
124 const char *reject_string;
125 switch (reject_reason) {
126 case SAMR_REJECT_TOO_SHORT:
127 reject_string = talloc_asprintf(mem_ctx, "Password too short, password must be at least %d characters long",
128 dominfo->min_password_length);
130 case SAMR_REJECT_COMPLEXITY:
131 reject_string = "Password does not meet complexity requirements";
133 case SAMR_REJECT_OTHER:
135 reject_string = talloc_asprintf(mem_ctx, "Password must be at least %d characters long, and cannot match any of your %d previous passwords",
136 dominfo->min_password_length, dominfo->password_history_length);
139 return kpasswdd_make_error_reply(kdc, mem_ctx,
140 KRB5_KPASSWD_SOFTERROR,
144 if (!NT_STATUS_IS_OK(status)) {
145 return kpasswdd_make_error_reply(kdc, mem_ctx,
146 KRB5_KPASSWD_HARDERROR,
147 talloc_asprintf(mem_ctx, "failed to set password: %s", nt_errstr(status)),
151 return kpasswdd_make_error_reply(kdc, mem_ctx, KRB5_KPASSWD_SUCCESS,
157 A user password change
159 Return true if there is a valid error packet (or sucess) formed in
162 static BOOL kpasswdd_change_password(struct kdc_server *kdc,
164 struct auth_session_info *session_info,
165 const char *password,
169 enum samr_RejectReason reject_reason;
170 struct samr_DomInfo1 *dominfo;
171 struct ldb_context *samdb;
173 samdb = samdb_connect(mem_ctx, system_session(mem_ctx));
175 return kpasswdd_make_error_reply(kdc, mem_ctx,
176 KRB5_KPASSWD_HARDERROR,
177 "Failed to open samdb",
181 DEBUG(3, ("Changing password of %s\\%s (%s)\n",
182 session_info->server_info->domain_name,
183 session_info->server_info->account_name,
184 dom_sid_string(mem_ctx, session_info->security_token->user_sid)));
186 /* User password change */
187 status = samdb_set_password_sid(samdb, mem_ctx,
188 session_info->security_token->user_sid,
189 password, NULL, NULL,
190 True, /* this is a user password change */
191 True, /* run restriction tests */
194 return kpasswd_make_pwchange_reply(kdc, mem_ctx,
202 static BOOL kpasswd_process_request(struct kdc_server *kdc,
204 struct gensec_security *gensec_security,
209 struct auth_session_info *session_info;
210 if (!NT_STATUS_IS_OK(gensec_session_info(gensec_security,
212 return kpasswdd_make_error_reply(kdc, mem_ctx,
213 KRB5_KPASSWD_HARDERROR,
214 "gensec_session_info failed!",
219 case KRB5_KPASSWD_VERS_CHANGEPW:
221 char *password = talloc_strndup(mem_ctx, (const char *)input->data, input->length);
225 return kpasswdd_change_password(kdc, mem_ctx, session_info,
229 case KRB5_KPASSWD_VERS_SETPW:
232 enum samr_RejectReason reject_reason;
233 struct samr_DomInfo1 *dominfo;
234 struct ldb_context *samdb;
235 struct ldb_message *msg;
236 krb5_context context = kdc->smb_krb5_context->krb5_context;
238 ChangePasswdDataMS chpw;
241 krb5_principal principal;
242 char *set_password_on_princ;
243 struct ldb_dn *set_password_on_dn;
248 msg = ldb_msg_new(mem_ctx);
253 ret = decode_ChangePasswdDataMS(input->data, input->length,
256 return kpasswdd_make_error_reply(kdc, mem_ctx,
257 KRB5_KPASSWD_MALFORMED,
258 "failed to decode password change structure",
262 password = talloc_strndup(mem_ctx, chpw.newpasswd.data,
263 chpw.newpasswd.length);
265 free_ChangePasswdDataMS(&chpw);
268 if ((chpw.targname && !chpw.targrealm)
269 || (!chpw.targname && chpw.targrealm)) {
270 return kpasswdd_make_error_reply(kdc, mem_ctx,
271 KRB5_KPASSWD_MALFORMED,
272 "Realm and principal must be both present, or neither present",
275 if (chpw.targname && chpw.targrealm) {
276 if (_krb5_principalname2krb5_principal(&principal, *chpw.targname,
277 *chpw.targrealm) != 0) {
278 free_ChangePasswdDataMS(&chpw);
279 return kpasswdd_make_error_reply(kdc, mem_ctx,
280 KRB5_KPASSWD_MALFORMED,
281 "failed to extract principal to set",
286 free_ChangePasswdDataMS(&chpw);
287 return kpasswdd_change_password(kdc, mem_ctx, session_info,
290 free_ChangePasswdDataMS(&chpw);
292 if (krb5_unparse_name(context, principal, &set_password_on_princ) != 0) {
293 krb5_free_principal(context, principal);
294 return kpasswdd_make_error_reply(kdc, mem_ctx,
295 KRB5_KPASSWD_MALFORMED,
296 "krb5_unparse_name failed!",
300 krb5_free_principal(context, principal);
302 samdb = samdb_connect(mem_ctx, session_info);
304 return kpasswdd_make_error_reply(kdc, mem_ctx,
305 KRB5_KPASSWD_HARDERROR,
306 "Unable to open database!",
310 DEBUG(3, ("%s\\%s (%s) is changing password of %s\n",
311 session_info->server_info->domain_name,
312 session_info->server_info->account_name,
313 dom_sid_string(mem_ctx, session_info->security_token->user_sid),
314 set_password_on_princ));
315 ret = ldb_transaction_start(samdb);
317 status = NT_STATUS_TRANSACTION_ABORTED;
318 return kpasswd_make_pwchange_reply(kdc, mem_ctx,
325 status = crack_user_principal_name(samdb, mem_ctx,
326 set_password_on_princ,
327 &set_password_on_dn, NULL);
328 free(set_password_on_princ);
329 if (!NT_STATUS_IS_OK(status)) {
330 ldb_transaction_cancel(samdb);
331 return kpasswd_make_pwchange_reply(kdc, mem_ctx,
338 msg = ldb_msg_new(mem_ctx);
340 ldb_transaction_cancel(samdb);
341 status = NT_STATUS_NO_MEMORY;
343 msg->dn = ldb_dn_copy(msg, set_password_on_dn);
345 status = NT_STATUS_NO_MEMORY;
349 if (NT_STATUS_IS_OK(status)) {
350 /* Admin password set */
351 status = samdb_set_password(samdb, mem_ctx,
352 set_password_on_dn, NULL,
353 msg, password, NULL, NULL,
354 False, /* this is not a user password change */
355 True, /* run restriction tests */
356 &reject_reason, &dominfo);
359 if (NT_STATUS_IS_OK(status)) {
360 /* modify the samdb record */
361 ret = samdb_replace(samdb, mem_ctx, msg);
363 DEBUG(2,("Failed to modify record to set password on %s: %s\n",
364 ldb_dn_linearize(mem_ctx, msg->dn),
365 ldb_errstring(samdb)));
366 status = NT_STATUS_ACCESS_DENIED;
369 if (NT_STATUS_IS_OK(status)) {
370 ret = ldb_transaction_commit(samdb);
372 DEBUG(1,("Failed to commit transaction to set password on %s: %s\n",
373 ldb_dn_linearize(mem_ctx, msg->dn),
374 ldb_errstring(samdb)));
375 status = NT_STATUS_TRANSACTION_ABORTED;
378 ldb_transaction_cancel(samdb);
380 return kpasswd_make_pwchange_reply(kdc, mem_ctx,
387 return kpasswdd_make_error_reply(kdc, mem_ctx,
388 KRB5_KPASSWD_BAD_VERSION,
389 talloc_asprintf(mem_ctx,
390 "Protocol version %u not supported",
397 BOOL kpasswdd_process(struct kdc_server *kdc,
401 struct socket_address *peer_addr,
402 struct socket_address *my_addr)
405 const uint16_t header_len = 6;
408 uint16_t krb_priv_len;
411 DATA_BLOB ap_req, krb_priv_req, krb_priv_rep, ap_rep;
412 DATA_BLOB kpasswd_req, kpasswd_rep;
413 struct cli_credentials *server_credentials;
414 struct gensec_security *gensec_security;
415 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
421 /* Be parinoid. We need to ensure we don't just let the
422 * caller lead us into a buffer overflow */
423 if (input->length <= header_len) {
424 talloc_free(tmp_ctx);
428 len = RSVAL(input->data, 0);
429 if (input->length != len) {
430 talloc_free(tmp_ctx);
434 /* There are two different versions of this protocol so far,
435 * plus others in the standards pipe. Fortunetly they all
436 * take a very similar framing */
437 version = RSVAL(input->data, 2);
438 ap_req_len = RSVAL(input->data, 4);
439 if ((ap_req_len >= len) || (ap_req_len + header_len) >= len) {
440 talloc_free(tmp_ctx);
444 krb_priv_len = len - ap_req_len;
445 ap_req = data_blob_const(&input->data[header_len], ap_req_len);
446 krb_priv_req = data_blob_const(&input->data[header_len + ap_req_len], krb_priv_len);
448 nt_status = gensec_server_start(tmp_ctx, &gensec_security, kdc->task->event_ctx);
449 if (!NT_STATUS_IS_OK(nt_status)) {
450 talloc_free(tmp_ctx);
455 = cli_credentials_init(tmp_ctx);
456 if (!server_credentials) {
457 DEBUG(1, ("Failed to init server credentials\n"));
461 /* We want the credentials subsystem to use the krb5 context
462 * we already have, rather than a new context */
463 cli_credentials_set_krb5_context(server_credentials, kdc->smb_krb5_context);
464 cli_credentials_set_conf(server_credentials);
465 nt_status = cli_credentials_set_stored_principal(server_credentials, "kadmin/changepw");
466 if (!NT_STATUS_IS_OK(nt_status)) {
467 ret = kpasswdd_make_unauth_error_reply(kdc, mem_ctx,
468 KRB5_KPASSWD_HARDERROR,
469 talloc_asprintf(mem_ctx,
470 "Failed to obtain server credentials for kadmin/changepw: %s\n",
471 nt_errstr(nt_status)),
477 talloc_free(tmp_ctx);
481 nt_status = gensec_set_credentials(gensec_security, server_credentials);
482 if (!NT_STATUS_IS_OK(nt_status)) {
483 talloc_free(tmp_ctx);
487 /* The kerberos PRIV packets include these addresses. MIT
488 * clients check that they are present */
489 nt_status = gensec_set_peer_addr(gensec_security, peer_addr);
490 if (!NT_STATUS_IS_OK(nt_status)) {
491 talloc_free(tmp_ctx);
494 nt_status = gensec_set_my_addr(gensec_security, my_addr);
495 if (!NT_STATUS_IS_OK(nt_status)) {
496 talloc_free(tmp_ctx);
500 /* We want the GENSEC wrap calls to generate PRIV tokens */
501 gensec_want_feature(gensec_security, GENSEC_FEATURE_SEAL);
503 nt_status = gensec_start_mech_by_name(gensec_security, "krb5");
504 if (!NT_STATUS_IS_OK(nt_status)) {
505 talloc_free(tmp_ctx);
509 /* Accept the AP-REQ and generate teh AP-REP we need for the reply */
510 nt_status = gensec_update(gensec_security, tmp_ctx, ap_req, &ap_rep);
511 if (!NT_STATUS_IS_OK(nt_status) && !NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
513 ret = kpasswdd_make_unauth_error_reply(kdc, mem_ctx,
514 KRB5_KPASSWD_HARDERROR,
515 talloc_asprintf(mem_ctx,
516 "gensec_update failed: %s",
517 nt_errstr(nt_status)),
523 talloc_free(tmp_ctx);
527 /* Extract the data from the KRB-PRIV half of the message */
528 nt_status = gensec_unwrap(gensec_security, tmp_ctx, &krb_priv_req, &kpasswd_req);
529 if (!NT_STATUS_IS_OK(nt_status)) {
530 ret = kpasswdd_make_unauth_error_reply(kdc, mem_ctx,
531 KRB5_KPASSWD_HARDERROR,
532 talloc_asprintf(mem_ctx,
533 "gensec_unwrap failed: %s",
534 nt_errstr(nt_status)),
540 talloc_free(tmp_ctx);
544 /* Figure out something to do with it (probably changing a password...) */
545 ret = kpasswd_process_request(kdc, tmp_ctx,
548 &kpasswd_req, &kpasswd_rep);
554 /* And wrap up the reply: This ensures that the error message
555 * or success can be verified by the client */
556 nt_status = gensec_wrap(gensec_security, tmp_ctx,
557 &kpasswd_rep, &krb_priv_rep);
558 if (!NT_STATUS_IS_OK(nt_status)) {
559 ret = kpasswdd_make_unauth_error_reply(kdc, mem_ctx,
560 KRB5_KPASSWD_HARDERROR,
561 talloc_asprintf(mem_ctx,
562 "gensec_wrap failed: %s",
563 nt_errstr(nt_status)),
569 talloc_free(tmp_ctx);
574 *reply = data_blob_talloc(mem_ctx, NULL, krb_priv_rep.length + ap_rep.length + header_len);
579 RSSVAL(reply->data, 0, reply->length);
580 RSSVAL(reply->data, 2, 1); /* This is a version 1 reply, MS change/set or otherwise */
581 RSSVAL(reply->data, 4, ap_rep.length);
582 memcpy(reply->data + header_len,
585 memcpy(reply->data + header_len + ap_rep.length,
587 krb_priv_rep.length);
589 talloc_free(tmp_ctx);