/*
* 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;
}
}