~oem-solutions-group/network-manager/ubuntu.hardy.07

« back to all changes in this revision

Viewing changes to vpn-daemons/openvpn/common-gnome/keyring-helpers.c

(merge) RELEASE 0.7~~svn20080928t225540+eni0-0ubuntu1 to ubuntu/intrepid

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
 
2
/* NetworkManager Wireless Applet -- Display wireless access points and allow user control
 
3
 *
 
4
 * Dan Williams <dcbw@redhat.com>
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; either version 2 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * This program 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
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License along
 
17
 * with this program; if not, write to the Free Software Foundation, Inc.,
 
18
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
19
 *
 
20
 * (C) Copyright 2004 - 2008 Red Hat, Inc.
 
21
 */
 
22
 
 
23
#include <string.h>
 
24
#include <gnome-keyring-memory.h>
 
25
 
 
26
#include <nm-setting-vpn.h>
 
27
 
 
28
#include "keyring-helpers.h"
 
29
#include "../src/nm-openvpn-service.h"
 
30
 
 
31
#define KEYRING_UUID_TAG "connection-uuid"
 
32
#define KEYRING_SN_TAG "setting-name"
 
33
#define KEYRING_SK_TAG "setting-key"
 
34
 
 
35
char *
 
36
keyring_helpers_lookup_secret (const char *vpn_uuid,
 
37
                                                           const char *secret_name,
 
38
                                                           gboolean *is_session)
 
39
{
 
40
        GList *found_list = NULL;
 
41
        GnomeKeyringResult ret;
 
42
        GnomeKeyringFound *found;
 
43
        char *secret;
 
44
 
 
45
        ret = gnome_keyring_find_itemsv_sync (GNOME_KEYRING_ITEM_GENERIC_SECRET,
 
46
                                              &found_list,
 
47
                                              KEYRING_UUID_TAG,
 
48
                                              GNOME_KEYRING_ATTRIBUTE_TYPE_STRING,
 
49
                                              vpn_uuid,
 
50
                                              KEYRING_SN_TAG,
 
51
                                              GNOME_KEYRING_ATTRIBUTE_TYPE_STRING,
 
52
                                              NM_SETTING_VPN_SETTING_NAME,
 
53
                                              KEYRING_SK_TAG,
 
54
                                              GNOME_KEYRING_ATTRIBUTE_TYPE_STRING,
 
55
                                              secret_name,
 
56
                                              NULL);
 
57
        if ((ret != GNOME_KEYRING_RESULT_OK) || (g_list_length (found_list) == 0))
 
58
                return NULL;
 
59
 
 
60
        found = (GnomeKeyringFound *) found_list->data;
 
61
 
 
62
        if (strcmp (found->keyring, "session") == 0)
 
63
                *is_session = TRUE;
 
64
        else
 
65
                *is_session = FALSE;
 
66
 
 
67
        secret = found->secret ? g_strdup (found->secret) : NULL;
 
68
        gnome_keyring_found_list_free (found_list);
 
69
 
 
70
        return secret;
 
71
}
 
72
 
 
73
GnomeKeyringResult
 
74
keyring_helpers_save_secret (const char *vpn_uuid,
 
75
                             const char *vpn_name,
 
76
                             const char *keyring,
 
77
                             const char *secret_name,
 
78
                             const char *secret)
 
79
{
 
80
        char *display_name;
 
81
        GnomeKeyringResult ret;
 
82
        GnomeKeyringAttributeList *attrs = NULL;
 
83
        guint32 id = 0;
 
84
 
 
85
        display_name = g_strdup_printf ("VPN %s secret for %s/%s/" NM_SETTING_VPN_SETTING_NAME,
 
86
                                        secret_name,
 
87
                                        vpn_name,
 
88
                                        NM_DBUS_SERVICE_OPENVPN);
 
89
 
 
90
        attrs = gnome_keyring_attribute_list_new ();
 
91
        gnome_keyring_attribute_list_append_string (attrs,
 
92
                                                    KEYRING_UUID_TAG,
 
93
                                                    vpn_uuid);
 
94
        gnome_keyring_attribute_list_append_string (attrs,
 
95
                                                    KEYRING_SN_TAG,
 
96
                                                    NM_SETTING_VPN_SETTING_NAME);
 
97
        gnome_keyring_attribute_list_append_string (attrs,
 
98
                                                    KEYRING_SK_TAG,
 
99
                                                    secret_name);
 
100
 
 
101
        ret = gnome_keyring_item_create_sync (keyring,
 
102
                                              GNOME_KEYRING_ITEM_GENERIC_SECRET,
 
103
                                              display_name,
 
104
                                              attrs,
 
105
                                              secret,
 
106
                                              TRUE,
 
107
                                              &id);
 
108
        gnome_keyring_attribute_list_free (attrs);
 
109
        g_free (display_name);
 
110
        return ret;
 
111
}
 
112
 
 
113
static void
 
114
ignore_callback (GnomeKeyringResult result, gpointer data)
 
115
{
 
116
}
 
117
 
 
118
gboolean
 
119
keyring_helpers_delete_secret (const char *vpn_uuid,
 
120
                               const char *secret_name)
 
121
{
 
122
        GList *found = NULL, *iter;
 
123
        GnomeKeyringResult ret;
 
124
 
 
125
        g_return_val_if_fail (vpn_uuid != NULL, FALSE);
 
126
        g_return_val_if_fail (secret_name != NULL, FALSE);
 
127
 
 
128
        ret = gnome_keyring_find_itemsv_sync (GNOME_KEYRING_ITEM_GENERIC_SECRET,
 
129
                                              &found,
 
130
                                              KEYRING_UUID_TAG,
 
131
                                              GNOME_KEYRING_ATTRIBUTE_TYPE_STRING,
 
132
                                              vpn_uuid,
 
133
                                              KEYRING_SN_TAG,
 
134
                                              GNOME_KEYRING_ATTRIBUTE_TYPE_STRING,
 
135
                                              NM_SETTING_VPN_SETTING_NAME,
 
136
                                              KEYRING_SK_TAG,
 
137
                                              GNOME_KEYRING_ATTRIBUTE_TYPE_STRING,
 
138
                                              secret_name,
 
139
                                              NULL);
 
140
        if (ret != GNOME_KEYRING_RESULT_OK && ret != GNOME_KEYRING_RESULT_NO_MATCH)
 
141
                return FALSE;
 
142
        if (g_list_length (found) == 0)
 
143
                return TRUE;
 
144
 
 
145
        /* delete them all */
 
146
        for (iter = found; iter; iter = g_list_next (iter)) {
 
147
                GnomeKeyringFound *item = (GnomeKeyringFound *) iter->data;
 
148
 
 
149
                gnome_keyring_item_delete (item->keyring, item->item_id,
 
150
                                           ignore_callback, NULL, NULL);
 
151
        }
 
152
 
 
153
        gnome_keyring_found_list_free (found);
 
154
        return TRUE;
 
155
}
 
156