Imported Upstream version 1.10.2
[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         memcpy(dest, src->data, src->length);
94         return src->length;
95     }
96
97     for (j=0; j < length; j++,cp++) {
98         int no_realm = (flags & KRB5_PRINCIPAL_UNPARSE_NO_REALM) &&
99             !(flags & KRB5_PRINCIPAL_UNPARSE_SHORT);
100
101         switch (*cp) {
102         case REALM_SEP:
103             if (no_realm) {
104                 *q++ = *cp;
105                 break;
106             }
107         case COMPONENT_SEP:
108         case '\\':
109             *q++ = '\\';
110             *q++ = *cp;
111             break;
112         case '\t':
113             *q++ = '\\';
114             *q++ = 't';
115             break;
116         case '\n':
117             *q++ = '\\';
118             *q++ = 'n';
119             break;
120         case '\b':
121             *q++ = '\\';
122             *q++ = 'b';
123             break;
124 #if 0
125             /* Heimdal escapes spaces in principal names upon unparsing */
126         case ' ':
127             *q++ = '\\';
128             *q++ = ' ';
129             break;
130 #endif
131         case '\0':
132             *q++ = '\\';
133             *q++ = '0';
134             break;
135         default:
136             *q++ = *cp;
137         }
138     }
139     return q - dest;
140 }
141
142 static krb5_error_code
143 k5_unparse_name(krb5_context context, krb5_const_principal principal,
144                 int flags, char **name, unsigned int *size)
145 {
146     char *q;
147     int i;
148     krb5_int32 nelem;
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         krb5_princ_realm(context, &p)->length = strlen(default_realm);
165         krb5_princ_realm(context, &p)->data = default_realm;
166
167         if (krb5_realm_compare(context, &p, principal))
168             flags |= KRB5_PRINCIPAL_UNPARSE_NO_REALM;
169     }
170
171     if ((flags & KRB5_PRINCIPAL_UNPARSE_NO_REALM) == 0) {
172         totalsize += component_length_quoted(krb5_princ_realm(context,
173                                                               principal),
174                                              flags);
175         totalsize++;            /* This is for the separator */
176     }
177
178     nelem = krb5_princ_size(context, principal);
179     for (i = 0; i < (int) nelem; i++) {
180         totalsize += component_length_quoted(krb5_princ_component(context, principal, i), flags);
181         totalsize++;    /* This is for the separator */
182     }
183     if (nelem == 0)
184         totalsize++;
185
186     /*
187      * Allocate space for the ascii string; if space has been
188      * provided, use it, realloc'ing it if necessary.
189      *
190      * We need only n-1 seperators for n components, but we need
191      * an extra byte for the NUL at the end.
192      */
193     if (size) {
194         if (*name && (*size < totalsize)) {
195             *name = realloc(*name, totalsize);
196         } else {
197             *name = malloc(totalsize);
198         }
199         *size = totalsize;
200     } else {
201         *name = malloc(totalsize);
202     }
203
204     if (!*name) {
205         ret = ENOMEM;
206         goto cleanup;
207     }
208
209     q = *name;
210
211     for (i = 0; i < (int) nelem; i++) {
212         q += copy_component_quoting(q,
213                                     krb5_princ_component(context,
214                                                          principal,
215                                                          i),
216                                     flags);
217         *q++ = COMPONENT_SEP;
218     }
219
220     if (i > 0)
221         q--;                /* Back up last component separator */
222     if ((flags & KRB5_PRINCIPAL_UNPARSE_NO_REALM) == 0) {
223         *q++ = REALM_SEP;
224         q += copy_component_quoting(q, krb5_princ_realm(context, principal), flags);
225     }
226     *q++ = '\0';
227
228 cleanup:
229     if (default_realm != NULL)
230         krb5_free_default_realm(context, default_realm);
231
232     return ret;
233 }
234
235 krb5_error_code KRB5_CALLCONV
236 krb5_unparse_name(krb5_context context, krb5_const_principal principal, register char **name)
237 {
238     if (name != NULL)                      /* name == NULL will return error from _ext */
239         *name = NULL;
240
241     return k5_unparse_name(context, principal, 0, name, NULL);
242 }
243
244 krb5_error_code KRB5_CALLCONV
245 krb5_unparse_name_ext(krb5_context context, krb5_const_principal principal,
246                       char **name, unsigned int *size)
247 {
248     return k5_unparse_name(context, principal, 0, name, size);
249 }
250
251 krb5_error_code KRB5_CALLCONV
252 krb5_unparse_name_flags(krb5_context context, krb5_const_principal principal,
253                         int flags, char **name)
254 {
255     if (name != NULL)
256         *name = NULL;
257     return k5_unparse_name(context, principal, flags, name, NULL);
258 }
259
260 krb5_error_code KRB5_CALLCONV
261 krb5_unparse_name_flags_ext(krb5_context context, krb5_const_principal principal,
262                             int flags, char **name, unsigned int *size)
263 {
264     return k5_unparse_name(context, principal, flags, name, size);
265 }