1 /*
2 ldb database library
3
4 Copyright (C) Andrew Tridgell 2004
5
6 ** NOTE! The following LGPL license applies to the ldb
7 ** library. This does NOT imply that all of Samba is released
8 ** under the LGPL
9
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 2 of the License, or (at your option) any later version.
14
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
19
20 You should have received a copy of the GNU Lesser General Public
21 License along with this library; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
25 /*
26 * Name: ldb
27 *
28 * Component: ldb utf8 handling
29 *
30 * Description: case folding and case comparison for UTF8 strings
31 *
32 * Author: Andrew Tridgell
33 */
34
35 #include "includes.h"
36 #include "ldb/include/includes.h"
37 #include "system/locale.h"
38
39
40 /*
41 this allow the user to pass in a caseless comparison
42 function to handle utf8 caseless comparisons
43 */
ldb_set_utf8_fns(struct ldb_context * ldb,void * context,char * (* casefold)(void *,void *,const char *))44 void ldb_set_utf8_fns(struct ldb_context *ldb,
45 void *context,
46 char *(*casefold)(void *, void *, const char *))
47 {
48 if (context)
49 ldb->utf8_fns.context = context;
50 if (casefold)
51 ldb->utf8_fns.casefold = casefold;
52 }
53
54 /*
55 a simple case folding function
56 NOTE: does not handle UTF8
57 */
ldb_casefold_default(void * context,void * mem_ctx,const char * s)58 char *ldb_casefold_default(void *context, void *mem_ctx, const char *s)
59 {
60 int i;
61 char *ret = talloc_strdup(mem_ctx, s);
62 if (!s) {
63 errno = ENOMEM;
64 return NULL;
65 }
66 for (i=0;ret[i];i++) {
67 ret[i] = toupper((unsigned char)ret[i]);
68 }
69 return ret;
70 }
71
ldb_set_utf8_default(struct ldb_context * ldb)72 void ldb_set_utf8_default(struct ldb_context *ldb)
73 {
74 ldb_set_utf8_fns(ldb, NULL, ldb_casefold_default);
75 }
76
ldb_casefold(struct ldb_context * ldb,void * mem_ctx,const char * s)77 char *ldb_casefold(struct ldb_context *ldb, void *mem_ctx, const char *s)
78 {
79 return ldb->utf8_fns.casefold(ldb->utf8_fns.context, mem_ctx, s);
80 }
81
82 /*
83 check the attribute name is valid according to rfc2251
84 returns 1 if the name is ok
85 */
86
ldb_valid_attr_name(const char * s)87 int ldb_valid_attr_name(const char *s)
88 {
89 int i;
90
91 if (!s || !s[0])
92 return 0;
93
94 /* handle special ldb_tdb wildcard */
95 if (strcmp(s, "*") == 0) return 1;
96
97 for (i = 0; s[i]; i++) {
98 if (! isascii(s[i])) {
99 return 0;
100 }
101 if (i == 0) { /* first char must be an alpha (or our special '@' identifier) */
102 if (! (isalpha(s[i]) || (s[i] == '@'))) {
103 return 0;
104 }
105 } else {
106 if (! (isalnum(s[i]) || (s[i] == '-'))) {
107 return 0;
108 }
109 }
110 }
111 return 1;
112 }
113
ldb_attr_casefold(void * mem_ctx,const char * s)114 char *ldb_attr_casefold(void *mem_ctx, const char *s)
115 {
116 int i;
117 char *ret = talloc_strdup(mem_ctx, s);
118 if (!ret) {
119 errno = ENOMEM;
120 return NULL;
121 }
122 for (i = 0; ret[i]; i++) {
123 ret[i] = toupper((unsigned char)ret[i]);
124 }
125 return ret;
126 }
127
128 /*
129 we accept either 'dn' or 'distinguishedName' for a distinguishedName
130 */
ldb_attr_dn(const char * attr)131 int ldb_attr_dn(const char *attr)
132 {
133 if (ldb_attr_cmp(attr, "dn") == 0 ||
134 ldb_attr_cmp(attr, "distinguishedName") == 0) {
135 return 0;
136 }
137 return -1;
138 }
139