~ubuntu-branches/ubuntu/edgy/network-manager-pptp/edgy

« back to all changes in this revision

Viewing changes to auth-dialog/main.c

  • Committer: Bazaar Package Importer
  • Author(s): Craig Box
  • Date: 2006-10-01 16:04:54 UTC
  • Revision ID: james.westby@ubuntu.com-20061001160454-3lepz9bs63bqtvfa
Tags: upstream-0.6.3+cvs20060819
ImportĀ upstreamĀ versionĀ 0.6.3+cvs20060819

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* NetworkManager Wireless Applet -- Display wireless access points and allow user control
 
2
 *
 
3
 * Dan Williams <dcbw@redhat.com>
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
18
 *
 
19
 * (C) Copyright 2004 Red Hat, Inc.
 
20
 */
 
21
 
 
22
#ifdef HAVE_CONFIG_H
 
23
#include <config.h>
 
24
#endif
 
25
 
 
26
#include <string.h>
 
27
#include <glib/gi18n.h>
 
28
#include <gtk/gtk.h>
 
29
#include <libgnomeui/libgnomeui.h>
 
30
#include <gnome-keyring.h>
 
31
 
 
32
#include "gnome-two-password-dialog.h"
 
33
 
 
34
#define VPN_SERVICE "org.freedesktop.NetworkManager.ppp_starter"
 
35
 
 
36
static GSList *
 
37
lookup_pass (const char *vpn_name, const char *vpn_service, char *auth_type, gboolean *is_session)
 
38
{
 
39
        GSList *passwords;
 
40
        GList *keyring_result;
 
41
 
 
42
        passwords = NULL;
 
43
 
 
44
        if (gnome_keyring_find_network_password_sync (NULL,  /* user */
 
45
                                                      NULL,                   /* domain */
 
46
                                                      vpn_name,               /* server */
 
47
                                                      NULL,                   /* object */
 
48
                                                      vpn_service,            /* protocol */
 
49
                                                      auth_type,              /* authtype */
 
50
                                                      0,                      /* port */
 
51
                                                      &keyring_result) != GNOME_KEYRING_RESULT_OK)
 
52
                return FALSE;
 
53
 
 
54
        if (keyring_result != NULL && g_list_length (keyring_result) == 1) {
 
55
                GnomeKeyringNetworkPasswordData *data = 
 
56
             (GnomeKeyringNetworkPasswordData *)keyring_result->data;
 
57
                char *username = NULL;
 
58
                char *password = NULL;
 
59
                char *auth_type = NULL;
 
60
 
 
61
        username = data->user;
 
62
        password = data->password;
 
63
        auth_type = data->authtype;
 
64
 
 
65
                if (password != NULL && username != NULL && auth_type != NULL) {
 
66
// Statically set the authentication type for now.
 
67
                    passwords = g_slist_append (passwords, g_strdup(auth_type));
 
68
                        passwords = g_slist_append (passwords, g_strdup (username));
 
69
                        passwords = g_slist_append (passwords, g_strdup (password));
 
70
                        if (strcmp (data->keyring, "session") == 0)
 
71
                                *is_session = TRUE;
 
72
                        else
 
73
                                *is_session = FALSE;
 
74
                }
 
75
 
 
76
                gnome_keyring_network_password_list_free (keyring_result);
 
77
        }
 
78
 
 
79
        return passwords;
 
80
}
 
81
 
 
82
static void save_vpn_password (const char *vpn_name, const char *vpn_service, const char *keyring, 
 
83
                               const char *auth_type, const char *username, const char *password)
 
84
{
 
85
        guint32 item_id;
 
86
        GnomeKeyringResult keyring_result;
 
87
 
 
88
        keyring_result = gnome_keyring_set_network_password_sync (keyring,
 
89
                                                                  username,
 
90
                                                                  NULL,
 
91
                                                                  vpn_name,
 
92
                                                                  NULL,
 
93
                                                                  vpn_service,
 
94
                                                                  auth_type,
 
95
                                                                  0,
 
96
                                                                  password,
 
97
                                                                  &item_id);
 
98
        if (keyring_result != GNOME_KEYRING_RESULT_OK)
 
99
        {
 
100
                g_warning ("Couldn't store authentication information in keyring, code %d", (int) keyring_result);
 
101
        }
 
102
 
 
103
}
 
104
 
 
105
static GSList *
 
106
get_passwords (const char *vpn_name, const char *vpn_service, gboolean retry)
 
107
{
 
108
        GSList          *result;
 
109
        char            *prompt;
 
110
        GtkWidget           *dialog;
 
111
        char            *keyring_authtype;
 
112
        char            *keyring_username;
 
113
        char            *keyring_password;
 
114
        gboolean         keyring_is_session;
 
115
        GSList          *keyring_result;
 
116
        GnomeTwoPasswordDialogRemember remember;
 
117
 
 
118
        result = NULL;
 
119
        keyring_password = NULL;
 
120
        keyring_username = NULL;
 
121
        keyring_result = NULL;
 
122
 
 
123
        g_return_val_if_fail (vpn_name != NULL, NULL);
 
124
 
 
125
        /* Use the system user name, since the VPN might have a different user name */
 
126
        if (!retry) {
 
127
                if ((result = lookup_pass (vpn_name, vpn_service, "CHAP", &keyring_is_session)) != NULL) {
 
128
                        return result;
 
129
                }
 
130
        } else {
 
131
                if ((keyring_result = lookup_pass (vpn_name, vpn_service, "CHAP", &keyring_is_session)) != NULL) {
 
132
                        keyring_authtype = g_strdup ((char *) keyring_result->data);
 
133
                        keyring_username = g_strdup ((char *) (g_slist_next (keyring_result))->data);
 
134
                        keyring_password = g_strdup ((char *) (g_slist_next (g_slist_next (keyring_result)))->data);
 
135
                }
 
136
                g_slist_foreach (keyring_result, (GFunc)g_free, NULL);
 
137
                g_slist_free (keyring_result);
 
138
        }
 
139
 
 
140
        prompt = g_strdup_printf (_("You need to authenticate to access '%s'."), vpn_name);
 
141
        dialog = gnome_two_password_dialog_new (_("Authenticate Connection"), prompt, NULL, NULL, FALSE);
 
142
        g_free (prompt);
 
143
 
 
144
        gnome_two_password_dialog_set_show_userpass_buttons (GNOME_TWO_PASSWORD_DIALOG (dialog), FALSE);
 
145
        gnome_two_password_dialog_set_show_username (GNOME_TWO_PASSWORD_DIALOG (dialog), TRUE);
 
146
        gnome_two_password_dialog_set_show_domain (GNOME_TWO_PASSWORD_DIALOG (dialog), FALSE);
 
147
        gnome_two_password_dialog_set_show_remember (GNOME_TWO_PASSWORD_DIALOG (dialog), TRUE);
 
148
        gnome_two_password_dialog_set_show_password_secondary (GNOME_TWO_PASSWORD_DIALOG (dialog), FALSE);
 
149
        //gnome_two_password_dialog_set_password_secondary_label (GNOME_TWO_PASSWORD_DIALOG (dialog), _("_Group Password:"));
 
150
        /* use the same keyring storage options as from the items we put in the entry boxes */
 
151
        remember = GNOME_TWO_PASSWORD_DIALOG_REMEMBER_NOTHING;
 
152
        if (keyring_result != NULL) {
 
153
                if (keyring_is_session)
 
154
                        remember = GNOME_TWO_PASSWORD_DIALOG_REMEMBER_SESSION;
 
155
                else
 
156
                        remember = GNOME_TWO_PASSWORD_DIALOG_REMEMBER_FOREVER;                          
 
157
        }
 
158
        gnome_two_password_dialog_set_remember (GNOME_TWO_PASSWORD_DIALOG (dialog), remember);
 
159
 
 
160
        /* if retrying, put in the passwords from the keyring */
 
161
        if (keyring_username != NULL) {
 
162
                gnome_two_password_dialog_set_username (GNOME_TWO_PASSWORD_DIALOG (dialog), keyring_username);
 
163
        }
 
164
        if (keyring_password != NULL) {
 
165
                gnome_two_password_dialog_set_password (GNOME_TWO_PASSWORD_DIALOG (dialog), keyring_password);
 
166
        }
 
167
 
 
168
        gtk_widget_show (dialog);
 
169
 
 
170
        if (gnome_two_password_dialog_run_and_block (GNOME_TWO_PASSWORD_DIALOG (dialog)))
 
171
        {
 
172
                char *username;
 
173
                char *password;
 
174
                char *auth_type;
 
175
 
 
176
                username = gnome_two_password_dialog_get_username (GNOME_TWO_PASSWORD_DIALOG (dialog));
 
177
                password = gnome_two_password_dialog_get_password (GNOME_TWO_PASSWORD_DIALOG (dialog));
 
178
// Statically set the authentication type for now.
 
179
        auth_type = g_strdup("CHAP");
 
180
                result = g_slist_append (result, auth_type);
 
181
                result = g_slist_append (result, username);
 
182
                result = g_slist_append (result, password);
 
183
 
 
184
                switch (gnome_two_password_dialog_get_remember (GNOME_TWO_PASSWORD_DIALOG (dialog)))
 
185
                {
 
186
                        case GNOME_TWO_PASSWORD_DIALOG_REMEMBER_SESSION:
 
187
                                save_vpn_password (vpn_name, vpn_service, "session", auth_type, username, password);
 
188
                                break;
 
189
                        case GNOME_TWO_PASSWORD_DIALOG_REMEMBER_FOREVER:
 
190
                                save_vpn_password (vpn_name, vpn_service, NULL, auth_type, username, password);
 
191
                                break;
 
192
                        default:
 
193
                                break;
 
194
                }
 
195
 
 
196
        }
 
197
 
 
198
        if (keyring_username!=NULL) g_free (keyring_username);
 
199
        if (keyring_password!=NULL) g_free (keyring_password);
 
200
 
 
201
        gtk_widget_destroy (dialog);
 
202
 
 
203
        return result;
 
204
}
 
205
 
 
206
int 
 
207
main (int argc, char *argv[])
 
208
{
 
209
        GSList *i;
 
210
        GSList *passwords;
 
211
        static gboolean retry = FALSE;
 
212
        static gchar *vpn_name = NULL;
 
213
        static gchar *vpn_service = NULL;
 
214
        GError *error = NULL;
 
215
        GOptionContext *context;
 
216
        static GOptionEntry entries[] = 
 
217
                {
 
218
                        { "reprompt", 'r', 0, G_OPTION_ARG_NONE, &retry, "Reprompt for passwords", NULL},
 
219
                        { "name", 'n', 0, G_OPTION_ARG_STRING, &vpn_name, "Name of VPN connection", NULL},
 
220
                        { "service", 's', 0, G_OPTION_ARG_STRING, &vpn_service, "VPN service type", NULL},
 
221
                        { NULL }
 
222
                };
 
223
        char buf[1];
 
224
 
 
225
        bindtextdomain (GETTEXT_PACKAGE, NULL);
 
226
        bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
 
227
        textdomain (GETTEXT_PACKAGE);
 
228
 
 
229
        passwords = NULL;
 
230
        
 
231
        context = g_option_context_new ("- ppp auth dialog");
 
232
        g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
 
233
        g_option_context_add_group (context, gtk_get_option_group (TRUE));
 
234
        g_option_context_parse (context, &argc, &argv, &error);
 
235
 
 
236
        if (vpn_name == NULL || vpn_service == NULL) {
 
237
                fprintf (stderr, "Have to supply both name and service\n");
 
238
                goto out;
 
239
        }
 
240
 
 
241
        if (strcmp (vpn_service, VPN_SERVICE) != 0) {
 
242
                fprintf (stderr, "This dialog only works with the '%s' service\n", VPN_SERVICE);
 
243
                goto out;               
 
244
        }
 
245
 
 
246
        gnome_program_init ("nm-ppp-auth-dialog", VERSION, LIBGNOMEUI_MODULE,
 
247
                            argc, argv, 
 
248
                            GNOME_PARAM_NONE);
 
249
          
 
250
        passwords = get_passwords (vpn_name, vpn_service, retry);
 
251
        if (passwords == NULL)
 
252
                goto out;
 
253
 
 
254
        /* dump the passwords to stdout */
 
255
        for (i = passwords; i != NULL; i = g_slist_next (i)) {
 
256
                char *password = (char *) i->data;
 
257
                printf ("%s\n", password);
 
258
        }
 
259
        printf ("\n\n");
 
260
        /* for good measure, flush stdout since Kansas is going Bye-Bye */
 
261
        fflush (stdout);
 
262
 
 
263
        g_slist_foreach (passwords, (GFunc)g_free, NULL);
 
264
        g_slist_free (passwords);
 
265
 
 
266
        /* wait for data on stdin  */
 
267
        fread (buf, sizeof (char), sizeof (buf), stdin);
 
268
 
 
269
out:
 
270
        g_option_context_free (context);
 
271
 
 
272
        return passwords != NULL ? 0 : 1;
 
273
}