Browse code

improve generating of temporary file names

git-svn-id: file:///var/lib/svn/clamav-devel/trunk/clamav-devel@1101 77e5149b-7576-45b1-b177-96237e5ba77b

Tomasz Kojm authored on 2004/11/17 02:10:47
Showing 5 changed files
... ...
@@ -1,3 +1,8 @@
1
+Tue Nov 16 18:07:39 CET 2004 (tk)
2
+---------------------------------
3
+  * libclamav: improve generating of temporary file names (as requested by
4
+	       Boguslaw Brandys and Nigel)
5
+
1 6
 Tue Nov 16 16:58:12 CET 2004 (tk)
2 7
 ---------------------------------
3 8
   * freshclam/manager.c: get_database: terminate buffer after recv() call
... ...
@@ -333,7 +333,6 @@ int cli_cvdload(FILE *fd, struct cl_node **root, unsigned int *signo, short warn
333 333
         char *dir, *tmp, *buffer;
334 334
 	struct cl_cvd cvd;
335 335
 	int bytes, ret;
336
-	const char *tmpdir;
337 336
 	FILE *tmpd;
338 337
 	time_t stime;
339 338
 
... ...
@@ -364,16 +363,7 @@ int cli_cvdload(FILE *fd, struct cl_node **root, unsigned int *signo, short warn
364 364
 
365 365
     fseek(fd, 512, SEEK_SET);
366 366
 
367
-    tmpdir = getenv("TMPDIR");
368
-
369
-    if(tmpdir == NULL)
370
-#ifdef P_tmpdir
371
-	tmpdir = P_tmpdir;
372
-#else
373
-	tmpdir = "/tmp";
374
-#endif
375
-
376
-    dir = cli_gentemp(tmpdir);
367
+    dir = cli_gentemp(NULL);
377 368
     if(mkdir(dir, 0700)) {
378 369
 	cli_errmsg("cli_cvdload():  Can't create temporary directory %s\n", dir);
379 370
 	return CL_ETMPDIR;
... ...
@@ -392,7 +382,7 @@ int cli_cvdload(FILE *fd, struct cl_node **root, unsigned int *signo, short warn
392 392
 
393 393
 	    /* start */
394 394
 
395
-	    tmp = cli_gentemp(tmpdir);
395
+	    tmp = cli_gentemp(NULL);
396 396
 	    if((tmpd = fopen(tmp, "wb+")) == NULL) {
397 397
 		cli_errmsg("Can't create temporary file %s\n", tmp);
398 398
 		free(dir);
... ...
@@ -352,10 +352,15 @@ char *cli_gentemp(const char *dir)
352 352
 	struct stat foo;
353 353
 
354 354
 
355
-    if(!dir)
356
-	mdir = "/tmp";
357
-    else
358
-	mdir = (char *) dir;
355
+    if(!dir) {
356
+	if((mdir = getenv("TMPDIR")) == NULL)
357
+#ifdef P_tmpdir
358
+	    mdir = P_tmpdir;
359
+#else
360
+	    mdir = "/tmp";
361
+#endif
362
+    } else
363
+	mdir = dir;
359 364
 
360 365
     name = (char*) cli_calloc(strlen(mdir) + 1 + 16 + 1 + 7, sizeof(char));
361 366
     if(name == NULL) {
... ...
@@ -656,7 +656,6 @@ static int cli_scanmscab(int desc, const char **virname, long int *scanned, cons
656 656
 	struct mscab_decompressor *cabd = NULL;
657 657
 	struct mscabd_cabinet *base, *cab;
658 658
 	struct mscabd_file *file;
659
-	const char *tmpdir;
660 659
 	char *tempname;
661 660
 	int ret = CL_CLEAN;
662 661
 
... ...
@@ -674,13 +673,6 @@ static int cli_scanmscab(int desc, const char **virname, long int *scanned, cons
674 674
 	return CL_EMSCAB;
675 675
     }
676 676
 
677
-    if((tmpdir = getenv("TMPDIR")) == NULL)
678
-#ifdef P_tmpdir
679
-	tmpdir = P_tmpdir;
680
-#else
681
-	tmpdir = "/tmp";
682
-#endif
683
-
684 677
     for(cab = base; cab; cab = cab->next) {
685 678
 	for(file = cab->files; file; file = file->next) {
686 679
 
... ...
@@ -695,7 +687,7 @@ static int cli_scanmscab(int desc, const char **virname, long int *scanned, cons
695 695
 		continue;
696 696
 	    }
697 697
 
698
-	    tempname = cli_gentemp(tmpdir);
698
+	    tempname = cli_gentemp(NULL);
699 699
 	    cli_dbgmsg("MSCAB: Extracting data to %s\n", tempname);
700 700
 	    if(cabd->extract(cabd, file, tempname)) {
701 701
 		cli_dbgmsg("MSCAB: libmscab error code: %d\n", cabd->last_error(cabd));
... ...
@@ -721,21 +713,12 @@ static int cli_scanmscab(int desc, const char **virname, long int *scanned, cons
721 721
 static int cli_scanhtml(int desc, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, int *arec, int *mrec)
722 722
 {
723 723
 	char *tempname, fullname[1024];
724
-	const char *tmpdir;
725 724
 	int ret=CL_CLEAN, fd;
726 725
 
727 726
 
728 727
     cli_dbgmsg("in cli_scanhtml()\n");
729 728
 
730
-    if((tmpdir = getenv("TMPDIR")) == NULL)
731
-#ifdef P_tmpdir
732
-        tmpdir = P_tmpdir;
733
-#else
734
-        tmpdir = "/tmp";
735
-#endif
736
-                                                                                                                                           
737
-    tempname = cli_gentemp(tmpdir);
738
-                                                                                                                                           
729
+    tempname = cli_gentemp(NULL);
739 730
     if(mkdir(tempname, 0700)) {
740 731
         cli_dbgmsg("ScanHTML -> Can't create temporary directory %s\n", tempname);
741 732
         return CL_ETMPDIR;
... ...
@@ -996,22 +979,14 @@ static int cli_vba_scandir(const char *dirname, const char **virname, long int *
996 996
 
997 997
 static int cli_scanole2(int desc, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, int *arec, int *mrec)
998 998
 {
999
-	const char *tmpdir;
1000 999
 	char *dir;
1001 1000
 	int ret = CL_CLEAN;
1002 1001
 
1003 1002
 
1004 1003
     cli_dbgmsg("in cli_scanole2()\n");
1005 1004
 
1006
-    if((tmpdir = getenv("TMPDIR")) == NULL)
1007
-#ifdef P_tmpdir
1008
-	tmpdir = P_tmpdir;
1009
-#else
1010
-	tmpdir = "/tmp";
1011
-#endif
1012
-
1013 1005
     /* generate the temporary directory */
1014
-    dir = cli_gentemp(tmpdir);
1006
+    dir = cli_gentemp(NULL);
1015 1007
     if(mkdir(dir, 0700)) {
1016 1008
 	cli_dbgmsg("OLE2: Can't create temporary directory %s\n", dir);
1017 1009
 	return CL_ETMPDIR;
... ...
@@ -1039,22 +1014,14 @@ static int cli_scanole2(int desc, const char **virname, long int *scanned, const
1039 1039
 
1040 1040
 static int cli_scantar(int desc, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, int *arec, int *mrec)
1041 1041
 {
1042
-	const char *tmpdir;
1043 1042
 	char *dir;
1044 1043
 	int ret = CL_CLEAN;
1045 1044
 
1046 1045
 
1047 1046
     cli_dbgmsg("in cli_scantar()\n");
1048 1047
 
1049
-    if((tmpdir = getenv("TMPDIR")) == NULL)
1050
-#ifdef P_tmpdir
1051
-	tmpdir = P_tmpdir;
1052
-#else
1053
-	tmpdir = "/tmp";
1054
-#endif
1055
-
1056 1048
     /* generate temporary directory */
1057
-    dir = cli_gentemp(tmpdir);
1049
+    dir = cli_gentemp(NULL);
1058 1050
     if(mkdir(dir, 0700)) {
1059 1051
 	cli_errmsg("Tar: Can't create temporary directory %s\n", dir);
1060 1052
 	return CL_ETMPDIR;
... ...
@@ -1074,22 +1041,13 @@ static int cli_scantar(int desc, const char **virname, long int *scanned, const
1074 1074
 
1075 1075
 static int cli_scanmschm(int desc, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, int *arec, int *mrec)
1076 1076
 {
1077
-	const char *tmpdir;
1078 1077
 	char *tempname;
1079 1078
 	int ret = CL_CLEAN;
1080 1079
 
1081 1080
 
1082 1081
     cli_dbgmsg("in cli_scanmschm()\n");	
1083 1082
 
1084
-    if((tmpdir = getenv("TMPDIR")) == NULL)
1085
-#ifdef P_tmpdir
1086
-	tmpdir = P_tmpdir;
1087
-#else
1088
-	tmpdir = "/tmp";
1089
-#endif
1090
-
1091
-    tempname = cli_gentemp(tmpdir);
1092
-
1083
+    tempname = cli_gentemp(NULL);
1093 1084
     if(mkdir(tempname, 0700)) {
1094 1085
 	cli_dbgmsg("CHM: Can't create temporary directory %s\n", tempname);
1095 1086
 	return CL_ETMPDIR;
... ...
@@ -1107,21 +1065,12 @@ static int cli_scanmschm(int desc, const char **virname, long int *scanned, cons
1107 1107
 
1108 1108
 static int cli_scanscrenc(int desc, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, int *arec, int *mrec)
1109 1109
 {
1110
-	const char *tmpdir;
1111 1110
 	char *tempname;
1112 1111
 	int ret = CL_CLEAN;
1113 1112
 
1114 1113
     cli_dbgmsg("in cli_scanscrenc()\n");
1115 1114
 
1116
-    if((tmpdir = getenv("TMPDIR")) == NULL)
1117
-#ifdef P_tmpdir
1118
-        tmpdir = P_tmpdir;
1119
-#else
1120
-        tmpdir = "/tmp";
1121
-#endif
1122
-                                                                                                                               
1123
-    tempname = cli_gentemp(tmpdir);
1124
-                                                                                                                               
1115
+    tempname = cli_gentemp(NULL);
1125 1116
     if(mkdir(tempname, 0700)) {
1126 1117
 	cli_dbgmsg("CHM: Can't create temporary directory %s\n", tempname);
1127 1118
 	return CL_ETMPDIR;
... ...
@@ -1139,45 +1088,37 @@ static int cli_scanscrenc(int desc, const char **virname, long int *scanned, con
1139 1139
 
1140 1140
 static int cli_scanmail(int desc, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, int *arec, int *mrec)
1141 1141
 {
1142
-	const char *tmpdir;
1143 1142
 	char *dir;
1144 1143
 	int ret;
1145 1144
 
1146 1145
 
1147 1146
     cli_dbgmsg("Starting cli_scanmail(), mrec == %d, arec == %d\n", *mrec, *arec);
1148 1147
 
1149
-    if((tmpdir = getenv("TMPDIR")) == NULL)
1150
-#ifdef P_tmpdir
1151
-	tmpdir = P_tmpdir;
1152
-#else
1153
-	tmpdir = "/tmp";
1154
-#endif
1155
-
1156
-	/* generate the temporary directory */
1157
-	dir = cli_gentemp(tmpdir);
1158
-	if(mkdir(dir, 0700)) {
1159
-	    cli_dbgmsg("Mail: Can't create temporary directory %s\n", dir);
1160
-	    free(dir);
1161
-	    return CL_ETMPDIR;
1162
-	}
1163
-
1164
-	/*
1165
-	 * Extract the attachments into the temporary directory
1166
-	 */
1167
-	if((ret = cli_mbox(dir, desc, options))) {
1168
-	    if(!cli_leavetemps_flag)
1169
-		cli_rmdirs(dir);
1170
-	    free(dir);
1171
-	    return ret;
1172
-	}
1173
-
1174
-	ret = cli_scandir(dir, virname, scanned, root, limits, options, arec, mrec);
1148
+    /* generate the temporary directory */
1149
+    dir = cli_gentemp(NULL);
1150
+    if(mkdir(dir, 0700)) {
1151
+	cli_dbgmsg("Mail: Can't create temporary directory %s\n", dir);
1152
+	free(dir);
1153
+	return CL_ETMPDIR;
1154
+    }
1175 1155
 
1156
+    /*
1157
+     * Extract the attachments into the temporary directory
1158
+     */
1159
+    if((ret = cli_mbox(dir, desc, options))) {
1176 1160
 	if(!cli_leavetemps_flag)
1177 1161
 	    cli_rmdirs(dir);
1178 1162
 	free(dir);
1179
-
1180 1163
 	return ret;
1164
+    }
1165
+
1166
+    ret = cli_scandir(dir, virname, scanned, root, limits, options, arec, mrec);
1167
+
1168
+    if(!cli_leavetemps_flag)
1169
+	cli_rmdirs(dir);
1170
+
1171
+    free(dir);
1172
+    return ret;
1181 1173
 }
1182 1174
 
1183 1175
 int cli_magic_scandesc(int desc, const char **virname, long int *scanned, const struct cl_node *root, const struct cl_limits *limits, unsigned int options, int *arec, int *mrec)
... ...
@@ -800,20 +800,10 @@ static char *ppt_stream_iter(int fd)
800 800
 {
801 801
 	atom_header_t atom_header;
802 802
 	uint32_t ole_id;
803
-	char *tmpdir, *out_dir;
803
+	char *out_dir;
804 804
 	
805 805
 	/* Create a directory to store the extracted OLE2 objects */
806
-	tmpdir = getenv("TMPDIR");
807
-
808
-	if(tmpdir == NULL)
809
-#ifdef P_tmpdir
810
-		tmpdir = P_tmpdir;
811
-#else
812
-		tmpdir = "/tmp";
813
-#endif
814
-
815
-	/* generate the temporary directory */
816
-	out_dir = cli_gentemp(tmpdir);
806
+	out_dir = cli_gentemp(NULL);
817 807
 	if(mkdir(out_dir, 0700)) {
818 808
 	    printf("ScanOLE2 -> Can't create temporary directory %s\n", out_dir);
819 809
 	    free(out_dir);