r25068: Older samba3 DCs will return DCERPC_FAULT_OP_RNG_ERROR for every opcode on the
[Samba.git] / source / lib / ldb / common / ldb_utf8.c
blob86ed40535a9c1756ded80735064439c9f21e72f0
1 /*
2 ldb database library
4 Copyright (C) Andrew Tridgell 2004
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
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.
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.
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
26 * Name: ldb
28 * Component: ldb utf8 handling
30 * Description: case folding and case comparison for UTF8 strings
32 * Author: Andrew Tridgell
35 #include "includes.h"
36 #include "ldb/include/includes.h"
37 #include "system/locale.h"
41 this allow the user to pass in a caseless comparison
42 function to handle utf8 caseless comparisons
44 void ldb_set_utf8_fns(struct ldb_context *ldb,
45 void *context,
46 char *(*casefold)(void *, void *, const char *))
48 if (context)
49 ldb->utf8_fns.context = context;
50 if (casefold)
51 ldb->utf8_fns.casefold = casefold;
55 a simple case folding function
56 NOTE: does not handle UTF8
58 char *ldb_casefold_default(void *context, void *mem_ctx, const char *s)
60 int i;
61 char *ret = talloc_strdup(mem_ctx, s);
62 if (!s) {
63 errno = ENOMEM;
64 return NULL;
66 for (i=0;ret[i];i++) {
67 ret[i] = toupper((unsigned char)ret[i]);
69 return ret;
72 void ldb_set_utf8_default(struct ldb_context *ldb)
74 ldb_set_utf8_fns(ldb, NULL, ldb_casefold_default);
77 char *ldb_casefold(struct ldb_context *ldb, void *mem_ctx, const char *s)
79 return ldb->utf8_fns.casefold(ldb->utf8_fns.context, mem_ctx, s);
83 check the attribute name is valid according to rfc2251
84 returns 1 if the name is ok
87 int ldb_valid_attr_name(const char *s)
89 int i;
91 if (!s || !s[0])
92 return 0;
94 /* handle special ldb_tdb wildcard */
95 if (strcmp(s, "*") == 0) return 1;
97 for (i = 0; s[i]; i++) {
98 if (! isascii(s[i])) {
99 return 0;
101 if (i == 0) { /* first char must be an alpha (or our special '@' identifier) */
102 if (! (isalpha(s[i]) || (s[i] == '@'))) {
103 return 0;
105 } else {
106 if (! (isalnum(s[i]) || (s[i] == '-'))) {
107 return 0;
111 return 1;
115 compare two attribute names
116 attribute names are restricted by rfc2251 so using
117 strcasecmp and toupper here is ok.
118 return 0 for match
120 int ldb_attr_cmp(const char *attr1, const char *attr2)
122 return strcasecmp(attr1, attr2);
125 char *ldb_attr_casefold(void *mem_ctx, const char *s)
127 int i;
128 char *ret = talloc_strdup(mem_ctx, s);
129 if (!ret) {
130 errno = ENOMEM;
131 return NULL;
133 for (i = 0; ret[i]; i++) {
134 ret[i] = toupper((unsigned char)ret[i]);
136 return ret;
140 we accept either 'dn' or 'distinguishedName' for a distinguishedName
142 int ldb_attr_dn(const char *attr)
144 if (ldb_attr_cmp(attr, "dn") == 0 ||
145 ldb_attr_cmp(attr, "distinguishedName") == 0) {
146 return 0;
148 return -1;