]> git.ipfire.org Git - thirdparty/sarg.git/commitdiff
Check the return code of every opendir.
authorFrédéric Marchal <fmarchal@users.sourceforge.net>
Sat, 20 Mar 2010 18:43:55 +0000 (18:43 +0000)
committerFrédéric Marchal <fmarchal@users.sourceforge.net>
Sat, 20 Mar 2010 18:43:55 +0000 (18:43 +0000)
Secure the writing of the sarg-period file.

datafile.c
html.c
index.c
indexonly.c
lastlog.c
report.c
sort.c
util.c

index 5756204c68d50ea1b63280cc004c4ff663ffde58..8da50f61ed557ca0976cb187c0243bc9fbd8b8bd 100644 (file)
@@ -74,7 +74,10 @@ void data_file(char *tmp)
    oldurl=NULL;
    ourl_size=0;
 
-   dirp = opendir(tmp);
+   if ((dirp = opendir(tmp)) == NULL) {
+      debuga(_("Failed to open directory %s - %s\n"),tmp,strerror(errno));
+      exit(EXIT_FAILURE);
+   }
    while ( (direntp = readdir( dirp )) != NULL ) {
       dlen=strlen(direntp->d_name)-(sizeof(logext)-1);
       if (dlen<=0) continue;
diff --git a/html.c b/html.c
index 1edc49ac6f79abf8c3645e377e9e88d247c562a1..223b2a07632539a5985fb9d92fbf4bddc1ce588d 100644 (file)
--- a/html.c
+++ b/html.c
@@ -119,7 +119,10 @@ void htmlrel(void)
    ntotuser=my_atoll(totuser);
    if (ntotuser<=0) ntotuser=1;
 
-   dirp = opendir(tmp);
+   if ((dirp = opendir(tmp)) == NULL) {
+      debuga(_("Failed to open directory %s - %s\n"),tmp,strerror(errno));
+      exit(EXIT_FAILURE);
+   }
    while ( (direntp = readdir( dirp )) != NULL ) {
       dlen=strlen(direntp->d_name)-(sizeof(txtext)-1);
       if (dlen<0) continue;
diff --git a/index.c b/index.c
index 092a7c6b3e7eb7e72521c1a41bc0d4a5df6f9e73..a39722c1475dacc14f81ea7bbe178c5f7cd5c601 100644 (file)
--- a/index.c
+++ b/index.c
@@ -49,7 +49,10 @@ void make_index(void)
    if(debug) debuga(_("Making index.html\n"));
 
    // convert any old report hierarchy
-   dirp = opendir(outdir);
+   if ((dirp = opendir(outdir)) == NULL) {
+      debuga(_("Failed to open directory %s - %s\n"),outdir,strerror(errno));
+      exit(EXIT_FAILURE);
+   }
    while ((direntp = readdir( dirp )) != NULL) {
       if(isdigit(direntp->d_name[0]) && isdigit(direntp->d_name[1])) {
          if(IndexTree == INDEX_TREE_DATE)
@@ -100,7 +103,10 @@ static void make_date_index(void)
    sprintf(yearindex,"%sindex.html",outdir);
 
    nyears=0;
-   dirp = opendir(outdir);
+   if ((dirp = opendir(outdir)) == NULL) {
+      debuga(_("Failed to open directory %s - %s\n"),outdir,strerror(errno));
+      exit(EXIT_FAILURE);
+   }
    while ((direntp = readdir( dirp )) != NULL) {
       if(strlen(direntp->d_name) > 4 || !isdigit(direntp->d_name[0]) || !isdigit(direntp->d_name[1]) ||
          !isdigit(direntp->d_name[2]) || !isdigit(direntp->d_name[3])) continue;
@@ -146,7 +152,10 @@ static void make_date_index(void)
       sprintf(yeardir,"%s%s",outdir,yearnum);
       // Year dir
       nmonths=0;
-      dirp2 = opendir(yeardir);
+      if ((dirp2 = opendir(yeardir)) == NULL) {
+         debuga(_("Failed to open directory %s - %s\n"),yeardir,strerror(errno));
+         exit(EXIT_FAILURE);
+      }
       while ((direntp2 = readdir( dirp2 )) != NULL) {
          if(!isdigit(direntp2->d_name[0]) || !isdigit(direntp2->d_name[1])) continue;
          i=-1;
@@ -204,7 +213,10 @@ static void make_date_index(void)
          sprintf(monthdir,"%s/%s",yeardir,monthnum);
          // month dir
          ndays=0;
-         dirp3 = opendir(monthdir);
+         if ((dirp3 = opendir(monthdir)) == NULL) {
+            debuga(_("Failed to open directory %s - %s\n"),monthdir,strerror(errno));
+            exit(EXIT_FAILURE);
+         }
          while ((direntp3 = readdir( dirp3 )) != NULL) {
             if(!isdigit(direntp3->d_name[0]) && !isdigit(direntp3->d_name[1])) continue;
             i=-1;
@@ -300,7 +312,10 @@ static void make_file_index(void)
 
    order=(strcmp(IndexSortOrder,"A") == 0) ? 1 : -1;
 
-   dirp = opendir(outdir);
+   if ((dirp = opendir(outdir)) == NULL) {
+      debuga(_("Failed to open directory %s - %s\n"),outdir,strerror(errno));
+      exit(EXIT_FAILURE);
+   }
 
    nsort=0;
    nallocated=0;
@@ -547,7 +562,7 @@ static void date_index_to_file_index(const char *entry)
    next=-1;
    if (sscanf(entry,"%d%n",&y1,&next)!=1 || next<0 || entry[next]) return;
 
-   val1len=sprintf(val1,"%s%s",outdir,entry);
+   val1len=snprintf(val1,sizeof(val1),"%s%s",outdir,entry);
    dirp2 = opendir(val1);
    if (!dirp2) return;
    while ((direntp2 = readdir( dirp2 )) != NULL) {
index ec93f9cbe4c07db2fc540065edd657b207b068bf..0c538a1a72a221b9668747653a4ef2ae83d899f5 100644 (file)
@@ -33,14 +33,22 @@ void index_only(const char *dirname,int debug)
    DIR *dirp;
    struct dirent *direntp;
    char remove[MAXLEN];
-  
-   dirp = opendir(dirname);
+
+   if ((dirp = opendir(dirname)) == NULL) {
+      debuga(_("Failed to open directory %s - %s\n"),dirname,strerror(errno));
+      exit(EXIT_FAILURE);
+   }
    while ( (direntp = readdir( dirp )) != NULL ){
       if(strcmp(direntp->d_name,".") == 0 || strcmp(direntp->d_name,"..") == 0 || strcmp(direntp->d_name, "index.html") == 0)
          continue;
-       
-      sprintf(remove,"%s/%s",dirname,direntp->d_name);
-      unlink(remove);
+
+      if (snprintf(remove,sizeof(remove),"%s/%s",dirname,direntp->d_name)>=sizeof(remove)) {
+         debuga(_("Name of the file to remove is too long: %s/%s\n"),dirname,direntp->d_name);
+         continue;
+      }
+      if (unlink(remove) == -1) {
+         debuga(_("Failed to remove the file %s\n"),remove);
+      }
    }
 
    (void)closedir( dirp );
index 87dcedd834127605cc9bcd23ed43826e60f082e4..0a4469905a64f5e0e5d40b540824f94b38ccfb85 100644 (file)
--- a/lastlog.c
+++ b/lastlog.c
@@ -47,19 +47,25 @@ void mklastlog(const char *outdir)
    if(LastLog <= 0)
       return;
 
-   sprintf(temp,"%slastlog1",outdir);
+   snprintf(temp,sizeof(temp),"%slastlog1",outdir);
    if((fp_ou=fopen(temp,"w"))==NULL) {
      debuga(_("(lastlog) Cannot open temporary file %s\n"),temp);
      exit(EXIT_FAILURE);
    }
 
-   dirp = opendir(outdir);
+   if ((dirp = opendir(outdir)) == NULL) {
+      debuga(_("Failed to open directory %s - %s\n"),outdir,strerror(errno));
+      exit(EXIT_FAILURE);
+   }
    while ((direntp = readdir( dirp )) != NULL ){
       if(strchr(direntp->d_name,'-') == 0)
          continue;
 
-      sprintf(warea,"%s%s",outdir,direntp->d_name);
-      stat(warea,&statb);
+      snprintf(warea,sizeof(warea),"%s%s",outdir,direntp->d_name);
+      if (stat(warea,&statb) == -1) {
+         debuga(_("Failed to get the creation time of %s\n"),warea);
+         continue;
+      }
       t=statb.st_ctime;
       local = localtime(&t);
       strftime(ftime, sizeof(ftime), "%Y%m%d%H%M%S", local);
@@ -81,7 +87,7 @@ void mklastlog(const char *outdir)
    unlink(temp);
 
    if(ftot<=LastLog) {
-      sprintf(temp,"%slastlog",outdir);
+      snprintf(temp,sizeof(temp),"%slastlog",outdir);
       if(access(temp, R_OK) == 0)
          unlink(temp);
       return;
@@ -89,7 +95,7 @@ void mklastlog(const char *outdir)
 
    ftot-=LastLog;
 
-   sprintf(temp,"%slastlog",outdir);
+   snprintf(temp,sizeof(temp),"%slastlog",outdir);
    if((fp_in=fopen(temp,"r"))==NULL) {
      debuga(_("(lastlog) Cannot open temporary file %s\n"),temp);
      exit(EXIT_FAILURE);
@@ -114,8 +120,10 @@ void mklastlog(const char *outdir)
    }
 
    fclose(fp_in);
-   sprintf(temp,"%slastlog",outdir);
-   unlink(temp);
+   snprintf(temp,sizeof(temp),"%slastlog",outdir);
+   if (unlink(temp) == -1) {
+      debuga(_("Failed to delete the file %s\n"),temp);
+   }
 
    return;
 }
index 903b42f8e6a8288570171720c1212edacdb2e67c..6d6b16e1205bf26374dc843bf2354e3dc159dc80 100644 (file)
--- a/report.c
+++ b/report.c
@@ -104,7 +104,10 @@ void gerarel(void)
    strncat(tmp,"/sarg",5);
    fp_tt=NULL;
 
-   dirp = opendir(tmp);
+   if ((dirp = opendir(tmp)) == NULL) {
+      debuga(_("Failed to open directory %s - %s\n"),tmp,strerror(errno));
+      exit(EXIT_FAILURE);
+   }
    while ((direntp = readdir( dirp )) != NULL ) {
       dlen=strlen(direntp->d_name)-(sizeof(logext)-1);
       if (dlen<0) continue;
diff --git a/sort.c b/sort.c
index 04a6dd3370934aaab699e94c6602f4ce92cdbe99..14b5cd93aebd0318e1e381f4af7cb075180df9af 100644 (file)
--- a/sort.c
+++ b/sort.c
@@ -64,7 +64,10 @@ void tmpsort(void)
    if(strcmp(UserSortOrder,"normal") == 0)
       order="";
 
-   dirp = opendir(tmp);
+   if ((dirp = opendir(tmp)) == NULL) {
+      debuga(_("Failed to open directory %s - %s\n"),tmp,strerror(errno));
+      exit(EXIT_FAILURE);
+   }
    while ((direntp = readdir( dirp )) != NULL ){
       dlen=strlen(direntp->d_name)-(sizeof(tmpext)-1);
       if (dlen<0) continue;
@@ -122,9 +125,12 @@ void sort_users_log(const char *tmp, int debug)
       debuga(_("pre-sorting files\n"));
    }
 
-   sprintf(wtmp,"%s/sarg",tmp);
+   snprintf(wtmp,sizeof(wtmp),"%s/sarg",tmp);
 
-   dirp = opendir(wtmp);
+   if ((dirp = opendir(wtmp)) == NULL) {
+      debuga(_("Failed to open directory %s - %s\n"),wtmp,strerror(errno));
+      exit(EXIT_FAILURE);
+   }
    while ( (direntp = readdir( dirp )) != NULL ){
       dlen=strlen(direntp->d_name)-(sizeof(unsortext)-1);
       if (dlen<0) continue;
diff --git a/util.c b/util.c
index 42c7f96ec17f1b80e48e64dc3d4021425adeacfb..4c1d163a7721945b554d43bd28f10b1162c55a73 100644 (file)
--- a/util.c
+++ b/util.c
@@ -829,26 +829,33 @@ void obttotal(const char *dirname, const char *name, char *tbytes, int nuser, ch
 
 void gperiod(const char *dirname, const char *period)
 {
-
    FILE *fp_ou;
    char wdirname[MAXLEN];
 
-   strcpy(wdirname,dirname);
-   strcat(wdirname,"/sarg-period");
+   if(debug)
+      debuga(_("Making period file\n"));
+
+   if (snprintf(wdirname,sizeof(wdirname),"%s/sarg-period",dirname)>=sizeof(wdirname)) {
+      debuga(_("Output file name too long: %s/sarg-period"),dirname);
+      exit(EXIT_FAILURE);
+   }
 
    if((fp_ou=fopen(wdirname,"w"))==NULL){
-      debuga(_("(report) Cannot open file %s\n"),wdirname);
+      debuga(_("Cannot open file %s for writing\n"),wdirname);
       exit(EXIT_FAILURE);
    }
 
-   fputs(period,fp_ou);
-   fclose(fp_ou);
+   if (fputs(period,fp_ou)==EOF) {
+      debuga(_("Failed to write the requested period in %s\n"),wdirname);
+      exit(EXIT_FAILURE);
+   }
 
-   if(debug)
-      debuga(_("Making period file\n"));
+   if (fclose(fp_ou)==EOF) {
+      debuga(_("Failed to close %s - %s\n"),wdirname,strerror(errno));
+      exit(EXIT_FAILURE);
+   }
 
    return;
-
 }
 
 static void copy_images(void)