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\n", dom_sid_string(mem_ctx, session_info->security_token->user_sid)));
183 /* User password change */
184 status = samdb_set_password_sid(samdb, mem_ctx,
185 session_info->security_token->user_sid,
186 password, NULL, NULL,
187 True, /* this is a user password change */
188 True, /* run restriction tests */
191 return kpasswd_make_pwchange_reply(kdc, mem_ctx,
199 static BOOL kpasswd_process_request(struct kdc_server *kdc,
201 struct gensec_security *gensec_security,
207 enum samr_RejectReason reject_reason;
208 struct samr_DomInfo1 *dominfo;
209 struct ldb_context *samdb;
210 struct auth_session_info *session_info;
211 struct ldb_message *msg = ldb_msg_new(gensec_security);
212 krb5_context context = kdc->smb_krb5_context->krb5_context;
218 if (!NT_STATUS_IS_OK(gensec_session_info(gensec_security,
220 return kpasswdd_make_error_reply(kdc, mem_ctx,
221 KRB5_KPASSWD_HARDERROR,
222 "gensec_session_info failed!",
227 case KRB5_KPASSWD_VERS_CHANGEPW:
229 char *password = talloc_strndup(mem_ctx, (const char *)input->data, input->length);
233 return kpasswdd_change_password(kdc, mem_ctx, session_info,
237 case KRB5_KPASSWD_VERS_SETPW:
240 ChangePasswdDataMS chpw;
242 krb5_principal principal;
243 char *set_password_on_princ;
244 struct ldb_dn *set_password_on_dn;
246 samdb = samdb_connect(gensec_security, session_info);
248 ret = decode_ChangePasswdDataMS(input->data, input->length,
251 return kpasswdd_make_error_reply(kdc, mem_ctx,
252 KRB5_KPASSWD_MALFORMED,
253 "failed to decode password change structure",
257 password = talloc_strndup(mem_ctx, chpw.newpasswd.data,
258 chpw.newpasswd.length);
260 free_ChangePasswdDataMS(&chpw);
263 if ((chpw.targname && !chpw.targrealm)
264 || (!chpw.targname && chpw.targrealm)) {
265 return kpasswdd_make_error_reply(kdc, mem_ctx,
266 KRB5_KPASSWD_MALFORMED,
267 "Realm and principal must be both present, or neither present",
270 if (chpw.targname && chpw.targrealm) {
271 if (_krb5_principalname2krb5_principal(&principal, *chpw.targname,
272 *chpw.targrealm) != 0) {
273 free_ChangePasswdDataMS(&chpw);
274 return kpasswdd_make_error_reply(kdc, mem_ctx,
275 KRB5_KPASSWD_MALFORMED,
276 "failed to extract principal to set",
281 free_ChangePasswdDataMS(&chpw);
282 return kpasswdd_change_password(kdc, mem_ctx, session_info,
285 free_ChangePasswdDataMS(&chpw);
287 if (krb5_unparse_name(context, principal, &set_password_on_princ) != 0) {
288 krb5_free_principal(context, principal);
289 return kpasswdd_make_error_reply(kdc, mem_ctx,
290 KRB5_KPASSWD_MALFORMED,
291 "krb5_unparse_name failed!",
295 krb5_free_principal(context, principal);
297 status = crack_user_principal_name(samdb, mem_ctx,
298 set_password_on_princ,
299 &set_password_on_dn, NULL);
300 free(set_password_on_princ);
301 if (!NT_STATUS_IS_OK(status)) {
302 return kpasswd_make_pwchange_reply(kdc, mem_ctx,
309 /* Admin password set */
310 status = samdb_set_password(samdb, mem_ctx,
311 set_password_on_dn, NULL,
312 msg, password, NULL, NULL,
313 False, /* this is not a user password change */
314 True, /* run restriction tests */
315 &reject_reason, &dominfo);
317 return kpasswd_make_pwchange_reply(kdc, mem_ctx,
324 return kpasswdd_make_error_reply(kdc, mem_ctx,
325 KRB5_KPASSWD_BAD_VERSION,
326 talloc_asprintf(mem_ctx,
327 "Protocol version %u not supported",
334 BOOL kpasswdd_process(struct kdc_server *kdc,
342 const uint16_t header_len = 6;
345 uint16_t krb_priv_len;
348 DATA_BLOB ap_req, krb_priv_req, krb_priv_rep, ap_rep;
349 DATA_BLOB kpasswd_req, kpasswd_rep;
350 struct cli_credentials *server_credentials;
351 struct gensec_security *gensec_security;
352 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
358 if (input->length <= header_len) {
359 talloc_free(tmp_ctx);
363 len = RSVAL(input->data, 0);
364 if (input->length != len) {
365 talloc_free(tmp_ctx);
369 version = RSVAL(input->data, 2);
370 ap_req_len = RSVAL(input->data, 4);
371 if ((ap_req_len >= len) || (ap_req_len + header_len) >= len) {
372 talloc_free(tmp_ctx);
376 krb_priv_len = len - ap_req_len;
377 ap_req = data_blob_const(&input->data[header_len], ap_req_len);
378 krb_priv_req = data_blob_const(&input->data[header_len + ap_req_len], krb_priv_len);
380 nt_status = gensec_server_start(tmp_ctx, &gensec_security, kdc->task->event_ctx);
381 if (!NT_STATUS_IS_OK(nt_status)) {
382 talloc_free(tmp_ctx);
387 = cli_credentials_init(tmp_ctx);
388 if (!server_credentials) {
389 DEBUG(1, ("Failed to init server credentials\n"));
393 cli_credentials_set_conf(server_credentials);
394 nt_status = cli_credentials_set_stored_principal(server_credentials, "kadmin/changepw");
395 if (!NT_STATUS_IS_OK(nt_status)) {
396 ret = kpasswdd_make_unauth_error_reply(kdc, mem_ctx,
397 KRB5_KPASSWD_HARDERROR,
398 talloc_asprintf(mem_ctx,
399 "Failed to obtain server credentials for kadmin/changepw: %s\n",
400 nt_errstr(nt_status)),
406 talloc_free(tmp_ctx);
410 gensec_set_credentials(gensec_security, server_credentials);
411 gensec_want_feature(gensec_security, GENSEC_FEATURE_SEAL);
413 nt_status = gensec_start_mech_by_name(gensec_security, "krb5");
414 if (!NT_STATUS_IS_OK(nt_status)) {
415 talloc_free(tmp_ctx);
419 nt_status = gensec_update(gensec_security, tmp_ctx, ap_req, &ap_rep);
420 if (!NT_STATUS_IS_OK(nt_status) && !NT_STATUS_EQUAL(nt_status, NT_STATUS_MORE_PROCESSING_REQUIRED)) {
422 ret = kpasswdd_make_unauth_error_reply(kdc, mem_ctx,
423 KRB5_KPASSWD_HARDERROR,
424 talloc_asprintf(mem_ctx,
425 "gensec_update failed: %s",
426 nt_errstr(nt_status)),
432 talloc_free(tmp_ctx);
436 nt_status = gensec_unwrap(gensec_security, tmp_ctx, &krb_priv_req, &kpasswd_req);
437 if (!NT_STATUS_IS_OK(nt_status)) {
438 ret = kpasswdd_make_unauth_error_reply(kdc, mem_ctx,
439 KRB5_KPASSWD_HARDERROR,
440 talloc_asprintf(mem_ctx,
441 "gensec_unwrap failed: %s",
442 nt_errstr(nt_status)),
448 talloc_free(tmp_ctx);
452 ret = kpasswd_process_request(kdc, tmp_ctx,
455 &kpasswd_req, &kpasswd_rep);
461 nt_status = gensec_wrap(gensec_security, tmp_ctx,
462 &kpasswd_rep, &krb_priv_rep);
463 if (!NT_STATUS_IS_OK(nt_status)) {
464 ret = kpasswdd_make_unauth_error_reply(kdc, mem_ctx,
465 KRB5_KPASSWD_HARDERROR,
466 talloc_asprintf(mem_ctx,
467 "gensec_wrap failed: %s",
468 nt_errstr(nt_status)),
474 talloc_free(tmp_ctx);
479 *reply = data_blob_talloc(mem_ctx, NULL, krb_priv_rep.length + ap_rep.length + header_len);
484 RSSVAL(reply->data, 0, reply->length);
485 RSSVAL(reply->data, 2, 1); /* This is a version 1 reply, MS change/set or otherwise */
486 RSSVAL(reply->data, 4, ap_rep.length);
487 memcpy(reply->data + header_len,
490 memcpy(reply->data + header_len + ap_rep.length,
492 krb_priv_rep.length);
494 talloc_free(tmp_ctx);