[PATCH] Enable command line authentication for gnomevfs-foo utilities
- From: Christian Neumair <chris gnome-de org>
- To: gnome-vfs-list gnome org
- Subject: [PATCH] Enable command line authentication for gnomevfs-foo utilities
- Date: Wed, 22 Mar 2006 16:19:09 +0100
The attached patch implements command line authentication for the
gnome-vfs programs which makes them more useful forgno admins, because
they don't have to remove passwords from their command line histories
anymore.
--
Christian Neumair <chris gnome-de org>
Index: programs/Makefile.am
===================================================================
RCS file: /cvs/gnome/gnome-vfs/programs/Makefile.am,v
retrieving revision 1.4
diff -u -p -r1.4 Makefile.am
--- programs/Makefile.am 11 Apr 2005 13:52:31 -0000 1.4
+++ programs/Makefile.am 22 Mar 2006 15:14:15 -0000
@@ -48,3 +48,4 @@ gnomevfs_mv_LDADD = $(libraries)
gnomevfs_rm_SOURCES = gnomevfs-rm.c
gnomevfs_rm_LDADD = $(libraries)
+EXTRA_DIST = authentication.c
Index: programs/authentication.c
===================================================================
RCS file: programs/authentication.c
diff -N programs/authentication.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ programs/authentication.c 22 Mar 2006 15:14:15 -0000
@@ -0,0 +1,262 @@
+#include <config.h>
+/*
+#include "gnome-authentication-manager.h"
+#include "gnome-authentication-manager-private.h"
+*/
+
+#include <libgnomevfs/gnome-vfs-module-callback.h>
+#include <libgnomevfs/gnome-vfs-standard-callbacks.h>
+#include <libgnomevfs/gnome-vfs-utils.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <termios.h>
+#include <unistd.h>
+
+
+#if 0
+#define DEBUG_MSG(x) printf x
+#else
+#define DEBUG_MSG(x)
+#endif
+
+#define _(x) x
+
+#define FGETS_NO_NEWLINE(buffer,size,fd) \
+ fgets (buffer, size, fd); \
+ if (strlen (buffer) > 0 && \
+ buffer[strlen (buffer) - 1] == '\n') \
+ buffer[strlen (buffer) - 1] = '\0';
+
+static char *
+ask_for_password (void)
+{
+ char buffer[BUFSIZ];
+ int old_flags;
+ struct termios term_attr;
+ char *ret;
+
+ ret = NULL;
+
+ if (tcgetattr(STDIN_FILENO, &term_attr) == 0) {
+ old_flags = term_attr.c_lflag;
+ term_attr.c_lflag &= ~ECHO;
+ if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &term_attr) == 0) {
+ FGETS_NO_NEWLINE (buffer, BUFSIZ, stdin);
+ ret = g_strdup (buffer);
+ printf ("\n");
+ } else
+ fprintf (stderr, "tcsetattr() failed, skipping password.\n");
+
+
+ term_attr.c_lflag = old_flags;
+ if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &term_attr) != 0)
+ fprintf (stderr, "tcsetattr() failed.\n");
+ }
+ else
+ fprintf (stderr, "tcgetattr() failed, skipping password.\n");
+
+ return ret;
+}
+
+static int
+ask_question (char *prim_msg, char *sec_msg, char **choices)
+{
+ int i, ans;
+ char buffer[BUFSIZ];
+
+ if (prim_msg && sec_msg)
+ printf ("%s\n%s", prim_msg, sec_msg);
+ else if (prim_msg || sec_msg);
+ printf ("%s", prim_msg != NULL ? prim_msg : sec_msg);
+
+ do {
+ printf ("\n");
+ for (i = 0; choices[i] != NULL; i++) {
+ printf (" %d\t%s\n", (i + 1), choices[i]);
+ }
+
+ FGETS_NO_NEWLINE (buffer, BUFSIZ, stdin);
+ ans = (int) strtol (buffer, NULL, 10);
+ } while (ans < 1 || ans > i);
+
+ return ans;
+}
+
+static void
+do_auth (gboolean is_proxy_authentication,
+ const GnomeVFSModuleCallbackAuthenticationIn *in_args,
+ GnomeVFSModuleCallbackAuthenticationOut *out_args)
+{
+ char buffer[BUFSIZ];
+
+ if (!isatty (STDOUT_FILENO)) {
+ out_args->username = NULL;
+ return;
+ }
+
+ printf (is_proxy_authentication
+ ? _("Your HTTP Proxy requires you to log in.\n")
+ : _("You must log in to access \"%s\".\n%s"),
+ in_args->uri,
+ in_args->auth_type == AuthTypeBasic
+ ? _("Your password will be transmitted unencrypted.")
+ : _("Your password will be transmitted encrypted."));
+
+ printf ("Username: ");
+ FGETS_NO_NEWLINE (buffer, BUFSIZ, stdin);
+ out_args->username = g_strdup (buffer);
+
+ printf ("Password: ");
+ out_args->password = ask_for_password ();
+}
+
+static void /* GnomeVFSModuleCallback */
+vfs_authentication_callback (gconstpointer in, size_t in_size,
+ gpointer out, size_t out_size,
+ gpointer user_data)
+{
+ gboolean is_proxy_authentication;
+
+ is_proxy_authentication = (user_data == GINT_TO_POINTER (1));
+ do_auth (is_proxy_authentication, in, out);
+}
+
+static void
+do_full_auth (const GnomeVFSModuleCallbackFullAuthenticationIn *in_args,
+ GnomeVFSModuleCallbackFullAuthenticationOut *out_args)
+{
+ char buffer[BUFSIZ];
+ char *message;
+ GString *name;
+
+ if (!isatty (STDOUT_FILENO)) {
+ out_args->username = NULL;
+ out_args->abort_auth = TRUE;
+ return;
+ }
+
+ out_args->abort_auth = FALSE;
+
+ name = g_string_new (NULL);
+ if (in_args->username != NULL) {
+ g_string_append_printf (name, "%s@", in_args->username);
+ }
+ if (in_args->server != NULL) {
+ g_string_append (name, in_args->server);
+ }
+ if (in_args->port != 0) {
+ g_string_append_printf (name, ":%d", in_args->port);
+ }
+ if (in_args->object != NULL) {
+ g_string_append_printf (name, "/%s", in_args->object);
+ }
+ if (in_args->domain != NULL) {
+ message = g_strdup_printf (_("You must log in to access %s domain %s\n"), name->str, in_args->domain);
+ } else {
+ message = g_strdup_printf (_("You must log in to access %s\n"), name->str);
+ }
+
+ g_string_free (name, TRUE);
+
+ printf (message);
+ g_free (message);
+
+ if (in_args->flags & GNOME_VFS_MODULE_CALLBACK_FULL_AUTHENTICATION_ANON_SUPPORTED) {
+ char *answers[] = { "Yes", "No", NULL };
+ if (ask_question ("Login anonymously?", NULL, answers) == 1)
+ out_args->out_flags |= GNOME_VFS_MODULE_CALLBACK_FULL_AUTHENTICATION_OUT_ANON_SELECTED;
+ }
+
+ if ((out_args->out_flags & GNOME_VFS_MODULE_CALLBACK_FULL_AUTHENTICATION_OUT_ANON_SELECTED) == 0) {
+ if (in_args->flags & GNOME_VFS_MODULE_CALLBACK_FULL_AUTHENTICATION_NEED_USERNAME) {
+ if (in_args->default_user != NULL) {
+ printf ("Username (default \"%s\"): ", in_args->default_user);
+ } else {
+ printf ("Username: ");
+ }
+
+ FGETS_NO_NEWLINE (buffer, BUFSIZ, stdin);
+ out_args->username = g_strdup (buffer);
+ }
+
+ if (in_args->flags & GNOME_VFS_MODULE_CALLBACK_FULL_AUTHENTICATION_NEED_DOMAIN) {
+ if (in_args->default_domain != NULL) {
+ printf ("Domain (default \"%s\"): ", in_args->default_domain);
+ } else {
+ printf ("Domain: ");
+ }
+
+ FGETS_NO_NEWLINE (buffer, BUFSIZ, stdin);
+ out_args->domain = g_strdup (buffer);
+ }
+
+ if (in_args->flags & GNOME_VFS_MODULE_CALLBACK_FULL_AUTHENTICATION_NEED_PASSWORD) {
+ printf ("Password: ");
+ out_args->password = ask_for_password ();
+ }
+ }
+
+ if ((out_args->username == NULL ||
+ strlen (out_args->username) == 0)
+ && in_args->username != NULL) {
+ g_free (out_args->username);
+ out_args->username = g_strdup (in_args->username);
+ }
+
+ if ((out_args->domain == NULL ||
+ strlen (out_args->domain) == 0)
+ && in_args->domain != NULL) {
+ g_free (out_args->domain);
+ out_args->domain = g_strdup (in_args->domain);
+ }
+
+ /* TODO support saving password? */
+ out_args->save_password = FALSE;
+ out_args->keyring = NULL;
+}
+
+static void /* GnomeVFSModuleCallback */
+vfs_full_authentication_callback (gconstpointer in, size_t in_size,
+ gpointer out, size_t out_size,
+ gpointer user_data)
+{
+ do_full_auth (in, out);
+}
+
+static void /* GnomeVFSModuleCallback */
+vfs_question_callback (gconstpointer in, size_t in_size,
+ gpointer out, size_t out_size,
+ gpointer user_data)
+{
+ GnomeVFSModuleCallbackQuestionIn *in_real;
+ GnomeVFSModuleCallbackQuestionOut *out_real;
+
+ in_real = (GnomeVFSModuleCallbackQuestionIn *)in;
+ out_real = (GnomeVFSModuleCallbackQuestionOut *)out;
+
+ out_real->answer = ask_question (in_real->primary_message,
+ in_real->secondary_message,
+ in_real->choices);
+}
+
+static void
+command_line_authentication_init (void)
+{
+ gnome_vfs_module_callback_set_default (GNOME_VFS_MODULE_CALLBACK_AUTHENTICATION,
+ vfs_authentication_callback,
+ GINT_TO_POINTER (0),
+ NULL);
+ gnome_vfs_module_callback_set_default (GNOME_VFS_MODULE_CALLBACK_HTTP_PROXY_AUTHENTICATION,
+ vfs_authentication_callback,
+ GINT_TO_POINTER (1),
+ NULL);
+ gnome_vfs_module_callback_set_default (GNOME_VFS_MODULE_CALLBACK_FULL_AUTHENTICATION,
+ vfs_full_authentication_callback,
+ GINT_TO_POINTER (0),
+ NULL);
+ gnome_vfs_module_callback_set_default (GNOME_VFS_MODULE_CALLBACK_QUESTION,
+ vfs_question_callback,
+ GINT_TO_POINTER (0),
+ NULL);
+}
Index: programs/gnomevfs-cat.c
===================================================================
RCS file: /cvs/gnome/gnome-vfs/programs/gnomevfs-cat.c,v
retrieving revision 1.4
diff -u -p -r1.4 gnomevfs-cat.c
--- programs/gnomevfs-cat.c 5 Jun 2005 17:35:12 -0000 1.4
+++ programs/gnomevfs-cat.c 22 Mar 2006 15:14:15 -0000
@@ -30,6 +30,8 @@
#include <stdlib.h>
#include <unistd.h>
+#include "authentication.c"
+
static void
show_result (GnomeVFSResult result, const gchar *what, const gchar *text_uri)
{
@@ -59,6 +61,8 @@ main (int argc, char **argv)
fprintf (stderr, "Cannot initialize gnome-vfs.\n");
return 1;
}
+
+ command_line_authentication_init ();
text_uri = gnome_vfs_make_uri_from_shell_arg (argv[1]);
Index: programs/gnomevfs-copy.c
===================================================================
RCS file: /cvs/gnome/gnome-vfs/programs/gnomevfs-copy.c,v
retrieving revision 1.4
diff -u -p -r1.4 gnomevfs-copy.c
--- programs/gnomevfs-copy.c 5 Jun 2005 17:35:12 -0000 1.4
+++ programs/gnomevfs-copy.c 22 Mar 2006 15:14:15 -0000
@@ -28,6 +28,7 @@
#include <stdlib.h>
#include <unistd.h>
+#include "authentication.c"
int
main (int argc, char **argv)
@@ -46,6 +47,8 @@ main (int argc, char **argv)
fprintf (stderr, "Cannot initialize gnome-vfs.\n");
return 1;
}
+
+ command_line_authentication_init ();
text_uri = gnome_vfs_make_uri_from_shell_arg (argv[1]);
Index: programs/gnomevfs-info.c
===================================================================
RCS file: /cvs/gnome/gnome-vfs/programs/gnomevfs-info.c,v
retrieving revision 1.8
diff -u -p -r1.8 gnomevfs-info.c
--- programs/gnomevfs-info.c 29 Nov 2005 16:35:05 -0000 1.8
+++ programs/gnomevfs-info.c 22 Mar 2006 15:14:16 -0000
@@ -34,6 +34,8 @@
#include <unistd.h>
#include <time.h>
+#include "authentication.c"
+
static const gchar *
type_to_string (GnomeVFSFileType type)
{
@@ -178,6 +180,8 @@ main (int argc, char **argv)
fprintf (stderr, "Cannot initialize gnome-vfs.\n");
return 1;
}
+
+ command_line_authentication_init ();
text_uri = gnome_vfs_make_uri_from_shell_arg (argv[1]);
Index: programs/gnomevfs-ls.c
===================================================================
RCS file: /cvs/gnome/gnome-vfs/programs/gnomevfs-ls.c,v
retrieving revision 1.13
diff -u -p -r1.13 gnomevfs-ls.c
--- programs/gnomevfs-ls.c 30 Nov 2005 14:46:50 -0000 1.13
+++ programs/gnomevfs-ls.c 22 Mar 2006 15:14:16 -0000
@@ -28,6 +28,7 @@
#include <locale.h>
#include <libgnomevfs/gnome-vfs.h>
+#include "authentication.c"
static gboolean timing = FALSE;
static gboolean quiet = FALSE;
@@ -189,7 +190,9 @@ main (int argc, char *argv[])
setlocale (LC_ALL, "");
gnome_vfs_init ();
-
+
+ command_line_authentication_init ();
+
error = NULL;
context = g_option_context_new ("- list files at <uri>");
g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
Index: programs/gnomevfs-mkdir.c
===================================================================
RCS file: /cvs/gnome/gnome-vfs/programs/gnomevfs-mkdir.c,v
retrieving revision 1.3
diff -u -p -r1.3 gnomevfs-mkdir.c
--- programs/gnomevfs-mkdir.c 5 Jun 2005 17:35:12 -0000 1.3
+++ programs/gnomevfs-mkdir.c 22 Mar 2006 15:14:16 -0000
@@ -24,6 +24,8 @@
#include <stdio.h>
#include <string.h>
+#include "authentication.c"
+
static GnomeVFSResult
make_directory_with_parents_for_uri (GnomeVFSURI * uri,
guint perm)
@@ -88,8 +90,6 @@ main (int argc, char *argv[])
GnomeVFSResult result;
gboolean with_parents;
- gnome_vfs_init ();
-
if (argc > 1) {
if (strcmp (argv[1], "-p") == 0) {
directory = argv[2];
@@ -103,6 +103,13 @@ main (int argc, char *argv[])
fprintf (stderr, " -p: Create parents of the directory if needed\n");
return 0;
}
+
+ if (!gnome_vfs_init ()) {
+ fprintf (stderr, "Cannot initialize gnome-vfs.\n");
+ return 1;
+ }
+
+ command_line_authentication_init ();
text_uri = gnome_vfs_make_uri_from_shell_arg (directory);
Index: programs/gnomevfs-monitor.c
===================================================================
RCS file: /cvs/gnome/gnome-vfs/programs/gnomevfs-monitor.c,v
retrieving revision 1.2
diff -u -p -r1.2 gnomevfs-monitor.c
--- programs/gnomevfs-monitor.c 5 Jun 2005 17:35:12 -0000 1.2
+++ programs/gnomevfs-monitor.c 22 Mar 2006 15:14:17 -0000
@@ -28,6 +28,7 @@
#include <unistd.h>
#include <signal.h>
+#include "authentication.c"
static void
monitor_event (GnomeVFSMonitorHandle *handle,
@@ -101,7 +102,9 @@ main (int argc, char **argv)
fprintf (stderr, "Cannot initialize gnome-vfs.\n");
return 1;
}
-
+
+ command_line_authentication_init ();
+
text_uri = gnome_vfs_make_uri_from_shell_arg (argv[1]);
if (text_uri == NULL) {
Index: programs/gnomevfs-mv.c
===================================================================
RCS file: /cvs/gnome/gnome-vfs/programs/gnomevfs-mv.c,v
retrieving revision 1.2
diff -u -p -r1.2 gnomevfs-mv.c
--- programs/gnomevfs-mv.c 5 Jun 2005 17:35:12 -0000 1.2
+++ programs/gnomevfs-mv.c 22 Mar 2006 15:14:17 -0000
@@ -28,6 +28,8 @@
#include <stdlib.h>
#include <unistd.h>
+#include "authentication.c"
+
static void
show_result (GnomeVFSResult result, const gchar *what, const gchar *from, const gchar *to)
{
@@ -54,6 +56,8 @@ main (int argc, char **argv)
fprintf (stderr, "Cannot initialize gnome-vfs.\n");
return 1;
}
+
+ command_line_authentication_init ();
from = gnome_vfs_make_uri_from_shell_arg (argv[1]);
Index: programs/gnomevfs-rm.c
===================================================================
RCS file: /cvs/gnome/gnome-vfs/programs/gnomevfs-rm.c,v
retrieving revision 1.2
diff -u -p -r1.2 gnomevfs-rm.c
--- programs/gnomevfs-rm.c 5 Jun 2005 17:35:12 -0000 1.2
+++ programs/gnomevfs-rm.c 22 Mar 2006 15:14:17 -0000
@@ -28,6 +28,8 @@
#include <stdlib.h>
#include <unistd.h>
+#include "authentication.c"
+
static void
show_result (GnomeVFSResult result, const gchar *what, const gchar *text_uri)
{
@@ -54,6 +56,8 @@ main (int argc, char **argv)
fprintf (stderr, "Cannot initialize gnome-vfs.\n");
return 1;
}
+
+ command_line_authentication_init ();
text_uri = gnome_vfs_make_uri_from_shell_arg (argv[1]);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]