ldap_escape.c

Go to the documentation of this file.
00001 /*
00002  * $Id: ldap_escape.c 4585 2008-08-06 08:20:30Z klaus_darilion $
00003  *
00004  * Kamailio LDAP Module
00005  *
00006  * Copyright (C) 2007 University of North Carolina
00007  *
00008  * Original author: Christian Schlatter, cs@unc.edu
00009  *
00010  *
00011  * This file is part of Kamailio, a free SIP server.
00012  *
00013  * Kamailio is free software; you can redistribute it and/or modify
00014  * it under the terms of the GNU General Public License as published by
00015  * the Free Software Foundation; either version 2 of the License, or
00016  * (at your option) any later version
00017  *
00018  * Kamailio is distributed in the hope that it will be useful,
00019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00021  * GNU General Public License for more details.
00022  *
00023  * You should have received a copy of the GNU General Public License
00024  * along with this program; if not, write to the Free Software
00025  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00026  *
00027  * History:
00028  * --------
00029  * 2007-02-18: Initial version
00030  */
00031 
00032 
00033 #include <stdio.h>
00034 #include <ctype.h>
00035 #include "ldap_escape.h"
00036 
00037 static const char hex[] = "0123456789ABCDEF";
00038 
00039 /*
00040 * escape string following RFC 4515 (LDAP filter syntax) escaping rules:
00041 *
00042 *   * --> \2a
00043 *   ( --> \28
00044 *   ) --> \29
00045 *   \ --> \5c
00046 *
00047 * and percent encode '?' according RFC 4516 (LDAP URL), Section 2.1
00048 * if url_encode equals TRUE
00049 *
00050 *   ? --> %3F
00051 *
00052 */
00053 int ldap_rfc4515_escape(str *sin, str *sout, int url_encode)
00054 {
00055    char *src, *dst;
00056    
00057    if (sin == NULL || sout == NULL || sin->s == NULL || sout->s == NULL 
00058          || sin->len <= 0 || sout->len < 3*sin->len+1)
00059    {
00060       return -1;
00061    }
00062 
00063    src = sin->s;
00064    dst = sout->s;
00065 
00066    while (src < (sin->s + sin->len))
00067    {
00068       switch (*src)
00069       {
00070       case '*':
00071          *dst++ = '\\';
00072          *dst++ = '2';
00073          *dst = 'a';
00074          break;
00075       case '(':
00076          *dst++ = '\\';
00077          *dst++ = '2';
00078          *dst = '8';
00079          break;
00080       case ')':
00081          *dst++ = '\\';
00082          *dst++ = '2';
00083          *dst = '9';
00084          break;
00085       case '\\':
00086          *dst++ = '\\';
00087          *dst++ = '5';
00088          *dst = 'c';
00089          break;
00090       case '?':
00091          if (url_encode)
00092          {
00093             *dst++ = '%';
00094             *dst++ = '3';
00095             *dst = 'F';
00096          } else
00097          {
00098             *dst = *src;
00099          }
00100          break;
00101       default:
00102          *dst = *src;
00103       }
00104 
00105       src++;
00106       dst++;
00107    }
00108 
00109    *dst = 0;
00110    sout->len = dst - sout->s;
00111    return 0;
00112 }

Generated on Wed May 23 18:00:28 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6