Update of /cvsroot/monetdb/sql/src/backends/monet5/merovingian
In directory 23jxhf1.ch3.sourceforge.com:/tmp/cvs-serv29836

Modified Files:
        database.c merovingian.c utils.c utils.h 
Log Message:
generate .merovingian_pass with control passphrase

U database.c
Index: database.c
===================================================================
RCS file: /cvsroot/monetdb/sql/src/backends/monet5/merovingian/database.c,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -d -r1.8 -r1.9
--- database.c  10 Sep 2009 08:20:33 -0000      1.8
+++ database.c  22 Sep 2009 19:17:10 -0000      1.9
@@ -29,12 +29,7 @@
 #include <sys/stat.h> /* mkdir, stat, umask */
 #include <sys/types.h> /* mkdir, readdir */
 #include <errno.h>
-
-static char seedChars[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
-       'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
-       'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
-       'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
-       '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};
+#include "utils.h"
 
 /* check if dbname matches [A-Za-z0-9-_]+ */
 char* db_validname(char *dbname) {
@@ -70,7 +65,6 @@
        char buf[8096];
        char path[8096];
        FILE *f;
-       unsigned int size;
 
        if ((e = db_validname(dbname)) != NULL)
                return(e);
@@ -141,22 +135,13 @@
                return(strdup(buf));
        }
        fclose(f);
+
        /* generate a vault key */
-       size = (unsigned int)rand();
-       size = (size % (36 - 20)) + 20;
-       for (c = 0; c < size; c++)
-               buf[c] = seedChars[rand() % 62];
-       for ( ; c < 48; c++)
-               buf[c] = '\0';
        snprintf(path, sizeof(path), "%s/%s/.vaultkey", dbfarm, dbname);
-       f = fopen(path, "w");
-       if (fwrite(buf, 1, 48, f) < 48) {
-               snprintf(buf, sizeof(buf), "cannot write vaultkey: %s",
-                               strerror(errno));
+       if ((e = generatePassphraseFile(path)) != NULL) {
                GDKfree(dbfarm);
-               return(strdup(buf));
+               return(e);
        }
-       fclose(f);
 
        /* without an .uplog file, Merovingian won't work, this
         * needs to be last to avoid race conditions */

U merovingian.c
Index: merovingian.c
===================================================================
RCS file: /cvsroot/monetdb/sql/src/backends/monet5/merovingian/merovingian.c,v
retrieving revision 1.61
retrieving revision 1.62
diff -u -d -r1.61 -r1.62
--- merovingian.c       21 Sep 2009 09:43:39 -0000      1.61
+++ merovingian.c       22 Sep 2009 19:17:10 -0000      1.62
@@ -56,6 +56,7 @@
 #include "properties.h"
 #include "glob.h"
 #include "database.h"
+#include "utils.h"
 #include <stdlib.h> /* exit, getenv, rand, srand */
 #include <stdarg.h>    /* variadic stuff */
 #include <stdio.h> /* fprintf */
@@ -165,6 +166,8 @@
 static struct sockaddr_in _mero_broadcastaddr;
 /* hostname of this machine */
 static char _mero_hostname[128];
+/* control channel passphrase */
+static char _mero_controlpass[128];
 /* full path to logfile for stdout messages, or NULL if tty */
 static str _mero_msglogfile = NULL;
 /* full path to logfile for stderr messages, or NULL if tty */
@@ -420,7 +423,6 @@
        str p, prefix;
        FILE *cnf = NULL, *pidfile = NULL;
        char buf[1024];
-       char lockfile[512];
        sabdb* stats = NULL;
        dpair d;
        int pfd[2];
@@ -433,6 +435,7 @@
        int unsock = -1;
        char doproxy = 1;
        unsigned short discoveryport;
+       unsigned short controlport;
        struct stat sb;
        FILE *oerr = NULL;
        pthread_mutexattr_t mta;
@@ -448,6 +451,7 @@
                {"mero_doproxy",       NULL,                       BOOL},
                {"mero_discoveryttl",  NULL,                       INT},
                {"mero_discoveryport", NULL,                       INT},
+               {"mero_controlport",   NULL,                       INT},
                { NULL,                NULL,                       INVALID}
        };
        confkeyval *kv;
@@ -565,6 +569,16 @@
                }
                discoveryport = (unsigned short)ret;
        }
+       controlport = 0;
+       kv = findConfKey(ckv, "mero_controlport");
+       if (kv && kv->val != NULL) {
+               ret = atoi(kv->val);
+               if (ret < 0 || ret > 65535) {
+                       Mfprintf(stderr, "invalid port number: %s\n", kv->val);
+                       MERO_EXIT(1);
+               }
+               controlport = (unsigned short)ret;
+       }
 
        /* where is the mserver5 binary we fork on demand? */
        snprintf(buf, 1023, "%s/bin/mserver5", prefix);
@@ -630,15 +644,47 @@
                MERO_EXIT(1);
        }
 
+       /* see if we have the passphrase if we do remote control stuff */
+       if (controlport != 0) {
+               struct stat statbuf;
+               FILE *secretf;
+               size_t len;
+
+               if (stat(".merovingian_pass", &statbuf) == -1) {
+                       if ((e = generatePassphraseFile(".merovingian_pass")) 
!= NULL) {
+                               Mfprintf(stderr, "cannot open .merovingian_pass 
for "
+                                               "writing: %s\n", e);
+                               free(e);
+                               MERO_EXIT(1);
+                       }
+               }
+
+               if ((secretf = fopen(".merovingian_pass", "r")) == NULL) {
+                       Mfprintf(stderr, "unable to open .merovingian_pass: 
%s\n",
+                                       strerror(errno));
+                       MERO_EXIT(1);
+               }
+
+               len = fread(_mero_controlpass, 1,
+                               sizeof(_mero_controlpass) - 1, secretf);
+               _mero_controlpass[len] = '\0';
+               len = strlen(_mero_controlpass); /* secret can contain 
null-bytes */
+               if (len == 0) {
+                       Mfprintf(stderr, "control passphrase has 
zero-length\n");
+                       fclose(secretf);
+                       MERO_EXIT(1);
+               }
+               fclose(secretf);
+       }
+
        /* we need a pidfile */
        if (pidfilename == NULL) {
                Mfprintf(stderr, "cannot find pidfilename via config file\n");
                MERO_EXIT(1);
        }
 
-       snprintf(lockfile, 512, "%s/.merovingian_lock", dbfarm);
        /* lock such that we are alone on this world */
-       if ((ret = MT_lockf(lockfile, F_TLOCK, 4, 1)) == -1) {
+       if ((ret = MT_lockf(".merovingian_lock", F_TLOCK, 4, 1)) == -1) {
                /* locking failed */
                Mfprintf(stderr, "another merovingian is already running\n");
                MERO_EXIT(1);
@@ -977,7 +1023,7 @@
        GDKfree(_mero_errlogfile);
 
        /* remove files that suggest our existence */
-       unlink(lockfile);
+       unlink(".merovingian_lock");
        unlink(pidfilename);
        GDKfree(pidfilename);
 

U utils.c
Index: utils.c
===================================================================
RCS file: /cvsroot/monetdb/sql/src/backends/monet5/merovingian/utils.c,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -d -r1.11 -r1.12
--- utils.c     5 Sep 2009 09:43:18 -0000       1.11
+++ utils.c     22 Sep 2009 19:17:10 -0000      1.12
@@ -256,4 +256,33 @@
        }
 }
 
+static char seedChars[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
+       'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
+       'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
+       'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
+       '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'};
+
+char *
+generatePassphraseFile(char *path)
+{
+       unsigned int c;
+       char buf[48];
+       FILE *f;
+       unsigned int size = (unsigned int)rand();
+       size = (size % (36 - 20)) + 20;
+       for (c = 0; c < size; c++)
+               buf[c] = seedChars[rand() % 62];
+       for ( ; c < 48; c++)
+               buf[c] = '\0';
+       f = fopen(path, "w");
+       if (fwrite(buf, 1, 48, f) < 48) {
+               snprintf(buf, sizeof(buf), "cannot write vaultkey: %s",
+                               strerror(errno));
+               fclose(f);
+               return(strdup(buf));
+       }
+       fclose(f);
+       return(NULL);
+}
+
 /* vim:set ts=4 sw=4 noexpandtab: */

U utils.h
Index: utils.h
===================================================================
RCS file: /cvsroot/monetdb/sql/src/backends/monet5/merovingian/utils.h,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -d -r1.7 -r1.8
--- utils.h     4 Sep 2009 12:37:16 -0000       1.7
+++ utils.h     22 Sep 2009 19:17:10 -0000      1.8
@@ -44,6 +44,7 @@
 char *setConfVal(confkeyval *ckv, char *val);
 void secondsToString(char *buf, time_t t, int longness);
 void abbreviateString(char *ret, char *in, size_t width);
+char *generatePassphraseFile(char *path);
 
 #endif
 


------------------------------------------------------------------------------
Come build with us! The BlackBerry&reg; Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay 
ahead of the curve. Join us from November 9&#45;12, 2009. Register now&#33;
http://p.sf.net/sfu/devconf
_______________________________________________
Monetdb-sql-checkins mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/monetdb-sql-checkins

Reply via email to