289a0bf3a4da23660a610cc266c87246235b05c6
[krb5.git] / src / lib / krb5 / krb / bld_princ.c
1 /*
2  * lib/krb5/krb/bld_princ.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.  M.I.T. makes no representations about the suitability of
20  * this software for any purpose.  It is provided "as is" without express
21  * or implied warranty.
22  * 
23  *
24  * Build a principal from a list of strings
25  */
26
27 /* Need <krb5/k5-config.h> for HAVE_STDARG_H */
28 #include "k5-int.h"
29
30 #ifdef HAVE_STDARG_H
31 #include <stdarg.h>
32 #else
33 #include <varargs.h>
34 #endif
35
36 krb5_error_code INTERFACE_C
37 krb5_build_principal_va(context, princ, rlen, realm, ap)
38     krb5_context context;
39     krb5_principal princ;
40     int rlen;
41     const char *realm;
42     va_list ap;
43 {
44     register int i, count = 0;
45     register char *next;
46     char *tmpdata;
47     krb5_data *data;
48
49     /* guess at an initial sufficent count of 2 pieces */
50     count = 2;
51
52     /* get space for array and realm, and insert realm */
53     data = (krb5_data *) malloc(sizeof(krb5_data) * count);
54     if (data == 0)
55         return ENOMEM;
56     krb5_princ_set_realm_length(context, princ, rlen);
57     tmpdata = malloc(rlen);
58     if (!tmpdata) {
59         free (data);
60         return ENOMEM;
61     }
62     krb5_princ_set_realm_data(context, princ, tmpdata);
63     memcpy(tmpdata, realm, rlen);
64
65     /* process rest of components */
66
67     for (i = 0, next = va_arg(ap, char *);
68          next;
69          next = va_arg(ap, char *), i++) {
70         if (i == count) {
71             /* not big enough.  realloc the array */
72             krb5_data *p_tmp;
73             p_tmp = (krb5_data *) realloc((char *)data,
74                                           sizeof(krb5_data)*(count*2));
75             if (!p_tmp) {
76             free_out:
77                     while (--i >= 0)
78                         krb5_xfree(data[i].data);
79                     krb5_xfree(data);
80                     krb5_xfree(tmpdata);
81                     return (ENOMEM);
82             }
83             count *= 2;
84             data = p_tmp;
85         }
86
87         data[i].length = strlen(next);
88         data[i].data = strdup(next);
89         if (!data[i].data)
90             goto free_out;
91     }
92     princ->data = data;
93     princ->length = i;
94     princ->type = KRB5_NT_UNKNOWN;
95     princ->magic = KV5M_PRINCIPAL;
96     return 0;
97 }
98
99 krb5_error_code INTERFACE_C
100 #ifdef HAVE_STDARG_H
101 krb5_build_principal(krb5_context context,  krb5_principal * princ, int rlen,
102     const char * realm, ...)
103 #else
104 krb5_build_principal(context, princ, rlen, realm, va_alist)
105     krb5_context context;
106     krb5_principal *princ;
107     int rlen;
108     const char *realm;
109     va_dcl
110 #endif
111 {
112     va_list ap;
113     krb5_error_code retval;
114     krb5_principal pr_ret = (krb5_principal)malloc(sizeof(krb5_principal_data));
115
116     if (!pr_ret)
117         return ENOMEM;
118
119 #ifdef HAVE_STDARG_H
120     va_start(ap, realm);
121 #else
122     va_start(ap);
123 #endif
124     retval = krb5_build_principal_va(context, pr_ret, rlen, realm, ap);
125     va_end(ap);
126     if (retval == 0)
127         *princ = pr_ret;
128     return retval;
129 }