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/gensec/gensec.h"
35 #include "auth/credentials/credentials.h"
36 #include "auth/credentials/credentials_krb5.h"
37 #include "auth/auth.h"
38 #include "dsdb/samdb/samdb.h"
39 #include "rpc_server/dcerpc_server.h"
40 #include "rpc_server/samr/proto.h"
41 #include "libcli/security/security.h"
43 /* hold information about one kdc socket */
44 struct kpasswd_socket {
45 struct socket_context *sock;
46 struct kdc_server *kdc;
49 /* a queue of outgoing replies that have been deferred */
50 struct kdc_reply *send_queue;
53 /* Return true if there is a valid error packet formed in the error_blob */
54 static BOOL kpasswdd_make_error_reply(struct kdc_server *kdc,
57 const char *error_string,
58 DATA_BLOB *error_blob)
60 char *error_string_utf8;
63 DEBUG(result_code ? 3 : 10, ("kpasswdd: %s\n", error_string));
65 len = push_utf8_talloc(mem_ctx, &error_string_utf8, error_string);
70 *error_blob = data_blob_talloc(mem_ctx, NULL, 2 + len + 1);
71 if (!error_blob->data) {
74 RSSVAL(error_blob->data, 0, result_code);
75 memcpy(error_blob->data + 2, error_string_utf8, len + 1);
79 /* Return true if there is a valid error packet formed in the error_blob */
80 static BOOL kpasswdd_make_unauth_error_reply(struct kdc_server *kdc,
83 const char *error_string,
84 DATA_BLOB *error_blob)
88 DATA_BLOB error_bytes;
89 krb5_data k5_error_bytes, k5_error_blob;
90 ret = kpasswdd_make_error_reply(kdc, mem_ctx, result_code, error_string,
95 k5_error_bytes.data = error_bytes.data;
96 k5_error_bytes.length = error_bytes.length;
97 kret = krb5_mk_error(kdc->smb_krb5_context->krb5_context,
98 result_code, NULL, &k5_error_bytes,
99 NULL, NULL, NULL, NULL, &k5_error_blob);
103 *error_blob = data_blob_talloc(mem_ctx, k5_error_blob.data, k5_error_blob.length);
104 krb5_data_free(&k5_error_blob);
105 if (!error_blob->data) {
111 static BOOL kpasswd_make_pwchange_reply(struct kdc_server *kdc,
114 enum samr_RejectReason reject_reason,
115 struct samr_DomInfo1 *dominfo,
116 DATA_BLOB *error_blob)
118 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_USER)) {
119 return kpasswdd_make_error_reply(kdc, mem_ctx,
120 KRB5_KPASSWD_ACCESSDENIED,
121 "No such user when changing password",
124 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
125 return kpasswdd_make_error_reply(kdc, mem_ctx,
126 KRB5_KPASSWD_ACCESSDENIED,
127 "Not permitted to change password",
130 if (dominfo && NT_STATUS_EQUAL(status, NT_STATUS_PASSWORD_RESTRICTION)) {
131 const char *reject_string;
132 switch (reject_reason) {
133 case SAMR_REJECT_TOO_SHORT:
134 reject_string = talloc_asprintf(mem_ctx, "Password too short, password must be at least %d characters long",
135 dominfo->min_password_length);
137 case SAMR_REJECT_COMPLEXITY:
138 reject_string = "Password does not meet complexity requirements";
140 case SAMR_REJECT_IN_HISTORY:
141 reject_string = "Password is already in password history";
143 case SAMR_REJECT_OTHER:
145 reject_string = talloc_asprintf(mem_ctx, "Password must be at least %d characters long, and cannot match any of your %d previous passwords",
146 dominfo->min_password_length, dominfo->password_history_length);
149 return kpasswdd_make_error_reply(kdc, mem_ctx,
150 KRB5_KPASSWD_SOFTERROR,
154 if (!NT_STATUS_IS_OK(status)) {
155 return kpasswdd_make_error_reply(kdc, mem_ctx,
156 KRB5_KPASSWD_HARDERROR,
157 talloc_asprintf(mem_ctx, "failed to set password: %s", nt_errstr(status)),
161 return kpasswdd_make_error_reply(kdc, mem_ctx, KRB5_KPASSWD_SUCCESS,
167 A user password change
169 Return true if there is a valid error packet (or sucess) formed in
172 static BOOL kpasswdd_change_password(struct kdc_server *kdc,
174 struct auth_session_info *session_info,
175 const char *password,
179 enum samr_RejectReason reject_reason;
180 struct samr_DomInfo1 *dominfo;
181 struct ldb_context *samdb;
183 samdb = samdb_connect(mem_ctx, system_session(mem_ctx));
185 return kpasswdd_make_error_reply(kdc, mem_ctx,
186 KRB5_KPASSWD_HARDERROR,
187 "Failed to open samdb",
191 DEBUG(3, ("Changing password of %s\\%s (%s)\n",
192 session_info->server_info->domain_name,
193 session_info->server_info->account_name,
194 dom_sid_string(mem_ctx, session_info->security_token->user_sid)));
196 /* User password change */
197 status = samdb_set_password_sid(samdb, mem_ctx,
198 session_info->security_token->user_sid,
199 password, NULL, NULL,
200 True, /* this is a user password change */
201 True, /* run restriction tests */
204 return kpasswd_make_pwchange_reply(kdc, mem_ctx,
212 static BOOL kpasswd_process_request(struct kdc_server *kdc,
214 struct gensec_security *gensec_security,
219 struct auth_session_info *session_info;
220 if (!NT_STATUS_IS_OK(gensec_session_info(gensec_security,
222 return kpasswdd_make_error_reply(kdc, mem_ctx,
223 KRB5_KPASSWD_HARDERROR,
224 "gensec_session_info failed!",
229 case KRB5_KPASSWD_VERS_CHANGEPW:
231 char *password = talloc_strndup(mem_ctx, (const char *)input->data, input->length);
235 return kpasswdd_change_password(kdc, mem_ctx, session_info,
239 case KRB5_KPASSWD_VERS_SETPW:
242 enum samr_RejectReason reject_reason = SAMR_REJECT_OTHER;
243 struct samr_DomInfo1 *dominfo = NULL;
244 struct ldb_context *samdb;
245 struct ldb_message *msg;
246 krb5_context context = kdc->smb_krb5_context->krb5_context;
248 ChangePasswdDataMS chpw;
251 krb5_principal principal;
252 char *set_password_on_princ;
253 struct ldb_dn *set_password_on_dn;
258 msg = ldb_msg_new(mem_ctx);
263 ret = decode_ChangePasswdDataMS(input->data, input->length,
266 return kpasswdd_make_error_reply(kdc, mem_ctx,
267 KRB5_KPASSWD_MALFORMED,
268 "failed to decode password change structure",
272 password = talloc_strndup(mem_ctx, chpw.newpasswd.data,
273 chpw.newpasswd.length);
275 free_ChangePasswdDataMS(&chpw);
278 if ((chpw.targname && !chpw.targrealm)
279 || (!chpw.targname && chpw.targrealm)) {
280 return kpasswdd_make_error_reply(kdc, mem_ctx,
281 KRB5_KPASSWD_MALFORMED,
282 "Realm and principal must be both present, or neither present",
285 if (chpw.targname && chpw.targrealm) {
286 if (_krb5_principalname2krb5_principal(kdc->smb_krb5_context->krb5_context,
287 &principal, *chpw.targname,
288 *chpw.targrealm) != 0) {
289 free_ChangePasswdDataMS(&chpw);
290 return kpasswdd_make_error_reply(kdc, mem_ctx,
291 KRB5_KPASSWD_MALFORMED,
292 "failed to extract principal to set",
297 free_ChangePasswdDataMS(&chpw);
298 return kpasswdd_change_password(kdc, mem_ctx, session_info,
301 free_ChangePasswdDataMS(&chpw);
303 if (krb5_unparse_name(context, principal, &set_password_on_princ) != 0) {
304 krb5_free_principal(context, principal);
305 return kpasswdd_make_error_reply(kdc, mem_ctx,
306 KRB5_KPASSWD_MALFORMED,
307 "krb5_unparse_name failed!",
311 krb5_free_principal(context, principal);
313 samdb = samdb_connect(mem_ctx, session_info);
315 return kpasswdd_make_error_reply(kdc, mem_ctx,
316 KRB5_KPASSWD_HARDERROR,
317 "Unable to open database!",
321 DEBUG(3, ("%s\\%s (%s) is changing password of %s\n",
322 session_info->server_info->domain_name,
323 session_info->server_info->account_name,
324 dom_sid_string(mem_ctx, session_info->security_token->user_sid),
325 set_password_on_princ));
326 ret = ldb_transaction_start(samdb);
328 status = NT_STATUS_TRANSACTION_ABORTED;
329 return kpasswd_make_pwchange_reply(kdc, mem_ctx,
336 status = crack_user_principal_name(samdb, mem_ctx,
337 set_password_on_princ,
338 &set_password_on_dn, NULL);
339 free(set_password_on_princ);
340 if (!NT_STATUS_IS_OK(status)) {
341 ldb_transaction_cancel(samdb);
342 return kpasswd_make_pwchange_reply(kdc, mem_ctx,
349 msg = ldb_msg_new(mem_ctx);
351 ldb_transaction_cancel(samdb);
352 status = NT_STATUS_NO_MEMORY;
354 msg->dn = ldb_dn_copy(msg, set_password_on_dn);
356 status = NT_STATUS_NO_MEMORY;
360 if (NT_STATUS_IS_OK(status)) {
361 /* Admin password set */
362 status = samdb_set_password(samdb, mem_ctx,
363 set_password_on_dn, NULL,
364 msg, password, NULL, NULL,
365 False, /* this is not a user password change */
366 True, /* run restriction tests */
367 &reject_reason, &dominfo);
370 if (NT_STATUS_IS_OK(status)) {
371 /* modify the samdb record */
372 ret = samdb_replace(samdb, mem_ctx, msg);
374 DEBUG(2,("Failed to modify record to set password on %s: %s\n",
375 ldb_dn_linearize(mem_ctx, msg->dn),
376 ldb_errstring(samdb)));
377 status = NT_STATUS_ACCESS_DENIED;
380 if (NT_STATUS_IS_OK(status)) {
381 ret = ldb_transaction_commit(samdb);
383 DEBUG(1,("Failed to commit transaction to set password on %s: %s\n",
384 ldb_dn_linearize(mem_ctx, msg->dn),
385 ldb_errstring(samdb)));
386 status = NT_STATUS_TRANSACTION_ABORTED;
389 ldb_transaction_cancel(samdb);
391 return kpasswd_make_pwchange_reply(kdc, mem_ctx,
398 return kpasswdd_make_error_reply(kdc, mem_ctx,
399 KRB5_KPASSWD_BAD_VERSION,
400 talloc_asprintf(mem_ctx,
401 "Protocol version %u not supported",
408 BOOL kpasswdd_process(struct kdc_server *kdc,
412 struct socket_address *peer_addr,
413 struct socket_address *my_addr,
417 const uint16_t header_len = 6;
420 uint16_t krb_priv_len;
423 DATA_BLOB ap_req, krb_priv_req;
424 DATA_BLOB krb_priv_rep = data_blob(NULL, 0);
425 DATA_BLOB ap_rep = data_blob(NULL, 0);
426 DATA_BLOB kpasswd_req, kpasswd_rep;
427 struct cli_credentials *server_credentials;
428 struct gensec_security *gensec_security;
429 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
435 /* Be parinoid. We need to ensure we don't just let the
436 * caller lead us into a buffer overflow */
437 if (input->length <= header_len) {
438 talloc_free(tmp_ctx);
442 len = RSVAL(input->data, 0);
443 if (input->length != len) {
444 talloc_free(tmp_ctx);
448 /* There are two different versions of this protocol so far,
449 * plus others in the standards pipe. Fortunetly they all
450 * take a very similar framing */
451 version = RSVAL(input->data, 2);
452 ap_req_len = RSVAL(input->data, 4);
453 if ((ap_req_len >= len) || (ap_req_len + header_len) >= len) {
454 talloc_free(tmp_ctx);
458 krb_priv_len = len - ap_req_len;
459 ap_req = data_blob_const(&input->data[header_len], ap_req_len);
460 krb_priv_req = data_blob_const(&input->data[header_len + ap_req_len], krb_priv_len);
462 nt_status = gensec_server_start(tmp_ctx, kdc->task->event_ctx, kdc->task->msg_ctx, &gensec_security);
463 if (!NT_STATUS_IS_OK(nt_status)) {
464 talloc_free(tmp_ctx);
468 server_credentials = cli_credentials_init(tmp_ctx);
469 if (!server_credentials) {
470 DEBUG(1, ("Failed to init server credentials\n"));
474 /* We want the credentials subsystem to use the krb5 context
475 * we already have, rather than a new context */
476 cli_credentials_set_krb5_context(server_credentials, kdc->smb_krb5_context);
477 cli_credentials_set_conf(server_credentials);
478 nt_status = cli_credentials_set_stored_principal(server_credentials, "kadmin/changepw");
479 if (!NT_STATUS_IS_OK(nt_status)) {
480 ret = kpasswdd_make_unauth_error_reply(kdc, mem_ctx,
481 KRB5_KPASSWD_HARDERROR,
482 talloc_asprintf(mem_ctx,
483 "Failed to obtain server credentials for kadmin/changepw: %s\n",
484 nt_errstr(nt_status)),
490 talloc_free(tmp_ctx);
494 nt_status = gensec_set_credentials(gensec_security, server_credentials);
495 if (!NT_STATUS_IS_OK(nt_status)) {
496 talloc_free(tmp_ctx);
500 /* The kerberos PRIV packets include these addresses. MIT
501 * clients check that they are present */
502 nt_status = gensec_set_peer_addr(gensec_security, peer_addr);
503 if (!NT_STATUS_IS_OK(nt_status)) {
504 talloc_free(tmp_ctx);
507 nt_status = gensec_set_my_addr(gensec_security, my_addr);
508 if (!NT_STATUS_IS_OK(nt_status)) {
509 talloc_free(tmp_ctx);
513 /* We want the GENSEC wrap calls to generate PRIV tokens */
514 gensec_want_feature(gensec_security, GENSEC_FEATURE_SEAL);
516 nt_status = gensec_start_mech_by_name(gensec_security, "krb5");
517 if (!NT_STATUS_IS_OK(nt_status)) {
518 talloc_free(tmp_ctx);
522 /* Accept the AP-REQ and generate teh AP-REP we need for the reply */
523 nt_status = gensec_update(gensec_security, tmp_ctx, ap_req, &ap_rep);
524 if (!NT_STATUS_IS_OK(nt_status) && !NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
526 ret = kpasswdd_make_unauth_error_reply(kdc, mem_ctx,
527 KRB5_KPASSWD_HARDERROR,
528 talloc_asprintf(mem_ctx,
529 "gensec_update failed: %s",
530 nt_errstr(nt_status)),
536 talloc_free(tmp_ctx);
540 /* Extract the data from the KRB-PRIV half of the message */
541 nt_status = gensec_unwrap(gensec_security, tmp_ctx, &krb_priv_req, &kpasswd_req);
542 if (!NT_STATUS_IS_OK(nt_status)) {
543 ret = kpasswdd_make_unauth_error_reply(kdc, mem_ctx,
544 KRB5_KPASSWD_HARDERROR,
545 talloc_asprintf(mem_ctx,
546 "gensec_unwrap failed: %s",
547 nt_errstr(nt_status)),
553 talloc_free(tmp_ctx);
557 /* Figure out something to do with it (probably changing a password...) */
558 ret = kpasswd_process_request(kdc, tmp_ctx,
561 &kpasswd_req, &kpasswd_rep);
567 /* And wrap up the reply: This ensures that the error message
568 * or success can be verified by the client */
569 nt_status = gensec_wrap(gensec_security, tmp_ctx,
570 &kpasswd_rep, &krb_priv_rep);
571 if (!NT_STATUS_IS_OK(nt_status)) {
572 ret = kpasswdd_make_unauth_error_reply(kdc, mem_ctx,
573 KRB5_KPASSWD_HARDERROR,
574 talloc_asprintf(mem_ctx,
575 "gensec_wrap failed: %s",
576 nt_errstr(nt_status)),
582 talloc_free(tmp_ctx);
587 *reply = data_blob_talloc(mem_ctx, NULL, krb_priv_rep.length + ap_rep.length + header_len);
592 RSSVAL(reply->data, 0, reply->length);
593 RSSVAL(reply->data, 2, 1); /* This is a version 1 reply, MS change/set or otherwise */
594 RSSVAL(reply->data, 4, ap_rep.length);
595 memcpy(reply->data + header_len,
598 memcpy(reply->data + header_len + ap_rep.length,
600 krb_priv_rep.length);
602 talloc_free(tmp_ctx);