summaryrefslogtreecommitdiff
path: root/source4/lib/ldb/common/ldb_utf8.c
blob: dc25d6cf13e6763fee0548d0f56b839f05688fe9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* 
   ldb database library

   Copyright (C) Andrew Tridgell  2004

     ** NOTE! The following LGPL license applies to the ldb
     ** library. This does NOT imply that all of Samba is released
     ** under the LGPL
   
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

/*
 *  Name: ldb
 *
 *  Component: ldb utf8 handling
 *
 *  Description: case folding and case comparison for UTF8 strings
 *
 *  Author: Andrew Tridgell
 */

#include "includes.h"
#include "ldb/include/ldb.h"
#include "ldb/include/ldb_private.h"
#include <ctype.h>

/*
  TODO:
  a simple case folding function - will be replaced by a UTF8 aware function later
*/
char *ldb_casefold(void *mem_ctx, const char *s)
{
	int i;
	char *ret = talloc_strdup(mem_ctx, s);
	if (!s) {
		errno = ENOMEM;
		return NULL;
	}
	for (i=0;ret[i];i++) {
		ret[i] = toupper(ret[i]);
	}
	return ret;
}

/*
  a caseless compare, optimised for 7 bit
  TODO: doesn't yet handle UTF8
*/
static int ldb_caseless_cmp(const char *s1, const char *s2)
{
	int i;
	for (i=0;s1[i] != 0;i++) {
		int c1 = toupper(s1[i]), c2 = toupper(s2[i]);
		if (c1 != c2) {
			return c1 - c2;
		}
	}
	return s2[i];
}

/*
  compare two basedn fields
  return 0 for match
*/
int ldb_dn_cmp(const char *dn1, const char *dn2)
{
	return ldb_caseless_cmp(dn1, dn2);
}

/*
  compare two attributes
  return 0 for match
*/
int ldb_attr_cmp(const char *dn1, const char *dn2)
{
	return ldb_caseless_cmp(dn1, dn2);
}


/*
  casefold a dn. We need to uppercase the attribute names, and the 
  attribute values of case insensitive attributes. We also need to remove
  extraneous spaces between elements
*/
char *ldb_dn_fold(struct ldb_module *module, const char *dn, int (*case_fold_attr_fn)(struct ldb_module * module, char * attr))
{
	const char *dn_orig = dn;
	struct ldb_context *ldb = module->ldb;
	TALLOC_CTX *tmp_ctx = talloc_new(ldb);
	char *ret;
	size_t len;

	ret = talloc_strdup(tmp_ctx, "");
	if (ret == NULL) goto failed;

	while ((len = strcspn(dn, ",")) > 0) {
		char *p = strchr(dn, '=');
		char *attr, *value;
		int case_fold_required;

		if (p == NULL || (p-dn) > len) goto failed;

		attr = talloc_strndup(tmp_ctx, dn, p-dn);
		if (attr == NULL) goto failed;

		/* trim spaces from the attribute name */
		while (' ' == *attr) attr++;
		while (' ' == attr[strlen(attr)-1]) {
			attr[strlen(attr)-1] = 0;
		}
		if (*attr == 0) goto failed;

		value = talloc_strndup(tmp_ctx, p+1, len-(p+1-dn));
		if (value == NULL) goto failed;

		/* trim spaces from the value */
		while (' ' == *value) value++;
		while (' ' == value[strlen(value)-1]) {
			value[strlen(value)-1] = 0;
		}
		if (*value == 0) goto failed;

		case_fold_required = (* case_fold_attr_fn)(module, attr);

		attr = ldb_casefold(ldb, attr);
		if (attr == NULL) goto failed;
		talloc_steal(tmp_ctx, attr);

		if (case_fold_required) {
			value = ldb_casefold(ldb, value);
			if (value == NULL) goto failed;
			talloc_steal(tmp_ctx, value);
		}		

		if (dn[len] == ',') {
			ret = talloc_asprintf_append(ret, "%s=%s,", attr, value);
		} else {
			ret = talloc_asprintf_append(ret, "%s=%s", attr, value);
		}
		if (ret == NULL) goto failed;

		dn += len;
		if (*dn == ',') dn++;
	}

	talloc_steal(ldb, ret);
	talloc_free(tmp_ctx);
	return ret;

failed:
	talloc_free(tmp_ctx);
	return ldb_casefold(ldb, dn_orig);
}