~ubuntu-branches/ubuntu/natty/gnome-keyring/natty

« back to all changes in this revision

Viewing changes to daemon/ssh-agent/gkd-ssh-agent-standalone.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2010-02-16 19:00:06 UTC
  • mfrom: (1.1.58 upstream)
  • Revision ID: james.westby@ubuntu.com-20100216190006-cqpnic4zxlkmmi0o
Tags: 2.29.90git20100218-0ubuntu1
Updated to a git snapshot version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
 
2
/* gkd-ssh-agent-standalone.c - Test standalone SSH agent
 
3
 
 
4
   Copyright (C) 2007 Stefan Walter
 
5
 
 
6
   Gnome keyring is free software; you can redistribute it and/or
 
7
   modify it under the terms of the GNU General Public License as
 
8
   published by the Free Software Foundation; either version 2 of the
 
9
   License, or (at your option) any later version.
 
10
 
 
11
   Gnome keyring is distributed in the hope that it will be useful,
 
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
   General Public License for more details.
 
15
 
 
16
   You should have received a copy of the GNU General Public License
 
17
   along with this program; if not, write to the Free Software
 
18
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
19
 
 
20
   Author: Stef Walter <stef@memberwebs.com>
 
21
*/
 
22
 
 
23
#include "config.h"
 
24
 
 
25
#include "gkd-ssh-agent.h"
 
26
#include "gkd-ssh-agent-private.h"
 
27
 
 
28
#include "egg/egg-secure-memory.h"
 
29
 
 
30
#include "gp11/gp11.h"
 
31
 
 
32
#include <glib.h>
 
33
#include <glib-object.h>
 
34
 
 
35
#include <pwd.h>
 
36
#include <string.h>
 
37
#include <unistd.h>
 
38
 
 
39
G_LOCK_DEFINE_STATIC (memory_mutex);
 
40
 
 
41
void egg_memory_lock (void)
 
42
        { G_LOCK (memory_mutex); }
 
43
 
 
44
void egg_memory_unlock (void)
 
45
        { G_UNLOCK (memory_mutex); }
 
46
 
 
47
void* egg_memory_fallback (void *p, size_t sz)
 
48
        { return g_realloc (p, sz); }
 
49
 
 
50
static gboolean
 
51
accept_client (GIOChannel *channel, GIOCondition cond, gpointer unused)
 
52
{
 
53
        gkd_ssh_agent_accept ();
 
54
        return TRUE;
 
55
}
 
56
 
 
57
static gboolean
 
58
authenticate_slot (GP11Module *module, GP11Slot *slot, gchar *label, gchar **password, gpointer unused)
 
59
{
 
60
        gchar *prompt = g_strdup_printf ("Enter token password (%s): ", label);
 
61
        char *result = getpass (prompt);
 
62
        g_free (prompt);
 
63
        *password = g_strdup (result);
 
64
        memset (result, 0, strlen (result));
 
65
        return TRUE;
 
66
}
 
67
 
 
68
static gboolean
 
69
authenticate_object (GP11Module *module, GP11Object *object, gchar *label, gchar **password)
 
70
{
 
71
        gchar *prompt = g_strdup_printf ("Enter object password (%s): ", label);
 
72
        char *result = getpass (prompt);
 
73
        g_free (prompt);
 
74
        *password = g_strdup (result);
 
75
        memset (result, 0, strlen (result));
 
76
        return TRUE;
 
77
}
 
78
 
 
79
int
 
80
main(int argc, char *argv[])
 
81
{
 
82
        GP11Module *module;
 
83
        GError *error = NULL;
 
84
        GIOChannel *channel;
 
85
        GMainLoop *loop;
 
86
        gboolean ret;
 
87
        int sock;
 
88
 
 
89
        g_type_init ();
 
90
 
 
91
        if (!g_thread_supported ())
 
92
                g_thread_init (NULL);
 
93
 
 
94
        if (argc <= 1) {
 
95
                g_message ("specify pkcs11 module on the command line");
 
96
                return 1;
 
97
        }
 
98
 
 
99
        module = gp11_module_initialize (argv[1], argc > 2 ? argv[2] : NULL, &error);
 
100
        if (!module) {
 
101
                g_message ("couldn't load pkcs11 module: %s", error->message);
 
102
                g_clear_error (&error);
 
103
                return 1;
 
104
        }
 
105
 
 
106
 
 
107
        g_signal_connect (module, "authenticate-slot", G_CALLBACK (authenticate_slot), NULL);
 
108
        g_signal_connect (module, "authenticate-object", G_CALLBACK (authenticate_object), NULL);
 
109
        gp11_module_set_auto_authenticate (module, GP11_AUTHENTICATE_OBJECTS);
 
110
 
 
111
        ret = gkd_ssh_agent_initialize_with_module (module);
 
112
        g_object_unref (module);
 
113
 
 
114
        if (ret == FALSE)
 
115
                return 1;
 
116
 
 
117
        sock = gkd_ssh_agent_startup ("/tmp");
 
118
        if (sock == -1)
 
119
                return 1;
 
120
 
 
121
        channel = g_io_channel_unix_new (sock);
 
122
        g_io_add_watch (channel, G_IO_IN | G_IO_HUP, accept_client, NULL);
 
123
        g_io_channel_unref (channel);
 
124
 
 
125
        g_print ("SSH_AUTH_SOCK=%s\n", g_getenv ("SSH_AUTH_SOCK"));
 
126
 
 
127
        /* Run a main loop */
 
128
        loop = g_main_loop_new (NULL, FALSE);
 
129
        g_main_loop_run (loop);
 
130
        g_main_loop_unref (loop);
 
131
 
 
132
        gkd_ssh_agent_shutdown ();
 
133
        gkd_ssh_agent_uninitialize ();
 
134
 
 
135
        return 0;
 
136
}