]> git.ipfire.org Git - thirdparty/sarg.git/commitdiff
Produce a user agent log if the information is in the extended log
authorFrederic Marchal <fmarchal@users.sourceforge.net>
Sun, 1 Nov 2015 08:21:46 +0000 (09:21 +0100)
committerFrederic Marchal <fmarchal@users.sourceforge.net>
Sun, 1 Nov 2015 08:21:46 +0000 (09:21 +0100)
The extended log can contain the user agent name. If it is available, we
produce the user agent log.

include/readlog.h
readlog.c
readlog_extlog.c
topuser.c

index a481362292df1dce3eb1290fd7c7e7674214e05d..27a21c3eb10bbe88708f671d6776c1331e4553f9 100644 (file)
@@ -43,6 +43,8 @@ struct ReadLogStruct
        char *HttpCode;
        //! HTTP method or NULL if the information is not stored in the log.
        char *HttpMethod;
+       //! Useragent string or NULL if it isn't available
+       const char *UserAgent;
 };
 
 /*!
index d81032e79315a77a4bf787ede68741d38d19ff6d..3805de0cb35f4355509a154933aa6fe797108e72 100644 (file)
--- a/readlog.c
+++ b/readlog.c
@@ -332,6 +332,7 @@ static void ReadOneLogFile(struct ReadLogDataStruct *Filter,const char *arq)
        struct userfilestruct *ufile1;
        struct ReadLogStruct log_entry;
        struct LogLineStruct log_line;
+       FILE *UseragentLog=NULL;
 
        LogLine_Init(&log_line);
        LogLine_File(&log_line,arq);
@@ -714,6 +715,12 @@ static void ReadOneLogFile(struct ReadLogDataStruct *Filter,const char *arq)
                denied_write(&log_entry);
                authfail_write(&log_entry);
                if (download_flag) download_write(&log_entry,download_url);
+               if (log_entry.UserAgent)
+               {
+                       if (!UseragentLog)
+                               UseragentLog=UserAgent_Open();
+                       UserAgent_Write(UseragentLog,log_entry.Ip,log_entry.User,log_entry.UserAgent);
+               }
 
                if (log_line.current_format!=&ReadSargLog) {
                        if (period.start.tm_year==0 || idata<mindate || compare_date(&period.start,&log_entry.EntryTime)>0){
@@ -744,6 +751,7 @@ static void ReadOneLogFile(struct ReadLogDataStruct *Filter,const char *arq)
                debuga(__FILE__,__LINE__,_("Read error in \"%s\": %s\n"),arq,FileObject_GetLastCloseError());
                exit(EXIT_FAILURE);
        }
+       if (UseragentLog) fclose(UseragentLog);
        if (ShowReadStatistics) {
                if (ShowReadPercent)
                        printf(_("SARG: Records in file: %lu, reading: %3.2f%%\n"),recs2, (float) 100 );
index 33491d5b774631263824e1fe5f06f30aea051593..1b8d735ff63bd29e4094207c95427a79b965de57 100644 (file)
@@ -50,6 +50,7 @@ enum ext_col_id {
        EXTCOL_Path,
        EXTCOL_Query,
        EXTCOL_Status,
+       EXTCOL_UserAgent,
        EXTCOL_Last //last entry of the list !
 };
 
@@ -161,6 +162,8 @@ static bool ExtLog_Fields(const char *columns)
                        if (strncasecmp(columns,"cs-uri-query",len)==0) col_id=EXTCOL_Query;
                } else if (len==13) {
                        if (strncasecmp(columns,"cs-uri-scheme",len)==0) col_id=EXTCOL_Scheme;
+               } else if (len==14) {
+                       if (strncasecmp(columns,"cs(User-Agent)",len)==0) col_id=EXTCOL_UserAgent;
                }
                if (col_id!=EXTCOL_Last) {
                        ExtCols[col_id]=col;
@@ -227,23 +230,22 @@ Scan through the string of a column.
 static char *ExtLog_GetString(char *Line,int col,char **End)
 {
        bool quote;
-       bool dequote;
 
        //skip opening double quote
        quote=(*Line=='\"');
        if (quote) ++Line;
 
-       dequote=false;
        while (*Line) {
                if (quote) {
                        if (*Line=='\"') {
-                               if (Line[1]!='\"') {
-                                       if (End) *End=(dequote) ? NULL : Line;
-                                       Line++;//skip the closing quote
+                               if (Line[1]=='\"') {
+                                       Line++;//skip the first quote here, the second is skipped by the other Line++
+                               } else {
+                                       if (End) *End=Line;
+                                       Line++;//skip closing quote
                                        quote=false;
                                        break;
                                }
-                               dequote=true;
                        }
                } else {
                        if (*Line==ExtColSep[col]) {
@@ -367,12 +369,16 @@ static void ExtLog_FixString(char *string,char *end_ptr)
        char *dest;
 
        if (!string) return;//string not parsed
-       if (end_ptr) { //end is known and no quotes are in the string
+       if (*string!='\"' && end_ptr) { //no quotes to remove from the string
                *end_ptr='\0';
                return;
        }
-       // remove the quotes and end at the first unremoveable quote
+
+       // remove first quote
        dest=string;
+       if (string[1]!='\"') string++;
+
+       // remove the quotes and end at the first unremoveable quote
        while (*string)
        {
                if (*string=='\"') {
@@ -502,6 +508,7 @@ static enum ReadLogReturnCodeEnum ExtLog_ReadEntry(char *Line,struct ReadLogStru
        char *UrlPort=NULL,*UrlPortEnd;
        char *UrlPath=NULL,*UrlPathEnd;
        char *UrlQuery=NULL,*UrlQueryEnd;
+       char *UserAgent=NULL,*UserAgentEnd;
 
        // is it a directive
        if (*Line=='#') {
@@ -581,6 +588,11 @@ static enum ReadLogReturnCodeEnum ExtLog_ReadEntry(char *Line,struct ReadLogStru
                                Line=ExtLog_GetString(Line,col,&HttpCodeEnd);
                                if (!Line) return(RLRC_Unknown);
                                break;
+                       case EXTCOL_UserAgent:
+                               UserAgent=Line;
+                               Line=ExtLog_GetString(Line,col,&UserAgentEnd);
+                               if (!Line) return(RLRC_Unknown);
+                               break;
                        case EXTCOL_Last://ignored column
                                Line=ExtLog_GetString(Line,col,NULL);
                                if (!Line) return(RLRC_Unknown);
@@ -614,6 +626,8 @@ static enum ReadLogReturnCodeEnum ExtLog_ReadEntry(char *Line,struct ReadLogStru
                ExtLog_FixString(UrlQuery,UrlQueryEnd);
                Entry->Url=ExtLog_ConcatUrl(UrlScheme,UrlHost,UrlPort,UrlPath,UrlQuery);
        }
+       ExtLog_FixString(UserAgent,UserAgentEnd);
+       Entry->UserAgent=ExtLog_FixEmptyString(UserAgent);
 
        return(RLRC_NoError);
 }
index 762478f04f79458fc40d246d77d955fd02c3f1a3..06cca7dcdfc091c7c274f3d6179f48011199dafe 100644 (file)
--- a/topuser.c
+++ b/topuser.c
@@ -46,7 +46,6 @@ struct SortInfoStruct
 
 extern struct globalstatstruct globstat;
 extern bool smartfilter;
-extern FileListObject UserAgentLog;
 
 /*!
 Save the total number of users. The number is written in sarg-users and set
@@ -139,7 +138,7 @@ static void TopUser_HtmlReport(const char *ListFile,struct TopUserStatistics *St
                if (is_denied()) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"denied.html\">%s</a></td></tr>\n",_("Denied accesses"));
                if (is_authfail()) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"authfail.html\">%s</a></td></tr>\n",_("Authentication Failures"));
                if(smartfilter) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"smartfilter.html\">%s</a></td></tr>\n",_("SmartFilter"));
-               if (!FileList_IsEmpty(UserAgentLog) && useragent_count) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"useragent.html\">%s</a></td></tr>\n",_("Useragent"));
+               if (useragent_count) fprintf(fp_top3,"<tr><td class=\"link\" colspan=\"0\"><a href=\"useragent.html\">%s</a></td></tr>\n",_("Useragent"));
                fputs("<tr><td></td></tr>\n</table></div>\n",fp_top3);
        }