]> granicus.if.org Git - fcron/commitdiff
bug corrected : in create_spooldir : detect if file exist and is not a dir
authorthib <thib>
Wed, 4 Jul 2001 17:45:39 +0000 (17:45 +0000)
committerthib <thib>
Wed, 4 Jul 2001 17:45:39 +0000 (17:45 +0000)
cleaner exit on error in create_spooldir

fcron.c

diff --git a/fcron.c b/fcron.c
index ab4e4f2fd86f502c084f53ac1771c75d67b60f88..ffbc87d421be6af9cafc20c2566fe3391421d299 100644 (file)
--- a/fcron.c
+++ b/fcron.c
  *  `LICENSE' that comes with the fcron source distribution.
  */
 
- /* $Id: fcron.c,v 1.50 2001-07-04 17:29:48 thib Exp $ */
+ /* $Id: fcron.c,v 1.51 2001-07-04 17:45:39 thib Exp $ */
 
 #include "fcron.h"
 
-char rcs_info[] = "$Id: fcron.c,v 1.50 2001-07-04 17:29:48 thib Exp $";
+char rcs_info[] = "$Id: fcron.c,v 1.51 2001-07-04 17:45:39 thib Exp $";
 
 void main_loop(void);
 void check_signal(void);
@@ -325,8 +325,9 @@ create_spooldir(char *dir)
     /* create a new spool dir for fcron : set correctly its mode and owner */
 {
     int dir_fd = -1;
-    struct passwd *pass;
-    struct group *grp;
+    struct passwd *pass = NULL;
+    struct group *grp = NULL;
+    struct stat st;
 
     if ( mkdir(dir, 0) != 0 && errno != EEXIST )
        die_e("Cannot create dir %s", dir);
@@ -334,17 +335,31 @@ create_spooldir(char *dir)
     if ( (dir_fd = open(dir, 0)) < 0 )
        die_e("Cannot open dir %s", dir);
 
+    if ( fstat(dir_fd, &st) != 0 ) {
+       close(dir_fd);
+       die_e("Cannot fstat %s", dir);
+    }
+
+    if ( ! S_ISDIR(st.st_mode) ) {
+       close(dir_fd);
+       die("%s exists and is not a directory", dir);
+    }
+
     if ( (pass = getpwnam(USERNAME)) == NULL )
        die_e("Cannot getpwnam(%s)", USERNAME);
 
     if ( (grp = getgrnam(GROUPNAME)) == NULL )
        die_e("Cannot getgrnam(%s)", GROUPNAME);
 
-    if ( fchown(dir_fd, pass->pw_uid, grp->gr_gid) != 0 )
+    if ( fchown(dir_fd, pass->pw_uid, grp->gr_gid) != 0 ) {
+       close(dir_fd);
        die_e("Cannot fchown dir %s to %s:%s", dir, USERNAME, GROUPNAME);
+    }
 
-    if ( fchmod(dir_fd, S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP) != 0)
+    if (fchmod(dir_fd, S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP) != 0) {
+       close(dir_fd);
        die_e("Cannot change dir %s's mode to 770", dir);
+    }
 
     close(dir_fd);