1 /*
2  * lib/krb5/krb/bld_pr_ext.c
3  *
4  * Copyright 1991 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  * Build a principal from a list of lengths and strings
28  */
29 
30 #include "k5-int.h"
31 
32 #include <stdarg.h>
33 
34 /*ARGSUSED*/
35 krb5_error_code KRB5_CALLCONV_C
krb5_build_principal_ext(krb5_context context,krb5_principal * princ,unsigned int rlen,const char * realm,...)36 krb5_build_principal_ext(krb5_context context,  krb5_principal * princ,
37 			 unsigned int rlen, const char * realm, ...)
38 {
39     va_list ap;
40     register int i, count = 0;
41     register unsigned int size;
42     register char *next;
43     char *tmpdata;
44     krb5_data *princ_data;
45     krb5_principal princ_ret;
46 
47     va_start(ap, realm);
48     /* count up */
49     while (va_arg(ap, int) != 0) {
50 	(void)va_arg(ap, char *);		/* pass one up */
51 	count++;
52     }
53     va_end(ap);
54 
55     /* we do a 2-pass to avoid the need to guess on allocation needs
56        cf. bld_princ.c */
57     /* get space for array */
58     princ_data = (krb5_data *) malloc(sizeof(krb5_data) * count);
59     if (!princ_data)
60 	return ENOMEM;
61     princ_ret = (krb5_principal) malloc(sizeof(krb5_principal_data));
62     if (!princ_ret) {
63 	krb5_xfree(princ_data);
64 	return ENOMEM;
65     }
66     princ_ret->data = princ_data;
67     princ_ret->length = count;
68     tmpdata = malloc(rlen+1);
69     if (!tmpdata) {
70 	krb5_xfree(princ_data);
71 	krb5_xfree(princ_ret);
72 	return ENOMEM;
73     }
74     krb5_princ_set_realm_length(context, princ_ret, rlen);
75     krb5_princ_set_realm_data(context, princ_ret, tmpdata);
76     memcpy(tmpdata, realm, rlen);
77     tmpdata[rlen] = 0;
78 
79     /* process rest of components */
80     va_start(ap, realm);
81     for (i = 0; i < count; i++) {
82 	size = va_arg(ap, unsigned int);
83 	next = va_arg(ap, char *);
84 	princ_data[i].length = size;
85 	princ_data[i].data = malloc(size+1);
86 	if (!princ_data[i].data)
87 	    goto free_out;
88 	memcpy(princ_data[i].data, next, size);
89 	princ_data[i].data[size] = 0;
90     }
91     va_end(ap);
92     *princ = princ_ret;
93     krb5_princ_type(context, princ_ret) = KRB5_NT_UNKNOWN;
94     return 0;
95 
96 free_out:
97     /* Solaris Kerberos */
98     while (--i >= 0)
99 	krb5_xfree(princ_data[i].data);
100     krb5_xfree(princ_data);
101     krb5_xfree(princ_ret);
102     va_end(ap);
103     return ENOMEM;
104 }
105