s3: Use talloc_tos() in idmap_nss_sids_to_unixids
[Samba.git] / source3 / libads / krb5_setpw.c
blobc919a257a48014205f1ffc1ae36216f86c51f12f
1 /*
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/>.
21 #include "includes.h"
22 #include "smb_krb5.h"
23 #include "libads/kerberos_proto.h"
25 #ifdef HAVE_KRB5
27 #define DEFAULT_KPASSWD_PORT 464
29 #define KRB5_KPASSWD_VERS_CHANGEPW 1
31 #define KRB5_KPASSWD_VERS_SETPW 0xff80
32 #define KRB5_KPASSWD_VERS_SETPW_ALT 2
34 #define KRB5_KPASSWD_SUCCESS 0
35 #define KRB5_KPASSWD_MALFORMED 1
36 #define KRB5_KPASSWD_HARDERROR 2
37 #define KRB5_KPASSWD_AUTHERROR 3
38 #define KRB5_KPASSWD_SOFTERROR 4
39 #define KRB5_KPASSWD_ACCESSDENIED 5
40 #define KRB5_KPASSWD_BAD_VERSION 6
41 #define KRB5_KPASSWD_INITIAL_FLAG_NEEDED 7
43 /* Those are defined by kerberos-set-passwd-02.txt and are probably
44 * not supported by M$ implementation */
45 #define KRB5_KPASSWD_POLICY_REJECT 8
46 #define KRB5_KPASSWD_BAD_PRINCIPAL 9
47 #define KRB5_KPASSWD_ETYPE_NOSUPP 10
50 * we've got to be able to distinguish KRB_ERRORs from other
51 * requests - valid response for CHPW v2 replies.
54 #define krb5_is_krb_error(packet) \
55 ( packet && packet->length && (((char *)packet->data)[0] == 0x7e || ((char *)packet->data)[0] == 0x5e))
57 /* This implements kerberos password change protocol as specified in
58 * kerb-chg-password-02.txt and kerberos-set-passwd-02.txt
59 * as well as microsoft version of the protocol
60 * as specified in kerberos-set-passwd-00.txt
62 static DATA_BLOB encode_krb5_setpw(const char *principal, const char *password)
64 char* princ_part1 = NULL;
65 char* princ_part2 = NULL;
66 char* realm = NULL;
67 char* c;
68 char* princ;
70 ASN1_DATA *req;
71 DATA_BLOB ret;
74 princ = SMB_STRDUP(principal);
76 if ((c = strchr_m(princ, '/')) == NULL) {
77 c = princ;
78 } else {
79 *c = '\0';
80 c++;
81 princ_part1 = princ;
84 princ_part2 = c;
86 if ((c = strchr_m(c, '@')) != NULL) {
87 *c = '\0';
88 c++;
89 realm = c;
90 } else {
91 /* We must have a realm component. */
92 return data_blob_null;
95 req = asn1_init(talloc_tos());
96 if (req == NULL) {
97 return data_blob_null;
100 asn1_push_tag(req, ASN1_SEQUENCE(0));
101 asn1_push_tag(req, ASN1_CONTEXT(0));
102 asn1_write_OctetString(req, password, strlen(password));
103 asn1_pop_tag(req);
105 asn1_push_tag(req, ASN1_CONTEXT(1));
106 asn1_push_tag(req, ASN1_SEQUENCE(0));
108 asn1_push_tag(req, ASN1_CONTEXT(0));
109 asn1_write_Integer(req, 1);
110 asn1_pop_tag(req);
112 asn1_push_tag(req, ASN1_CONTEXT(1));
113 asn1_push_tag(req, ASN1_SEQUENCE(0));
115 if (princ_part1) {
116 asn1_write_GeneralString(req, princ_part1);
119 asn1_write_GeneralString(req, princ_part2);
120 asn1_pop_tag(req);
121 asn1_pop_tag(req);
122 asn1_pop_tag(req);
123 asn1_pop_tag(req);
125 asn1_push_tag(req, ASN1_CONTEXT(2));
126 asn1_write_GeneralString(req, realm);
127 asn1_pop_tag(req);
128 asn1_pop_tag(req);
130 ret = data_blob(req->data, req->length);
131 asn1_free(req);
133 free(princ);
135 return ret;
138 static krb5_error_code build_kpasswd_request(uint16 pversion,
139 krb5_context context,
140 krb5_auth_context auth_context,
141 krb5_data *ap_req,
142 const char *princ,
143 const char *passwd,
144 bool use_tcp,
145 krb5_data *packet)
147 krb5_error_code ret;
148 krb5_data cipherpw;
149 krb5_data encoded_setpw;
150 krb5_replay_data replay;
151 char *p, *msg_start;
152 DATA_BLOB setpw;
153 unsigned int msg_length;
155 ret = krb5_auth_con_setflags(context,
156 auth_context,KRB5_AUTH_CONTEXT_DO_SEQUENCE);
157 if (ret) {
158 DEBUG(1,("krb5_auth_con_setflags failed (%s)\n",
159 error_message(ret)));
160 return ret;
163 /* handle protocol differences in chpw and setpw */
164 if (pversion == KRB5_KPASSWD_VERS_CHANGEPW)
165 setpw = data_blob(passwd, strlen(passwd));
166 else if (pversion == KRB5_KPASSWD_VERS_SETPW ||
167 pversion == KRB5_KPASSWD_VERS_SETPW_ALT)
168 setpw = encode_krb5_setpw(princ, passwd);
169 else
170 return EINVAL;
172 if (setpw.data == NULL || setpw.length == 0) {
173 return EINVAL;
176 encoded_setpw.data = (char *)setpw.data;
177 encoded_setpw.length = setpw.length;
179 ret = krb5_mk_priv(context, auth_context,
180 &encoded_setpw, &cipherpw, &replay);
182 data_blob_free(&setpw); /*from 'encode_krb5_setpw(...)' */
184 if (ret) {
185 DEBUG(1,("krb5_mk_priv failed (%s)\n", error_message(ret)));
186 return ret;
189 packet->data = (char *)SMB_MALLOC(ap_req->length + cipherpw.length + (use_tcp ? 10 : 6 ));
190 if (!packet->data)
191 return -1;
195 /* see the RFC for details */
197 msg_start = p = ((char *)packet->data) + (use_tcp ? 4 : 0);
198 p += 2;
199 RSSVAL(p, 0, pversion);
200 p += 2;
201 RSSVAL(p, 0, ap_req->length);
202 p += 2;
203 memcpy(p, ap_req->data, ap_req->length);
204 p += ap_req->length;
205 memcpy(p, cipherpw.data, cipherpw.length);
206 p += cipherpw.length;
207 packet->length = PTR_DIFF(p,packet->data);
208 msg_length = PTR_DIFF(p,msg_start);
210 if (use_tcp) {
211 RSIVAL(packet->data, 0, msg_length);
213 RSSVAL(msg_start, 0, msg_length);
215 free(cipherpw.data); /* from 'krb5_mk_priv(...)' */
217 return 0;
220 static const struct kpasswd_errors {
221 int result_code;
222 const char *error_string;
223 } kpasswd_errors[] = {
224 {KRB5_KPASSWD_MALFORMED, "Malformed request error"},
225 {KRB5_KPASSWD_HARDERROR, "Server error"},
226 {KRB5_KPASSWD_AUTHERROR, "Authentication error"},
227 {KRB5_KPASSWD_SOFTERROR, "Password change rejected"},
228 {KRB5_KPASSWD_ACCESSDENIED, "Client does not have proper authorization"},
229 {KRB5_KPASSWD_BAD_VERSION, "Protocol version not supported"},
230 {KRB5_KPASSWD_INITIAL_FLAG_NEEDED, "Authorization ticket must have initial flag set"},
231 {KRB5_KPASSWD_POLICY_REJECT, "Password rejected due to policy requirements"},
232 {KRB5_KPASSWD_BAD_PRINCIPAL, "Target principal does not exist"},
233 {KRB5_KPASSWD_ETYPE_NOSUPP, "Unsupported encryption type"},
234 {0, NULL}
237 static krb5_error_code setpw_result_code_string(krb5_context context,
238 int result_code,
239 const char **code_string)
241 unsigned int idx = 0;
243 while (kpasswd_errors[idx].error_string != NULL) {
244 if (kpasswd_errors[idx].result_code ==
245 result_code) {
246 *code_string = kpasswd_errors[idx].error_string;
247 return 0;
249 idx++;
251 *code_string = "Password change failed";
252 return (0);
255 krb5_error_code kpasswd_err_to_krb5_err(krb5_error_code res_code)
257 switch(res_code) {
258 case KRB5_KPASSWD_ACCESSDENIED:
259 return KRB5KDC_ERR_BADOPTION;
260 case KRB5_KPASSWD_INITIAL_FLAG_NEEDED:
261 return KRB5KDC_ERR_BADOPTION;
262 /* return KV5M_ALT_METHOD; MIT-only define */
263 case KRB5_KPASSWD_ETYPE_NOSUPP:
264 return KRB5KDC_ERR_ETYPE_NOSUPP;
265 case KRB5_KPASSWD_BAD_PRINCIPAL:
266 return KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
267 case KRB5_KPASSWD_POLICY_REJECT:
268 case KRB5_KPASSWD_SOFTERROR:
269 return KRB5KDC_ERR_POLICY;
270 default:
271 return KRB5KRB_ERR_GENERIC;
274 static krb5_error_code parse_setpw_reply(krb5_context context,
275 bool use_tcp,
276 krb5_auth_context auth_context,
277 krb5_data *packet)
279 krb5_data ap_rep;
280 char *p;
281 int vnum, ret, res_code;
282 krb5_data cipherresult;
283 krb5_data clearresult;
284 krb5_ap_rep_enc_part *ap_rep_enc;
285 krb5_replay_data replay;
286 unsigned int msg_length = packet->length;
289 if (packet->length < (use_tcp ? 8 : 4)) {
290 return KRB5KRB_AP_ERR_MODIFIED;
293 p = (char *)packet->data;
295 ** see if it is an error
297 if (krb5_is_krb_error(packet)) {
299 ret = handle_krberror_packet(context, packet);
300 if (ret) {
301 return ret;
306 /* tcp... */
307 if (use_tcp) {
308 msg_length -= 4;
309 if (RIVAL(p, 0) != msg_length) {
310 DEBUG(1,("Bad TCP packet length (%d/%d) from kpasswd server\n",
311 RIVAL(p, 0), msg_length));
312 return KRB5KRB_AP_ERR_MODIFIED;
315 p += 4;
318 if (RSVAL(p, 0) != msg_length) {
319 DEBUG(1,("Bad packet length (%d/%d) from kpasswd server\n",
320 RSVAL(p, 0), msg_length));
321 return KRB5KRB_AP_ERR_MODIFIED;
324 p += 2;
326 vnum = RSVAL(p, 0); p += 2;
328 /* FIXME: According to standard there is only one type of reply */
329 if (vnum != KRB5_KPASSWD_VERS_SETPW &&
330 vnum != KRB5_KPASSWD_VERS_SETPW_ALT &&
331 vnum != KRB5_KPASSWD_VERS_CHANGEPW) {
332 DEBUG(1,("Bad vnum (%d) from kpasswd server\n", vnum));
333 return KRB5KDC_ERR_BAD_PVNO;
336 ap_rep.length = RSVAL(p, 0); p += 2;
338 if (p + ap_rep.length >= (char *)packet->data + packet->length) {
339 DEBUG(1,("ptr beyond end of packet from kpasswd server\n"));
340 return KRB5KRB_AP_ERR_MODIFIED;
343 if (ap_rep.length == 0) {
344 DEBUG(1,("got unencrypted setpw result?!\n"));
345 return KRB5KRB_AP_ERR_MODIFIED;
348 /* verify ap_rep */
349 ap_rep.data = p;
350 p += ap_rep.length;
352 ret = krb5_rd_rep(context, auth_context, &ap_rep, &ap_rep_enc);
353 if (ret) {
354 DEBUG(1,("failed to rd setpw reply (%s)\n", error_message(ret)));
355 return KRB5KRB_AP_ERR_MODIFIED;
358 krb5_free_ap_rep_enc_part(context, ap_rep_enc);
360 cipherresult.data = p;
361 cipherresult.length = ((char *)packet->data + packet->length) - p;
363 ret = krb5_rd_priv(context, auth_context, &cipherresult, &clearresult,
364 &replay);
365 if (ret) {
366 DEBUG(1,("failed to decrypt setpw reply (%s)\n", error_message(ret)));
367 return KRB5KRB_AP_ERR_MODIFIED;
370 if (clearresult.length < 2) {
371 free(clearresult.data);
372 ret = KRB5KRB_AP_ERR_MODIFIED;
373 return KRB5KRB_AP_ERR_MODIFIED;
376 p = (char *)clearresult.data;
378 res_code = RSVAL(p, 0);
380 free(clearresult.data);
382 if ((res_code < KRB5_KPASSWD_SUCCESS) ||
383 (res_code > KRB5_KPASSWD_ETYPE_NOSUPP)) {
384 return KRB5KRB_AP_ERR_MODIFIED;
387 if (res_code == KRB5_KPASSWD_SUCCESS) {
388 return 0;
389 } else {
390 const char *errstr;
391 setpw_result_code_string(context, res_code, &errstr);
392 DEBUG(1, ("Error changing password: %s (%d)\n", errstr, res_code));
394 return kpasswd_err_to_krb5_err(res_code);
398 static ADS_STATUS do_krb5_kpasswd_request(krb5_context context,
399 const char *kdc_host,
400 uint16 pversion,
401 krb5_creds *credsp,
402 const char *princ,
403 const char *newpw)
405 krb5_auth_context auth_context = NULL;
406 krb5_data ap_req, chpw_req, chpw_rep;
407 int ret, sock;
408 socklen_t addr_len;
409 struct sockaddr_storage remote_addr, local_addr;
410 struct sockaddr_storage addr;
411 krb5_address local_kaddr, remote_kaddr;
412 bool use_tcp = False;
415 if (!interpret_string_addr(&addr, kdc_host, 0)) {
418 ret = krb5_mk_req_extended(context, &auth_context, AP_OPTS_USE_SUBKEY,
419 NULL, credsp, &ap_req);
420 if (ret) {
421 DEBUG(1,("krb5_mk_req_extended failed (%s)\n", error_message(ret)));
422 return ADS_ERROR_KRB5(ret);
425 do {
427 if (!use_tcp) {
429 sock = open_udp_socket(kdc_host, DEFAULT_KPASSWD_PORT);
430 if (sock == -1) {
431 int rc = errno;
432 SAFE_FREE(ap_req.data);
433 krb5_auth_con_free(context, auth_context);
434 DEBUG(1,("failed to open kpasswd socket to %s "
435 "(%s)\n", kdc_host, strerror(errno)));
436 return ADS_ERROR_SYSTEM(rc);
438 } else {
439 NTSTATUS status;
440 status = open_socket_out(&addr, DEFAULT_KPASSWD_PORT,
441 LONG_CONNECT_TIMEOUT, &sock);
442 if (!NT_STATUS_IS_OK(status)) {
443 SAFE_FREE(ap_req.data);
444 krb5_auth_con_free(context, auth_context);
445 DEBUG(1,("failed to open kpasswd socket to %s "
446 "(%s)\n", kdc_host,
447 nt_errstr(status)));
448 return ADS_ERROR_NT(status);
452 addr_len = sizeof(remote_addr);
453 if (getpeername(sock, (struct sockaddr *)&remote_addr, &addr_len) != 0) {
454 close(sock);
455 SAFE_FREE(ap_req.data);
456 krb5_auth_con_free(context, auth_context);
457 DEBUG(1,("getpeername() failed (%s)\n", error_message(errno)));
458 return ADS_ERROR_SYSTEM(errno);
460 addr_len = sizeof(local_addr);
461 if (getsockname(sock, (struct sockaddr *)&local_addr, &addr_len) != 0) {
462 close(sock);
463 SAFE_FREE(ap_req.data);
464 krb5_auth_con_free(context, auth_context);
465 DEBUG(1,("getsockname() failed (%s)\n", error_message(errno)));
466 return ADS_ERROR_SYSTEM(errno);
468 if (!setup_kaddr(&remote_kaddr, &remote_addr) ||
469 !setup_kaddr(&local_kaddr, &local_addr)) {
470 DEBUG(1,("do_krb5_kpasswd_request: "
471 "Failed to setup addresses.\n"));
472 close(sock);
473 SAFE_FREE(ap_req.data);
474 krb5_auth_con_free(context, auth_context);
475 errno = EINVAL;
476 return ADS_ERROR_SYSTEM(EINVAL);
479 ret = krb5_auth_con_setaddrs(context, auth_context, &local_kaddr, NULL);
480 if (ret) {
481 close(sock);
482 SAFE_FREE(ap_req.data);
483 krb5_auth_con_free(context, auth_context);
484 DEBUG(1,("krb5_auth_con_setaddrs failed (%s)\n", error_message(ret)));
485 return ADS_ERROR_KRB5(ret);
488 ret = build_kpasswd_request(pversion, context, auth_context, &ap_req,
489 princ, newpw, use_tcp, &chpw_req);
490 if (ret) {
491 close(sock);
492 SAFE_FREE(ap_req.data);
493 krb5_auth_con_free(context, auth_context);
494 DEBUG(1,("build_setpw_request failed (%s)\n", error_message(ret)));
495 return ADS_ERROR_KRB5(ret);
498 ret = write(sock, chpw_req.data, chpw_req.length);
500 if (ret != chpw_req.length) {
501 close(sock);
502 SAFE_FREE(chpw_req.data);
503 SAFE_FREE(ap_req.data);
504 krb5_auth_con_free(context, auth_context);
505 DEBUG(1,("send of chpw failed (%s)\n", strerror(errno)));
506 return ADS_ERROR_SYSTEM(errno);
509 SAFE_FREE(chpw_req.data);
511 chpw_rep.length = 1500;
512 chpw_rep.data = (char *) SMB_MALLOC(chpw_rep.length);
513 if (!chpw_rep.data) {
514 close(sock);
515 SAFE_FREE(ap_req.data);
516 krb5_auth_con_free(context, auth_context);
517 DEBUG(1,("send of chpw failed (%s)\n", strerror(errno)));
518 errno = ENOMEM;
519 return ADS_ERROR_SYSTEM(errno);
522 ret = read(sock, chpw_rep.data, chpw_rep.length);
523 if (ret < 0) {
524 close(sock);
525 SAFE_FREE(chpw_rep.data);
526 SAFE_FREE(ap_req.data);
527 krb5_auth_con_free(context, auth_context);
528 DEBUG(1,("recv of chpw reply failed (%s)\n", strerror(errno)));
529 return ADS_ERROR_SYSTEM(errno);
532 close(sock);
533 chpw_rep.length = ret;
535 ret = krb5_auth_con_setaddrs(context, auth_context, NULL,&remote_kaddr);
536 if (ret) {
537 SAFE_FREE(chpw_rep.data);
538 SAFE_FREE(ap_req.data);
539 krb5_auth_con_free(context, auth_context);
540 DEBUG(1,("krb5_auth_con_setaddrs on reply failed (%s)\n",
541 error_message(ret)));
542 return ADS_ERROR_KRB5(ret);
545 ret = parse_setpw_reply(context, use_tcp, auth_context, &chpw_rep);
546 SAFE_FREE(chpw_rep.data);
548 if (ret) {
550 if (ret == KRB5KRB_ERR_RESPONSE_TOO_BIG && !use_tcp) {
551 DEBUG(5, ("Trying setpw with TCP!!!\n"));
552 use_tcp = True;
553 continue;
556 SAFE_FREE(ap_req.data);
557 krb5_auth_con_free(context, auth_context);
558 DEBUG(1,("parse_setpw_reply failed (%s)\n",
559 error_message(ret)));
560 return ADS_ERROR_KRB5(ret);
563 SAFE_FREE(ap_req.data);
564 krb5_auth_con_free(context, auth_context);
565 } while ( ret );
567 return ADS_SUCCESS;
570 ADS_STATUS ads_krb5_set_password(const char *kdc_host, const char *princ,
571 const char *newpw, int time_offset)
574 ADS_STATUS aret;
575 krb5_error_code ret = 0;
576 krb5_context context = NULL;
577 const char *realm = NULL;
578 unsigned int realm_len = 0;
579 krb5_creds creds, *credsp = NULL;
580 krb5_ccache ccache = NULL;
582 ZERO_STRUCT(creds);
584 initialize_krb5_error_table();
585 ret = krb5_init_context(&context);
586 if (ret) {
587 DEBUG(1,("Failed to init krb5 context (%s)\n", error_message(ret)));
588 return ADS_ERROR_KRB5(ret);
591 if (time_offset != 0) {
592 krb5_set_real_time(context, time(NULL) + time_offset, 0);
595 ret = krb5_cc_default(context, &ccache);
596 if (ret) {
597 krb5_free_context(context);
598 DEBUG(1,("Failed to get default creds (%s)\n", error_message(ret)));
599 return ADS_ERROR_KRB5(ret);
602 ret = krb5_cc_get_principal(context, ccache, &creds.client);
603 if (ret) {
604 krb5_cc_close(context, ccache);
605 krb5_free_context(context);
606 DEBUG(1,("Failed to get principal from ccache (%s)\n",
607 error_message(ret)));
608 return ADS_ERROR_KRB5(ret);
611 realm = smb_krb5_principal_get_realm(context, creds.client);
612 realm_len = strlen(realm);
613 ret = krb5_build_principal(context,
614 &creds.server,
615 realm_len,
616 realm, "kadmin", "changepw", NULL);
618 ret = krb5_get_credentials(context, 0, ccache, &creds, &credsp);
619 if (ret) {
620 krb5_cc_close(context, ccache);
621 krb5_free_principal(context, creds.client);
622 krb5_free_principal(context, creds.server);
623 krb5_free_context(context);
624 DEBUG(1,("krb5_build_prinipal_ext (%s)\n", error_message(ret)));
625 return ADS_ERROR_KRB5(ret);
628 ret = krb5_get_credentials(context, 0, ccache, &creds, &credsp);
629 if (ret) {
630 krb5_cc_close(context, ccache);
631 krb5_free_principal(context, creds.client);
632 krb5_free_principal(context, creds.server);
633 krb5_free_context(context);
634 DEBUG(1,("krb5_get_credentials failed (%s)\n", error_message(ret)));
635 return ADS_ERROR_KRB5(ret);
638 /* we might have to call krb5_free_creds(...) from now on ... */
640 aret = do_krb5_kpasswd_request(context, kdc_host,
641 KRB5_KPASSWD_VERS_SETPW,
642 credsp, princ, newpw);
644 krb5_free_creds(context, credsp);
645 krb5_free_principal(context, creds.client);
646 krb5_free_principal(context, creds.server);
647 krb5_cc_close(context, ccache);
648 krb5_free_context(context);
650 return aret;
654 we use a prompter to avoid a crash bug in the kerberos libs when
655 dealing with empty passwords
656 this prompter is just a string copy ...
658 static krb5_error_code
659 kerb_prompter(krb5_context ctx, void *data,
660 const char *name,
661 const char *banner,
662 int num_prompts,
663 krb5_prompt prompts[])
665 if (num_prompts == 0) return 0;
667 memset(prompts[0].reply->data, 0, prompts[0].reply->length);
668 if (prompts[0].reply->length > 0) {
669 if (data) {
670 strncpy((char *)prompts[0].reply->data,
671 (const char *)data,
672 prompts[0].reply->length-1);
673 prompts[0].reply->length = strlen((const char *)prompts[0].reply->data);
674 } else {
675 prompts[0].reply->length = 0;
678 return 0;
681 static ADS_STATUS ads_krb5_chg_password(const char *kdc_host,
682 const char *principal,
683 const char *oldpw,
684 const char *newpw,
685 int time_offset)
687 ADS_STATUS aret;
688 krb5_error_code ret;
689 krb5_context context = NULL;
690 krb5_principal princ;
691 krb5_get_init_creds_opt opts;
692 krb5_creds creds;
693 char *chpw_princ = NULL, *password;
694 const char *realm = NULL;
696 initialize_krb5_error_table();
697 ret = krb5_init_context(&context);
698 if (ret) {
699 DEBUG(1,("Failed to init krb5 context (%s)\n", error_message(ret)));
700 return ADS_ERROR_KRB5(ret);
703 if ((ret = smb_krb5_parse_name(context, principal,
704 &princ))) {
705 krb5_free_context(context);
706 DEBUG(1,("Failed to parse %s (%s)\n", principal, error_message(ret)));
707 return ADS_ERROR_KRB5(ret);
710 krb5_get_init_creds_opt_init(&opts);
711 krb5_get_init_creds_opt_set_tkt_life(&opts, 5*60);
712 krb5_get_init_creds_opt_set_renew_life(&opts, 0);
713 krb5_get_init_creds_opt_set_forwardable(&opts, 0);
714 krb5_get_init_creds_opt_set_proxiable(&opts, 0);
716 realm = smb_krb5_principal_get_realm(context, princ);
718 /* We have to obtain an INITIAL changepw ticket for changing password */
719 if (asprintf(&chpw_princ, "kadmin/changepw@%s", realm) == -1) {
720 krb5_free_context(context);
721 DEBUG(1,("ads_krb5_chg_password: asprintf fail\n"));
722 return ADS_ERROR_NT(NT_STATUS_NO_MEMORY);
725 password = SMB_STRDUP(oldpw);
726 ret = krb5_get_init_creds_password(context, &creds, princ, password,
727 kerb_prompter, NULL,
728 0, chpw_princ, &opts);
729 SAFE_FREE(chpw_princ);
730 SAFE_FREE(password);
732 if (ret) {
733 if (ret == KRB5KRB_AP_ERR_BAD_INTEGRITY)
734 DEBUG(1,("Password incorrect while getting initial ticket"));
735 else
736 DEBUG(1,("krb5_get_init_creds_password failed (%s)\n", error_message(ret)));
738 krb5_free_principal(context, princ);
739 krb5_free_context(context);
740 return ADS_ERROR_KRB5(ret);
743 aret = do_krb5_kpasswd_request(context, kdc_host,
744 KRB5_KPASSWD_VERS_CHANGEPW,
745 &creds, principal, newpw);
747 krb5_free_principal(context, princ);
748 krb5_free_context(context);
750 return aret;
754 ADS_STATUS kerberos_set_password(const char *kpasswd_server,
755 const char *auth_principal, const char *auth_password,
756 const char *target_principal, const char *new_password,
757 int time_offset)
759 int ret;
761 if ((ret = kerberos_kinit_password(auth_principal, auth_password, time_offset, NULL))) {
762 DEBUG(1,("Failed kinit for principal %s (%s)\n", auth_principal, error_message(ret)));
763 return ADS_ERROR_KRB5(ret);
766 if (!strcmp(auth_principal, target_principal))
767 return ads_krb5_chg_password(kpasswd_server, target_principal,
768 auth_password, new_password, time_offset);
769 else
770 return ads_krb5_set_password(kpasswd_server, target_principal,
771 new_password, time_offset);
774 #endif