RIP BOOL. Convert BOOL -> bool. I found a few interesting
[nivanova/samba-autobuild/.git] / source3 / 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 3 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, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22
23 #ifdef HAVE_KRB5
24
25 #define DEFAULT_KPASSWD_PORT    464
26
27 #define KRB5_KPASSWD_VERS_CHANGEPW              1
28
29 #define KRB5_KPASSWD_VERS_SETPW                 0xff80
30 #define KRB5_KPASSWD_VERS_SETPW_ALT             2
31
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
40
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
46
47 /*
48  * we've got to be able to distinguish KRB_ERRORs from other
49  * requests - valid response for CHPW v2 replies.
50  */
51
52 #define krb5_is_krb_error(packet) \
53         ( packet && packet->length && (((char *)packet->data)[0] == 0x7e || ((char *)packet->data)[0] == 0x5e))
54
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
59  */
60 static DATA_BLOB encode_krb5_setpw(const char *principal, const char *password)
61 {
62         char* princ_part1 = NULL;
63         char* princ_part2 = NULL;
64         char* realm = NULL;
65         char* c;
66         char* princ;
67
68         ASN1_DATA req;
69         DATA_BLOB ret;
70
71
72         princ = SMB_STRDUP(principal);
73
74         if ((c = strchr_m(princ, '/')) == NULL) {
75                 c = princ; 
76         } else {
77                 *c = '\0';
78                 c++;
79                 princ_part1 = princ;
80         }
81
82         princ_part2 = c;
83
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;
91         }
92
93         memset(&req, 0, sizeof(req));
94         
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);
99
100         asn1_push_tag(&req, ASN1_CONTEXT(1));
101         asn1_push_tag(&req, ASN1_SEQUENCE(0));
102
103         asn1_push_tag(&req, ASN1_CONTEXT(0));
104         asn1_write_Integer(&req, 1);
105         asn1_pop_tag(&req);
106
107         asn1_push_tag(&req, ASN1_CONTEXT(1));
108         asn1_push_tag(&req, ASN1_SEQUENCE(0));
109
110         if (princ_part1) {
111                 asn1_write_GeneralString(&req, princ_part1);
112         }
113         
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);
119
120         asn1_push_tag(&req, ASN1_CONTEXT(2));
121         asn1_write_GeneralString(&req, realm);
122         asn1_pop_tag(&req);
123         asn1_pop_tag(&req);
124
125         ret = data_blob(req.data, req.length);
126         asn1_free(&req);
127
128         free(princ);
129
130         return ret;
131 }       
132
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)
141 {
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;
149
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;
156         }
157
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;
166
167         if (setpw.data == NULL || setpw.length == 0) {
168                 return EINVAL;
169         }
170
171         encoded_setpw.data = (char *)setpw.data;
172         encoded_setpw.length = setpw.length;
173
174         ret = krb5_mk_priv(context, auth_context,
175                            &encoded_setpw, &cipherpw, &replay);
176         
177         data_blob_free(&setpw);         /*from 'encode_krb5_setpw(...)' */
178         
179         if (ret) {
180                 DEBUG(1,("krb5_mk_priv failed (%s)\n", error_message(ret)));
181                 return ret;
182         }
183
184         packet->data = (char *)SMB_MALLOC(ap_req->length + cipherpw.length + (use_tcp ? 10 : 6 ));
185         if (!packet->data)
186                 return -1;
187
188
189
190         /* see the RFC for details */
191
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);
204
205         if (use_tcp) {
206                 RSIVAL(packet->data, 0, msg_length);
207         }
208         RSSVAL(msg_start, 0, msg_length);
209         
210         free(cipherpw.data);    /* from 'krb5_mk_priv(...)' */
211
212         return 0;
213 }
214
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}
230 };
231
232 static krb5_error_code setpw_result_code_string(krb5_context context,
233                                                 int result_code,
234                                                 const char **code_string)
235 {
236         unsigned int idx = 0;
237
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;
243                 }
244                 idx++;
245         }
246         *code_string = "Password change failed";
247         return (0);
248 }
249
250  krb5_error_code kpasswd_err_to_krb5_err(krb5_error_code res_code) 
251 {
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;
267         }
268 }
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)
273 {
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;
282
283
284         if (packet->length < (use_tcp ? 8 : 4)) {
285                 return KRB5KRB_AP_ERR_MODIFIED;
286         }
287         
288         p = packet->data;
289         /*
290         ** see if it is an error
291         */
292         if (krb5_is_krb_error(packet)) {
293
294                 ret = handle_krberror_packet(context, packet);
295                 if (ret) {
296                         return ret;
297                 }
298         }
299
300         
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;
308                 }
309
310                 p += 4;
311         }
312         
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;
317         }
318
319         p += 2;
320
321         vnum = RSVAL(p, 0); p += 2;
322
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;
329         }
330         
331         ap_rep.length = RSVAL(p, 0); p += 2;
332         
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;
336         }
337         
338         if (ap_rep.length == 0) {
339                 DEBUG(1,("got unencrypted setpw result?!\n"));
340                 return KRB5KRB_AP_ERR_MODIFIED;
341         }
342
343         /* verify ap_rep */
344         ap_rep.data = p;
345         p += ap_rep.length;
346         
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;
351         }
352         
353         krb5_free_ap_rep_enc_part(context, ap_rep_enc);
354         
355         cipherresult.data = p;
356         cipherresult.length = ((char *)packet->data + packet->length) - p;
357                 
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;
363         }
364
365         if (clearresult.length < 2) {
366                 free(clearresult.data);
367                 ret = KRB5KRB_AP_ERR_MODIFIED;
368                 return KRB5KRB_AP_ERR_MODIFIED;
369         }
370         
371         p = clearresult.data;
372         
373         res_code = RSVAL(p, 0);
374         
375         free(clearresult.data);
376
377         if ((res_code < KRB5_KPASSWD_SUCCESS) || 
378             (res_code > KRB5_KPASSWD_ETYPE_NOSUPP)) {
379                 return KRB5KRB_AP_ERR_MODIFIED;
380         }
381
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));
388
389                 return kpasswd_err_to_krb5_err(res_code);
390         }
391 }
392
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)
399 {
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 remote_addr, local_addr;
405         struct in_addr *addr = interpret_addr2(kdc_host);
406         krb5_address local_kaddr, remote_kaddr;
407         bool use_tcp = False;
408
409
410         ret = krb5_mk_req_extended(context, &auth_context, AP_OPTS_USE_SUBKEY,
411                                    NULL, credsp, &ap_req);
412         if (ret) {
413                 DEBUG(1,("krb5_mk_req_extended failed (%s)\n", error_message(ret)));
414                 return ADS_ERROR_KRB5(ret);
415         }
416
417         do {
418
419                 if (!use_tcp) {
420
421                         sock = open_udp_socket(kdc_host, DEFAULT_KPASSWD_PORT);
422
423                 } else {
424
425                         sock = open_socket_out(SOCK_STREAM, addr, DEFAULT_KPASSWD_PORT, 
426                                                LONG_CONNECT_TIMEOUT);
427                 }
428
429                 if (sock == -1) {
430                         int rc = errno;
431                         SAFE_FREE(ap_req.data);
432                         krb5_auth_con_free(context, auth_context);
433                         DEBUG(1,("failed to open kpasswd socket to %s (%s)\n", 
434                                  kdc_host, strerror(errno)));
435                         return ADS_ERROR_SYSTEM(rc);
436                 }
437                 
438                 addr_len = sizeof(remote_addr);
439                 getpeername(sock, &remote_addr, &addr_len);
440                 addr_len = sizeof(local_addr);
441                 getsockname(sock, &local_addr, &addr_len);
442                 
443                 setup_kaddr(&remote_kaddr, &remote_addr);
444                 setup_kaddr(&local_kaddr, &local_addr);
445         
446                 ret = krb5_auth_con_setaddrs(context, auth_context, &local_kaddr, NULL);
447                 if (ret) {
448                         close(sock);
449                         SAFE_FREE(ap_req.data);
450                         krb5_auth_con_free(context, auth_context);
451                         DEBUG(1,("krb5_auth_con_setaddrs failed (%s)\n", error_message(ret)));
452                         return ADS_ERROR_KRB5(ret);
453                 }
454         
455                 ret = build_kpasswd_request(pversion, context, auth_context, &ap_req,
456                                           princ, newpw, use_tcp, &chpw_req);
457                 if (ret) {
458                         close(sock);
459                         SAFE_FREE(ap_req.data);
460                         krb5_auth_con_free(context, auth_context);
461                         DEBUG(1,("build_setpw_request failed (%s)\n", error_message(ret)));
462                         return ADS_ERROR_KRB5(ret);
463                 }
464
465                 ret = write(sock, chpw_req.data, chpw_req.length); 
466
467                 if (ret != chpw_req.length) {
468                         close(sock);
469                         SAFE_FREE(chpw_req.data);
470                         SAFE_FREE(ap_req.data);
471                         krb5_auth_con_free(context, auth_context);
472                         DEBUG(1,("send of chpw failed (%s)\n", strerror(errno)));
473                         return ADS_ERROR_SYSTEM(errno);
474                 }
475         
476                 SAFE_FREE(chpw_req.data);
477         
478                 chpw_rep.length = 1500;
479                 chpw_rep.data = (char *) SMB_MALLOC(chpw_rep.length);
480                 if (!chpw_rep.data) {
481                         close(sock);
482                         SAFE_FREE(ap_req.data);
483                         krb5_auth_con_free(context, auth_context);
484                         DEBUG(1,("send of chpw failed (%s)\n", strerror(errno)));
485                         errno = ENOMEM;
486                         return ADS_ERROR_SYSTEM(errno);
487                 }
488         
489                 ret = read(sock, chpw_rep.data, chpw_rep.length);
490                 if (ret < 0) {
491                         close(sock);
492                         SAFE_FREE(chpw_rep.data);
493                         SAFE_FREE(ap_req.data);
494                         krb5_auth_con_free(context, auth_context);
495                         DEBUG(1,("recv of chpw reply failed (%s)\n", strerror(errno)));
496                         return ADS_ERROR_SYSTEM(errno);
497                 }
498         
499                 close(sock);
500                 chpw_rep.length = ret;
501         
502                 ret = krb5_auth_con_setaddrs(context, auth_context, NULL,&remote_kaddr);
503                 if (ret) {
504                         SAFE_FREE(chpw_rep.data);
505                         SAFE_FREE(ap_req.data);
506                         krb5_auth_con_free(context, auth_context);
507                         DEBUG(1,("krb5_auth_con_setaddrs on reply failed (%s)\n", 
508                                  error_message(ret)));
509                         return ADS_ERROR_KRB5(ret);
510                 }
511         
512                 ret = parse_setpw_reply(context, use_tcp, auth_context, &chpw_rep);
513                 SAFE_FREE(chpw_rep.data);
514         
515                 if (ret) {
516                         
517                         if (ret == KRB5KRB_ERR_RESPONSE_TOO_BIG && !use_tcp) {
518                                 DEBUG(5, ("Trying setpw with TCP!!!\n"));
519                                 use_tcp = True;
520                                 continue;
521                         }
522
523                         SAFE_FREE(ap_req.data);
524                         krb5_auth_con_free(context, auth_context);
525                         DEBUG(1,("parse_setpw_reply failed (%s)\n", 
526                                  error_message(ret)));
527                         return ADS_ERROR_KRB5(ret);
528                 }
529         
530                 SAFE_FREE(ap_req.data);
531                 krb5_auth_con_free(context, auth_context);
532         } while ( ret );
533
534         return ADS_SUCCESS;
535 }
536
537 ADS_STATUS ads_krb5_set_password(const char *kdc_host, const char *princ, 
538                                  const char *newpw, int time_offset)
539 {
540
541         ADS_STATUS aret;
542         krb5_error_code ret = 0;
543         krb5_context context = NULL;
544         krb5_principal principal = NULL;
545         char *princ_name = NULL;
546         char *realm = NULL;
547         krb5_creds creds, *credsp = NULL;
548 #if KRB5_PRINC_REALM_RETURNS_REALM
549         krb5_realm orig_realm;
550 #else
551         krb5_data orig_realm;
552 #endif
553         krb5_ccache ccache = NULL;
554
555         ZERO_STRUCT(creds);
556         
557         initialize_krb5_error_table();
558         ret = krb5_init_context(&context);
559         if (ret) {
560                 DEBUG(1,("Failed to init krb5 context (%s)\n", error_message(ret)));
561                 return ADS_ERROR_KRB5(ret);
562         }
563         
564         if (time_offset != 0) {
565                 krb5_set_real_time(context, time(NULL) + time_offset, 0);
566         }
567
568         ret = krb5_cc_default(context, &ccache);
569         if (ret) {
570                 krb5_free_context(context);
571                 DEBUG(1,("Failed to get default creds (%s)\n", error_message(ret)));
572                 return ADS_ERROR_KRB5(ret);
573         }
574
575         realm = strchr_m(princ, '@');
576         if (!realm) {
577                 krb5_cc_close(context, ccache);
578                 krb5_free_context(context);
579                 DEBUG(1,("Failed to get realm\n"));
580                 return ADS_ERROR_KRB5(-1);
581         }
582         realm++;
583
584         asprintf(&princ_name, "kadmin/changepw@%s", realm);
585         ret = smb_krb5_parse_name(context, princ_name, &creds.server);
586         if (ret) {
587                 krb5_cc_close(context, ccache);
588                 krb5_free_context(context);
589                 DEBUG(1,("Failed to parse kadmin/changepw (%s)\n", error_message(ret)));
590                 return ADS_ERROR_KRB5(ret);
591         }
592
593         /* parse the principal we got as a function argument */
594         ret = smb_krb5_parse_name(context, princ, &principal);
595         if (ret) {
596                 krb5_cc_close(context, ccache);
597                 krb5_free_principal(context, creds.server);
598                 krb5_free_context(context);
599                 DEBUG(1,("Failed to parse %s (%s)\n", princ_name, error_message(ret)));
600                 free(princ_name);
601                 return ADS_ERROR_KRB5(ret);
602         }
603
604         free(princ_name);
605
606         /* The creds.server principal takes ownership of this memory.
607                 Remember to set back to original value before freeing. */
608         orig_realm = *krb5_princ_realm(context, creds.server);
609         krb5_princ_set_realm(context, creds.server, krb5_princ_realm(context, principal));
610         
611         ret = krb5_cc_get_principal(context, ccache, &creds.client);
612         if (ret) {
613                 krb5_cc_close(context, ccache);
614                 krb5_princ_set_realm(context, creds.server, &orig_realm);
615                 krb5_free_principal(context, creds.server);
616                 krb5_free_principal(context, principal);
617                 krb5_free_context(context);
618                 DEBUG(1,("Failed to get principal from ccache (%s)\n", 
619                          error_message(ret)));
620                 return ADS_ERROR_KRB5(ret);
621         }
622         
623         ret = krb5_get_credentials(context, 0, ccache, &creds, &credsp); 
624         if (ret) {
625                 krb5_cc_close(context, ccache);
626                 krb5_free_principal(context, creds.client);
627                 krb5_princ_set_realm(context, creds.server, &orig_realm);
628                 krb5_free_principal(context, creds.server);
629                 krb5_free_principal(context, principal);
630                 krb5_free_context(context);
631                 DEBUG(1,("krb5_get_credentials failed (%s)\n", error_message(ret)));
632                 return ADS_ERROR_KRB5(ret);
633         }
634         
635         /* we might have to call krb5_free_creds(...) from now on ... */
636
637         aret = do_krb5_kpasswd_request(context, kdc_host,
638                                        KRB5_KPASSWD_VERS_SETPW,
639                                        credsp, princ, newpw);
640
641         krb5_free_creds(context, credsp);
642         krb5_free_principal(context, creds.client);
643         krb5_princ_set_realm(context, creds.server, &orig_realm);
644         krb5_free_principal(context, creds.server);
645         krb5_free_principal(context, principal);
646         krb5_cc_close(context, ccache);
647         krb5_free_context(context);
648
649         return aret;
650 }
651
652 /*
653   we use a prompter to avoid a crash bug in the kerberos libs when 
654   dealing with empty passwords
655   this prompter is just a string copy ...
656 */
657 static krb5_error_code 
658 kerb_prompter(krb5_context ctx, void *data,
659                const char *name,
660                const char *banner,
661                int num_prompts,
662                krb5_prompt prompts[])
663 {
664         if (num_prompts == 0) return 0;
665
666         memset(prompts[0].reply->data, 0, prompts[0].reply->length);
667         if (prompts[0].reply->length > 0) {
668                 if (data) {
669                         strncpy(prompts[0].reply->data,
670                                 (const char *)data,
671                                 prompts[0].reply->length-1);
672                         prompts[0].reply->length = strlen(prompts[0].reply->data);
673                 } else {
674                         prompts[0].reply->length = 0;
675                 }
676         }
677         return 0;
678 }
679
680 static ADS_STATUS ads_krb5_chg_password(const char *kdc_host,
681                                         const char *principal,
682                                         const char *oldpw, 
683                                         const char *newpw, 
684                                         int time_offset)
685 {
686     ADS_STATUS aret;
687     krb5_error_code ret;
688     krb5_context context = NULL;
689     krb5_principal princ;
690     krb5_get_init_creds_opt opts;
691     krb5_creds creds;
692     char *chpw_princ = NULL, *password;
693
694     initialize_krb5_error_table();
695     ret = krb5_init_context(&context);
696     if (ret) {
697         DEBUG(1,("Failed to init krb5 context (%s)\n", error_message(ret)));
698         return ADS_ERROR_KRB5(ret);
699     }
700
701     if ((ret = smb_krb5_parse_name(context, principal,
702                                     &princ))) {
703         krb5_free_context(context);
704         DEBUG(1,("Failed to parse %s (%s)\n", principal, error_message(ret)));
705         return ADS_ERROR_KRB5(ret);
706     }
707
708     krb5_get_init_creds_opt_init(&opts);
709     krb5_get_init_creds_opt_set_tkt_life(&opts, 5*60);
710     krb5_get_init_creds_opt_set_renew_life(&opts, 0);
711     krb5_get_init_creds_opt_set_forwardable(&opts, 0);
712     krb5_get_init_creds_opt_set_proxiable(&opts, 0);
713
714     /* We have to obtain an INITIAL changepw ticket for changing password */
715     asprintf(&chpw_princ, "kadmin/changepw@%s",
716                                 (char *) krb5_princ_realm(context, princ));
717     password = SMB_STRDUP(oldpw);
718     ret = krb5_get_init_creds_password(context, &creds, princ, password,
719                                            kerb_prompter, NULL, 
720                                            0, chpw_princ, &opts);
721     SAFE_FREE(chpw_princ);
722     SAFE_FREE(password);
723
724     if (ret) {
725       if (ret == KRB5KRB_AP_ERR_BAD_INTEGRITY)
726         DEBUG(1,("Password incorrect while getting initial ticket"));
727       else
728         DEBUG(1,("krb5_get_init_creds_password failed (%s)\n", error_message(ret)));
729
730         krb5_free_principal(context, princ);
731         krb5_free_context(context);
732         return ADS_ERROR_KRB5(ret);
733     }
734
735     aret = do_krb5_kpasswd_request(context, kdc_host,
736                                    KRB5_KPASSWD_VERS_CHANGEPW,
737                                    &creds, principal, newpw);
738
739     krb5_free_principal(context, princ);
740     krb5_free_context(context);
741
742     return aret;
743 }
744
745
746 ADS_STATUS kerberos_set_password(const char *kpasswd_server, 
747                                  const char *auth_principal, const char *auth_password,
748                                  const char *target_principal, const char *new_password,
749                                  int time_offset)
750 {
751     int ret;
752
753     if ((ret = kerberos_kinit_password(auth_principal, auth_password, time_offset, NULL))) {
754         DEBUG(1,("Failed kinit for principal %s (%s)\n", auth_principal, error_message(ret)));
755         return ADS_ERROR_KRB5(ret);
756     }
757
758     if (!strcmp(auth_principal, target_principal))
759         return ads_krb5_chg_password(kpasswd_server, target_principal,
760                                      auth_password, new_password, time_offset);
761     else
762         return ads_krb5_set_password(kpasswd_server, target_principal,
763                                      new_password, time_offset);
764 }
765
766
767 /**
768  * Set the machine account password
769  * @param ads connection to ads server
770  * @param hostname machine whose password is being set
771  * @param password new password
772  * @return status of password change
773  **/
774 ADS_STATUS ads_set_machine_password(ADS_STRUCT *ads,
775                                     const char *machine_account,
776                                     const char *password)
777 {
778         ADS_STATUS status;
779         char *principal = NULL; 
780
781         /*
782           we need to use the '$' form of the name here (the machine account name), 
783           as otherwise the server might end up setting the password for a user
784           instead
785          */
786         asprintf(&principal, "%s@%s", machine_account, ads->config.realm);
787         
788         status = ads_krb5_set_password(ads->auth.kdc_server, principal, 
789                                        password, ads->auth.time_offset);
790         
791         free(principal);
792
793         return status;
794 }
795
796
797
798 #endif