summaryrefslogtreecommitdiff
path: root/source4/libads/krb5_setpw.c
blob: 9cf15221a8dd2417ca96415c300d4bdccdd5075f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
/* 
   Unix SMB/CIFS implementation.
   krb5 set password implementation
   Copyright (C) Andrew Tridgell 2001
   Copyright (C) Remus Koos 2001 (remuskoos@yahoo.com)
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"

#ifdef HAVE_KRB5

#define DEFAULT_KPASSWD_PORT	464
#define KRB5_KPASSWD_VERS_CHANGEPW		1
#define KRB5_KPASSWD_VERS_SETPW			2
#define KRB5_KPASSWD_VERS_SETPW_MS		0xff80
#define KRB5_KPASSWD_ACCESSDENIED		5
#define KRB5_KPASSWD_BAD_VERSION		6
#define KRB5_KPASSWD_INITIAL_FLAG_NEEDED	7

/* Those are defined by kerberos-set-passwd-02.txt and are probably 
 * not supported by M$ implementation */
#define KRB5_KPASSWD_POLICY_REJECT		8
#define KRB5_KPASSWD_BAD_PRINCIPAL		9
#define KRB5_KPASSWD_ETYPE_NOSUPP		10

/* This implements kerberos password change protocol as specified in 
 * kerb-chg-password-02.txt and kerberos-set-passwd-02.txt
 * as well as microsoft version of the protocol 
 * as specified in kerberos-set-passwd-00.txt
 */
static DATA_BLOB encode_krb5_setpw(const char *principal, const char *password)
{
        char* princ_part1 = NULL;
	char* princ_part2 = NULL;
	char* realm = NULL;
	char* c;
	char* princ;

	ASN1_DATA req;
	DATA_BLOB ret;


	princ = strdup(principal);

	if ((c = strchr(princ, '/')) == NULL) {
	    c = princ; 
	} else {
	    *c = '\0';
	    c++;
	    princ_part1 = princ;
	}

	princ_part2 = c;

	if ((c = strchr(c, '@')) != NULL) {
	    *c = '\0';
	    c++;
	    realm = c;
	}

	memset(&req, 0, sizeof(req));
	
	asn1_push_tag(&req, ASN1_SEQUENCE(0));
	asn1_push_tag(&req, ASN1_CONTEXT(0));
	asn1_write_OctetString(&req, password, strlen(password));
	asn1_pop_tag(&req);

	asn1_push_tag(&req, ASN1_CONTEXT(1));
	asn1_push_tag(&req, ASN1_SEQUENCE(0));

	asn1_push_tag(&req, ASN1_CONTEXT(0));
	asn1_write_Integer(&req, 1);
	asn1_pop_tag(&req);

	asn1_push_tag(&req, ASN1_CONTEXT(1));
	asn1_push_tag(&req, ASN1_SEQUENCE(0));

	if (princ_part1) 
	    asn1_write_GeneralString(&req, princ_part1);
	
	asn1_write_GeneralString(&req, princ_part2);
	asn1_pop_tag(&req);
	asn1_pop_tag(&req);
	asn1_pop_tag(&req);
	asn1_pop_tag(&req);

	asn1_push_tag(&req, ASN1_CONTEXT(2));
	asn1_write_GeneralString(&req, realm);
	asn1_pop_tag(&req);
	asn1_pop_tag(&req);

	ret = data_blob(req.data, req.length);
	asn1_free(&req);

	free(princ);

	return ret;
}	

static krb5_error_code build_kpasswd_request(uint16 pversion,
					   krb5_context context,
					   krb5_auth_context auth_context,
					   krb5_data *ap_req,
					   const char *princ,
					   const char *passwd,
					   krb5_data *packet)
{
	krb5_error_code ret;
	krb5_data cipherpw;
	krb5_data encoded_setpw;
	krb5_replay_data replay;
	char *p;
	DATA_BLOB setpw;

	ret = krb5_auth_con_setflags(context,
				     auth_context,KRB5_AUTH_CONTEXT_DO_SEQUENCE);
	if (ret) {
		DEBUG(1,("krb5_auth_con_setflags failed (%s)\n",
			 error_message(ret)));
		return ret;
	}

	/* handle protocol differences in chpw and setpw */
	if (pversion  == KRB5_KPASSWD_VERS_CHANGEPW)
		setpw = data_blob(passwd, strlen(passwd));
	else if (pversion == KRB5_KPASSWD_VERS_SETPW ||
		 pversion == KRB5_KPASSWD_VERS_SETPW_MS)
		setpw = encode_krb5_setpw(princ, passwd);
	else
		return EINVAL;

	encoded_setpw.data = (char *)setpw.data;
	encoded_setpw.length = setpw.length;

	ret = krb5_mk_priv(context, auth_context,
			   &encoded_setpw, &cipherpw, &replay);
	
	data_blob_free(&setpw); 	/*from 'encode_krb5_setpw(...)' */
	
	if (ret) {
		DEBUG(1,("krb5_mk_priv failed (%s)\n", error_message(ret)));
		return ret;
	}

	packet->data = (char *)malloc(ap_req->length + cipherpw.length + 6);
	if (!packet->data)
		return -1;

	/* see the RFC for details */
	p = ((char *)packet->data) + 2;
	RSSVAL(p, 0, pversion);
	p += 2;
	RSSVAL(p, 0, ap_req->length);
	p += 2;
	memcpy(p, ap_req->data, ap_req->length);
	p += ap_req->length;
	memcpy(p, cipherpw.data, cipherpw.length);
	p += cipherpw.length;
	packet->length = PTR_DIFF(p,packet->data);
	RSSVAL(packet->data, 0, packet->length);
	
	free(cipherpw.data);    /* from 'krb5_mk_priv(...)' */

	return 0;
}

static const struct kpasswd_errors {
	int result_code;
	const char *error_string;
} kpasswd_errors[] = {
	{KRB5_KPASSWD_MALFORMED, "Malformed request error"},
	{KRB5_KPASSWD_HARDERROR, "Server error"},
	{KRB5_KPASSWD_AUTHERROR, "Authentication error"},
	{KRB5_KPASSWD_SOFTERROR, "Password change rejected"},
	{KRB5_KPASSWD_ACCESSDENIED, "Client does not have proper authorization"},
	{KRB5_KPASSWD_BAD_VERSION, "Protocol version not supported"},
	{KRB5_KPASSWD_INITIAL_FLAG_NEEDED, "Authorization ticket must have initial flag set"},
	{KRB5_KPASSWD_POLICY_REJECT, "Password rejected due to policy requirements"},
	{KRB5_KPASSWD_BAD_PRINCIPAL, "Target principal does not exist"},
	{KRB5_KPASSWD_ETYPE_NOSUPP, "Unsupported encryption type"},
	{0, NULL}
};

static krb5_error_code setpw_result_code_string(krb5_context context,
						int result_code,
						const char **code_string)
{
        unsigned int idx = 0;

	while (kpasswd_errors[idx].error_string != NULL) {
		if (kpasswd_errors[idx].result_code == 
                    result_code) {
			*code_string = kpasswd_errors[idx].error_string;
			return 0;
		}
		idx++;
	}
	*code_string = "Password change failed";
        return (0);
}

static krb5_error_code parse_setpw_reply(krb5_context context, 
					 krb5_auth_context auth_context,
					 krb5_data *packet)
{
	krb5_data ap_rep;
	char *p;
	int vnum, ret, res_code;
	krb5_data cipherresult;
	krb5_data clearresult;
	krb5_ap_rep_enc_part *ap_rep_enc;
	krb5_replay_data replay;
	
	if (packet->length < 4) {
		return KRB5KRB_AP_ERR_MODIFIED;
	}
	
	p = packet->data;
	
	if (((char *)packet->data)[0] == 0x7e || ((char *)packet->data)[0] == 0x5e) {
		/* it's an error packet. We should parse it ... */
		DEBUG(1,("Got error packet 0x%x from kpasswd server\n",
			 ((char *)packet->data)[0]));
		return KRB5KRB_AP_ERR_MODIFIED;
	}
	
	if (RSVAL(p, 0) != packet->length) {
		DEBUG(1,("Bad packet length (%d/%d) from kpasswd server\n",
			 RSVAL(p, 0), packet->length));
		return KRB5KRB_AP_ERR_MODIFIED;
	}

	p += 2;

	vnum = RSVAL(p, 0); p += 2;

	/* FIXME: According to standard there is only one type of reply */	
	if (vnum != KRB5_KPASSWD_VERS_SETPW && 
	    vnum != KRB5_KPASSWD_VERS_SETPW_MS && 
	    vnum != KRB5_KPASSWD_VERS_CHANGEPW) {
		DEBUG(1,("Bad vnum (%d) from kpasswd server\n", vnum));
		return KRB5KDC_ERR_BAD_PVNO;
	}
	
	ap_rep.length = RSVAL(p, 0); p += 2;
	
	if (p + ap_rep.length >= (char *)packet->data + packet->length) {
		DEBUG(1,("ptr beyond end of packet from kpasswd server\n"));
		return KRB5KRB_AP_ERR_MODIFIED;
	}
	
	if (ap_rep.length == 0) {
		DEBUG(1,("got unencrypted setpw result?!\n"));
		return KRB5KRB_AP_ERR_MODIFIED;
	}

	/* verify ap_rep */
	ap_rep.data = p;
	p += ap_rep.length;
	
	ret = krb5_rd_rep(context, auth_context, &ap_rep, &ap_rep_enc);
	if (ret) {
		DEBUG(1,("failed to rd setpw reply (%s)\n", error_message(ret)));
		return KRB5KRB_AP_ERR_MODIFIED;
	}
	
	krb5_free_ap_rep_enc_part(context, ap_rep_enc);
	
	cipherresult.data = p;
	cipherresult.length = ((char *)packet->data + packet->length) - p;
		
	ret = krb5_rd_priv(context, auth_context, &cipherresult, &clearresult,
			   &replay);
	if (ret) {
		DEBUG(1,("failed to decrypt setpw reply (%s)\n", error_message(ret)));
		return KRB5KRB_AP_ERR_MODIFIED;
	}

	if (clearresult.length < 2) {
	        free(clearresult.data);
		ret = KRB5KRB_AP_ERR_MODIFIED;
		return KRB5KRB_AP_ERR_MODIFIED;
	}
	
	p = clearresult.data;
	
	res_code = RSVAL(p, 0);
	
	free(clearresult.data);

	if ((res_code < KRB5_KPASSWD_SUCCESS) || 
	    (res_code > KRB5_KPASSWD_ETYPE_NOSUPP)) {
		return KRB5KRB_AP_ERR_MODIFIED;
	}

	if(res_code == KRB5_KPASSWD_SUCCESS)
			return 0;
	else {
		const char *errstr;
		setpw_result_code_string(context, res_code, &errstr);
		DEBUG(1, ("Error changing password: %s\n", errstr));

		switch(res_code) {
			case KRB5_KPASSWD_ACCESSDENIED:
				return KRB5KDC_ERR_BADOPTION;
				break;
			case KRB5_KPASSWD_INITIAL_FLAG_NEEDED:
				return KRB5KDC_ERR_BADOPTION;
				/* return KV5M_ALT_METHOD; MIT-only define */
				break;
			case KRB5_KPASSWD_ETYPE_NOSUPP:
				return KRB5KDC_ERR_ETYPE_NOSUPP;
				break;
			case KRB5_KPASSWD_BAD_PRINCIPAL:
				return KRB5KDC_ERR_C_PRINCIPAL_UNKNOWN;
				break;
			case KRB5_KPASSWD_POLICY_REJECT:
				return KRB5KDC_ERR_POLICY;
				break;
			default:
				return KRB5KRB_ERR_GENERIC;
				break;
		}
	}
}

static ADS_STATUS do_krb5_kpasswd_request(krb5_context context,
					  const char *kdc_host,
					  uint16 pversion,
					  krb5_creds *credsp,
					  const char *princ,
					  const char *newpw)
{
	krb5_auth_context auth_context = NULL;
	krb5_data ap_req, chpw_req, chpw_rep;
	int ret, sock, addr_len;
	struct sockaddr remote_addr, local_addr;
	krb5_address local_kaddr, remote_kaddr;

	ret = krb5_mk_req_extended(context, &auth_context, AP_OPTS_USE_SUBKEY,
				   NULL, credsp, &ap_req);
	if (ret) {
		DEBUG(1,("krb5_mk_req_extended failed (%s)\n", error_message(ret)));
		return ADS_ERROR_KRB5(ret);
	}
	
	sock = open_udp_socket(kdc_host, DEFAULT_KPASSWD_PORT);
	if (sock == -1) {
		int rc = errno;
	        free(ap_req.data);
		krb5_auth_con_free(context, auth_context);
		DEBUG(1,("failed to open kpasswd socket to %s (%s)\n", 
			 kdc_host, strerror(errno)));
		return ADS_ERROR_SYSTEM(rc);
	}
	
	addr_len = sizeof(remote_addr);
	getpeername(sock, &remote_addr, &addr_len);
	addr_len = sizeof(local_addr);
	getsockname(sock, &local_addr, &addr_len);
	
	setup_kaddr(&remote_kaddr, &remote_addr);
	setup_kaddr(&local_kaddr, &local_addr);

	ret = krb5_auth_con_setaddrs(context, auth_context, &local_kaddr, NULL);
	if (ret) {
	        close(sock);
	        free(ap_req.data);
		krb5_auth_con_free(context, auth_context);
		DEBUG(1,("krb5_auth_con_setaddrs failed (%s)\n", error_message(ret)));
		return ADS_ERROR_KRB5(ret);
	}

	ret = build_kpasswd_request(pversion, context, auth_context, &ap_req,
				  princ, newpw, &chpw_req);
	if (ret) {
	        close(sock);
	        free(ap_req.data);
		krb5_auth_con_free(context, auth_context);
		DEBUG(1,("build_setpw_request failed (%s)\n", error_message(ret)));
		return ADS_ERROR_KRB5(ret);
	}

	if (write(sock, chpw_req.data, chpw_req.length) != chpw_req.length) {
	        close(sock);
		free(chpw_req.data);
	        free(ap_req.data);
		krb5_auth_con_free(context, auth_context);
		DEBUG(1,("send of chpw failed (%s)\n", strerror(errno)));
		return ADS_ERROR_SYSTEM(errno);
	}

	free(chpw_req.data);

	chpw_rep.length = 1500;
	chpw_rep.data = (char *) malloc(chpw_rep.length);
	if (!chpw_rep.data) {
	        close(sock);
	        free(ap_req.data);
		krb5_auth_con_free(context, auth_context);
		DEBUG(1,("send of chpw failed (%s)\n", strerror(errno)));
		errno = ENOMEM;
		return ADS_ERROR_SYSTEM(errno);
	}

	ret = read(sock, chpw_rep.data, chpw_rep.length);
	if (ret < 0) {
	        close(sock);
		free(chpw_rep.data);
		free(ap_req.data);
		krb5_auth_con_free(context, auth_context);
		DEBUG(1,("recv of chpw reply failed (%s)\n", strerror(errno)));
		return ADS_ERROR_SYSTEM(errno);
	}

	close(sock);
	chpw_rep.length = ret;

	ret = krb5_auth_con_setaddrs(context, auth_context, NULL,&remote_kaddr);
	if (ret) {
	        free(chpw_rep.data);
	        free(ap_req.data);
		krb5_auth_con_free(context, auth_context);
		DEBUG(1,("krb5_auth_con_setaddrs on reply failed (%s)\n", 
			 error_message(ret)));
		return ADS_ERROR_KRB5(ret);
	}

	ret = parse_setpw_reply(context, auth_context, &chpw_rep);
	free(chpw_rep.data);

	if (ret) {
	        free(ap_req.data);
		krb5_auth_con_free(context, auth_context);
		DEBUG(1,("parse_setpw_reply failed (%s)\n", 
			 error_message(ret)));
		return ADS_ERROR_KRB5(ret);
	}

	free(ap_req.data);
	krb5_auth_con_free(context, auth_context);

	return ADS_SUCCESS;
}

ADS_STATUS ads_krb5_set_password(const char *kdc_host, const char *princ, 
				 const char *newpw, int time_offset)
{

	ADS_STATUS aret;
	krb5_error_code ret;
	krb5_context context;
	krb5_principal principal;
	char *princ_name;
	char *realm;
	krb5_creds creds, *credsp;
	krb5_ccache ccache;

	ret = krb5_init_context(&context);
	if (ret) {
		DEBUG(1,("Failed to init krb5 context (%s)\n", error_message(ret)));
		return ADS_ERROR_KRB5(ret);
	}
	
	if (time_offset != 0) {
		krb5_set_real_time(context, time(NULL) + time_offset, 0);
	}

	ret = krb5_cc_default(context, &ccache);
	if (ret) {
	        krb5_free_context(context);
		DEBUG(1,("Failed to get default creds (%s)\n", error_message(ret)));
		return ADS_ERROR_KRB5(ret);
	}

	ZERO_STRUCT(creds);
	
	realm = strchr(princ, '@');
	realm++;

	asprintf(&princ_name, "kadmin/changepw@%s", realm);
	ret = krb5_parse_name(context, princ_name, &creds.server);
	if (ret) {
                krb5_free_context(context);
		DEBUG(1,("Failed to parse kadmin/changepw (%s)\n", error_message(ret)));
		return ADS_ERROR_KRB5(ret);
	}
	free(princ_name);

	/* parse the principal we got as a function argument */
	ret = krb5_parse_name(context, princ, &principal);
	if (ret) {
                krb5_free_context(context);
		DEBUG(1,("Failed to parse %s (%s)\n", princ_name, error_message(ret)));
		return ADS_ERROR_KRB5(ret);
	}

	krb5_princ_set_realm(context, creds.server,
			     krb5_princ_realm(context, principal));
	
	ret = krb5_cc_get_principal(context, ccache, &creds.client);
	if (ret) {
	        krb5_free_principal(context, principal);
                krb5_free_context(context);
		DEBUG(1,("Failed to get principal from ccache (%s)\n", 
			 error_message(ret)));
		return ADS_ERROR_KRB5(ret);
	}
	
	ret = krb5_get_credentials(context, 0, ccache, &creds, &credsp); 
	if (ret) {
	        krb5_free_principal(context, creds.client);
	        krb5_free_principal(context, principal);
	        krb5_free_context(context);
		DEBUG(1,("krb5_get_credentials failed (%s)\n", error_message(ret)));
		return ADS_ERROR_KRB5(ret);
	}
	
	/* we might have to call krb5_free_creds(...) from now on ... */

	aret = do_krb5_kpasswd_request(context, kdc_host,
				       KRB5_KPASSWD_VERS_SETPW_MS,
				       credsp, princ, newpw);

	krb5_free_creds(context, credsp);
	krb5_free_principal(context, creds.client);
	krb5_free_principal(context, principal);
	krb5_free_context(context);

	return aret;
}

/*
  we use a prompter to avoid a crash bug in the kerberos libs when 
  dealing with empty passwords
  this prompter is just a string copy ...
*/
static krb5_error_code 
kerb_prompter(krb5_context ctx, void *data,
	       const char *name,
	       const char *banner,
	       int num_prompts,
	       krb5_prompt prompts[])
{
	if (num_prompts == 0) return 0;

	memset(prompts[0].reply->data, 0, prompts[0].reply->length);
	if (prompts[0].reply->length > 0) {
		if (data) {
			strncpy(prompts[0].reply->data, data, prompts[0].reply->length-1);
			prompts[0].reply->length = strlen(prompts[0].reply->data);
		} else {
			prompts[0].reply->length = 0;
		}
	}
	return 0;
}

static ADS_STATUS ads_krb5_chg_password(const char *kdc_host,
					const char *principal,
					const char *oldpw, 
					const char *newpw, 
					int time_offset)
{
    ADS_STATUS aret;
    krb5_error_code ret;
    krb5_context context;
    krb5_principal princ;
    krb5_get_init_creds_opt opts;
    krb5_creds creds;
    char *chpw_princ = NULL, *password;

    ret = krb5_init_context(&context);
    if (ret) {
	DEBUG(1,("Failed to init krb5 context (%s)\n", error_message(ret)));
	return ADS_ERROR_KRB5(ret);
    }

    if ((ret = krb5_parse_name(context, principal,
                                    &princ))) {
	krb5_free_context(context);
	DEBUG(1,("Failed to parse %s (%s)\n", principal, error_message(ret)));
	return ADS_ERROR_KRB5(ret);
    }

    krb5_get_init_creds_opt_init(&opts);
    krb5_get_init_creds_opt_set_tkt_life(&opts, 5*60);
    krb5_get_init_creds_opt_set_renew_life(&opts, 0);
    krb5_get_init_creds_opt_set_forwardable(&opts, 0);
    krb5_get_init_creds_opt_set_proxiable(&opts, 0);

    /* We have to obtain an INITIAL changepw ticket for changing password */
    asprintf(&chpw_princ, "kadmin/changepw@%s",
				(char *) krb5_princ_realm(context, princ));
    password = strdup(oldpw);
    ret = krb5_get_init_creds_password(context, &creds, princ, password,
					   kerb_prompter, NULL, 
					   0, chpw_princ, &opts);
    SAFE_FREE(chpw_princ);
    SAFE_FREE(password);

    if (ret) {
      if (ret == KRB5KRB_AP_ERR_BAD_INTEGRITY)
	DEBUG(1,("Password incorrect while getting initial ticket"));
      else
	DEBUG(1,("krb5_get_init_creds_password failed (%s)\n", error_message(ret)));

	krb5_free_principal(context, princ);
	krb5_free_context(context);
	return ADS_ERROR_KRB5(ret);
    }

    aret = do_krb5_kpasswd_request(context, kdc_host,
				   KRB5_KPASSWD_VERS_CHANGEPW,
				   &creds, principal, newpw);

    krb5_free_principal(context, princ);
    krb5_free_context(context);

    return aret;
}


ADS_STATUS kerberos_set_password(const char *kpasswd_server, 
				 const char *auth_principal, const char *auth_password,
				 const char *target_principal, const char *new_password,
				 int time_offset)
{
    int ret;

    if ((ret = kerberos_kinit_password(auth_principal, auth_password, time_offset))) {
	DEBUG(1,("Failed kinit for principal %s (%s)\n", auth_principal, error_message(ret)));
	return ADS_ERROR_KRB5(ret);
    }

    if (!strcmp(auth_principal, target_principal))
	return ads_krb5_chg_password(kpasswd_server, target_principal,
				     auth_password, new_password, time_offset);
    else
    	return ads_krb5_set_password(kpasswd_server, target_principal,
				     new_password, time_offset);
}


/**
 * Set the machine account password
 * @param ads connection to ads server
 * @param hostname machine whose password is being set
 * @param password new password
 * @return status of password change
 **/
ADS_STATUS ads_set_machine_password(ADS_STRUCT *ads,
				    const char *machine_account,
				    const char *password)
{
	ADS_STATUS status;
	char *principal = NULL; 

	/*
	  we need to use the '$' form of the name here (the machine account name), 
	  as otherwise the server might end up setting the password for a user
	  instead
	 */
	asprintf(&principal, "%s@%s", machine_account, ads->config.realm);
	
	status = ads_krb5_set_password(ads->auth.kdc_server, principal, 
				       password, ads->auth.time_offset);
	
	free(principal);

	return status;
}



#endif