s3-prefork: add support for multiple listning file descriptors
[idra/samba.git] / source3 / lib / ldap_escape.c
index 9e88b4999cc1c0e12b37b6d8a7c8eb040aa8e809..a731cb986421d440913282f53be01d1ece1a5209 100644 (file)
@@ -8,7 +8,7 @@
   
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
@@ -17,8 +17,7 @@
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
  * and to be free()ed by the caller.
  **/
 
-char *escape_ldap_string_alloc(const char *s)
+char *escape_ldap_string(TALLOC_CTX *mem_ctx, const char *s)
 {
        size_t len = strlen(s)+1;
-       char *output = malloc(len);
-       char *output_tmp;
+       char *output = talloc_array(mem_ctx, char, len);
        const char *sub;
        int i = 0;
        char *p = output;
-       
+
+       if (output == NULL) {
+               return NULL;
+       }
+
        while (*s)
        {
                switch (*s)
@@ -62,16 +64,17 @@ char *escape_ldap_string_alloc(const char *s)
                        sub = NULL;
                        break;
                }
-               
+
                if (sub) {
+                       char *tmp;
                        len = len + 3;
-                       output_tmp = realloc(output, len);
-                       if (!output_tmp) { 
-                               SAFE_FREE(output);
+                       tmp = talloc_realloc(mem_ctx, output, char, len);
+                       if (tmp == NULL) {
+                               TALLOC_FREE(output);
                                return NULL;
                        }
-                       output = output_tmp;
-                       
+                       output = tmp;
+
                        p = &output[i];
                        strncpy (p, sub, 3);
                        p += 3;
@@ -84,7 +87,51 @@ char *escape_ldap_string_alloc(const char *s)
                }
                s++;
        }
-       
+
        *p = '\0';
        return output;
 }
+
+char *escape_rdn_val_string_alloc(const char *s)
+{
+       char *output, *p;
+
+       /* The maximum size of the escaped string can be twice the actual size */
+       output = (char *)SMB_MALLOC(2*strlen(s) + 1);
+
+       if (output == NULL) {
+               return NULL;
+       }
+
+       p = output;
+
+       while (*s)
+       {
+               switch (*s)
+               {
+               case ',':
+               case '=':
+               case '+':
+               case '<':
+               case '>':
+               case '#':
+               case ';':
+               case '\\':
+               case '\"':
+                       *p++ = '\\';
+                       *p++ = *s;
+                       break;
+               default:
+                       *p = *s;
+                       p++;
+               }
+
+               s++;
+       }
+
+       *p = '\0';
+
+       /* resize the string to the actual final size */
+       output = (char *)SMB_REALLOC(output, strlen(output) + 1);
+       return output;
+}