]> git.ipfire.org Git - thirdparty/sarg.git/commitdiff
Perform the user's ID processing in one function
authorFrederic Marchal <fmarchal@users.sourceforge.net>
Sun, 26 Jul 2015 08:15:31 +0000 (10:15 +0200)
committerFrederic Marchal <fmarchal@users.sourceforge.net>
Sun, 26 Jul 2015 08:15:31 +0000 (10:15 +0200)
There are quite a lot of options to drive the user's ID or IP address
processing, replace the ID, include or exclude the entry from the report
and so on.

That processing is only applied in full to the access.log. The redirector
and dansguardian logs are much more primitive in comparison.

By moving the processing to one function, I intent to use it everywhere.

include/defs.h
readlog.c
userinfo.c

index 73e20d959d837eac7e5167286b8a670b4ec5ff90..19743a31a10481e6b247a2a27f2c0df85ff4e9ee 100644 (file)
@@ -58,6 +58,21 @@ struct generalitemstruct
    long long oucache;
 };
 
+/*!
+ * \brief Error codes returned by process_user.
+ */
+enum UserProcessError
+{
+       USERERR_NoError,
+       USERERR_NameTooLong,
+       USERERR_Excluded,
+       USERERR_InvalidChar,
+       USERERR_EmptyUser,
+       USERERR_SysUser,
+       USERERR_Ignored,
+       USERERR_Untracked,
+};
+
 /*! \brief What is known about a user.
 */
 struct userinfostruct
@@ -336,7 +351,7 @@ struct userinfostruct *userinfo_advancescan(userscan uscan);
 void userinfo_clearflag(void);
 void read_useralias(const char *Filename);
 void free_useralias(void);
-const char *process_user(const char *user);
+enum UserProcessError process_user(const char **UserPtr,const char *IpAddress,bool *IsIp);
 
 // usertab.c
 void init_usertab(const char *UserTabFile);
index 34253b391b377140bb5ca6fd8a134da0a1c01024..2f70acbded194644f34995ba0b404b75d7253389 100644 (file)
--- a/readlog.c
+++ b/readlog.c
@@ -94,7 +94,6 @@ char StripUserSuffix[MAX_USER_LEN]="";
 //! Length of the suffix to strip from the user name.
 int StripSuffixLen=0;
 
-extern char *userfile;
 extern FileListObject AccessLog;
 
 extern const struct ReadLogProcessStruct ReadSquidLog;
@@ -308,13 +307,10 @@ static void ReadOneLogFile(struct ReadLogDataStruct *Filter,const char *arq)
        char *str;
        char hora[30];
        char dia[128]="";
-       char wuser[MAXLEN];
        char tmp3[MAXLEN]="";
        char download_url[MAXLEN];
        char smartfilter[MAXLEN];
        const char *url;
-       const char *user;
-       char UserBuffer[MAX_USER_LEN];
        int OutputNonZero = REPORT_EVERY_X_LINES ;
        int idata=0;
        int x;
@@ -327,6 +323,7 @@ static void ReadOneLogFile(struct ReadLogDataStruct *Filter,const char *arq)
        bool download_flag=false;
        bool id_is_ip;
        enum ReadLogReturnCodeEnum log_entry_status;
+       enum UserProcessError PUser;
        struct stat logstat;
        struct getwordstruct gwarea;
        struct userfilestruct *prev_ufile;
@@ -529,35 +526,35 @@ static void ReadOneLogFile(struct ReadLogDataStruct *Filter,const char *arq)
                        continue;
                }
 
-               if (StripSuffixLen>0)
+               PUser=process_user(&log_entry.User,log_entry.Ip,&id_is_ip);
+               switch (PUser)
                {
-                       x=strlen(log_entry.User);
-                       if (x>StripSuffixLen && strcasecmp(log_entry.User+(x-StripSuffixLen),StripUserSuffix)==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,x-StripSuffixLen+1);
-                               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);
-                       excluded_count[ER_UserNameTooLong]++;
-                       totregsx++;
-                       continue;
-               }
-
-               // include_users
-               if(IncludeUsers[0] != '\0') {
-                       snprintf(val1,sizeof(val1),":%s:",log_entry.User);
-                       if((str=(char *) strstr(IncludeUsers,val1)) == (char *) NULL ) {
+                       case USERERR_NoError:
+                               break;
+                       case USERERR_NameTooLong:
+                               if (debugz>=LogLevel_Process) debuga(__FILE__,__LINE__,_("User ID too long: %s\n"),log_entry.User);
+                               excluded_count[ER_UserNameTooLong]++;
+                               totregsx++;
+                               continue;
+                       case USERERR_Excluded:
                                excluded_count[ER_User]++;
                                continue;
-                       }
+                       case USERERR_InvalidChar:
+                               excluded_count[ER_InvalidUserChar]++;
+                               continue;
+                       case USERERR_EmptyUser:
+                               excluded_count[ER_NoUser]++;
+                               continue;
+                       case USERERR_SysUser:
+                               excluded_count[ER_SysUser]++;
+                               continue;
+                       case USERERR_Ignored:
+                               excluded_count[ER_IgnoredUser]++;
+                               totregsx++;
+                               continue;
+                       case USERERR_Untracked:
+                               excluded_count[ER_UntrackedUser]++;
+                               continue;
                }
 
                if(vercode(log_entry.HttpCode)) {
@@ -567,11 +564,6 @@ static void ReadOneLogFile(struct ReadLogDataStruct *Filter,const char *arq)
                        continue;
                }
 
-               if(testvaliduserchar(log_entry.User)) {
-                       excluded_count[ER_InvalidUserChar]++;
-                       continue;
-               }
-
                // replace any tab by a single space
                for (str=log_entry.Url ; *str ; str++)
                        if (*str=='\t') *str=' ';
@@ -626,67 +618,6 @@ static void ReadOneLogFile(struct ReadLogDataStruct *Filter,const char *arq)
                        }
                }
 
-               if(UserIp) {
-                       log_entry.User=log_entry.Ip;
-                       id_is_ip=true;
-               } else {
-                       log_entry.User=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(RecordsWithoutUser == RECORDWITHOUTUSER_IP) {
-                                       log_entry.User=log_entry.Ip;
-                                       id_is_ip=true;
-                               }
-                               if(RecordsWithoutUser == RECORDWITHOUTUSER_IGNORE) {
-                                       excluded_count[ER_NoUser]++;
-                                       continue;
-                               }
-                               if(RecordsWithoutUser == RECORDWITHOUTUSER_EVERYBODY)
-                                       log_entry.User="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(us[0] != '\0'){
-                       if(strcmp(log_entry.User,us)!=0) {
-                               excluded_count[ER_UntrackedUser]++;
-                               continue;
-                       }
-               }
-
-               if(Filter->SysUsers) {
-                       snprintf(wuser,sizeof(wuser),":%s:",log_entry.User);
-                       if(strstr(userfile, wuser) == 0) {
-                               excluded_count[ER_SysUser]++;
-                               continue;
-                       }
-               }
-
-               if(Filter->UserFilter) {
-                       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(log_entry.User);
-               if (log_entry.User!=user)
-               {
-                       log_entry.User=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]==':'))) {
-                       excluded_count[ER_NoUser]++;
-                       continue;
-               }
-
                if (log_entry.DataSize<0) log_entry.DataSize=0;
 
                if (log_entry.ElapsedTime<0) log_entry.ElapsedTime=0;
index 1a48778f212f50ba93a65fca0cfedb4b2252aadc..f7e5e0badd1ff3f48a3b535a08737e4bfc163585 100644 (file)
@@ -65,6 +65,11 @@ static StringBufferObject UserStrings=NULL;
 //! User aliases.
 static AliasObject UserAliases=NULL;
 
+extern struct ReadLogDataStruct ReadFilter;
+extern char StripUserSuffix[MAX_USER_LEN];
+extern int StripSuffixLen;
+extern char *userfile;
+
 struct userinfostruct *userinfo_create(const char *userid,const char *ip)
 {
        struct usergroupstruct *group, *last;
@@ -365,9 +370,98 @@ void free_useralias(void)
 Replace the user's name or ID by an alias if one is defined.
 
 \param user The user's name or ID as extracted from the report.
+
+\retval USERERR_NoError No error.
+\retval USERERR_NameTooLong User name too long.
 */
-const char *process_user(const char *user)
+enum UserProcessError process_user(const char **UserPtr,const char *IpAddress,bool *IsIp)
 {
-       user=Alias_Replace(UserAliases,user);
-       return(user);
+       const char *user=*UserPtr;
+       static char UserBuffer[MAX_USER_LEN];
+       const char *auser;
+
+       if (UserIp) {
+               user=IpAddress;
+               *IsIp=true;
+       } else {
+               *IsIp=false;
+
+               if (StripSuffixLen>0)
+               {
+                       int x=strlen(user);
+                       if (x>StripSuffixLen && strcasecmp(user+(x-StripSuffixLen),StripUserSuffix)==0)
+                       {
+                               if (x-StripSuffixLen>=sizeof(UserBuffer))
+                                       return(USERERR_NameTooLong);
+                               safe_strcpy(UserBuffer,user,x-StripSuffixLen+1);
+                               user=UserBuffer;
+                       }
+               }
+               if (strlen(user)>MAX_USER_LEN)
+                       return(USERERR_NameTooLong);
+
+               // include_users
+               if (IncludeUsers[0] != '\0') {
+                       char wuser[MAX_USER_LEN+2]=":";
+                       char *str;
+
+                       strcat(wuser,user);
+                       strcat(wuser,":");
+                       str=strstr(IncludeUsers,wuser);
+                       if (!str)
+                               return(USERERR_Excluded);
+               }
+
+               if (testvaliduserchar(user))
+                       return(USERERR_InvalidChar);
+
+               if ((user[0]=='\0') || (user[1]=='\0' && (user[0]=='-' || user[0]==' '))) {
+                       if(RecordsWithoutUser == RECORDWITHOUTUSER_IP) {
+                               user=IpAddress;
+                               *IsIp=true;
+                       }
+                       if (RecordsWithoutUser == RECORDWITHOUTUSER_IGNORE)
+                               return(USERERR_EmptyUser);
+                       if (RecordsWithoutUser == RECORDWITHOUTUSER_EVERYBODY)
+                               user="everybody";
+               } else {
+                       if (NtlmUserFormat == NTLMUSERFORMAT_USER) {
+                               const char *str;
+                               if ((str=strchr(user,'+'))!=NULL || (str=strchr(user,'\\'))!=NULL || (str=strchr(user,'_'))!=NULL) {
+                                       user=str+1;
+                               }
+                       }
+               }
+       }
+
+       if (us[0]!='\0' && strcmp(user,us)!=0)
+               return(USERERR_Untracked);
+
+       if (ReadFilter.SysUsers) {
+               char wuser[MAX_USER_LEN+2]=":";
+
+               strcat(wuser,user);
+               strcat(wuser,":");
+               if (strstr(userfile, wuser) == 0)
+                       return(USERERR_SysUser);
+       }
+
+       if (ReadFilter.UserFilter) {
+               if (!vuexclude(user)) {
+                       if (debugz>=LogLevel_Process) debuga(__FILE__,__LINE__,_("Excluded user: %s\n"),user);
+                       return(USERERR_Ignored);
+               }
+       }
+
+       auser=Alias_Replace(UserAliases,user);
+       if (auser!=user) {
+               user=auser;
+               *IsIp=false;
+       }
+
+       if (user[0]=='\0' || (user[1]=='\0' && (user[0]=='-' || user[0]==' ' || user[0]==':')))
+               return(USERERR_EmptyUser);
+
+       *UserPtr=user;
+       return(USERERR_NoError);
 }