From: Frederic Marchal Date: Fri, 24 Jul 2015 14:09:20 +0000 (+0200) Subject: Fix an error introduced when stripping the user domain X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=7799ec8d818674a432daf6291b60fafb77ac32f5;p=thirdparty%2Fsarg.git Fix an error introduced when stripping the user domain Several reports such as the download and authentication reports would fail with an error about unknown user "-". It has been fixed by choosing a new method to strip the domain. The change partially reverts some changes made in commit 36a0b94cbcaa8a9899fcc878639945e8787d0fec that were responsible for the proper user name not being propagated to the intermediary report files. Thanks to Yakushev Evgeniy for reporting this bug. --- diff --git a/include/readlog.h b/include/readlog.h index 0861344..a481362 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. - char *User; + const char *User; /*! The URL of the visited site. diff --git a/readlog.c b/readlog.c index cface7d..c984b6e 100644 --- a/readlog.c +++ b/readlog.c @@ -312,7 +312,7 @@ static void ReadOneLogFile(struct ReadLogDataStruct *Filter,const char *arq) char smartfilter[MAXLEN]; const char *url; const char *user; - const char *UserPtr; + char UserBuffer[MAX_USER_LEN]; int OutputNonZero = REPORT_EVERY_X_LINES ; int idata=0; int x; @@ -533,7 +533,17 @@ static void ReadOneLogFile(struct ReadLogDataStruct *Filter,const char *arq) { x=strlen(log_entry.User); if (x>StripSuffixLen && strcasecmp(log_entry.User+(x-StripSuffixLen),StripUserSuffix)==0) - log_entry.User[x-StripSuffixLen]='\0'; + { + if (x-StripSuffixLen>=sizeof(UserBuffer)) { + if (debugz>=LogLevel_Process) debuga(__FILE__,__LINE__,_("User ID too long: %s\n"),log_entry.User); + excluded_count[ER_UserNameTooLong]++; + totregsx++; + continue; + } + safe_strcpy(UserBuffer,log_entry.User,sizeof(UserBuffer)); + UserBuffer[x-StripSuffixLen]='\0'; + log_entry.User=UserBuffer; + } } if(strlen(log_entry.User) > MAX_USER_LEN) { if (debugz>=LogLevel_Process) debuga(__FILE__,__LINE__,_("User ID too long: %s\n"),log_entry.User); @@ -618,14 +628,14 @@ static void ReadOneLogFile(struct ReadLogDataStruct *Filter,const char *arq) } if(UserIp) { - UserPtr=log_entry.Ip; + log_entry.User=log_entry.Ip; id_is_ip=true; } else { - UserPtr=log_entry.User; + log_entry.User=log_entry.User; id_is_ip=false; - if ((UserPtr[0]=='\0') || (UserPtr[1]=='\0' && (UserPtr[0]=='-' || UserPtr[0]==' '))) { + if ((log_entry.User[0]=='\0') || (log_entry.User[1]=='\0' && (log_entry.User[0]=='-' || log_entry.User[0]==' '))) { if(RecordsWithoutUser == RECORDWITHOUTUSER_IP) { - UserPtr=log_entry.Ip; + log_entry.User=log_entry.Ip; id_is_ip=true; } if(RecordsWithoutUser == RECORDWITHOUTUSER_IGNORE) { @@ -633,25 +643,25 @@ static void ReadOneLogFile(struct ReadLogDataStruct *Filter,const char *arq) continue; } if(RecordsWithoutUser == RECORDWITHOUTUSER_EVERYBODY) - UserPtr="everybody"; + log_entry.User="everybody"; } else { if(NtlmUserFormat == NTLMUSERFORMAT_USER) { - if ((str=strchr(UserPtr,'+'))!=NULL || (str=strchr(UserPtr,'\\'))!=NULL || (str=strchr(UserPtr,'_'))!=NULL) { - UserPtr=str+1; + 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(us[0] != '\0'){ - if(strcmp(UserPtr,us)!=0) { + if(strcmp(log_entry.User,us)!=0) { excluded_count[ER_UntrackedUser]++; continue; } } if(Filter->SysUsers) { - snprintf(wuser,sizeof(wuser),":%s:",UserPtr); + snprintf(wuser,sizeof(wuser),":%s:",log_entry.User); if(strstr(userfile, wuser) == 0) { excluded_count[ER_SysUser]++; continue; @@ -659,21 +669,21 @@ static void ReadOneLogFile(struct ReadLogDataStruct *Filter,const char *arq) } if(Filter->UserFilter) { - if(!vuexclude(UserPtr)) { - if (debugz>=LogLevel_Process) debuga(__FILE__,__LINE__,_("Excluded user: %s\n"),UserPtr); + if(!vuexclude(log_entry.User)) { + if (debugz>=LogLevel_Process) debuga(__FILE__,__LINE__,_("Excluded user: %s\n"),log_entry.User); excluded_count[ER_IgnoredUser]++; totregsx++; continue; } } - user=process_user(UserPtr); - if (UserPtr!=user) + user=process_user(log_entry.User); + if (log_entry.User!=user) { - UserPtr=user; + log_entry.User=user; id_is_ip=false; } - if (UserPtr[0]=='\0' || (UserPtr[1]=='\0' && (UserPtr[0]=='-' || UserPtr[0]==' ' || UserPtr[0]==':'))) { + if (log_entry.User[0]=='\0' || (log_entry.User[1]=='\0' && (log_entry.User[0]=='-' || log_entry.User[0]==' ' || log_entry.User[0]==':'))) { excluded_count[ER_NoUser]++; continue; } @@ -692,14 +702,14 @@ static void ReadOneLogFile(struct ReadLogDataStruct *Filter,const char *arq) nopen=0; prev_ufile=NULL; - for (ufile=first_user_file ; ufile && strcmp(UserPtr,ufile->user->id)!=0 ; ufile=ufile->next) { + for (ufile=first_user_file ; ufile && strcmp(log_entry.User,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"),UserPtr); + debuga(__FILE__,__LINE__,_("Not enough memory to store the user %s\n"),log_entry.User); exit(EXIT_FAILURE); } memset(ufile,0,sizeof(*ufile)); @@ -709,7 +719,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(UserPtr,(id_is_ip) ? NULL : log_entry.Ip); + uinfo=userinfo_create(log_entry.User,(id_is_ip) ? NULL : log_entry.Ip); ufile->user=uinfo; nusers++; } else { @@ -758,14 +768,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"),UserPtr); + debuga(__FILE__,__LINE__,_("Write error in the log file of user %s\n"),log_entry.User); 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, - UserPtr,log_entry.Ip,url,(uint64_t)log_entry.DataSize, + log_entry.User,log_entry.Ip,url,(uint64_t)log_entry.DataSize, log_entry.HttpCode,log_entry.ElapsedTime,smartfilter); } @@ -788,7 +798,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",UserPtr); + printf("USER=\t%s\n",log_entry.User); 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 e223d01..b39ee9b 100644 --- a/readlog_common.c +++ b/readlog_common.c @@ -80,6 +80,7 @@ static enum ReadLogReturnCodeEnum Common_ReadEntry(char *Line,struct ReadLogStru int Minute; int Second; char *Ip; + char *User; // get IP address Entry->Ip=Ip=Line; @@ -94,7 +95,7 @@ static enum ReadLogReturnCodeEnum Common_ReadEntry(char *Line,struct ReadLogStru } // the ID of the user or - if the user is unidentified - Entry->User=++Line; + Entry->User=User=++Line; for (UserLen=0 ; *Line && *Line!=' ' ; UserLen++) Line++; if (*Line!=' ' || UserLen==0) return(RLRC_Unknown); @@ -195,7 +196,7 @@ static enum ReadLogReturnCodeEnum Common_ReadEntry(char *Line,struct ReadLogStru Ip[IpLen]='\0'; Entry->HttpCode[HttpCodeLen]='\0'; Entry->Url[UrlLen]='\0'; - Entry->User[UserLen]='\0'; + User[UserLen]='\0'; return(RLRC_NoError); } diff --git a/readlog_extlog.c b/readlog_extlog.c index 9718008..4498497 100644 --- a/readlog_extlog.c +++ b/readlog_extlog.c @@ -480,6 +480,7 @@ 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; @@ -512,7 +513,7 @@ static enum ReadLogReturnCodeEnum ExtLog_ReadEntry(char *Line,struct ReadLogStru if (!Line) return(RLRC_Unknown); break; case EXTCOL_UserName: - Entry->User=Line; + Entry->User=User=Line; Line=ExtLog_GetString(Line,col,&UserEnd); if (!Line) return(RLRC_Unknown); break; @@ -588,7 +589,7 @@ static enum ReadLogReturnCodeEnum ExtLog_ReadEntry(char *Line,struct ReadLogStru } ExtLog_FixString(Ip,IpEnd); - ExtLog_FixString(Entry->User,UserEnd); + ExtLog_FixString(User,UserEnd); ExtLog_FixString(Entry->Url,UrlEnd); ExtLog_FixString(Entry->HttpCode,HttpCodeEnd); if (!Entry->Url) diff --git a/readlog_sarg.c b/readlog_sarg.c index 8d429ad..c60ff28 100644 --- a/readlog_sarg.c +++ b/readlog_sarg.c @@ -68,6 +68,7 @@ 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) { @@ -119,7 +120,7 @@ static enum ReadLogReturnCodeEnum Sarg_ReadEntry(char *Line,struct ReadLogStruct Entry->EntryTime.tm_isdst=-1; // the ID of the user - Entry->User=++Line; + Entry->User=User=++Line; for (UserLen=0 ; *Line && *Line!='\t' ; UserLen++) Line++; if (*Line!='\t' || UserLen==0) return(RLRC_Unknown); @@ -163,7 +164,7 @@ static enum ReadLogReturnCodeEnum Sarg_ReadEntry(char *Line,struct ReadLogStruct Ip[IpLen]='\0'; Entry->HttpCode[HttpCodeLen]='\0'; Entry->Url[UrlLen]='\0'; - Entry->User[UserLen]='\0'; + User[UserLen]='\0'; return(RLRC_NoError); } diff --git a/readlog_squid.c b/readlog_squid.c index 188cb56..529abc9 100644 --- a/readlog_squid.c +++ b/readlog_squid.c @@ -56,6 +56,7 @@ static enum ReadLogReturnCodeEnum Squid_ReadEntry(char *Line,struct ReadLogStruc int UserLen; struct tm *tt; char *Ip; + char *User; // get log time. Begin=Line; @@ -120,7 +121,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=++Line; + Entry->User=User=++Line; for (UserLen=0 ; *Line && *Line!=' ' ; UserLen++) Line++; if (*Line!=' ' || UserLen==0) return(RLRC_Unknown); @@ -137,7 +138,7 @@ static enum ReadLogReturnCodeEnum Squid_ReadEntry(char *Line,struct ReadLogStruc Entry->HttpCode[HttpCodeLen]='\0'; Entry->HttpMethod[HttpMethodLen]='\0'; Entry->Url[UrlLen]='\0'; - Entry->User[UserLen]='\0'; + User[UserLen]='\0'; return(RLRC_NoError); }