Secure the writing of the sarg-period file.
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;
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;
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)
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;
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;
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;
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;
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) {
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 );
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);
unlink(temp);
if(ftot<=LastLog) {
- sprintf(temp,"%slastlog",outdir);
+ snprintf(temp,sizeof(temp),"%slastlog",outdir);
if(access(temp, R_OK) == 0)
unlink(temp);
return;
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);
}
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;
}
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;
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;
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;
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)