]> git.ipfire.org Git - thirdparty/sarg.git/blobdiff - util.c
Added an option to use GCC's protections at compile and run time.
[thirdparty/sarg.git] / util.c
diff --git a/util.c b/util.c
index 7e768205b88265e2bbdac55101c6a26fdd1fc82c..05b8ca3d383d70c5de2db1a2eb656590f791f1bb 100644 (file)
--- a/util.c
+++ b/util.c
@@ -627,9 +627,12 @@ void obtdate(const char *dirname, const char *name, char *data)
       }
    }
 
-   fgets(data,80,fp_in);
+   if (!fgets(data,80,fp_in)) {
+      fprintf(stderr,"Failed to read the date in %s\n",wdir);
+      exit(1);
+   }
    fclose(fp_in);
-   data[strlen(data)-1]='\0';
+   fixendofline(data);
 
    return;
 
@@ -651,9 +654,12 @@ void obtuser(const char *dirname, const char *name, char *tuser)
       }
    }
 
-   fgets(tuser,20,fp_in);
-   tuser[strlen(tuser)-1]='\0';
+   if (!fgets(tuser,20,fp_in)) {
+      fprintf(stderr,"Failed to read the user in %s\n",wdir);
+      exit(1);
+   }
    fclose(fp_in);
+   fixendofline(tuser);
 
    return;
 
@@ -753,6 +759,7 @@ void vrfydir(const char *dir, const char *per1, const char *addr, const char *si
    char d1[3], d2[3];
    char m1[4], m2[4];
    time_t curtime;
+   int cstatus;
 
    if(strcmp(IndexTree,"date") == 0) {
       bzero(y1,5);
@@ -842,7 +849,12 @@ void vrfydir(const char *dir, const char *per1, const char *addr, const char *si
       } else {
          if(access(dir,R_OK) == 0) {
             sprintf(csort,"rm -r \"%s\"",dir);
-            system(csort);
+            cstatus=system(csort);
+            if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
+               fprintf(stderr, "SARG: command return status %d\n",WEXITSTATUS(cstatus));
+               fprintf(stderr, "SARG: command: %s\n",csort);
+               exit(1);
+            }
          }
       }
       my_mkdir(dirname);
@@ -868,7 +880,12 @@ void vrfydir(const char *dir, const char *per1, const char *addr, const char *si
       } else {
          if(access(wdir,R_OK) == 0) {
             sprintf(csort,"rm -r \"%s\"",wdir);
-            system(csort);
+            cstatus=system(csort);
+            if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
+               fprintf(stderr, "SARG: command return status %d\n",WEXITSTATUS(cstatus));
+               fprintf(stderr, "SARG: command: %s\n",csort);
+               exit(1);
+            }
          }
       }
 
@@ -1312,7 +1329,10 @@ char *get_size(const char *path, const char *file)
       exit(1);
    }
    fp = popen(cmd, "r");
-   fgets(response, sizeof(response), fp);
+   if (!fgets(response, sizeof(response), fp)) {
+      fprintf(stderr,"SARG: Cannot get disk size with command %s",cmd);
+      exit(1);
+   }
    if (getword_multisep(val5,sizeof(val5),response,'\t')<0) {
       printf("SARG: Maybe the command %s failed.\n",cmd);
       exit(1);
@@ -1381,6 +1401,9 @@ void output_html_string(FILE *fp_ou,const char *str)
 
 void baddata(void)
 {
+   char cmd[1024];
+   int cstatus;
+
    printf("SARG: ------------------------------------------------------------------------------\n");
    printf("SARG: MALICIUS CODE DETECTED.\n");
    printf("SARG: I think someone is trying to execute arbitrary code in your system using sarg.\n");
@@ -1388,10 +1411,26 @@ void baddata(void)
    printf("SARG: process stoped. No actions taken.\n");
    printf("SARG: ------------------------------------------------------------------------------\n");
 
-   system("rm -rf \"/tmp/sarg\"");
-   sprintf(tmp4,"rm -rf \"%s\"",dirname);
-   system(tmp4);
-   system("rm -rf \"/tmp/sarg\"");
+   if (snprintf(cmd,sizeof(cmd),"rm -rf \"%s/sarg\"",tmp)>=sizeof(cmd)) {
+      fprintf(stderr,"SARG: temporary directory too long: %s/sarg\n",tmp);
+      exit(1);
+   }
+   cstatus=system(cmd);
+   if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
+      fprintf(stderr, "SARG: command return status %d\n",WEXITSTATUS(cstatus));
+      fprintf(stderr, "SARG: command: %s\n",cmd);
+      exit(1);
+   }
+   if (snprintf(cmd,sizeof(cmd),"rm -rf \"%s\"",dirname)>=sizeof(cmd)) {
+      fprintf(stderr,"SARG: directory to delete too long: %s\n",dirname);
+      exit(1);
+   }
+   cstatus=system(cmd);
+   if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
+      fprintf(stderr, "SARG: command return status %d\n",WEXITSTATUS(cstatus));
+      fprintf(stderr, "SARG: command: %s\n",cmd);
+      exit(1);
+   }
 
    exit(1);
 }