From: Frederic Marchal Date: Wed, 22 Jul 2015 18:29:00 +0000 (+0200) Subject: Strip a suffix from the user name X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=36a0b94cbcaa8a9899fcc878639945e8787d0fec;p=thirdparty%2Fsarg.git Strip a suffix from the user name Only applied to the access.log file. Not yet available for the redirector.log. --- diff --git a/getconf.c b/getconf.c index 9ae0e40..188b466 100644 --- 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; diff --git a/include/readlog.h b/include/readlog.h index a481362..0861344 100644 --- a/include/readlog.h +++ b/include/readlog.h @@ -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. diff --git a/readlog.c b/readlog.c index 406f52b..cface7d 100644 --- 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); diff --git a/readlog_common.c b/readlog_common.c index b39ee9b..e223d01 100644 --- a/readlog_common.c +++ b/readlog_common.c @@ -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); } diff --git a/readlog_extlog.c b/readlog_extlog.c index dd1542a..74c3eb8 100644 --- a/readlog_extlog.c +++ b/readlog_extlog.c @@ -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); diff --git a/readlog_sarg.c b/readlog_sarg.c index c60ff28..8d429ad 100644 --- a/readlog_sarg.c +++ b/readlog_sarg.c @@ -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); } diff --git a/readlog_squid.c b/readlog_squid.c index 529abc9..188cb56 100644 --- a/readlog_squid.c +++ b/readlog_squid.c @@ -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); } diff --git a/sarg.conf b/sarg.conf index 76ada77..7ad9353 100644 --- a/sarg.conf +++ b/sarg.conf @@ -773,6 +773,12 @@ # #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