heimdal: import heimdal's trunk svn rev 23697 + lorikeet-heimdal patches
[metze/samba/wip.git] / source / heimdal / lib / wind / stringprep.c
1 /*
2  * Copyright (c) 2004, 2006, 2008 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden). 
4  * All rights reserved. 
5  *
6  * Redistribution and use in source and binary forms, with or without 
7  * modification, are permitted provided that the following conditions 
8  * are met: 
9  *
10  * 1. Redistributions of source code must retain the above copyright 
11  *    notice, this list of conditions and the following disclaimer. 
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright 
14  *    notice, this list of conditions and the following disclaimer in the 
15  *    documentation and/or other materials provided with the distribution. 
16  *
17  * 3. Neither the name of the Institute nor the names of its contributors 
18  *    may be used to endorse or promote products derived from this software 
19  *    without specific prior written permission. 
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
31  * SUCH DAMAGE. 
32  */
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37 #include "windlocl.h"
38 #include <stdlib.h>
39 #include <string.h>
40 #include <errno.h>
41
42 RCSID("$Id$");
43
44 /**
45  * Process a input UCS4 string according a string-prep profile.
46  *
47  * @param in input UCS4 string to process
48  * @param in_len length of the input string
49  * @param out output UCS4 string
50  * @param out_len length of the output string.
51  * @param flags stringprep profile.
52  *
53  * @return returns 0 on success, an wind error code otherwise
54  * @ingroup wind
55  */
56
57 int
58 wind_stringprep(const uint32_t *in, size_t in_len,
59                 uint32_t *out, size_t *out_len,
60                 wind_profile_flags flags)
61 {
62     size_t tmp_len = in_len * 3;
63     uint32_t *tmp = malloc(tmp_len * sizeof(uint32_t));
64     int ret;
65     size_t olen;
66
67     if (tmp == NULL)
68         return ENOMEM;
69
70     ret = _wind_stringprep_map(in, in_len, tmp, &tmp_len, flags);
71     if (ret) {
72         free(tmp);
73         return ret;
74     }
75
76     olen = *out_len;
77     ret = _wind_stringprep_normalize(tmp, tmp_len, tmp, &olen);
78     if (ret) {
79         free(tmp);
80         return ret;
81     }
82     ret = _wind_stringprep_prohibited(tmp, olen, flags);
83     if (ret) {
84         free(tmp);
85         return ret;
86     }
87     ret = _wind_stringprep_testbidi(tmp, olen, flags);
88     if (ret) {
89         free(tmp);
90         return ret;
91     }
92
93     /* Insignificant Character Handling for ldap-prep */
94     if (flags & WIND_PROFILE_LDAP_CASE_EXACT_ATTRIBUTE) {
95         ret = _wind_ldap_case_exact_attribute(tmp, olen, out, out_len);
96 #if 0
97     } else if (flags & WIND_PROFILE_LDAP_CASE_EXACT_ASSERTION) {
98     } else if (flags & WIND_PROFILE_LDAP_NUMERIC) {
99     } else if (flags & WIND_PROFILE_LDAP_TELEPHONE) {
100 #endif
101     } else {
102         memcpy(out, tmp, sizeof(out[0]) * olen);
103         *out_len = olen;
104     }
105     free(tmp);
106
107     return ret;
108 }
109
110 static struct {
111     const char *name;
112     wind_profile_flags flags;
113 } profiles[] = {
114     { "nameprep", WIND_PROFILE_NAME },
115     { "saslprep", WIND_PROFILE_SASL },
116     { "ldapprep", WIND_PROFILE_LDAP }
117 };
118
119 /**
120  * Try to find the profile given a name.
121  *
122  * @param name name of the profile.
123  * @param flags the resulting profile.
124  *
125  * @return returns 0 on success, an wind error code otherwise
126  * @ingroup wind
127  */
128
129 int
130 wind_profile(const char *name, wind_profile_flags *flags)
131 {
132     unsigned int i;
133
134     for (i = 0; i < sizeof(profiles)/sizeof(profiles[0]); i++) {
135         if (strcasecmp(profiles[i].name, name) == 0) {
136             *flags = profiles[i].flags;
137             return 0;
138         }
139     }
140     return WIND_ERR_NO_PROFILE;
141 }