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.
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)
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)
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)
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),"_",".");
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)
}
}
-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)))
node = left;
tmp->left = right;
node->right = tmp;
-
+
if (root_bt == tmp)
root_bt = node;
else
default:
exit(1);
break;
-
+
}
}
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)
{
}
-char *search_in_cache(char *key)
+char *search_in_cache(const char *key)
{
struct bt *node;
if ((node = search_item(root_bt, key)))
return NULL;
}
-void destroy_cache()
+void destroy_cache(void)
{
delete_tree(root_bt);
root_bt = NULL;
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),"_",".");
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),"_",".");
--- /dev/null
+/*!\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.
+*/
-/*! \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.
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),"_",".");
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;
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);
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),"_",".");
--- /dev/null
+#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
#ifdef HAVE_WINSOCK_H
#include <winsock.h>
#endif
-#ifdef HAVE_LDAP_H
-#include <ldap.h>
-#endif
#ifdef HAVE_MATH_H
#include <math.h>
#endif
char urly[MAXLEN];
char user[MAXLEN];
char period[MAXLEN];
-char msg[1024];
char code[MAXLEN];
char code2[MAXLEN];
char tmp[MAXLEN];
char AuthName[512];
char AuthType[255];
char Require[512];
-char *userfile;
char *str;
char *str2;
char text[200][255];
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];
// 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);
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();
-#define VERSION PACKAGE_VERSION" Jan-22-2010"
+#define VERSION PACKAGE_VERSION" Jan-24-2010"
#define PGM PACKAGE_NAME
#define URL "http://sarg.sourceforge.net"
char data[255];
char elap[255];
char ip[MAXLEN];
- char msg[MAXLEN];
char tam[255];
char fun[MAXLEN];
char wuser[MAXLEN];
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';
}
#endif
- read_usertab(UserTabFile);
+ init_usertab(UserTabFile);
sprintf ( sz_Download_Unsort , "%s/sarg/download.unsort", tmp);
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)
int fd1,fd2;
int cstatus;
- read_usertab(UserTabFile);
+ init_usertab(UserTabFile);
fd1 = mkstemp(template1);
fd2 = mkstemp(template2);
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),"_",".");
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),"_",".");
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),"_",".");
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),"_",".");
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),"_",".");
}
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) &&
/*
* 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;
}
}
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;