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_IN_HISTORY:
138 reject_string = "Password is already in password history";
140 case SAMR_REJECT_OTHER:
142 reject_string = talloc_asprintf(mem_ctx, "Password must be at least %d characters long, and cannot match any of your %d previous passwords",
143 dominfo->min_password_length, dominfo->password_history_length);
146 return kpasswdd_make_error_reply(kdc, mem_ctx,
147 KRB5_KPASSWD_SOFTERROR,
151 if (!NT_STATUS_IS_OK(status)) {
152 return kpasswdd_make_error_reply(kdc, mem_ctx,
153 KRB5_KPASSWD_HARDERROR,
154 talloc_asprintf(mem_ctx, "failed to set password: %s", nt_errstr(status)),
158 return kpasswdd_make_error_reply(kdc, mem_ctx, KRB5_KPASSWD_SUCCESS,
164 A user password change
166 Return true if there is a valid error packet (or sucess) formed in
169 static BOOL kpasswdd_change_password(struct kdc_server *kdc,
171 struct auth_session_info *session_info,
172 const char *password,
176 enum samr_RejectReason reject_reason;
177 struct samr_DomInfo1 *dominfo;
178 struct ldb_context *samdb;
180 samdb = samdb_connect(mem_ctx, system_session(mem_ctx));
182 return kpasswdd_make_error_reply(kdc, mem_ctx,
183 KRB5_KPASSWD_HARDERROR,
184 "Failed to open samdb",
188 DEBUG(3, ("Changing password of %s\\%s (%s)\n",
189 session_info->server_info->domain_name,
190 session_info->server_info->account_name,
191 dom_sid_string(mem_ctx, session_info->security_token->user_sid)));
193 /* User password change */
194 status = samdb_set_password_sid(samdb, mem_ctx,
195 session_info->security_token->user_sid,
196 password, NULL, NULL,
197 True, /* this is a user password change */
198 True, /* run restriction tests */
201 return kpasswd_make_pwchange_reply(kdc, mem_ctx,
209 static BOOL kpasswd_process_request(struct kdc_server *kdc,
211 struct gensec_security *gensec_security,
216 struct auth_session_info *session_info;
217 if (!NT_STATUS_IS_OK(gensec_session_info(gensec_security,
219 return kpasswdd_make_error_reply(kdc, mem_ctx,
220 KRB5_KPASSWD_HARDERROR,
221 "gensec_session_info failed!",
226 case KRB5_KPASSWD_VERS_CHANGEPW:
228 char *password = talloc_strndup(mem_ctx, (const char *)input->data, input->length);
232 return kpasswdd_change_password(kdc, mem_ctx, session_info,
236 case KRB5_KPASSWD_VERS_SETPW:
239 enum samr_RejectReason reject_reason = SAMR_REJECT_OTHER;
240 struct samr_DomInfo1 *dominfo = NULL;
241 struct ldb_context *samdb;
242 struct ldb_message *msg;
243 krb5_context context = kdc->smb_krb5_context->krb5_context;
245 ChangePasswdDataMS chpw;
248 krb5_principal principal;
249 char *set_password_on_princ;
250 struct ldb_dn *set_password_on_dn;
255 msg = ldb_msg_new(mem_ctx);
260 ret = decode_ChangePasswdDataMS(input->data, input->length,
263 return kpasswdd_make_error_reply(kdc, mem_ctx,
264 KRB5_KPASSWD_MALFORMED,
265 "failed to decode password change structure",
269 password = talloc_strndup(mem_ctx, chpw.newpasswd.data,
270 chpw.newpasswd.length);
272 free_ChangePasswdDataMS(&chpw);
275 if ((chpw.targname && !chpw.targrealm)
276 || (!chpw.targname && chpw.targrealm)) {
277 return kpasswdd_make_error_reply(kdc, mem_ctx,
278 KRB5_KPASSWD_MALFORMED,
279 "Realm and principal must be both present, or neither present",
282 if (chpw.targname && chpw.targrealm) {
283 if (_krb5_principalname2krb5_principal(&principal, *chpw.targname,
284 *chpw.targrealm) != 0) {
285 free_ChangePasswdDataMS(&chpw);
286 return kpasswdd_make_error_reply(kdc, mem_ctx,
287 KRB5_KPASSWD_MALFORMED,
288 "failed to extract principal to set",
293 free_ChangePasswdDataMS(&chpw);
294 return kpasswdd_change_password(kdc, mem_ctx, session_info,
297 free_ChangePasswdDataMS(&chpw);
299 if (krb5_unparse_name(context, principal, &set_password_on_princ) != 0) {
300 krb5_free_principal(context, principal);
301 return kpasswdd_make_error_reply(kdc, mem_ctx,
302 KRB5_KPASSWD_MALFORMED,
303 "krb5_unparse_name failed!",
307 krb5_free_principal(context, principal);
309 samdb = samdb_connect(mem_ctx, session_info);
311 return kpasswdd_make_error_reply(kdc, mem_ctx,
312 KRB5_KPASSWD_HARDERROR,
313 "Unable to open database!",
317 DEBUG(3, ("%s\\%s (%s) is changing password of %s\n",
318 session_info->server_info->domain_name,
319 session_info->server_info->account_name,
320 dom_sid_string(mem_ctx, session_info->security_token->user_sid),
321 set_password_on_princ));
322 ret = ldb_transaction_start(samdb);
324 status = NT_STATUS_TRANSACTION_ABORTED;
325 return kpasswd_make_pwchange_reply(kdc, mem_ctx,
332 status = crack_user_principal_name(samdb, mem_ctx,
333 set_password_on_princ,
334 &set_password_on_dn, NULL);
335 free(set_password_on_princ);
336 if (!NT_STATUS_IS_OK(status)) {
337 ldb_transaction_cancel(samdb);
338 return kpasswd_make_pwchange_reply(kdc, mem_ctx,
345 msg = ldb_msg_new(mem_ctx);
347 ldb_transaction_cancel(samdb);
348 status = NT_STATUS_NO_MEMORY;
350 msg->dn = ldb_dn_copy(msg, set_password_on_dn);
352 status = NT_STATUS_NO_MEMORY;
356 if (NT_STATUS_IS_OK(status)) {
357 /* Admin password set */
358 status = samdb_set_password(samdb, mem_ctx,
359 set_password_on_dn, NULL,
360 msg, password, NULL, NULL,
361 False, /* this is not a user password change */
362 True, /* run restriction tests */
363 &reject_reason, &dominfo);
366 if (NT_STATUS_IS_OK(status)) {
367 /* modify the samdb record */
368 ret = samdb_replace(samdb, mem_ctx, msg);
370 DEBUG(2,("Failed to modify record to set password on %s: %s\n",
371 ldb_dn_linearize(mem_ctx, msg->dn),
372 ldb_errstring(samdb)));
373 status = NT_STATUS_ACCESS_DENIED;
376 if (NT_STATUS_IS_OK(status)) {
377 ret = ldb_transaction_commit(samdb);
379 DEBUG(1,("Failed to commit transaction to set password on %s: %s\n",
380 ldb_dn_linearize(mem_ctx, msg->dn),
381 ldb_errstring(samdb)));
382 status = NT_STATUS_TRANSACTION_ABORTED;
385 ldb_transaction_cancel(samdb);
387 return kpasswd_make_pwchange_reply(kdc, mem_ctx,
394 return kpasswdd_make_error_reply(kdc, mem_ctx,
395 KRB5_KPASSWD_BAD_VERSION,
396 talloc_asprintf(mem_ctx,
397 "Protocol version %u not supported",
404 BOOL kpasswdd_process(struct kdc_server *kdc,
408 struct socket_address *peer_addr,
409 struct socket_address *my_addr)
412 const uint16_t header_len = 6;
415 uint16_t krb_priv_len;
418 DATA_BLOB ap_req, krb_priv_req;
419 DATA_BLOB krb_priv_rep = data_blob(NULL, 0);
420 DATA_BLOB ap_rep = data_blob(NULL, 0);
421 DATA_BLOB kpasswd_req, kpasswd_rep;
422 struct cli_credentials *server_credentials;
423 struct gensec_security *gensec_security;
424 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
430 /* Be parinoid. We need to ensure we don't just let the
431 * caller lead us into a buffer overflow */
432 if (input->length <= header_len) {
433 talloc_free(tmp_ctx);
437 len = RSVAL(input->data, 0);
438 if (input->length != len) {
439 talloc_free(tmp_ctx);
443 /* There are two different versions of this protocol so far,
444 * plus others in the standards pipe. Fortunetly they all
445 * take a very similar framing */
446 version = RSVAL(input->data, 2);
447 ap_req_len = RSVAL(input->data, 4);
448 if ((ap_req_len >= len) || (ap_req_len + header_len) >= len) {
449 talloc_free(tmp_ctx);
453 krb_priv_len = len - ap_req_len;
454 ap_req = data_blob_const(&input->data[header_len], ap_req_len);
455 krb_priv_req = data_blob_const(&input->data[header_len + ap_req_len], krb_priv_len);
457 nt_status = gensec_server_start(tmp_ctx, kdc->task->event_ctx, kdc->task->msg_ctx, &gensec_security);
458 if (!NT_STATUS_IS_OK(nt_status)) {
459 talloc_free(tmp_ctx);
463 server_credentials = cli_credentials_init(tmp_ctx);
464 if (!server_credentials) {
465 DEBUG(1, ("Failed to init server credentials\n"));
469 /* We want the credentials subsystem to use the krb5 context
470 * we already have, rather than a new context */
471 cli_credentials_set_krb5_context(server_credentials, kdc->smb_krb5_context);
472 cli_credentials_set_conf(server_credentials);
473 nt_status = cli_credentials_set_stored_principal(server_credentials, "kadmin/changepw");
474 if (!NT_STATUS_IS_OK(nt_status)) {
475 ret = kpasswdd_make_unauth_error_reply(kdc, mem_ctx,
476 KRB5_KPASSWD_HARDERROR,
477 talloc_asprintf(mem_ctx,
478 "Failed to obtain server credentials for kadmin/changepw: %s\n",
479 nt_errstr(nt_status)),
485 talloc_free(tmp_ctx);
489 nt_status = gensec_set_credentials(gensec_security, server_credentials);
490 if (!NT_STATUS_IS_OK(nt_status)) {
491 talloc_free(tmp_ctx);
495 /* The kerberos PRIV packets include these addresses. MIT
496 * clients check that they are present */
497 nt_status = gensec_set_peer_addr(gensec_security, peer_addr);
498 if (!NT_STATUS_IS_OK(nt_status)) {
499 talloc_free(tmp_ctx);
502 nt_status = gensec_set_my_addr(gensec_security, my_addr);
503 if (!NT_STATUS_IS_OK(nt_status)) {
504 talloc_free(tmp_ctx);
508 /* We want the GENSEC wrap calls to generate PRIV tokens */
509 gensec_want_feature(gensec_security, GENSEC_FEATURE_SEAL);
511 nt_status = gensec_start_mech_by_name(gensec_security, "krb5");
512 if (!NT_STATUS_IS_OK(nt_status)) {
513 talloc_free(tmp_ctx);
517 /* Accept the AP-REQ and generate teh AP-REP we need for the reply */
518 nt_status = gensec_update(gensec_security, tmp_ctx, ap_req, &ap_rep);
519 if (!NT_STATUS_IS_OK(nt_status) && !NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
521 ret = kpasswdd_make_unauth_error_reply(kdc, mem_ctx,
522 KRB5_KPASSWD_HARDERROR,
523 talloc_asprintf(mem_ctx,
524 "gensec_update failed: %s",
525 nt_errstr(nt_status)),
531 talloc_free(tmp_ctx);
535 /* Extract the data from the KRB-PRIV half of the message */
536 nt_status = gensec_unwrap(gensec_security, tmp_ctx, &krb_priv_req, &kpasswd_req);
537 if (!NT_STATUS_IS_OK(nt_status)) {
538 ret = kpasswdd_make_unauth_error_reply(kdc, mem_ctx,
539 KRB5_KPASSWD_HARDERROR,
540 talloc_asprintf(mem_ctx,
541 "gensec_unwrap failed: %s",
542 nt_errstr(nt_status)),
548 talloc_free(tmp_ctx);
552 /* Figure out something to do with it (probably changing a password...) */
553 ret = kpasswd_process_request(kdc, tmp_ctx,
556 &kpasswd_req, &kpasswd_rep);
562 /* And wrap up the reply: This ensures that the error message
563 * or success can be verified by the client */
564 nt_status = gensec_wrap(gensec_security, tmp_ctx,
565 &kpasswd_rep, &krb_priv_rep);
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_wrap failed: %s",
571 nt_errstr(nt_status)),
577 talloc_free(tmp_ctx);
582 *reply = data_blob_talloc(mem_ctx, NULL, krb_priv_rep.length + ap_rep.length + header_len);
587 RSSVAL(reply->data, 0, reply->length);
588 RSSVAL(reply->data, 2, 1); /* This is a version 1 reply, MS change/set or otherwise */
589 RSSVAL(reply->data, 4, ap_rep.length);
590 memcpy(reply->data + header_len,
593 memcpy(reply->data + header_len + ap_rep.length,
595 krb_priv_rep.length);
597 talloc_free(tmp_ctx);