]> git.ipfire.org Git - thirdparty/sarg.git/commitdiff
Fixed a regression in the usertab file not accepting IPv6 addresses any more.
authorFrédéric Marchal <fmarchal@users.sourceforge.net>
Sun, 24 Jan 2010 16:31:04 +0000 (16:31 +0000)
committerFrédéric Marchal <fmarchal@users.sourceforge.net>
Sun, 24 Jan 2010 16:31:04 +0000 (16:31 +0000)
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.

24 files changed:
CMakeLists.txt
authfail.c
btree_cache.c
dansguardian_report.c
denied.c
documentation/usertab.txt [new file with mode: 0644]
documentation/util.txt
download.c
getconf.c
grepday.c
html.c
include/btree_cache.h [new file with mode: 0644]
include/conf.h
include/defs.h
include/info.h
log.c
realtime.c
repday.c
report.c
siteuser.c
squidguard_report.c
topuser.c
usertab.c
util.c

index b43a1c7008be4e35134b5aa6b22aa973a91f03c8..578ee22fb079aa6e0add7cdc38bb379a94a957ec 100755 (executable)
@@ -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)
index 0e1a93410eb16351a33ceaced4d6d8812bcfb231..401c0ef4f9799343420e0502a63bc4e5315c4758 100644 (file)
@@ -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),"_",".");
index b3b9005e120cc068d35ca66aad942ad2f774c28f..719c3810141e1fa4c73faaef67c33e9afa7b0ddc 100644 (file)
@@ -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;
index 43890bcdd9a803640f066bc239bad088e56c807f..3b6a411b1ece2b7a31f88078388e55a79ddc69fd 100644 (file)
@@ -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),"_",".");
index 76c6e9d6a769665d1b8a3312b6458d733243acf7..ba9d432b84407a993a095158808c5f8e2bc79446 100644 (file)
--- 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 (file)
index 0000000..a9c5653
--- /dev/null
@@ -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.
+*/
index 15ce1867b207b0b4a33a40d2bd48cb7c14e44058..ce89c0aeb778dfd259d909bfb2bcbb4ddeec0e30 100644 (file)
@@ -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.
index 6efba37e612f4d9d732d0a2fc2850aa465945442..0cf96bd47732ddc82887aed1680a558faf96615f 100644 (file)
@@ -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),"_",".");
index 6d43c51d60efe973b0ad09cd490d39ae8dc11657..32e38f41ee6bfc9f96aa8567f5146927d6412431 100644 (file)
--- 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;
 
index e17b3acfe3699b191bfc31aa88e15b05bf00e7cb..ba2ed71ffef5a498f206fb8f1c540741d95d5ba8 100644 (file)
--- 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 0c0fb33838c71085db277450533118868b06ec73..79f174d2afc01611063724b004081d8ed391cdd0 100644 (file)
--- 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 (file)
index 0000000..2892610
--- /dev/null
@@ -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
index 30432f3c6a1618378e78714687bb4f66f2ca8799..b58a70a25ea78c797ffb78a1860a38f41c7c6886 100755 (executable)
@@ -95,9 +95,6 @@ gdPoint points[4];
 #ifdef HAVE_WINSOCK_H
 #include <winsock.h>
 #endif
-#ifdef HAVE_LDAP_H
-#include <ldap.h>
-#endif
 #ifdef HAVE_MATH_H
 #include <math.h>
 #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];
index e79a44752e348b139c8b0be2bfd92d991dfe822a..1df5f5ea08301ba5b8ab8874dcc0a411cbbcc34d 100755 (executable)
@@ -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();
index 45a38c8484a93ea622b92e2f9fb7fa4d8a2c12e2..fe4a1f72393cf326f300a374223f2c24ea8dbc7a 100755 (executable)
@@ -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 f64802d46f83960f0991faf85ef5fcd9309427b8..299bc9e182159b4bb11294b82209625ab3344475 100644 (file)
--- 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)
index 4e10c817568788aa6cba92e5cd4d808a746a332f..fe33c30251ff9648034e481516783da40ef0fce9 100755 (executable)
@@ -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),"_",".");
index ce2ca9ed48f52c61a91ee4719385019ddce36278..0a0f2504a6116d3dc4643b3405568549f03efaa0 100644 (file)
--- 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),"_",".");
index 2568f666aa37383183f8a8023d96d3cd4f901034..6c095a01feff748865697906dbfade782727f3ae 100644 (file)
--- 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),"_",".");
index 870f4471800b411aedcc457518822f5ce218567a..6484664b98bf6d060b0c918ae4ca29ab6275f715 100644 (file)
@@ -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),"_",".");
index 08743cf0de5e7611b60d85cb72aa35fa4230713b..a062aecd355cf54ea0f154961881cf55de0e9fae 100644 (file)
@@ -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),"_",".");
index 0bfea87d0abdd62024af6c3350de8a65d22d450b..bf98eca35e60fd4fe2a1994390afa8e9c797fd4d 100644 (file)
--- 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) &&
index 30afc256e43d0ecfd6c5364d6387e9eb809bad7d..df4d0063f639e96991dbff1a78b3ee2a217f4c20 100644 (file)
--- 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:
 #include "include/conf.h"
 #include "include/defs.h"
 
+#ifdef HAVE_LDAP_H
+#define LDAP_DEPRECATED 1
+
 #include <ldap.h>
 #include <ldap_cdefs.h>
 #include <ldap_features.h>
+#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<namelen ; z1++) {
+         name[z1]=*str++;
+      }
+      name[z1]=0;
+   }
+}
+
+#ifdef HAVE_LDAP_H
+static void init_ldap_usertab(void) {
        /* Setting LDAP connection and initializing cache */
        ldap_handle = NULL;
-       int ldap_port = atoi(LDAPPort);
-        if ((ldap_handle = (LDAP *)ldap_init(LDAPHost, ldap_port)) == NULL) {
-               sprintf(msg,"\nUnable to connect to LDAP server:%s port:%d\n", LDAPHost, ldap_port);
-               debuga(msg);
+   if ((ldap_handle = ldap_init(LDAPHost, LDAPPort)) == NULL) {
+               debuga("\nUnable to connect to LDAP server:%s port:%d\n", LDAPHost, LDAPPort);
                exit(1);
-        }
+   }
 
-        int ldap_protocol_version = atoi(LDAPProtocolVersion);
-        if (ldap_set_option(ldap_handle, LDAP_OPT_PROTOCOL_VERSION, &ldap_protocol_version) != LDAP_SUCCESS) {
-               sprintf(msg, "Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", ldap_protocol_version);
-               debuga(msg);
+   int ldap_protocol_version = LDAPProtocolVersion;
+   if (ldap_set_option(ldap_handle, LDAP_OPT_PROTOCOL_VERSION, &ldap_protocol_version) != LDAP_SUCCESS) {
+               debuga("Could not set LDAP_OPT_PROTOCOL_VERSION %d\n", ldap_protocol_version);
                exit(1);
-        }
+   }
 
        /* Bind to the LDAP server. */
        int rc;
-       rc = ldap_simple_bind_s( ldap_handle, LDAPBindDN, LDAPBindPW ); 
-       if ( rc != LDAP_SUCCESS ) { 
-               sprintf(msg, "ldap_simple_bind_s: %s\n", ldap_err2string(rc)); 
-               debuga(msg);
-               exit(1); 
+       rc = ldap_simple_bind_s( ldap_handle, LDAPBindDN, LDAPBindPW );
+       if ( rc != LDAP_SUCCESS ) {
+               debuga("ldap_simple_bind_s: %s\n", ldap_err2string(rc));
+               exit(1);
        }
-       
+
        /* Initializing cache */
 
        init_cache();
 }
 
-void user_find(char *mappedname, char *userlogin) {
-   if(UserTabFile[0] != '\0') {
-      if (strcasecmp(UserTabFile, "ldap")) {
-         sprintf(warea,":%s:",userlogin);
-         if((str=(char *) strstr(userfile,warea)) != (char *) NULL ) {
-            z1=0;
-           str2=(char *) strstr(str+1,":");
-           str2++;
-           bzero(name, MAXLEN);
-           while(str2[z1] != ':') {
-              name[z1]=str2[z1];
-              z1++;
-           }
-        } else strcpy(mappedname,userlogin);
-      } else {
-
-      /* Start searching username in cache */
-       
-      char filtersearch[256], strictchars[] = " ~!@^&(){}|<>?:;\"\'\\[]`,\r\n\0", *strictptr = strictchars, *searched_in_cache;
-      char *attr, **vals;
-      LDAPMessage *result, *e;
-      BerElement *ber;
-
-      while (*strictptr) {
-         char *foundchr;
-         if ((foundchr = strchr(userlogin, *strictptr)))
-            *foundchr = '\0';
-         strictptr++;
+static void get_ldap_name(const char *userlogin,char *mappedname,int namelen)
+{
+   /* Start searching username in cache */
+
+   char filtersearch[256], strictchars[] = " ~!@^&(){}|<>?:;\"\'\\[]`,\r\n\0", *strictptr = strictchars, *searched_in_cache;
+   char *attr, **vals;
+   LDAPMessage *result, *e;
+   BerElement *ber;
+
+   while (*strictptr) {
+      char *foundchr;
+      if ((foundchr = strchr(userlogin, *strictptr)))
+         *foundchr = '\0';
+      strictptr++;
+   }
+
+   if (!(searched_in_cache = search_in_cache(userlogin))) {
+      snprintf(filtersearch, sizeof(filtersearch), LDAPFilterSearch, userlogin, userlogin, userlogin, userlogin, userlogin);
+
+      /* Search record(s) in LDAP base */
+
+      int rc= ldap_search_s(ldap_handle, LDAPBaseSearch, LDAP_SCOPE_SUBTREE, filtersearch, NULL, 0, &result);
+      if ( rc != LDAP_SUCCESS ) {
+         debuga("ldap_search_s: %s\n", ldap_err2string(rc));
+         strcpy(mappedname,userlogin);
+         return;
       }
-               
-      if (!(searched_in_cache = search_in_cache(userlogin))) {
-         snprintf(filtersearch, sizeof(filtersearch), LDAPFilterSearch, userlogin, userlogin, userlogin, userlogin, userlogin);
-                               
-         /* Search record(s) in LDAP base */
-                       
-         int rc= ldap_search_s(ldap_handle, LDAPBaseSearch, LDAP_SCOPE_SUBTREE, filtersearch, NULL, 0, &result);
-         if ( rc != LDAP_SUCCESS ) { 
-            sprintf(msg, "ldap_search_s: %s\n", ldap_err2string(rc));
-            debuga(msg);
-            strcpy(mappedname,userlogin);
-            return;
-         }
-                               
-         if (!(e = ldap_first_entry(ldap_handle, result)))
-            insert_to_cache(userlogin, userlogin);
-         else
-            for (attr = ldap_first_attribute(ldap_handle, e, &ber); attr != NULL; attr = ldap_next_attribute(ldap_handle, e, ber)) {
-               if (!strcasecmp(attr, LDAPTargetAttr)) {
-                  if ((vals = (char **)ldap_get_values(ldap_handle, e, attr))!=NULL) {
-                     insert_to_cache(userlogin, vals[0]);
-                     strcpy(mappedname, vals[0]);
-                     ldap_memfree(vals);
-                  }
-                  ldap_memfree(attr);
-                  break;
+
+      if (!(e = ldap_first_entry(ldap_handle, result)))
+         insert_to_cache(userlogin, userlogin);
+      else
+         for (attr = ldap_first_attribute(ldap_handle, e, &ber); attr != NULL; attr = ldap_next_attribute(ldap_handle, e, ber)) {
+            if (!strcasecmp(attr, LDAPTargetAttr)) {
+               if ((vals = (char **)ldap_get_values(ldap_handle, e, attr))!=NULL) {
+                  insert_to_cache(userlogin, vals[0]);
+                  strncpy(mappedname, vals[0],namelen-1);
+                  mappedname[namelen-1]='\0';
+                  ldap_memfree(vals);
                }
                ldap_memfree(attr);
+               break;
             }
-            ldap_msgfree(result);
-         } else
-              strcpy(mappedname, searched_in_cache);
-      }
-   } else
-       strcpy(mappedname,userlogin);
+            ldap_memfree(attr);
+         }
+         ldap_msgfree(result);
+   } else {
+       strncpy(mappedname, searched_in_cache,namelen-1);
+       mappedname[namelen-1]='\0';
+   }
+}
+#endif //HAVE_LDAP_H
+
+void init_usertab(const char *UserTabFile)
+{
+   if (strcmp(UserTabFile, "ldap") == 0) {
+      if(debug)
+         debuga("%s: %s",text[86],UserTabFile);
+#ifdef HAVE_LDAP_H
+      which_usertab=UTT_Ldap;
+      init_ldap_usertab();
+#else
+      fprintf(stderr,"SARG: LDAP module not compiled in sarg\n");
+      exit(1);
+#endif //HAVE_LDAP_H
+   } else if (UserTabFile[0] != '\0') {
+      if(debug)
+         debuga("%s: %s",text[86],UserTabFile);
+      which_usertab=UTT_File;
+      init_file_usertab(UserTabFile);
+   } else {
+      which_usertab=UTT_None;
+   }
+}
+
+void user_find(char *mappedname, int namelen, const char *userlogin)
+{
+   if (which_usertab==UTT_File) {
+      get_usertab_name(userlogin,mappedname,namelen);
+   }
+#ifdef HAVE_LDAP_H
+   else if (which_usertab==UTT_Ldap) {
+      get_ldap_name(userlogin,mappedname,namelen);
+   }
+#endif //HAVE_LDAP_H
+   else {
+      strncpy(mappedname,userlogin,namelen-1);
+      mappedname[namelen-1]='\0';
+   }
 }
 
-void close_usertab() {
-   if (!strcasecmp(UserTabFile, "ldap")) {
+void close_usertab(void)
+{
+#ifdef HAVE_LDAP_H
+   if (ldap_handle) {
       destroy_cache();
       ldap_unbind(ldap_handle);
-   } else {
-       if(userfile)
-           free(userfile);
+      ldap_handle=NULL;
+   }
+#endif //HAVE_LDAP_H
+   if(userfile) {
+      free(userfile);
+      userfile=NULL;
    }
 }
 
diff --git a/util.c b/util.c
index 95a005591e06cf86a35318ae544befceebc3d239..e16aa8ab66d4ac4e74d1812d5eb0ad57ac79c25a 100644 (file)
--- a/util.c
+++ b/util.c
@@ -1546,91 +1546,6 @@ char *get_param_value(const char *param,char *line)
    return(line);
 }
 
-void read_usertab(const char *UserTabFile)
-{
-   FILE *fp_usr;
-   long int nreg;
-   char buf[MAXLEN];
-   int z2;
-   int z1;
-
-   if (strcmp(UserTabFile, "ldap") == 0) {
-      if(debug)
-         debuga("%s: %s",text[86],UserTabFile);
-      init_ldap_usertab();
-   } else if (UserTabFile[0] != '\0') {
-      if(debug)
-         debuga("%s: %s",text[86],UserTabFile);
-      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);
-   }
-}
-
-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<namelen ; z1++) {
-            name[z1]=*str++;
-         }
-         name[z1]=0;
-      }
-   }
-}
-
 void unlinkdir(const char *dir,int contentonly)
 {
    struct stat st;