/src/samba/third_party/heimdal/lib/wind/stringprep.c
Line | Count | Source |
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 | | /** |
43 | | * Process a input UCS4 string according a string-prep profile. |
44 | | * |
45 | | * @param in input UCS4 string to process |
46 | | * @param in_len length of the input string |
47 | | * @param out output UCS4 string |
48 | | * @param out_len length of the output string. |
49 | | * @param flags stringprep profile. |
50 | | * |
51 | | * @return returns 0 on success, an wind error code otherwise |
52 | | * @ingroup wind |
53 | | */ |
54 | | |
55 | | int |
56 | | wind_stringprep(const uint32_t *in, size_t in_len, |
57 | | uint32_t *out, size_t *out_len, |
58 | | wind_profile_flags flags) |
59 | 0 | { |
60 | 0 | size_t tmp_len = in_len * 3; |
61 | 0 | uint32_t *tmp; |
62 | 0 | int ret; |
63 | 0 | size_t olen; |
64 | |
|
65 | 0 | if (in_len == 0) { |
66 | 0 | *out_len = 0; |
67 | 0 | return 0; |
68 | 0 | } |
69 | | |
70 | 0 | tmp = malloc(tmp_len * sizeof(uint32_t)); |
71 | 0 | if (tmp == NULL) |
72 | 0 | return ENOMEM; |
73 | | |
74 | 0 | ret = _wind_stringprep_map(in, in_len, tmp, &tmp_len, flags); |
75 | 0 | if (ret) { |
76 | 0 | free(tmp); |
77 | 0 | return ret; |
78 | 0 | } |
79 | | |
80 | 0 | olen = *out_len; |
81 | 0 | ret = _wind_stringprep_normalize(tmp, tmp_len, tmp, &olen); |
82 | 0 | if (ret) { |
83 | 0 | free(tmp); |
84 | 0 | return ret; |
85 | 0 | } |
86 | 0 | ret = _wind_stringprep_prohibited(tmp, olen, flags); |
87 | 0 | if (ret) { |
88 | 0 | free(tmp); |
89 | 0 | return ret; |
90 | 0 | } |
91 | 0 | ret = _wind_stringprep_testbidi(tmp, olen, flags); |
92 | 0 | if (ret) { |
93 | 0 | free(tmp); |
94 | 0 | return ret; |
95 | 0 | } |
96 | | |
97 | | /* Insignificant Character Handling for ldap-prep */ |
98 | 0 | if (flags & WIND_PROFILE_LDAP_CASE_EXACT_ATTRIBUTE) { |
99 | 0 | ret = _wind_ldap_case_exact_attribute(tmp, olen, out, out_len); |
100 | | #if 0 |
101 | | } else if (flags & WIND_PROFILE_LDAP_CASE_EXACT_ASSERTION) { |
102 | | } else if (flags & WIND_PROFILE_LDAP_NUMERIC) { |
103 | | } else if (flags & WIND_PROFILE_LDAP_TELEPHONE) { |
104 | | #endif |
105 | 0 | } else { |
106 | 0 | memcpy(out, tmp, sizeof(out[0]) * olen); |
107 | 0 | *out_len = olen; |
108 | 0 | } |
109 | 0 | free(tmp); |
110 | |
|
111 | 0 | return ret; |
112 | 0 | } |
113 | | |
114 | | static const struct { |
115 | | const char *name; |
116 | | wind_profile_flags flags; |
117 | | } profiles[] = { |
118 | | { "nameprep", WIND_PROFILE_NAME }, |
119 | | { "saslprep", WIND_PROFILE_SASL }, |
120 | | { "ldapprep", WIND_PROFILE_LDAP } |
121 | | }; |
122 | | |
123 | | /** |
124 | | * Try to find the profile given a name. |
125 | | * |
126 | | * @param name name of the profile. |
127 | | * @param flags the resulting profile. |
128 | | * |
129 | | * @return returns 0 on success, an wind error code otherwise |
130 | | * @ingroup wind |
131 | | */ |
132 | | |
133 | | int |
134 | | wind_profile(const char *name, wind_profile_flags *flags) |
135 | 0 | { |
136 | 0 | unsigned int i; |
137 | |
|
138 | 0 | for (i = 0; i < sizeof(profiles)/sizeof(profiles[0]); i++) { |
139 | 0 | if (strcasecmp(profiles[i].name, name) == 0) { |
140 | 0 | *flags = profiles[i].flags; |
141 | 0 | return 0; |
142 | 0 | } |
143 | 0 | } |
144 | 0 | return WIND_ERR_NO_PROFILE; |
145 | 0 | } |