From: Frederic Marchal Date: Thu, 6 Jun 2013 12:31:45 +0000 (+0200) Subject: Use a string to decide the content of the per_user_limit file X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=3877d630596c31f52f006eb40f273b2f0fc2a811;p=thirdparty%2Fsarg.git Use a string to decide the content of the per_user_limit file Instead of a numerical value 0/1, the strings "ip" or "id" are used in the per_user_limit option to decide if the file contains the IP address or the user's ID. --- diff --git a/getconf.c b/getconf.c index 4d2c3a3..c231f63 100644 --- a/getconf.c +++ b/getconf.c @@ -482,7 +482,18 @@ static int getparam_userlimit(const char *param,char *buf) char *file_begin,*file_end; int limit; int digit; - int userid; + char *str; + int i; + enum PerUserOutputEnum output; + const struct + { + const char *name; + enum PerUserOutputEnum value; + } output_types[]= + { + {"id",PUOE_UserId}, + {"ip",PUOE_UserIp}, + }; plen=strlen(param); if (strncmp(buf,param,plen) != 0) return(0); @@ -513,14 +524,21 @@ static int getparam_userlimit(const char *param,char *buf) } } while (*buf && *buf!=' '); - userid=0; + output=PUOE_UserId; if (*buf==' ') { while (*buf && (unsigned char)*buf<=' ') buf++; - if (!isdigit(*buf)) { - debuga(_("Invalid user ID/IP output type flag in per_user_limit\n")); + str=buf; + while (*buf && (unsigned char)*buf>' ') buf++; + *buf='\0'; + for (i=sizeof(output_types)/sizeof(output_types[0])-1 ; i>=0 ; i--) + if (strcasecmp(output_types[i].name,str)==0) { + output=output_types[i].value; + break; + } + if (i<0) { + debuga(_("Invalid output type in per_user_limit\n")); exit(EXIT_FAILURE); } - userid=*buf-'0'; } if (PerUserLimitsNumber>=MAX_USER_LIMITS) { @@ -530,7 +548,7 @@ static int getparam_userlimit(const char *param,char *buf) *file_end='\0'; safe_strcpy(PerUserLimits[PerUserLimitsNumber].File,file_begin,sizeof(PerUserLimits[PerUserLimitsNumber].File)); PerUserLimits[PerUserLimitsNumber].Limit=limit; - PerUserLimits[PerUserLimitsNumber].UserId=userid; + PerUserLimits[PerUserLimitsNumber].Output=output; PerUserLimitsNumber++; return(1); diff --git a/html.c b/html.c index ad5be73..958634b 100644 --- a/html.c +++ b/html.c @@ -578,10 +578,15 @@ void htmlrel(void) debuga(_("(html10) Cannot open per user limit file %s: %s\n"),PerUserLimits[i].File,strerror(errno)); exit(EXIT_FAILURE); } - if (PerUserLimits[i].UserId) - fprintf(fp_usr,"%s\n",uinfo->label); - else - fprintf(fp_usr,"%s\n",uinfo->ip); + switch (PerUserLimits[i].Output) + { + case PUOE_UserId: + fprintf(fp_usr,"%s\n",uinfo->label); + break; + case PUOE_UserIp: + fprintf(fp_usr,"%s\n",uinfo->ip); + break; + } if (fclose(fp_usr)==EOF) { debuga(_("Write error in per user limit file %s: %s\n"),PerUserLimits[i].File,strerror(errno)); exit(EXIT_FAILURE); diff --git a/include/defs.h b/include/defs.h index b0802ab..f723483 100755 --- a/include/defs.h +++ b/include/defs.h @@ -127,6 +127,15 @@ enum PerUserFileCreationEnum PUFC_AsRequired }; +/*! +\brief What to write into the per_user_limit file. +*/ +enum PerUserOutputEnum +{ + PUOE_UserId, + PUOE_UserIp +}; + /*! \brief How to log every user crossing the download limit. */ @@ -136,8 +145,8 @@ struct PerUserLimitStruct char File[255]; //! Limit above which the user is reported. int Limit; - //! \c True to report the user's ID or \c false to report the user's IP. - bool UserId; + //! What to write into the file. + enum PerUserOutputEnum Output; }; // auth.c diff --git a/sarg.conf b/sarg.conf index 65e23c3..b90733c 100644 --- a/sarg.conf +++ b/sarg.conf @@ -224,12 +224,17 @@ # #date_format u -# TAG: per_user_limit file MB flag -# Saves userid (if flag is 1) or userip (if flag is 0) in file if download exceed n MB. -# This option allow you to disable user access if user exceed a download limit. +# TAG: per_user_limit file MB ip/id +# Write the user's ID (if last flag is 'id') or the user's IP address (if last flag is 'ip') +# in file if download exceed n MB. +# This option allows you to disable user access if users exceed a download limit. # The option may be repeated up to 16 times to generate several files with # different content type or limit. # +# Examples: +# per_user_limit userlimit_1G.txt 1000 ip +# per_user_limit /var/log/sarg/userlimit_500M.log 500 id +# #per_user_limit none # TAG: per_user_limit_file_create always/as_required