Explorar o código

libdpkg: Move copyfileperms to non-static file_copy_perms

This functionality is also needed by the conffile handling code to
ensure that the merge output is stored in a file with the same
permissions as the original conffile, preventing the accidental
opportunity for unintended information disclosure.

Therefore the function is moved into a new library module (file.{c,h}),
and given an appropriate prefix. Note that some of the translatable
error messages have been modified as they would otherwise be misleading.

Signed-off-by: Guillem Jover <guillem@debian.org>
Sean Finney %!s(int64=17) %!d(string=hai) anos
pai
achega
4a256f2cd3
Modificáronse 4 ficheiros con 93 adicións e 26 borrados
  1. 1 0
      lib/dpkg/Makefile.am
  2. 53 0
      lib/dpkg/file.c
  3. 37 0
      lib/dpkg/file.h
  4. 2 26
      src/configure.c

+ 1 - 0
lib/dpkg/Makefile.am

@@ -26,6 +26,7 @@ libdpkg_a_SOURCES = \
 	dbmodify.c \
 	dump.c \
 	ehandle.c \
+	file.c file.h \
 	fields.c \
 	i18n.h \
 	lock.c \

+ 53 - 0
lib/dpkg/file.c

@@ -0,0 +1,53 @@
+/*
+ * libdpkg - Debian packaging suite library routines
+ * file.c - file handling functions
+ *
+ * Copyright © 1995 Ian Jackson <ian@chiark.greenend.org.uk>
+ * Copyright © 2008 Guillem Jover <guillem@debian.org>
+ *
+ * This is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2,
+ * or (at your option) any later version.
+ *
+ * This is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with dpkg; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <config.h>
+#include <compat.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <errno.h>
+#include <unistd.h>
+
+#include <dpkg/dpkg.h>
+#include <dpkg/i18n.h>
+#include <dpkg/file.h>
+
+void
+file_copy_perms(const char *src, const char *dst)
+{
+	struct stat stab;
+
+	if (stat(src, &stab) == -1) {
+		if (errno == ENOENT)
+			return;
+		ohshite(_("unable to stat source file '%.250s'"), src);
+	}
+
+	if (chown(dst, stab.st_uid, stab.st_gid) == -1)
+		ohshite(_("unable to change ownership of target file '%.250s'"),
+		        dst);
+
+	if (chmod(dst, (stab.st_mode & 07777)) == -1)
+		ohshite(_("unable to set mode of target file '%.250s'"), dst);
+}
+

+ 37 - 0
lib/dpkg/file.h

@@ -0,0 +1,37 @@
+/*
+ * libdpkg - Debian packaging suite library routines
+ * file.h - file handling routines
+ *
+ * Copyright © 2008 Guillem Jover <guillem@debian.org>
+ *
+ * This is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2,
+ * or (at your option) any later version.
+ *
+ * This is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public
+ * License along with dpkg; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#ifndef DPKG_FILE_H
+#define DPKG_FILE_H
+
+#include <dpkg/macros.h>
+
+DPKG_BEGIN_DECLS
+
+/*
+ * Copy file ownership and permissions from one file to another.
+ */
+void file_copy_perms(const char *src, const char *dst);
+
+DPKG_END_DECLS
+
+#endif /* DPKG_FILE_H */
+

+ 2 - 26
src/configure.c

@@ -47,6 +47,7 @@
 #include <dpkg/dpkg.h>
 #include <dpkg/dpkg-db.h>
 #include <dpkg/buffer.h>
+#include <dpkg/file.h>
 
 #include "filesdb.h"
 #include "main.h"
@@ -58,7 +59,6 @@ static int conffoptcells[2][2] = {
 };
 
 static void md5hash(struct pkginfo *pkg, char *hashbuf, const char *fn);
-static void copyfileperm(const char *source, const char *target);
 static void showdiff(const char *old, const char *new);
 static void suspend(void);
 static enum conffopt promptconfaction(const char *cfgfile,
@@ -108,7 +108,7 @@ deferred_configure_conffile(struct pkginfo *pkg, struct conffile *conff)
 	/* Copy the permissions from the installed version to the new
 	 * distributed version. */
 	if (!stat(cdr.buf, &stab))
-		copyfileperm(cdr.buf, cdr2.buf);
+		file_copy_perms(cdr.buf, cdr2.buf);
 	else if (errno != ENOENT)
 		ohshite(_("unable to stat current installed conffile `%.250s'"),
 		        cdr.buf);
@@ -464,30 +464,6 @@ md5hash(struct pkginfo *pkg, char *hashbuf, const char *fn)
 	}
 }
 
-/*
- * Copy file ownership and permissions from one file to another.
- */
-static void
-copyfileperm(const char *source, const char *target)
-{
-	struct stat stab;
-
-	if (stat(source, &stab) == -1) {
-		if (errno == ENOENT)
-			return;
-		ohshite(_("unable to stat current installed conffile `%.250s'"),
-		        source);
-	}
-
-	if (chown(target, stab.st_uid, stab.st_gid) == -1)
-		ohshite(_("unable to change ownership of new dist conffile `%.250s'"),
-		        target);
-
-	if (chmod(target, (stab.st_mode & 07777)) == -1)
-		ohshite(_("unable to set mode of new dist conffile `%.250s'"),
-		        target);
-}
-
 /*
  * Show a diff between two files.
  */