>From b043c36eb1b575acc54c971b5eb14020e73d9abd Mon Sep 17 00:00:00 2001 From: Jim Meyering <[email protected]> Date: Fri, 10 Apr 2009 09:02:36 +0200 Subject: [PATCH] corosync-keygen.c: diagnose a few more failures
* tools/corosync-keygen.c (main): Diagnose short reads, failed mkdir and fchmod; detect write failure. Close file descriptors. --- tools/corosync-keygen.c | 26 +++++++++++++++++++------- 1 files changed, 19 insertions(+), 7 deletions(-) diff --git a/tools/corosync-keygen.c b/tools/corosync-keygen.c index 5b70d52..b4e0f24 100644 --- a/tools/corosync-keygen.c +++ b/tools/corosync-keygen.c @@ -6,7 +6,7 @@ * Author: Steven Dake ([email protected]) * * This software licensed under BSD license, the text of which follows: - * + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * @@ -50,13 +50,16 @@ int main (void) { int random_fd; unsigned char key[128]; ssize_t res; - + printf ("Corosync Cluster Engine Authentication key generator.\n"); if (geteuid() != 0) { printf ("Error: Authorization key must be generated as root user.\n"); exit (1); } - mkdir (SYSCONFDIR "/ais", 0700); + if (mkdir (SYSCONFDIR "/ais", 0700)) { + perror ("Failed to create directory: " SYSCONFDIR "/ais"); + exit (1); + } printf ("Gathering %lu bits for key from /dev/random.\n", (unsigned long)(sizeof (key) * 8)); random_fd = open ("/dev/random", O_RDONLY); @@ -69,10 +72,11 @@ int main (void) { * Read random data */ res = read (random_fd, key, sizeof (key)); - if (res == -1) { + if (res != sizeof (key)) { perror ("Could not read /dev/random"); exit (1); } + close (random_fd); /* * Open key @@ -90,7 +94,10 @@ int main (void) { perror ("Could not fchown key to uid 0 and gid 0\n"); exit (1); } - fchmod (authkey_fd, 0400); + if (fchmod (authkey_fd, 0400)) { + perror ("Failed to set key file permissions to 0400\n"); + exit (1); + } printf ("Writing corosync key to " KEYFILE ".\n"); @@ -98,10 +105,15 @@ int main (void) { * Write key */ res = write (authkey_fd, key, sizeof (key)); - if (res == -1) { + if (res != sizeof (key)) { + perror ("Could not write " KEYFILE); + exit (1); + } + + if (close (authkey_fd)) { perror ("Could not write " KEYFILE); exit (1); } - + return (0); } -- 1.6.2.rc1.285.gc5f54 _______________________________________________ Openais mailing list [email protected] https://lists.linux-foundation.org/mailman/listinfo/openais
