From b529be21c80b84875f43017e5ff5f52545963091 Mon Sep 17 00:00:00 2001 From: Gregor Riepl Date: Tue, 27 Jul 2010 23:51:11 +0200 Subject: [PATCH] Added sudo scripts for enabling and disabling USB networking Added sudoers file, added update-sudoers to dpkg control scripts Implemented real loading, unloading and status checking in status menu button --- Makefile | 7 +++ debian/changelog | 6 +++ debian/dirs | 2 - debian/postinst | 1 + debian/postrm | 40 ++++++++++++++++ mtetherd-usbnet-disable.sh | 32 +++++++++++++ mtetherd-usbnet-enable.sh | 109 ++++++++++++++++++++++++++++++++++++++++++++ mtetherd.c | 6 --- mtetherd.sudoers | 2 + status.c | 56 +++++++++++++++++++++-- 10 files changed, 248 insertions(+), 13 deletions(-) delete mode 100644 debian/dirs create mode 100644 debian/postrm create mode 100755 mtetherd-usbnet-disable.sh create mode 100755 mtetherd-usbnet-enable.sh create mode 100644 mtetherd.sudoers diff --git a/Makefile b/Makefile index b2d6d0a..6b0ea7b 100644 --- a/Makefile +++ b/Makefile @@ -27,6 +27,9 @@ install: mtetherd mtetherd-plugin.so install -m 644 -D README $(DESTDIR)/$(PREFIX)/share/doc/mtetherd/README install -m 644 -D mtetherd-plugin.desktop $(DESTDIR)/$(HILDON_DESKTOP)/mtetherd-plugin.desktop install -m 755 -D mtetherd-plugin.so $(DESTDIR)/$(HILDON_PLUGIN)/mtetherd-plugin.so + install -m 755 -D mtetherd-usbnet-enable.sh $(DESTDIR)/$(PREFIX)/sbin/mtetherd-usbnet-enable.sh + install -m 755 -D mtetherd-usbnet-disable.sh $(DESTDIR)/$(PREFIX)/sbin/mtetherd-usbnet-disable.sh + install -m 755 -D mtetherd.sudoers $(DESTDIR)/etc/sudoers.d/mtetherd.sudoers uninstall: rm -f $(DESTDIR)/$(PREFIX)/sbin/mtetherd @@ -34,6 +37,10 @@ uninstall: rm -rf $(DESTDIR)/$(PREFIX)/share/doc/mtetherd rm -f $(DESTDIR)/$(HILDON_DESKTOP)/mtetherd-plugin.desktop rm -f $(DESTDIR)/$(HILDON_PLUGIN)/mtetherd-plugin.so + rm -f $(DESTDIR)/$(PREFIX)/sbin/mtetherd-usbnet-enable.sh + rm -f $(DESTDIR)/$(PREFIX)/sbin/mtetherd-usbnet-disable.sh + rm -f $(DESTDIR)/etc/sudoers.d/mtetherd.sudoers %.o: %.c $(CC) $(CFLAGS) $(INCLUDES) -o $@ -c $^ + diff --git a/debian/changelog b/debian/changelog index bc8abe1..a1c2843 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,3 +1,9 @@ +mtetherd (0.2-1) unstable; urgency=low + + * Added status menu button to enable/disable USB networking + + -- Gregor Riepl Tue, 27 Jul 2010 23:48:00 +0200 + mtetherd (0.1-1) unstable; urgency=low * Initial release diff --git a/debian/dirs b/debian/dirs deleted file mode 100644 index dc34a69..0000000 --- a/debian/dirs +++ /dev/null @@ -1,2 +0,0 @@ -usr/sbin -etc/event.d diff --git a/debian/postinst b/debian/postinst index 47c10de..fe27a4f 100644 --- a/debian/postinst +++ b/debian/postinst @@ -20,6 +20,7 @@ set -e case "$1" in configure) + update-sudoers start mtetherd ;; diff --git a/debian/postrm b/debian/postrm new file mode 100644 index 0000000..c5232c6 --- /dev/null +++ b/debian/postrm @@ -0,0 +1,40 @@ +#!/bin/sh +# postrm script for mtetherd +# +# see: dh_installdeb(1) + +set -e + +# summary of how this script can be called: +# * `remove' +# * `purge' +# * `upgrade' +# * `failed-upgrade' +# * `abort-install' +# * `abort-install' +# * `abort-upgrade' +# * `disappear' +# +# for details, see http://www.debian.org/doc/debian-policy/ or +# the debian-policy package + + +case "$1" in + purge|remove|upgrade|failed-upgrade|abort-install|abort-upgrade|disappear) + update-sudoers + ;; + + *) + echo "postrm called with unknown argument \`$1'" >&2 + exit 1 + ;; +esac + +# dh_installdeb will replace this with shell code automatically +# generated by other debhelper scripts. + +#DEBHELPER# + +exit 0 + + diff --git a/mtetherd-usbnet-disable.sh b/mtetherd-usbnet-disable.sh new file mode 100755 index 0000000..317e350 --- /dev/null +++ b/mtetherd-usbnet-disable.sh @@ -0,0 +1,32 @@ +#!/bin/sh +# This file is part of mtetherd +# +# Copyright (c) 2010 Gregor Riepl +# +# Based on osso-usb-mass-storage-disable.sh, +# Copyright (C) 2004-2009 Nokia Corporation. All rights reserved. +# Contact: Kimmo Hämäläinen +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# version 2 as published by the Free Software Foundation. +# +# 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., 51 Franklin St, Fifth Floor, Boston, MA +# 02110-1301 USA + +/sbin/lsmod | grep g_ether > /dev/null +if [ $? != 0 ]; then + # the module is not loaded + exit 0 +fi + +/sbin/rmmod g_ether + +exit 0 diff --git a/mtetherd-usbnet-enable.sh b/mtetherd-usbnet-enable.sh new file mode 100755 index 0000000..fcea859 --- /dev/null +++ b/mtetherd-usbnet-enable.sh @@ -0,0 +1,109 @@ +#!/bin/sh +# This file is part of mtetherd +# +# Copyright (c) 2010 Gregor Riepl +# +# Based on osso-usb-mass-storage-enable.sh and osso-usb-mass-storage-disable.sh, +# Copyright (C) 2004-2009 Nokia Corporation. All rights reserved. +# Contact: Kimmo Hämäläinen +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# version 2 as published by the Free Software Foundation. +# +# 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., 51 Franklin St, Fifth Floor, Boston, MA +# 02110-1301 USA + +RC=0 + +/sbin/lsmod | grep g_nokia > /dev/null +if [ $? = 0 ]; then + logger "$0: removing g_nokia" + + initctl emit G_NOKIA_REMOVE + + PNATD_PID=`pidof pnatd` + if [ $? = 0 ]; then + kill $PNATD_PID + else + logger "$0: pnatd is not running" + fi + OBEXD_PID=`pidof obexd` + if [ $? = 0 ]; then + kill -HUP $OBEXD_PID + else + logger "$0: obexd is not running" + fi + SYNCD_PID=`pidof syncd` + if [ $? = 0 ]; then + kill $SYNCD_PID + else + logger "$0: syncd is not running" + fi + + sleep 2 + /sbin/rmmod g_nokia + if [ $? != 0 ]; then + logger "$0: failed to rmmod g_nokia!" + exit 1 + fi +fi + +/sbin/lsmod | grep g_file_storage > /dev/null +if [ $? = 0 ]; then + logger "$0: removing g_file_storage" + + GADGETPATH='/sys/devices/platform/musb_hdrc/gadget' + LUN0='gadget-lun0' + LUN1='gadget-lun1' + + if [ $# = 0 ]; then + # unload all + echo "" > $GADGETPATH/$LUN0/file + echo "" > $GADGETPATH/$LUN1/file + exit 0 + fi + + # NOTE: works only for 1 or 2 device arguments + for lun in $LUN0 $LUN1; do + grep $1 $GADGETPATH/$lun/file > /dev/null + RC=$? + + if [ $# = 2 ]; then + grep $2 $GADGETPATH/$lun/file > /dev/null + RC2=$? + else + RC2=1 + fi + + if [ $RC = 0 -o $RC2 = 0 ]; then + echo "" > $GADGETPATH/$lun/file + fi + done + + /sbin/rmmod g_file_storage + if [ $? != 0 ]; then + logger "$0: failed to rmmod g_file_storage!" + exit 1 + fi +fi + +/sbin/lsmod | grep g_ether > /dev/null +if [ $? != 0 ]; then + /sbin/modprobe g_ether + RC=$? +fi + +if [ $RC != 0 ]; then + logger "$0: failed to install g_ether" + exit 1 +fi + +exit 0 diff --git a/mtetherd.c b/mtetherd.c index 6305bc7..ec74151 100644 --- a/mtetherd.c +++ b/mtetherd.c @@ -85,12 +85,6 @@ static int launch(const char *args[]) { fprintf(stderr, "Can't fork external process %s: %s\n", args[0], strerror(errno)); return -1; } else { - /*printf("Launching:"); - size_t a; - for (a = 0; args[a]; a++) { - printf(" %s", args[a]); - } - printf("\n");*/ int status = 0; if (waitpid(pid, &status, WNOHANG) == -1) { fprintf(stderr, "Error waiting for child process completion: %s\n", strerror(errno)); diff --git a/mtetherd.sudoers b/mtetherd.sudoers new file mode 100644 index 0000000..24bb3d0 --- /dev/null +++ b/mtetherd.sudoers @@ -0,0 +1,2 @@ +user ALL = NOPASSWD: /usr/sbin/mtetherd-usbnet-enable.sh +user ALL = NOPASSWD: /usr/sbin/mtetherd-usbnet-disable.sh diff --git a/status.c b/status.c index 52efb5e..7ca2364 100644 --- a/status.c +++ b/status.c @@ -19,6 +19,7 @@ */ #include +#include #include #include #include @@ -43,6 +44,25 @@ static void mtetherd_status_plugin_class_init(MTetherDStatusPluginClass *klass) g_type_class_add_private(klass, sizeof(MTetherDStatusPluginPrivate)); } +static gboolean get_usbnet_enabled(MTetherDStatusPlugin *plugin) { + FILE *fp = fopen("/proc/modules", "r"); + if (!fp) { + // fallback (useless?) + return plugin->priv->neton; + } + gboolean found = FALSE; + char *line = NULL; + while (!found && getline(&line, NULL, fp) != -1) { + if (strncmp(line, "g_ether", 7) == 0) { + found = TRUE; + } + free(line); + line = NULL; + } + fclose(fp); + return found; +} + static void set_button_text(GtkWidget *button, gboolean enabled) { if (enabled) { gtk_button_set_label(GTK_BUTTON(button), "Disable USB Networking"); @@ -51,16 +71,42 @@ static void set_button_text(GtkWidget *button, gboolean enabled) { } } +static gboolean launch_usbnet_script(gboolean enable) { + const char *arg; + if (enable) { + arg = "/usr/sbin/mtetherd-usbnet-enable.sh"; + } else { + arg = "/usr/sbin/mtetherd-usbnet-disable.sh"; + } + const char *command[] = { "/usr/bin/sudo", arg, NULL }; + pid_t pid = fork(); + if (pid == 0) { + if (execv(command[0], (char **const) command) == -1) { + exit(1); + } + } else if (pid == -1) { + // error forking + return FALSE; + } + // launch ok + return TRUE; +} + static void enable_usb_net_clicked(GtkWidget *button, gpointer user) { MTetherDStatusPlugin *plugin = MTETHERD_STATUS_PLUGIN(user); if (button == plugin->priv->button) { - plugin->priv->neton = !plugin->priv->neton; - set_button_text(plugin->priv->button, plugin->priv->neton); - if (plugin->priv->neton) { - // enable + gboolean enabled = get_usbnet_enabled(plugin); + set_button_text(plugin->priv->button, enabled); + if (enabled) { + if (launch_usbnet_script(FALSE)) { + // launch ok + plugin->priv->neton = FALSE; + } } else { - // disable + if (launch_usbnet_script(TRUE)) { + plugin->priv->neton = TRUE; + } } } } -- 1.7.9.5