2 Unix SMB/CIFS implementation.
3 krb5 set password implementation
4 Copyright (C) Andrew Tridgell 2001
5 Copyright (C) Remus Koos 2001 (remuskoos@yahoo.com)
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #define DEFAULT_KPASSWD_PORT 464
28 #define KRB5_KPASSWD_VERS_CHANGEPW 1
30 #define KRB5_KPASSWD_VERS_SETPW 0xff80
31 #define KRB5_KPASSWD_VERS_SETPW_ALT 2
33 #define KRB5_KPASSWD_SUCCESS 0
34 #define KRB5_KPASSWD_MALFORMED 1
35 #define KRB5_KPASSWD_HARDERROR 2
36 #define KRB5_KPASSWD_AUTHERROR 3
37 #define KRB5_KPASSWD_SOFTERROR 4
38 #define KRB5_KPASSWD_ACCESSDENIED 5
39 #define KRB5_KPASSWD_BAD_VERSION 6
40 #define KRB5_KPASSWD_INITIAL_FLAG_NEEDED 7
42 /* Those are defined by kerberos-set-passwd-02.txt and are probably
43 * not supported by M$ implementation */
44 #define KRB5_KPASSWD_POLICY_REJECT 8
45 #define KRB5_KPASSWD_BAD_PRINCIPAL 9
46 #define KRB5_KPASSWD_ETYPE_NOSUPP 10
49 * we've got to be able to distinguish KRB_ERRORs from other
50 * requests - valid response for CHPW v2 replies.
53 #define krb5_is_krb_error(packet) \
54 ( packet && packet->length && (((char *)packet->data)[0] == 0x7e || ((char *)packet->data)[0] == 0x5e))
56 /* This implements kerberos password change protocol as specified in
57 * kerb-chg-password-02.txt and kerberos-set-passwd-02.txt
58 * as well as microsoft version of the protocol
59 * as specified in kerberos-set-passwd-00.txt
61 static DATA_BLOB
encode_krb5_setpw(const char *principal
, const char *password
)
63 char* princ_part1
= NULL
;
64 char* princ_part2
= NULL
;
73 princ
= SMB_STRDUP(principal
);
75 if ((c
= strchr_m(princ
, '/')) == NULL
) {
85 if ((c
= strchr_m(c
, '@')) != NULL
) {
90 /* We must have a realm component. */
91 return data_blob_null
;
94 memset(&req
, 0, sizeof(req
));
96 asn1_push_tag(&req
, ASN1_SEQUENCE(0));
97 asn1_push_tag(&req
, ASN1_CONTEXT(0));
98 asn1_write_OctetString(&req
, password
, strlen(password
));
101 asn1_push_tag(&req
, ASN1_CONTEXT(1));
102 asn1_push_tag(&req
, ASN1_SEQUENCE(0));
104 asn1_push_tag(&req
, ASN1_CONTEXT(0));
105 asn1_write_Integer(&req
, 1);
108 asn1_push_tag(&req
, ASN1_CONTEXT(1));
109 asn1_push_tag(&req
, ASN1_SEQUENCE(0));
112 asn1_write_GeneralString(&req
, princ_part1
);
115 asn1_write_GeneralString(&req
, princ_part2
);
121 asn1_push_tag(&req
, ASN1_CONTEXT(2));
122 asn1_write_GeneralString(&req
, realm
);
126 ret
= data_blob(req
.data
, req
.length
);
134 static krb5_error_code
build_kpasswd_request(uint16 pversion
,
135 krb5_context context
,
136 krb5_auth_context auth_context
,
145 krb5_data encoded_setpw
;
146 krb5_replay_data replay
;
149 unsigned int msg_length
;
151 ret
= krb5_auth_con_setflags(context
,
152 auth_context
,KRB5_AUTH_CONTEXT_DO_SEQUENCE
);
154 DEBUG(1,("krb5_auth_con_setflags failed (%s)\n",
155 error_message(ret
)));
159 /* handle protocol differences in chpw and setpw */
160 if (pversion
== KRB5_KPASSWD_VERS_CHANGEPW
)
161 setpw
= data_blob(passwd
, strlen(passwd
));
162 else if (pversion
== KRB5_KPASSWD_VERS_SETPW
||
163 pversion
== KRB5_KPASSWD_VERS_SETPW_ALT
)
164 setpw
= encode_krb5_setpw(princ
, passwd
);
168 if (setpw
.data
== NULL
|| setpw
.length
== 0) {
172 encoded_setpw
.data
= (char *)setpw
.data
;
173 encoded_setpw
.length
= setpw
.length
;
175 ret
= krb5_mk_priv(context
, auth_context
,
176 &encoded_setpw
, &cipherpw
, &replay
);
178 data_blob_free(&setpw
); /*from 'encode_krb5_setpw(...)' */
181 DEBUG(1,("krb5_mk_priv failed (%s)\n", error_message(ret
)));
185 packet
->data
= (char *)SMB_MALLOC(ap_req
->length
+ cipherpw
.length
+ (use_tcp
? 10 : 6 ));
191 /* see the RFC for details */
193 msg_start
= p
= ((char *)packet
->data
) + (use_tcp
? 4 : 0);
195 RSSVAL(p
, 0, pversion
);
197 RSSVAL(p
, 0, ap_req
->length
);
199 memcpy(p
, ap_req
->data
, ap_req
->length
);
201 memcpy(p
, cipherpw
.data
, cipherpw
.length
);
202 p
+= cipherpw
.length
;
203 packet
->length
= PTR_DIFF(p
,packet
->data
);
204 msg_length
= PTR_DIFF(p
,msg_start
);
207 RSIVAL(packet
->data
, 0, msg_length
);
209 RSSVAL(msg_start
, 0, msg_length
);
211 free(cipherpw
.data
); /* from 'krb5_mk_priv(...)' */
216 static const struct kpasswd_errors
{
218 const char *error_string
;
219 } kpasswd_errors
[] = {
220 {KRB5_KPASSWD_MALFORMED
, "Malformed request error"},
221 {KRB5_KPASSWD_HARDERROR
, "Server error"},
222 {KRB5_KPASSWD_AUTHERROR
, "Authentication error"},
223 {KRB5_KPASSWD_SOFTERROR
, "Password change rejected"},
224 {KRB5_KPASSWD_ACCESSDENIED
, "Client does not have proper authorization"},
225 {KRB5_KPASSWD_BAD_VERSION
, "Protocol version not supported"},
226 {KRB5_KPASSWD_INITIAL_FLAG_NEEDED
, "Authorization ticket must have initial flag set"},
227 {KRB5_KPASSWD_POLICY_REJECT
, "Password rejected due to policy requirements"},
228 {KRB5_KPASSWD_BAD_PRINCIPAL
, "Target principal does not exist"},
229 {KRB5_KPASSWD_ETYPE_NOSUPP
, "Unsupported encryption type"},
233 static krb5_error_code
setpw_result_code_string(krb5_context context
,
235 const char **code_string
)
237 unsigned int idx
= 0;
239 while (kpasswd_errors
[idx
].error_string
!= NULL
) {
240 if (kpasswd_errors
[idx
].result_code
==
242 *code_string
= kpasswd_errors
[idx
].error_string
;
247 *code_string
= "Password change failed";
251 krb5_error_code
kpasswd_err_to_krb5_err(krb5_error_code res_code
)
254 case KRB5_KPASSWD_ACCESSDENIED
:
255 return KRB5KDC_ERR_BADOPTION
;
256 case KRB5_KPASSWD_INITIAL_FLAG_NEEDED
:
257 return KRB5KDC_ERR_BADOPTION
;
258 /* return KV5M_ALT_METHOD; MIT-only define */
259 case KRB5_KPASSWD_ETYPE_NOSUPP
:
260 return KRB5KDC_ERR_ETYPE_NOSUPP
;
261 case KRB5_KPASSWD_BAD_PRINCIPAL
:
262 return KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN
;
263 case KRB5_KPASSWD_POLICY_REJECT
:
264 case KRB5_KPASSWD_SOFTERROR
:
265 return KRB5KDC_ERR_POLICY
;
267 return KRB5KRB_ERR_GENERIC
;
270 static krb5_error_code
parse_setpw_reply(krb5_context context
,
272 krb5_auth_context auth_context
,
277 int vnum
, ret
, res_code
;
278 krb5_data cipherresult
;
279 krb5_data clearresult
;
280 krb5_ap_rep_enc_part
*ap_rep_enc
;
281 krb5_replay_data replay
;
282 unsigned int msg_length
= packet
->length
;
285 if (packet
->length
< (use_tcp
? 8 : 4)) {
286 return KRB5KRB_AP_ERR_MODIFIED
;
289 p
= (char *)packet
->data
;
291 ** see if it is an error
293 if (krb5_is_krb_error(packet
)) {
295 ret
= handle_krberror_packet(context
, packet
);
305 if (RIVAL(p
, 0) != msg_length
) {
306 DEBUG(1,("Bad TCP packet length (%d/%d) from kpasswd server\n",
307 RIVAL(p
, 0), msg_length
));
308 return KRB5KRB_AP_ERR_MODIFIED
;
314 if (RSVAL(p
, 0) != msg_length
) {
315 DEBUG(1,("Bad packet length (%d/%d) from kpasswd server\n",
316 RSVAL(p
, 0), msg_length
));
317 return KRB5KRB_AP_ERR_MODIFIED
;
322 vnum
= RSVAL(p
, 0); p
+= 2;
324 /* FIXME: According to standard there is only one type of reply */
325 if (vnum
!= KRB5_KPASSWD_VERS_SETPW
&&
326 vnum
!= KRB5_KPASSWD_VERS_SETPW_ALT
&&
327 vnum
!= KRB5_KPASSWD_VERS_CHANGEPW
) {
328 DEBUG(1,("Bad vnum (%d) from kpasswd server\n", vnum
));
329 return KRB5KDC_ERR_BAD_PVNO
;
332 ap_rep
.length
= RSVAL(p
, 0); p
+= 2;
334 if (p
+ ap_rep
.length
>= (char *)packet
->data
+ packet
->length
) {
335 DEBUG(1,("ptr beyond end of packet from kpasswd server\n"));
336 return KRB5KRB_AP_ERR_MODIFIED
;
339 if (ap_rep
.length
== 0) {
340 DEBUG(1,("got unencrypted setpw result?!\n"));
341 return KRB5KRB_AP_ERR_MODIFIED
;
348 ret
= krb5_rd_rep(context
, auth_context
, &ap_rep
, &ap_rep_enc
);
350 DEBUG(1,("failed to rd setpw reply (%s)\n", error_message(ret
)));
351 return KRB5KRB_AP_ERR_MODIFIED
;
354 krb5_free_ap_rep_enc_part(context
, ap_rep_enc
);
356 cipherresult
.data
= p
;
357 cipherresult
.length
= ((char *)packet
->data
+ packet
->length
) - p
;
359 ret
= krb5_rd_priv(context
, auth_context
, &cipherresult
, &clearresult
,
362 DEBUG(1,("failed to decrypt setpw reply (%s)\n", error_message(ret
)));
363 return KRB5KRB_AP_ERR_MODIFIED
;
366 if (clearresult
.length
< 2) {
367 free(clearresult
.data
);
368 ret
= KRB5KRB_AP_ERR_MODIFIED
;
369 return KRB5KRB_AP_ERR_MODIFIED
;
372 p
= (char *)clearresult
.data
;
374 res_code
= RSVAL(p
, 0);
376 free(clearresult
.data
);
378 if ((res_code
< KRB5_KPASSWD_SUCCESS
) ||
379 (res_code
> KRB5_KPASSWD_ETYPE_NOSUPP
)) {
380 return KRB5KRB_AP_ERR_MODIFIED
;
383 if (res_code
== KRB5_KPASSWD_SUCCESS
) {
387 setpw_result_code_string(context
, res_code
, &errstr
);
388 DEBUG(1, ("Error changing password: %s (%d)\n", errstr
, res_code
));
390 return kpasswd_err_to_krb5_err(res_code
);
394 static ADS_STATUS
do_krb5_kpasswd_request(krb5_context context
,
395 const char *kdc_host
,
401 krb5_auth_context auth_context
= NULL
;
402 krb5_data ap_req
, chpw_req
, chpw_rep
;
405 struct sockaddr_storage remote_addr
, local_addr
;
406 struct sockaddr_storage addr
;
407 krb5_address local_kaddr
, remote_kaddr
;
408 bool use_tcp
= False
;
411 if (!interpret_string_addr(&addr
, kdc_host
, 0)) {
414 ret
= krb5_mk_req_extended(context
, &auth_context
, AP_OPTS_USE_SUBKEY
,
415 NULL
, credsp
, &ap_req
);
417 DEBUG(1,("krb5_mk_req_extended failed (%s)\n", error_message(ret
)));
418 return ADS_ERROR_KRB5(ret
);
425 sock
= open_udp_socket(kdc_host
, DEFAULT_KPASSWD_PORT
);
429 sock
= open_socket_out(SOCK_STREAM
, &addr
, DEFAULT_KPASSWD_PORT
,
430 LONG_CONNECT_TIMEOUT
);
435 SAFE_FREE(ap_req
.data
);
436 krb5_auth_con_free(context
, auth_context
);
437 DEBUG(1,("failed to open kpasswd socket to %s (%s)\n",
438 kdc_host
, strerror(errno
)));
439 return ADS_ERROR_SYSTEM(rc
);
441 addr_len
= sizeof(remote_addr
);
442 if (getpeername(sock
, (struct sockaddr
*)&remote_addr
, &addr_len
) != 0) {
444 SAFE_FREE(ap_req
.data
);
445 krb5_auth_con_free(context
, auth_context
);
446 DEBUG(1,("getpeername() failed (%s)\n", error_message(errno
)));
447 return ADS_ERROR_SYSTEM(errno
);
449 addr_len
= sizeof(local_addr
);
450 if (getsockname(sock
, (struct sockaddr
*)&local_addr
, &addr_len
) != 0) {
452 SAFE_FREE(ap_req
.data
);
453 krb5_auth_con_free(context
, auth_context
);
454 DEBUG(1,("getsockname() failed (%s)\n", error_message(errno
)));
455 return ADS_ERROR_SYSTEM(errno
);
457 if (!setup_kaddr(&remote_kaddr
, &remote_addr
) ||
458 !setup_kaddr(&local_kaddr
, &local_addr
)) {
459 DEBUG(1,("do_krb5_kpasswd_request: "
460 "Failed to setup addresses.\n"));
462 SAFE_FREE(ap_req
.data
);
463 krb5_auth_con_free(context
, auth_context
);
465 return ADS_ERROR_SYSTEM(EINVAL
);
468 ret
= krb5_auth_con_setaddrs(context
, auth_context
, &local_kaddr
, NULL
);
471 SAFE_FREE(ap_req
.data
);
472 krb5_auth_con_free(context
, auth_context
);
473 DEBUG(1,("krb5_auth_con_setaddrs failed (%s)\n", error_message(ret
)));
474 return ADS_ERROR_KRB5(ret
);
477 ret
= build_kpasswd_request(pversion
, context
, auth_context
, &ap_req
,
478 princ
, newpw
, use_tcp
, &chpw_req
);
481 SAFE_FREE(ap_req
.data
);
482 krb5_auth_con_free(context
, auth_context
);
483 DEBUG(1,("build_setpw_request failed (%s)\n", error_message(ret
)));
484 return ADS_ERROR_KRB5(ret
);
487 ret
= write(sock
, chpw_req
.data
, chpw_req
.length
);
489 if (ret
!= chpw_req
.length
) {
491 SAFE_FREE(chpw_req
.data
);
492 SAFE_FREE(ap_req
.data
);
493 krb5_auth_con_free(context
, auth_context
);
494 DEBUG(1,("send of chpw failed (%s)\n", strerror(errno
)));
495 return ADS_ERROR_SYSTEM(errno
);
498 SAFE_FREE(chpw_req
.data
);
500 chpw_rep
.length
= 1500;
501 chpw_rep
.data
= (char *) SMB_MALLOC(chpw_rep
.length
);
502 if (!chpw_rep
.data
) {
504 SAFE_FREE(ap_req
.data
);
505 krb5_auth_con_free(context
, auth_context
);
506 DEBUG(1,("send of chpw failed (%s)\n", strerror(errno
)));
508 return ADS_ERROR_SYSTEM(errno
);
511 ret
= read(sock
, chpw_rep
.data
, chpw_rep
.length
);
514 SAFE_FREE(chpw_rep
.data
);
515 SAFE_FREE(ap_req
.data
);
516 krb5_auth_con_free(context
, auth_context
);
517 DEBUG(1,("recv of chpw reply failed (%s)\n", strerror(errno
)));
518 return ADS_ERROR_SYSTEM(errno
);
522 chpw_rep
.length
= ret
;
524 ret
= krb5_auth_con_setaddrs(context
, auth_context
, NULL
,&remote_kaddr
);
526 SAFE_FREE(chpw_rep
.data
);
527 SAFE_FREE(ap_req
.data
);
528 krb5_auth_con_free(context
, auth_context
);
529 DEBUG(1,("krb5_auth_con_setaddrs on reply failed (%s)\n",
530 error_message(ret
)));
531 return ADS_ERROR_KRB5(ret
);
534 ret
= parse_setpw_reply(context
, use_tcp
, auth_context
, &chpw_rep
);
535 SAFE_FREE(chpw_rep
.data
);
539 if (ret
== KRB5KRB_ERR_RESPONSE_TOO_BIG
&& !use_tcp
) {
540 DEBUG(5, ("Trying setpw with TCP!!!\n"));
545 SAFE_FREE(ap_req
.data
);
546 krb5_auth_con_free(context
, auth_context
);
547 DEBUG(1,("parse_setpw_reply failed (%s)\n",
548 error_message(ret
)));
549 return ADS_ERROR_KRB5(ret
);
552 SAFE_FREE(ap_req
.data
);
553 krb5_auth_con_free(context
, auth_context
);
559 ADS_STATUS
ads_krb5_set_password(const char *kdc_host
, const char *princ
,
560 const char *newpw
, int time_offset
)
564 krb5_error_code ret
= 0;
565 krb5_context context
= NULL
;
566 krb5_principal principal
= NULL
;
567 char *princ_name
= NULL
;
569 krb5_creds creds
, *credsp
= NULL
;
570 #if KRB5_PRINC_REALM_RETURNS_REALM
571 krb5_realm orig_realm
;
573 krb5_data orig_realm
;
575 krb5_ccache ccache
= NULL
;
579 initialize_krb5_error_table();
580 ret
= krb5_init_context(&context
);
582 DEBUG(1,("Failed to init krb5 context (%s)\n", error_message(ret
)));
583 return ADS_ERROR_KRB5(ret
);
586 if (time_offset
!= 0) {
587 krb5_set_real_time(context
, time(NULL
) + time_offset
, 0);
590 ret
= krb5_cc_default(context
, &ccache
);
592 krb5_free_context(context
);
593 DEBUG(1,("Failed to get default creds (%s)\n", error_message(ret
)));
594 return ADS_ERROR_KRB5(ret
);
597 realm
= strchr_m(princ
, '@');
599 krb5_cc_close(context
, ccache
);
600 krb5_free_context(context
);
601 DEBUG(1,("Failed to get realm\n"));
602 return ADS_ERROR_KRB5(-1);
606 if (asprintf(&princ_name
, "kadmin/changepw@%s", realm
) == -1) {
607 krb5_cc_close(context
, ccache
);
608 krb5_free_context(context
);
609 DEBUG(1,("asprintf failed\n"));
610 return ADS_ERROR_NT(NT_STATUS_NO_MEMORY
);
613 ret
= smb_krb5_parse_name(context
, princ_name
, &creds
.server
);
615 krb5_cc_close(context
, ccache
);
616 krb5_free_context(context
);
617 DEBUG(1,("Failed to parse kadmin/changepw (%s)\n", error_message(ret
)));
618 return ADS_ERROR_KRB5(ret
);
621 /* parse the principal we got as a function argument */
622 ret
= smb_krb5_parse_name(context
, princ
, &principal
);
624 krb5_cc_close(context
, ccache
);
625 krb5_free_principal(context
, creds
.server
);
626 krb5_free_context(context
);
627 DEBUG(1,("Failed to parse %s (%s)\n", princ_name
, error_message(ret
)));
629 return ADS_ERROR_KRB5(ret
);
634 /* The creds.server principal takes ownership of this memory.
635 Remember to set back to original value before freeing. */
636 orig_realm
= *krb5_princ_realm(context
, creds
.server
);
637 krb5_princ_set_realm(context
, creds
.server
, krb5_princ_realm(context
, principal
));
639 ret
= krb5_cc_get_principal(context
, ccache
, &creds
.client
);
641 krb5_cc_close(context
, ccache
);
642 krb5_princ_set_realm(context
, creds
.server
, &orig_realm
);
643 krb5_free_principal(context
, creds
.server
);
644 krb5_free_principal(context
, principal
);
645 krb5_free_context(context
);
646 DEBUG(1,("Failed to get principal from ccache (%s)\n",
647 error_message(ret
)));
648 return ADS_ERROR_KRB5(ret
);
651 ret
= krb5_get_credentials(context
, 0, ccache
, &creds
, &credsp
);
653 krb5_cc_close(context
, ccache
);
654 krb5_free_principal(context
, creds
.client
);
655 krb5_princ_set_realm(context
, creds
.server
, &orig_realm
);
656 krb5_free_principal(context
, creds
.server
);
657 krb5_free_principal(context
, principal
);
658 krb5_free_context(context
);
659 DEBUG(1,("krb5_get_credentials failed (%s)\n", error_message(ret
)));
660 return ADS_ERROR_KRB5(ret
);
663 /* we might have to call krb5_free_creds(...) from now on ... */
665 aret
= do_krb5_kpasswd_request(context
, kdc_host
,
666 KRB5_KPASSWD_VERS_SETPW
,
667 credsp
, princ
, newpw
);
669 krb5_free_creds(context
, credsp
);
670 krb5_free_principal(context
, creds
.client
);
671 krb5_princ_set_realm(context
, creds
.server
, &orig_realm
);
672 krb5_free_principal(context
, creds
.server
);
673 krb5_free_principal(context
, principal
);
674 krb5_cc_close(context
, ccache
);
675 krb5_free_context(context
);
681 we use a prompter to avoid a crash bug in the kerberos libs when
682 dealing with empty passwords
683 this prompter is just a string copy ...
685 static krb5_error_code
686 kerb_prompter(krb5_context ctx
, void *data
,
690 krb5_prompt prompts
[])
692 if (num_prompts
== 0) return 0;
694 memset(prompts
[0].reply
->data
, 0, prompts
[0].reply
->length
);
695 if (prompts
[0].reply
->length
> 0) {
697 strncpy((char *)prompts
[0].reply
->data
,
699 prompts
[0].reply
->length
-1);
700 prompts
[0].reply
->length
= strlen((const char *)prompts
[0].reply
->data
);
702 prompts
[0].reply
->length
= 0;
708 static ADS_STATUS
ads_krb5_chg_password(const char *kdc_host
,
709 const char *principal
,
716 krb5_context context
= NULL
;
717 krb5_principal princ
;
718 krb5_get_init_creds_opt opts
;
720 char *chpw_princ
= NULL
, *password
;
722 initialize_krb5_error_table();
723 ret
= krb5_init_context(&context
);
725 DEBUG(1,("Failed to init krb5 context (%s)\n", error_message(ret
)));
726 return ADS_ERROR_KRB5(ret
);
729 if ((ret
= smb_krb5_parse_name(context
, principal
,
731 krb5_free_context(context
);
732 DEBUG(1,("Failed to parse %s (%s)\n", principal
, error_message(ret
)));
733 return ADS_ERROR_KRB5(ret
);
736 krb5_get_init_creds_opt_init(&opts
);
737 krb5_get_init_creds_opt_set_tkt_life(&opts
, 5*60);
738 krb5_get_init_creds_opt_set_renew_life(&opts
, 0);
739 krb5_get_init_creds_opt_set_forwardable(&opts
, 0);
740 krb5_get_init_creds_opt_set_proxiable(&opts
, 0);
742 /* We have to obtain an INITIAL changepw ticket for changing password */
743 if (asprintf(&chpw_princ
, "kadmin/changepw@%s",
744 (char *) krb5_princ_realm(context
, princ
)) == -1) {
745 krb5_free_context(context
);
746 DEBUG(1,("ads_krb5_chg_password: asprintf fail\n"));
747 return ADS_ERROR_NT(NT_STATUS_NO_MEMORY
);
750 password
= SMB_STRDUP(oldpw
);
751 ret
= krb5_get_init_creds_password(context
, &creds
, princ
, password
,
753 0, chpw_princ
, &opts
);
754 SAFE_FREE(chpw_princ
);
758 if (ret
== KRB5KRB_AP_ERR_BAD_INTEGRITY
)
759 DEBUG(1,("Password incorrect while getting initial ticket"));
761 DEBUG(1,("krb5_get_init_creds_password failed (%s)\n", error_message(ret
)));
763 krb5_free_principal(context
, princ
);
764 krb5_free_context(context
);
765 return ADS_ERROR_KRB5(ret
);
768 aret
= do_krb5_kpasswd_request(context
, kdc_host
,
769 KRB5_KPASSWD_VERS_CHANGEPW
,
770 &creds
, principal
, newpw
);
772 krb5_free_principal(context
, princ
);
773 krb5_free_context(context
);
779 ADS_STATUS
kerberos_set_password(const char *kpasswd_server
,
780 const char *auth_principal
, const char *auth_password
,
781 const char *target_principal
, const char *new_password
,
786 if ((ret
= kerberos_kinit_password(auth_principal
, auth_password
, time_offset
, NULL
))) {
787 DEBUG(1,("Failed kinit for principal %s (%s)\n", auth_principal
, error_message(ret
)));
788 return ADS_ERROR_KRB5(ret
);
791 if (!strcmp(auth_principal
, target_principal
))
792 return ads_krb5_chg_password(kpasswd_server
, target_principal
,
793 auth_password
, new_password
, time_offset
);
795 return ads_krb5_set_password(kpasswd_server
, target_principal
,
796 new_password
, time_offset
);
801 * Set the machine account password
802 * @param ads connection to ads server
803 * @param hostname machine whose password is being set
804 * @param password new password
805 * @return status of password change
807 ADS_STATUS
ads_set_machine_password(ADS_STRUCT
*ads
,
808 const char *machine_account
,
809 const char *password
)
812 char *principal
= NULL
;
815 we need to use the '$' form of the name here (the machine account name),
816 as otherwise the server might end up setting the password for a user
819 if (asprintf(&principal
, "%s@%s", machine_account
, ads
->config
.realm
) < 0) {
820 return ADS_ERROR_NT(NT_STATUS_NO_MEMORY
);
823 status
= ads_krb5_set_password(ads
->auth
.kdc_server
, principal
,
824 password
, ads
->auth
.time_offset
);
826 SAFE_FREE(principal
);