bug-sysutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Bug-sysutils] patch for cppw to work even if files don't exist


From: Barry deFreese
Subject: [Bug-sysutils] patch for cppw to work even if files don't exist
Date: Wed, 09 Jun 2004 18:25:11 -0700
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040413 Debian/1.6-5

OK, here are patches for cppw and sysutils.c to fix the problem with cppw dumping if /etc/{passwd|group|shadow|gshadow} don't already exist.

I have included a patch for ChangeLog this time, let me know if this is incorrect.

Thank you,

--
Barry deFreese
Debian 3.0r1 "Woody"
GNU/Hurd
Registered Linux "Newbie" #302256 - Hurd H4XX0r wannabe

"Programming today is a race between software engineers striving
to build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots. So far, the Universe is
winning." Rich Cook.



Index: ChangeLog
===================================================================
RCS file: /cvsroot/sysutils/sysutils/ChangeLog,v
retrieving revision 1.16
diff -u -p -r1.16 ChangeLog
--- ChangeLog   8 Jun 2004 18:09:38 -0000       1.16
+++ ChangeLog   10 Jun 2004 02:08:50 -0000
@@ -1,3 +1,8 @@
+2004-06-09  Barry deFreese <address@hidden>
+       * src/sysutils.c (set_file_perm): New function.
+       * src/cppw.c (main): Added check to continue even if
+         /etc/{passwd|group|shadow|gshadow} don't exist
+
 2004-06-07  David Weinehall  <address@hidden>
 
        * src/chage.c (main): Fixed bug that caused aging information
Index: src/cppw.c
===================================================================
RCS file: /cvsroot/sysutils/sysutils/src/cppw.c,v
retrieving revision 1.4
diff -u -p -r1.4 cppw.c
--- src/cppw.c  5 Jun 2004 14:39:39 -0000       1.4
+++ src/cppw.c  10 Jun 2004 02:08:08 -0000
@@ -26,6 +26,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <sys/stat.h>
 
 #include "misc.h"
 #include "sysutils.h"
@@ -143,6 +144,8 @@ int main(int argc, char *argv[])
        char *cname = NULL;
        const char *fname = NULL;
 
+       struct stat statbuf;
+
        int passwd = F_DEFAULT;
        int shadow = F_DEFAULT;
 
@@ -233,6 +236,7 @@ int main(int argc, char *argv[])
                goto EXIT;
        }
 
+
        /* Create filename /etc/<file>- */
        if (!(bname = create_filename(fname, BACKUP_EXT))) {
                status = errno;
@@ -249,21 +253,43 @@ int main(int argc, char *argv[])
        if ((status = lock_files()))
                goto EXIT;
 
-       /* Backup file to /etc/<file>- */
-       if ((status = backup_file(fname, bname)))
-               goto EXIT2;
-
-       /* Copy permissions from /etc/<file> to /etc/<file>- */
-       if ((status = copy_file_modes(fname, bname)))
-               goto EXIT2;
-
-       /* Copy <newfile> to /etc/<file>.copy */
-       if ((status = copy_file(args.newfile, cname)))
+       /* Check that the source file /etc/<file> exists */
+       if ((stat(fname, &statbuf)) != -1) {
+               /* Backup file to /etc/<file>- */
+               if ((status = backup_file(fname, bname)))
+                       goto EXIT2;
+
+               /* Copy <newfile> to /etc/<file>.copy */
+               if ((status = copy_file(args.newfile, cname)))
+                       goto EXIT2;
+
+               /* Copy permissions from /etc/<file> to /etc/<file>- */
+               if ((status = copy_file_modes(fname, bname)))
+                       goto EXIT2;
+
+       } else if (errno != ENOENT) {
+               fprintf(stderr,
+                       _("%s: '%s' failed: %s\n"),
+                       progname, "stat()", strerror(errno));
+               status = errno;
                goto EXIT2;
+       } else {
+               errno = 0;
 
-       /* Copy permissions from /etc/<file> to /etc/<file>.copy */
-       if ((status = copy_file_modes(fname, cname)))
-               goto EXIT2;
+               fprintf(stderr,
+                       _("%s: warning: '%s' does not exist; no backups 
made\n"),
+                       progname, fname);
+
+               /* Copy <newfile> to /etc/<file>.copy */
+               if ((status = copy_file(args.newfile, cname)))
+                       goto EXIT2;
+
+               /* Set the permissions for /etc/<file>.copy */
+               if ((status = set_file_perms(cname, "root",
+                                            args.shadow ? "shadow" : "root",
+                                            args.shadow ? 0640 : 0644)))
+                       goto EXIT2;
+       }
 
        /* Finally, move the new file in place */
        status = replace_file(cname, fname);
Index: src/sysutils.c
===================================================================
RCS file: /cvsroot/sysutils/sysutils/src/sysutils.c,v
retrieving revision 1.3
diff -u -p -r1.3 sysutils.c
--- src/sysutils.c      6 Jun 2004 14:20:03 -0000       1.3
+++ src/sysutils.c      10 Jun 2004 02:07:48 -0000
@@ -1384,6 +1384,70 @@ EXIT:
 }
 
 /**
+ * Set file owner and modes manually
+ *
+ *  @param file A string with the name of the file to set perms
+ *  @param user A string with the file owner name
+ *  @param group A string with the group owner name
+ *  @param mode A string with the new mode for the file
+ * @return 0 on success, errno on failure
+ */
+
+error_t set_file_perms(const char *file, const char *user,
+                      const char *group, mode_t mode)
+{
+printf("file, user, group, and mode coming into function are: %s, %s, %s, 
%o\n",
+       file, user, group, mode);
+       error_t status = 0;
+       struct stat statbuf;
+       struct passwd *pw;
+       struct group *gr;
+
+       if (stat(file, &statbuf)) {
+               status = errno;
+               goto EXIT;
+       }
+printf("stat of %s succeeded\n", file);
+
+       if (!(pw=getpwnam(user))) {
+printf("getpwnam failed: %s\n", strerror(errno));
+               status = errno;
+               goto EXIT;
+       }
+
+printf("getpwnam succeeded\n");
+
+       if (!(gr=getgrnam(group))) {
+printf("getgrnam failed");
+               status = errno;
+               goto EXIT;
+       }
+
+printf("getgrnam succeeded\n");
+
+printf("user and group ids are: %d, %d\n", pw->pw_uid, gr->gr_gid);
+
+       if (chown(file, pw->pw_uid, gr->gr_gid)) {
+               status = errno;
+               goto EXIT;
+       }
+
+       if (chmod(file, mode)) {
+               status = errno;
+               goto EXIT;
+       }
+
+EXIT:
+       if (status)
+               fprintf(stderr,
+                       _("%s: failed to set permissions for "
+                         "`%s'; %s\n"),
+                       progname, file, strerror(errno));
+
+       return status;
+}
+
+/**
  * Replace target with source, atomically
  *
  *   @param source A string with the name of the file to copy from

reply via email to

[Prev in Thread] Current Thread [Next in Thread]