From: Frederic Marchal Date: Sun, 1 Nov 2015 08:21:46 +0000 (+0100) Subject: Produce a user agent log if the information is in the extended log X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=468d879d6d1b51474ffc60799c34eae31c9c7b86;p=thirdparty%2Fsarg.git Produce a user agent log if the information is in the extended log The extended log can contain the user agent name. If it is available, we produce the user agent log. --- diff --git a/include/readlog.h b/include/readlog.h index a481362..27a21c3 100644 --- a/include/readlog.h +++ b/include/readlog.h @@ -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; }; /*! diff --git a/readlog.c b/readlog.c index d81032e..3805de0 100644 --- 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 || idata0){ @@ -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 ); diff --git a/readlog_extlog.c b/readlog_extlog.c index 33491d5..1b8d735 100644 --- a/readlog_extlog.c +++ b/readlog_extlog.c @@ -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); } diff --git a/topuser.c b/topuser.c index 762478f..06cca7d 100644 --- 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,"%s\n",_("Denied accesses")); if (is_authfail()) fprintf(fp_top3,"%s\n",_("Authentication Failures")); if(smartfilter) fprintf(fp_top3,"%s\n",_("SmartFilter")); - if (!FileList_IsEmpty(UserAgentLog) && useragent_count) fprintf(fp_top3,"%s\n",_("Useragent")); + if (useragent_count) fprintf(fp_top3,"%s\n",_("Useragent")); fputs("\n\n",fp_top3); }