r21606: Implement escaping function for ldap RDN values
[samba.git] / source / lib / ldap_escape.c
index 26230884341deb1952064b16c7d36dcc9cb7f98c..8907399be4a71ef8908ee80097e792f4dc380f76 100644 (file)
@@ -89,3 +89,47 @@ char *escape_ldap_string_alloc(const char *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;
+}