r13915: Fixed a very interesting class of realloc() bugs found by Coverity.
[nivanova/samba-autobuild/.git] / source3 / lib / ldap_escape.c
1 /* 
2    Unix SMB/CIFS implementation.
3    ldap filter argument escaping
4
5    Copyright (C) 1998, 1999, 2000 Luke Howard <lukeh@padl.com>,
6    Copyright (C) 2003 Andrew Bartlett <abartlet@samba.org>
7
8   
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.
13    
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.
18    
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.
22 */
23
24 #include "includes.h"
25
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. 
29  *
30  * @param s The input string
31  *
32  * @return A string allocated with malloc(), containing the escaped string, 
33  * and to be free()ed by the caller.
34  **/
35
36 char *escape_ldap_string_alloc(const char *s)
37 {
38         size_t len = strlen(s)+1;
39         char *output = SMB_MALLOC(len);
40         const char *sub;
41         int i = 0;
42         char *p = output;
43         
44         while (*s)
45         {
46                 switch (*s)
47                 {
48                 case '*':
49                         sub = "\\2a";
50                         break;
51                 case '(':
52                         sub = "\\28";
53                         break;
54                 case ')':
55                         sub = "\\29";
56                         break;
57                 case '\\':
58                         sub = "\\5c";
59                         break;
60                 default:
61                         sub = NULL;
62                         break;
63                 }
64                 
65                 if (sub) {
66                         len = len + 3;
67                         output = SMB_REALLOC(output, len);
68                         if (!output) { 
69                                 return NULL;
70                         }
71                         
72                         p = &output[i];
73                         strncpy (p, sub, 3);
74                         p += 3;
75                         i += 3;
76
77                 } else {
78                         *p = *s;
79                         p++;
80                         i++;
81                 }
82                 s++;
83         }
84         
85         *p = '\0';
86         return output;
87 }