1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/krb5/krb/unparse.c */
4 * Copyright 1990, 2008 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.
28 * krb5_unparse_name() routine
30 * Rewritten by Theodore Ts'o to properly unparse principal names
31 * which have the component or realm separator as part of one of their
40 * converts the multi-part principal format used in the protocols to a
41 * single-string representation of the name.
43 * The name returned is in allocated storage and should be freed by
44 * the caller when finished.
46 * Conventions: / is used to separate components; @ is used to
47 * separate the realm from the rest of the name. If '/', '@', or '\0'
48 * appear in any the component, they will be representing using
49 * backslash encoding. ("\/", "\@", or '\0', respectively)
52 * KRB_PARSE_MALFORMED principal is invalid (does not contain
53 * at least 2 components)
54 * also returns system errors
55 * ENOMEM unable to allocate memory for string
59 #define COMPONENT_SEP '/'
62 component_length_quoted(const krb5_data *src, int flags)
64 const char *cp = src->data;
65 int length = src->length;
69 if ((flags & KRB5_PRINCIPAL_UNPARSE_DISPLAY) == 0) {
70 int no_realm = (flags & KRB5_PRINCIPAL_UNPARSE_NO_REALM) &&
71 !(flags & KRB5_PRINCIPAL_UNPARSE_SHORT);
73 for (j = 0; j < length; j++,cp++)
74 if ((!no_realm && *cp == REALM_SEP) ||
75 *cp == COMPONENT_SEP ||
76 *cp == '\0' || *cp == '\\' || *cp == '\t' ||
77 *cp == '\n' || *cp == '\b')
85 copy_component_quoting(char *dest, const krb5_data *src, int flags)
88 const char *cp = src->data;
90 int length = src->length;
92 if (flags & KRB5_PRINCIPAL_UNPARSE_DISPLAY) {
94 memcpy(dest, src->data, src->length);
98 for (j=0; j < length; j++,cp++) {
99 int no_realm = (flags & KRB5_PRINCIPAL_UNPARSE_NO_REALM) &&
100 !(flags & KRB5_PRINCIPAL_UNPARSE_SHORT);
136 static krb5_error_code
137 k5_unparse_name(krb5_context context, krb5_const_principal principal,
138 int flags, char **name, unsigned int *size)
142 unsigned int totalsize = 0;
143 char *default_realm = NULL;
144 krb5_error_code ret = 0;
146 if (!principal || !name)
147 return KRB5_PARSE_MALFORMED;
149 if (flags & KRB5_PRINCIPAL_UNPARSE_SHORT) {
150 /* omit realm if local realm */
151 krb5_principal_data p;
153 ret = krb5_get_default_realm(context, &default_realm);
157 p.realm = string2data(default_realm);
159 if (krb5_realm_compare(context, &p, principal))
160 flags |= KRB5_PRINCIPAL_UNPARSE_NO_REALM;
163 if ((flags & KRB5_PRINCIPAL_UNPARSE_NO_REALM) == 0) {
164 totalsize += component_length_quoted(&principal->realm, flags);
165 totalsize++; /* This is for the separator */
168 for (i = 0; i < principal->length; i++) {
169 totalsize += component_length_quoted(&principal->data[i], flags);
170 totalsize++; /* This is for the separator */
172 if (principal->length == 0)
176 * Allocate space for the ascii string; if space has been
177 * provided, use it, realloc'ing it if necessary.
179 * We need only n-1 separators for n components, but we need
180 * an extra byte for the NUL at the end.
183 if (*name && (*size < totalsize)) {
184 *name = realloc(*name, totalsize);
186 *name = malloc(totalsize);
190 *name = malloc(totalsize);
200 for (i = 0; i < principal->length; i++) {
201 q += copy_component_quoting(q, &principal->data[i], flags);
202 *q++ = COMPONENT_SEP;
206 q--; /* Back up last component separator */
207 if ((flags & KRB5_PRINCIPAL_UNPARSE_NO_REALM) == 0) {
209 q += copy_component_quoting(q, &principal->realm, flags);
214 if (default_realm != NULL)
215 krb5_free_default_realm(context, default_realm);
220 krb5_error_code KRB5_CALLCONV
221 krb5_unparse_name(krb5_context context, krb5_const_principal principal,
224 if (name != NULL) /* name == NULL will return error from _ext */
227 return k5_unparse_name(context, principal, 0, name, NULL);
230 krb5_error_code KRB5_CALLCONV
231 krb5_unparse_name_ext(krb5_context context, krb5_const_principal principal,
232 char **name, unsigned int *size)
234 return k5_unparse_name(context, principal, 0, name, size);
237 krb5_error_code KRB5_CALLCONV
238 krb5_unparse_name_flags(krb5_context context, krb5_const_principal principal,
239 int flags, char **name)
243 return k5_unparse_name(context, principal, flags, name, NULL);
246 krb5_error_code KRB5_CALLCONV
247 krb5_unparse_name_flags_ext(krb5_context context, krb5_const_principal principal,
248 int flags, char **name, unsigned int *size)
250 return k5_unparse_name(context, principal, flags, name, size);