From: Frédéric Marchal Date: Sun, 24 Jan 2010 16:31:04 +0000 (+0000) Subject: Fixed a regression in the usertab file not accepting IPv6 addresses any more. X-Git-Tag: v2.3-pre2~112 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=965c4a6f192669504dea3f8c67d8993031588f37;p=thirdparty%2Fsarg.git Fixed a regression in the usertab file not accepting IPv6 addresses any more. Reintroduced in user_name the buffer size check that was present in get_usertab_name. Rewrote the code from the old usertab file to blend it cleanly with the ldap code. Added btree_cache.h that was missing from the repository. Added const declaration where appropriate in btree_cache. Fixed the inclusion of the ldap headers to compile without the local declarations in defs.h. --- diff --git a/CMakeLists.txt b/CMakeLists.txt index b43a1c7..578ee22 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,7 @@ PROJECT(sarg C) SET(sarg_VERSION 2) SET(sarg_REVISION 3) SET(sarg_BUILD "") -SET(sarg_BUILDDATE "Jan-22-2010") +SET(sarg_BUILDDATE "Jan-24-2010") INCLUDE(AddFileDependencies) INCLUDE(CheckIncludeFile) @@ -101,7 +101,6 @@ CHECK_INCLUDE_FILE(sys/wait.h HAVE_SYS_WAIT_H) CHECK_INCLUDE_FILE(stdarg.h HAVE_STDARG_H) CHECK_INCLUDE_FILE(inttypes.h HAVE_INTTYPES_H) CHECK_INCLUDE_FILE(limits.h HAVE_LIMITS_H) -CHECK_INCLUDE_FILE(ldap.h HAVE_LDAP_H) CHECK_INCLUDE_FILE(math.h HAVE_MATH_H) CHECK_INCLUDE_FILE(locale.h HAVE_LOCALE_H) CHECK_INCLUDE_FILE(execinfo.h HAVE_EXECINFO_H) @@ -162,6 +161,17 @@ size_t iconv(); ENDIF(HAVE_ICONV_H) ENDIF(ENABLE_ICONV) +# Find ldap +CHECK_INCLUDE_FILE(ldap.h HAVE_LDAP_H) +IF(HAVE_LDAP_H) + FIND_LIBRARY(LDAP_LIBRARY NAMES ldap DOC "The ldap library") + IF(LDAP_LIBRARY) + TARGET_LINK_LIBRARIES(sarg ${LDAP_LIBRARY}) + SET(HAVE_LDAP LDAP_LIBRARY CACHE PATH DOC "True if LDAP was found") + SET(HAVE_LDAP CACHE BOOL DOC "True if must use LDAP") + ENDIF(LDAP_LIBRARY) +ENDIF(HAVE_LDAP_H) + # Support for large files OPTION(ENABLE_LARGEFILE "Enable the usage of large files" ON) IF(ENABLE_LARGEFILE) diff --git a/authfail.c b/authfail.c index 0e1a934..401c0ef 100644 --- a/authfail.c +++ b/authfail.c @@ -149,8 +149,7 @@ void authfail_report(void) strcpy(oip,ip); } -// get_usertab_name(user,name,sizeof(name)); - user_find(name, user); + user_find(name,sizeof(name), user); if(dotinuser && strchr(name,'_')) { subs(name,sizeof(name),"_","."); diff --git a/btree_cache.c b/btree_cache.c index b3b9005..719c381 100644 --- a/btree_cache.c +++ b/btree_cache.c @@ -47,7 +47,7 @@ struct bt *get_disbalanced_node(struct bt *node); void balance_node(struct bt *node); -struct bt *insert_node(struct bt *root, char *item, char *value) +struct bt *insert_node(struct bt *root, const char *item, const char *value) { struct bt *new_item_bt = NULL; if (!root) @@ -113,7 +113,7 @@ void delete_tree(struct bt *root) } } -struct bt *search_item(struct bt *root, char *item) +struct bt *search_item(struct bt *root, const char *item) { int result; while (root && (result = strncmp(root->value, item, 64))) @@ -189,7 +189,7 @@ void rotate_right(struct bt *node) node = left; tmp->left = right; node->right = tmp; - + if (root_bt == tmp) root_bt = node; else @@ -260,7 +260,7 @@ void balance_node(struct bt *node) default: exit(1); break; - + } } @@ -285,20 +285,20 @@ struct bt *get_disbalanced_node(struct bt *node) return NULL; } -void init_cache() +void init_cache(void) { root_bt = NULL; sizeof_bt = sizeof(struct bt); } -int insert_to_cache(char *key, char *value) +int insert_to_cache(const char *key, const char *value) { struct bt *root = NULL; char strict_chars[] = " ~!@^&(){}|<>?:;\"\'\\[]`,\r\n\0", *strict_chars_ptr; - + strict_chars_ptr = strict_chars; while (*strict_chars_ptr) { @@ -319,7 +319,7 @@ int insert_to_cache(char *key, char *value) } -char *search_in_cache(char *key) +char *search_in_cache(const char *key) { struct bt *node; if ((node = search_item(root_bt, key))) @@ -330,7 +330,7 @@ char *search_in_cache(char *key) return NULL; } -void destroy_cache() +void destroy_cache(void) { delete_tree(root_bt); root_bt = NULL; diff --git a/dansguardian_report.c b/dansguardian_report.c index 43890bc..3b6a411 100644 --- a/dansguardian_report.c +++ b/dansguardian_report.c @@ -153,8 +153,7 @@ void dansguardian_report(void) strcpy(oip,ip); } -// get_usertab_name(user,name,sizeof(name)); - user_find(name, user); + user_find(name, sizeof(name), user); if(dotinuser && strchr(name,'_')) { subs(name,sizeof(name),"_","."); diff --git a/denied.c b/denied.c index 76c6e9d..ba9d432 100644 --- a/denied.c +++ b/denied.c @@ -135,8 +135,7 @@ void gen_denied_report(void) strcpy(oip,ip); } -// get_usertab_name(user,name,sizeof(name)); - user_find(name, user); + user_find(name, sizeof(name), user); if(dotinuser && strchr(name,'_')) { subs(name,sizeof(name),"_","."); diff --git a/documentation/usertab.txt b/documentation/usertab.txt new file mode 100644 index 0000000..a9c5653 --- /dev/null +++ b/documentation/usertab.txt @@ -0,0 +1,115 @@ +/*!\file +\brief Provide a meanigfull name instead of the user ID or IP address shown in the +reports. +*/ + + +/*! \enum UserTabEnum +The possible sources to map the user ID or IP address to the name to display +in the reports. +*/ + + + + + +/*! \var enum UserTabEnum which_usertab +Tell the database source to use to map the user ID or IP address to a meaningfull +name. +*/ + + + + + +/*! \fn void init_usertab(const char *UserTabFile) +Initialize the data used by user_find(). + +If \a UserTabFile is ldap, the user ID is fetched from a LDAP server. + +\param UserTabFile The name of the file to read or ldap. If it is empty, the function does nothing. + +\note The memory and resources allocated by this function must be released by +a call to close_usertab(). +*/ + + + + + +/*! \fn void user_find(char *mappedname, int namelen, const char *userlogin) +Find the real name of the user with the ID or IP address in \a userlogin. The name is fetched +from the source initialized by init_usertab(). + +The usertab data must have been initialized by init_usertab(). + +\param mappedname A buffer to write the real name of the user. +\param namelen The size of the buffer. +\param userlogin The ID or IP address of the user. +*/ + + + + + +/*! \fn void close_usertab(void) +Free the memory and resources allocated by init_usertab(). +*/ + + + + + +/*! \fn static void init_file_usertab(const char *UserTabFile) +Read the \a UserTabFile database. + +The file contains the IP address or ID of the user then some spaces and +the real name of the user to show in the report. + +Any trailing space or tabulation is removed from the real name. The user ID or IP cannot contain +a space or a tabulation but it may contain any other character, including the colon that was +forbidden in the past. That change was made to allow IPv6 addresses. + +The file may contain comments if the line starts with a #. + +\param UserTabFile The name of the file to read. +*/ + + + + + +/*! \fn static void get_usertab_name(const char *user,char *name,int namelen) +Get the real name of the user from the usertab file read by init_file_usertab(). + +\param user The user ID or IP address to search. +\param name The buffer to store the real name of the user. +\param namelen The size of the \a name buffer. + +If the user ID or IP address isn't found, the output buffer \a name contains +the unmatched input string. +*/ + + + + + +/*! \fn static void init_ldap_usertab(void) +Initialize the communication with the LDAP server whose name is in +::LDAPHost and connect to port ::LDAPPort. +*/ + + + + + +/*! \fn static void get_ldap_name(const char *userlogin,char *mappedname,int namelen) +Get the real name of a user by searching the userlogin (user ID) in a LDAP. + +\param userlogin The user ID to search. +\param name The buffer to store the real name of the user. +\param namelen The size of the \a name buffer. + +If the user ID isn't found in the LDAP, the output buffer \a name contains +the unmatched input string. +*/ diff --git a/documentation/util.txt b/documentation/util.txt index 15ce186..ce89c0a 100644 --- a/documentation/util.txt +++ b/documentation/util.txt @@ -778,38 +778,6 @@ the function returns NULL. -/*! \fn void read_usertab(const char *UserTabFile) -Read the content of the \a UserTabFile and store it for further use with get_usertab_name(). - -The file contains the IP address or ID of the user then some spaces and the real name of the user. - -Any trailing space or tabulation is removed from the real name. The user ID or IP cannot contain -a space or a tabulation but it may contain any other character, including the colon that was -forbidden in the past. That change was made to allow IPv6 addresses. - -The file may contain comments if the line starts with a #. - -\param UserTabFile The name of the file to read. If it is empty, the function does nothing. -*/ - - - - - -/*! \fn void get_usertab_name(const char *user,char *name,int namelen) -Find the real name of the user with the ID or IP address in \a user. The name is fetched -from the usertab file. - -The usertab file must have been read by read_usertab(). - -\param user The ID or IP address of the user. -\param name A buffer to write the real name of the user. -\param namelen The size of the buffer. -*/ - - - - /*! \fn void write_logo_image(FILE *fp_ou) Write a link of the logo of the organisation that generate the report in the HTML file. The logo is written in a centered table. diff --git a/download.c b/download.c index 6efba37..0cf96bd 100644 --- a/download.c +++ b/download.c @@ -142,8 +142,7 @@ void download_report(void) strcpy(oip,ip); } -// get_usertab_name(user,name,sizeof(name)); - user_find(name, user); + user_find(name, sizeof(name), user); if(dotinuser && strchr(name,'_')) { subs(name,sizeof(name),"_","."); diff --git a/getconf.c b/getconf.c index 6d43c51..32e38f4 100644 --- a/getconf.c +++ b/getconf.c @@ -436,9 +436,9 @@ static void parmtest(char *buf) if (getparam_string("LDAPHost",buf,LDAPHost,sizeof(LDAPHost))>0) return; - if (getparam_string("LDAPPort",buf,LDAPPort,sizeof(LDAPPort))>0) return; + if (getparam_int("LDAPPort",buf,&LDAPPort)>0) return; - if (getparam_string("LDAPProtocolVersion",buf,LDAPProtocolVersion,sizeof(LDAPProtocolVersion))>0) return; + if (getparam_int("LDAPProtocolVersion",buf,&LDAPProtocolVersion)>0) return; if (getparam_string("LDAPBindDN",buf,LDAPBindDN,sizeof(LDAPBindDN))>0) return; diff --git a/grepday.c b/grepday.c index e17b3ac..ba2ed71 100644 --- a/grepday.c +++ b/grepday.c @@ -443,8 +443,7 @@ void greport_day(const char *user) ip2name(wuser,sizeof(wuser)); } -// get_usertab_name(wuser,name,sizeof(name)); - user_find(name, wuser); + user_find(name, sizeof(name), wuser); while(fgets(buf,sizeof(buf),fp_in)!=NULL) { fixendofline(buf); diff --git a/html.c b/html.c index 0c0fb33..79f174d 100644 --- a/html.c +++ b/html.c @@ -173,8 +173,7 @@ void htmlrel(void) if(Ip2Name) ip2name(u2,sizeof(u2)); -// get_usertab_name(u2,name2,sizeof(name2)); - user_find(name2, u2); + user_find(name2, sizeof(name2), u2); if(dotinuser && strchr(name2,'_')) { subs(name2,sizeof(name2),"_","."); diff --git a/include/btree_cache.h b/include/btree_cache.h new file mode 100644 index 0000000..2892610 --- /dev/null +++ b/include/btree_cache.h @@ -0,0 +1,9 @@ +#ifndef BTREE_CACHE_H +#define BTREE_CACHE_H + +void init_cache(void); +void destroy_cache(void); +char *search_in_cache(const char *key); +int insert_to_cache(const char *key, const char *value); + +#endif //BTREE_CACHE_H diff --git a/include/conf.h b/include/conf.h index 30432f3..b58a70a 100755 --- a/include/conf.h +++ b/include/conf.h @@ -95,9 +95,6 @@ gdPoint points[4]; #ifdef HAVE_WINSOCK_H #include #endif -#ifdef HAVE_LDAP_H -#include -#endif #ifdef HAVE_MATH_H #include #endif @@ -149,7 +146,6 @@ char url[MAXLEN]; char urly[MAXLEN]; char user[MAXLEN]; char period[MAXLEN]; -char msg[1024]; char code[MAXLEN]; char code2[MAXLEN]; char tmp[MAXLEN]; @@ -285,7 +281,6 @@ char AuthUserFile[255]; char AuthName[512]; char AuthType[255]; char Require[512]; -char *userfile; char *str; char *str2; char text[200][255]; @@ -324,8 +319,8 @@ char RealtimeUnauthRec[15]; char LDAPHost[255]; char LDAPBindDN[512]; char LDAPBindPW[255]; -char LDAPPort[16]; -char LDAPProtocolVersion[3]; +int LDAPPort; +int LDAPProtocolVersion; char LDAPBaseSearch[255]; char LDAPFilterSearch[512]; char LDAPTargetAttr[64]; diff --git a/include/defs.h b/include/defs.h index e79a447..1df5f5e 100755 --- a/include/defs.h +++ b/include/defs.h @@ -126,6 +126,11 @@ void usage(const char *prog); // useragent.c void useragent(void); +// usertab.c +void init_usertab(const char *UserTabFile); +void user_find(char *mappedname, int namelen, const char *userlogin); +void close_usertab(void); + // util.c void getword_start(struct getwordstruct *gwarea, const char *line); void getword_restart(struct getwordstruct *gwarea); @@ -181,18 +186,5 @@ void removetmp(const char *outdir); void zdate(char *ftime,int ftimesize, const char *DateFormat); void baddata(void); char *get_param_value(const char *param,char *line); -void read_usertab(const char *UserTabFile); -void get_usertab_name(const char *user,char *name,int namelen); int compar( const void *, const void * ); void unlinkdir(const char *dir,int contentonly); - -int ldap_init(); -int ldap_search(); -int ldap_search_s(); -int ldap_unbind(); -int ldap_simple_bind(); -int ldap_simple_bind_s(); -char *ldap_get_values(); -void init_ldap_usertab(); -void user_find(char *mappedname, char *userlogin); -void close_usertab(); diff --git a/include/info.h b/include/info.h index 45a38c8..fe4a1f7 100755 --- a/include/info.h +++ b/include/info.h @@ -1,3 +1,3 @@ -#define VERSION PACKAGE_VERSION" Jan-22-2010" +#define VERSION PACKAGE_VERSION" Jan-24-2010" #define PGM PACKAGE_NAME #define URL "http://sarg.sourceforge.net" diff --git a/log.c b/log.c index f64802d..299bc9e 100644 --- a/log.c +++ b/log.c @@ -72,7 +72,6 @@ int main(int argc,char *argv[]) char data[255]; char elap[255]; char ip[MAXLEN]; - char msg[MAXLEN]; char tam[255]; char fun[MAXLEN]; char wuser[MAXLEN]; @@ -224,8 +223,8 @@ int main(int argc,char *argv[]) BytesInSitesUsersReport=0; UserAuthentication=0; strcpy(LDAPHost,"127.0.0.1"); - strcpy(LDAPPort,"389"); - strcpy(LDAPProtocolVersion,"3"); + LDAPPort=389; + LDAPProtocolVersion=3; LDAPBindDN[0]='\0'; LDAPBindPW[0]='\0'; LDAPBaseSearch[0]='\0'; @@ -683,7 +682,7 @@ int main(int argc,char *argv[]) } #endif - read_usertab(UserTabFile); + init_usertab(UserTabFile); sprintf ( sz_Download_Unsort , "%s/sarg/download.unsort", tmp); @@ -1478,7 +1477,6 @@ int main(int argc,char *argv[]) fprintf(stderr, "SARG: %s\n",text[16]); fprintf(stderr, "SARG: %s\n",text[21]); } else fprintf(stderr, "SARG: %s\n",text[15]); - bzero(msg,sizeof(msg)); if(fp_denied) fclose(fp_denied); if(fp_authfail) diff --git a/realtime.c b/realtime.c index 4e10c81..fe33c30 100755 --- a/realtime.c +++ b/realtime.c @@ -54,7 +54,7 @@ static void getlog(void) int fd1,fd2; int cstatus; - read_usertab(UserTabFile); + init_usertab(UserTabFile); fd1 = mkstemp(template1); fd2 = mkstemp(template2); @@ -218,8 +218,7 @@ static void datashow(const char *tmp) strcpy(u2,user); if(Ip2Name) ip2name(u2,sizeof(u2)); -// get_usertab_name(u2,name,sizeof(name)); - user_find(name, u2); + user_find(name, sizeof(name), u2); if(dotinuser && strchr(name,'_')) { subs(name,sizeof(name),"_","."); diff --git a/repday.c b/repday.c index ce2ca9e..0a0f250 100644 --- a/repday.c +++ b/repday.c @@ -98,8 +98,7 @@ void report_day(const char *user) ip2name(wuser,sizeof(wuser)); } -// get_usertab_name(wuser,name,sizeof(name)); - user_find(name, wuser); + user_find(name, sizeof(name), wuser); if(dotinuser && strchr(name,'_')) { subs(name,sizeof(name),"_","."); diff --git a/report.c b/report.c index 2568f66..6c095a0 100644 --- a/report.c +++ b/report.c @@ -119,8 +119,7 @@ void gerarel(void) strcpy(u2,user); if(Ip2Name) ip2name(u2,sizeof(u2)); -// get_usertab_name(u2,name,sizeof(name)); - user_find(name, u2); + user_find(name,sizeof(name), u2); if(dotinuser && strchr(name,'_')) { subs(name,sizeof(name),"_","."); diff --git a/siteuser.c b/siteuser.c index 870f447..6484664 100644 --- a/siteuser.c +++ b/siteuser.c @@ -138,8 +138,7 @@ void siteuser(void) if(userip) fixip(user); -// get_usertab_name(user,name,sizeof(name)); - user_find(name, user); + user_find(name, sizeof(name), user); if(dotinuser && strchr(name,'_')) { subs(name,sizeof(name),"_","."); diff --git a/squidguard_report.c b/squidguard_report.c index 08743cf..a062aec 100644 --- a/squidguard_report.c +++ b/squidguard_report.c @@ -153,8 +153,7 @@ void squidguard_report(void) strcpy(oip,ip); } -// get_usertab_name(user,name,sizeof(name)); - user_find(name, user); + user_find(name, sizeof(name), user); if(dotinuser && strchr(name,'_')) { subs(name,sizeof(name),"_","."); diff --git a/topuser.c b/topuser.c index 0bfea87..bf98eca 100644 --- a/topuser.c +++ b/topuser.c @@ -472,8 +472,7 @@ void topuser(void) } if(strcmp(user2,"TOTAL") != 0) { -// get_usertab_name(user2,name,sizeof(name)); - user_find(name, user2); + user_find(name, sizeof(name), user2); if(Ip2Name && ((str=(char *) strstr(name, ".")) != (char *) NULL) && diff --git a/usertab.c b/usertab.c index 30afc25..df4d006 100644 --- a/usertab.c +++ b/usertab.c @@ -1,6 +1,6 @@ /* * AUTHOR: Pedro Lineu Orso pedro.orso@gmail.com - * 1998, 2009 + * 1998, 2010 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net * * SARG donations: @@ -26,118 +26,234 @@ #include "include/conf.h" #include "include/defs.h" +#ifdef HAVE_LDAP_H +#define LDAP_DEPRECATED 1 + #include #include #include +#endif //HAVE_LDAP_H -#define LDAP_DEPRECATED 1 +enum UserTabEnum +{ + //! Users matched against the ::UserTabFile file. + UTT_File, + //! Users matched agains a LDAP. + UTT_Ldap, + //! No user matching performed. + UTT_None +}; + +enum UserTabEnum which_usertab=UTT_None; -LDAP *ldap_handle; +static char *userfile=NULL; -void init_ldap_usertab() { +#ifdef HAVE_LDAP_H +static LDAP *ldap_handle=NULL; +#endif //HAVE_LDAP_H + +static void init_file_usertab(const char *UserTabFile) +{ + FILE *fp_usr; + long int nreg; + char buf[MAXLEN]; + int z2; + int z1; + + if((fp_usr=fopen(UserTabFile,"r"))==NULL) { + fprintf(stderr, "SARG: (log) %s: %s - %s\n",text[45],UserTabFile,strerror(errno)); + exit(1); + } + fseek(fp_usr, 0, SEEK_END); + nreg = ftell(fp_usr); + if (nreg<0) { + fprintf(stderr,"SARG: Cannot get the size of file %s",UserTabFile); + exit(1); + } + nreg += 100; + fseek(fp_usr, 0, SEEK_SET); + if((userfile=(char *) malloc(nreg))==NULL){ + fprintf(stderr, "SARG ERROR: %s",text[87]); + exit(1); + } + userfile[0]='\t'; + z2=1; + while(fgets(buf,sizeof(buf),fp_usr)!=NULL) { + if (buf[0]=='#') continue; + fixendofline(buf); + z1=0; + while(buf[z1] && (unsigned char)buf[z1]>' ') { + if (z2+3>=nreg) { //need at least 3 additional bytes for the minimum string "\n\t\0" + fprintf(stderr,"SARG: The list of the users is too long in your %s file.\n",UserTabFile); + exit(1); + } + userfile[z2++]=buf[z1++]; + } + while(buf[z1] && (unsigned char)buf[z1]<=' ') z1++; + userfile[z2++]='\n'; + while(buf[z1] && (unsigned char)buf[z1]>' ') { + if (z2+2>=nreg) { //need at least 2 additional bytes for "\t\0" + fprintf(stderr,"SARG: The list of the users is too long in your %s file.\n",UserTabFile); + exit(1); + } + userfile[z2++]=buf[z1++]; + } + userfile[z2++]='\t'; + } + userfile[z2]='\0'; + fclose(fp_usr); +} + +static void get_usertab_name(const char *user,char *name,int namelen) +{ + char warea[MAXLEN]; + char *str; + + namelen--; + sprintf(warea,"\t%s\n",user); + if((str=(char *) strstr(userfile,warea)) == (char *) NULL ) { + strncpy(name,user,namelen); + name[namelen]=0; + } else { + str=strchr(str+1,'\n'); + str++; + for(z1=0; *str != '\t' && z1' ') { - if (z2+3>=nreg) { //need at least 3 additional bytes for the minimum string "\n\t\0" - fprintf(stderr,"SARG: The list of the users is too long in your %s file.\n",UserTabFile); - exit(1); - } - userfile[z2++]=buf[z1++]; - } - while(buf[z1] && (unsigned char)buf[z1]<=' ') z1++; - userfile[z2++]='\n'; - while(buf[z1] && (unsigned char)buf[z1]>' ') { - if (z2+2>=nreg) { //need at least 2 additional bytes for "\t\0" - fprintf(stderr,"SARG: The list of the users is too long in your %s file.\n",UserTabFile); - exit(1); - } - userfile[z2++]=buf[z1++]; - } - userfile[z2++]='\t'; - } - userfile[z2]='\0'; - fclose(fp_usr); - } -} - -void get_usertab_name(const char *user,char *name,int namelen) -{ - char warea[MAXLEN]; - char *str; - - namelen--; - if(UserTabFile[0] == '\0') { - strncpy(name,user,namelen); - name[namelen]=0; - } else { - sprintf(warea,"\t%s\n",user); - if((str=(char *) strstr(userfile,warea)) == (char *) NULL ) { - strncpy(name,user,namelen); - name[namelen]=0; - } else { - str=strchr(str+1,'\n'); - str++; - for(z1=0; *str != '\t' && z1