[master] e99742a Prefer /dev/random over /dev/urandom.

Poul-Henning Kamp phk at FreeBSD.org
Wed Apr 15 14:02:59 CEST 2015


commit e99742af53fbec57488722304e5f272e7f4e3471
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Wed Apr 15 11:31:21 2015 +0000

    Prefer /dev/random over /dev/urandom.
    
    Close the connection if authentication fails.
    
    Syslog authentication failures.
    
    Use /dev/[u]random directly to create secret files.  Fail creation
    if none of them are available.
    
    Always use "varnishd" as syslog identity, but append "/" + up to seven
    leading alphanumeric chars from the -i(dent) argument if available.

diff --git a/bin/varnishd/mgt/mgt_cli.c b/bin/varnishd/mgt/mgt_cli.c
index 74980a4..312cf8f 100644
--- a/bin/varnishd/mgt/mgt_cli.c
+++ b/bin/varnishd/mgt/mgt_cli.c
@@ -298,7 +298,9 @@ mcf_auth(struct cli *cli, const char *const *av, void *priv)
 	VCLI_AuthResponse(fd, cli->challenge, buf);
 	AZ(close(fd));
 	if (strcasecmp(buf, av[2])) {
-		mgt_cli_challenge(cli);
+		syslog(LOG_WARNING|LOG_AUTHPRIV,
+		    "CLI Authentication failure from %s", cli->ident);
+		VCLI_SetResult(cli, CLIS_CLOSE);
 		return;
 	}
 	cli->auth = MCF_AUTH;
diff --git a/bin/varnishd/mgt/mgt_main.c b/bin/varnishd/mgt/mgt_main.c
index 9ddaece..91adc0d 100644
--- a/bin/varnishd/mgt/mgt_main.c
+++ b/bin/varnishd/mgt/mgt_main.c
@@ -299,7 +299,9 @@ cli_stdin_close(void *priv)
 	}
 }
 
-/*--------------------------------------------------------------------*/
+/*--------------------------------------------------------------------
+ * Autogenerate a -S file using strong random bits from the kernel.
+ */
 
 static void
 mgt_secret_atexit(void)
@@ -317,25 +319,32 @@ static const char *
 make_secret(const char *dirname)
 {
 	char *fn;
-	int fd;
-	int i;
+	int fdo;
+	int i, j;
 	unsigned char b;
+	int fdi;
 
 	assert(asprintf(&fn, "%s/_.secret", dirname) > 0);
 
 	VJ_master(JAIL_MASTER_FILE);
-	fd = open(fn, O_RDWR|O_CREAT|O_TRUNC, 0640);
-	if (fd < 0) {
-		fprintf(stderr, "Cannot create secret-file in %s (%s)\n",
+	fdo = open(fn, O_RDWR|O_CREAT|O_TRUNC, 0640);
+	if (fdo < 0)
+		ARGV_ERR("Cannot create secret-file in %s (%s)\n",
 		    dirname, strerror(errno));
-		exit(1);
-	}
-	VRND_Seed();
+
+	fdi = open("/dev/random", O_RDONLY);
+	if (fdi < 0)
+		fdi = open("/dev/urandom", O_RDONLY);
+	if (fdi < 0)
+		ARGV_ERR("No /dev/[u]random, cannot autogenerate -S file\n");
+
 	for (i = 0; i < 256; i++) {
-		b = random() & 0xff;
-		assert(1 == write(fd, &b, 1));
+		j = read(fdi, &b, 1);
+		assert(j == 1);
+		assert(1 == write(fdo, &b, 1));
 	}
-	AZ(close(fd));
+	AZ(close(fdi));
+	AZ(close(fdo));
 	VJ_master(JAIL_MASTER_LOW);
 	AZ(atexit(mgt_secret_atexit));
 	return (fn);
@@ -380,6 +389,36 @@ init_params(struct cli *cli)
 	MCF_InitParams(cli);
 }
 
+
+/*--------------------------------------------------------------------*/
+
+static void
+identify(const char *i_arg)
+{
+	char id[17], *p;
+	int i;
+
+	strcpy(id, "varnishd");
+
+	if (i_arg != NULL) {
+		if (strlen(i_arg) + 1 > sizeof heritage.identity)
+			ARGV_ERR("Identity (-i) name too long.\n");
+		strncpy(heritage.identity, i_arg, sizeof heritage.identity);
+		i = strlen(id);
+		id[i++] = '/';
+		for (; i + 1 < sizeof(id); i++) {
+			if (!isalnum(*i_arg))
+				break;
+			id[i] = *i_arg++;
+		}
+		id[i] = '\0';
+	}
+	p = strdup(id);
+	AN(p);
+
+	openlog(p, LOG_PID, LOG_LOCAL0);
+}
+
 /*--------------------------------------------------------------------*/
 
 int
@@ -619,16 +658,7 @@ main(int argc, char * const *argv)
 	if (VIN_N_Arg(n_arg, &heritage.name, &dirname, NULL) != 0)
 		ARGV_ERR("Invalid instance (-n) name: %s\n", strerror(errno));
 
-	if (i_arg != NULL) {
-		if (strlen(i_arg) + 1 > sizeof heritage.identity)
-			ARGV_ERR("Identity (-i) name too long.\n");
-		strncpy(heritage.identity, i_arg, sizeof heritage.identity);
-	}
-
-	if (n_arg != NULL)
-		openlog(n_arg, LOG_PID, LOG_LOCAL0);	/* XXX: i_arg ? */
-	else
-		openlog("varnishd", LOG_PID, LOG_LOCAL0);
+	identify(i_arg);
 
 	VJ_make_workdir(dirname);
 
diff --git a/lib/libvarnish/vrnd.c b/lib/libvarnish/vrnd.c
index 02f3970..7c2cc2c 100644
--- a/lib/libvarnish/vrnd.c
+++ b/lib/libvarnish/vrnd.c
@@ -51,9 +51,9 @@ VRND_Seed(void)
 	int fd;
 	ssize_t sz;
 
-	fd = open("/dev/urandom", O_RDONLY);
+	fd = open("/dev/random", O_RDONLY);
 	if (fd < 0)
-		fd = open("/dev/random", O_RDONLY);
+		fd = open("/dev/urandom", O_RDONLY);
 	if (fd >= 0) {
 		sz = read(fd, &seed, sizeof seed);
 		AZ(close(fd));



More information about the varnish-commit mailing list