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 "lib/util/dlinklist.h"
31 #include "lib/ldb/include/ldb.h"
32 #include "heimdal/lib/krb5/krb5_locl.h"
33 #include "heimdal/lib/krb5/krb5-private.h"
34 #include "auth/auth.h"
35 #include "dsdb/samdb/samdb.h"
36 #include "rpc_server/dcerpc_server.h"
37 #include "rpc_server/samr/proto.h"
38 #include "libcli/security/security.h"
40 /* hold information about one kdc socket */
41 struct kpasswd_socket {
42 struct socket_context *sock;
43 struct kdc_server *kdc;
46 /* a queue of outgoing replies that have been deferred */
47 struct kdc_reply *send_queue;
50 /* Return true if there is a valid error packet formed in the error_blob */
51 static BOOL kpasswdd_make_error_reply(struct kdc_server *kdc,
54 const char *error_string,
55 DATA_BLOB *error_blob)
57 char *error_string_utf8;
60 DEBUG(result_code ? 3 : 10, ("kpasswdd: %s\n", error_string));
62 len = push_utf8_talloc(mem_ctx, &error_string_utf8, error_string);
67 *error_blob = data_blob_talloc(mem_ctx, NULL, 2 + len + 1);
68 if (!error_blob->data) {
71 RSSVAL(error_blob->data, 0, result_code);
72 memcpy(error_blob->data + 2, error_string_utf8, len + 1);
76 /* Return true if there is a valid error packet formed in the error_blob */
77 static BOOL kpasswdd_make_unauth_error_reply(struct kdc_server *kdc,
80 const char *error_string,
81 DATA_BLOB *error_blob)
85 DATA_BLOB error_bytes;
86 krb5_data k5_error_bytes, k5_error_blob;
87 ret = kpasswdd_make_error_reply(kdc, mem_ctx, result_code, error_string,
92 k5_error_bytes.data = error_bytes.data;
93 k5_error_bytes.length = error_bytes.length;
94 kret = krb5_mk_error(kdc->smb_krb5_context->krb5_context,
95 result_code, NULL, &k5_error_bytes,
96 NULL, NULL, NULL, NULL, &k5_error_blob);
100 *error_blob = data_blob_talloc(mem_ctx, k5_error_blob.data, k5_error_blob.length);
101 krb5_data_free(&k5_error_blob);
102 if (!error_blob->data) {
108 static BOOL kpasswd_make_pwchange_reply(struct kdc_server *kdc,
111 enum samr_RejectReason reject_reason,
112 struct samr_DomInfo1 *dominfo,
113 DATA_BLOB *error_blob)
115 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
116 return kpasswdd_make_error_reply(kdc, mem_ctx,
117 KRB5_KPASSWD_ACCESSDENIED,
118 "No such user when changing password",
121 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
122 return kpasswdd_make_error_reply(kdc, mem_ctx,
123 KRB5_KPASSWD_ACCESSDENIED,
124 "Not permitted to change password",
127 if (dominfo && NT_STATUS_EQUAL(status, NT_STATUS_PASSWORD_RESTRICTION)) {
128 const char *reject_string;
129 switch (reject_reason) {
130 case SAMR_REJECT_TOO_SHORT:
131 reject_string = talloc_asprintf(mem_ctx, "Password too short, password must be at least %d characters long",
132 dominfo->min_password_length);
134 case SAMR_REJECT_COMPLEXITY:
135 reject_string = "Password does not meet complexity requirements";
137 case SAMR_REJECT_OTHER:
139 reject_string = talloc_asprintf(mem_ctx, "Password must be at least %d characters long, and cannot match any of your %d previous passwords",
140 dominfo->min_password_length, dominfo->password_history_length);
143 return kpasswdd_make_error_reply(kdc, mem_ctx,
144 KRB5_KPASSWD_SOFTERROR,
148 if (!NT_STATUS_IS_OK(status)) {
149 return kpasswdd_make_error_reply(kdc, mem_ctx,
150 KRB5_KPASSWD_HARDERROR,
151 talloc_asprintf(mem_ctx, "failed to set password: %s", nt_errstr(status)),
155 return kpasswdd_make_error_reply(kdc, mem_ctx, KRB5_KPASSWD_SUCCESS,
161 A user password change
163 Return true if there is a valid error packet (or sucess) formed in
166 static BOOL kpasswdd_change_password(struct kdc_server *kdc,
168 struct auth_session_info *session_info,
169 const char *password,
173 enum samr_RejectReason reject_reason;
174 struct samr_DomInfo1 *dominfo;
175 struct ldb_context *samdb;
177 samdb = samdb_connect(mem_ctx, system_session(mem_ctx));
179 return kpasswdd_make_error_reply(kdc, mem_ctx,
180 KRB5_KPASSWD_HARDERROR,
181 "Failed to open samdb",
185 DEBUG(3, ("Changing password of %s\\%s (%s)\n",
186 session_info->server_info->domain_name,
187 session_info->server_info->account_name,
188 dom_sid_string(mem_ctx, session_info->security_token->user_sid)));
190 /* User password change */
191 status = samdb_set_password_sid(samdb, mem_ctx,
192 session_info->security_token->user_sid,
193 password, NULL, NULL,
194 True, /* this is a user password change */
195 True, /* run restriction tests */
198 return kpasswd_make_pwchange_reply(kdc, mem_ctx,
206 static BOOL kpasswd_process_request(struct kdc_server *kdc,
208 struct gensec_security *gensec_security,
213 struct auth_session_info *session_info;
214 if (!NT_STATUS_IS_OK(gensec_session_info(gensec_security,
216 return kpasswdd_make_error_reply(kdc, mem_ctx,
217 KRB5_KPASSWD_HARDERROR,
218 "gensec_session_info failed!",
223 case KRB5_KPASSWD_VERS_CHANGEPW:
225 char *password = talloc_strndup(mem_ctx, (const char *)input->data, input->length);
229 return kpasswdd_change_password(kdc, mem_ctx, session_info,
233 case KRB5_KPASSWD_VERS_SETPW:
236 enum samr_RejectReason reject_reason = SAMR_REJECT_OTHER;
237 struct samr_DomInfo1 *dominfo = NULL;
238 struct ldb_context *samdb;
239 struct ldb_message *msg;
240 krb5_context context = kdc->smb_krb5_context->krb5_context;
242 ChangePasswdDataMS chpw;
245 krb5_principal principal;
246 char *set_password_on_princ;
247 struct ldb_dn *set_password_on_dn;
252 msg = ldb_msg_new(mem_ctx);
257 ret = decode_ChangePasswdDataMS(input->data, input->length,
260 return kpasswdd_make_error_reply(kdc, mem_ctx,
261 KRB5_KPASSWD_MALFORMED,
262 "failed to decode password change structure",
266 password = talloc_strndup(mem_ctx, chpw.newpasswd.data,
267 chpw.newpasswd.length);
269 free_ChangePasswdDataMS(&chpw);
272 if ((chpw.targname && !chpw.targrealm)
273 || (!chpw.targname && chpw.targrealm)) {
274 return kpasswdd_make_error_reply(kdc, mem_ctx,
275 KRB5_KPASSWD_MALFORMED,
276 "Realm and principal must be both present, or neither present",
279 if (chpw.targname && chpw.targrealm) {
280 if (_krb5_principalname2krb5_principal(&principal, *chpw.targname,
281 *chpw.targrealm) != 0) {
282 free_ChangePasswdDataMS(&chpw);
283 return kpasswdd_make_error_reply(kdc, mem_ctx,
284 KRB5_KPASSWD_MALFORMED,
285 "failed to extract principal to set",
290 free_ChangePasswdDataMS(&chpw);
291 return kpasswdd_change_password(kdc, mem_ctx, session_info,
294 free_ChangePasswdDataMS(&chpw);
296 if (krb5_unparse_name(context, principal, &set_password_on_princ) != 0) {
297 krb5_free_principal(context, principal);
298 return kpasswdd_make_error_reply(kdc, mem_ctx,
299 KRB5_KPASSWD_MALFORMED,
300 "krb5_unparse_name failed!",
304 krb5_free_principal(context, principal);
306 samdb = samdb_connect(mem_ctx, session_info);
308 return kpasswdd_make_error_reply(kdc, mem_ctx,
309 KRB5_KPASSWD_HARDERROR,
310 "Unable to open database!",
314 DEBUG(3, ("%s\\%s (%s) is changing password of %s\n",
315 session_info->server_info->domain_name,
316 session_info->server_info->account_name,
317 dom_sid_string(mem_ctx, session_info->security_token->user_sid),
318 set_password_on_princ));
319 ret = ldb_transaction_start(samdb);
321 status = NT_STATUS_TRANSACTION_ABORTED;
322 return kpasswd_make_pwchange_reply(kdc, mem_ctx,
329 status = crack_user_principal_name(samdb, mem_ctx,
330 set_password_on_princ,
331 &set_password_on_dn, NULL);
332 free(set_password_on_princ);
333 if (!NT_STATUS_IS_OK(status)) {
334 ldb_transaction_cancel(samdb);
335 return kpasswd_make_pwchange_reply(kdc, mem_ctx,
342 msg = ldb_msg_new(mem_ctx);
344 ldb_transaction_cancel(samdb);
345 status = NT_STATUS_NO_MEMORY;
347 msg->dn = ldb_dn_copy(msg, set_password_on_dn);
349 status = NT_STATUS_NO_MEMORY;
353 if (NT_STATUS_IS_OK(status)) {
354 /* Admin password set */
355 status = samdb_set_password(samdb, mem_ctx,
356 set_password_on_dn, NULL,
357 msg, password, NULL, NULL,
358 False, /* this is not a user password change */
359 True, /* run restriction tests */
360 &reject_reason, &dominfo);
363 if (NT_STATUS_IS_OK(status)) {
364 /* modify the samdb record */
365 ret = samdb_replace(samdb, mem_ctx, msg);
367 DEBUG(2,("Failed to modify record to set password on %s: %s\n",
368 ldb_dn_linearize(mem_ctx, msg->dn),
369 ldb_errstring(samdb)));
370 status = NT_STATUS_ACCESS_DENIED;
373 if (NT_STATUS_IS_OK(status)) {
374 ret = ldb_transaction_commit(samdb);
376 DEBUG(1,("Failed to commit transaction to set password on %s: %s\n",
377 ldb_dn_linearize(mem_ctx, msg->dn),
378 ldb_errstring(samdb)));
379 status = NT_STATUS_TRANSACTION_ABORTED;
382 ldb_transaction_cancel(samdb);
384 return kpasswd_make_pwchange_reply(kdc, mem_ctx,
391 return kpasswdd_make_error_reply(kdc, mem_ctx,
392 KRB5_KPASSWD_BAD_VERSION,
393 talloc_asprintf(mem_ctx,
394 "Protocol version %u not supported",
401 BOOL kpasswdd_process(struct kdc_server *kdc,
405 struct socket_address *peer_addr,
406 struct socket_address *my_addr)
409 const uint16_t header_len = 6;
412 uint16_t krb_priv_len;
415 DATA_BLOB ap_req, krb_priv_req;
416 DATA_BLOB krb_priv_rep = data_blob(NULL, 0);
417 DATA_BLOB ap_rep = data_blob(NULL, 0);
418 DATA_BLOB kpasswd_req, kpasswd_rep;
419 struct cli_credentials *server_credentials;
420 struct gensec_security *gensec_security;
421 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
427 /* Be parinoid. We need to ensure we don't just let the
428 * caller lead us into a buffer overflow */
429 if (input->length <= header_len) {
430 talloc_free(tmp_ctx);
434 len = RSVAL(input->data, 0);
435 if (input->length != len) {
436 talloc_free(tmp_ctx);
440 /* There are two different versions of this protocol so far,
441 * plus others in the standards pipe. Fortunetly they all
442 * take a very similar framing */
443 version = RSVAL(input->data, 2);
444 ap_req_len = RSVAL(input->data, 4);
445 if ((ap_req_len >= len) || (ap_req_len + header_len) >= len) {
446 talloc_free(tmp_ctx);
450 krb_priv_len = len - ap_req_len;
451 ap_req = data_blob_const(&input->data[header_len], ap_req_len);
452 krb_priv_req = data_blob_const(&input->data[header_len + ap_req_len], krb_priv_len);
454 nt_status = gensec_server_start(tmp_ctx, kdc->task->event_ctx, kdc->task->msg_ctx, &gensec_security);
455 if (!NT_STATUS_IS_OK(nt_status)) {
456 talloc_free(tmp_ctx);
460 server_credentials = cli_credentials_init(tmp_ctx);
461 if (!server_credentials) {
462 DEBUG(1, ("Failed to init server credentials\n"));
466 /* We want the credentials subsystem to use the krb5 context
467 * we already have, rather than a new context */
468 cli_credentials_set_krb5_context(server_credentials, kdc->smb_krb5_context);
469 cli_credentials_set_conf(server_credentials);
470 nt_status = cli_credentials_set_stored_principal(server_credentials, "kadmin/changepw");
471 if (!NT_STATUS_IS_OK(nt_status)) {
472 ret = kpasswdd_make_unauth_error_reply(kdc, mem_ctx,
473 KRB5_KPASSWD_HARDERROR,
474 talloc_asprintf(mem_ctx,
475 "Failed to obtain server credentials for kadmin/changepw: %s\n",
476 nt_errstr(nt_status)),
482 talloc_free(tmp_ctx);
486 nt_status = gensec_set_credentials(gensec_security, server_credentials);
487 if (!NT_STATUS_IS_OK(nt_status)) {
488 talloc_free(tmp_ctx);
492 /* The kerberos PRIV packets include these addresses. MIT
493 * clients check that they are present */
494 nt_status = gensec_set_peer_addr(gensec_security, peer_addr);
495 if (!NT_STATUS_IS_OK(nt_status)) {
496 talloc_free(tmp_ctx);
499 nt_status = gensec_set_my_addr(gensec_security, my_addr);
500 if (!NT_STATUS_IS_OK(nt_status)) {
501 talloc_free(tmp_ctx);
505 /* We want the GENSEC wrap calls to generate PRIV tokens */
506 gensec_want_feature(gensec_security, GENSEC_FEATURE_SEAL);
508 nt_status = gensec_start_mech_by_name(gensec_security, "krb5");
509 if (!NT_STATUS_IS_OK(nt_status)) {
510 talloc_free(tmp_ctx);
514 /* Accept the AP-REQ and generate teh AP-REP we need for the reply */
515 nt_status = gensec_update(gensec_security, tmp_ctx, ap_req, &ap_rep);
516 if (!NT_STATUS_IS_OK(nt_status) && !NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
518 ret = kpasswdd_make_unauth_error_reply(kdc, mem_ctx,
519 KRB5_KPASSWD_HARDERROR,
520 talloc_asprintf(mem_ctx,
521 "gensec_update failed: %s",
522 nt_errstr(nt_status)),
528 talloc_free(tmp_ctx);
532 /* Extract the data from the KRB-PRIV half of the message */
533 nt_status = gensec_unwrap(gensec_security, tmp_ctx, &krb_priv_req, &kpasswd_req);
534 if (!NT_STATUS_IS_OK(nt_status)) {
535 ret = kpasswdd_make_unauth_error_reply(kdc, mem_ctx,
536 KRB5_KPASSWD_HARDERROR,
537 talloc_asprintf(mem_ctx,
538 "gensec_unwrap failed: %s",
539 nt_errstr(nt_status)),
545 talloc_free(tmp_ctx);
549 /* Figure out something to do with it (probably changing a password...) */
550 ret = kpasswd_process_request(kdc, tmp_ctx,
553 &kpasswd_req, &kpasswd_rep);
559 /* And wrap up the reply: This ensures that the error message
560 * or success can be verified by the client */
561 nt_status = gensec_wrap(gensec_security, tmp_ctx,
562 &kpasswd_rep, &krb_priv_rep);
563 if (!NT_STATUS_IS_OK(nt_status)) {
564 ret = kpasswdd_make_unauth_error_reply(kdc, mem_ctx,
565 KRB5_KPASSWD_HARDERROR,
566 talloc_asprintf(mem_ctx,
567 "gensec_wrap failed: %s",
568 nt_errstr(nt_status)),
574 talloc_free(tmp_ctx);
579 *reply = data_blob_talloc(mem_ctx, NULL, krb_priv_rep.length + ap_rep.length + header_len);
584 RSSVAL(reply->data, 0, reply->length);
585 RSSVAL(reply->data, 2, 1); /* This is a version 1 reply, MS change/set or otherwise */
586 RSSVAL(reply->data, 4, ap_rep.length);
587 memcpy(reply->data + header_len,
590 memcpy(reply->data + header_len + ap_rep.length,
592 krb_priv_rep.length);
594 talloc_free(tmp_ctx);