NetworkManager/info-daemon/NetworkManagerInfo.c
Dan Williams aab500146f 2004-07-19 Dan Williams <dcbw@redhat.com>
* Makefile.am
		- Add info-daemon directory

	* configure.in
		- Check for glade libs and headers
		- Add info-daemon directory

	* src/NetworkManagerAP.c
		- nm_ap_new_from_ap(): Fix bug that resulted in an APs encryption status not getting
			copied over to the new AP.

	* src/NetworkManagerDbus.c
	  src/NetworkManagerDbus.h
		- Deal with nm_device_ap_list_get_ap()->nm_device_ap_list_get_ap_by_index() change
		- Remove nm_dbus_signal_need_key_for_network()
		- Add disabled code for asynchronous user wep key callbacks
		- Add functions for getting, setting, and cancelling user key operations
		- Remove "setKeyForNetwork" device dbus method call, its on NetworkManager object instead
		- Add "setKeyForNetwork" dbus method call on NetworkManager object

	* src/NetworkManagerDevice.c
	  src/NetworkManagerDevice.h
		- nm_device_update_link_active(): revert changes for wireless link detection, the WEP-key-is-wrong
			logic is in device activation now
		- nm_device_activate(): for wireless devices, if we can't associate with access point (perhaps
			key is wrong) trigger get-user-key pending action
		- Implement get-user-key pending action stuff, tie to dbus messages
		- Rename nm_device_ap_list_get_ap() -> nm_device_ap_list_get_ap_by_index()
		- Add nm_device_ap_list_get_ap_by_essid()
		- Instead of copying "best" access points, ref them instead so that the key we set
			sticks around

	* src/NetworkManagerPolicy.c
		- Deal with wrong WEP key, but right access point (and if so, return link_active = TRUE)
		- Don't cancel pending actions on a device if its the same device as last iteration
		- Only promote pending_device->active_device if activation was successfull

	* src/Makefile.am
		- Rename nmclienttest->nmtest

	* info-daemon/Makefile.am
	  info-daemon/NetworkManagerInfo.c
	  info-daemon/NetworkManagerInfo.h
	  info-daemon/NetworkManagerInfoDbus.c
	  info-daemon/NetworkManagerInfoDbus.h
	  info-daemon/passphrase.glade
	  info-daemon/NetworkManagerInfo.conf
	  info-daemon/keyring.png
	  	- Import sources for info-daemon, which pops up dialog for passphrase/key when
	  		NetworkManager asks for it, and also will (soon) provide "allowed" access point
	  		lists to NetworkManager by proxying user's GConf


git-svn-id: http://svn-archive.gnome.org/svn/NetworkManager/trunk@16 4912f4e0-d625-0410-9fb7-b9a5a253dbdc
2004-07-19 06:08:52 +00:00

360 lines
8.9 KiB
C

/* NetworkManagerInfo -- Manage allowed access points and provide a UI
* for WEP key entry
*
* Dan Williams <dcbw@redhat.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* (C) Copyright 2004 Red Hat, Inc.
*/
#include <glib.h>
#include <dbus/dbus-glib.h>
#include <getopt.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <gtk/gtk.h>
#include <glade/glade.h>
#include "NetworkManagerInfoDbus.h"
#include "NetworkManagerInfo.h"
/*
* nmi_clear_dialog
*
* Return dialog to its original state; clear out any network or device qdatas,
* clear the passphrase entry, and hide the dialog.
*
*/
static void nmi_clear_dialog (GtkWidget *dialog, GtkWidget *entry)
{
char *data;
g_return_if_fail (dialog != NULL);
g_return_if_fail (entry != NULL);
data = g_object_get_qdata (G_OBJECT (dialog), g_quark_from_static_string ("device"));
if (data)
{
g_free (data);
g_object_set_qdata (G_OBJECT (dialog), g_quark_from_static_string ("device"), NULL);
}
data = g_object_get_qdata (G_OBJECT (dialog), g_quark_from_static_string ("network"));
if (data)
{
g_free (data);
g_object_set_qdata (G_OBJECT (dialog), g_quark_from_static_string ("network"), NULL);
}
gtk_entry_set_text (GTK_ENTRY (entry), "");
gtk_widget_hide (dialog);
}
/*
* ok_button_clicked
*
* OK button handler; grab the passphrase and send it back
* to NetworkManager. Get rid of the dialog.
*
*/
static void ok_button_clicked (GtkWidget *ok_button, gpointer user_data)
{
GtkWidget *dialog = gtk_widget_get_toplevel (ok_button);
NMIAppInfo *info = (NMIAppInfo *)user_data;
g_return_if_fail (info != NULL);
if (GTK_WIDGET_TOPLEVEL (dialog))
{
GtkWidget *entry = glade_xml_get_widget (info->xml, "passphrase_entry");
const char *passphrase = gtk_entry_get_text (GTK_ENTRY (entry));
const char *device = g_object_get_qdata (G_OBJECT (dialog), g_quark_from_static_string ("device"));
const char *network = g_object_get_qdata (G_OBJECT (dialog), g_quark_from_static_string ("network"));
nmi_dbus_return_user_key (info->connection, device, network, passphrase);
nmi_clear_dialog (dialog, entry);
}
}
/*
* cancel_button_clicked
*
* Cancel button handler; return a cancellation message to NetworkManager
* and get rid of the dialog.
*
*/
static void cancel_button_clicked (GtkWidget *cancel_button, gpointer user_data)
{
GtkWidget *dialog = gtk_widget_get_toplevel (cancel_button);
NMIAppInfo *info = (NMIAppInfo *)user_data;
g_return_if_fail (info != NULL);
if (GTK_WIDGET_TOPLEVEL (dialog))
{
const char *device = g_object_get_qdata (G_OBJECT (dialog), g_quark_from_static_string ("device"));
const char *network = g_object_get_qdata (G_OBJECT (dialog), g_quark_from_static_string ("network"));
nmi_dbus_return_user_key (info->connection, device, network, "***cancelled***");
nmi_clear_dialog (dialog, glade_xml_get_widget (info->xml, "passphrase_entry"));
}
}
/*
* nmi_show_user_key_dialog
*
* Pop up the user key dialog in response to a dbus message
*
*/
void nmi_show_user_key_dialog (const char *device, const char *network, NMIAppInfo *info)
{
GtkWidget *dialog;
GtkWidget *label;
const gchar *label_text;
g_return_if_fail (info != NULL);
g_return_if_fail (device != NULL);
g_return_if_fail (network != NULL);
dialog = glade_xml_get_widget (info->xml, "passphrase_dialog");
nmi_clear_dialog (dialog, glade_xml_get_widget (info->xml, "passphrase_entry"));
/* Insert the Network name into the dialog text */
label = glade_xml_get_widget (info->xml, "label1");
label_text = gtk_label_get_label (GTK_LABEL (label));
if (label_text)
{
gchar *new_label_text = g_strdup_printf (label_text, network);
gtk_label_set_label (GTK_LABEL (label), new_label_text);
}
g_object_set_qdata (G_OBJECT (dialog), g_quark_from_static_string ("device"), g_strdup (device));
g_object_set_qdata (G_OBJECT (dialog), g_quark_from_static_string ("network"), g_strdup (network));
gtk_widget_show (dialog);
}
/*
* nmi_cancel_user_key_dialog
*
* Cancel and hide any user key dialog that might be up
*
*/
void nmi_cancel_user_key_dialog (NMIAppInfo *info)
{
GtkWidget *dialog;
GtkWidget *entry;
g_return_if_fail (info != NULL);
dialog = glade_xml_get_widget (info->xml, "passphrase_dialog");
entry = glade_xml_get_widget (info->xml, "passphrase_entry");
nmi_clear_dialog (dialog, entry);
}
/*
* nmi_interface_init
*
* Initialize the UI pieces of NMI.
*
*/
static void nmi_interface_init (NMIAppInfo *info)
{
GtkWidget *dialog;
GtkButton *ok_button;
GtkButton *cancel_button;
GtkEntry *entry;
info->xml = glade_xml_new("passphrase.glade", NULL, NULL);
if (!info->xml)
{
fprintf (stderr, "Could not open glade file!\n");
exit (1);
}
dialog = glade_xml_get_widget (info->xml, "passphrase_dialog");
gtk_widget_hide (dialog);
ok_button = GTK_BUTTON (glade_xml_get_widget (info->xml, "login_button"));
gtk_signal_connect (GTK_OBJECT (ok_button), "clicked", GTK_SIGNAL_FUNC (ok_button_clicked), info);
gtk_widget_grab_default (GTK_WIDGET (ok_button));
cancel_button = GTK_BUTTON (glade_xml_get_widget (info->xml, "cancel_button"));
gtk_signal_connect (GTK_OBJECT (cancel_button), "clicked", GTK_SIGNAL_FUNC (cancel_button_clicked), info);
entry = GTK_ENTRY (glade_xml_get_widget (info->xml, "passphrase_entry"));
gtk_entry_set_visibility (entry, FALSE);
gtk_entry_set_invisible_char (entry, '*');
nmi_clear_dialog (dialog, GTK_WIDGET (entry));
}
/*
* nmi_print_usage
*
* Prints program usage.
*
*/
static void nmi_print_usage (void)
{
fprintf (stderr, "\n" "usage : NetworkManagerInfo [--daemon=yes|no] [--help]\n");
fprintf (stderr,
"\n"
" --daemon=yes|no Become a daemon\n"
" --help Show this information and exit\n"
"\n"
"NetworkManagerInfo responds to NetworkManager requests for allowed access points\n"
"and WEP keys.\n"
"\n");
}
/*
* main
*
*/
int main( int argc, char *argv[] )
{
gboolean become_daemon = TRUE;
DBusError dbus_error;
DBusConnection *dbus_connection;
int err;
NMIAppInfo *app_info = NULL;
GMainLoop *loop;
/* Parse options */
while (1)
{
int c;
int option_index = 0;
const char *opt;
static struct option options[] = {
{"daemon", 1, NULL, 0},
{"help", 0, NULL, 0},
{NULL, 0, NULL, 0}
};
c = getopt_long (argc, argv, "", options, &option_index);
if (c == -1)
break;
switch (c)
{
case 0:
opt = options[option_index].name;
if (strcmp (opt, "help") == 0)
{
nmi_print_usage ();
return 0;
}
else if (strcmp (opt, "daemon") == 0)
{
if (strcmp ("yes", optarg) == 0)
become_daemon = TRUE;
else if (strcmp ("no", optarg) == 0)
become_daemon = FALSE;
else
{
nmi_print_usage ();
return 1;
}
}
break;
default:
nmi_print_usage ();
return 1;
break;
}
}
if (become_daemon)
{
int child_pid;
if (chdir ("/") < 0)
{
fprintf( stderr, "NetworkManagerInfo could not chdir to /. errno=%d", errno);
return 1;
}
child_pid = fork ();
switch (child_pid)
{
case -1:
fprintf( stderr, "NetworkManagerInfo could not daemonize. errno = %d\n", errno );
break;
case 0:
/* Child */
break;
default:
exit (0);
break;
}
}
app_info = g_new0 (NMIAppInfo, 1);
if (!app_info)
{
fprintf (stderr, "Not enough memory for application data.\n");
exit (1);
}
g_type_init ();
if (!g_thread_supported ())
g_thread_init (NULL);
/* Set up our connection to the message bus */
dbus_error_init (&dbus_error);
dbus_connection = dbus_bus_get (DBUS_BUS_SYSTEM, &dbus_error);
if (dbus_connection == NULL)
{
fprintf (stderr, "NetworkManagerInfo could not get the system bus. Make sure the message bus daemon is running?\n");
exit (1);
}
dbus_connection_set_change_sigpipe (TRUE);
dbus_connection_set_exit_on_disconnect (dbus_connection, FALSE);
dbus_connection_setup_with_g_main (dbus_connection, NULL);
app_info->connection = dbus_connection;
/* Create our own dbus service */
err = nmi_dbus_service_init (dbus_connection, app_info);
if (err == -1)
exit (1);
gtk_init (&argc, &argv);
nmi_interface_init (app_info);
loop = g_main_loop_new (NULL, FALSE);
g_main_loop_run (loop);
g_free (app_info);
return 0;
}