]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Fix purging old log files with absolute file path
authorMatthijs Mekking <matthijs@isc.org>
Wed, 5 Apr 2023 15:50:12 +0000 (17:50 +0200)
committerMatthijs Mekking <matthijs@isc.org>
Wed, 3 May 2023 07:12:11 +0000 (09:12 +0200)
Removing old timestamp or increment versions of log backup files did
not work when the file is an absolute path: only the entry name was
provided to the file remove function.

The dirname was also bogus, since the file separater was put back too
soon.

Fix these issues to make log file rotation work when the file is
configured to be an absolute path.

lib/isc/log.c

index a24e1c37fefedbb52a191e7209f5457be9d6cd60..e94f54c43c3241e9c399a2eac61ad0f3bd6639cf 100644 (file)
@@ -1036,18 +1036,11 @@ greatest_version(isc_logfile_t *file, int versions, int *greatestp) {
        isc_dir_init(&dir);
        result = isc_dir_open(&dir, dirname);
 
-       /*
-        * Replace the file separator if it was taken out.
-        */
-       if (bname != file->name) {
-               *(bname - 1) = sep;
-       }
-
        /*
         * Return if the directory open failed.
         */
        if (result != ISC_R_SUCCESS) {
-               return (result);
+               goto out;
        }
 
        while (isc_dir_read(&dir) == ISC_R_SUCCESS) {
@@ -1061,14 +1054,25 @@ greatest_version(isc_logfile_t *file, int versions, int *greatestp) {
                         * Remove any backup files that exceed versions.
                         */
                        if (*digit_end == '\0' && version >= versions) {
-                               result = isc_file_remove(dir.entry.name);
+                               char rmfile[PATH_MAX + 1];
+                               int n = snprintf(rmfile, sizeof(rmfile),
+                                                "%s/%s", dirname,
+                                                dir.entry.name);
+                               if (n >= (int)sizeof(rmfile) || n < 0) {
+                                       result = ISC_R_NOSPACE;
+                                       syslog(LOG_ERR,
+                                              "unable to remove log files: %s",
+                                              isc_result_totext(result));
+                                       break;
+                               }
+                               result = isc_file_remove(rmfile);
                                if (result != ISC_R_SUCCESS &&
-                                   result != ISC_R_FILENOTFOUND)
+                                   result != ISC_R_NOTFOUND)
                                {
                                        syslog(LOG_ERR,
-                                              "unable to remove "
-                                              "log file '%s': %s",
-                                              dir.entry.name,
+                                              "unable to remove log file "
+                                              "'%s': %s",
+                                              rmfile,
                                               isc_result_totext(result));
                                }
                        } else if (*digit_end == '\0' && version > greatest) {
@@ -1079,8 +1083,16 @@ greatest_version(isc_logfile_t *file, int versions, int *greatestp) {
        isc_dir_close(&dir);
 
        *greatestp = greatest;
+       result = ISC_R_SUCCESS;
 
-       return (ISC_R_SUCCESS);
+out:
+       /*
+        * Replace the file separator if it was taken out.
+        */
+       if (bname != file->name) {
+               *(bname - 1) = sep;
+       }
+       return (result);
 }
 
 static void
@@ -1168,18 +1180,11 @@ remove_old_tsversions(isc_logfile_t *file, int versions) {
        isc_dir_init(&dir);
        result = isc_dir_open(&dir, dirname);
 
-       /*
-        * Replace the file separator if it was taken out.
-        */
-       if (bname != file->name) {
-               *(bname - 1) = sep;
-       }
-
        /*
         * Return if the directory open failed.
         */
        if (result != ISC_R_SUCCESS) {
-               return (result);
+               goto out;
        }
 
        last = last_to_keep(versions, &dir, bname, bnamelen);
@@ -1198,14 +1203,25 @@ remove_old_tsversions(isc_logfile_t *file, int versions) {
                         * Remove any backup files that exceed versions.
                         */
                        if (*digit_end == '\0' && version < last) {
-                               result = isc_file_remove(dir.entry.name);
+                               char rmfile[PATH_MAX + 1];
+                               int n = snprintf(rmfile, sizeof(rmfile),
+                                                "%s/%s", dirname,
+                                                dir.entry.name);
+                               if (n >= (int)sizeof(rmfile) || n < 0) {
+                                       result = ISC_R_NOSPACE;
+                                       syslog(LOG_ERR,
+                                              "unable to remove log files: %s",
+                                              isc_result_totext(result));
+                                       break;
+                               }
+                               result = isc_file_remove(rmfile);
                                if (result != ISC_R_SUCCESS &&
-                                   result != ISC_R_FILENOTFOUND)
+                                   result != ISC_R_NOTFOUND)
                                {
                                        syslog(LOG_ERR,
-                                              "unable to remove "
-                                              "log file '%s': %s",
-                                              dir.entry.name,
+                                              "unable to remove log file "
+                                              "'%s': %s",
+                                              rmfile,
                                               isc_result_totext(result));
                                }
                        }
@@ -1213,8 +1229,16 @@ remove_old_tsversions(isc_logfile_t *file, int versions) {
        }
 
        isc_dir_close(&dir);
+       result = ISC_R_SUCCESS;
 
-       return (ISC_R_SUCCESS);
+out:
+       /*
+        * Replace the file separator if it was taken out.
+        */
+       if (bname != file->name) {
+               *(bname - 1) = sep;
+       }
+       return (result);
 }
 
 static isc_result_t