95825748514e12f194383c50dbf022544eafed08
[tprouty/samba.git] / source / libads / krb5_setpw.c
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)
6    
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 2 of the License, or
10    (at your option) any later version.
11    
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.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24 #ifdef HAVE_KRB5
25
26 #define DEFAULT_KPASSWD_PORT    464
27 #define KRB5_KPASSWD_VERS_CHANGEPW              1
28 #define KRB5_KPASSWD_VERS_SETPW                 0xff80
29 #define KRB5_KPASSWD_VERS_SETPW_ALT             2
30 #define KRB5_KPASSWD_ACCESSDENIED               5
31 #define KRB5_KPASSWD_BAD_VERSION                6
32 #define KRB5_KPASSWD_INITIAL_FLAG_NEEDED        7
33
34 /* Those are defined by kerberos-set-passwd-02.txt and are probably 
35  * not supported by M$ implementation */
36 #define KRB5_KPASSWD_POLICY_REJECT              8
37 #define KRB5_KPASSWD_BAD_PRINCIPAL              9
38 #define KRB5_KPASSWD_ETYPE_NOSUPP               10
39
40 /* This implements kerberos password change protocol as specified in 
41  * kerb-chg-password-02.txt and kerberos-set-passwd-02.txt
42  * as well as microsoft version of the protocol 
43  * as specified in kerberos-set-passwd-00.txt
44  */
45 static DATA_BLOB encode_krb5_setpw(const char *principal, const char *password)
46 {
47         char* princ_part1 = NULL;
48         char* princ_part2 = NULL;
49         char* realm = NULL;
50         char* c;
51         char* princ;
52
53         ASN1_DATA req;
54         DATA_BLOB ret;
55
56
57         princ = strdup(principal);
58
59         if ((c = strchr_m(princ, '/')) == NULL) {
60             c = princ; 
61         } else {
62             *c = '\0';
63             c++;
64             princ_part1 = princ;
65         }
66
67         princ_part2 = c;
68
69         if ((c = strchr_m(c, '@')) != NULL) {
70             *c = '\0';
71             c++;
72             realm = c;
73         }
74
75         memset(&req, 0, sizeof(req));
76         
77         asn1_push_tag(&req, ASN1_SEQUENCE(0));
78         asn1_push_tag(&req, ASN1_CONTEXT(0));
79         asn1_write_OctetString(&req, password, strlen(password));
80         asn1_pop_tag(&req);
81
82         asn1_push_tag(&req, ASN1_CONTEXT(1));
83         asn1_push_tag(&req, ASN1_SEQUENCE(0));
84
85         asn1_push_tag(&req, ASN1_CONTEXT(0));
86         asn1_write_Integer(&req, 1);
87         asn1_pop_tag(&req);
88
89         asn1_push_tag(&req, ASN1_CONTEXT(1));
90         asn1_push_tag(&req, ASN1_SEQUENCE(0));
91
92         if (princ_part1) 
93             asn1_write_GeneralString(&req, princ_part1);
94         
95         asn1_write_GeneralString(&req, princ_part2);
96         asn1_pop_tag(&req);
97         asn1_pop_tag(&req);
98         asn1_pop_tag(&req);
99         asn1_pop_tag(&req);
100
101         asn1_push_tag(&req, ASN1_CONTEXT(2));
102         asn1_write_GeneralString(&req, realm);
103         asn1_pop_tag(&req);
104         asn1_pop_tag(&req);
105
106         ret = data_blob(req.data, req.length);
107         asn1_free(&req);
108
109         free(princ);
110
111         return ret;
112 }       
113
114 static krb5_error_code build_kpasswd_request(uint16 pversion,
115                                            krb5_context context,
116                                            krb5_auth_context auth_context,
117                                            krb5_data *ap_req,
118                                            const char *princ,
119                                            const char *passwd,
120                                            krb5_data *packet)
121 {
122         krb5_error_code ret;
123         krb5_data cipherpw;
124         krb5_data encoded_setpw;
125         krb5_replay_data replay;
126         char *p;
127         DATA_BLOB setpw;
128
129         ret = krb5_auth_con_setflags(context,
130                                      auth_context,KRB5_AUTH_CONTEXT_DO_SEQUENCE);
131         if (ret) {
132                 DEBUG(1,("krb5_auth_con_setflags failed (%s)\n",
133                          error_message(ret)));
134                 return ret;
135         }
136
137         /* handle protocol differences in chpw and setpw */
138         if (pversion  == KRB5_KPASSWD_VERS_CHANGEPW)
139                 setpw = data_blob(passwd, strlen(passwd));
140         else if (pversion == KRB5_KPASSWD_VERS_SETPW ||
141                  pversion == KRB5_KPASSWD_VERS_SETPW_ALT)
142                 setpw = encode_krb5_setpw(princ, passwd);
143         else
144                 return EINVAL;
145
146         encoded_setpw.data = (char *)setpw.data;
147         encoded_setpw.length = setpw.length;
148
149         ret = krb5_mk_priv(context, auth_context,
150                            &encoded_setpw, &cipherpw, &replay);
151         
152         data_blob_free(&setpw);         /*from 'encode_krb5_setpw(...)' */
153         
154         if (ret) {
155                 DEBUG(1,("krb5_mk_priv failed (%s)\n", error_message(ret)));
156                 return ret;
157         }
158
159         packet->data = (char *)malloc(ap_req->length + cipherpw.length + 6);
160         if (!packet->data)
161                 return -1;
162
163         /* see the RFC for details */
164         p = ((char *)packet->data) + 2;
165         RSSVAL(p, 0, pversion);
166         p += 2;
167         RSSVAL(p, 0, ap_req->length);
168         p += 2;
169         memcpy(p, ap_req->data, ap_req->length);
170         p += ap_req->length;
171         memcpy(p, cipherpw.data, cipherpw.length);
172         p += cipherpw.length;
173         packet->length = PTR_DIFF(p,packet->data);
174         RSSVAL(packet->data, 0, packet->length);
175         
176         free(cipherpw.data);    /* from 'krb5_mk_priv(...)' */
177
178         return 0;
179 }
180
181 static const struct kpasswd_errors {
182         int result_code;
183         const char *error_string;
184 } kpasswd_errors[] = {
185         {KRB5_KPASSWD_MALFORMED, "Malformed request error"},
186         {KRB5_KPASSWD_HARDERROR, "Server error"},
187         {KRB5_KPASSWD_AUTHERROR, "Authentication error"},
188         {KRB5_KPASSWD_SOFTERROR, "Password change rejected"},
189         {KRB5_KPASSWD_ACCESSDENIED, "Client does not have proper authorization"},
190         {KRB5_KPASSWD_BAD_VERSION, "Protocol version not supported"},
191         {KRB5_KPASSWD_INITIAL_FLAG_NEEDED, "Authorization ticket must have initial flag set"},
192         {KRB5_KPASSWD_POLICY_REJECT, "Password rejected due to policy requirements"},
193         {KRB5_KPASSWD_BAD_PRINCIPAL, "Target principal does not exist"},
194         {KRB5_KPASSWD_ETYPE_NOSUPP, "Unsupported encryption type"},
195         {0, NULL}
196 };
197
198 static krb5_error_code setpw_result_code_string(krb5_context context,
199                                                 int result_code,
200                                                 const char **code_string)
201 {
202         unsigned int idx = 0;
203
204         while (kpasswd_errors[idx].error_string != NULL) {
205                 if (kpasswd_errors[idx].result_code == 
206                     result_code) {
207                         *code_string = kpasswd_errors[idx].error_string;
208                         return 0;
209                 }
210                 idx++;
211         }
212         *code_string = "Password change failed";
213         return (0);
214 }
215
216 static krb5_error_code parse_setpw_reply(krb5_context context, 
217                                          krb5_auth_context auth_context,
218                                          krb5_data *packet)
219 {
220         krb5_data ap_rep;
221         char *p;
222         int vnum, ret, res_code;
223         krb5_data cipherresult;
224         krb5_data clearresult;
225         krb5_ap_rep_enc_part *ap_rep_enc;
226         krb5_replay_data replay;
227         
228         if (packet->length < 4) {
229                 return KRB5KRB_AP_ERR_MODIFIED;
230         }
231         
232         p = packet->data;
233         
234         if (((char *)packet->data)[0] == 0x7e || ((char *)packet->data)[0] == 0x5e) {
235                 /* it's an error packet. We should parse it ... */
236                 DEBUG(1,("Got error packet 0x%x from kpasswd server\n",
237                          ((char *)packet->data)[0]));
238                 return KRB5KRB_AP_ERR_MODIFIED;
239         }
240         
241         if (RSVAL(p, 0) != packet->length) {
242                 DEBUG(1,("Bad packet length (%d/%d) from kpasswd server\n",
243                          RSVAL(p, 0), packet->length));
244                 return KRB5KRB_AP_ERR_MODIFIED;
245         }
246
247         p += 2;
248
249         vnum = RSVAL(p, 0); p += 2;
250
251         /* FIXME: According to standard there is only one type of reply */      
252         if (vnum != KRB5_KPASSWD_VERS_SETPW && 
253             vnum != KRB5_KPASSWD_VERS_SETPW_ALT && 
254             vnum != KRB5_KPASSWD_VERS_CHANGEPW) {
255                 DEBUG(1,("Bad vnum (%d) from kpasswd server\n", vnum));
256                 return KRB5KDC_ERR_BAD_PVNO;
257         }
258         
259         ap_rep.length = RSVAL(p, 0); p += 2;
260         
261         if (p + ap_rep.length >= (char *)packet->data + packet->length) {
262                 DEBUG(1,("ptr beyond end of packet from kpasswd server\n"));
263                 return KRB5KRB_AP_ERR_MODIFIED;
264         }
265         
266         if (ap_rep.length == 0) {
267                 DEBUG(1,("got unencrypted setpw result?!\n"));
268                 return KRB5KRB_AP_ERR_MODIFIED;
269         }
270
271         /* verify ap_rep */
272         ap_rep.data = p;
273         p += ap_rep.length;
274         
275         ret = krb5_rd_rep(context, auth_context, &ap_rep, &ap_rep_enc);
276         if (ret) {
277                 DEBUG(1,("failed to rd setpw reply (%s)\n", error_message(ret)));
278                 return KRB5KRB_AP_ERR_MODIFIED;
279         }
280         
281         krb5_free_ap_rep_enc_part(context, ap_rep_enc);
282         
283         cipherresult.data = p;
284         cipherresult.length = ((char *)packet->data + packet->length) - p;
285                 
286         ret = krb5_rd_priv(context, auth_context, &cipherresult, &clearresult,
287                            &replay);
288         if (ret) {
289                 DEBUG(1,("failed to decrypt setpw reply (%s)\n", error_message(ret)));
290                 return KRB5KRB_AP_ERR_MODIFIED;
291         }
292
293         if (clearresult.length < 2) {
294                 free(clearresult.data);
295                 ret = KRB5KRB_AP_ERR_MODIFIED;
296                 return KRB5KRB_AP_ERR_MODIFIED;
297         }
298         
299         p = clearresult.data;
300         
301         res_code = RSVAL(p, 0);
302         
303         free(clearresult.data);
304
305         if ((res_code < KRB5_KPASSWD_SUCCESS) || 
306             (res_code > KRB5_KPASSWD_ETYPE_NOSUPP)) {
307                 return KRB5KRB_AP_ERR_MODIFIED;
308         }
309
310         if(res_code == KRB5_KPASSWD_SUCCESS)
311                         return 0;
312         else {
313                 const char *errstr;
314                 setpw_result_code_string(context, res_code, &errstr);
315                 DEBUG(1, ("Error changing password: %s\n", errstr));
316
317                 switch(res_code) {
318                         case KRB5_KPASSWD_ACCESSDENIED:
319                                 return KRB5KDC_ERR_BADOPTION;
320                                 break;
321                         case KRB5_KPASSWD_INITIAL_FLAG_NEEDED:
322                                 return KRB5KDC_ERR_BADOPTION;
323                                 /* return KV5M_ALT_METHOD; MIT-only define */
324                                 break;
325                         case KRB5_KPASSWD_ETYPE_NOSUPP:
326                                 return KRB5KDC_ERR_ETYPE_NOSUPP;
327                                 break;
328                         case KRB5_KPASSWD_BAD_PRINCIPAL:
329                                 return KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
330                                 break;
331                         case KRB5_KPASSWD_POLICY_REJECT:
332                                 return KRB5KDC_ERR_POLICY;
333                                 break;
334                         default:
335                                 return KRB5KRB_ERR_GENERIC;
336                                 break;
337                 }
338         }
339 }
340
341 static ADS_STATUS do_krb5_kpasswd_request(krb5_context context,
342                                           const char *kdc_host,
343                                           uint16 pversion,
344                                           krb5_creds *credsp,
345                                           const char *princ,
346                                           const char *newpw)
347 {
348         krb5_auth_context auth_context = NULL;
349         krb5_data ap_req, chpw_req, chpw_rep;
350         int ret, sock, addr_len;
351         struct sockaddr remote_addr, local_addr;
352         krb5_address local_kaddr, remote_kaddr;
353
354         ret = krb5_mk_req_extended(context, &auth_context, AP_OPTS_USE_SUBKEY,
355                                    NULL, credsp, &ap_req);
356         if (ret) {
357                 DEBUG(1,("krb5_mk_req_extended failed (%s)\n", error_message(ret)));
358                 return ADS_ERROR_KRB5(ret);
359         }
360         
361         sock = open_udp_socket(kdc_host, DEFAULT_KPASSWD_PORT);
362         if (sock == -1) {
363                 int rc = errno;
364                 free(ap_req.data);
365                 krb5_auth_con_free(context, auth_context);
366                 DEBUG(1,("failed to open kpasswd socket to %s (%s)\n", 
367                          kdc_host, strerror(errno)));
368                 return ADS_ERROR_SYSTEM(rc);
369         }
370         
371         addr_len = sizeof(remote_addr);
372         getpeername(sock, &remote_addr, &addr_len);
373         addr_len = sizeof(local_addr);
374         getsockname(sock, &local_addr, &addr_len);
375         
376         setup_kaddr(&remote_kaddr, &remote_addr);
377         setup_kaddr(&local_kaddr, &local_addr);
378
379         ret = krb5_auth_con_setaddrs(context, auth_context, &local_kaddr, NULL);
380         if (ret) {
381                 close(sock);
382                 free(ap_req.data);
383                 krb5_auth_con_free(context, auth_context);
384                 DEBUG(1,("krb5_auth_con_setaddrs failed (%s)\n", error_message(ret)));
385                 return ADS_ERROR_KRB5(ret);
386         }
387
388         ret = build_kpasswd_request(pversion, context, auth_context, &ap_req,
389                                   princ, newpw, &chpw_req);
390         if (ret) {
391                 close(sock);
392                 free(ap_req.data);
393                 krb5_auth_con_free(context, auth_context);
394                 DEBUG(1,("build_setpw_request failed (%s)\n", error_message(ret)));
395                 return ADS_ERROR_KRB5(ret);
396         }
397
398         if (write(sock, chpw_req.data, chpw_req.length) != chpw_req.length) {
399                 close(sock);
400                 free(chpw_req.data);
401                 free(ap_req.data);
402                 krb5_auth_con_free(context, auth_context);
403                 DEBUG(1,("send of chpw failed (%s)\n", strerror(errno)));
404                 return ADS_ERROR_SYSTEM(errno);
405         }
406
407         free(chpw_req.data);
408
409         chpw_rep.length = 1500;
410         chpw_rep.data = (char *) malloc(chpw_rep.length);
411         if (!chpw_rep.data) {
412                 close(sock);
413                 free(ap_req.data);
414                 krb5_auth_con_free(context, auth_context);
415                 DEBUG(1,("send of chpw failed (%s)\n", strerror(errno)));
416                 errno = ENOMEM;
417                 return ADS_ERROR_SYSTEM(errno);
418         }
419
420         ret = read(sock, chpw_rep.data, chpw_rep.length);
421         if (ret < 0) {
422                 close(sock);
423                 free(chpw_rep.data);
424                 free(ap_req.data);
425                 krb5_auth_con_free(context, auth_context);
426                 DEBUG(1,("recv of chpw reply failed (%s)\n", strerror(errno)));
427                 return ADS_ERROR_SYSTEM(errno);
428         }
429
430         close(sock);
431         chpw_rep.length = ret;
432
433         ret = krb5_auth_con_setaddrs(context, auth_context, NULL,&remote_kaddr);
434         if (ret) {
435                 free(chpw_rep.data);
436                 free(ap_req.data);
437                 krb5_auth_con_free(context, auth_context);
438                 DEBUG(1,("krb5_auth_con_setaddrs on reply failed (%s)\n", 
439                          error_message(ret)));
440                 return ADS_ERROR_KRB5(ret);
441         }
442
443         ret = parse_setpw_reply(context, auth_context, &chpw_rep);
444         free(chpw_rep.data);
445
446         if (ret) {
447                 free(ap_req.data);
448                 krb5_auth_con_free(context, auth_context);
449                 DEBUG(1,("parse_setpw_reply failed (%s)\n", 
450                          error_message(ret)));
451                 return ADS_ERROR_KRB5(ret);
452         }
453
454         free(ap_req.data);
455         krb5_auth_con_free(context, auth_context);
456
457         return ADS_SUCCESS;
458 }
459
460 ADS_STATUS ads_krb5_set_password(const char *kdc_host, const char *princ, 
461                                  const char *newpw, int time_offset)
462 {
463
464         ADS_STATUS aret;
465         krb5_error_code ret = 0;
466         krb5_context context = NULL;
467         krb5_principal principal = NULL;
468         char *princ_name = NULL;
469         char *realm = NULL;
470         krb5_creds creds, *credsp = NULL;
471 #if KRB5_PRINC_REALM_RETURNS_REALM
472         krb5_realm orig_realm;
473 #else
474         krb5_data orig_realm;
475 #endif
476         krb5_ccache ccache = NULL;
477
478         ZERO_STRUCT(creds);
479         
480         ret = krb5_init_context(&context);
481         if (ret) {
482                 DEBUG(1,("Failed to init krb5 context (%s)\n", error_message(ret)));
483                 return ADS_ERROR_KRB5(ret);
484         }
485         
486         if (time_offset != 0) {
487                 krb5_set_real_time(context, time(NULL) + time_offset, 0);
488         }
489
490         ret = krb5_cc_default(context, &ccache);
491         if (ret) {
492                 krb5_free_context(context);
493                 DEBUG(1,("Failed to get default creds (%s)\n", error_message(ret)));
494                 return ADS_ERROR_KRB5(ret);
495         }
496
497         realm = strchr_m(princ, '@');
498         if (!realm) {
499                 krb5_cc_close(context, ccache);
500                 krb5_free_context(context);
501                 DEBUG(1,("Failed to get realm\n"));
502                 return ADS_ERROR_KRB5(-1);
503         }
504         realm++;
505
506         asprintf(&princ_name, "kadmin/changepw@%s", realm);
507         ret = krb5_parse_name(context, princ_name, &creds.server);
508         if (ret) {
509                 krb5_cc_close(context, ccache);
510                 krb5_free_context(context);
511                 DEBUG(1,("Failed to parse kadmin/changepw (%s)\n", error_message(ret)));
512                 return ADS_ERROR_KRB5(ret);
513         }
514         free(princ_name);
515
516         /* parse the principal we got as a function argument */
517         ret = krb5_parse_name(context, princ, &principal);
518         if (ret) {
519                 krb5_cc_close(context, ccache);
520                 krb5_free_principal(context, creds.server);
521                 krb5_free_context(context);
522                 DEBUG(1,("Failed to parse %s (%s)\n", princ_name, error_message(ret)));
523                 return ADS_ERROR_KRB5(ret);
524         }
525
526         /* The creds.server principal takes ownership of this memory.
527                 Remember to set back to original value before freeing. */
528         orig_realm = *krb5_princ_realm(context, creds.server);
529         krb5_princ_set_realm(context, creds.server, krb5_princ_realm(context, principal));
530         
531         ret = krb5_cc_get_principal(context, ccache, &creds.client);
532         if (ret) {
533                 krb5_cc_close(context, ccache);
534                 krb5_princ_set_realm(context, creds.server, &orig_realm);
535                 krb5_free_principal(context, creds.server);
536                 krb5_free_principal(context, principal);
537                 krb5_free_context(context);
538                 DEBUG(1,("Failed to get principal from ccache (%s)\n", 
539                          error_message(ret)));
540                 return ADS_ERROR_KRB5(ret);
541         }
542         
543         ret = krb5_get_credentials(context, 0, ccache, &creds, &credsp); 
544         if (ret) {
545                 krb5_cc_close(context, ccache);
546                 krb5_free_principal(context, creds.client);
547                 krb5_princ_set_realm(context, creds.server, &orig_realm);
548                 krb5_free_principal(context, creds.server);
549                 krb5_free_principal(context, principal);
550                 krb5_free_context(context);
551                 DEBUG(1,("krb5_get_credentials failed (%s)\n", error_message(ret)));
552                 return ADS_ERROR_KRB5(ret);
553         }
554         
555         /* we might have to call krb5_free_creds(...) from now on ... */
556
557         aret = do_krb5_kpasswd_request(context, kdc_host,
558                                        KRB5_KPASSWD_VERS_SETPW,
559                                        credsp, princ, newpw);
560
561         krb5_free_creds(context, credsp);
562         krb5_free_principal(context, creds.client);
563         krb5_princ_set_realm(context, creds.server, &orig_realm);
564         krb5_free_principal(context, creds.server);
565         krb5_free_principal(context, principal);
566         krb5_cc_close(context, ccache);
567         krb5_free_context(context);
568
569         return aret;
570 }
571
572 /*
573   we use a prompter to avoid a crash bug in the kerberos libs when 
574   dealing with empty passwords
575   this prompter is just a string copy ...
576 */
577 static krb5_error_code 
578 kerb_prompter(krb5_context ctx, void *data,
579                const char *name,
580                const char *banner,
581                int num_prompts,
582                krb5_prompt prompts[])
583 {
584         if (num_prompts == 0) return 0;
585
586         memset(prompts[0].reply->data, 0, prompts[0].reply->length);
587         if (prompts[0].reply->length > 0) {
588                 if (data) {
589                         strncpy(prompts[0].reply->data, data, prompts[0].reply->length-1);
590                         prompts[0].reply->length = strlen(prompts[0].reply->data);
591                 } else {
592                         prompts[0].reply->length = 0;
593                 }
594         }
595         return 0;
596 }
597
598 static ADS_STATUS ads_krb5_chg_password(const char *kdc_host,
599                                         const char *principal,
600                                         const char *oldpw, 
601                                         const char *newpw, 
602                                         int time_offset)
603 {
604     ADS_STATUS aret;
605     krb5_error_code ret;
606     krb5_context context = NULL;
607     krb5_principal princ;
608     krb5_get_init_creds_opt opts;
609     krb5_creds creds;
610     char *chpw_princ = NULL, *password;
611
612     ret = krb5_init_context(&context);
613     if (ret) {
614         DEBUG(1,("Failed to init krb5 context (%s)\n", error_message(ret)));
615         return ADS_ERROR_KRB5(ret);
616     }
617
618     if ((ret = krb5_parse_name(context, principal,
619                                     &princ))) {
620         krb5_free_context(context);
621         DEBUG(1,("Failed to parse %s (%s)\n", principal, error_message(ret)));
622         return ADS_ERROR_KRB5(ret);
623     }
624
625     krb5_get_init_creds_opt_init(&opts);
626     krb5_get_init_creds_opt_set_tkt_life(&opts, 5*60);
627     krb5_get_init_creds_opt_set_renew_life(&opts, 0);
628     krb5_get_init_creds_opt_set_forwardable(&opts, 0);
629     krb5_get_init_creds_opt_set_proxiable(&opts, 0);
630
631     /* We have to obtain an INITIAL changepw ticket for changing password */
632     asprintf(&chpw_princ, "kadmin/changepw@%s",
633                                 (char *) krb5_princ_realm(context, princ));
634     password = strdup(oldpw);
635     ret = krb5_get_init_creds_password(context, &creds, princ, password,
636                                            kerb_prompter, NULL, 
637                                            0, chpw_princ, &opts);
638     SAFE_FREE(chpw_princ);
639     SAFE_FREE(password);
640
641     if (ret) {
642       if (ret == KRB5KRB_AP_ERR_BAD_INTEGRITY)
643         DEBUG(1,("Password incorrect while getting initial ticket"));
644       else
645         DEBUG(1,("krb5_get_init_creds_password failed (%s)\n", error_message(ret)));
646
647         krb5_free_principal(context, princ);
648         krb5_free_context(context);
649         return ADS_ERROR_KRB5(ret);
650     }
651
652     aret = do_krb5_kpasswd_request(context, kdc_host,
653                                    KRB5_KPASSWD_VERS_CHANGEPW,
654                                    &creds, principal, newpw);
655
656     krb5_free_principal(context, princ);
657     krb5_free_context(context);
658
659     return aret;
660 }
661
662
663 ADS_STATUS kerberos_set_password(const char *kpasswd_server, 
664                                  const char *auth_principal, const char *auth_password,
665                                  const char *target_principal, const char *new_password,
666                                  int time_offset)
667 {
668     int ret;
669
670     if ((ret = kerberos_kinit_password(auth_principal, auth_password, time_offset, NULL, NULL))) {
671         DEBUG(1,("Failed kinit for principal %s (%s)\n", auth_principal, error_message(ret)));
672         return ADS_ERROR_KRB5(ret);
673     }
674
675     if (!strcmp(auth_principal, target_principal))
676         return ads_krb5_chg_password(kpasswd_server, target_principal,
677                                      auth_password, new_password, time_offset);
678     else
679         return ads_krb5_set_password(kpasswd_server, target_principal,
680                                      new_password, time_offset);
681 }
682
683
684 /**
685  * Set the machine account password
686  * @param ads connection to ads server
687  * @param hostname machine whose password is being set
688  * @param password new password
689  * @return status of password change
690  **/
691 ADS_STATUS ads_set_machine_password(ADS_STRUCT *ads,
692                                     const char *machine_account,
693                                     const char *password)
694 {
695         ADS_STATUS status;
696         char *principal = NULL; 
697
698         /*
699           we need to use the '$' form of the name here (the machine account name), 
700           as otherwise the server might end up setting the password for a user
701           instead
702          */
703         asprintf(&principal, "%s@%s", machine_account, ads->config.realm);
704         
705         status = ads_krb5_set_password(ads->auth.kdc_server, principal, 
706                                        password, ads->auth.time_offset);
707         
708         free(principal);
709
710         return status;
711 }
712
713
714
715 #endif