1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/krb5/krb/bld_princ.c - Build a principal from a list of strings */
4 * Copyright 1991 by the Massachusetts Institute of Technology.
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.
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.
30 /* Takes first component as argument for KIM API,
31 * which does not allow realms with zero components */
32 static krb5_error_code
33 krb5int_build_principal_va(krb5_context context,
40 krb5_error_code retval = 0;
42 krb5_data *data = NULL;
44 krb5_int32 size = 2; /* initial guess at needed space */
45 char *component = NULL;
47 data = malloc(size * sizeof(krb5_data));
48 if (!data) { retval = ENOMEM; }
52 if (!r) { retval = ENOMEM; }
55 if (!retval && first) {
56 data[0].length = strlen(first);
57 data[0].data = strdup(first);
58 if (!data[0].data) { retval = ENOMEM; }
61 /* ap is only valid if first is non-NULL */
62 while (!retval && (component = va_arg(ap, char *))) {
64 krb5_data *new_data = NULL;
67 new_data = realloc ((char *) data, sizeof(krb5_data) * size);
76 data[count].length = strlen(component);
77 data[count].data = strdup(component);
78 if (!data[count].data) { retval = ENOMEM; }
85 princ->type = KRB5_NT_UNKNOWN;
86 princ->magic = KV5M_PRINCIPAL;
87 krb5_princ_set_realm_data(context, princ, r);
88 krb5_princ_set_realm_length(context, princ, rlen);
90 princ->length = count;
91 r = NULL; /* take ownership */
92 data = NULL; /* take ownership */
96 while (--count >= 0) {
97 free(data[count].data);
106 krb5_error_code KRB5_CALLCONV
107 krb5_build_principal_va(krb5_context context,
108 krb5_principal princ,
113 char *first = va_arg(ap, char *);
115 return krb5int_build_principal_va(context, princ, rlen, realm, first, ap);
118 /* Takes first component as argument for KIM API,
119 * which does not allow realms with zero components */
120 krb5_error_code KRB5_CALLCONV
121 krb5int_build_principal_alloc_va(krb5_context context,
122 krb5_principal *princ,
128 krb5_error_code retval = 0;
130 krb5_principal p = malloc(sizeof(krb5_principal_data));
131 if (!p) { retval = ENOMEM; }
134 retval = krb5int_build_principal_va(context, p, rlen, realm, first, ap);
146 krb5_error_code KRB5_CALLCONV
147 krb5_build_principal_alloc_va(krb5_context context,
148 krb5_principal *princ,
153 krb5_error_code retval = 0;
157 p = malloc(sizeof(krb5_principal_data));
161 first = va_arg(ap, char *);
162 retval = krb5int_build_principal_va(context, p, rlen, realm, first, ap);
172 krb5_error_code KRB5_CALLCONV_C
173 krb5_build_principal(krb5_context context,
174 krb5_principal * princ,
176 const char * realm, ...)
178 krb5_error_code retval = 0;
182 retval = krb5_build_principal_alloc_va(context, princ, rlen, realm, ap);
188 /*Anonymous and well known principals*/
189 static const char anon_realm_str[] = KRB5_ANONYMOUS_REALMSTR;
190 static const krb5_data anon_realm_data = {
191 KV5M_DATA, sizeof(anon_realm_str) - 1, (char *) anon_realm_str
193 static const char wellknown_str[] = KRB5_WELLKNOWN_NAMESTR;
194 static const char anon_str[] = KRB5_ANONYMOUS_PRINCSTR;
195 static const krb5_data anon_princ_data[] = {
196 { KV5M_DATA, sizeof(wellknown_str) - 1, (char *) wellknown_str },
197 { KV5M_DATA, sizeof(anon_str) - 1, (char *) anon_str }
200 const krb5_principal_data anon_princ = {
202 { KV5M_DATA, sizeof(anon_realm_str) - 1, (char *) anon_realm_str },
203 (krb5_data *) anon_princ_data, 2, KRB5_NT_WELLKNOWN
206 const krb5_data * KRB5_CALLCONV
207 krb5_anonymous_realm()
209 return &anon_realm_data;
212 krb5_const_principal KRB5_CALLCONV
213 krb5_anonymous_principal()