winbind/idmap_ad: be verbose about the user that we fail to map
[Samba.git] / lib / ldb / common / ldb_utf8.c
blob55d8f905d35c9cb222935098650b3aa431cbb558
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 3 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, see <http://www.gnu.org/licenses/>.
25 * Name: ldb
27 * Component: ldb utf8 handling
29 * Description: case folding and case comparison for UTF8 strings
31 * Author: Andrew Tridgell
34 #include "ldb_private.h"
35 #include "system/locale.h"
39 this allow the user to pass in a caseless comparison
40 function to handle utf8 caseless comparisons
42 void ldb_set_utf8_fns(struct ldb_context *ldb,
43 void *context,
44 char *(*casefold)(void *, void *, const char *, size_t))
46 if (context)
47 ldb->utf8_fns.context = context;
48 if (casefold)
49 ldb->utf8_fns.casefold = casefold;
53 a simple case folding function
54 NOTE: does not handle UTF8
56 char *ldb_casefold_default(void *context, TALLOC_CTX *mem_ctx, const char *s, size_t n)
58 size_t i;
59 char *ret = talloc_strndup(mem_ctx, s, n);
60 if (!s) {
61 errno = ENOMEM;
62 return NULL;
64 for (i=0;ret[i];i++) {
65 ret[i] = toupper((unsigned char)ret[i]);
67 return ret;
70 void ldb_set_utf8_default(struct ldb_context *ldb)
72 ldb_set_utf8_fns(ldb, NULL, ldb_casefold_default);
75 char *ldb_casefold(struct ldb_context *ldb, TALLOC_CTX *mem_ctx, const char *s, size_t n)
77 return ldb->utf8_fns.casefold(ldb->utf8_fns.context, mem_ctx, s, n);
81 check the attribute name is valid according to rfc2251
82 returns 1 if the name is ok
85 int ldb_valid_attr_name(const char *s)
87 size_t i;
89 if (!s || !s[0])
90 return 0;
92 /* handle special ldb_tdb wildcard */
93 if (strcmp(s, "*") == 0) return 1;
95 for (i = 0; s[i]; i++) {
96 if (! isascii(s[i])) {
97 return 0;
99 if (i == 0) { /* first char must be an alpha (or our special '@' identifier) */
100 if (! (isalpha(s[i]) || (s[i] == '@'))) {
101 return 0;
103 } else {
104 if (! (isalnum(s[i]) || (s[i] == '-'))) {
105 return 0;
109 return 1;
112 char *ldb_attr_casefold(TALLOC_CTX *mem_ctx, const char *s)
114 size_t i;
115 char *ret = talloc_strdup(mem_ctx, s);
116 if (!ret) {
117 errno = ENOMEM;
118 return NULL;
120 for (i = 0; ret[i]; i++) {
121 ret[i] = toupper((unsigned char)ret[i]);
123 return ret;
127 we accept either 'dn' or 'distinguishedName' for a distinguishedName
129 int ldb_attr_dn(const char *attr)
131 if (ldb_attr_cmp(attr, "dn") == 0 ||
132 ldb_attr_cmp(attr, "distinguishedName") == 0) {
133 return 0;
135 return -1;