17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * lib/krb5/krb/copy_addrs.c
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * Copyright 1990 by the Massachusetts Institute of Technology.
57c478bd9Sstevel@tonic-gate * All Rights Reserved.
67c478bd9Sstevel@tonic-gate *
77c478bd9Sstevel@tonic-gate * Export of this software from the United States of America may
87c478bd9Sstevel@tonic-gate * require a specific license from the United States Government.
97c478bd9Sstevel@tonic-gate * It is the responsibility of any person or organization contemplating
107c478bd9Sstevel@tonic-gate * export to obtain such a license before exporting.
11*55fea89dSDan Cross *
127c478bd9Sstevel@tonic-gate * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
137c478bd9Sstevel@tonic-gate * distribute this software and its documentation for any purpose and
147c478bd9Sstevel@tonic-gate * without fee is hereby granted, provided that the above copyright
157c478bd9Sstevel@tonic-gate * notice appear in all copies and that both that copyright notice and
167c478bd9Sstevel@tonic-gate * this permission notice appear in supporting documentation, and that
177c478bd9Sstevel@tonic-gate * the name of M.I.T. not be used in advertising or publicity pertaining
187c478bd9Sstevel@tonic-gate * to distribution of the software without specific, written prior
197c478bd9Sstevel@tonic-gate * permission. Furthermore if you modify this software you must label
207c478bd9Sstevel@tonic-gate * your software as modified software and not distribute it in such a
217c478bd9Sstevel@tonic-gate * fashion that it might be confused with the original M.I.T. software.
227c478bd9Sstevel@tonic-gate * M.I.T. makes no representations about the suitability of
237c478bd9Sstevel@tonic-gate * this software for any purpose. It is provided "as is" without express
247c478bd9Sstevel@tonic-gate * or implied warranty.
25*55fea89dSDan Cross *
267c478bd9Sstevel@tonic-gate *
277c478bd9Sstevel@tonic-gate * krb5_copy_addresses()
287c478bd9Sstevel@tonic-gate */
297c478bd9Sstevel@tonic-gate
30159d09a2SMark Phalan #include "k5-int.h"
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate /*ARGSUSED*/
33505d05c7Sgtb krb5_error_code KRB5_CALLCONV
krb5_copy_addr(krb5_context context,const krb5_address * inad,krb5_address ** outad)34505d05c7Sgtb krb5_copy_addr(krb5_context context, const krb5_address *inad, krb5_address **outad)
357c478bd9Sstevel@tonic-gate {
36505d05c7Sgtb krb5_address *tmpad;
377c478bd9Sstevel@tonic-gate
38505d05c7Sgtb if (!(tmpad = (krb5_address *)malloc(sizeof(*tmpad))))
397c478bd9Sstevel@tonic-gate return ENOMEM;
407c478bd9Sstevel@tonic-gate *tmpad = *inad;
417c478bd9Sstevel@tonic-gate if (!(tmpad->contents = (krb5_octet *)malloc(inad->length))) {
427c478bd9Sstevel@tonic-gate krb5_xfree(tmpad);
437c478bd9Sstevel@tonic-gate return ENOMEM;
447c478bd9Sstevel@tonic-gate }
457c478bd9Sstevel@tonic-gate memcpy((char *)tmpad->contents, (char *)inad->contents, inad->length);
467c478bd9Sstevel@tonic-gate *outad = tmpad;
477c478bd9Sstevel@tonic-gate return 0;
487c478bd9Sstevel@tonic-gate }
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate * Copy an address array, with fresh allocation.
527c478bd9Sstevel@tonic-gate */
53505d05c7Sgtb krb5_error_code KRB5_CALLCONV
krb5_copy_addresses(krb5_context context,krb5_address * const * inaddr,krb5_address *** outaddr)54505d05c7Sgtb krb5_copy_addresses(krb5_context context, krb5_address *const *inaddr, krb5_address ***outaddr)
557c478bd9Sstevel@tonic-gate {
567c478bd9Sstevel@tonic-gate krb5_error_code retval;
577c478bd9Sstevel@tonic-gate krb5_address ** tempaddr;
58505d05c7Sgtb register unsigned int nelems = 0;
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate if (!inaddr) {
617c478bd9Sstevel@tonic-gate *outaddr = 0;
627c478bd9Sstevel@tonic-gate return 0;
637c478bd9Sstevel@tonic-gate }
64*55fea89dSDan Cross
657c478bd9Sstevel@tonic-gate while (inaddr[nelems]) nelems++;
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate /* one more for a null terminated list */
687c478bd9Sstevel@tonic-gate if (!(tempaddr = (krb5_address **) calloc(nelems+1, sizeof(*tempaddr))))
697c478bd9Sstevel@tonic-gate return ENOMEM;
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate for (nelems = 0; inaddr[nelems]; nelems++) {
727c478bd9Sstevel@tonic-gate retval = krb5_copy_addr(context, inaddr[nelems], &tempaddr[nelems]);
737c478bd9Sstevel@tonic-gate if (retval) {
747c478bd9Sstevel@tonic-gate krb5_free_addresses(context, tempaddr);
757c478bd9Sstevel@tonic-gate return retval;
767c478bd9Sstevel@tonic-gate }
777c478bd9Sstevel@tonic-gate }
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate *outaddr = tempaddr;
807c478bd9Sstevel@tonic-gate return 0;
817c478bd9Sstevel@tonic-gate }
827c478bd9Sstevel@tonic-gate
837c478bd9Sstevel@tonic-gate #if 0
847c478bd9Sstevel@tonic-gate /*
857c478bd9Sstevel@tonic-gate * Append an address array, to another address array, with fresh allocation.
867c478bd9Sstevel@tonic-gate * Note that this function may change the value of *outaddr even if it
877c478bd9Sstevel@tonic-gate * returns failure, but it will not change the contents of the list.
887c478bd9Sstevel@tonic-gate */
897c478bd9Sstevel@tonic-gate krb5_error_code
907c478bd9Sstevel@tonic-gate krb5_append_addresses(context, inaddr, outaddr)
917c478bd9Sstevel@tonic-gate krb5_context context;
927c478bd9Sstevel@tonic-gate krb5_address * const * inaddr;
937c478bd9Sstevel@tonic-gate krb5_address ***outaddr;
947c478bd9Sstevel@tonic-gate {
957c478bd9Sstevel@tonic-gate krb5_error_code retval;
967c478bd9Sstevel@tonic-gate krb5_address ** tempaddr;
977c478bd9Sstevel@tonic-gate krb5_address ** tempaddr2;
98505d05c7Sgtb register unsigned int nelems = 0;
997c478bd9Sstevel@tonic-gate register int norigelems = 0;
1007c478bd9Sstevel@tonic-gate
1017c478bd9Sstevel@tonic-gate if (!inaddr)
1027c478bd9Sstevel@tonic-gate return 0;
1037c478bd9Sstevel@tonic-gate
1047c478bd9Sstevel@tonic-gate tempaddr2 = *outaddr;
1057c478bd9Sstevel@tonic-gate
1067c478bd9Sstevel@tonic-gate while (inaddr[nelems]) nelems++;
1077c478bd9Sstevel@tonic-gate while (tempaddr2[norigelems]) norigelems++;
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate tempaddr = (krb5_address **) realloc((char *)*outaddr,
1107c478bd9Sstevel@tonic-gate (nelems + norigelems + 1) * sizeof(*tempaddr));
1117c478bd9Sstevel@tonic-gate if (!tempaddr)
1127c478bd9Sstevel@tonic-gate return ENOMEM;
1137c478bd9Sstevel@tonic-gate
1147c478bd9Sstevel@tonic-gate /* The old storage has been freed. */
1157c478bd9Sstevel@tonic-gate *outaddr = tempaddr;
1167c478bd9Sstevel@tonic-gate
1177c478bd9Sstevel@tonic-gate
1187c478bd9Sstevel@tonic-gate for (nelems = 0; inaddr[nelems]; nelems++) {
1197c478bd9Sstevel@tonic-gate retval = krb5_copy_addr(context, inaddr[nelems],
1207c478bd9Sstevel@tonic-gate &tempaddr[norigelems + nelems]);
1217c478bd9Sstevel@tonic-gate if (retval)
1227c478bd9Sstevel@tonic-gate goto cleanup;
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate
1257c478bd9Sstevel@tonic-gate tempaddr[norigelems + nelems] = 0;
1267c478bd9Sstevel@tonic-gate return 0;
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate cleanup:
1297c478bd9Sstevel@tonic-gate while (--nelems >= 0)
1307c478bd9Sstevel@tonic-gate krb5_free_address(context, tempaddr[norigelems + nelems]);
1317c478bd9Sstevel@tonic-gate
1327c478bd9Sstevel@tonic-gate /* Try to allocate a smaller amount of memory for *outaddr. */
1337c478bd9Sstevel@tonic-gate tempaddr = (krb5_address **) realloc((char *)tempaddr,
1347c478bd9Sstevel@tonic-gate (norigelems + 1) * sizeof(*tempaddr));
1357c478bd9Sstevel@tonic-gate if (tempaddr)
1367c478bd9Sstevel@tonic-gate *outaddr = tempaddr;
1377c478bd9Sstevel@tonic-gate return retval;
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate #endif
1407c478bd9Sstevel@tonic-gate
141