aboutsummaryrefslogtreecommitdiff
path: root/src/lib/krb5/os/changepw.c
blob: 3355538c88dcf1fc6a642f25f9b6bfb149bb2702 (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
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
/*
 * lib/krb5/os/changepw.c
 *
 * Copyright 1990,1999 by the Massachusetts Institute of Technology.
 * All Rights Reserved.
 *
 * Export of this software from the United States of America may
 *   require a specific license from the United States Government.
 *   It is the responsibility of any person or organization contemplating
 *   export to obtain such a license before exporting.
 * 
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
 * distribute this software and its documentation for any purpose and
 * without fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation, and that
 * the name of M.I.T. not be used in advertising or publicity pertaining
 * to distribution of the software without specific, written prior
 * permission.  Furthermore if you modify this software you must label
 * your software as modified software and not distribute it in such a
 * fashion that it might be confused with the original M.I.T. software.
 * M.I.T. makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is" without express
 * or implied warranty.
 * 
 */

#define NEED_SOCKETS
#include "k5-int.h"
#include "adm_err.h"

#include <stdio.h>
#include <errno.h>

/* Win32 defines. */
#if defined(_WIN16) || (defined(_WIN32) && !defined(__CYGWIN32__))
#ifndef ECONNABORTED
#define ECONNABORTED WSAECONNABORTED
#endif
#ifndef ECONNREFUSED
#define ECONNREFUSED WSAECONNREFUSED
#endif
#ifndef EHOSTUNREACH
#define EHOSTUNREACH WSAEHOSTUNREACH
#endif
#endif /* _WIN32 && !__CYGWIN32__ */

#ifdef KRB5_DNS_LOOKUP /* pbh says we can wrap this last-minute
			  addition and ignore testing it since it's
			  not part of what mit officially supports in
			  this release */

/*
 * Wrapper function for the two backends
 */

krb5_error_code 
krb5_locate_srv_conf(context, realm, name, addr_pp, naddrs, master_index, nmasters);
#ifdef KRB5_DNS_LOOKUP
krb5_error_code krb5_locate_srv_dns(realm, service, protocol, addr_pp, naddrs);
#endif

static krb5_error_code
krb5_locate_kpasswd(context, realm, addr_pp, naddrs, master_index, nmasters)
    krb5_context context;
    const krb5_data *realm;
    struct sockaddr **addr_pp;
    int *naddrs;
    int *master_index;
    int *nmasters;
{
    krb5_error_code code;
#ifdef KRB5_DNS_LOOKUP
    struct sockaddr *admin_addr_p, *kdc_addr_p;
    int nadmin_addrs, nkdc_addrs;
    int i,j;
#endif /* KRB5_DNS_LOOKUP */

    /*
     * We always try the local file first
     */

    code = krb5_locate_srv_conf(context, realm, "kpasswd_server", addr_pp, naddrs,
                                 master_index, nmasters);
    if (code) {
        code = krb5_locate_srv_conf(context, realm, "admin_server", addr_pp, naddrs,
                                     master_index, nmasters);
        if ( !code ) {
            /* success with admin_server but now we need to change the port */
            /* number to use DEFAULT_KPASSWD_PORT.                          */
            for ( i=0;i<*naddrs;i++ ) {
                struct sockaddr_in *sin = (struct sockaddr_in *) &addr_pp[i];
                sin->sin_port = htons(DEFAULT_KPASSWD_PORT);
            }
        }
    }

#ifdef KRB5_DNS_LOOKUP
    if (code) {
        int use_dns = _krb5_use_dns(context);
        if ( use_dns ) {
            code = krb5_locate_srv_dns(realm, "_kpasswd", "_udp",
                                        addr_pp, naddrs);
            if ( code ) {
                code = krb5_locate_srv_dns(realm, 
                                            "_kerberos-adm", 
                                            "_tcp",
                                            addr_pp, naddrs);
                if ( !code ) {
                    /* success with admin_server but now we need to change the port */
                    /* number to use DEFAULT_KPASSWD_PORT.                          */
                    for ( i=0;i<*naddrs;i++ ) {
                        struct sockaddr_in *sin = (struct sockaddr_in *) &(*addr_pp)[i];
                        sin->sin_port = htons(DEFAULT_KPASSWD_PORT);
                    }
                }
            }
            if ( !code && master_index && nmasters ) {
                *master_index = 1;
                *nmasters = *naddrs;
            }
        }
    }
#endif /* KRB5_DNS_LOOKUP */
    return (code);
}


KRB5_DLLIMP krb5_error_code KRB5_CALLCONV
krb5_change_password(context, creds, newpw, result_code,
		     result_code_string, result_string)
    krb5_context context;
    krb5_creds *creds;
    char *newpw;
    int *result_code;
    krb5_data *result_code_string;
    krb5_data *result_string;
{
    krb5_auth_context auth_context;
    krb5_data ap_req, chpw_req, chpw_rep;
    krb5_address local_kaddr, remote_kaddr;
    char *code_string;
    krb5_error_code code = 0;
    int i, addrlen;
    struct sockaddr *addr_p, local_addr, remote_addr, tmp_addr;
    int naddr_p;
    int cc, local_result_code, tmp_len;
    SOCKET s1 = INVALID_SOCKET, s2 = INVALID_SOCKET;


    /* Initialize values so that cleanup call can safely check for NULL */
    auth_context = NULL;
    addr_p = NULL;
    memset(&chpw_req, 0, sizeof(krb5_data));
    memset(&chpw_rep, 0, sizeof(krb5_data));
    memset(&ap_req, 0, sizeof(krb5_data));
    
    /* initialize auth_context so that we know we have to free it */
    if ((code = krb5_auth_con_init(context, &auth_context)))
	  goto cleanup;
    
    if (code = krb5_mk_req_extended(context, &auth_context, AP_OPTS_USE_SUBKEY,
				    NULL, creds, &ap_req))
	  goto cleanup;

    if (code = krb5_locate_kpasswd(context, 
                                    krb5_princ_realm(context, creds->client), 
                                    &addr_p, &naddr_p,NULL,NULL))
        goto cleanup;

    /* this is really obscure.  s1 is used for all communications.  it
       is left unconnected in case the server is multihomed and routes
       are asymmetric.  s2 is connected to resolve routes and get
       addresses.  this is the *only* way to get proper addresses for
       multihomed hosts if routing is asymmetric.  

       A related problem in the server, but not the client, is that
       many os's have no way to disconnect a connected udp socket, so
       the s2 socket needs to be closed and recreated for each
       request.  The s1 socket must not be closed, or else queued
       requests will be lost.

       A "naive" client implementation (one socket, no connect,
       hostname resolution to get the local ip addr) will work and
       interoperate if the client is single-homed. */

    if ((s1 = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET) 
      {
	    code = SOCKET_ERRNO;
	    goto cleanup;
      }

    if ((s2 = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET)
      {
	    code = SOCKET_ERRNO;
	    goto cleanup;
      }

    for (i=0; i<naddr_p; i++) 
      {
		fd_set fdset;
		struct timeval timeout;

		if (connect(s2, &addr_p[i], sizeof(addr_p[i])) == SOCKET_ERROR) 
		  {
		    if ((SOCKET_ERRNO == ECONNREFUSED) || (SOCKET_ERRNO == EHOSTUNREACH))
			  continue; /* try the next addr */
		    
		    code = SOCKET_ERRNO;
		    goto cleanup;
		  }
      
        addrlen = sizeof(local_addr);

		if (getsockname(s2, &local_addr, &addrlen) < 0) 
		  {
		    if ((SOCKET_ERRNO == ECONNREFUSED) || (SOCKET_ERRNO == EHOSTUNREACH))
			  continue; /* try the next addr */
		    
		    code = SOCKET_ERRNO;
			goto cleanup;
		  }

		/* some brain-dead OS's don't return useful information from
		 * the getsockname call.  Namely, windows and solaris.  */

		if (((struct sockaddr_in *)&local_addr)->sin_addr.s_addr != 0) 
		  {
		    local_kaddr.addrtype = ADDRTYPE_INET;
		    local_kaddr.length = sizeof(((struct sockaddr_in *) &local_addr)->sin_addr);
		    local_kaddr.contents = (krb5_octet *) &(((struct sockaddr_in *) &local_addr)->sin_addr);
		  } 
		else 
		  {
		    krb5_address **addrs;

		    krb5_os_localaddr(context, &addrs);
		    
		    local_kaddr.magic = addrs[0]->magic;
		    local_kaddr.addrtype = addrs[0]->addrtype;
		    local_kaddr.length = addrs[0]->length;
		    local_kaddr.contents = malloc(addrs[0]->length);
		    memcpy(local_kaddr.contents, addrs[0]->contents, addrs[0]->length);

		    krb5_free_addresses(context, addrs);
		  }

		addrlen = sizeof(remote_addr);
		if (getpeername(s2, &remote_addr, &addrlen) < 0) 
		  {
		    if ((SOCKET_ERRNO == ECONNREFUSED) || (SOCKET_ERRNO == EHOSTUNREACH))
			  continue; /* try the next addr */
		    
		    code = SOCKET_ERRNO;
			goto cleanup;
		  }

		remote_kaddr.addrtype = ADDRTYPE_INET;
		remote_kaddr.length = sizeof(((struct sockaddr_in *) &remote_addr)->sin_addr);
		remote_kaddr.contents = (krb5_octet *) &(((struct sockaddr_in *) &remote_addr)->sin_addr);

		/* mk_priv requires that the local address be set.
		  getsockname is used for this.  rd_priv requires that the
		  remote address be set.  recvfrom is used for this.  If
		  rd_priv is given a local address, and the message has the
		  recipient addr in it, this will be checked.  However, there
		  is simply no way to know ahead of time what address the
		  message will be delivered *to*.  Therefore, it is important
		  that either no recipient address is in the messages when
		  mk_priv is called, or that no local address is passed to
		  rd_priv.  Both is a better idea, and I have done that.  In
		  summary, when mk_priv is called, *only* a local address is
		  specified.  when rd_priv is called, *only* a remote address
		  is specified.  Are we having fun yet?  */

		if (code = krb5_auth_con_setaddrs(context, auth_context, &local_kaddr, NULL)) 
		  {
		    code = SOCKET_ERRNO;
			goto cleanup;
		  }

		if (code = krb5_mk_chpw_req(context, auth_context, &ap_req, newpw, &chpw_req)) 
		  {
		    code = SOCKET_ERRNO;
			goto cleanup;
		  }

		if ((cc = sendto(s1, chpw_req.data, chpw_req.length, 0, 
		                 (struct sockaddr *) &addr_p[i], 
		                 sizeof(addr_p[i]))) != chpw_req.length) 
		  {
		    if ((cc < 0) && ((SOCKET_ERRNO == ECONNREFUSED) ||
				     (SOCKET_ERRNO == EHOSTUNREACH)))
			  continue; /* try the next addr */
		    
		    code = (cc < 0) ? SOCKET_ERRNO : ECONNABORTED;
			goto cleanup;
		  }

		chpw_rep.length = 1500;
		chpw_rep.data = (char *) malloc(chpw_rep.length);

		/* XXX need a timeout/retry loop here */
		FD_ZERO (&fdset);
		FD_SET (s1, &fdset);
		timeout.tv_sec = 120;
		timeout.tv_usec = 0;
		switch (select (s1 + 1, &fdset, 0, 0, &timeout)) {
		case -1:
		    code = SOCKET_ERRNO;
		    goto cleanup;
		case 0:
		    code = ETIMEDOUT;
		    goto cleanup;
		default:
		    /* fall through */
		    ;
		}

		/* "recv" would be good enough here... except that Windows/NT
		   commits the atrocity of returning -1 to indicate failure,
		   but leaving errno set to 0.
		   
		   "recvfrom(...,NULL,NULL)" would seem to be a good enough
		   alternative, and it works on NT, but it doesn't work on
		   SunOS 4.1.4 or Irix 5.3.  Thus we must actually accept the
		   value and discard it. */
		tmp_len = sizeof(tmp_addr);
		if ((cc = recvfrom(s1, chpw_rep.data, chpw_rep.length, 0, &tmp_addr, &tmp_len)) < 0) 
		  {
		    code = SOCKET_ERRNO;
		    goto cleanup;
		  }

		closesocket(s1);
		s1 = INVALID_SOCKET;
		closesocket(s2);
		s2 = INVALID_SOCKET;

		chpw_rep.length = cc;

		if (code = krb5_auth_con_setaddrs(context, auth_context, NULL, &remote_kaddr)) 
		  goto cleanup;

		if(code = krb5_rd_chpw_rep(context, auth_context, &chpw_rep,
					&local_result_code, result_string))
		  goto cleanup;

		if (result_code)
		  *result_code = local_result_code;

		if (result_code_string) 
		  {
		    if (code = krb5_chpw_result_code_string(context, local_result_code,
							    &code_string))
			  goto cleanup;

		    result_code_string->length = strlen(code_string);
		    if ((result_code_string->data =
			    (char *) malloc(result_code_string->length)) == NULL)
			  return(ENOMEM);
		    strncpy(result_code_string->data, code_string, result_code_string->length);
		  }

		code = 0;
		goto cleanup;
      }

    code = SOCKET_ERRNO;
    
cleanup:
    if(auth_context != NULL)
      krb5_auth_con_free(context, auth_context);
    
    if(addr_p != NULL)
      krb5_xfree(addr_p);
    
    if(s1 != INVALID_SOCKET)
      closesocket(s1);
    
    if(s2 != INVALID_SOCKET)
      closesocket(s2);
      
    krb5_free_data_contents(context, &chpw_req);
    krb5_free_data_contents(context, &chpw_rep);
	krb5_free_data_contents(context, &ap_req);
    
    return(code);
}

#else /* big KRB5_DNS_LOOKUP wrapper */

KRB5_DLLIMP krb5_error_code KRB5_CALLCONV
krb5_change_password(context, creds, newpw, result_code,
		     result_code_string, result_string)
    krb5_context context;
    krb5_creds *creds;
    char *newpw;
    int *result_code;
    krb5_data *result_code_string;
    krb5_data *result_string;
{
    krb5_auth_context auth_context;
    krb5_data ap_req, chpw_req, chpw_rep;
    krb5_address local_kaddr, remote_kaddr;
    const char *realm_kdc_names[4];
    int default_port;
    char **hostlist, *host, *tmphost, *port, *cp, *code_string;
    krb5_error_code code = 0;
    int i, j, out, count, addrlen;
    struct sockaddr *addr_p, local_addr, remote_addr, tmp_addr;
    struct sockaddr_in *sin_p;
    struct hostent *hp;
    struct servent *sp;
#ifdef HAVE_NETINET_IN_H
    u_short udpport = htons(KRB5_DEFAULT_PORT);
#endif
    int cc, local_result_code, tmp_len;
    SOCKET s1 = INVALID_SOCKET, s2 = INVALID_SOCKET;

    /* Initialize values so that cleanup call can safely check for NULL */
    auth_context = NULL;
    addr_p = NULL;
    host = NULL;
    hostlist = NULL;
    memset(&chpw_req, 0, sizeof(krb5_data));
    memset(&chpw_rep, 0, sizeof(krb5_data));
    memset(&ap_req, 0, sizeof(krb5_data));
    
    /* initialize auth_context so that we know we have to free it */
    if ((code = krb5_auth_con_init(context, &auth_context)))
	  goto cleanup;
    
    if (code = krb5_mk_req_extended(context, &auth_context, AP_OPTS_USE_SUBKEY,
				    NULL, creds, &ap_req))
	  goto cleanup;

    if ((host = malloc(krb5_princ_realm(context, creds->client)->length + 1)) == NULL) 
	  {
	    code = ENOMEM;
	    goto cleanup;
	  }

    strncpy(host, krb5_princ_realm(context, creds->client)->data,
	    krb5_princ_realm(context, creds->client)->length);
    host[krb5_princ_realm(context, creds->client)->length] = '\0';
    hostlist = 0;
    
    realm_kdc_names[0] = "realms";
    realm_kdc_names[1] = host;
    realm_kdc_names[2] = "kpasswd_server";
    realm_kdc_names[3] = 0;

    default_port = 0;

    code = profile_get_values(context->profile, realm_kdc_names, &hostlist);

    if (code == PROF_NO_RELATION) 
      {
        realm_kdc_names[2] = "admin_server";
        default_port = 1;
        code = profile_get_values(context->profile, realm_kdc_names, &hostlist);
      }

    if (code == PROF_NO_SECTION)
      {
        code = KRB5_REALM_UNKNOWN;
        goto cleanup;
      }
    else 
      if (code == PROF_NO_RELATION)
        {
          code = KRB5_CONFIG_BADFORMAT;
          goto cleanup;
        }
      else 
        if (code)
          goto cleanup;

#ifdef HAVE_NETINET_IN_H
    /* XXX should look for "kpasswd" in /etc/services */
    udpport = htons(DEFAULT_KPASSWD_PORT);
#endif

    count = 0;
    while (hostlist && hostlist[count])
	    count++;
    
    if (count == 0)
      {
        /* XXX */
        code = KADM_NO_HOST;
        goto cleanup;
      }
    
    addr_p = (struct sockaddr *) malloc(sizeof(struct sockaddr) * count);
    if (addr_p == NULL)
      {
        code = ENOMEM;
        goto cleanup;
      }

    tmphost = hostlist[0];
    out = 0;

    /*
     * Strip off excess whitespace
     */
    cp = strchr(tmphost, ' ');
    if (cp)
      *cp = 0;
    cp = strchr(tmphost, '\t');
    if (cp)
      *cp = 0;
    port = strchr(tmphost, ':');
    if (port) {
      *port = 0;
	port++;
	/* if the admin_server line was used, ignore the specified
           port */
	if (default_port)
	    port = NULL;
    }
    hp = gethostbyname(hostlist[0]);

    if (hp != 0) 
      {
        switch (hp->h_addrtype) 
          {
#ifdef HAVE_NETINET_IN_H
          case AF_INET:
            for (j=0; hp->h_addr_list[j]; j++) 
              {
                sin_p = (struct sockaddr_in *) &addr_p[out++];
                memset ((char *)sin_p, 0, sizeof(struct sockaddr));
                sin_p->sin_family = hp->h_addrtype;
                sin_p->sin_port = port ? htons(atoi(port)) : udpport;
                memcpy((char *)&sin_p->sin_addr,
                       (char *)hp->h_addr_list[j],
                       sizeof(struct in_addr));
                if (out+1 >= count) 
                  {
                    count += 5;
                    addr_p = (struct sockaddr *)
                    realloc ((char *)addr_p, sizeof(struct sockaddr) * count);
                    if (addr_p == NULL)
                      {
                        code = ENOMEM;
                        goto cleanup;
                      }
                  }
                }
              break;
#endif
          default:
            break;
        }
      }

    if (out == 0) 
      {     
        /* Couldn't resolve any KDC names */
        code = KADM_NO_HOST;
        goto cleanup;
      }

    /* this is really obscure.  s1 is used for all communications.  it
       is left unconnected in case the server is multihomed and routes
       are asymmetric.  s2 is connected to resolve routes and get
       addresses.  this is the *only* way to get proper addresses for
       multihomed hosts if routing is asymmetric.  

       A related problem in the server, but not the client, is that
       many os's have no way to disconnect a connected udp socket, so
       the s2 socket needs to be closed and recreated for each
       request.  The s1 socket must not be closed, or else queued
       requests will be lost.

       A "naive" client implementation (one socket, no connect,
       hostname resolution to get the local ip addr) will work and
       interoperate if the client is single-homed. */

    if ((s1 = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET) 
      {
	    code = SOCKET_ERRNO;
	    goto cleanup;
      }

    if ((s2 = socket(AF_INET, SOCK_DGRAM, 0)) == INVALID_SOCKET)
      {
	    code = SOCKET_ERRNO;
	    goto cleanup;
      }

    for (i=0; i<out; i++) 
      {
		fd_set fdset;
		struct timeval timeout;

		if (connect(s2, &addr_p[i], sizeof(addr_p[i])) == SOCKET_ERROR) 
		  {
		    if ((SOCKET_ERRNO == ECONNREFUSED) || (SOCKET_ERRNO == EHOSTUNREACH))
			  continue; /* try the next addr */
		    
		    code = SOCKET_ERRNO;
		    goto cleanup;
		  }
      
        addrlen = sizeof(local_addr);

		if (getsockname(s2, &local_addr, &addrlen) < 0) 
		  {
		    if ((SOCKET_ERRNO == ECONNREFUSED) || (SOCKET_ERRNO == EHOSTUNREACH))
			  continue; /* try the next addr */
		    
		    code = SOCKET_ERRNO;
			goto cleanup;
		  }

		/* some brain-dead OS's don't return useful information from
		 * the getsockname call.  Namely, windows and solaris.  */

		if (((struct sockaddr_in *)&local_addr)->sin_addr.s_addr != 0) 
		  {
		    local_kaddr.addrtype = ADDRTYPE_INET;
		    local_kaddr.length = sizeof(((struct sockaddr_in *) &local_addr)->sin_addr);
		    local_kaddr.contents = (krb5_octet *) &(((struct sockaddr_in *) &local_addr)->sin_addr);
		  } 
		else 
		  {
		    krb5_address **addrs;

		    krb5_os_localaddr(context, &addrs);
		    
		    local_kaddr.magic = addrs[0]->magic;
		    local_kaddr.addrtype = addrs[0]->addrtype;
		    local_kaddr.length = addrs[0]->length;
		    local_kaddr.contents = malloc(addrs[0]->length);
		    memcpy(local_kaddr.contents, addrs[0]->contents, addrs[0]->length);

		    krb5_free_addresses(context, addrs);
		  }

		addrlen = sizeof(remote_addr);
		if (getpeername(s2, &remote_addr, &addrlen) < 0) 
		  {
		    if ((SOCKET_ERRNO == ECONNREFUSED) || (SOCKET_ERRNO == EHOSTUNREACH))
			  continue; /* try the next addr */
		    
		    code = SOCKET_ERRNO;
			goto cleanup;
		  }

		remote_kaddr.addrtype = ADDRTYPE_INET;
		remote_kaddr.length = sizeof(((struct sockaddr_in *) &remote_addr)->sin_addr);
		remote_kaddr.contents = (krb5_octet *) &(((struct sockaddr_in *) &remote_addr)->sin_addr);

		/* mk_priv requires that the local address be set.
		  getsockname is used for this.  rd_priv requires that the
		  remote address be set.  recvfrom is used for this.  If
		  rd_priv is given a local address, and the message has the
		  recipient addr in it, this will be checked.  However, there
		  is simply no way to know ahead of time what address the
		  message will be delivered *to*.  Therefore, it is important
		  that either no recipient address is in the messages when
		  mk_priv is called, or that no local address is passed to
		  rd_priv.  Both is a better idea, and I have done that.  In
		  summary, when mk_priv is called, *only* a local address is
		  specified.  when rd_priv is called, *only* a remote address
		  is specified.  Are we having fun yet?  */

		if (code = krb5_auth_con_setaddrs(context, auth_context, &local_kaddr, NULL)) 
		  {
		    code = SOCKET_ERRNO;
			goto cleanup;
		  }

		if (code = krb5_mk_chpw_req(context, auth_context, &ap_req, newpw, &chpw_req)) 
		  {
		    code = SOCKET_ERRNO;
			goto cleanup;
		  }

		if ((cc = sendto(s1, chpw_req.data, chpw_req.length, 0, 
		                 (struct sockaddr *) &addr_p[i], 
		                 sizeof(addr_p[i]))) != chpw_req.length) 
		  {
		    if ((cc < 0) && ((SOCKET_ERRNO == ECONNREFUSED) ||
				     (SOCKET_ERRNO == EHOSTUNREACH)))
			  continue; /* try the next addr */
		    
		    code = (cc < 0) ? SOCKET_ERRNO : ECONNABORTED;
			goto cleanup;
		  }

		chpw_rep.length = 1500;
		chpw_rep.data = (char *) malloc(chpw_rep.length);

		/* XXX need a timeout/retry loop here */

		FD_ZERO (&fdset);
		FD_SET (s1, &fdset);
		timeout.tv_sec = 120;
		timeout.tv_usec = 0;
		switch (select (s1 + 1, &fdset, 0, 0, &timeout)) {
		case -1:
		    code = SOCKET_ERRNO;
		    goto cleanup;
		case 0:
		    code = ETIMEDOUT;
		    goto cleanup;
		default:
		    /* fall through */
		    ;
		}

		/* "recv" would be good enough here... except that Windows/NT
		   commits the atrocity of returning -1 to indicate failure,
		   but leaving errno set to 0.
		   
		   "recvfrom(...,NULL,NULL)" would seem to be a good enough
		   alternative, and it works on NT, but it doesn't work on
		   SunOS 4.1.4 or Irix 5.3.  Thus we must actually accept the
		   value and discard it. */
		tmp_len = sizeof(tmp_addr);
		if ((cc = recvfrom(s1, chpw_rep.data, chpw_rep.length, 0, &tmp_addr, &tmp_len)) < 0) 
		  {
		    code = SOCKET_ERRNO;
		    goto cleanup;
		  }

		closesocket(s1);
		s1 = INVALID_SOCKET;
		closesocket(s2);
		s2 = INVALID_SOCKET;

		chpw_rep.length = cc;

		if (code = krb5_auth_con_setaddrs(context, auth_context, NULL, &remote_kaddr)) 
		  goto cleanup;

		if(code = krb5_rd_chpw_rep(context, auth_context, &chpw_rep,
					&local_result_code, result_string))
		  goto cleanup;

		if (result_code)
		  *result_code = local_result_code;

		if (result_code_string) 
		  {
		    if (code = krb5_chpw_result_code_string(context, local_result_code,
							    &code_string))
			  goto cleanup;

		    result_code_string->length = strlen(code_string);
		    if ((result_code_string->data =
			    (char *) malloc(result_code_string->length)) == NULL)
			  return(ENOMEM);
		    strncpy(result_code_string->data, code_string, result_code_string->length);
		  }

		code = 0;
		goto cleanup;
      }

    code = SOCKET_ERRNO;
    
cleanup:
    if(auth_context != NULL)
      krb5_auth_con_free(context, auth_context);
    
    if(host != NULL)
      krb5_xfree(host);
    
    if(addr_p != NULL)
      krb5_xfree(addr_p);
    
    if(hostlist != NULL)
      profile_free_list(hostlist);
      
    if(s1 != INVALID_SOCKET)
      closesocket(s1);
    
    if(s2 != INVALID_SOCKET)
      closesocket(s2);
      
    krb5_free_data_contents(context, &chpw_req);
    krb5_free_data_contents(context, &chpw_rep);
	krb5_free_data_contents(context, &ap_req);
    
    return(code);
}

#endif /* big KRB5_DNS_LOOKUP wrapper */