int curvezmq_keypair_save (curvezmq_keypair_t *self) { assert (self); // Get printable key strings char *public_key = s_key_to_hex (self->public_key); char *secret_key = s_key_to_hex (self->secret_key); // Set process file create mask to owner access only zfile_mode_private (); // The public key file contains just the public key zconfig_t *root = zconfig_new ("root", NULL); zconfig_t *key = zconfig_new ("public-key", root); zconfig_set_value (key, public_key); zconfig_save (root, "public.key"); // The secret key file contains both secret and public keys key = zconfig_new ("secret-key", root); zconfig_set_value (key, secret_key); zconfig_save (root, "secret.key"); zconfig_destroy (&root); // Reset process file create mask zfile_mode_default (); free (public_key); free (secret_key); return 0; }
int zcert_save (zcert_t *self, char *format, ...) { assert (self); assert (format); va_list argptr; va_start (argptr, format); char *filename = zsys_vprintf (format, argptr); va_end (argptr); // Save public certificate using specified filename zcert_save_public (self, filename); // Now save secret certificate using filename with "_secret" suffix s_save_metadata_all (self); zconfig_comment (self->config, " ZeroMQ CURVE **Secret** Certificate"); zconfig_comment (self->config, " DO NOT PROVIDE THIS FILE TO OTHER USERS nor change its permissions."); zconfig_put (self->config, "/curve/public-key", self->public_txt); zconfig_put (self->config, "/curve/secret-key", self->secret_txt); char filename_secret [256]; snprintf (filename_secret, 256, "%s_secret", filename); zsys_file_mode_private (); int rc = zconfig_save (self->config, filename_secret); zsys_file_mode_default (); zstr_free (&filename); return rc; }
void zconfig_test (bool verbose) { printf (" * zconfig: "); // @selftest // Create temporary directory for test files # define TESTDIR ".test_zconfig" zsys_dir_create (TESTDIR); zconfig_t *root = zconfig_new ("root", NULL); zconfig_t *section, *item; section = zconfig_new ("headers", root); item = zconfig_new ("email", section); zconfig_set_value (item, "*****@*****.**"); item = zconfig_new ("name", section); zconfig_set_value (item, "Justin Kayce"); zconfig_put (root, "/curve/secret-key", "Top Secret"); zconfig_set_comment (root, " CURVE certificate"); zconfig_set_comment (root, " -----------------"); assert (zconfig_comments (root)); zconfig_save (root, TESTDIR "/test.cfg"); zconfig_destroy (&root); root = zconfig_load (TESTDIR "/test.cfg"); if (verbose) zconfig_save (root, "-"); char *email = zconfig_resolve (root, "/headers/email", NULL); assert (email); assert (streq (email, "*****@*****.**")); char *passwd = zconfig_resolve (root, "/curve/secret-key", NULL); assert (passwd); assert (streq (passwd, "Top Secret")); zconfig_save (root, TESTDIR "/test.cfg"); zconfig_destroy (&root); // Delete all test files zdir_t *dir = zdir_new (TESTDIR, NULL); zdir_remove (dir, true); zdir_destroy (&dir); // @end printf ("OK\n"); }
JNIEXPORT jint JNICALL Java_zconfig__1_1save (JNIEnv *env, jclass c, jlong self, jstring filename) { char *filename_ = (char *) (*env)->GetStringUTFChars (env, filename, NULL); jint save_ = (jint) zconfig_save ((zconfig_t *) self, filename_); (*env)->ReleaseStringUTFChars (env, filename, filename_); return save_; }
int zconfig_savef (zconfig_t *self, const char *format, ...) { assert (self); va_list argptr; va_start (argptr, format); char *filename = zsys_vprintf (format, argptr); va_end (argptr); if (filename) { int rc = zconfig_save (self, filename); zstr_free (&filename); return rc; } else return -1; }
int zcert_save_secret (zcert_t *self, const char *filename) { assert (self); assert (filename); s_save_metadata_all (self); zconfig_set_comment (self->config, " ZeroMQ CURVE **Secret** Certificate"); zconfig_set_comment (self->config, " DO NOT PROVIDE THIS FILE TO OTHER USERS nor change its permissions."); zconfig_put (self->config, "/curve/public-key", self->public_txt); zconfig_put (self->config, "/curve/secret-key", self->secret_txt); zsys_file_mode_private (); int rc = zconfig_save (self->config, filename); zsys_file_mode_default (); return rc; }
int zcert_save_public (zcert_t *self, const char *filename) { assert (self); assert (filename); s_save_metadata_all (self); zconfig_set_comment (self->config, " ZeroMQ CURVE Public Certificate"); zconfig_set_comment (self->config, " Exchange securely, or use a secure mechanism to verify the contents"); zconfig_set_comment (self->config, " of this file after exchange. Store public certificates in your home"); zconfig_set_comment (self->config, " directory, in the .curve subdirectory."); zconfig_put (self->config, "/curve/public-key", self->public_txt); int rc = zconfig_save (self->config, filename); return rc; }
int zcert_save_public (zcert_t *self, char *format, ...) { assert (self); assert (format); va_list argptr; va_start (argptr, format); char *filename = zsys_vprintf (format, argptr); va_end (argptr); s_save_metadata_all (self); zconfig_comment (self->config, " ZeroMQ CURVE Public Certificate"); zconfig_comment (self->config, " Exchange securely, or use a secure mechanism to verify the contents"); zconfig_comment (self->config, " of this file after exchange. Store public certificates in your home"); zconfig_comment (self->config, " directory, in the .curve subdirectory."); zconfig_put (self->config, "/curve/public-key", self->public_txt); int rc = zconfig_save (self->config, filename); zstr_free (&filename); return rc; }
/// // Save a config tree to a specified ZPL text file, where a filename // "-" means dump to standard output. int QmlZconfig::save (const QString &filename) { return zconfig_save (self, filename.toUtf8().data()); };
void zconfig_test (bool verbose) { printf (" * zconfig: "); // @selftest // Create temporary directory for test files # define TESTDIR ".test_zconfig" zsys_dir_create (TESTDIR); zconfig_t *root = zconfig_new ("root", NULL); assert (root); zconfig_t *section, *item; section = zconfig_new ("headers", root); assert (section); item = zconfig_new ("email", section); assert (item); zconfig_set_value (item, "*****@*****.**"); item = zconfig_new ("name", section); assert (item); zconfig_set_value (item, "Justin Kayce"); zconfig_putf (root, "/curve/secret-key", "%s", "Top Secret"); zconfig_set_comment (root, " CURVE certificate"); zconfig_set_comment (root, " -----------------"); assert (zconfig_comments (root)); zconfig_save (root, TESTDIR "/test.cfg"); zconfig_destroy (&root); root = zconfig_load (TESTDIR "/test.cfg"); if (verbose) zconfig_save (root, "-"); assert (streq (zconfig_filename (root), TESTDIR "/test.cfg")); char *email = zconfig_get (root, "/headers/email", NULL); assert (email); assert (streq (email, "*****@*****.**")); char *passwd = zconfig_get (root, "/curve/secret-key", NULL); assert (passwd); assert (streq (passwd, "Top Secret")); zconfig_savef (root, "%s/%s", TESTDIR, "test.cfg"); assert (!zconfig_has_changed (root)); int rc = zconfig_reload (&root); assert (rc == 0); assert (!zconfig_has_changed (root)); zconfig_destroy (&root); // Test chunk load/save root = zconfig_new ("root", NULL); assert (root); section = zconfig_new ("section", root); assert (section); item = zconfig_new ("value", section); assert (item); zconfig_set_value (item, "somevalue"); zconfig_t *search = zconfig_locate (root, "section/value"); assert (search == item); zchunk_t *chunk = zconfig_chunk_save (root); assert (strlen ((char *) zchunk_data (chunk)) == 32); char *string = zconfig_str_save (root); assert (string); assert (streq (string, (char *) zchunk_data (chunk))); free (string); assert (chunk); zconfig_destroy (&root); root = zconfig_chunk_load (chunk); assert (root); char *value = zconfig_get (root, "/section/value", NULL); assert (value); assert (streq (value, "somevalue")); // Test config can't be saved to a file in a path that doesn't // exist or isn't writable rc = zconfig_savef (root, "%s/path/that/doesnt/exist/%s", TESTDIR, "test.cfg"); assert (rc == -1); zconfig_destroy (&root); zchunk_destroy (&chunk); // Delete all test files zdir_t *dir = zdir_new (TESTDIR, NULL); assert (dir); zdir_remove (dir, true); zdir_destroy (&dir); // @end printf ("OK\n"); }
int main (int argc, char *argv []) { puts (PRODUCT); puts (COPYRIGHT); puts (NOWARRANTY); int argn = 1; bool verbose = false; if (argn < argc && streq (argv [argn], "-h")) { puts ("syntax: hydrad [ directory ]"); puts (" -- defaults to .hydra in current directory"); exit (0); } if (argn < argc && streq (argv [argn], "-v")) { verbose = true; argn++; } // By default, current node runs in .hydra directory; create this if // it's missing (don't create directory passed as argument); char *workdir = ".hydra"; if (argn < argc) workdir = argv [argn++]; else zsys_dir_create (workdir); // ---------------------------------------------------------------------- // This code eventually goes into a reusable hydra actor class // Switch to working directory zsys_info ("hydrad: data store in %s directory", workdir); if (zsys_dir_change (workdir)) { zsys_error ("hydrad: cannot access %s: %s", workdir, strerror (errno)); return 1; } // Check we are the only process currently running here if (zsys_run_as ("hydrad.lock", NULL, NULL)) { zsys_error ("hydrad: cannot start process safely, exiting"); return 1; } // Get node identity from config file, or generate new identity zconfig_t *config = zconfig_load ("hydra.cfg"); if (!config) { // Set defaults for Hydra service config = zconfig_new ("root", NULL); zconfig_put (config, "/server/timeout", "5000"); zconfig_put (config, "/server/background", "0"); zconfig_put (config, "/server/verbose", "0"); } char *identity = zconfig_resolve (config, "/hydra/identity", NULL); if (!identity) { zuuid_t *uuid = zuuid_new (); zconfig_put (config, "/hydra/identity", zuuid_str (uuid)); zconfig_put (config, "/hydra/nickname", "Anonymous"); zconfig_save (config, "hydra.cfg"); zuuid_destroy (&uuid); } // Create store structure, if necessary zsys_dir_create ("content"); zsys_dir_create ("posts"); // Start server and bind to ephemeral TCP port. We can run many // servers on the same box, for testing. zactor_t *server = zactor_new (hydra_server, NULL); if (verbose) zstr_send (server, "VERBOSE"); // Bind Hydra service to ephemeral port and get that port number char *command; int port_nbr; zsock_send (server, "ss", "CONFIGURE", "hydra.cfg"); zsock_send (server, "ss", "BIND", "tcp://*:*"); zsock_send (server, "s", "PORT"); zsock_recv (server, "si", &command, &port_nbr); zsys_info ("hydrad: TCP server started on port=%d", port_nbr); assert (streq (command, "PORT")); free (command); // We're going to use Zyre for discovery and presence, and our own // Hydra protocol for content exchange zyre_t *zyre = zyre_new (NULL); if (verbose) zyre_set_verbose (zyre); char *hostname = zsys_hostname (); char *endpoint = zsys_sprintf ("tcp://%s:%d", hostname, port_nbr); zyre_set_header (zyre, "X-HYDRA", "%s", endpoint); zstr_free (&endpoint); zstr_free (&hostname); if (zyre_start (zyre)) { zsys_info ("hydrad: can't start Zyre discovery service"); zactor_destroy (&server); zyre_destroy (&zyre); return 1; } // When we get a new peer, handle it zpoller_t *poller = zpoller_new (zyre_socket (zyre), NULL); while (!zpoller_terminated (poller)) { void *which = zpoller_wait (poller, -1); if (which == zyre_socket (zyre)) { zyre_event_t *event = zyre_event_new (zyre); if (zyre_event_type (event) == ZYRE_EVENT_ENTER) { zsys_debug ("hydrad: new peer name=%s endpoint=%s", zyre_event_name (event), zyre_event_header (event, "X-HYDRA")); s_handle_peer (zyre_event_header (event, "X-HYDRA"), verbose); } zyre_event_destroy (&event); } else break; } zsys_info ("hydrad: shutting down..."); zpoller_destroy (&poller); // Shutdown all services zactor_destroy (&server); zyre_destroy (&zyre); zconfig_destroy (&config); return 0; }
int main (int argc, char *argv []) { puts (PRODUCT); puts (COPYRIGHT); puts (NOWARRANTY); int argn = 1; bool verbose = false; bool force_foreground = false; if (argc > argn && streq (argv [argn], "-v")) { verbose = true; argn++; } if (argc > argn && streq (argv [argn], "-f")) { force_foreground = true; argn++; } if (argc > argn && streq (argv [argn], "-h")) { puts ("Usage: malamute [ -v ] [ -f ] [ -h | config-file ]"); puts (" Default config-file is 'malamute.cfg'"); return 0; } // Collect configuration file name const char *config_file = "malamute.cfg"; if (argc > argn) { config_file = argv [argn]; argn++; } zsys_init (); // Keep old behavior unless specified otherwise. if (!getenv ("ZSYS_LOGSYSTEM")) { zsys_set_logsystem(true); } zsys_set_pipehwm (0); zsys_set_sndhwm (0); zsys_set_rcvhwm (0); // Load config file for our own use here zsys_info ("loading configuration from '%s'...", config_file); zconfig_t *config = zconfig_load (config_file); if (!config) { zsys_info ("'%s' is missing, creating with defaults:", config_file); config = zconfig_new ("root", NULL); zconfig_put (config, "server/timeout", "5000"); zconfig_put (config, "server/background", "0"); zconfig_put (config, "server/workdir", "."); zconfig_put (config, "server/verbose", "0"); zconfig_put (config, "mlm_server/security/mechanism", "null"); zconfig_put (config, "mlm_server/bind/endpoint", MLM_DEFAULT_ENDPOINT); zconfig_print (config); zconfig_save (config, config_file); } // Do we want to run broker in the background? int as_daemon = !force_foreground && atoi (zconfig_resolve (config, "server/background", "0")); const char *workdir = zconfig_resolve (config, "server/workdir", "."); if (as_daemon) { zsys_info ("switching Malamute to background..."); if (zsys_daemonize (workdir)) return -1; } // Switch to user/group to run process under, if any if (zsys_run_as ( zconfig_resolve (config, "server/lockfile", NULL), zconfig_resolve (config, "server/group", NULL), zconfig_resolve (config, "server/user", NULL))) return -1; // Install authenticator (NULL or PLAIN) zactor_t *auth = zactor_new (zauth, NULL); assert (auth); if (verbose || atoi (zconfig_resolve (config, "server/auth/verbose", "0"))) { zstr_sendx (auth, "VERBOSE", NULL); zsock_wait (auth); } // Do PLAIN password authentication if requested const char *passwords = zconfig_resolve (config, "server/auth/plain", NULL); if (passwords) { zstr_sendx (auth, "PLAIN", passwords, NULL); zsock_wait (auth); } // Start Malamute server instance zactor_t *server = zactor_new (mlm_server, "Malamute"); if (verbose) zstr_send (server, "VERBOSE"); zstr_sendx (server, "LOAD", config_file, NULL); // Accept and print any message back from server while (true) { char *message = zstr_recv (server); if (message) { puts (message); free (message); } else { puts ("interrupted"); break; } } // Shutdown all services zactor_destroy (&server); zactor_destroy (&auth); // Destroy config tree zconfig_destroy (&config); #if defined (__WINDOWS__) zsys_shutdown (); #endif return 0; }
void zconfig_test (bool verbose) { printf (" * zconfig: "); // @selftest const char *SELFTEST_DIR_RW = "src/selftest-rw"; const char *testbasedir = ".test_zconfig"; const char *testfile = "test.cfg"; char *basedirpath = NULL; // subdir in a test, under SELFTEST_DIR_RW char *filepath = NULL; // pathname to testfile in a test, in dirpath basedirpath = zsys_sprintf ("%s/%s", SELFTEST_DIR_RW, testbasedir); assert (basedirpath); filepath = zsys_sprintf ("%s/%s", basedirpath, testfile); assert (filepath); // Make sure old aborted tests do not hinder us zdir_t *dir = zdir_new (basedirpath, NULL); if (dir) { zdir_remove (dir, true); zdir_destroy (&dir); } zsys_file_delete (filepath); zsys_dir_delete (basedirpath); // Create temporary directory for test files zsys_dir_create (basedirpath); zconfig_t *root = zconfig_new ("root", NULL); assert (root); zconfig_t *section, *item; section = zconfig_new ("headers", root); assert (section); item = zconfig_new ("email", section); assert (item); zconfig_set_value (item, "*****@*****.**"); item = zconfig_new ("name", section); assert (item); zconfig_set_value (item, "Justin Kayce"); zconfig_putf (root, "/curve/secret-key", "%s", "Top Secret"); zconfig_set_comment (root, " CURVE certificate"); zconfig_set_comment (root, " -----------------"); assert (zconfig_comments (root)); zconfig_save (root, filepath); zconfig_destroy (&root); root = zconfig_load (filepath); if (verbose) zconfig_save (root, "-"); assert (streq (zconfig_filename (root), filepath)); char *email = zconfig_get (root, "/headers/email", NULL); assert (email); assert (streq (email, "*****@*****.**")); char *passwd = zconfig_get (root, "/curve/secret-key", NULL); assert (passwd); assert (streq (passwd, "Top Secret")); zconfig_savef (root, "%s/%s", basedirpath, testfile); assert (!zconfig_has_changed (root)); int rc = zconfig_reload (&root); assert (rc == 0); assert (!zconfig_has_changed (root)); zconfig_destroy (&root); // Test chunk load/save root = zconfig_new ("root", NULL); assert (root); section = zconfig_new ("section", root); assert (section); item = zconfig_new ("value", section); assert (item); zconfig_set_value (item, "somevalue"); zconfig_t *search = zconfig_locate (root, "section/value"); assert (search == item); zchunk_t *chunk = zconfig_chunk_save (root); assert (strlen ((char *) zchunk_data (chunk)) == 32); char *string = zconfig_str_save (root); assert (string); assert (streq (string, (char *) zchunk_data (chunk))); freen (string); assert (chunk); zconfig_destroy (&root); root = zconfig_chunk_load (chunk); assert (root); char *value = zconfig_get (root, "/section/value", NULL); assert (value); assert (streq (value, "somevalue")); // Test config can't be saved to a file in a path that doesn't // exist or isn't writable rc = zconfig_savef (root, "%s/path/that/doesnt/exist/%s", basedirpath, testfile); assert (rc == -1); zconfig_destroy (&root); zchunk_destroy (&chunk); // Test subtree removal { zconfig_t *root = zconfig_str_load ( "context\n" " iothreads = 1\n" " verbose = 1 # Ask for a trace\n" "main\n" " type = zqueue # ZMQ_DEVICE type\n" " frontend\n" " option\n" " hwm = 1000\n" " swap = 25000000 # 25MB\n" " bind = 'inproc://addr1'\n" " bind = 'ipc://addr2'\n" " backend\n" " bind = inproc://addr3\n" ); zconfig_t *to_delete = zconfig_locate (root, "main/frontend"); assert (to_delete); zconfig_remove (to_delete); char *value = zconfig_get (root, "/main/type", NULL); assert (value); assert (streq (value, "zqueue")); value = zconfig_get (root, "/main/backend/bind", NULL); assert (value); assert (streq (value, "inproc://addr3")); value = zconfig_get (root, "/main/frontend", NULL); assert (value); value = zconfig_get (root, "/main/frontend/option", NULL); assert (value == NULL); value = zconfig_get (root, "/main/frontend/option/swap", NULL); assert (value == NULL); zconfig_destroy (&root); } // Test str_load zconfig_t *config = zconfig_str_load ( "malamute\n" " endpoint = ipc://@/malamute\n" " producer = STREAM\n" " consumer\n" " STREAM2 = .*\n" " STREAM3 = HAM\n" "server\n" " verbose = true\n" ); assert (config); assert (streq (zconfig_get (config, "malamute/endpoint", NULL), "ipc://@/malamute")); assert (streq (zconfig_get (config, "malamute/producer", NULL), "STREAM")); assert (zconfig_locate (config, "malamute/consumer")); zconfig_t *c = zconfig_child (zconfig_locate (config, "malamute/consumer")); assert (c); assert (streq (zconfig_name (c), "STREAM2")); assert (streq (zconfig_value (c), ".*")); c = zconfig_next (c); assert (c); assert (streq (zconfig_name (c), "STREAM3")); assert (streq (zconfig_value (c), "HAM")); c = zconfig_next (c); assert (!c); assert (streq (zconfig_get (config, "server/verbose", NULL), "true")); zconfig_destroy (&config); // Delete all test files dir = zdir_new (basedirpath, NULL); assert (dir); zdir_remove (dir, true); zdir_destroy (&dir); zstr_free (&basedirpath); zstr_free (&filepath); #if defined (__WINDOWS__) zsys_shutdown(); #endif // @end printf ("OK\n"); }