Imported Upstream version 1.20.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         case '\0':
126             *q++ = '\\';
127             *q++ = '0';
128             break;
129         default:
130             *q++ = *cp;
131         }
132     }
133     return q - dest;
134 }
135
136 static krb5_error_code
137 k5_unparse_name(krb5_context context, krb5_const_principal principal,
138                 int flags, char **name, unsigned int *size)
139 {
140     char *q;
141     krb5_int32 i;
142     unsigned int totalsize = 0;
143     char *default_realm = NULL;
144     krb5_error_code ret = 0;
145
146     if (!principal || !name)
147         return KRB5_PARSE_MALFORMED;
148
149     if (flags & KRB5_PRINCIPAL_UNPARSE_SHORT) {
150         /* omit realm if local realm */
151         krb5_principal_data p;
152
153         ret = krb5_get_default_realm(context, &default_realm);
154         if (ret != 0)
155             goto cleanup;
156
157         p.realm = string2data(default_realm);
158
159         if (krb5_realm_compare(context, &p, principal))
160             flags |= KRB5_PRINCIPAL_UNPARSE_NO_REALM;
161     }
162
163     if ((flags & KRB5_PRINCIPAL_UNPARSE_NO_REALM) == 0) {
164         totalsize += component_length_quoted(&principal->realm, flags);
165         totalsize++;            /* This is for the separator */
166     }
167
168     for (i = 0; i < principal->length; i++) {
169         totalsize += component_length_quoted(&principal->data[i], flags);
170         totalsize++;    /* This is for the separator */
171     }
172     if (principal->length == 0)
173         totalsize++;
174
175     /*
176      * Allocate space for the ascii string; if space has been
177      * provided, use it, realloc'ing it if necessary.
178      *
179      * We need only n-1 separators for n components, but we need
180      * an extra byte for the NUL at the end.
181      */
182     if (size) {
183         if (*name && (*size < totalsize)) {
184             *name = realloc(*name, totalsize);
185         } else {
186             *name = malloc(totalsize);
187         }
188         *size = totalsize;
189     } else {
190         *name = malloc(totalsize);
191     }
192
193     if (!*name) {
194         ret = ENOMEM;
195         goto cleanup;
196     }
197
198     q = *name;
199
200     for (i = 0; i < principal->length; i++) {
201         q += copy_component_quoting(q, &principal->data[i], flags);
202         *q++ = COMPONENT_SEP;
203     }
204
205     if (i > 0)
206         q--;                /* Back up last component separator */
207     if ((flags & KRB5_PRINCIPAL_UNPARSE_NO_REALM) == 0) {
208         *q++ = REALM_SEP;
209         q += copy_component_quoting(q, &principal->realm, flags);
210     }
211     *q++ = '\0';
212
213 cleanup:
214     if (default_realm != NULL)
215         krb5_free_default_realm(context, default_realm);
216
217     return ret;
218 }
219
220 krb5_error_code KRB5_CALLCONV
221 krb5_unparse_name(krb5_context context, krb5_const_principal principal,
222                   char **name)
223 {
224     if (name != NULL)                      /* name == NULL will return error from _ext */
225         *name = NULL;
226
227     return k5_unparse_name(context, principal, 0, name, NULL);
228 }
229
230 krb5_error_code KRB5_CALLCONV
231 krb5_unparse_name_ext(krb5_context context, krb5_const_principal principal,
232                       char **name, unsigned int *size)
233 {
234     return k5_unparse_name(context, principal, 0, name, size);
235 }
236
237 krb5_error_code KRB5_CALLCONV
238 krb5_unparse_name_flags(krb5_context context, krb5_const_principal principal,
239                         int flags, char **name)
240 {
241     if (name != NULL)
242         *name = NULL;
243     return k5_unparse_name(context, principal, flags, name, NULL);
244 }
245
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)
249 {
250     return k5_unparse_name(context, principal, flags, name, size);
251 }