]> git.ipfire.org Git - thirdparty/sarg.git/commitdiff
Strip a suffix from the user name
authorFrederic Marchal <fmarchal@users.sourceforge.net>
Wed, 22 Jul 2015 18:29:00 +0000 (20:29 +0200)
committerFrederic Marchal <fmarchal@users.sourceforge.net>
Wed, 22 Jul 2015 18:29:00 +0000 (20:29 +0200)
Only applied to the access.log file.

Not yet available for the redirector.log.

getconf.c
include/readlog.h
readlog.c
readlog_common.c
readlog_extlog.c
readlog_sarg.c
readlog_squid.c
sarg.conf

index 9ae0e4057c6ca9719d4c41a3a18fdacc1ca30aff..188b4663725ca87b121e8e8c0cf2baf725a7c18a 100644 (file)
--- a/getconf.c
+++ b/getconf.c
@@ -38,6 +38,7 @@ extern enum PerUserFileCreationEnum PerUserFileCreation;
 extern char ImageDir[MAXLEN];
 extern FileListObject UserAgentLog;
 extern bool UserAgentFromCmdLine;
+extern char StripUserSuffix[MAX_USER_LEN];
 
 struct param_list
 {
@@ -905,6 +906,8 @@ static void parmtest(char *buf)
 
        if (getparam_list("ntlm_user_format",SET_LIST(ntml_userformat_values),buf,&NtlmUserFormat)>0) return;
 
+       if (getparam_string("strip_user_suffix",buf,StripUserSuffix,sizeof(StripUserSuffix))>0) return;
+
        if (getparam_string("realtime_types",buf,RealtimeTypes,sizeof(RealtimeTypes))>0) return;
 
        if (getparam_list("realtime_unauthenticated_records",SET_LIST(realtime_unauth_values),buf,&RealtimeUnauthRec)>0) return;
index a481362292df1dce3eb1290fd7c7e7674214e05d..0861344a56ebcb006e353f435931d45f6280cd43 100644 (file)
@@ -28,7 +28,7 @@ struct ReadLogStruct
        //! The IP address connecting to internet.
        const char *Ip;
        //! The user's name.
-       const char *User;
+       char *User;
        /*!
        The URL of the visited site.
 
index 406f52bbca30d0cd4ae2e97507c63c192441ad72..cface7d5bcdb0f669482c350f07073fafcf6bf05 100644 (file)
--- a/readlog.c
+++ b/readlog.c
@@ -89,6 +89,8 @@ enum ExcludeReasonEnum
 
 numlist weekdays = { { 0, 1, 2, 3, 4, 5, 6 }, 7 };
 numlist hours = { { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 }, 24 };
+//! Domain suffix to strip from the user name.
+char StripUserSuffix[MAX_USER_LEN]="";
 
 extern char *userfile;
 extern FileListObject AccessLog;
@@ -310,11 +312,13 @@ static void ReadOneLogFile(struct ReadLogDataStruct *Filter,const char *arq)
        char smartfilter[MAXLEN];
        const char *url;
        const char *user;
+       const char *UserPtr;
        int OutputNonZero = REPORT_EVERY_X_LINES ;
        int idata=0;
        int x;
        int hmr;
        int nopen;
+       int StripSuffixLen;
        int maxopenfiles=MAX_OPEN_USER_FILES;
        unsigned long int recs1=0UL;
        unsigned long int recs2=0UL;
@@ -365,6 +369,7 @@ static void ReadOneLogFile(struct ReadLogDataStruct *Filter,const char *arq)
 
        recs1=0UL;
        recs2=0UL;
+       StripSuffixLen=strlen(StripUserSuffix);
 
        // pre-read the file only if we have to show stats
        if (ShowReadStatistics && ShowReadPercent && fp_in->Rewind) {
@@ -524,7 +529,12 @@ static void ReadOneLogFile(struct ReadLogDataStruct *Filter,const char *arq)
                        continue;
                }
 
-
+               if (StripSuffixLen>0)
+               {
+                       x=strlen(log_entry.User);
+                       if (x>StripSuffixLen && strcasecmp(log_entry.User+(x-StripSuffixLen),StripUserSuffix)==0)
+                               log_entry.User[x-StripSuffixLen]='\0';
+               }
                if(strlen(log_entry.User) > MAX_USER_LEN) {
                        if (debugz>=LogLevel_Process) debuga(__FILE__,__LINE__,_("User ID too long: %s\n"),log_entry.User);
                        excluded_count[ER_UserNameTooLong]++;
@@ -608,13 +618,14 @@ static void ReadOneLogFile(struct ReadLogDataStruct *Filter,const char *arq)
                }
 
                if(UserIp) {
-                       log_entry.User=log_entry.Ip;
+                       UserPtr=log_entry.Ip;
                        id_is_ip=true;
                } else {
+                       UserPtr=log_entry.User;
                        id_is_ip=false;
-                       if ((log_entry.User[0]=='\0') || (log_entry.User[1]=='\0' && (log_entry.User[0]=='-' || log_entry.User[0]==' '))) {
+                       if ((UserPtr[0]=='\0') || (UserPtr[1]=='\0' && (UserPtr[0]=='-' || UserPtr[0]==' '))) {
                                if(RecordsWithoutUser == RECORDWITHOUTUSER_IP) {
-                                       log_entry.User=log_entry.Ip;
+                                       UserPtr=log_entry.Ip;
                                        id_is_ip=true;
                                }
                                if(RecordsWithoutUser == RECORDWITHOUTUSER_IGNORE) {
@@ -622,25 +633,25 @@ static void ReadOneLogFile(struct ReadLogDataStruct *Filter,const char *arq)
                                        continue;
                                }
                                if(RecordsWithoutUser == RECORDWITHOUTUSER_EVERYBODY)
-                                       log_entry.User="everybody";
+                                       UserPtr="everybody";
                        } else {
                                if(NtlmUserFormat == NTLMUSERFORMAT_USER) {
-                                       if ((str=strchr(log_entry.User,'+'))!=NULL || (str=strchr(log_entry.User,'\\'))!=NULL || (str=strchr(log_entry.User,'_'))!=NULL) {
-                                               log_entry.User=str+1;
+                                       if ((str=strchr(UserPtr,'+'))!=NULL || (str=strchr(UserPtr,'\\'))!=NULL || (str=strchr(UserPtr,'_'))!=NULL) {
+                                               UserPtr=str+1;
                                        }
                                }
                        }
                }
 
                if(us[0] != '\0'){
-                       if(strcmp(log_entry.User,us)!=0) {
+                       if(strcmp(UserPtr,us)!=0) {
                                excluded_count[ER_UntrackedUser]++;
                                continue;
                        }
                }
 
                if(Filter->SysUsers) {
-                       snprintf(wuser,sizeof(wuser),":%s:",log_entry.User);
+                       snprintf(wuser,sizeof(wuser),":%s:",UserPtr);
                        if(strstr(userfile, wuser) == 0) {
                                excluded_count[ER_SysUser]++;
                                continue;
@@ -648,21 +659,21 @@ static void ReadOneLogFile(struct ReadLogDataStruct *Filter,const char *arq)
                }
 
                if(Filter->UserFilter) {
-                       if(!vuexclude(log_entry.User)) {
-                               if (debugz>=LogLevel_Process) debuga(__FILE__,__LINE__,_("Excluded user: %s\n"),log_entry.User);
+                       if(!vuexclude(UserPtr)) {
+                               if (debugz>=LogLevel_Process) debuga(__FILE__,__LINE__,_("Excluded user: %s\n"),UserPtr);
                                excluded_count[ER_IgnoredUser]++;
                                totregsx++;
                                continue;
                        }
                }
 
-               user=process_user(log_entry.User);
-               if (log_entry.User!=user) {
-                       log_entry.User=user;
+               user=process_user(UserPtr);
+               if (UserPtr!=user)
+               {
+                       UserPtr=user;
                        id_is_ip=false;
                }
-               if (log_entry.User[0]=='\0' || (log_entry.User[1]=='\0' && (log_entry.User[0]=='-' ||
-                               log_entry.User[0]==' ' || log_entry.User[0]==':'))) {
+               if (UserPtr[0]=='\0' || (UserPtr[1]=='\0' && (UserPtr[0]=='-' || UserPtr[0]==' ' || UserPtr[0]==':'))) {
                        excluded_count[ER_NoUser]++;
                        continue;
                }
@@ -681,14 +692,14 @@ static void ReadOneLogFile(struct ReadLogDataStruct *Filter,const char *arq)
 
                nopen=0;
                prev_ufile=NULL;
-               for (ufile=first_user_file ; ufile && strcmp(log_entry.User,ufile->user->id)!=0 ; ufile=ufile->next) {
+               for (ufile=first_user_file ; ufile && strcmp(UserPtr,ufile->user->id)!=0 ; ufile=ufile->next) {
                        prev_ufile=ufile;
                        if (ufile->file) nopen++;
                }
                if (!ufile) {
                        ufile=malloc(sizeof(*ufile));
                        if (!ufile) {
-                               debuga(__FILE__,__LINE__,_("Not enough memory to store the user %s\n"),log_entry.User);
+                               debuga(__FILE__,__LINE__,_("Not enough memory to store the user %s\n"),UserPtr);
                                exit(EXIT_FAILURE);
                        }
                        memset(ufile,0,sizeof(*ufile));
@@ -698,7 +709,7 @@ static void ReadOneLogFile(struct ReadLogDataStruct *Filter,const char *arq)
                         * This id_is_ip stuff is just to store the string only once if the user is
                         * identified by its IP address instead of a distinct ID and IP address.
                         */
-                       uinfo=userinfo_create(log_entry.User,(id_is_ip) ? NULL : log_entry.Ip);
+                       uinfo=userinfo_create(UserPtr,(id_is_ip) ? NULL : log_entry.Ip);
                        ufile->user=uinfo;
                        nusers++;
                } else {
@@ -747,14 +758,14 @@ static void ReadOneLogFile(struct ReadLogDataStruct *Filter,const char *arq)
                if (fprintf(ufile->file, "%s\t%s\t%s\t%s\t%"PRIu64"\t%s\t%ld\t%s\n",dia,hora,
                                                                log_entry.Ip,url,(uint64_t)log_entry.DataSize,
                                                                log_entry.HttpCode,log_entry.ElapsedTime,smartfilter)<=0) {
-                       debuga(__FILE__,__LINE__,_("Write error in the log file of user %s\n"),log_entry.User);
+                       debuga(__FILE__,__LINE__,_("Write error in the log file of user %s\n"),UserPtr);
                        exit(EXIT_FAILURE);
                }
                records_kept++;
 
                if (fp_log && log_line.current_format!=&ReadSargLog) {
                        fprintf(fp_log, "%s\t%s\t%s\t%s\t%s\t%"PRIu64"\t%s\t%ld\t%s\n",dia,hora,
-                                                       log_entry.User,log_entry.Ip,url,(uint64_t)log_entry.DataSize,
+                                                       UserPtr,log_entry.Ip,url,(uint64_t)log_entry.DataSize,
                                                        log_entry.HttpCode,log_entry.ElapsedTime,smartfilter);
                }
 
@@ -777,7 +788,7 @@ static void ReadOneLogFile(struct ReadLogDataStruct *Filter,const char *arq)
 
                if (debugz>=LogLevel_Data){
                        printf("IP=\t%s\n",log_entry.Ip);
-                       printf("USER=\t%s\n",log_entry.User);
+                       printf("USER=\t%s\n",UserPtr);
                        printf("ELAP=\t%ld\n",log_entry.ElapsedTime);
                        printf("DATE=\t%s\n",dia);
                        printf("TIME=\t%s\n",hora);
index b39ee9b9a313c5f8164f25144cd31e8f3223d98f..e223d01166bcdff6f19383285f8e10fae5274683 100644 (file)
@@ -80,7 +80,6 @@ static enum ReadLogReturnCodeEnum Common_ReadEntry(char *Line,struct ReadLogStru
        int Minute;
        int Second;
        char *Ip;
-       char *User;
 
        // get IP address
        Entry->Ip=Ip=Line;
@@ -95,7 +94,7 @@ static enum ReadLogReturnCodeEnum Common_ReadEntry(char *Line,struct ReadLogStru
        }
 
        // the ID of the user or - if the user is unidentified
-       Entry->User=User=++Line;
+       Entry->User=++Line;
        for (UserLen=0 ; *Line && *Line!=' ' ; UserLen++) Line++;
        if (*Line!=' ' || UserLen==0) return(RLRC_Unknown);
 
@@ -196,7 +195,7 @@ static enum ReadLogReturnCodeEnum Common_ReadEntry(char *Line,struct ReadLogStru
        Ip[IpLen]='\0';
        Entry->HttpCode[HttpCodeLen]='\0';
        Entry->Url[UrlLen]='\0';
-       User[UserLen]='\0';
+       Entry->User[UserLen]='\0';
 
        return(RLRC_NoError);
 }
index dd1542a83c00b31546fe4ada6d40c9ecc09d08c3..74c3eb874d6e6a7ab254466e14a3af318fcdef5f 100644 (file)
@@ -385,7 +385,6 @@ static enum ReadLogReturnCodeEnum ExtLog_ReadEntry(char *Line,struct ReadLogStru
        enum ext_col_id col_id;
        char *Ip=NULL;
        char *IpEnd;
-       char *User=NULL;
        char *UserEnd;
        char *UrlEnd;
        char *HttpCodeEnd;
@@ -413,7 +412,7 @@ static enum ReadLogReturnCodeEnum ExtLog_ReadEntry(char *Line,struct ReadLogStru
                                if (!Line) return(RLRC_Unknown);
                                break;
                        case EXTCOL_UserName:
-                               Entry->User=User=Line;
+                               Entry->User=Line;
                                Line=ExtLog_GetString(Line,col,&UserEnd);
                                if (!Line) return(RLRC_Unknown);
                                break;
@@ -464,7 +463,7 @@ static enum ReadLogReturnCodeEnum ExtLog_ReadEntry(char *Line,struct ReadLogStru
        }
 
        ExtLog_FixString(Ip,IpEnd);
-       ExtLog_FixString(User,UserEnd);
+       ExtLog_FixString(Entry->User,UserEnd);
        ExtLog_FixString(Entry->Url,UrlEnd);
        ExtLog_FixString(Entry->HttpCode,HttpCodeEnd);
 
index c60ff285d0f1340b3cefddf778f0b44abf2a9988..8d429ad3507e2f89d425ee60bec462908f5fd0c4 100644 (file)
@@ -68,7 +68,6 @@ static enum ReadLogReturnCodeEnum Sarg_ReadEntry(char *Line,struct ReadLogStruct
        int Minute;
        int Second;
        char *Ip;
-       char *User;
 
        if (strncmp(Line,"*** SARG Log ***",16)==0) {
                if (InvalidFileName) {
@@ -120,7 +119,7 @@ static enum ReadLogReturnCodeEnum Sarg_ReadEntry(char *Line,struct ReadLogStruct
        Entry->EntryTime.tm_isdst=-1;
 
        // the ID of the user
-       Entry->User=User=++Line;
+       Entry->User=++Line;
        for (UserLen=0 ; *Line && *Line!='\t' ; UserLen++) Line++;
        if (*Line!='\t' || UserLen==0) return(RLRC_Unknown);
 
@@ -164,7 +163,7 @@ static enum ReadLogReturnCodeEnum Sarg_ReadEntry(char *Line,struct ReadLogStruct
        Ip[IpLen]='\0';
        Entry->HttpCode[HttpCodeLen]='\0';
        Entry->Url[UrlLen]='\0';
-       User[UserLen]='\0';
+       Entry->User[UserLen]='\0';
 
        return(RLRC_NoError);
 }
index 529abc9a5290c20e69332c9137fea30d2b6b5bf0..188cb56836083dbcc26751e1c0b4d36fbf2a3adb 100644 (file)
@@ -56,7 +56,6 @@ static enum ReadLogReturnCodeEnum Squid_ReadEntry(char *Line,struct ReadLogStruc
        int UserLen;
        struct tm *tt;
        char *Ip;
-       char *User;
 
        // get log time.
        Begin=Line;
@@ -121,7 +120,7 @@ static enum ReadLogReturnCodeEnum Squid_ReadEntry(char *Line,struct ReadLogStruc
        if (*Line!=' ' || UrlLen==0) return(RLRC_Unknown);
 
        // the ID of the user or - if the user is unidentified
-       Entry->User=User=++Line;
+       Entry->User=++Line;
        for (UserLen=0 ; *Line && *Line!=' ' ; UserLen++) Line++;
        if (*Line!=' ' || UserLen==0) return(RLRC_Unknown);
 
@@ -138,7 +137,7 @@ static enum ReadLogReturnCodeEnum Squid_ReadEntry(char *Line,struct ReadLogStruc
        Entry->HttpCode[HttpCodeLen]='\0';
        Entry->HttpMethod[HttpMethodLen]='\0';
        Entry->Url[UrlLen]='\0';
-       User[UserLen]='\0';
+       Entry->User[UserLen]='\0';
 
        return(RLRC_NoError);
 }
index 76ada770ef549bf6351977087071b27e30a64dcf..7ad93534fb0d677c2959fcb89f68cd4701ac83c8 100644 (file)
--- a/sarg.conf
+++ b/sarg.conf
 #
 #ntlm_user_format domainname+username
 
+# TAG: strip_user_suffix suffix
+#      Remove a suffix from the user name. The suffix may be
+#      a Kerberos domain name. It must be at the end of the
+#      user name (as is implied by a suffix).
+#strip_user_suffix @example.com
+
 # TAG: realtime_refresh_time num sec
 #      How many time to auto refresh the realtime report
 #      0 = disable