xref: /titanic_41/usr/src/lib/gss_mechs/mech_krb5/krb5/krb/copy_addrs.c (revision 159d09a20817016f09b3ea28d1bdada4a336bb91)
1 /*
2  * lib/krb5/krb/copy_addrs.c
3  *
4  * Copyright 1990 by the Massachusetts Institute of Technology.
5  * All Rights Reserved.
6  *
7  * Export of this software from the United States of America may
8  *   require a specific license from the United States Government.
9  *   It is the responsibility of any person or organization contemplating
10  *   export to obtain such a license before exporting.
11  *
12  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13  * distribute this software and its documentation for any purpose and
14  * without fee is hereby granted, provided that the above copyright
15  * notice appear in all copies and that both that copyright notice and
16  * this permission notice appear in supporting documentation, and that
17  * the name of M.I.T. not be used in advertising or publicity pertaining
18  * to distribution of the software without specific, written prior
19  * permission.  Furthermore if you modify this software you must label
20  * your software as modified software and not distribute it in such a
21  * fashion that it might be confused with the original M.I.T. software.
22  * M.I.T. makes no representations about the suitability of
23  * this software for any purpose.  It is provided "as is" without express
24  * or implied warranty.
25  *
26  *
27  * krb5_copy_addresses()
28  */
29 
30 #include "k5-int.h"
31 
32 /*ARGSUSED*/
33 krb5_error_code KRB5_CALLCONV
krb5_copy_addr(krb5_context context,const krb5_address * inad,krb5_address ** outad)34 krb5_copy_addr(krb5_context context, const krb5_address *inad, krb5_address **outad)
35 {
36     krb5_address *tmpad;
37 
38     if (!(tmpad = (krb5_address *)malloc(sizeof(*tmpad))))
39 	return ENOMEM;
40     *tmpad = *inad;
41     if (!(tmpad->contents = (krb5_octet *)malloc(inad->length))) {
42 	krb5_xfree(tmpad);
43 	return ENOMEM;
44     }
45     memcpy((char *)tmpad->contents, (char *)inad->contents, inad->length);
46     *outad = tmpad;
47     return 0;
48 }
49 
50 /*
51  * Copy an address array, with fresh allocation.
52  */
53 krb5_error_code KRB5_CALLCONV
krb5_copy_addresses(krb5_context context,krb5_address * const * inaddr,krb5_address *** outaddr)54 krb5_copy_addresses(krb5_context context, krb5_address *const *inaddr, krb5_address ***outaddr)
55 {
56     krb5_error_code retval;
57     krb5_address ** tempaddr;
58     register unsigned int nelems = 0;
59 
60     if (!inaddr) {
61 	    *outaddr = 0;
62 	    return 0;
63     }
64 
65     while (inaddr[nelems]) nelems++;
66 
67     /* one more for a null terminated list */
68     if (!(tempaddr = (krb5_address **) calloc(nelems+1, sizeof(*tempaddr))))
69 	return ENOMEM;
70 
71     for (nelems = 0; inaddr[nelems]; nelems++) {
72 	retval = krb5_copy_addr(context, inaddr[nelems], &tempaddr[nelems]);
73         if (retval) {
74 	    krb5_free_addresses(context, tempaddr);
75 	    return retval;
76 	}
77     }
78 
79     *outaddr = tempaddr;
80     return 0;
81 }
82 
83 #if 0
84 /*
85  * Append an address array, to another address array, with fresh allocation.
86  * Note that this function may change the value of *outaddr even if it
87  * returns failure, but it will not change the contents of the list.
88  */
89 krb5_error_code
90 krb5_append_addresses(context, inaddr, outaddr)
91     krb5_context context;
92 	krb5_address * const * inaddr;
93 	krb5_address ***outaddr;
94 {
95     krb5_error_code retval;
96     krb5_address ** tempaddr;
97     krb5_address ** tempaddr2;
98     register unsigned int nelems = 0;
99     register int norigelems = 0;
100 
101     if (!inaddr)
102 	return 0;
103 
104     tempaddr2 = *outaddr;
105 
106     while (inaddr[nelems]) nelems++;
107     while (tempaddr2[norigelems]) norigelems++;
108 
109     tempaddr = (krb5_address **) realloc((char *)*outaddr,
110 		       (nelems + norigelems + 1) * sizeof(*tempaddr));
111     if (!tempaddr)
112 	return ENOMEM;
113 
114     /* The old storage has been freed.  */
115     *outaddr = tempaddr;
116 
117 
118     for (nelems = 0; inaddr[nelems]; nelems++) {
119 	retval = krb5_copy_addr(context, inaddr[nelems],
120 				&tempaddr[norigelems + nelems]);
121 	if (retval)
122 	    goto cleanup;
123     }
124 
125     tempaddr[norigelems + nelems] = 0;
126     return 0;
127 
128   cleanup:
129     while (--nelems >= 0)
130 	krb5_free_address(context, tempaddr[norigelems + nelems]);
131 
132     /* Try to allocate a smaller amount of memory for *outaddr.  */
133     tempaddr = (krb5_address **) realloc((char *)tempaddr,
134 					 (norigelems + 1) * sizeof(*tempaddr));
135     if (tempaddr)
136 	*outaddr = tempaddr;
137     return retval;
138 }
139 #endif
140 
141