lib/ldap_escape.c

説明を見る。
00001 /* 
00002    Unix SMB/CIFS implementation.
00003    ldap filter argument escaping
00004 
00005    Copyright (C) 1998, 1999, 2000 Luke Howard <lukeh@padl.com>,
00006    Copyright (C) 2003 Andrew Bartlett <abartlet@samba.org>
00007 
00008   
00009    This program is free software; you can redistribute it and/or modify
00010    it under the terms of the GNU General Public License as published by
00011    the Free Software Foundation; either version 2 of the License, or
00012    (at your option) any later version.
00013    
00014    This program is distributed in the hope that it will be useful,
00015    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017    GNU General Public License for more details.
00018    
00019    You should have received a copy of the GNU General Public License
00020    along with this program; if not, write to the Free Software
00021    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00022 */
00023 
00024 #include "includes.h"
00025 
00026 /**
00027  * Escape a parameter to an LDAP filter string, so they cannot contain
00028  * embeded ( ) * or \ chars which may cause it not to parse correctly. 
00029  *
00030  * @param s The input string
00031  *
00032  * @return A string allocated with malloc(), containing the escaped string, 
00033  * and to be free()ed by the caller.
00034  **/
00035 
00036 char *escape_ldap_string_alloc(const char *s)
00037 {
00038         size_t len = strlen(s)+1;
00039         char *output = (char *)SMB_MALLOC(len);
00040         const char *sub;
00041         int i = 0;
00042         char *p = output;
00043 
00044         if (output == NULL) {
00045                 return NULL;
00046         }
00047         
00048         while (*s)
00049         {
00050                 switch (*s)
00051                 {
00052                 case '*':
00053                         sub = "\\2a";
00054                         break;
00055                 case '(':
00056                         sub = "\\28";
00057                         break;
00058                 case ')':
00059                         sub = "\\29";
00060                         break;
00061                 case '\\':
00062                         sub = "\\5c";
00063                         break;
00064                 default:
00065                         sub = NULL;
00066                         break;
00067                 }
00068                 
00069                 if (sub) {
00070                         len = len + 3;
00071                         output = (char *)SMB_REALLOC(output, len);
00072                         if (!output) { 
00073                                 return NULL;
00074                         }
00075                         
00076                         p = &output[i];
00077                         strncpy (p, sub, 3);
00078                         p += 3;
00079                         i += 3;
00080 
00081                 } else {
00082                         *p = *s;
00083                         p++;
00084                         i++;
00085                 }
00086                 s++;
00087         }
00088         
00089         *p = '\0';
00090         return output;
00091 }
00092 
00093 char *escape_rdn_val_string_alloc(const char *s)
00094 {
00095         char *output, *p;
00096 
00097         /* The maximum size of the escaped string can be twice the actual size */
00098         output = (char *)SMB_MALLOC(2*strlen(s) + 1);
00099 
00100         if (output == NULL) {
00101                 return NULL;
00102         }
00103 
00104         p = output;
00105         
00106         while (*s)
00107         {
00108                 switch (*s)
00109                 {
00110                 case ',':
00111                 case '=':
00112                 case '+':
00113                 case '<':
00114                 case '>':
00115                 case '#':
00116                 case ';':
00117                 case '\\':
00118                 case '\"':
00119                         *p++ = '\\';
00120                         *p++ = *s;
00121                         break;
00122                 default:
00123                         *p = *s;
00124                         p++;
00125                 }
00126                 
00127                 s++;
00128         }
00129         
00130         *p = '\0';
00131 
00132         /* resize the string to the actual final size */
00133         output = (char *)SMB_REALLOC(output, strlen(output) + 1);
00134         return output;
00135 }

Sambaに対してSat Aug 29 21:22:58 2009に生成されました。  doxygen 1.4.7