From: Frédéric Marchal Date: Wed, 23 Nov 2011 19:26:47 +0000 (+0000) Subject: Fix some errors introduced by the previous patch for windows X-Git-Tag: v2.3.2^0 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d7cb0aec2d8149e981cc045ac9b82cc56ba48baf;p=thirdparty%2Fsarg.git Fix some errors introduced by the previous patch for windows The changes made to make the realtime report compatible with windows produced some errors on linux. This patch fixes those problems. This fix also correctly sorts the entries by date and time whatever the requested output date format is. --- diff --git a/CMakeLists.txt b/CMakeLists.txt index ac935a5..8869213 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ CMAKE_MINIMUM_REQUIRED (VERSION 2.6) PROJECT(sarg C) SET(sarg_VERSION 2) -SET(sarg_REVISION "3.2-pre2") +SET(sarg_REVISION "3.2") SET(sarg_BUILD "") SET(sarg_BUILDDATE "Nov-23-2011") diff --git a/configure.in b/configure.in index 9b9a620..854a94c 100644 --- a/configure.in +++ b/configure.in @@ -1,5 +1,5 @@ dnl Process this file with autoconf to produce a configure script. -AC_INIT([sarg],[2.3.2-pre2]) +AC_INIT([sarg],[2.3.2]) AC_CONFIG_SRCDIR([log.c]) AC_CONFIG_AUX_DIR(cfgaux) @@ -75,7 +75,7 @@ AC_CHECK_HEADERS(stdio.h stdlib.h string.h strings.h sys/time.h time.h unistd.h dirent.h sys/types.h sys/socket.h netdb.h arpa/inet.h netinet/in.h sys/stat.h \ ctype.h gd.h gdfontl.h gdfontt.h gdfonts.h gdfontmb.h gdfontg.h iconv.h \ errno.h sys/resource.h sys/wait.h stdarg.h inttypes.h limits.h locale.h \ - execinfo.h ldap.h math.h libintl.h libgen.h stdbool.h getopt.h) + execinfo.h ldap.h math.h libintl.h libgen.h stdbool.h getopt.h fcntl.h) if test $ac_cv_header_getopt_h = "no" ; then AC_MSG_ERROR("getopt.h is required to compile sarg") @@ -132,6 +132,7 @@ AC_CHECK_FUNCS(symlink) AC_CHECK_FUNCS(lstat) AC_CHECK_FUNCS(getnameinfo) AC_CHECK_FUNCS(getaddrinfo) +AC_CHECK_FUNCS(mkstemp) dnl check for structure members AC_CHECK_MEMBER([struct sockaddr_storage.ss_len],[AC_DEFINE([HAVE_SOCKADDR_SA_LEN],1,[ss_len in sockaddr_storage])]) diff --git a/realtime.c b/realtime.c index 664480c..7652186 100755 --- a/realtime.c +++ b/realtime.c @@ -44,18 +44,28 @@ static void getlog(void) char template2[255]="/var/tmp/sargtpl2.XXXXXX"; char cmd[512]; char *buf; - int fd1; + int fd1,fd2; int cstatus; longline line; init_usertab(UserTabFile); #ifdef HAVE_MKSTEMP + fd2 = mkstemp(template2); + if (fd2 == -1) { + debuga(_("Cannot create a temporary file name to produce the report: %s\n"),strerror(errno)); + exit(EXIT_FAILURE); + } fd1 = mkstemp(template1); #else - fd1=open(mktemp(template1),O_RDWR); + buf = mktemp(template2); + if (buf[0]=='\0') { + debuga(_("Cannot create a temporary file name to produce the report: %s\n"),strerror(errno)); + exit(EXIT_FAILURE); + } + fd2 = -1; + fd1 = open(mktemp(template1),O_RDWR); #endif - mktemp(template2); if((fd1 == -1 ) || ((tmp = fdopen (fd1, "w+" )) == NULL) ) { /* failure, bail out */ debuga(_("(realtime) mkstemp error - %s\n"),strerror(errno)); @@ -83,7 +93,8 @@ static void getlog(void) fclose(tmp); longline_destroy(&line); - if (snprintf(cmd,sizeof(cmd),"sort -t \"\t\" -r -k 1,1 -k 2,2 -o \"%s\" \"%s\"",template2,template1)>=sizeof(cmd)) { + if (fd2!=-1) close(fd2);//not safe at all but good enough for now. + if (snprintf(cmd,sizeof(cmd),"sort -t \"\t\" -r -n -k 1,1 -o \"%s\" \"%s\"",template2,template1)>=sizeof(cmd)) { debuga(_("Sort command too long when sorting file \"%s\" to \"%s\"\n"),template1,template2); exit(EXIT_FAILURE); } @@ -102,10 +113,7 @@ static void getlog(void) static int getdata(char *rec, FILE *ftmp) { - time_t tt; - struct tm *t; - char dat[128]; - char tbuf[128]; + int dat; char typ[128]; char warea[MAXLEN]; char user[MAX_USER_LEN]; @@ -114,10 +122,14 @@ static int getdata(char *rec, FILE *ftmp) struct getwordstruct gwarea; getword_start(&gwarea,rec); - if (getword(dat,sizeof(dat),&gwarea,' ')<0) { + if (getword_atoi(&dat,&gwarea,'.')<0) { debuga(_("The time stamp at column 1 is too long\n")); return(-1); } + if (getword_skip(10,&gwarea,' ')<0) { + debuga(_("The time stamp decimal part at column 1 is too long\n")); + return(-1); + } if (getword(warea,sizeof(warea),&gwarea,' ')<0) { debuga(_("The connection duration at column 2 is too long\n")); return(-1); @@ -177,22 +189,17 @@ static int getdata(char *rec, FILE *ftmp) if(strncmp(user,"-",1) == 0 && RealtimeUnauthRec==REALTIME_UNAUTH_REC_IGNORE) return(0); - tt=atoi(dat); - t=localtime(&tt); - if(strncmp(DateFormat,"u",1) == 0) - strftime(tbuf, sizeof(tbuf), "%Y-%m-%d\t%H:%M", t); - else if(strncmp(DateFormat,"e",1) == 0) - strftime(tbuf, sizeof(tbuf), "%d-%m-%Y\t%H:%M", t); - - fprintf(ftmp,"%s\t%s\t%s\t%s\t%s\n",tbuf,ip,user,url,typ); + fprintf(ftmp,"%d\t%s\t%s\t%s\t%s\n",dat,ip,user,url,typ); return(0); } static void datashow(const char *tmp) { FILE *fin; - char dat[128]; - char tim[128]; + time_t tt; + struct tm *t; + char tbuf[128]; + int dat; char *buf; char *url; char *ourl=NULL; @@ -221,29 +228,25 @@ static void datashow(const char *tmp) while((buf=longline_read(fin,line))!=NULL) { fixendofline(buf); getword_start(&gwarea,buf); - if (getword(dat,sizeof(dat),&gwarea,'\t')<0) { - debuga(_("Maybe you have a broken record or garbage in your %s file\n"),tmp); - exit(EXIT_FAILURE); - } - if (getword(tim,sizeof(tim),&gwarea,'\t')<0) { - debuga(_("Maybe you have a broken record or garbage in your %s file\n"),tmp); + if (getword_atoi(&dat,&gwarea,'\t')<0) { + debuga(_("Invalid time column in file %s\n"),tmp); exit(EXIT_FAILURE); } if (getword(ip,sizeof(ip),&gwarea,'\t')<0) { - debuga(_("Maybe you have a broken record or garbage in your %s file\n"),tmp); + debuga(_("Invalid IP address in file %s\n"),tmp); exit(EXIT_FAILURE); } if (getword(user,sizeof(user),&gwarea,'\t')<0) { - debuga(_("Maybe you have a broken record or garbage in your %s file\n"),tmp); + debuga(_("Invalid user name in file %s\n"),tmp); exit(EXIT_FAILURE); } - if(strlen(dat) < 3 || strlen(user) < 1) continue; + if (strlen(user) < 1) continue; if (getword_ptr(buf,&url,&gwarea,'\t')<0) { - debuga(_("Maybe you have a broken url in your %s file\n"),tmp); + debuga(_("Invalid URL in file %s\n"),tmp); exit(EXIT_FAILURE); } if (getword(typ,sizeof(typ),&gwarea,'\t')<0) { - debuga(_("Maybe you have a broken record or garbage in your %s file\n"),tmp); + debuga(_("Invalid access type in file %s\n"),tmp); exit(EXIT_FAILURE); } if(strstr(RealtimeTypes,typ) == 0) @@ -259,7 +262,14 @@ static void datashow(const char *tmp) ip2name(u2,sizeof(u2)); user_find(name, sizeof(name), u2); - printf("%s %s%s%s%s%s\n",dat,tim,ip,name,typ,url,url); + tt=(time_t)dat; + t=localtime(&tt); + if(DateFormat[0]=='u') + strftime(tbuf, sizeof(tbuf), "%Y-%m-%d %H:%M", t); + else if(DateFormat[0]=='e') + strftime(tbuf, sizeof(tbuf), "%d-%m-%Y %H:%M", t); + + printf("%s%s%s%s%s\n",tbuf,ip,name,typ,url,url); strcpy(ouser,user); url_len=strlen(url);