root/source3/lib/ldap_escape.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. escape_ldap_string_alloc
  2. escape_rdn_val_string_alloc

   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 3 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, see <http://www.gnu.org/licenses/>.
  21 */
  22 
  23 #include "includes.h"
  24 
  25 /**
  26  * Escape a parameter to an LDAP filter string, so they cannot contain
  27  * embeded ( ) * or \ chars which may cause it not to parse correctly. 
  28  *
  29  * @param s The input string
  30  *
  31  * @return A string allocated with malloc(), containing the escaped string, 
  32  * and to be free()ed by the caller.
  33  **/
  34 
  35 char *escape_ldap_string_alloc(const char *s)
     /* [<][>][^][v][top][bottom][index][help] */
  36 {
  37         size_t len = strlen(s)+1;
  38         char *output = (char *)SMB_MALLOC(len);
  39         const char *sub;
  40         int i = 0;
  41         char *p = output;
  42 
  43         if (output == NULL) {
  44                 return NULL;
  45         }
  46         
  47         while (*s)
  48         {
  49                 switch (*s)
  50                 {
  51                 case '*':
  52                         sub = "\\2a";
  53                         break;
  54                 case '(':
  55                         sub = "\\28";
  56                         break;
  57                 case ')':
  58                         sub = "\\29";
  59                         break;
  60                 case '\\':
  61                         sub = "\\5c";
  62                         break;
  63                 default:
  64                         sub = NULL;
  65                         break;
  66                 }
  67                 
  68                 if (sub) {
  69                         len = len + 3;
  70                         output = (char *)SMB_REALLOC(output, len);
  71                         if (!output) { 
  72                                 return NULL;
  73                         }
  74                         
  75                         p = &output[i];
  76                         strncpy (p, sub, 3);
  77                         p += 3;
  78                         i += 3;
  79 
  80                 } else {
  81                         *p = *s;
  82                         p++;
  83                         i++;
  84                 }
  85                 s++;
  86         }
  87         
  88         *p = '\0';
  89         return output;
  90 }
  91 
  92 char *escape_rdn_val_string_alloc(const char *s)
     /* [<][>][^][v][top][bottom][index][help] */
  93 {
  94         char *output, *p;
  95 
  96         /* The maximum size of the escaped string can be twice the actual size */
  97         output = (char *)SMB_MALLOC(2*strlen(s) + 1);
  98 
  99         if (output == NULL) {
 100                 return NULL;
 101         }
 102 
 103         p = output;
 104         
 105         while (*s)
 106         {
 107                 switch (*s)
 108                 {
 109                 case ',':
 110                 case '=':
 111                 case '+':
 112                 case '<':
 113                 case '>':
 114                 case '#':
 115                 case ';':
 116                 case '\\':
 117                 case '\"':
 118                         *p++ = '\\';
 119                         *p++ = *s;
 120                         break;
 121                 default:
 122                         *p = *s;
 123                         p++;
 124                 }
 125                 
 126                 s++;
 127         }
 128         
 129         *p = '\0';
 130 
 131         /* resize the string to the actual final size */
 132         output = (char *)SMB_REALLOC(output, strlen(output) + 1);
 133         return output;
 134 }

/* [<][>][^][v][top][bottom][index][help] */