[GLUE] Rsync SAMBA_3_0 SVN r25598 in order to create the v3-0-test branch.
[Samba.git] / source / lib / ldap_escape.c
blob8907399be4a71ef8908ee80097e792f4dc380f76
1 /*
2 Unix SMB/CIFS implementation.
3 ldap filter argument escaping
5 Copyright (C) 1998, 1999, 2000 Luke Howard <lukeh@padl.com>,
6 Copyright (C) 2003 Andrew Bartlett <abartlet@samba.org>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "includes.h"
26 /**
27 * Escape a parameter to an LDAP filter string, so they cannot contain
28 * embeded ( ) * or \ chars which may cause it not to parse correctly.
30 * @param s The input string
32 * @return A string allocated with malloc(), containing the escaped string,
33 * and to be free()ed by the caller.
34 **/
36 char *escape_ldap_string_alloc(const char *s)
38 size_t len = strlen(s)+1;
39 char *output = (char *)SMB_MALLOC(len);
40 const char *sub;
41 int i = 0;
42 char *p = output;
44 if (output == NULL) {
45 return NULL;
48 while (*s)
50 switch (*s)
52 case '*':
53 sub = "\\2a";
54 break;
55 case '(':
56 sub = "\\28";
57 break;
58 case ')':
59 sub = "\\29";
60 break;
61 case '\\':
62 sub = "\\5c";
63 break;
64 default:
65 sub = NULL;
66 break;
69 if (sub) {
70 len = len + 3;
71 output = (char *)SMB_REALLOC(output, len);
72 if (!output) {
73 return NULL;
76 p = &output[i];
77 strncpy (p, sub, 3);
78 p += 3;
79 i += 3;
81 } else {
82 *p = *s;
83 p++;
84 i++;
86 s++;
89 *p = '\0';
90 return output;
93 char *escape_rdn_val_string_alloc(const char *s)
95 char *output, *p;
97 /* The maximum size of the escaped string can be twice the actual size */
98 output = (char *)SMB_MALLOC(2*strlen(s) + 1);
100 if (output == NULL) {
101 return NULL;
104 p = output;
106 while (*s)
108 switch (*s)
110 case ',':
111 case '=':
112 case '+':
113 case '<':
114 case '>':
115 case '#':
116 case ';':
117 case '\\':
118 case '\"':
119 *p++ = '\\';
120 *p++ = *s;
121 break;
122 default:
123 *p = *s;
124 p++;
127 s++;
130 *p = '\0';
132 /* resize the string to the actual final size */
133 output = (char *)SMB_REALLOC(output, strlen(output) + 1);
134 return output;