All the mail mirrored from lore.kernel.org
 help / color / mirror / Atom feed
* [pull] fixes to todo items etc
@ 2012-03-04 13:35 Sami Kerola
  2012-03-05 12:38 ` Karel Zak
                   ` (2 more replies)
  0 siblings, 3 replies; 29+ messages in thread
From: Sami Kerola @ 2012-03-04 13:35 UTC (permalink / raw
  To: util-linux

Hi,

This pull request is mostly uninteresting. There are perhaps two
changes which are worthwhile to announce. User database editing will
use libc function go acquire exclusive lock at the time of edits. This
should protect util-linux software clashing, which other projects
which are editing same files. The second more notable change is
chkdupexe regression fix.


The following changes since commit 6699e742f238d4bc15ac396dd56f0df1480bd36c:

  libmount: add mnt_fs_streq_target() and export all mnt_fs_streq_*
(2012-03-02 15:53:55 +0100)

are available in the git repository at:

  git://github.com/kerolasa/lelux-utiliteetit.git 2012wk9

for you to fetch changes up to 72544ad04922fad6c164b51262cad56986c12ebd:

  build-sys: fix chkdupexe regression (2012-03-04 14:31:39 +0100)

----------------------------------------------------------------
Sami Kerola (17):
      docs: add deprecation comments
      docs: TODO removal, login-utils error printing
      sfdisk: use rpmatch to yes/no question
      mesg: use rpmatch to yes/no question
      vipw: use rpmatch to yes/no question
      docs: TODO removal, rpmatch task is done
      chfn: use pathnames.h for paths
      chsh: use pathnames.h for paths
      lib: add fileutils function collection
      wall: use xmkstemp for temporary file
      setpwnam: use xmkstemp() and lckpwdf()
      vipw: use xmkstemp() and lckpwdf()
      pathnames: clean up various user database tmp paths
      docs: TODO removal, ldattach usage is done
      include: add asprintf wrapper
      xalloc: use xasprintf in all files
      build-sys: fix chkdupexe regression

 Documentation/TODO             |   11 -----
 configure.ac                   |    3 ++
 disk-utils/elvtune.c           |    9 ++++
 disk-utils/fsck.cramfs.c       |    2 +-
 fdisk/sfdisk.c                 |   19 ++++----
 include/Makefile.am            |    5 +-
 include/fileutils.h            |    6 +++
 include/pathnames.h            |   12 +----
 include/xalloc.h               |   11 +++++
 lib/Makefile.am                |    2 +
 lib/fileutils.c                |   56 ++++++++++++++++++++++
 login-utils/Makefile.am        |    6 ++-
 login-utils/chfn.c             |    4 +-
 login-utils/chsh.c             |   22 ++++-----
 login-utils/last.c             |    9 ++++
 login-utils/newgrp.c           |    9 ++++
 login-utils/setpwnam.c         |   51 +++++++-------------
 login-utils/setpwnam.h         |   39 ++++-----------
 login-utils/vipw.8             |    1 +
 login-utils/vipw.c             |  103 +++++++++++++++++-----------------------
 misc-utils/findmnt.c           |    7 +--
 misc-utils/lsblk.c             |    2 +-
 misc-utils/namei.c             |    2 +-
 partx/partx.c                  |   14 +++---
 sys-utils/arch.c               |    9 ++++
 sys-utils/lscpu.c              |    2 +-
 sys-utils/mount.c              |    2 +-
 sys-utils/prlimit.c            |   12 ++---
 term-utils/Makefile.am         |    2 +-
 term-utils/mesg.c              |   14 ++++--
 term-utils/wall.c              |   19 ++------
 tests/expected/paths/built-in  |    8 ----
 tests/helpers/test_pathnames.c |    8 ----
 text-utils/line.c              |    9 ++++
 34 files changed, 261 insertions(+), 229 deletions(-)
 create mode 100644 include/fileutils.h
 create mode 100644 lib/fileutils.c

-- 
   Sami Kerola
   http://www.iki.fi/kerolasa/

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [pull] fixes to todo items etc
  2012-03-04 13:35 [pull] fixes to todo items etc Sami Kerola
@ 2012-03-05 12:38 ` Karel Zak
  2012-03-05 19:38 ` Sami Kerola
  2012-03-20  8:07 ` [pull] fixes to todo items etc Karel Zak
  2 siblings, 0 replies; 29+ messages in thread
From: Karel Zak @ 2012-03-05 12:38 UTC (permalink / raw
  To: kerolasa; +Cc: util-linux


 Sami,

On Sun, Mar 04, 2012 at 02:35:05PM +0100, Sami Kerola wrote:
> Sami Kerola (17):
>       docs: add deprecation comments
>       docs: TODO removal, login-utils error printing
>       sfdisk: use rpmatch to yes/no question
>       mesg: use rpmatch to yes/no question
>       vipw: use rpmatch to yes/no question
>       docs: TODO removal, rpmatch task is done
>       chfn: use pathnames.h for paths
>       chsh: use pathnames.h for paths

 if you already have file descriptor then use the descriptor for
 fchmod/fchown rather than chmod by filenames.

>       lib: add fileutils function collection
>       wall: use xmkstemp for temporary file
>       setpwnam: use xmkstemp() and lckpwdf()
>       vipw: use xmkstemp() and lckpwdf()

 Please, send all security sensitive changes (and xmkstemp() code)
 to our mailing list for review.

    Karel

-- 
 Karel Zak  <kzak@redhat.com>
 http://karelzak.blogspot.com

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [pull] fixes to todo items etc
  2012-03-04 13:35 [pull] fixes to todo items etc Sami Kerola
  2012-03-05 12:38 ` Karel Zak
@ 2012-03-05 19:38 ` Sami Kerola
  2012-03-05 19:38   ` [PATCH 01/17] docs: add deprecation comments Sami Kerola
                     ` (16 more replies)
  2012-03-20  8:07 ` [pull] fixes to todo items etc Karel Zak
  2 siblings, 17 replies; 29+ messages in thread
From: Sami Kerola @ 2012-03-05 19:38 UTC (permalink / raw
  To: util-linux; +Cc: kerolasa

On Mon, Mar 5, 2012 at 13:38, Karel Zak <kzak@redhat.com> wrote:

Hi Karel et.al.

>  if you already have file descriptor then use the descriptor for
>  fchmod/fchown rather than chmod by filenames.

Fixed to patched following this mail && in my git repository.

>  Please, send all security sensitive changes (and xmkstemp() code)
>  to our mailing list for review.

Good point.  I'll send the whole change set, just in case someone wants to comment other, mostly boring, changes.

-- 
   Sami Kerola
   http://www.iki.fi/kerolasa/


^ permalink raw reply	[flat|nested] 29+ messages in thread

* [PATCH 01/17] docs: add deprecation comments
  2012-03-05 19:38 ` Sami Kerola
@ 2012-03-05 19:38   ` Sami Kerola
  2012-03-05 19:38   ` [PATCH 02/17] docs: TODO removal, login-utils error printing Sami Kerola
                     ` (15 subsequent siblings)
  16 siblings, 0 replies; 29+ messages in thread
From: Sami Kerola @ 2012-03-05 19:38 UTC (permalink / raw
  To: util-linux; +Cc: kerolasa

Markup deprecation to command header to avoid people wasting time in
fixing these utilities.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 disk-utils/elvtune.c |    9 +++++++++
 login-utils/last.c   |    9 +++++++++
 login-utils/newgrp.c |    9 +++++++++
 login-utils/vipw.c   |    9 +++++++++
 sys-utils/arch.c     |    9 +++++++++
 text-utils/line.c    |    9 +++++++++
 6 files changed, 54 insertions(+)

diff --git a/disk-utils/elvtune.c b/disk-utils/elvtune.c
index 7c3caed..7c074ff 100644
--- a/disk-utils/elvtune.c
+++ b/disk-utils/elvtune.c
@@ -18,6 +18,15 @@
  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
+/*
+ * This command is deprecated.  The utility is in maintenance mode,
+ * meaning we keep them in source tree for backward compatibility
+ * only.  Do not waste time making this command better, unless the
+ * fix is about security or other very critical issue.
+ *
+ * See Documentation/deprecated.txt for more information.
+ */
+
 #include <fcntl.h>
 #include <errno.h>
 #include <stdio.h>
diff --git a/login-utils/last.c b/login-utils/last.c
index 734730b..417314e 100644
--- a/login-utils/last.c
+++ b/login-utils/last.c
@@ -27,6 +27,15 @@
   */
 
 /*
+ * This command is deprecated.  The utility is in maintenance mode,
+ * meaning we keep them in source tree for backward compatibility
+ * only.  Do not waste time making this command better, unless the
+ * fix is about security or other very critical issue.
+ *
+ * See Documentation/deprecated.txt for more information.
+ */
+
+/*
  * last
  */
 #include <sys/param.h>
diff --git a/login-utils/newgrp.c b/login-utils/newgrp.c
index 9839934..707c589 100644
--- a/login-utils/newgrp.c
+++ b/login-utils/newgrp.c
@@ -6,6 +6,15 @@
  * - added Native Language Support
  */
 
+/*
+ * This command is deprecated.  The utility is in maintenance mode,
+ * meaning we keep them in source tree for backward compatibility
+ * only.  Do not waste time making this command better, unless the
+ * fix is about security or other very critical issue.
+ *
+ * See Documentation/deprecated.txt for more information.
+ */
+
 #include <errno.h>
 #include <getopt.h>
 #include <grp.h>
diff --git a/login-utils/vipw.c b/login-utils/vipw.c
index 92b4231..c0ed0b3 100644
--- a/login-utils/vipw.c
+++ b/login-utils/vipw.c
@@ -44,6 +44,15 @@
  * - fixed strerr(errno) in gettext calls
  */
 
+/*
+ * This command is deprecated.  The utility is in maintenance mode,
+ * meaning we keep them in source tree for backward compatibility
+ * only.  Do not waste time making this command better, unless the
+ * fix is about security or other very critical issue.
+ *
+ * See Documentation/deprecated.txt for more information.
+ */
+
 #include <errno.h>
 #include <fcntl.h>
 #include <paths.h>
diff --git a/sys-utils/arch.c b/sys-utils/arch.c
index 0c29c3e..470b12b 100644
--- a/sys-utils/arch.c
+++ b/sys-utils/arch.c
@@ -18,6 +18,15 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
+/*
+ * This command is deprecated.  The utility is in maintenance mode,
+ * meaning we keep them in source tree for backward compatibility
+ * only.  Do not waste time making this command better, unless the
+ * fix is about security or other very critical issue.
+ *
+ * See Documentation/deprecated.txt for more information.
+ */
+
 #include <err.h>
 #include <errno.h>
 #include <getopt.h>
diff --git a/text-utils/line.c b/text-utils/line.c
index 826e5a2..636122e 100644
--- a/text-utils/line.c
+++ b/text-utils/line.c
@@ -6,6 +6,15 @@
  * Public Domain.
  */
 
+/*
+ * This command is deprecated.  The utility is in maintenance mode,
+ * meaning we keep them in source tree for backward compatibility
+ * only.  Do not waste time making this command better, unless the
+ * fix is about security or other very critical issue.
+ *
+ * See Documentation/deprecated.txt for more information.
+ */
+
 #include	<stdio.h>
 #include	<unistd.h>
 
-- 
1.7.9.2


^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH 02/17] docs: TODO removal, login-utils error printing
  2012-03-05 19:38 ` Sami Kerola
  2012-03-05 19:38   ` [PATCH 01/17] docs: add deprecation comments Sami Kerola
@ 2012-03-05 19:38   ` Sami Kerola
  2012-03-05 19:38   ` [PATCH 03/17] sfdisk: use rpmatch to yes/no question Sami Kerola
                     ` (14 subsequent siblings)
  16 siblings, 0 replies; 29+ messages in thread
From: Sami Kerola @ 2012-03-05 19:38 UTC (permalink / raw
  To: util-linux; +Cc: kerolasa

Error printing was fixed at November 2010.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 Documentation/TODO |    1 -
 1 file changed, 1 deletion(-)

diff --git a/Documentation/TODO b/Documentation/TODO
index 10980d1..6b8c2cc 100644
--- a/Documentation/TODO
+++ b/Documentation/TODO
@@ -151,7 +151,6 @@ lib/tt.c
 login-utils:
 -----------
 
- - use err() and warn() macros rather than fprintf(stderr, ...)
  - (!) merge newgrp from shadow-utils
  - (!) enable login utils by default
 
-- 
1.7.9.2


^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH 03/17] sfdisk: use rpmatch to yes/no question
  2012-03-05 19:38 ` Sami Kerola
  2012-03-05 19:38   ` [PATCH 01/17] docs: add deprecation comments Sami Kerola
  2012-03-05 19:38   ` [PATCH 02/17] docs: TODO removal, login-utils error printing Sami Kerola
@ 2012-03-05 19:38   ` Sami Kerola
  2012-03-05 19:38   ` [PATCH 04/17] mesg: " Sami Kerola
                     ` (13 subsequent siblings)
  16 siblings, 0 replies; 29+ messages in thread
From: Sami Kerola @ 2012-03-05 19:38 UTC (permalink / raw
  To: util-linux; +Cc: kerolasa

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 fdisk/sfdisk.c |   19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)

diff --git a/fdisk/sfdisk.c b/fdisk/sfdisk.c
index 6267652..f549c64 100644
--- a/fdisk/sfdisk.c
+++ b/fdisk/sfdisk.c
@@ -53,6 +53,7 @@
 #include "gpt.h"
 #include "pathnames.h"
 #include "canonicalize.h"
+#include "rpmatch.h"
 
 /*
  * Table of contents:
@@ -3140,7 +3141,7 @@ do_reread(char *dev) {
 static void
 do_fdisk(char *dev) {
     int fd;
-    int c, answer;
+    char answer[32];
     struct stat statbuf;
     int interactive = isatty(0);
     struct disk_desc *z;
@@ -3197,22 +3198,20 @@ do_fdisk(char *dev) {
 	    else
 		warnx(_("I don't like this - probably you should answer No\n"));
 	}
- ask:
 	if (interactive) {
+ ask:
 	    if (no_write)
+		/* TRANSLATORS: sfdisk uses rpmatch which means the answers y and n
+		 * should be translated, but that is not the case with q answer. */
 		printf(_("Are you satisfied with this? [ynq] "));
 	    else
 		printf(_("Do you want to write this to disk? [ynq] "));
-	    answer = c = getchar();
-	    while (c != '\n' && c != EOF)
-		c = getchar();
-	    if (c == EOF)
-		printf(_("\nsfdisk: premature end of input\n"));
-	    if (c == EOF || answer == 'q' || answer == 'Q') {
+	    fgets(answer, sizeof(answer), stdin);
+	    if (answer[0] == 'q' || answer[0] == 'Q') {
 		errx(EXIT_FAILURE, _("Quitting - nothing changed"));
-	    } else if (answer == 'n' || answer == 'N') {
+	    } else if (rpmatch(answer) == 1) {
 		continue;
-	    } else if (answer == 'y' || answer == 'Y') {
+	    } else if (rpmatch(answer) == 0) {
 		break;
 	    } else {
 		printf(_("Please answer one of y,n,q\n"));
-- 
1.7.9.2


^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH 04/17] mesg: use rpmatch to yes/no question
  2012-03-05 19:38 ` Sami Kerola
                     ` (2 preceding siblings ...)
  2012-03-05 19:38   ` [PATCH 03/17] sfdisk: use rpmatch to yes/no question Sami Kerola
@ 2012-03-05 19:38   ` Sami Kerola
  2012-03-05 19:38   ` [PATCH 05/17] vipw: " Sami Kerola
                     ` (12 subsequent siblings)
  16 siblings, 0 replies; 29+ messages in thread
From: Sami Kerola @ 2012-03-05 19:38 UTC (permalink / raw
  To: util-linux; +Cc: kerolasa

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 term-utils/mesg.c |   14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/term-utils/mesg.c b/term-utils/mesg.c
index 13d4fb7..4fad6f5 100644
--- a/term-utils/mesg.c
+++ b/term-utils/mesg.c
@@ -66,6 +66,8 @@
 static void __attribute__ ((__noreturn__)) usage(FILE * out)
 {
 	fputs(_("\nUsage:\n"), out);
+	/* TRANSLATORS: this program uses for y and n rpmatch(3),
+	 * which means they can be translated.  */
 	fprintf(out,
 	      _(" %s [options] [y | n]\n"), program_invocation_short_name);
 
@@ -127,8 +129,8 @@ int main(int argc, char *argv[])
 		return IS_NOT_ALLOWED;
 	}
 
-	switch (*argv[0]) {
-	case 'y':
+	switch (rpmatch(argv[0])) {
+	case 1:
 #ifdef USE_TTY_GROUP
 		if (chmod(tty, sb.st_mode | S_IWGRP) < 0)
 #else
@@ -138,14 +140,16 @@ int main(int argc, char *argv[])
 		if (verbose)
 			puts(_("write access to your terminal is allowed"));
 		return IS_ALLOWED;
-	case 'n':
+	case 0:
 		if (chmod(tty, sb.st_mode & ~(S_IWGRP|S_IWOTH)) < 0)
 			 err(MESG_EXIT_FAILURE, _("change %s mode failed"), tty);
 		if (verbose)
 			puts(_("write access to your terminal is denied"));
 		return IS_NOT_ALLOWED;
-	default:
-		warnx(_("invalid argument: %c"), *argv[0]);
+        case -1:
+		warnx(_("invalid argument: %s"), argv[0]);
 		usage(stderr);
+        default:
+                abort();
 	}
 }
-- 
1.7.9.2


^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH 05/17] vipw: use rpmatch to yes/no question
  2012-03-05 19:38 ` Sami Kerola
                     ` (3 preceding siblings ...)
  2012-03-05 19:38   ` [PATCH 04/17] mesg: " Sami Kerola
@ 2012-03-05 19:38   ` Sami Kerola
  2012-03-05 19:38   ` [PATCH 06/17] docs: TODO removal, rpmatch task is done Sami Kerola
                     ` (11 subsequent siblings)
  16 siblings, 0 replies; 29+ messages in thread
From: Sami Kerola @ 2012-03-05 19:38 UTC (permalink / raw
  To: util-linux; +Cc: kerolasa

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 login-utils/vipw.c |    5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/login-utils/vipw.c b/login-utils/vipw.c
index c0ed0b3..992a3a3 100644
--- a/login-utils/vipw.c
+++ b/login-utils/vipw.c
@@ -347,11 +347,12 @@ int main(int argc, char *argv[])
 		printf((program == VIGR)
 		       ? _("You are using shadow groups on this system.\n")
 		       : _("You are using shadow passwords on this system.\n"));
+		/* TRANSLATORS: this program uses for y and n rpmatch(3),
+		 * which means they can be translated. */
 		printf(_("Would you like to edit %s now [y/n]? "), orig_file);
 
-		/* EOF means no */
 		if (fgets(response, sizeof(response), stdin)) {
-			if (response[0] == 'y' || response[0] == 'Y')
+			if (rpmatch(response) == 1)
 				edit_file(1);
 		}
 	}
-- 
1.7.9.2


^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH 06/17] docs: TODO removal, rpmatch task is done
  2012-03-05 19:38 ` Sami Kerola
                     ` (4 preceding siblings ...)
  2012-03-05 19:38   ` [PATCH 05/17] vipw: " Sami Kerola
@ 2012-03-05 19:38   ` Sami Kerola
  2012-03-05 19:38   ` [PATCH 07/17] chfn: use pathnames.h for paths Sami Kerola
                     ` (10 subsequent siblings)
  16 siblings, 0 replies; 29+ messages in thread
From: Sami Kerola @ 2012-03-05 19:38 UTC (permalink / raw
  To: util-linux; +Cc: kerolasa

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 Documentation/TODO |    5 -----
 1 file changed, 5 deletions(-)

diff --git a/Documentation/TODO b/Documentation/TODO
index 6b8c2cc..d922d7e 100644
--- a/Documentation/TODO
+++ b/Documentation/TODO
@@ -189,9 +189,6 @@ fdisk(s)
    This feature seems very attractive to users who resizing their disks 
    (for example in virtual machines).
 
- - sfdisk has to use rpmatch() for answers to y/n questions
-   (e.g. "Are you satisfied with this? [ynq]")
-
  - sfdisk rounds to cylinders is -uM (megabyte units) is specified, this is
    pretty stupid feature. It has to round to sectors if -uS or -uM is specified.
 
@@ -227,8 +224,6 @@ misc
  - add mllockall() and SCHED_FIFO to hwclock,
    see http://lkml.org/lkml/2008/10/12/132
  
- - use rpmatch() for all Y/N questions
-
  - (!) rewrite ipcs to use /proc/sys/kernel rather than unreliable syscalls
    (there are problems with 32bit userspace on 64bit kernel)
 
-- 
1.7.9.2


^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH 07/17] chfn: use pathnames.h for paths
  2012-03-05 19:38 ` Sami Kerola
                     ` (5 preceding siblings ...)
  2012-03-05 19:38   ` [PATCH 06/17] docs: TODO removal, rpmatch task is done Sami Kerola
@ 2012-03-05 19:38   ` Sami Kerola
  2012-03-05 19:38   ` [PATCH 08/17] chsh: " Sami Kerola
                     ` (9 subsequent siblings)
  16 siblings, 0 replies; 29+ messages in thread
From: Sami Kerola @ 2012-03-05 19:38 UTC (permalink / raw
  To: util-linux; +Cc: kerolasa

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 login-utils/chfn.c |    4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/login-utils/chfn.c b/login-utils/chfn.c
index 21c6466..2d9a092 100644
--- a/login-utils/chfn.c
+++ b/login-utils/chfn.c
@@ -142,9 +142,9 @@ int main(int argc, char **argv)
 				     oldf.username);
 			}
 		}
-		if (setupDefaultContext("/etc/passwd"))
+		if (setupDefaultContext(_PATH_PASSWD))
 			errx(EXIT_FAILURE,
-			     _("can't set default context for /etc/passwd"));
+			     _("can't set default context for %s"), _PATH_PASSWD);
 	}
 #endif
 
-- 
1.7.9.2


^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH 08/17] chsh: use pathnames.h for paths
  2012-03-05 19:38 ` Sami Kerola
                     ` (6 preceding siblings ...)
  2012-03-05 19:38   ` [PATCH 07/17] chfn: use pathnames.h for paths Sami Kerola
@ 2012-03-05 19:38   ` Sami Kerola
  2012-03-05 19:38   ` [PATCH 09/17] lib: add fileutils function collection Sami Kerola
                     ` (8 subsequent siblings)
  16 siblings, 0 replies; 29+ messages in thread
From: Sami Kerola @ 2012-03-05 19:38 UTC (permalink / raw
  To: util-linux; +Cc: kerolasa

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 include/pathnames.h |    1 +
 login-utils/chsh.c  |   22 +++++++++++-----------
 2 files changed, 12 insertions(+), 11 deletions(-)

diff --git a/include/pathnames.h b/include/pathnames.h
index 3547e8e..6c00d9f 100644
--- a/include/pathnames.h
+++ b/include/pathnames.h
@@ -68,6 +68,7 @@
 #define _PATH_SHADOW_GROUP      "/etc/gshadow"
 #define _PATH_SHADOW_GTMP       "/etc/sgtmp"
 #define _PATH_SHADOW_GTMPTMP    "/etc/sgtmptmp"
+#define _PATH_SHELLS		"/etc/shells"
 
 /* used in term-utils/agetty.c */
 #define _PATH_ISSUE		"/etc/issue"
diff --git a/login-utils/chsh.c b/login-utils/chsh.c
index 5481f7c..4f0615c 100644
--- a/login-utils/chsh.c
+++ b/login-utils/chsh.c
@@ -118,9 +118,9 @@ int main(int argc, char **argv)
 				     pw->pw_name);
 			}
 		}
-		if (setupDefaultContext("/etc/passwd") != 0)
+		if (setupDefaultContext(_PATH_PASSWD) != 0)
 			errx(EXIT_FAILURE,
-			     _("can't set default context for /etc/passwd"));
+			     _("can't set default context for %s"), _PATH_PASSWD);
 	}
 #endif
 
@@ -137,8 +137,8 @@ int main(int argc, char **argv)
 	}
 	if (uid != 0 && !get_shell_list(oldshell)) {
 		errno = EACCES;
-		err(EXIT_FAILURE, _("your shell is not in /etc/shells, "
-				    "shell change denied"));
+		err(EXIT_FAILURE, _("your shell is not in %s, "
+				    "shell change denied"), _PATH_SHELLS);
 	}
 
 	shell = info.shell;
@@ -316,18 +316,18 @@ static int check_shell(char *shell)
 	if (!get_shell_list(shell)) {
 		if (!getuid())
 			warnx(_
-			      ("Warning: \"%s\" is not listed in /etc/shells."),
-			      shell);
+			      ("Warning: \"%s\" is not listed in %s."),
+			      shell, _PATH_SHELLS);
 		else
 			errx(EXIT_FAILURE,
-			     _("\"%s\" is not listed in /etc/shells.\n"
-			       "Use %s -l to see list."), shell,
+			     _("\"%s\" is not listed in %s.\n"
+			       "Use %s -l to see list."), shell, _PATH_SHELLS,
 			     program_invocation_short_name);
 	}
 #else
 	if (!get_shell_list(shell)) {
-		warnx(_("\"%s\" is not listed in /etc/shells.\n"
-			"Use %s -l to see list."), shell,
+		warnx(_("\"%s\" is not listed in %s.\n"
+			"Use %s -l to see list."), shell, _PATH_SHELLS,
 		      program_invocation_short_name);
 	}
 #endif
@@ -347,7 +347,7 @@ static int get_shell_list(char *shell_name)
 	char buf[PATH_MAX];
 
 	found = false;
-	fp = fopen("/etc/shells", "r");
+	fp = fopen(_PATH_SHELLS, "r");
 	if (!fp) {
 		if (!shell_name)
 			warnx(_("No known shells."));
-- 
1.7.9.2


^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH 09/17] lib: add fileutils function collection
  2012-03-05 19:38 ` Sami Kerola
                     ` (7 preceding siblings ...)
  2012-03-05 19:38   ` [PATCH 08/17] chsh: " Sami Kerola
@ 2012-03-05 19:38   ` Sami Kerola
       [not found]     ` <"1330984305.2953.25.camel"@offbook>
                       ` (2 more replies)
  2012-03-05 19:38   ` [PATCH 10/17] wall: use xmkstemp for temporary file Sami Kerola
                     ` (7 subsequent siblings)
  16 siblings, 3 replies; 29+ messages in thread
From: Sami Kerola @ 2012-03-05 19:38 UTC (permalink / raw
  To: util-linux; +Cc: kerolasa

The fileutils contains xmkstemp function will create temporary file
safe and reusable manner.

Reference: http://www.dwheeler.com/secure-programs/Secure-Programs-HOWTO.html#TEMPORARY-FILES
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 include/Makefile.am |    5 +++--
 include/fileutils.h |    6 ++++++
 lib/Makefile.am     |    2 ++
 lib/fileutils.c     |   55 +++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 66 insertions(+), 2 deletions(-)
 create mode 100644 include/fileutils.h
 create mode 100644 lib/fileutils.c

diff --git a/include/Makefile.am b/include/Makefile.am
index 4f5453f..5e4e54e 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -11,6 +11,7 @@ dist_noinst_HEADERS = \
 	crc32.h \
 	env.h \
 	exitcodes.h \
+	fileutils.h \
 	fsprobe.h \
 	ismounted.h \
 	linux_reboot.h \
@@ -38,5 +39,5 @@ dist_noinst_HEADERS = \
 	wholedisk.h \
 	widechar.h \
 	writeall.h \
-	xgetpass.h \
-	xalloc.h
+	xalloc.h \
+	xgetpass.h
diff --git a/include/fileutils.h b/include/fileutils.h
new file mode 100644
index 0000000..27b5661
--- /dev/null
+++ b/include/fileutils.h
@@ -0,0 +1,6 @@
+#ifndef UTIL_LINUX_FILEUTILS
+#define UTIL_LINUX_FILEUTILS
+
+extern FILE * xmkstemp(char **tmpname);
+
+#endif
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 19a00f5..c34481d 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -6,6 +6,7 @@ noinst_PROGRAMS = \
 	test_at \
 	test_blkdev \
 	test_canonicalize \
+	test_fileutils \
 	test_ismounted \
 	test_mangle \
 	test_procutils \
@@ -45,6 +46,7 @@ test_loopdev_SOURCES = \
 test_loopdev_CFLAGS = -DTEST_PROGRAM_LOOPDEV
 endif
 
+test_fileutils_SOURCES = fileutils.c
 test_tt_SOURCES = tt.c $(top_srcdir)/lib/mbsalign.c
 
 test_canonicalize_SOURCES = canonicalize.c
diff --git a/lib/fileutils.c b/lib/fileutils.c
new file mode 100644
index 0000000..b3b7438
--- /dev/null
+++ b/lib/fileutils.c
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2012 Sami Kerola <kerolasa@iki.fi>
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include "c.h"
+#include "pathnames.h"
+
+/* Create open temporary file in safe way.  Please notice that the
+ * file permissions are -rw------- by default. */
+FILE *xmkstemp(char **tmpname)
+{
+	char *localtmp;
+	char *tmpenv;
+	mode_t old_mode;
+	int fd;
+	FILE *ret;
+
+	tmpenv = getenv("TMPDIR");
+	if (tmpenv)
+		asprintf(&localtmp, "%s/%s.XXXXXX", tmpenv,
+			 program_invocation_short_name);
+	else
+		asprintf(&localtmp, "%s/%s.XXXXXX", _PATH_TMP,
+			 program_invocation_short_name);
+	old_mode = umask(077);
+	fd = mkstemp(localtmp);
+	umask(old_mode);
+	if (fd == -1)
+		goto err;
+	if (!(ret = fdopen(fd, "w+")))
+		goto err;
+	*tmpname = localtmp;
+	return ret;
+ err:
+	close(fd);
+	return NULL;
+}
+
+#ifdef TEST_PROGRAM
+int main(void)
+{
+	FILE *f;
+	char *tmpname;
+	f = xmkstemp(&tmpname);
+	unlink(tmpname);
+	free(tmpname);
+	fclose(f);
+	return EXIT_FAILURE;
+}
+#endif
-- 
1.7.9.2


^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH 10/17] wall: use xmkstemp for temporary file
  2012-03-05 19:38 ` Sami Kerola
                     ` (8 preceding siblings ...)
  2012-03-05 19:38   ` [PATCH 09/17] lib: add fileutils function collection Sami Kerola
@ 2012-03-05 19:38   ` Sami Kerola
  2012-03-05 19:38   ` [PATCH 11/17] setpwnam: use xmkstemp() and lckpwdf() Sami Kerola
                     ` (6 subsequent siblings)
  16 siblings, 0 replies; 29+ messages in thread
From: Sami Kerola @ 2012-03-05 19:38 UTC (permalink / raw
  To: util-linux; +Cc: kerolasa

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 term-utils/Makefile.am |    2 +-
 term-utils/wall.c      |   19 +++++--------------
 2 files changed, 6 insertions(+), 15 deletions(-)

diff --git a/term-utils/Makefile.am b/term-utils/Makefile.am
index 49ad5ff..378676f 100644
--- a/term-utils/Makefile.am
+++ b/term-utils/Makefile.am
@@ -51,7 +51,7 @@ endif
 
 if BUILD_WALL
 usrbin_exec_PROGRAMS += wall
-wall_SOURCES = wall.c ttymsg.c ttymsg.h $(top_srcdir)/lib/strutils.c
+wall_SOURCES = wall.c ttymsg.c ttymsg.h $(top_srcdir)/lib/strutils.c $(top_srcdir)/lib/fileutils.c
 dist_man_MANS += wall.1
 wall_CFLAGS = $(SUID_CFLAGS) $(AM_CFLAGS)
 wall_LDFLAGS = $(SUID_LDFLAGS) $(AM_LDFLAGS)
diff --git a/term-utils/wall.c b/term-utils/wall.c
index 7089826..b9f5321 100644
--- a/term-utils/wall.c
+++ b/term-utils/wall.c
@@ -66,6 +66,7 @@
 #include "pathnames.h"
 #include "carefulputc.h"
 #include "c.h"
+#include "fileutils.h"
 
 #define	IGNOREUSER	"sleeper"
 #define WRITE_TIME_OUT	300		/* in seconds */
@@ -188,24 +189,15 @@ makemsg(char *fname, size_t *mbufsize, int print_banner)
 	struct stat sbuf;
 	time_t now;
 	FILE *fp;
-	int fd;
-	char *p, *whom, *where, *hostname, *lbuf, *tmpname, *tmpenv, *mbuf;
+	char *p, *whom, *where, *hostname, *lbuf, *tmpname, *mbuf;
 	long line_max;
 
 	hostname = xmalloc(sysconf(_SC_HOST_NAME_MAX) + 1);
 	line_max = sysconf(_SC_LINE_MAX);
 	lbuf = xmalloc(line_max);
 
-	tmpname = xmalloc(PATH_MAX);
-	tmpenv = getenv("TMPDIR");
-	if ((tmpenv))
-		snprintf(tmpname, PATH_MAX, "%s/%s.XXXXXX", tmpenv,
-			program_invocation_short_name);
-	else
-		snprintf(tmpname, PATH_MAX, "%s/%s.XXXXXX", _PATH_TMP,
-			program_invocation_short_name);
-	if (!(fd = mkstemp(tmpname)) || !(fp = fdopen(fd, "r+")))
-		err(EXIT_FAILURE, _("can't open temporary file %s"), tmpname);
+	if ((fp = xmkstemp(&tmpname)) == NULL)
+		err(EXIT_FAILURE, _("can't open temporary file"));
 	unlink(tmpname);
 	free(tmpname);
 
@@ -280,7 +272,7 @@ makemsg(char *fname, size_t *mbufsize, int print_banner)
 	free(lbuf);
 	rewind(fp);
 
-	if (fstat(fd, &sbuf))
+	if (fstat(fileno(fp), &sbuf))
 		err(EXIT_FAILURE, _("fstat failed"));
 
 	*mbufsize = (size_t) sbuf.st_size;
@@ -289,7 +281,6 @@ makemsg(char *fname, size_t *mbufsize, int print_banner)
 	if (fread(mbuf, 1, *mbufsize, fp) != *mbufsize)
 		err(EXIT_FAILURE, _("fread failed"));
 
-	close(fd);
 	fclose(fp);
 	return mbuf;
 }
-- 
1.7.9.2


^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH 11/17] setpwnam: use xmkstemp() and lckpwdf()
  2012-03-05 19:38 ` Sami Kerola
                     ` (9 preceding siblings ...)
  2012-03-05 19:38   ` [PATCH 10/17] wall: use xmkstemp for temporary file Sami Kerola
@ 2012-03-05 19:38   ` Sami Kerola
  2012-03-05 19:38   ` [PATCH 12/17] vipw: " Sami Kerola
                     ` (5 subsequent siblings)
  16 siblings, 0 replies; 29+ messages in thread
From: Sami Kerola @ 2012-03-05 19:38 UTC (permalink / raw
  To: util-linux; +Cc: kerolasa

Get rid private locking schema and use libc instead.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 login-utils/Makefile.am |    3 ++-
 login-utils/setpwnam.c  |   52 ++++++++++++++++-------------------------------
 2 files changed, 20 insertions(+), 35 deletions(-)

diff --git a/login-utils/Makefile.am b/login-utils/Makefile.am
index 47274bf..7bd2ddb 100644
--- a/login-utils/Makefile.am
+++ b/login-utils/Makefile.am
@@ -42,7 +42,8 @@ chfn_chsh_common = \
 	islocal.h \
 	setpwnam.c \
 	setpwnam.h \
-	$(top_srcdir)/lib/env.c
+	$(top_srcdir)/lib/env.c \
+	$(top_srcdir)/lib/fileutils.c
 login_SOURCES = \
 	login.c \
 	logindefs.c \
diff --git a/login-utils/setpwnam.c b/login-utils/setpwnam.c
index 0e0c047..97fd822 100644
--- a/login-utils/setpwnam.c
+++ b/login-utils/setpwnam.c
@@ -47,6 +47,7 @@
 #include <fcntl.h>
 #include <paths.h>
 #include <pwd.h>
+#include <shadow.h>
 #include <signal.h>
 #include <stdbool.h>
 #include <stdio.h>
@@ -58,6 +59,7 @@
 #include <unistd.h>
 
 #include "c.h"
+#include "fileutils.h"
 #include "setpwnam.h"
 
 static void pw_init(void);
@@ -71,47 +73,26 @@ static void pw_init(void);
 int setpwnam(struct passwd *pwd)
 {
 	FILE *fp = NULL, *pwf = NULL;
-	int x, save_errno, fd, ret;
+	int save_errno;
 	int found;
-	int oldumask;
 	int namelen;
 	int buflen = 256;
 	int contlen, rc;
 	char *linebuf = NULL;
-
-	oldumask = umask(0);	/* Create with exact permissions */
+	char *tmpname = NULL;
 
 	pw_init();
 
-	/* sanity check */
-	for (x = 0; x < 3; x++) {
-		if (x > 0)
-			sleep(1);
-		fd = open(PTMPTMP_FILE, O_WRONLY | O_CREAT | O_EXCL, 0644);
-		if (fd == -1) {
-			umask(oldumask);
-			return -1;
-		}
-		ret = link(PTMPTMP_FILE, PTMP_FILE);
-		unlink(PTMPTMP_FILE);
-		if (ret == -1)
-			close(fd);
-		else
-			break;
-	}
-	umask(oldumask);
-	if (ret == -1)
+	if ((fp = xmkstemp(&tmpname)) == NULL)
 		return -1;
 
 	/* ptmp should be owned by root.root or root.wheel */
-	if (chown(PTMP_FILE, (uid_t) 0, (gid_t) 0) < 0)
-		return -1;
-
-	/* open ptmp for writing and passwd for reading */
-	fp = fdopen(fd, "w");
-	if (!fp)
+	if (fchown(fileno(fp), (uid_t) 0, (gid_t) 0) < 0)
 		goto fail;
 
+	/* acquire exclusive lock */
+	if (lckpwdf() < 0)
+		goto fail;
 	pwf = fopen(PASSWD_FILE, "r");
 	if (!pwf)
 		goto fail;
@@ -164,8 +145,6 @@ int setpwnam(struct passwd *pwd)
 	if (rc < 0)
 		goto fail;
 
-	close(fd);
-	fd = -1;
 	fclose(pwf);	/* I don't think I want to know if this failed */
 	pwf = NULL;
 
@@ -178,22 +157,27 @@ int setpwnam(struct passwd *pwd)
 	unlink(PASSWD_FILE ".OLD");
 	/* we don't care if we can't create the backup file */
 	ignore_result(link(PASSWD_FILE, PASSWD_FILE ".OLD"));
+	/* xmkstemp is too restrictive by default for passwd file */
+	if (fchmod(fileno(fp), 0644) < 0)
+		goto fail;
 	/* we DO care if we can't rename to the passwd file */
-	if (rename(PTMP_FILE, PASSWD_FILE) < 0)
+	if (rename(tmpname, PASSWD_FILE) < 0)
 		goto fail;
 	/* finally:  success */
+	ulckpwdf();
 	return 0;
 
  fail:
 	save_errno = errno;
+	ulckpwdf();
 	if (fp != NULL)
 		fclose(fp);
+	if (tmpname != NULL)
+		unlink(tmpname);
+	free(tmpname);
 	if (pwf != NULL)
 		fclose(pwf);
-	if (fd >= 0)
-		close(fd);
 	free(linebuf);
-	unlink(PTMP_FILE);
 	errno = save_errno;
 	return -1;
 }
-- 
1.7.9.2


^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH 12/17] vipw: use xmkstemp() and lckpwdf()
  2012-03-05 19:38 ` Sami Kerola
                     ` (10 preceding siblings ...)
  2012-03-05 19:38   ` [PATCH 11/17] setpwnam: use xmkstemp() and lckpwdf() Sami Kerola
@ 2012-03-05 19:38   ` Sami Kerola
  2012-03-05 19:38   ` [PATCH 13/17] pathnames: clean up various user database paths Sami Kerola
                     ` (4 subsequent siblings)
  16 siblings, 0 replies; 29+ messages in thread
From: Sami Kerola @ 2012-03-05 19:38 UTC (permalink / raw
  To: util-linux; +Cc: kerolasa

Get rid private locking schema and use libc instead.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 login-utils/Makefile.am |    3 +-
 login-utils/setpwnam.h  |   39 ++++--------------
 login-utils/vipw.8      |    1 +
 login-utils/vipw.c      |  100 ++++++++++++++++++-----------------------------
 4 files changed, 49 insertions(+), 94 deletions(-)

diff --git a/login-utils/Makefile.am b/login-utils/Makefile.am
index 7bd2ddb..7b3b739 100644
--- a/login-utils/Makefile.am
+++ b/login-utils/Makefile.am
@@ -51,7 +51,8 @@ login_SOURCES = \
 	$(top_srcdir)/lib/setproctitle.c
 vipw_SOURCES = \
 	vipw.c \
-	setpwnam.h
+	setpwnam.h \
+	$(top_srcdir)/lib/fileutils.c
 
 chfn_LDADD = $(login_ldadd_common)
 chsh_LDADD = $(login_ldadd_common)
diff --git a/login-utils/setpwnam.h b/login-utils/setpwnam.h
index e7f44a9..6a4dbb7 100644
--- a/login-utils/setpwnam.h
+++ b/login-utils/setpwnam.h
@@ -15,38 +15,15 @@
 #include "pathnames.h"
 
 #ifndef DEBUG
-#define PASSWD_FILE    _PATH_PASSWD
-#define PTMP_FILE      _PATH_PTMP
-#define PTMPTMP_FILE   _PATH_PTMPTMP
-
-#define GROUP_FILE     _PATH_GROUP
-#define GTMP_FILE      _PATH_GTMP
-#define GTMPTMP_FILE   _PATH_GTMPTMP
-
-#define SHADOW_FILE	_PATH_SHADOW_PASSWD
-#define SPTMP_FILE	_PATH_SHADOW_PTMP
-#define SPTMPTMP_FILE	_PATH_SHADOW_PTMPTMP
-
-#define SGROUP_FILE	_PATH_SHADOW_GROUP
-#define SGTMP_FILE	_PATH_SHADOW_GTMP
-#define SGTMPTMP_FILE	_PATH_SHADOW_GTMPTMP
-
+# define PASSWD_FILE	_PATH_PASSWD
+# define GROUP_FILE	_PATH_GROUP
+# define SHADOW_FILE	_PATH_SHADOW_PASSWD
+# define SGROUP_FILE	_PATH_SHADOW_GROUP
 #else
-#define PASSWD_FILE    "/tmp/passwd"
-#define PTMP_FILE      "/tmp/ptmp"
-#define PTMPTMP_FILE   "/tmp/ptmptmp"
-
-#define GROUP_FILE     "/tmp/group"
-#define GTMP_FILE      "/tmp/gtmp"
-#define GTMPTMP_FILE   "/tmp/gtmptmp"
-
-#define SHADOW_FILE	"/tmp/shadow"
-#define SPTMP_FILE	"/tmp/sptmp"
-#define SPTMPTMP_FILE	"/tmp/sptmptmp"
-
-#define SGROUP_FILE	"/tmp/gshadow"
-#define SGTMP_FILE	"/tmp/sgtmp"
-#define SGTMPTMP_FILE	"/tmp/sgtmptmp"
+# define PASSWD_FILE	"/tmp/passwd"
+# define GROUP_FILE	"/tmp/group"
+# define SHADOW_FILE	"/tmp/shadow"
+# define SGROUP_FILE	"/tmp/gshadow"
 #endif
 
 extern int setpwnam (struct passwd *pwd);
diff --git a/login-utils/vipw.8 b/login-utils/vipw.8
index 66c99d1..62dcc85 100644
--- a/login-utils/vipw.8
+++ b/login-utils/vipw.8
@@ -72,6 +72,7 @@ will be invoked instead of the default editor
 .El
 .SH SEE ALSO
 .BR passwd (1),
+.BR flock (2),
 .BR vi (1),
 .BR passwd (5)
 .SH HISTORY
diff --git a/login-utils/vipw.c b/login-utils/vipw.c
index 992a3a3..765d111 100644
--- a/login-utils/vipw.c
+++ b/login-utils/vipw.c
@@ -57,6 +57,7 @@
 #include <fcntl.h>
 #include <paths.h>
 #include <pwd.h>
+#include <shadow.h>
 #include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -71,6 +72,7 @@
 #include <unistd.h>
 
 #include "c.h"
+#include "fileutils.h"
 #include "nls.h"
 #include "setpwnam.h"
 #include "strutils.h"
@@ -88,8 +90,7 @@ enum {
 };
 int program;
 char orig_file[FILENAMELEN];	/* original file /etc/passwd or /etc/group */
-char tmp_file[FILENAMELEN];	/* tmp file */
-char tmptmp_file[FILENAMELEN];	/* very tmp file */
+char *tmp_file;			/* tmp file */
 
 void pw_error __P((char *, int, int));
 
@@ -137,55 +138,22 @@ static void pw_init(void)
 	(void)umask(0);
 }
 
-static int pw_lock(void)
+static FILE * pw_tmpfile(int lockfd)
 {
-	int lockfd, fd, ret;
-
-	/*
-	 * If the password file doesn't exist, the system is hosed.  Might as
-	 * well try to build one.  Set the close-on-exec bit so that users
-	 * can't get at the encrypted passwords while editing.  Open should
-	 * allow flock'ing the file; see 4.4BSD.  XXX
-	 */
-#if 0
-	/* flock()ing is superfluous here, with the ptmp/ptmptmp system. */
-	if (flock(lockfd, LOCK_EX | LOCK_NB)) {
-		if (program == VIPW)
-			err(EXIT_FAILURE, _("cannot lock password file"));
-		else
-			err(EXIT_FAILURE, _("cannot lock group file"));
-	}
-#endif
-
-	if ((fd = open(tmptmp_file, O_WRONLY | O_CREAT, 0600)) == -1)
-		err(EXIT_FAILURE, _("%s: open failed"), tmptmp_file);
-
-	ret = link(tmptmp_file, tmp_file);
-	(void)unlink(tmptmp_file);
-	if (ret == -1) {
-		if (errno == EEXIST)
-			errx(EXIT_FAILURE,
-			     _("the %s file is busy (%s present)"),
-			     program == VIPW ? "password" : "group", tmp_file);
-		else
-			err(EXIT_FAILURE, _("can't link %s"), tmp_file);
-	}
-
-	lockfd = open(orig_file, O_RDONLY, 0);
+	FILE *fd;
+	char *tmpname = NULL;
 
-	if (lockfd < 0) {
-		warn("%s", orig_file);
-		unlink(tmp_file);
-		exit(EXIT_FAILURE);
+	if ((fd = xmkstemp(&tmpname)) == NULL) {
+		ulckpwdf();
+		err(EXIT_FAILURE, _("can't open temporary file"));
 	}
 
-	copyfile(lockfd, fd);
-	(void)close(lockfd);
-	(void)close(fd);
-	return 1;
+	copyfile(lockfd, fileno(fd));
+	tmp_file = tmpname;
+	return fd;
 }
 
-static void pw_unlock(void)
+static void pw_write(void)
 {
 	char tmp[FILENAMELEN + 4];
 
@@ -215,10 +183,11 @@ static void pw_unlock(void)
 	if (rename(tmp_file, orig_file) == -1) {
 		int errsv = errno;
 		errx(EXIT_FAILURE,
-		     ("can't unlock %s: %s (your changes are still in %s)"),
+		     ("cannot write %s: %s (your changes are still in %s)"),
 		     orig_file, strerror(errsv), tmp_file);
 	}
 	unlink(tmp_file);
+	free(tmp_file);
 }
 
 static void pw_edit(int notsetuid)
@@ -275,33 +244,49 @@ void pw_error(char *name, int err, int eval)
 	}
 	warnx(_("%s unchanged"), orig_file);
 	unlink(tmp_file);
+	ulckpwdf();
 	exit(eval);
 }
 
 static void edit_file(int is_shadow)
 {
 	struct stat begin, end;
+	int passwd_file, ch_ret;
+	FILE *tmp_fd;
 
 	pw_init();
-	pw_lock();
 
-	if (stat(tmp_file, &begin))
+	/* acquire exclusive lock */
+	if (lckpwdf() < 0)
+		err(EXIT_FAILURE, _("cannot get lock"));
+
+	passwd_file = open(orig_file, O_RDONLY, 0);
+	if (passwd_file < 0)
+		err(EXIT_FAILURE, "%s: %s", _("cannot open file"), orig_file);
+	tmp_fd = pw_tmpfile(passwd_file);
+
+	if (fstat(fileno(tmp_fd), &begin))
 		pw_error(tmp_file, 1, 1);
 
 	pw_edit(0);
 
-	if (stat(tmp_file, &end))
+	if (fstat(fileno(tmp_fd), &end))
 		pw_error(tmp_file, 1, 1);
 	if (begin.st_mtime == end.st_mtime) {
 		warnx(_("no changes made"));
 		pw_error((char *)NULL, 0, 0);
 	}
-	/* see pw_lock() where we create the file with mode 600 */
+	/* pw_tmpfile() will create the file with mode 600 */
 	if (!is_shadow)
-		chmod(tmp_file, 0644);
+		ch_ret = fchmod(fileno(tmp_fd), 0644);
 	else
-		chmod(tmp_file, 0400);
-	pw_unlock();
+		ch_ret = fchmod(fileno(tmp_fd), 0400);
+	if (ch_ret < 0)
+		err(EXIT_FAILURE, "%s: %s", _("cannot chmod file"), orig_file);
+	fclose(tmp_fd);
+	pw_write();
+	close(passwd_file);
+	ulckpwdf();
 }
 
 int main(int argc, char *argv[])
@@ -310,17 +295,12 @@ int main(int argc, char *argv[])
 	bindtextdomain(PACKAGE, LOCALEDIR);
 	textdomain(PACKAGE);
 
-	memset(tmp_file, '\0', FILENAMELEN);
 	if (!strcmp(program_invocation_short_name, "vigr")) {
 		program = VIGR;
 		xstrncpy(orig_file, GROUP_FILE, sizeof(orig_file));
-		xstrncpy(tmp_file, GTMP_FILE, sizeof(tmp_file));
-		xstrncpy(tmptmp_file, GTMPTMP_FILE, sizeof(tmptmp_file));
 	} else {
 		program = VIPW;
 		xstrncpy(orig_file, PASSWD_FILE, sizeof(orig_file));
-		xstrncpy(tmp_file, PTMP_FILE, sizeof(tmp_file));
-		xstrncpy(tmptmp_file, PTMPTMP_FILE, sizeof(tmptmp_file));
 	}
 
 	if ((argc > 1) &&
@@ -333,12 +313,8 @@ int main(int argc, char *argv[])
 
 	if (program == VIGR) {
 		strncpy(orig_file, SGROUP_FILE, FILENAMELEN - 1);
-		strncpy(tmp_file, SGTMP_FILE, FILENAMELEN - 1);
-		strncpy(tmptmp_file, SGTMPTMP_FILE, FILENAMELEN - 1);
 	} else {
 		strncpy(orig_file, SHADOW_FILE, FILENAMELEN - 1);
-		strncpy(tmp_file, SPTMP_FILE, FILENAMELEN - 1);
-		strncpy(tmptmp_file, SPTMPTMP_FILE, FILENAMELEN - 1);
 	}
 
 	if (access(orig_file, F_OK) == 0) {
-- 
1.7.9.2


^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH 13/17] pathnames: clean up various user database paths
  2012-03-05 19:38 ` Sami Kerola
                     ` (11 preceding siblings ...)
  2012-03-05 19:38   ` [PATCH 12/17] vipw: " Sami Kerola
@ 2012-03-05 19:38   ` Sami Kerola
  2012-03-05 20:44     ` Sami Kerola
  2012-03-05 19:38   ` [PATCH 14/17] docs: TODO removal, ldattach usage is done Sami Kerola
                     ` (3 subsequent siblings)
  16 siblings, 1 reply; 29+ messages in thread
From: Sami Kerola @ 2012-03-05 19:38 UTC (permalink / raw
  To: util-linux; +Cc: kerolasa

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 include/pathnames.h            |   17 ++++-------------
 login-utils/setpwnam.h         |    2 +-
 tests/expected/paths/built-in  |    8 --------
 tests/helpers/test_pathnames.c |    8 --------
 4 files changed, 5 insertions(+), 30 deletions(-)

diff --git a/include/pathnames.h b/include/pathnames.h
index 6c00d9f..70c6ecf 100644
--- a/include/pathnames.h
+++ b/include/pathnames.h
@@ -51,23 +51,14 @@
 /* used in login-utils/shutdown.c */
 
 /* used in login-utils/setpwnam.h and login-utils/islocal.c */
-#define _PATH_PASSWD            "/etc/passwd"
+#define _PATH_PASSWD		"/etc/passwd"
 
-/* used in login-utils/newgrp */
+/* used in login-utils/newgrp and login-utils/setpwnam.h*/
 #define _PATH_GSHADOW		"/etc/gshadow"
 
 /* used in login-utils/setpwnam.h */
-#define _PATH_PTMP              "/etc/ptmp"
-#define _PATH_PTMPTMP           "/etc/ptmptmp"
-#define _PATH_GROUP             "/etc/group"
-#define _PATH_GTMP              "/etc/gtmp"
-#define _PATH_GTMPTMP           "/etc/gtmptmp"
-#define _PATH_SHADOW_PASSWD     "/etc/shadow"
-#define _PATH_SHADOW_PTMP       "/etc/sptmp"
-#define _PATH_SHADOW_PTMPTMP    "/etc/sptmptmp"
-#define _PATH_SHADOW_GROUP      "/etc/gshadow"
-#define _PATH_SHADOW_GTMP       "/etc/sgtmp"
-#define _PATH_SHADOW_GTMPTMP    "/etc/sgtmptmp"
+#define _PATH_GROUP		"/etc/group"
+#define _PATH_SHADOW_PASSWD	"/etc/shadow"
 #define _PATH_SHELLS		"/etc/shells"
 
 /* used in term-utils/agetty.c */
diff --git a/login-utils/setpwnam.h b/login-utils/setpwnam.h
index 6a4dbb7..7d69445 100644
--- a/login-utils/setpwnam.h
+++ b/login-utils/setpwnam.h
@@ -18,7 +18,7 @@
 # define PASSWD_FILE	_PATH_PASSWD
 # define GROUP_FILE	_PATH_GROUP
 # define SHADOW_FILE	_PATH_SHADOW_PASSWD
-# define SGROUP_FILE	_PATH_SHADOW_GROUP
+# define SGROUP_FILE	_PATH_GSHADOW
 #else
 # define PASSWD_FILE	"/tmp/passwd"
 # define GROUP_FILE	"/tmp/group"
diff --git a/tests/expected/paths/built-in b/tests/expected/paths/built-in
index 51372de..836eff5 100644
--- a/tests/expected/paths/built-in
+++ b/tests/expected/paths/built-in
@@ -19,17 +19,9 @@
         _PATH_UMOUNT /bin/umount
         _PATH_PASSWD /etc/passwd
        _PATH_GSHADOW /etc/gshadow
-          _PATH_PTMP /etc/ptmp
-       _PATH_PTMPTMP /etc/ptmptmp
          _PATH_GROUP /etc/group
-          _PATH_GTMP /etc/gtmp
-       _PATH_GTMPTMP /etc/gtmptmp
  _PATH_SHADOW_PASSWD /etc/shadow
-   _PATH_SHADOW_PTMP /etc/sptmp
-_PATH_SHADOW_PTMPTMP /etc/sptmptmp
   _PATH_SHADOW_GROUP /etc/gshadow
-   _PATH_SHADOW_GTMP /etc/sgtmp
-_PATH_SHADOW_GTMPTMP /etc/sgtmptmp
          _PATH_WORDS /usr/share/dict/words
      _PATH_WORDS_ALT /usr/share/dict/web2
         _PATH_UMOUNT /bin/umount
diff --git a/tests/helpers/test_pathnames.c b/tests/helpers/test_pathnames.c
index 0270938..962fe5a 100644
--- a/tests/helpers/test_pathnames.c
+++ b/tests/helpers/test_pathnames.c
@@ -52,17 +52,9 @@ struct hlpPath paths[] =
 	DEF_HLPPATH(_PATH_UMOUNT),
 	DEF_HLPPATH(_PATH_PASSWD),
 	DEF_HLPPATH(_PATH_GSHADOW),
-	DEF_HLPPATH(_PATH_PTMP),
-	DEF_HLPPATH(_PATH_PTMPTMP),
 	DEF_HLPPATH(_PATH_GROUP),
-	DEF_HLPPATH(_PATH_GTMP),
-	DEF_HLPPATH(_PATH_GTMPTMP),
 	DEF_HLPPATH(_PATH_SHADOW_PASSWD),
-	DEF_HLPPATH(_PATH_SHADOW_PTMP),
-	DEF_HLPPATH(_PATH_SHADOW_PTMPTMP),
 	DEF_HLPPATH(_PATH_SHADOW_GROUP),
-	DEF_HLPPATH(_PATH_SHADOW_GTMP),
-	DEF_HLPPATH(_PATH_SHADOW_GTMPTMP),
 	DEF_HLPPATH(_PATH_WORDS),
 	DEF_HLPPATH(_PATH_WORDS_ALT),
 	DEF_HLPPATH(_PATH_UMOUNT),
-- 
1.7.9.2


^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH 14/17] docs: TODO removal, ldattach usage is done
  2012-03-05 19:38 ` Sami Kerola
                     ` (12 preceding siblings ...)
  2012-03-05 19:38   ` [PATCH 13/17] pathnames: clean up various user database paths Sami Kerola
@ 2012-03-05 19:38   ` Sami Kerola
  2012-03-05 19:38   ` [PATCH 15/17] include: add asprintf wrapper Sami Kerola
                     ` (2 subsequent siblings)
  16 siblings, 0 replies; 29+ messages in thread
From: Sami Kerola @ 2012-03-05 19:38 UTC (permalink / raw
  To: util-linux; +Cc: kerolasa

Done at Nov 2011.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 Documentation/TODO |    5 -----
 1 file changed, 5 deletions(-)

diff --git a/Documentation/TODO b/Documentation/TODO
index d922d7e..ed7a0c4 100644
--- a/Documentation/TODO
+++ b/Documentation/TODO
@@ -41,11 +41,6 @@ hwclock
 	ticks)\n\", x * 1e6, x * 1e4}" < /dev/null
 	done
 
-ldattach
---------
-
-  - write usage()
-
 minix (fsck, mkfs)
 ------------------
 
-- 
1.7.9.2


^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH 15/17] include: add asprintf wrapper
  2012-03-05 19:38 ` Sami Kerola
                     ` (13 preceding siblings ...)
  2012-03-05 19:38   ` [PATCH 14/17] docs: TODO removal, ldattach usage is done Sami Kerola
@ 2012-03-05 19:38   ` Sami Kerola
  2012-03-05 19:52     ` Dave Reisner
  2012-03-05 19:38   ` [PATCH 16/17] xalloc: use xasprintf in all files Sami Kerola
  2012-03-05 19:38   ` [PATCH 17/17] build-sys: fix chkdupexe regression Sami Kerola
  16 siblings, 1 reply; 29+ messages in thread
From: Sami Kerola @ 2012-03-05 19:38 UTC (permalink / raw
  To: util-linux; +Cc: kerolasa

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 include/xalloc.h |   11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/include/xalloc.h b/include/xalloc.h
index feeb114..8c22ff7 100644
--- a/include/xalloc.h
+++ b/include/xalloc.h
@@ -63,4 +63,15 @@ static inline char *xstrdup(const char *str)
         return ret;
 }
 
+static inline int xasprintf(char **strp, char *fmt, ...)
+{
+	int ret;
+	va_list args;
+	va_start(args, fmt);
+	ret = vasprintf(&(*strp), fmt, args);
+	va_end(args);
+	if (ret < 0)
+		err(XALLOC_EXIT_CODE, "cannot allocate string");
+	return ret;
+}
 #endif
-- 
1.7.9.2


^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH 16/17] xalloc: use xasprintf in all files
  2012-03-05 19:38 ` Sami Kerola
                     ` (14 preceding siblings ...)
  2012-03-05 19:38   ` [PATCH 15/17] include: add asprintf wrapper Sami Kerola
@ 2012-03-05 19:38   ` Sami Kerola
  2012-03-05 19:38   ` [PATCH 17/17] build-sys: fix chkdupexe regression Sami Kerola
  16 siblings, 0 replies; 29+ messages in thread
From: Sami Kerola @ 2012-03-05 19:38 UTC (permalink / raw
  To: util-linux; +Cc: kerolasa

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 disk-utils/fsck.cramfs.c |    2 +-
 lib/fileutils.c          |    5 +++--
 misc-utils/findmnt.c     |    7 ++++---
 misc-utils/lsblk.c       |    2 +-
 misc-utils/namei.c       |    2 +-
 partx/partx.c            |   14 +++++++-------
 sys-utils/lscpu.c        |    2 +-
 sys-utils/mount.c        |    2 +-
 sys-utils/prlimit.c      |   12 ++++++------
 9 files changed, 25 insertions(+), 23 deletions(-)

diff --git a/disk-utils/fsck.cramfs.c b/disk-utils/fsck.cramfs.c
index eab44f3..465e115 100644
--- a/disk-utils/fsck.cramfs.c
+++ b/disk-utils/fsck.cramfs.c
@@ -530,7 +530,7 @@ static void do_symlink(char *path, struct cramfs_inode *i)
 	if (opt_verbose) {
 		char *str;
 
-		asprintf(&str, "%s -> %s", path, outbuffer);
+		xasprintf(&str, "%s -> %s", path, outbuffer);
 		print_node('l', i, str);
 		if (opt_verbose > 1)
 			printf(_("  uncompressing block at %ld to %ld (%ld)\n"),
diff --git a/lib/fileutils.c b/lib/fileutils.c
index b3b7438..b13db61 100644
--- a/lib/fileutils.c
+++ b/lib/fileutils.c
@@ -9,6 +9,7 @@
 
 #include "c.h"
 #include "pathnames.h"
+#include "xalloc.h"
 
 /* Create open temporary file in safe way.  Please notice that the
  * file permissions are -rw------- by default. */
@@ -22,10 +23,10 @@ FILE *xmkstemp(char **tmpname)
 
 	tmpenv = getenv("TMPDIR");
 	if (tmpenv)
-		asprintf(&localtmp, "%s/%s.XXXXXX", tmpenv,
+		xasprintf(&localtmp, "%s/%s.XXXXXX", tmpenv,
 			 program_invocation_short_name);
 	else
-		asprintf(&localtmp, "%s/%s.XXXXXX", _PATH_TMP,
+		xasprintf(&localtmp, "%s/%s.XXXXXX", _PATH_TMP,
 			 program_invocation_short_name);
 	old_mode = umask(077);
 	fd = mkstemp(localtmp);
diff --git a/misc-utils/findmnt.c b/misc-utils/findmnt.c
index 77897d3..2df79a4 100644
--- a/misc-utils/findmnt.c
+++ b/misc-utils/findmnt.c
@@ -38,6 +38,7 @@
 #include "c.h"
 #include "tt.h"
 #include "strutils.h"
+#include "xalloc.h"
 
 /* flags */
 enum {
@@ -305,7 +306,7 @@ static const char *get_data(struct libmnt_fs *fs, int num)
 		if (root && str && !(flags & FL_NOFSROOT) && strcmp(root, "/")) {
 			char *tmp;
 
-			if (asprintf(&tmp, "%s[%s]", str, root) > 0)
+			if (xasprintf(&tmp, "%s[%s]", str, root) > 0)
 				str = tmp;
 		}
 		break;
@@ -338,10 +339,10 @@ static const char *get_data(struct libmnt_fs *fs, int num)
 			char *tmp;
 			int rc = 0;
 			if ((tt_flags & TT_FL_RAW) || (tt_flags & TT_FL_EXPORT))
-				rc = asprintf(&tmp, "%u:%u",
+				rc = xasprintf(&tmp, "%u:%u",
 					      major(devno), minor(devno));
 			else
-				rc = asprintf(&tmp, "%3u:%-3u",
+				rc = xasprintf(&tmp, "%3u:%-3u",
 					      major(devno), minor(devno));
 			if (rc)
 				str = tmp;
diff --git a/misc-utils/lsblk.c b/misc-utils/lsblk.c
index f33696a..58b0084 100644
--- a/misc-utils/lsblk.c
+++ b/misc-utils/lsblk.c
@@ -566,7 +566,7 @@ static void set_tt_data(struct blkdev_cxt *cxt, int col, int id, struct tt_line
 	case COL_SIZE:
 		if (cxt->size) {
 			if (lsblk->bytes) {
-				if (asprintf(&p, "%jd", cxt->size) < 0)
+				if (xasprintf(&p, "%jd", cxt->size) < 0)
 					p = NULL;
 			} else
 				p = size_to_human_string(SIZE_SUFFIX_1LETTER, cxt->size);
diff --git a/misc-utils/namei.c b/misc-utils/namei.c
index 3c2e7fc..e53b4c9 100644
--- a/misc-utils/namei.c
+++ b/misc-utils/namei.c
@@ -123,7 +123,7 @@ add_id(struct idcache **ic, char *name, unsigned long int id, int *width)
 	/* note, we ignore names with non-printable widechars */
 	if (w > 0)
 		nc->name = xstrdup(name);
-	else if (asprintf(&nc->name, "%lu", id) == -1)
+	else if (xasprintf(&nc->name, "%lu", id) == -1)
 		nc->name = NULL;
 
 	for (x = *ic; x && x->next; x = x->next);
diff --git a/partx/partx.c b/partx/partx.c
index e5f4319..ddce6fe 100644
--- a/partx/partx.c
+++ b/partx/partx.c
@@ -445,25 +445,25 @@ static void add_tt_line(struct tt *tt, blkid_partition par)
 
 		switch (get_column_id(i)) {
 		case COL_PARTNO:
-			rc = asprintf(&str, "%d",
+			rc = xasprintf(&str, "%d",
 					blkid_partition_get_partno(par));
 			break;
 		case COL_START:
-			rc = asprintf(&str, "%ju",
+			rc = xasprintf(&str, "%ju",
 					blkid_partition_get_start(par));
 			break;
 		case COL_END:
-			rc = asprintf(&str, "%ju",
+			rc = xasprintf(&str, "%ju",
 					blkid_partition_get_start(par) +
 					blkid_partition_get_size(par) - 1);
 			break;
 		case COL_SECTORS:
-			rc = asprintf(&str, "%ju",
+			rc = xasprintf(&str, "%ju",
 					blkid_partition_get_size(par));
 			break;
 		case COL_SIZE:
 			if (partx_flags & FL_BYTES)
-				rc = asprintf(&str, "%ju", (uintmax_t)
+				rc = xasprintf(&str, "%ju", (uintmax_t)
 					blkid_partition_get_size(par) << 9);
 			else
 				str = size_to_human_string(SIZE_SUFFIX_1LETTER,
@@ -484,11 +484,11 @@ static void add_tt_line(struct tt *tt, blkid_partition par)
 			if (str)
 				str = xstrdup(str);
 			else
-				rc = asprintf(&str, "0x%x",
+				rc = xasprintf(&str, "0x%x",
 					blkid_partition_get_type(par));
 			break;
 		case COL_FLAGS:
-			rc = asprintf(&str, "0x%llx", blkid_partition_get_flags(par));
+			rc = xasprintf(&str, "0x%llx", blkid_partition_get_flags(par));
 			break;
 		case COL_SCHEME:
 		{
diff --git a/sys-utils/lscpu.c b/sys-utils/lscpu.c
index 3cdd767..6e17b2e 100644
--- a/sys-utils/lscpu.c
+++ b/sys-utils/lscpu.c
@@ -543,7 +543,7 @@ read_hypervisor(struct lscpu_desc *desc)
 			str = strchr(buf, ':');
 			if (!str)
 				continue;
-			if (asprintf(&str, "%s", str + 1) == -1)
+			if (xasprintf(&str, "%s", str + 1) == -1)
 				errx(EXIT_FAILURE, _("failed to allocate memory"));
 			/* remove leading, trailing and repeating whitespace */
 			while (*str == ' ')
diff --git a/sys-utils/mount.c b/sys-utils/mount.c
index 69f536b..b1a0390 100644
--- a/sys-utils/mount.c
+++ b/sys-utils/mount.c
@@ -769,7 +769,7 @@ int main(int argc, char **argv)
 		case 'U':
 			if (source)
 				errx(MOUNT_EX_USAGE, _("only one <source> may be specified"));
-			if (asprintf(&srcbuf, "%s=\"%s\"",
+			if (xasprintf(&srcbuf, "%s=\"%s\"",
 				     c == 'L' ? "LABEL" : "UUID", optarg) <= 0)
 				err(MOUNT_EX_SYSERR, _("failed to allocate source buffer"));
 			source = srcbuf;
diff --git a/sys-utils/prlimit.c b/sys-utils/prlimit.c
index a446187..c45b85e 100644
--- a/sys-utils/prlimit.c
+++ b/sys-utils/prlimit.c
@@ -228,20 +228,20 @@ static void add_tt_line(struct tt *tt, struct prlimit *l)
 
 		switch (get_column_id(i)) {
 		case COL_RES:
-			rc = asprintf(&str, "%s", l->desc->name);
+			rc = xasprintf(&str, "%s", l->desc->name);
 			break;
 		case COL_HELP:
-			rc = asprintf(&str, "%s", l->desc->help);
+			rc = xasprintf(&str, "%s", l->desc->help);
 			break;
 		case COL_SOFT:
 			rc = l->rlim.rlim_cur == RLIM_INFINITY ?
-				asprintf(&str, "%s", "unlimited") :
-				asprintf(&str, "%llu", (unsigned long long) l->rlim.rlim_cur);
+				xasprintf(&str, "%s", "unlimited") :
+				xasprintf(&str, "%llu", (unsigned long long) l->rlim.rlim_cur);
 			break;
 		case COL_HARD:
 			rc = l->rlim.rlim_max == RLIM_INFINITY ?
-				asprintf(&str, "%s", "unlimited") :
-				asprintf(&str, "%llu", (unsigned long long) l->rlim.rlim_max);
+				xasprintf(&str, "%s", "unlimited") :
+				xasprintf(&str, "%llu", (unsigned long long) l->rlim.rlim_max);
 			break;
 		case COL_UNITS:
 			str = l->desc->unit ? xstrdup(_(l->desc->unit)) : NULL;
-- 
1.7.9.2


^ permalink raw reply related	[flat|nested] 29+ messages in thread

* [PATCH 17/17] build-sys: fix chkdupexe regression
  2012-03-05 19:38 ` Sami Kerola
                     ` (15 preceding siblings ...)
  2012-03-05 19:38   ` [PATCH 16/17] xalloc: use xasprintf in all files Sami Kerola
@ 2012-03-05 19:38   ` Sami Kerola
  16 siblings, 0 replies; 29+ messages in thread
From: Sami Kerola @ 2012-03-05 19:38 UTC (permalink / raw
  To: util-linux; +Cc: kerolasa

Commit 2897f50a6a6d1aab653c7017f7542d26ac2a8a0b resulted breaking
shebang substitution for chkdupexe.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 configure.ac |    3 +++
 1 file changed, 3 insertions(+)

diff --git a/configure.ac b/configure.ac
index b40a636..63be8da 100644
--- a/configure.ac
+++ b/configure.ac
@@ -114,6 +114,9 @@ UL_SET_ARCH(M68K, m68*)
 UL_SET_ARCH(MIPS, mips*)
 UL_SET_ARCH(HPPA, hppa*)
 
+dnl script chkdupexe requires perl
+AC_PATH_PROG(PERL, perl)
+
 AC_SYS_LARGEFILE
 
 AM_GNU_GETTEXT_VERSION([0.14.1])
-- 
1.7.9.2


^ permalink raw reply related	[flat|nested] 29+ messages in thread

* Re: [PATCH 15/17] include: add asprintf wrapper
  2012-03-05 19:38   ` [PATCH 15/17] include: add asprintf wrapper Sami Kerola
@ 2012-03-05 19:52     ` Dave Reisner
  2012-03-05 20:47       ` Sami Kerola
  0 siblings, 1 reply; 29+ messages in thread
From: Dave Reisner @ 2012-03-05 19:52 UTC (permalink / raw
  To: Sami Kerola; +Cc: util-linux

On Mon, Mar 05, 2012 at 08:38:52PM +0100, Sami Kerola wrote:
> Signed-off-by: Sami Kerola <kerolasa@iki.fi>
> ---
>  include/xalloc.h |   11 +++++++++++
>  1 file changed, 11 insertions(+)

maybe add xasprintf to tools/checkxalloc.sh as well?

> diff --git a/include/xalloc.h b/include/xalloc.h
> index feeb114..8c22ff7 100644
> --- a/include/xalloc.h
> +++ b/include/xalloc.h
> @@ -63,4 +63,15 @@ static inline char *xstrdup(const char *str)
>          return ret;
>  }
>  
> +static inline int xasprintf(char **strp, char *fmt, ...)
> +{
> +	int ret;
> +	va_list args;
> +	va_start(args, fmt);
> +	ret = vasprintf(&(*strp), fmt, args);
> +	va_end(args);
> +	if (ret < 0)
> +		err(XALLOC_EXIT_CODE, "cannot allocate string");
> +	return ret;
> +}
>  #endif
> -- 
> 1.7.9.2
> 
> --
> To unsubscribe from this list: send the line "unsubscribe util-linux" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH 13/17] pathnames: clean up various user database paths
  2012-03-05 19:38   ` [PATCH 13/17] pathnames: clean up various user database paths Sami Kerola
@ 2012-03-05 20:44     ` Sami Kerola
  0 siblings, 0 replies; 29+ messages in thread
From: Sami Kerola @ 2012-03-05 20:44 UTC (permalink / raw
  To: util-linux

On Mon, Mar 5, 2012 at 20:38, Sami Kerola <kerolasa@iki.fi> wrote:
> Signed-off-by: Sami Kerola <kerolasa@iki.fi>
> ---
>  include/pathnames.h            |   17 ++++-------------
>  login-utils/setpwnam.h         |    2 +-
>  tests/expected/paths/built-in  |    8 --------
>  tests/helpers/test_pathnames.c |    8 --------
>  4 files changed, 5 insertions(+), 30 deletions(-)

The original patch is incomplete. I missed a line removal below. Patch
is fixed in my git.

diff --git a/tests/helpers/test_pathnames.c b/tests/helpers/test_pathnames.c
index 962fe5a..ebd5249 100644
--- a/tests/helpers/test_pathnames.c
+++ b/tests/helpers/test_pathnames.c
@@ -54,7 +54,6 @@ struct hlpPath paths[] =
 	DEF_HLPPATH(_PATH_GSHADOW),
 	DEF_HLPPATH(_PATH_GROUP),
 	DEF_HLPPATH(_PATH_SHADOW_PASSWD),
-	DEF_HLPPATH(_PATH_SHADOW_GROUP),
 	DEF_HLPPATH(_PATH_WORDS),
 	DEF_HLPPATH(_PATH_WORDS_ALT),
 	DEF_HLPPATH(_PATH_UMOUNT),

-- 
   Sami Kerola
   http://www.iki.fi/kerolasa/

^ permalink raw reply related	[flat|nested] 29+ messages in thread

* Re: [PATCH 15/17] include: add asprintf wrapper
  2012-03-05 19:52     ` Dave Reisner
@ 2012-03-05 20:47       ` Sami Kerola
  0 siblings, 0 replies; 29+ messages in thread
From: Sami Kerola @ 2012-03-05 20:47 UTC (permalink / raw
  To: Sami Kerola, util-linux

On Mon, Mar 5, 2012 at 20:52, Dave Reisner <d@falconindy.com> wrote:
> On Mon, Mar 05, 2012 at 08:38:52PM +0100, Sami Kerola wrote:
>> Signed-off-by: Sami Kerola <kerolasa@iki.fi>
>> ---
>>  include/xalloc.h |   11 +++++++++++
>>  1 file changed, 11 insertions(+)
>
> maybe add xasprintf to tools/checkxalloc.sh as well?

Hi Dave,

Great idea.

https://github.com/kerolasa/lelux-utiliteetit/commit/7a63067e36c67f70a6b4cc4394295d72e22c0b5b

And immediately useful.

https://github.com/kerolasa/lelux-utiliteetit/commit/a516dbf53363865f27589bf72e85c91ca0e24094

-- 
   Sami Kerola
   http://www.iki.fi/kerolasa/

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH 09/17] lib: add fileutils function collection
  2012-03-05 19:38   ` [PATCH 09/17] lib: add fileutils function collection Sami Kerola
       [not found]     ` <"1330984305.2953.25.camel"@offbook>
       [not found]     ` <"1 330984305.2953.25.camel"@offbook>
@ 2012-03-05 21:51     ` Davidlohr Bueso
  2012-03-09  7:56       ` Sami Kerola
  2 siblings, 1 reply; 29+ messages in thread
From: Davidlohr Bueso @ 2012-03-05 21:51 UTC (permalink / raw
  To: Sami Kerola; +Cc: util-linux

On Mon, 2012-03-05 at 20:38 +0100, Sami Kerola wrote:
> The fileutils contains xmkstemp function will create temporary file
> safe and reusable manner.
> 
> Reference: http://www.dwheeler.com/secure-programs/Secure-Programs-HOWTO.html#TEMPORARY-FILES
> Signed-off-by: Sami Kerola <kerolasa@iki.fi>
> ---
>  include/Makefile.am |    5 +++--
>  include/fileutils.h |    6 ++++++
>  lib/Makefile.am     |    2 ++
>  lib/fileutils.c     |   55 +++++++++++++++++++++++++++++++++++++++++++++++++++
>  4 files changed, 66 insertions(+), 2 deletions(-)
>  create mode 100644 include/fileutils.h
>  create mode 100644 lib/fileutils.c

Doesn't really make much sense creating two files for just one function.
Couldn't xmkstemp() so somewhere else? You might argue that xgetpass
also does that, I just think it's an overkill...

> 
> diff --git a/include/Makefile.am b/include/Makefile.am
> index 4f5453f..5e4e54e 100644
> --- a/include/Makefile.am
> +++ b/include/Makefile.am
> @@ -11,6 +11,7 @@ dist_noinst_HEADERS = \
>  	crc32.h \
>  	env.h \
>  	exitcodes.h \
> +	fileutils.h \
>  	fsprobe.h \
>  	ismounted.h \
>  	linux_reboot.h \
> @@ -38,5 +39,5 @@ dist_noinst_HEADERS = \
>  	wholedisk.h \
>  	widechar.h \
>  	writeall.h \
> -	xgetpass.h \
> -	xalloc.h
> +	xalloc.h \
> +	xgetpass.h
> diff --git a/include/fileutils.h b/include/fileutils.h
> new file mode 100644
> index 0000000..27b5661
> --- /dev/null
> +++ b/include/fileutils.h
> @@ -0,0 +1,6 @@
> +#ifndef UTIL_LINUX_FILEUTILS
> +#define UTIL_LINUX_FILEUTILS
> +
> +extern FILE * xmkstemp(char **tmpname);
> +
> +#endif
> diff --git a/lib/Makefile.am b/lib/Makefile.am
> index 19a00f5..c34481d 100644
> --- a/lib/Makefile.am
> +++ b/lib/Makefile.am
> @@ -6,6 +6,7 @@ noinst_PROGRAMS = \
>  	test_at \
>  	test_blkdev \
>  	test_canonicalize \
> +	test_fileutils \
>  	test_ismounted \
>  	test_mangle \
>  	test_procutils \
> @@ -45,6 +46,7 @@ test_loopdev_SOURCES = \
>  test_loopdev_CFLAGS = -DTEST_PROGRAM_LOOPDEV
>  endif
>  
> +test_fileutils_SOURCES = fileutils.c
>  test_tt_SOURCES = tt.c $(top_srcdir)/lib/mbsalign.c
>  
>  test_canonicalize_SOURCES = canonicalize.c
> diff --git a/lib/fileutils.c b/lib/fileutils.c
> new file mode 100644
> index 0000000..b3b7438
> --- /dev/null
> +++ b/lib/fileutils.c
> @@ -0,0 +1,55 @@
> +/*
> + * Copyright (C) 2012 Sami Kerola <kerolasa@iki.fi>
> + */
> +
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <sys/stat.h>
> +#include <unistd.h>
> +
> +#include "c.h"
> +#include "pathnames.h"
> +
> +/* Create open temporary file in safe way.  Please notice that the
> + * file permissions are -rw------- by default. */
> +FILE *xmkstemp(char **tmpname)

Returning the file descriptor seems a more flexible interface instead of
the FILE's representation; the user can always use fdopen on his own.

> +{
> +	char *localtmp;
> +	char *tmpenv;
> +	mode_t old_mode;
> +	int fd;
> +	FILE *ret;
> +
> +	tmpenv = getenv("TMPDIR");
> +	if (tmpenv)
> +		asprintf(&localtmp, "%s/%s.XXXXXX", tmpenv,
> +			 program_invocation_short_name);
> +	else
> +		asprintf(&localtmp, "%s/%s.XXXXXX", _PATH_TMP,
> +			 program_invocation_short_name);
> +	old_mode = umask(077);
> +	fd = mkstemp(localtmp);
> +	umask(old_mode);
> +	if (fd == -1)
> +		goto err;

the file isn't open on failure, just return NULL.

> +	if (!(ret = fdopen(fd, "w+")))
> +		goto err;
> +	*tmpname = localtmp;
> +	return ret;
> + err:
> +	close(fd);
> +	return NULL;
> +}
> +
> +#ifdef TEST_PROGRAM
> +int main(void)
> +{
> +	FILE *f;
> +	char *tmpname;
> +	f = xmkstemp(&tmpname);
> +	unlink(tmpname);
> +	free(tmpname);
> +	fclose(f);
> +	return EXIT_FAILURE;
> +}
> +#endif



^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH 09/17] lib: add fileutils function collection
  2012-03-05 21:51     ` Davidlohr Bueso
@ 2012-03-09  7:56       ` Sami Kerola
  2012-03-09 11:48         ` Davidlohr Bueso
  0 siblings, 1 reply; 29+ messages in thread
From: Sami Kerola @ 2012-03-09  7:56 UTC (permalink / raw
  To: dave; +Cc: util-linux

On Mon, Mar 5, 2012 at 22:51, Davidlohr Bueso <dave@gnu.org> wrote:
> On Mon, 2012-03-05 at 20:38 +0100, Sami Kerola wrote:
>>  create mode 100644 include/fileutils.h
>>  create mode 100644 lib/fileutils.c
>
> Doesn't really make much sense creating two files for just one
> function.  Couldn't xmkstemp() so somewhere else?  You might argue
> that xgetpass also does that, I just think it's an overkill...

Hi Davidlohr,

You are right, at least partially.  The reason why I added the files
is simply that non of the existing files felt correct to add this
function.  IMHO xgetpass.c would be more strange file to have
xmkstemp() than fileutils.c to contain xgetpass().

>> +/* Create open temporary file in safe way.  Please notice that the
>> + * file permissions are -rw------- by default. */
>> +FILE *xmkstemp(char **tmpname)
>
> Returning the file descriptor seems a more flexible interface
> instead of the FILE's representation; the user can always use
> fdopen on his own.

If I need temporary file I rather have stream than file descriptor.
Could you give example why flexibility is better than completeness.
With completeness I mean service that the function provides.  If
after every single occurrence of xmkstemp() there would be fdopen() I
would argue adding flexibility did not do anything good.

>> +     fd = mkstemp(localtmp);
>> +     umask(old_mode);
>> +     if (fd == -1)
>> +             goto err;
>
> the file isn't open on failure, just return NULL.

You are right. Fixed in my git.

Thank you for feedback.

-- 
   Sami Kerola
   http://www.iki.fi/kerolasa/

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH 09/17] lib: add fileutils function collection
  2012-03-09  7:56       ` Sami Kerola
@ 2012-03-09 11:48         ` Davidlohr Bueso
  2012-03-09 12:20           ` Karel Zak
  0 siblings, 1 reply; 29+ messages in thread
From: Davidlohr Bueso @ 2012-03-09 11:48 UTC (permalink / raw
  To: kerolasa; +Cc: util-linux

On Fri, 2012-03-09 at 08:56 +0100, Sami Kerola wrote:
> On Mon, Mar 5, 2012 at 22:51, Davidlohr Bueso <dave@gnu.org> wrote:
> > On Mon, 2012-03-05 at 20:38 +0100, Sami Kerola wrote:
> >>  create mode 100644 include/fileutils.h
> >>  create mode 100644 lib/fileutils.c
> >
> > Doesn't really make much sense creating two files for just one
> > function.  Couldn't xmkstemp() so somewhere else?  You might argue
> > that xgetpass also does that, I just think it's an overkill...
> 
> Hi Davidlohr,
> 
> You are right, at least partially.  The reason why I added the files
> is simply that non of the existing files felt correct to add this
> function.  IMHO xgetpass.c would be more strange file to have
> xmkstemp() than fileutils.c to contain xgetpass().

I'm just saying that xgetpass also just contains one function and that
it's an overkill, no big deal in any case.

> 
> >> +/* Create open temporary file in safe way.  Please notice that the
> >> + * file permissions are -rw------- by default. */
> >> +FILE *xmkstemp(char **tmpname)
> >
> > Returning the file descriptor seems a more flexible interface
> > instead of the FILE's representation; the user can always use
> > fdopen on his own.
> 
> If I need temporary file I rather have stream than file descriptor.
> Could you give example why flexibility is better than completeness.
> With completeness I mean service that the function provides.  If
> after every single occurrence of xmkstemp() there would be fdopen() I
> would argue adding flexibility did not do anything good.

I don't see how streams are more complete than descriptors - and no,
every call to mkstemp *does not* require a call to fdopen, that's
completely up to the user's use case.

Cheers,
Davidlohr


^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH 09/17] lib: add fileutils function collection
  2012-03-09 11:48         ` Davidlohr Bueso
@ 2012-03-09 12:20           ` Karel Zak
  2012-03-10 11:49             ` Sami Kerola
  0 siblings, 1 reply; 29+ messages in thread
From: Karel Zak @ 2012-03-09 12:20 UTC (permalink / raw
  To: Davidlohr Bueso; +Cc: kerolasa, util-linux

On Fri, Mar 09, 2012 at 12:48:55PM +0100, Davidlohr Bueso wrote:
> I don't see how streams are more complete than descriptors - and no,
> every call to mkstemp *does not* require a call to fdopen, that's
> completely up to the user's use case.

 Add two functions :-)

    int xmkstemp()
    FILE *xfmkstemp()

 the second function might be an inline function...

    Karel

-- 
 Karel Zak  <kzak@redhat.com>
 http://karelzak.blogspot.com

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [PATCH 09/17] lib: add fileutils function collection
  2012-03-09 12:20           ` Karel Zak
@ 2012-03-10 11:49             ` Sami Kerola
  0 siblings, 0 replies; 29+ messages in thread
From: Sami Kerola @ 2012-03-10 11:49 UTC (permalink / raw
  To: Karel Zak; +Cc: Davidlohr Bueso, util-linux

On Fri, Mar 9, 2012 at 13:20, Karel Zak <kzak@redhat.com> wrote:
> On Fri, Mar 09, 2012 at 12:48:55PM +0100, Davidlohr Bueso wrote:
>> I don't see how streams are more complete than descriptors - and no,
>> every call to mkstemp *does not* require a call to fdopen, that's
>> completely up to the user's use case.
>
>  Add two functions :-)
>
>    int xmkstemp()
>    FILE *xfmkstemp()
>
>  the second function might be an inline function...

Hi Karel & Davidlohr,

Done the suggested way.

https://github.com/kerolasa/lelux-utiliteetit/commit/1d2594dde3de6bf4e23a284c08dc4c8cb7c86a6f

BTW I noticed mistake in earlier change, which made file descriptors
to be preferred way of changing permissions etc. The issue was that fd
was referred after closure. That is now fixed in commit below.

https://github.com/kerolasa/lelux-utiliteetit/commit/58681bfe79ed03d2b3c39f81b14cdb4723a51b63

Assuming no more comments arrive the week 9 patches might be ready for
final maintainer review hopefully resulting to merge.

-- 
   Sami Kerola
   http://www.iki.fi/kerolasa/

^ permalink raw reply	[flat|nested] 29+ messages in thread

* Re: [pull] fixes to todo items etc
  2012-03-04 13:35 [pull] fixes to todo items etc Sami Kerola
  2012-03-05 12:38 ` Karel Zak
  2012-03-05 19:38 ` Sami Kerola
@ 2012-03-20  8:07 ` Karel Zak
  2 siblings, 0 replies; 29+ messages in thread
From: Karel Zak @ 2012-03-20  8:07 UTC (permalink / raw
  To: kerolasa; +Cc: util-linux

On Sun, Mar 04, 2012 at 02:35:05PM +0100, Sami Kerola wrote:
> are available in the git repository at:
> 
>   git://github.com/kerolasa/lelux-utiliteetit.git 2012wk9

 Merged. Thanks.

-- 
 Karel Zak  <kzak@redhat.com>
 http://karelzak.blogspot.com

^ permalink raw reply	[flat|nested] 29+ messages in thread

end of thread, other threads:[~2012-03-20  8:07 UTC | newest]

Thread overview: 29+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-03-04 13:35 [pull] fixes to todo items etc Sami Kerola
2012-03-05 12:38 ` Karel Zak
2012-03-05 19:38 ` Sami Kerola
2012-03-05 19:38   ` [PATCH 01/17] docs: add deprecation comments Sami Kerola
2012-03-05 19:38   ` [PATCH 02/17] docs: TODO removal, login-utils error printing Sami Kerola
2012-03-05 19:38   ` [PATCH 03/17] sfdisk: use rpmatch to yes/no question Sami Kerola
2012-03-05 19:38   ` [PATCH 04/17] mesg: " Sami Kerola
2012-03-05 19:38   ` [PATCH 05/17] vipw: " Sami Kerola
2012-03-05 19:38   ` [PATCH 06/17] docs: TODO removal, rpmatch task is done Sami Kerola
2012-03-05 19:38   ` [PATCH 07/17] chfn: use pathnames.h for paths Sami Kerola
2012-03-05 19:38   ` [PATCH 08/17] chsh: " Sami Kerola
2012-03-05 19:38   ` [PATCH 09/17] lib: add fileutils function collection Sami Kerola
     [not found]     ` <"1330984305.2953.25.camel"@offbook>
     [not found]     ` <"1 330984305.2953.25.camel"@offbook>
2012-03-05 21:51     ` Davidlohr Bueso
2012-03-09  7:56       ` Sami Kerola
2012-03-09 11:48         ` Davidlohr Bueso
2012-03-09 12:20           ` Karel Zak
2012-03-10 11:49             ` Sami Kerola
2012-03-05 19:38   ` [PATCH 10/17] wall: use xmkstemp for temporary file Sami Kerola
2012-03-05 19:38   ` [PATCH 11/17] setpwnam: use xmkstemp() and lckpwdf() Sami Kerola
2012-03-05 19:38   ` [PATCH 12/17] vipw: " Sami Kerola
2012-03-05 19:38   ` [PATCH 13/17] pathnames: clean up various user database paths Sami Kerola
2012-03-05 20:44     ` Sami Kerola
2012-03-05 19:38   ` [PATCH 14/17] docs: TODO removal, ldattach usage is done Sami Kerola
2012-03-05 19:38   ` [PATCH 15/17] include: add asprintf wrapper Sami Kerola
2012-03-05 19:52     ` Dave Reisner
2012-03-05 20:47       ` Sami Kerola
2012-03-05 19:38   ` [PATCH 16/17] xalloc: use xasprintf in all files Sami Kerola
2012-03-05 19:38   ` [PATCH 17/17] build-sys: fix chkdupexe regression Sami Kerola
2012-03-20  8:07 ` [pull] fixes to todo items etc Karel Zak

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.