Imported Upstream version 1.15.1
[platform/upstream/krb5.git] / src / lib / krb5 / krb / unparse.c
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/krb5/krb/unparse.c */
3 /*
4  * Copyright 1990, 2008 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 /*
28  * krb5_unparse_name() routine
29  *
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
32  * components.
33  */
34
35
36 #include "k5-int.h"
37 #include <stdio.h>
38
39 /*
40  * converts the multi-part principal format used in the protocols to a
41  * single-string representation of the name.
42  *
43  * The name returned is in allocated storage and should be freed by
44  * the caller when finished.
45  *
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)
50  *
51  * returns error
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
56  */
57
58 #define REALM_SEP       '@'
59 #define COMPONENT_SEP   '/'
60
61 static int
62 component_length_quoted(const krb5_data *src, int flags)
63 {
64     const char *cp = src->data;
65     int length = src->length;
66     int j;
67     int size = length;
68
69     if ((flags & KRB5_PRINCIPAL_UNPARSE_DISPLAY) == 0) {
70         int no_realm = (flags & KRB5_PRINCIPAL_UNPARSE_NO_REALM) &&
71             !(flags & KRB5_PRINCIPAL_UNPARSE_SHORT);
72
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')
78                 size++;
79     }
80
81     return size;
82 }
83
84 static int
85 copy_component_quoting(char *dest, const krb5_data *src, int flags)
86 {
87     int j;
88     const char *cp = src->data;
89     char *q = dest;
90     int length = src->length;
91
92     if (flags & KRB5_PRINCIPAL_UNPARSE_DISPLAY) {
93         if (src->length > 0)
94             memcpy(dest, src->data, src->length);
95         return src->length;
96     }
97
98     for (j=0; j < length; j++,cp++) {
99         int no_realm = (flags & KRB5_PRINCIPAL_UNPARSE_NO_REALM) &&
100             !(flags & KRB5_PRINCIPAL_UNPARSE_SHORT);
101
102         switch (*cp) {
103         case REALM_SEP:
104             if (no_realm) {
105                 *q++ = *cp;
106                 break;
107             }
108         case COMPONENT_SEP:
109         case '\\':
110             *q++ = '\\';
111             *q++ = *cp;
112             break;
113         case '\t':
114             *q++ = '\\';
115             *q++ = 't';
116             break;
117         case '\n':
118             *q++ = '\\';
119             *q++ = 'n';
120             break;
121         case '\b':
122             *q++ = '\\';
123             *q++ = 'b';
124             break;
125 #if 0
126             /* Heimdal escapes spaces in principal names upon unparsing */
127         case ' ':
128             *q++ = '\\';
129             *q++ = ' ';
130             break;
131 #endif
132         case '\0':
133             *q++ = '\\';
134             *q++ = '0';
135             break;
136         default:
137             *q++ = *cp;
138         }
139     }
140     return q - dest;
141 }
142
143 static krb5_error_code
144 k5_unparse_name(krb5_context context, krb5_const_principal principal,
145                 int flags, char **name, unsigned int *size)
146 {
147     char *q;
148     krb5_int32 i;
149     unsigned int totalsize = 0;
150     char *default_realm = NULL;
151     krb5_error_code ret = 0;
152
153     if (!principal || !name)
154         return KRB5_PARSE_MALFORMED;
155
156     if (flags & KRB5_PRINCIPAL_UNPARSE_SHORT) {
157         /* omit realm if local realm */
158         krb5_principal_data p;
159
160         ret = krb5_get_default_realm(context, &default_realm);
161         if (ret != 0)
162             goto cleanup;
163
164         p.realm = string2data(default_realm);
165
166         if (krb5_realm_compare(context, &p, principal))
167             flags |= KRB5_PRINCIPAL_UNPARSE_NO_REALM;
168     }
169
170     if ((flags & KRB5_PRINCIPAL_UNPARSE_NO_REALM) == 0) {
171         totalsize += component_length_quoted(&principal->realm, flags);
172         totalsize++;            /* This is for the separator */
173     }
174
175     for (i = 0; i < principal->length; i++) {
176         totalsize += component_length_quoted(&principal->data[i], flags);
177         totalsize++;    /* This is for the separator */
178     }
179     if (principal->length == 0)
180         totalsize++;
181
182     /*
183      * Allocate space for the ascii string; if space has been
184      * provided, use it, realloc'ing it if necessary.
185      *
186      * We need only n-1 seperators for n components, but we need
187      * an extra byte for the NUL at the end.
188      */
189     if (size) {
190         if (*name && (*size < totalsize)) {
191             *name = realloc(*name, totalsize);
192         } else {
193             *name = malloc(totalsize);
194         }
195         *size = totalsize;
196     } else {
197         *name = malloc(totalsize);
198     }
199
200     if (!*name) {
201         ret = ENOMEM;
202         goto cleanup;
203     }
204
205     q = *name;
206
207     for (i = 0; i < principal->length; i++) {
208         q += copy_component_quoting(q, &principal->data[i], flags);
209         *q++ = COMPONENT_SEP;
210     }
211
212     if (i > 0)
213         q--;                /* Back up last component separator */
214     if ((flags & KRB5_PRINCIPAL_UNPARSE_NO_REALM) == 0) {
215         *q++ = REALM_SEP;
216         q += copy_component_quoting(q, &principal->realm, flags);
217     }
218     *q++ = '\0';
219
220 cleanup:
221     if (default_realm != NULL)
222         krb5_free_default_realm(context, default_realm);
223
224     return ret;
225 }
226
227 krb5_error_code KRB5_CALLCONV
228 krb5_unparse_name(krb5_context context, krb5_const_principal principal, register char **name)
229 {
230     if (name != NULL)                      /* name == NULL will return error from _ext */
231         *name = NULL;
232
233     return k5_unparse_name(context, principal, 0, name, NULL);
234 }
235
236 krb5_error_code KRB5_CALLCONV
237 krb5_unparse_name_ext(krb5_context context, krb5_const_principal principal,
238                       char **name, unsigned int *size)
239 {
240     return k5_unparse_name(context, principal, 0, name, size);
241 }
242
243 krb5_error_code KRB5_CALLCONV
244 krb5_unparse_name_flags(krb5_context context, krb5_const_principal principal,
245                         int flags, char **name)
246 {
247     if (name != NULL)
248         *name = NULL;
249     return k5_unparse_name(context, principal, flags, name, NULL);
250 }
251
252 krb5_error_code KRB5_CALLCONV
253 krb5_unparse_name_flags_ext(krb5_context context, krb5_const_principal principal,
254                             int flags, char **name, unsigned int *size)
255 {
256     return k5_unparse_name(context, principal, flags, name, size);
257 }