]> git.ipfire.org Git - thirdparty/sarg.git/blame - lastlog.c
Fix several possible sprintf buffer overflows
[thirdparty/sarg.git] / lastlog.c
CommitLineData
25697a35 1/*
94ff9470 2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
67302a9e 3 * 1998, 2013
25697a35
GS
4 *
5 * SARG donations:
6 * please look at http://sarg.sourceforge.net/donations.php
1164c474
FM
7 * Support:
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
25697a35
GS
9 * ---------------------------------------------------------------------
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
24 *
25 */
26
27#include "include/conf.h"
5f3cfd1d 28#include "include/defs.h"
25697a35 29
32e71fa4 30void mklastlog(const char *outdir)
25697a35 31{
9bd92830
FM
32 FILE *fp_in, *fp_ou;
33 DIR *dirp;
34 struct dirent *direntp;
35 char buf[MAXLEN];
36 char temp[MAXLEN];
37 char warea[MAXLEN];
38 char ftime[128];
39 int ftot=0;
40 time_t t;
41 struct tm *local;
42 struct stat statb;
43 int cstatus;
44 struct getwordstruct gwarea;
45
46 if(LastLog <= 0)
47 return;
48
49 snprintf(temp,sizeof(temp),"%slastlog1",outdir);
50 if((fp_ou=fopen(temp,"w"))==NULL) {
d6f0349d 51 debuga(_("(lastlog) Cannot open temporary file %s: %s\n"),temp,strerror(errno));
007905af 52 exit(EXIT_FAILURE);
9bd92830
FM
53 }
54
55 if ((dirp = opendir(outdir)) == NULL) {
56 debuga(_("Failed to open directory %s - %s\n"),outdir,strerror(errno));
57 exit(EXIT_FAILURE);
58 }
59 while ((direntp = readdir( dirp )) != NULL ){
60 if(strchr(direntp->d_name,'-') == 0)
61 continue;
62
63 snprintf(warea,sizeof(warea),"%s%s",outdir,direntp->d_name);
64 if (stat(warea,&statb) == -1) {
65 debuga(_("Failed to get the creation time of %s\n"),warea);
66 continue;
67 }
68 t=statb.st_ctime;
69 local = localtime(&t);
70 strftime(ftime, sizeof(ftime), "%Y%m%d%H%M%S", local);
71 fprintf(fp_ou,"%s\t%s\n",ftime,direntp->d_name);
72 ftot++;
73 }
74
75 closedir( dirp );
507460ae
FM
76 if (fclose(fp_ou)==EOF) {
77 debuga(_("Write error in %s: %s\n"),temp,strerror(errno));
78 exit(EXIT_FAILURE);
79 }
9bd92830 80
eb843369 81 snprintf(buf,sizeof(buf),"sort -n -t \"\t\" -k 1,1 -o \"%slastlog\" \"%s\"",outdir,temp);
9bd92830
FM
82 cstatus=system(buf);
83 if (!WIFEXITED(cstatus) || WEXITSTATUS(cstatus)) {
84 debuga(_("sort command return status %d\n"),WEXITSTATUS(cstatus));
85 debuga(_("sort command: %s\n"),buf);
86 exit(EXIT_FAILURE);
87 }
88
08f9b029 89 if (unlink(temp)) {
7c4264ec 90 debuga(_("Cannot delete \"%s\": %s\n"),temp,strerror(errno));
08f9b029
FM
91 exit(EXIT_FAILURE);
92 }
9bd92830
FM
93
94 if(ftot<=LastLog) {
95 snprintf(temp,sizeof(temp),"%slastlog",outdir);
08f9b029
FM
96 if(access(temp, R_OK) == 0) {
97 if (unlink(temp)) {
7c4264ec 98 debuga(_("Cannot delete \"%s\": %s\n"),temp,strerror(errno));
08f9b029
FM
99 exit(EXIT_FAILURE);
100 }
101 }
9bd92830
FM
102 return;
103 }
104
105 ftot-=LastLog;
106
107 snprintf(temp,sizeof(temp),"%slastlog",outdir);
108 if((fp_in=fopen(temp,"r"))==NULL) {
d6f0349d 109 debuga(_("(lastlog) Cannot open temporary file %s: %s\n"),temp,strerror(errno));
007905af 110 exit(EXIT_FAILURE);
9bd92830
FM
111 }
112
113 while(ftot>0 && fgets(buf,sizeof(buf),fp_in)!=NULL) {
114 fixendofline(buf);
115 getword_start(&gwarea,buf);
116 if (getword(warea,sizeof(warea),&gwarea,'\t')<0) {
117 debuga(_("Maybe you have a broken record or garbage in your %s file\n"),temp);
118 exit(EXIT_FAILURE);
119 }
120
121 if(debug)
122 debuga(_("Removing old report file %s\n"),gwarea.current);
123 if (snprintf(temp,sizeof(temp),"%s%s",outdir,gwarea.current)>=sizeof(temp)) {
124 debuga(_("Directory name too long: %s%s\n"),outdir,gwarea.current);
125 exit(EXIT_FAILURE);
126 }
127 unlinkdir(temp,0);
128 ftot--;
129 }
130
131 fclose(fp_in);
132 snprintf(temp,sizeof(temp),"%slastlog",outdir);
133 if (unlink(temp) == -1) {
7c4264ec 134 debuga(_("Cannot delete \"%s\": %s\n"),temp,strerror(errno));
9bd92830
FM
135 }
136
137 return;
25697a35 138}