]> git.ipfire.org Git - thirdparty/sarg.git/commitdiff
Use a string to decide the content of the per_user_limit file
authorFrederic Marchal <fmarchal@users.sourceforge.net>
Thu, 6 Jun 2013 12:31:45 +0000 (14:31 +0200)
committerFrederic Marchal <fmarchal@users.sourceforge.net>
Thu, 6 Jun 2013 12:31:45 +0000 (14:31 +0200)
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.

getconf.c
html.c
include/defs.h
sarg.conf

index 4d2c3a3780a5a64621f030408f516dc7f3c5e9dc..c231f6399a150a4c56df270b0e8f5dce5931d097 100644 (file)
--- 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 ad5be738e80adcea141bdf66d228aa911fa98070..958634b8980621f335708f85e973feef99d41ff9 100644 (file)
--- 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);
index b0802ab87d0105dfde8091391c0025e4abfe6a3b..f7234838b26b88429010e2d9c3279bab57192bf0 100755 (executable)
@@ -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
index 65e23c3e9bdc6354d158d11addd2b75a0bdde987..b90733ce53dece19047d9f570668a665ae37fe8d 100644 (file)
--- a/sarg.conf
+++ b/sarg.conf
 #
 #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