From: Frederic Marchal Date: Sun, 26 May 2013 18:36:57 +0000 (+0200) Subject: Apply patch 36 submitted by Dayel Zabin X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=fd75cd9098441410ec6b0954266029523f7b4724;p=thirdparty%2Fsarg.git Apply patch 36 submitted by Dayel Zabin The patch converts the string returned by a LDAP search into a user selected character set. The patch has been slightly modified to avoid memory leaks and report errors the sarg's way. --- diff --git a/getconf.c b/getconf.c index f034e7c..6722a2b 100644 --- a/getconf.c +++ b/getconf.c @@ -773,6 +773,8 @@ static void parmtest(char *buf) if (getparam_string("LDAPTargetAttr",buf,LDAPTargetAttr,sizeof(LDAPTargetAttr))>0) return; + if (getparam_string("LDAPNativeCharset",buf,LDAPNativeCharset,sizeof(LDAPNativeCharset))>0) return; + if (getparam_string("graph_font",buf,GraphFont,sizeof(GraphFont))>0) return; if (getparam_string("sorttable",buf,SortTableJs,sizeof(SortTableJs))>0) return; diff --git a/include/conf.h b/include/conf.h index 3de0631..61736cc 100755 --- a/include/conf.h +++ b/include/conf.h @@ -436,6 +436,8 @@ int LDAPProtocolVersion; char LDAPBaseSearch[255]; char LDAPFilterSearch[512]; char LDAPTargetAttr[64]; +//! Character set to convert the LDAP returned string to. +char LDAPNativeCharset[20]; char GraphFont[MAXLEN]; //! The full path to sorttable.js if the table in the reports must be dynamicaly sorted. char SortTableJs[256]; diff --git a/log.c b/log.c index 92e10a1..46e8cae 100644 --- a/log.c +++ b/log.c @@ -216,6 +216,7 @@ int main(int argc,char *argv[]) LDAPBaseSearch[0]='\0'; strcpy(LDAPFilterSearch, "(uid=%s)"); strcpy(LDAPTargetAttr, "cn"); + LDAPNativeCharset[0]='\0'; SortTableJs[0]='\0'; tmp[0]='\0'; diff --git a/sarg.conf b/sarg.conf index 881fda8..5a078d4 100644 --- a/sarg.conf +++ b/sarg.conf @@ -408,6 +408,13 @@ # default value is 'cn' #LDAPTargetAttr cn +# TAG: LDAPNativeCharset charset-iconv-style +# Character set to convert the LDAP string to. +# For the list of some available charsets use: "iconv -l". +# This option requires libiconv and sarg must have been built with --with-iconv. +# default is empty line (UTF-8) +#LDAPNativeCharset ISO-8859-1 + # TAG: long_url yes|no # If yes, the full url is showed in report. # If no, only the site will be showed diff --git a/usertab.c b/usertab.c index 583030d..780c200 100644 --- a/usertab.c +++ b/usertab.c @@ -33,6 +33,12 @@ #include #include #include + +#if defined(HAVE_ICONV_H) +#include +#define USE_ICONV 1 +#endif //HAVE_ICONV_H + #endif //HAVE_LDAP_H enum UserTabEnum @@ -53,6 +59,15 @@ static char *userfile=NULL; static LDAP *ldap_handle=NULL; #endif //HAVE_LDAP_H +#ifdef USE_ICONV +//! iconv conversion descriptor to convert the string returned by LDAP. +static iconv_t ldapiconv=(iconv_t)-1; +//! Buffer to store the converted string. +static char *ldapconvbuffer=NULL; +//! Size of the converted string buffer. +static int ldapconvbuffersize=0; +#endif + static void init_file_usertab(const char *UserTabFile) { FILE *fp_usr; @@ -174,11 +189,72 @@ static void init_ldap_usertab(void) { exit(EXIT_FAILURE); } +#ifdef USE_ICONV + // prepare for the string conversion + if (LDAPNativeCharset[0]!='\0') { + ldapiconv = iconv_open( LDAPNativeCharset, "UTF-8" ); + if (ldapiconv==(iconv_t)-1) { + debuga(_("iconv cannot convert from UTF-8 to %s: %s\n"),LDAPNativeCharset,strerror(errno)); + exit(EXIT_FAILURE); + } + } + ldapconvbuffer=NULL; + ldapconvbuffersize=0; +#endif + /* Initializing cache */ init_cache(); } +const char * charset_convert( const char * str_in, const char * charset_to ) +{ +#ifdef USE_ICONV + size_t return_value; + const char * str_in_orig; + char * str_out; + size_t str_in_len; + size_t str_out_len; + + str_in_len = strlen( str_in ); + str_out_len = ( 2 * str_in_len ); + if (ldapconvbuffer==NULL || ldapconvbuffersize