]> source.dussan.org Git - rspamd.git/commitdiff
[Feature] Allow to read password from console for rspamc
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 4 Jun 2016 19:36:55 +0000 (21:36 +0200)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Sat, 4 Jun 2016 19:36:55 +0000 (21:36 +0200)
Issue: #644
Reported by: @dehnli

src/client/rspamc.c

index 5280914b9ce39b8b4925225a4808bcc200c8ba21..9c00a4bc8e858d17062e186c13210fa42d57b741 100644 (file)
@@ -67,12 +67,17 @@ static GList *children;
     g_queue_push_tail ((o), nh); \
 } while (0)
 
+static gboolean rspamc_password_callback (const gchar *option_name,
+               const gchar *value,
+               gpointer data,
+               GError **error);
+
 static GOptionEntry entries[] =
 {
        { "connect", 'h', 0, G_OPTION_ARG_STRING, &connect_str,
          "Specify host and port", NULL },
-       { "password", 'P', 0, G_OPTION_ARG_STRING, &password,
-         "Specify control password", NULL },
+       { "password", 'P', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
+         &rspamc_password_callback, "Specify control password", NULL },
        { "classifier", 'c', 0, G_OPTION_ARG_STRING, &classifier,
          "Classifier to learn spam or ham", NULL },
        { "weight", 'w', 0, G_OPTION_ARG_INT, &weight,
@@ -292,6 +297,31 @@ struct rspamc_callback_data {
        gdouble start;
 };
 
+gboolean
+rspamc_password_callback (const gchar *option_name,
+               const gchar *value,
+               gpointer data,
+               GError **error)
+{
+       guint plen = 8192;
+
+       if (value != NULL) {
+               password = g_strdup (value);
+       }
+       else {
+               /* Read password from console */
+               password = g_malloc0 (plen);
+               plen = rspamd_read_passphrase (password, plen, 0, NULL);
+       }
+
+       if (plen == 0) {
+               rspamd_fprintf (stderr, "Invalid password\n");
+               exit (EXIT_FAILURE);
+       }
+
+       return TRUE;
+}
+
 /*
  * Parse command line
  */