Remove trailing withespace from wbinfo -m which breaks gdm auth.
[Samba/bb.git] / source3 / libads / krb5_setpw.c
blob719f3bd3ecf16dada423f61db78173de18c2eb01
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"
23 #ifdef HAVE_KRB5
25 #define DEFAULT_KPASSWD_PORT 464
27 #define KRB5_KPASSWD_VERS_CHANGEPW 1
29 #define KRB5_KPASSWD_VERS_SETPW 0xff80
30 #define KRB5_KPASSWD_VERS_SETPW_ALT 2
32 #define KRB5_KPASSWD_SUCCESS 0
33 #define KRB5_KPASSWD_MALFORMED 1
34 #define KRB5_KPASSWD_HARDERROR 2
35 #define KRB5_KPASSWD_AUTHERROR 3
36 #define KRB5_KPASSWD_SOFTERROR 4
37 #define KRB5_KPASSWD_ACCESSDENIED 5
38 #define KRB5_KPASSWD_BAD_VERSION 6
39 #define KRB5_KPASSWD_INITIAL_FLAG_NEEDED 7
41 /* Those are defined by kerberos-set-passwd-02.txt and are probably
42 * not supported by M$ implementation */
43 #define KRB5_KPASSWD_POLICY_REJECT 8
44 #define KRB5_KPASSWD_BAD_PRINCIPAL 9
45 #define KRB5_KPASSWD_ETYPE_NOSUPP 10
48 * we've got to be able to distinguish KRB_ERRORs from other
49 * requests - valid response for CHPW v2 replies.
52 #define krb5_is_krb_error(packet) \
53 ( packet && packet->length && (((char *)packet->data)[0] == 0x7e || ((char *)packet->data)[0] == 0x5e))
55 /* This implements kerberos password change protocol as specified in
56 * kerb-chg-password-02.txt and kerberos-set-passwd-02.txt
57 * as well as microsoft version of the protocol
58 * as specified in kerberos-set-passwd-00.txt
60 static DATA_BLOB encode_krb5_setpw(const char *principal, const char *password)
62 char* princ_part1 = NULL;
63 char* princ_part2 = NULL;
64 char* realm = NULL;
65 char* c;
66 char* princ;
68 ASN1_DATA req;
69 DATA_BLOB ret;
72 princ = SMB_STRDUP(principal);
74 if ((c = strchr_m(princ, '/')) == NULL) {
75 c = princ;
76 } else {
77 *c = '\0';
78 c++;
79 princ_part1 = princ;
82 princ_part2 = c;
84 if ((c = strchr_m(c, '@')) != NULL) {
85 *c = '\0';
86 c++;
87 realm = c;
88 } else {
89 /* We must have a realm component. */
90 return data_blob_null;
93 memset(&req, 0, sizeof(req));
95 asn1_push_tag(&req, ASN1_SEQUENCE(0));
96 asn1_push_tag(&req, ASN1_CONTEXT(0));
97 asn1_write_OctetString(&req, password, strlen(password));
98 asn1_pop_tag(&req);
100 asn1_push_tag(&req, ASN1_CONTEXT(1));
101 asn1_push_tag(&req, ASN1_SEQUENCE(0));
103 asn1_push_tag(&req, ASN1_CONTEXT(0));
104 asn1_write_Integer(&req, 1);
105 asn1_pop_tag(&req);
107 asn1_push_tag(&req, ASN1_CONTEXT(1));
108 asn1_push_tag(&req, ASN1_SEQUENCE(0));
110 if (princ_part1) {
111 asn1_write_GeneralString(&req, princ_part1);
114 asn1_write_GeneralString(&req, princ_part2);
115 asn1_pop_tag(&req);
116 asn1_pop_tag(&req);
117 asn1_pop_tag(&req);
118 asn1_pop_tag(&req);
120 asn1_push_tag(&req, ASN1_CONTEXT(2));
121 asn1_write_GeneralString(&req, realm);
122 asn1_pop_tag(&req);
123 asn1_pop_tag(&req);
125 ret = data_blob(req.data, req.length);
126 asn1_free(&req);
128 free(princ);
130 return ret;
133 static krb5_error_code build_kpasswd_request(uint16 pversion,
134 krb5_context context,
135 krb5_auth_context auth_context,
136 krb5_data *ap_req,
137 const char *princ,
138 const char *passwd,
139 bool use_tcp,
140 krb5_data *packet)
142 krb5_error_code ret;
143 krb5_data cipherpw;
144 krb5_data encoded_setpw;
145 krb5_replay_data replay;
146 char *p, *msg_start;
147 DATA_BLOB setpw;
148 unsigned int msg_length;
150 ret = krb5_auth_con_setflags(context,
151 auth_context,KRB5_AUTH_CONTEXT_DO_SEQUENCE);
152 if (ret) {
153 DEBUG(1,("krb5_auth_con_setflags failed (%s)\n",
154 error_message(ret)));
155 return ret;
158 /* handle protocol differences in chpw and setpw */
159 if (pversion == KRB5_KPASSWD_VERS_CHANGEPW)
160 setpw = data_blob(passwd, strlen(passwd));
161 else if (pversion == KRB5_KPASSWD_VERS_SETPW ||
162 pversion == KRB5_KPASSWD_VERS_SETPW_ALT)
163 setpw = encode_krb5_setpw(princ, passwd);
164 else
165 return EINVAL;
167 if (setpw.data == NULL || setpw.length == 0) {
168 return EINVAL;
171 encoded_setpw.data = (char *)setpw.data;
172 encoded_setpw.length = setpw.length;
174 ret = krb5_mk_priv(context, auth_context,
175 &encoded_setpw, &cipherpw, &replay);
177 data_blob_free(&setpw); /*from 'encode_krb5_setpw(...)' */
179 if (ret) {
180 DEBUG(1,("krb5_mk_priv failed (%s)\n", error_message(ret)));
181 return ret;
184 packet->data = (char *)SMB_MALLOC(ap_req->length + cipherpw.length + (use_tcp ? 10 : 6 ));
185 if (!packet->data)
186 return -1;
190 /* see the RFC for details */
192 msg_start = p = ((char *)packet->data) + (use_tcp ? 4 : 0);
193 p += 2;
194 RSSVAL(p, 0, pversion);
195 p += 2;
196 RSSVAL(p, 0, ap_req->length);
197 p += 2;
198 memcpy(p, ap_req->data, ap_req->length);
199 p += ap_req->length;
200 memcpy(p, cipherpw.data, cipherpw.length);
201 p += cipherpw.length;
202 packet->length = PTR_DIFF(p,packet->data);
203 msg_length = PTR_DIFF(p,msg_start);
205 if (use_tcp) {
206 RSIVAL(packet->data, 0, msg_length);
208 RSSVAL(msg_start, 0, msg_length);
210 free(cipherpw.data); /* from 'krb5_mk_priv(...)' */
212 return 0;
215 static const struct kpasswd_errors {
216 int result_code;
217 const char *error_string;
218 } kpasswd_errors[] = {
219 {KRB5_KPASSWD_MALFORMED, "Malformed request error"},
220 {KRB5_KPASSWD_HARDERROR, "Server error"},
221 {KRB5_KPASSWD_AUTHERROR, "Authentication error"},
222 {KRB5_KPASSWD_SOFTERROR, "Password change rejected"},
223 {KRB5_KPASSWD_ACCESSDENIED, "Client does not have proper authorization"},
224 {KRB5_KPASSWD_BAD_VERSION, "Protocol version not supported"},
225 {KRB5_KPASSWD_INITIAL_FLAG_NEEDED, "Authorization ticket must have initial flag set"},
226 {KRB5_KPASSWD_POLICY_REJECT, "Password rejected due to policy requirements"},
227 {KRB5_KPASSWD_BAD_PRINCIPAL, "Target principal does not exist"},
228 {KRB5_KPASSWD_ETYPE_NOSUPP, "Unsupported encryption type"},
229 {0, NULL}
232 static krb5_error_code setpw_result_code_string(krb5_context context,
233 int result_code,
234 const char **code_string)
236 unsigned int idx = 0;
238 while (kpasswd_errors[idx].error_string != NULL) {
239 if (kpasswd_errors[idx].result_code ==
240 result_code) {
241 *code_string = kpasswd_errors[idx].error_string;
242 return 0;
244 idx++;
246 *code_string = "Password change failed";
247 return (0);
250 krb5_error_code kpasswd_err_to_krb5_err(krb5_error_code res_code)
252 switch(res_code) {
253 case KRB5_KPASSWD_ACCESSDENIED:
254 return KRB5KDC_ERR_BADOPTION;
255 case KRB5_KPASSWD_INITIAL_FLAG_NEEDED:
256 return KRB5KDC_ERR_BADOPTION;
257 /* return KV5M_ALT_METHOD; MIT-only define */
258 case KRB5_KPASSWD_ETYPE_NOSUPP:
259 return KRB5KDC_ERR_ETYPE_NOSUPP;
260 case KRB5_KPASSWD_BAD_PRINCIPAL:
261 return KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
262 case KRB5_KPASSWD_POLICY_REJECT:
263 case KRB5_KPASSWD_SOFTERROR:
264 return KRB5KDC_ERR_POLICY;
265 default:
266 return KRB5KRB_ERR_GENERIC;
269 static krb5_error_code parse_setpw_reply(krb5_context context,
270 bool use_tcp,
271 krb5_auth_context auth_context,
272 krb5_data *packet)
274 krb5_data ap_rep;
275 char *p;
276 int vnum, ret, res_code;
277 krb5_data cipherresult;
278 krb5_data clearresult;
279 krb5_ap_rep_enc_part *ap_rep_enc;
280 krb5_replay_data replay;
281 unsigned int msg_length = packet->length;
284 if (packet->length < (use_tcp ? 8 : 4)) {
285 return KRB5KRB_AP_ERR_MODIFIED;
288 p = packet->data;
290 ** see if it is an error
292 if (krb5_is_krb_error(packet)) {
294 ret = handle_krberror_packet(context, packet);
295 if (ret) {
296 return ret;
301 /* tcp... */
302 if (use_tcp) {
303 msg_length -= 4;
304 if (RIVAL(p, 0) != msg_length) {
305 DEBUG(1,("Bad TCP packet length (%d/%d) from kpasswd server\n",
306 RIVAL(p, 0), msg_length));
307 return KRB5KRB_AP_ERR_MODIFIED;
310 p += 4;
313 if (RSVAL(p, 0) != msg_length) {
314 DEBUG(1,("Bad packet length (%d/%d) from kpasswd server\n",
315 RSVAL(p, 0), msg_length));
316 return KRB5KRB_AP_ERR_MODIFIED;
319 p += 2;
321 vnum = RSVAL(p, 0); p += 2;
323 /* FIXME: According to standard there is only one type of reply */
324 if (vnum != KRB5_KPASSWD_VERS_SETPW &&
325 vnum != KRB5_KPASSWD_VERS_SETPW_ALT &&
326 vnum != KRB5_KPASSWD_VERS_CHANGEPW) {
327 DEBUG(1,("Bad vnum (%d) from kpasswd server\n", vnum));
328 return KRB5KDC_ERR_BAD_PVNO;
331 ap_rep.length = RSVAL(p, 0); p += 2;
333 if (p + ap_rep.length >= (char *)packet->data + packet->length) {
334 DEBUG(1,("ptr beyond end of packet from kpasswd server\n"));
335 return KRB5KRB_AP_ERR_MODIFIED;
338 if (ap_rep.length == 0) {
339 DEBUG(1,("got unencrypted setpw result?!\n"));
340 return KRB5KRB_AP_ERR_MODIFIED;
343 /* verify ap_rep */
344 ap_rep.data = p;
345 p += ap_rep.length;
347 ret = krb5_rd_rep(context, auth_context, &ap_rep, &ap_rep_enc);
348 if (ret) {
349 DEBUG(1,("failed to rd setpw reply (%s)\n", error_message(ret)));
350 return KRB5KRB_AP_ERR_MODIFIED;
353 krb5_free_ap_rep_enc_part(context, ap_rep_enc);
355 cipherresult.data = p;
356 cipherresult.length = ((char *)packet->data + packet->length) - p;
358 ret = krb5_rd_priv(context, auth_context, &cipherresult, &clearresult,
359 &replay);
360 if (ret) {
361 DEBUG(1,("failed to decrypt setpw reply (%s)\n", error_message(ret)));
362 return KRB5KRB_AP_ERR_MODIFIED;
365 if (clearresult.length < 2) {
366 free(clearresult.data);
367 ret = KRB5KRB_AP_ERR_MODIFIED;
368 return KRB5KRB_AP_ERR_MODIFIED;
371 p = clearresult.data;
373 res_code = RSVAL(p, 0);
375 free(clearresult.data);
377 if ((res_code < KRB5_KPASSWD_SUCCESS) ||
378 (res_code > KRB5_KPASSWD_ETYPE_NOSUPP)) {
379 return KRB5KRB_AP_ERR_MODIFIED;
382 if (res_code == KRB5_KPASSWD_SUCCESS) {
383 return 0;
384 } else {
385 const char *errstr;
386 setpw_result_code_string(context, res_code, &errstr);
387 DEBUG(1, ("Error changing password: %s (%d)\n", errstr, res_code));
389 return kpasswd_err_to_krb5_err(res_code);
393 static ADS_STATUS do_krb5_kpasswd_request(krb5_context context,
394 const char *kdc_host,
395 uint16 pversion,
396 krb5_creds *credsp,
397 const char *princ,
398 const char *newpw)
400 krb5_auth_context auth_context = NULL;
401 krb5_data ap_req, chpw_req, chpw_rep;
402 int ret, sock;
403 socklen_t addr_len;
404 struct sockaddr_storage remote_addr, local_addr;
405 struct sockaddr_storage addr;
406 krb5_address local_kaddr, remote_kaddr;
407 bool use_tcp = False;
410 if (!interpret_string_addr(&addr, kdc_host, 0)) {
413 ret = krb5_mk_req_extended(context, &auth_context, AP_OPTS_USE_SUBKEY,
414 NULL, credsp, &ap_req);
415 if (ret) {
416 DEBUG(1,("krb5_mk_req_extended failed (%s)\n", error_message(ret)));
417 return ADS_ERROR_KRB5(ret);
420 do {
422 if (!use_tcp) {
424 sock = open_udp_socket(kdc_host, DEFAULT_KPASSWD_PORT);
426 } else {
428 sock = open_socket_out(SOCK_STREAM, &addr, DEFAULT_KPASSWD_PORT,
429 LONG_CONNECT_TIMEOUT);
432 if (sock == -1) {
433 int rc = errno;
434 SAFE_FREE(ap_req.data);
435 krb5_auth_con_free(context, auth_context);
436 DEBUG(1,("failed to open kpasswd socket to %s (%s)\n",
437 kdc_host, strerror(errno)));
438 return ADS_ERROR_SYSTEM(rc);
440 addr_len = sizeof(remote_addr);
441 if (getpeername(sock, (struct sockaddr *)&remote_addr, &addr_len) != 0) {
442 close(sock);
443 SAFE_FREE(ap_req.data);
444 krb5_auth_con_free(context, auth_context);
445 DEBUG(1,("getpeername() failed (%s)\n", error_message(errno)));
446 return ADS_ERROR_SYSTEM(errno);
448 addr_len = sizeof(local_addr);
449 if (getsockname(sock, (struct sockaddr *)&local_addr, &addr_len) != 0) {
450 close(sock);
451 SAFE_FREE(ap_req.data);
452 krb5_auth_con_free(context, auth_context);
453 DEBUG(1,("getsockname() failed (%s)\n", error_message(errno)));
454 return ADS_ERROR_SYSTEM(errno);
456 if (!setup_kaddr(&remote_kaddr, &remote_addr) ||
457 !setup_kaddr(&local_kaddr, &local_addr)) {
458 DEBUG(1,("do_krb5_kpasswd_request: "
459 "Failed to setup addresses.\n"));
460 close(sock);
461 SAFE_FREE(ap_req.data);
462 krb5_auth_con_free(context, auth_context);
463 errno = EINVAL;
464 return ADS_ERROR_SYSTEM(EINVAL);
467 ret = krb5_auth_con_setaddrs(context, auth_context, &local_kaddr, NULL);
468 if (ret) {
469 close(sock);
470 SAFE_FREE(ap_req.data);
471 krb5_auth_con_free(context, auth_context);
472 DEBUG(1,("krb5_auth_con_setaddrs failed (%s)\n", error_message(ret)));
473 return ADS_ERROR_KRB5(ret);
476 ret = build_kpasswd_request(pversion, context, auth_context, &ap_req,
477 princ, newpw, use_tcp, &chpw_req);
478 if (ret) {
479 close(sock);
480 SAFE_FREE(ap_req.data);
481 krb5_auth_con_free(context, auth_context);
482 DEBUG(1,("build_setpw_request failed (%s)\n", error_message(ret)));
483 return ADS_ERROR_KRB5(ret);
486 ret = write(sock, chpw_req.data, chpw_req.length);
488 if (ret != chpw_req.length) {
489 close(sock);
490 SAFE_FREE(chpw_req.data);
491 SAFE_FREE(ap_req.data);
492 krb5_auth_con_free(context, auth_context);
493 DEBUG(1,("send of chpw failed (%s)\n", strerror(errno)));
494 return ADS_ERROR_SYSTEM(errno);
497 SAFE_FREE(chpw_req.data);
499 chpw_rep.length = 1500;
500 chpw_rep.data = (char *) SMB_MALLOC(chpw_rep.length);
501 if (!chpw_rep.data) {
502 close(sock);
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 errno = ENOMEM;
507 return ADS_ERROR_SYSTEM(errno);
510 ret = read(sock, chpw_rep.data, chpw_rep.length);
511 if (ret < 0) {
512 close(sock);
513 SAFE_FREE(chpw_rep.data);
514 SAFE_FREE(ap_req.data);
515 krb5_auth_con_free(context, auth_context);
516 DEBUG(1,("recv of chpw reply failed (%s)\n", strerror(errno)));
517 return ADS_ERROR_SYSTEM(errno);
520 close(sock);
521 chpw_rep.length = ret;
523 ret = krb5_auth_con_setaddrs(context, auth_context, NULL,&remote_kaddr);
524 if (ret) {
525 SAFE_FREE(chpw_rep.data);
526 SAFE_FREE(ap_req.data);
527 krb5_auth_con_free(context, auth_context);
528 DEBUG(1,("krb5_auth_con_setaddrs on reply failed (%s)\n",
529 error_message(ret)));
530 return ADS_ERROR_KRB5(ret);
533 ret = parse_setpw_reply(context, use_tcp, auth_context, &chpw_rep);
534 SAFE_FREE(chpw_rep.data);
536 if (ret) {
538 if (ret == KRB5KRB_ERR_RESPONSE_TOO_BIG && !use_tcp) {
539 DEBUG(5, ("Trying setpw with TCP!!!\n"));
540 use_tcp = True;
541 continue;
544 SAFE_FREE(ap_req.data);
545 krb5_auth_con_free(context, auth_context);
546 DEBUG(1,("parse_setpw_reply failed (%s)\n",
547 error_message(ret)));
548 return ADS_ERROR_KRB5(ret);
551 SAFE_FREE(ap_req.data);
552 krb5_auth_con_free(context, auth_context);
553 } while ( ret );
555 return ADS_SUCCESS;
558 ADS_STATUS ads_krb5_set_password(const char *kdc_host, const char *princ,
559 const char *newpw, int time_offset)
562 ADS_STATUS aret;
563 krb5_error_code ret = 0;
564 krb5_context context = NULL;
565 krb5_principal principal = NULL;
566 char *princ_name = NULL;
567 char *realm = NULL;
568 krb5_creds creds, *credsp = NULL;
569 #if KRB5_PRINC_REALM_RETURNS_REALM
570 krb5_realm orig_realm;
571 #else
572 krb5_data orig_realm;
573 #endif
574 krb5_ccache ccache = NULL;
576 ZERO_STRUCT(creds);
578 initialize_krb5_error_table();
579 ret = krb5_init_context(&context);
580 if (ret) {
581 DEBUG(1,("Failed to init krb5 context (%s)\n", error_message(ret)));
582 return ADS_ERROR_KRB5(ret);
585 if (time_offset != 0) {
586 krb5_set_real_time(context, time(NULL) + time_offset, 0);
589 ret = krb5_cc_default(context, &ccache);
590 if (ret) {
591 krb5_free_context(context);
592 DEBUG(1,("Failed to get default creds (%s)\n", error_message(ret)));
593 return ADS_ERROR_KRB5(ret);
596 realm = strchr_m(princ, '@');
597 if (!realm) {
598 krb5_cc_close(context, ccache);
599 krb5_free_context(context);
600 DEBUG(1,("Failed to get realm\n"));
601 return ADS_ERROR_KRB5(-1);
603 realm++;
605 asprintf(&princ_name, "kadmin/changepw@%s", realm);
606 ret = smb_krb5_parse_name(context, princ_name, &creds.server);
607 if (ret) {
608 krb5_cc_close(context, ccache);
609 krb5_free_context(context);
610 DEBUG(1,("Failed to parse kadmin/changepw (%s)\n", error_message(ret)));
611 return ADS_ERROR_KRB5(ret);
614 /* parse the principal we got as a function argument */
615 ret = smb_krb5_parse_name(context, princ, &principal);
616 if (ret) {
617 krb5_cc_close(context, ccache);
618 krb5_free_principal(context, creds.server);
619 krb5_free_context(context);
620 DEBUG(1,("Failed to parse %s (%s)\n", princ_name, error_message(ret)));
621 free(princ_name);
622 return ADS_ERROR_KRB5(ret);
625 free(princ_name);
627 /* The creds.server principal takes ownership of this memory.
628 Remember to set back to original value before freeing. */
629 orig_realm = *krb5_princ_realm(context, creds.server);
630 krb5_princ_set_realm(context, creds.server, krb5_princ_realm(context, principal));
632 ret = krb5_cc_get_principal(context, ccache, &creds.client);
633 if (ret) {
634 krb5_cc_close(context, ccache);
635 krb5_princ_set_realm(context, creds.server, &orig_realm);
636 krb5_free_principal(context, creds.server);
637 krb5_free_principal(context, principal);
638 krb5_free_context(context);
639 DEBUG(1,("Failed to get principal from ccache (%s)\n",
640 error_message(ret)));
641 return ADS_ERROR_KRB5(ret);
644 ret = krb5_get_credentials(context, 0, ccache, &creds, &credsp);
645 if (ret) {
646 krb5_cc_close(context, ccache);
647 krb5_free_principal(context, creds.client);
648 krb5_princ_set_realm(context, creds.server, &orig_realm);
649 krb5_free_principal(context, creds.server);
650 krb5_free_principal(context, principal);
651 krb5_free_context(context);
652 DEBUG(1,("krb5_get_credentials failed (%s)\n", error_message(ret)));
653 return ADS_ERROR_KRB5(ret);
656 /* we might have to call krb5_free_creds(...) from now on ... */
658 aret = do_krb5_kpasswd_request(context, kdc_host,
659 KRB5_KPASSWD_VERS_SETPW,
660 credsp, princ, newpw);
662 krb5_free_creds(context, credsp);
663 krb5_free_principal(context, creds.client);
664 krb5_princ_set_realm(context, creds.server, &orig_realm);
665 krb5_free_principal(context, creds.server);
666 krb5_free_principal(context, principal);
667 krb5_cc_close(context, ccache);
668 krb5_free_context(context);
670 return aret;
674 we use a prompter to avoid a crash bug in the kerberos libs when
675 dealing with empty passwords
676 this prompter is just a string copy ...
678 static krb5_error_code
679 kerb_prompter(krb5_context ctx, void *data,
680 const char *name,
681 const char *banner,
682 int num_prompts,
683 krb5_prompt prompts[])
685 if (num_prompts == 0) return 0;
687 memset(prompts[0].reply->data, 0, prompts[0].reply->length);
688 if (prompts[0].reply->length > 0) {
689 if (data) {
690 strncpy(prompts[0].reply->data,
691 (const char *)data,
692 prompts[0].reply->length-1);
693 prompts[0].reply->length = strlen(prompts[0].reply->data);
694 } else {
695 prompts[0].reply->length = 0;
698 return 0;
701 static ADS_STATUS ads_krb5_chg_password(const char *kdc_host,
702 const char *principal,
703 const char *oldpw,
704 const char *newpw,
705 int time_offset)
707 ADS_STATUS aret;
708 krb5_error_code ret;
709 krb5_context context = NULL;
710 krb5_principal princ;
711 krb5_get_init_creds_opt opts;
712 krb5_creds creds;
713 char *chpw_princ = NULL, *password;
715 initialize_krb5_error_table();
716 ret = krb5_init_context(&context);
717 if (ret) {
718 DEBUG(1,("Failed to init krb5 context (%s)\n", error_message(ret)));
719 return ADS_ERROR_KRB5(ret);
722 if ((ret = smb_krb5_parse_name(context, principal,
723 &princ))) {
724 krb5_free_context(context);
725 DEBUG(1,("Failed to parse %s (%s)\n", principal, error_message(ret)));
726 return ADS_ERROR_KRB5(ret);
729 krb5_get_init_creds_opt_init(&opts);
730 krb5_get_init_creds_opt_set_tkt_life(&opts, 5*60);
731 krb5_get_init_creds_opt_set_renew_life(&opts, 0);
732 krb5_get_init_creds_opt_set_forwardable(&opts, 0);
733 krb5_get_init_creds_opt_set_proxiable(&opts, 0);
735 /* We have to obtain an INITIAL changepw ticket for changing password */
736 asprintf(&chpw_princ, "kadmin/changepw@%s",
737 (char *) krb5_princ_realm(context, princ));
738 password = SMB_STRDUP(oldpw);
739 ret = krb5_get_init_creds_password(context, &creds, princ, password,
740 kerb_prompter, NULL,
741 0, chpw_princ, &opts);
742 SAFE_FREE(chpw_princ);
743 SAFE_FREE(password);
745 if (ret) {
746 if (ret == KRB5KRB_AP_ERR_BAD_INTEGRITY)
747 DEBUG(1,("Password incorrect while getting initial ticket"));
748 else
749 DEBUG(1,("krb5_get_init_creds_password failed (%s)\n", error_message(ret)));
751 krb5_free_principal(context, princ);
752 krb5_free_context(context);
753 return ADS_ERROR_KRB5(ret);
756 aret = do_krb5_kpasswd_request(context, kdc_host,
757 KRB5_KPASSWD_VERS_CHANGEPW,
758 &creds, principal, newpw);
760 krb5_free_principal(context, princ);
761 krb5_free_context(context);
763 return aret;
767 ADS_STATUS kerberos_set_password(const char *kpasswd_server,
768 const char *auth_principal, const char *auth_password,
769 const char *target_principal, const char *new_password,
770 int time_offset)
772 int ret;
774 if ((ret = kerberos_kinit_password(auth_principal, auth_password, time_offset, NULL))) {
775 DEBUG(1,("Failed kinit for principal %s (%s)\n", auth_principal, error_message(ret)));
776 return ADS_ERROR_KRB5(ret);
779 if (!strcmp(auth_principal, target_principal))
780 return ads_krb5_chg_password(kpasswd_server, target_principal,
781 auth_password, new_password, time_offset);
782 else
783 return ads_krb5_set_password(kpasswd_server, target_principal,
784 new_password, time_offset);
789 * Set the machine account password
790 * @param ads connection to ads server
791 * @param hostname machine whose password is being set
792 * @param password new password
793 * @return status of password change
795 ADS_STATUS ads_set_machine_password(ADS_STRUCT *ads,
796 const char *machine_account,
797 const char *password)
799 ADS_STATUS status;
800 char *principal = NULL;
803 we need to use the '$' form of the name here (the machine account name),
804 as otherwise the server might end up setting the password for a user
805 instead
807 asprintf(&principal, "%s@%s", machine_account, ads->config.realm);
809 status = ads_krb5_set_password(ads->auth.kdc_server, principal,
810 password, ads->auth.time_offset);
812 free(principal);
814 return status;
819 #endif