aboutsummaryrefslogtreecommitdiffstats
path: root/wsutil
diff options
context:
space:
mode:
authorJeff Morriss <jeff.morriss@ulticom.com>2008-06-30 17:16:29 +0000
committerJeff Morriss <jeff.morriss@ulticom.com>2008-06-30 17:16:29 +0000
commit83f9ecf96fdfac34b5c19c12f4bee9b04c12420c (patch)
tree90375f8b4b84d99576ef5374cdb7e7d596d92329 /wsutil
parent6d4f9c1edab6390b383858efc26c692f1f2ddb1f (diff)
Move privileges.c and unicode-utils.c from epan to wsutil (so things like
capinfos and dumpcap don't need to depend on libwireshark nor directly pull in those modules). Because capinfos and editcap were only being linked with privileges.c if we had plugins, this allows those programs to be linked when someone is compiling --without-plugins. svn path=/trunk/; revision=25640
Diffstat (limited to 'wsutil')
-rw-r--r--wsutil/Makefile.common12
-rw-r--r--wsutil/privileges.c301
-rw-r--r--wsutil/privileges.h74
-rw-r--r--wsutil/unicode-utils.c128
-rw-r--r--wsutil/unicode-utils.h54
5 files changed, 565 insertions, 4 deletions
diff --git a/wsutil/Makefile.common b/wsutil/Makefile.common
index 6684c8faad..df0c87d6e0 100644
--- a/wsutil/Makefile.common
+++ b/wsutil/Makefile.common
@@ -28,9 +28,13 @@
# into a list of object files by replacing ".c" with ".obj") or files
# generated from YACC or Lex files (as Automake doesn't want them in
# _SOURCES variables).
-LIBWSUTIL_SRC = \
- mpeg-audio.c
+LIBWSUTIL_SRC = \
+ mpeg-audio.c \
+ privileges.c \
+ unicode-utils.c
# Header files that are not generated from other files
-LIBWSUTIL_INCLUDES = \
- mpeg-audio.h
+LIBWSUTIL_INCLUDES = \
+ mpeg-audio.h \
+ privileges.h \
+ unicode-utils.h
diff --git a/wsutil/privileges.c b/wsutil/privileges.c
new file mode 100644
index 0000000000..19adbd3d70
--- /dev/null
+++ b/wsutil/privileges.c
@@ -0,0 +1,301 @@
+/* privileges.c
+ * Routines for handling privileges, e.g. set-UID and set-GID on UNIX.
+ *
+ * $Id$
+ *
+ * Wireshark - Network traffic analyzer
+ * By Gerald Combs <gerald@wireshark.org>
+ * Copyright 2006 Gerald Combs
+ *
+ * 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.
+ */
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#if defined(HAVE_SETRESUID) || defined(HAVE_SETREGUID)
+#define _GNU_SOURCE /* Otherwise [sg]etres[gu]id won't be defined on Linux */
+#endif
+
+#include <glib.h>
+
+#include "privileges.h"
+
+#ifdef _WIN32
+#include <windows.h>
+#include <wchar.h>
+#include <tchar.h>
+
+/*
+ * Called when the program starts, to save whatever credential information
+ * we'll need later.
+ */
+void
+get_credential_info(void)
+{
+ npf_sys_is_running();
+}
+
+/*
+ * For now, we say the program wasn't started with special privileges.
+ * There are ways of running programs with credentials other than those
+ * for the session in which it's run, but I don't know whether that'd be
+ * done with Wireshark/TShark or not.
+ */
+gboolean
+started_with_special_privs(void)
+{
+ return FALSE;
+}
+
+/*
+ * For now, we say the program isn't running with special privileges.
+ * There are ways of running programs with credentials other than those
+ * for the session in which it's run, but I don't know whether that'd be
+ * done with Wireshark/TShark or not.
+ */
+gboolean
+running_with_special_privs(void)
+{
+ return FALSE;
+}
+
+/*
+ * For now, we don't do anything when asked to relinquish special privileges.
+ */
+void
+relinquish_special_privs_perm(void)
+{
+}
+
+/*
+ * Get the current username. String must be g_free()d after use.
+ */
+gchar *
+get_cur_username(void) {
+ gchar *username;
+ username = g_strdup("UNKNOWN");
+ return username;
+}
+
+/*
+ * Get the current group. String must be g_free()d after use.
+ */
+gchar *
+get_cur_groupname(void) {
+ gchar *groupname;
+ groupname = g_strdup("UNKNOWN");
+ return groupname;
+}
+
+/*
+ * If npf.sys is running, return TRUE.
+ */
+gboolean
+npf_sys_is_running() {
+ SC_HANDLE h_scm, h_serv;
+ SERVICE_STATUS ss;
+
+ h_scm = OpenSCManager(NULL, NULL, 0);
+ if (!h_scm)
+ return FALSE;
+
+ h_serv = OpenService(h_scm, _T("npf"), SC_MANAGER_CONNECT|SERVICE_QUERY_STATUS);
+ if (!h_serv)
+ return FALSE;
+
+ if (QueryServiceStatus(h_serv, &ss)) {
+ if (ss.dwCurrentState & SERVICE_RUNNING)
+ return TRUE;
+ }
+ return FALSE;
+}
+
+
+#else /* _WIN32 */
+
+#ifdef HAVE_SYS_TYPES_H
+# include <sys/types.h>
+#endif
+
+#ifdef HAVE_UNISTD_H
+#include <unistd.h>
+#endif
+
+#ifdef HAVE_PWD_H
+#include <pwd.h>
+#endif
+
+#ifdef HAVE_GRP_H
+#include <grp.h>
+#endif
+
+#include <glib.h>
+#include <string.h>
+#include <errno.h>
+
+static uid_t ruid, euid;
+static gid_t rgid, egid;
+static gboolean get_credential_info_called = FALSE;
+
+/*
+ * Called when the program starts, to save whatever credential information
+ * we'll need later.
+ * That'd be the real and effective UID and GID on UNIX.
+ */
+void
+get_credential_info(void)
+{
+ ruid = getuid();
+ euid = geteuid();
+ rgid = getgid();
+ egid = getegid();
+
+ get_credential_info_called = TRUE;
+}
+
+/*
+ * "Started with special privileges" means "started out set-UID or set-GID",
+ * or run as the root user or group.
+ */
+gboolean
+started_with_special_privs(void)
+{
+ g_assert(get_credential_info_called);
+#ifdef HAVE_ISSETUGID
+ return issetugid();
+#else
+ return (ruid != euid || rgid != egid || ruid == 0 || rgid == 0);
+#endif
+}
+
+/*
+ * Return TRUE if the real, effective, or saved (if we can check it) user
+ * ID or group are 0.
+ */
+gboolean
+running_with_special_privs(void)
+{
+#ifdef HAVE_SETRESUID
+ uid_t ru, eu, su;
+#endif
+#ifdef HAVE_SETRESGID
+ gid_t rg, eg, sg;
+#endif
+
+#ifdef HAVE_SETRESUID
+ getresuid(&ru, &eu, &su);
+ if (ru == 0 || eu == 0 || su == 0)
+ return TRUE;
+#else
+ if (getuid() == 0 || geteuid() == 0)
+ return TRUE;
+#endif
+#ifdef HAVE_SETRESGID
+ getresgid(&rg, &eg, &sg);
+ if (rg == 0 || eg == 0 || sg == 0)
+ return TRUE;
+#else
+ if (getgid() == 0 || getegid() == 0)
+ return TRUE;
+#endif
+ return FALSE;
+}
+
+/*
+ * Permanently relinquish set-UID and set-GID privileges.
+ * Ignore errors for now - if we have the privileges, we should
+ * be able to relinquish them.
+ */
+
+void
+relinquish_special_privs_perm(void)
+{
+ /*
+ * If we were started with special privileges, set the
+ * real and effective group and user IDs to the original
+ * values of the real and effective group and user IDs.
+ * If we're not, don't bother - doing so seems to mung
+ * our group set, at least in OS X 10.5.
+ *
+ * (Set the effective UID last - that takes away our
+ * rights to set anything else.)
+ */
+ if (started_with_special_privs()) {
+#ifdef HAVE_SETRESGID
+ setresgid(rgid, rgid, rgid);
+#else
+ setgid(rgid);
+ setegid(rgid);
+#endif
+
+#ifdef HAVE_SETRESUID
+ setresuid(ruid, ruid, ruid);
+#else
+ setuid(ruid);
+ seteuid(ruid);
+#endif
+ }
+}
+
+/*
+ * Get the current username. String must be g_free()d after use.
+ */
+gchar *
+get_cur_username(void) {
+ gchar *username;
+ struct passwd *pw = getpwuid(getuid());
+
+ if (pw) {
+ username = g_strdup(pw->pw_name);
+ } else {
+ username = g_strdup("UNKNOWN");
+ }
+ endpwent();
+ return username;
+}
+
+/*
+ * Get the current group. String must be g_free()d after use.
+ */
+gchar *
+get_cur_groupname(void) {
+ gchar *groupname;
+ struct group *gr = getgrgid(getgid());
+
+ if (gr) {
+ groupname = g_strdup(gr->gr_name);
+ } else {
+ groupname = g_strdup("UNKNOWN");
+ }
+ endgrent();
+ return groupname;
+}
+
+#endif /* _WIN32 */
+
+/*
+ * Editor modelines
+ *
+ * Local Variables:
+ * c-basic-offset: 8
+ * tab-width: 8
+ * indent-tabs-mode: tabs
+ * End:
+ *
+ * ex: set shiftwidth=8 tabstop=8 noexpandtab
+ * :indentSize=8:tabSize=8:noTabs=false:
+ */
diff --git a/wsutil/privileges.h b/wsutil/privileges.h
new file mode 100644
index 0000000000..e45b77807f
--- /dev/null
+++ b/wsutil/privileges.h
@@ -0,0 +1,74 @@
+/* privileges.h
+ * Declarations of routines for handling privileges.
+ *
+ * $Id$
+ *
+ * Wireshark - Network traffic analyzer
+ * By Gerald Combs <gerald@wireshark.org>
+ * Copyright 2006 Gerald Combs
+ *
+ * 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.
+ */
+
+/**
+ * Called when the program starts, to save whatever credential information
+ * we'll need later.
+ */
+extern void get_credential_info(void);
+
+/**
+ * Was this program started with special privileges? get_credential_info()
+ * MUST be called before calling this.
+ * @return TRUE if the program was started with special privileges,
+ * FALSE otherwise.
+ */
+extern gboolean started_with_special_privs(void);
+
+/**
+ * Is this program running with special privileges? get_credential_info()
+ * MUST be called before calling this.
+ * @return TRUE if the program is running with special privileges,
+ * FALSE otherwise.
+ */
+extern gboolean running_with_special_privs(void);
+
+/**
+ * Permanently relinquish special privileges. get_credential_info()
+ * MUST be called before calling this.
+ */
+extern void relinquish_special_privs_perm(void);
+
+/**
+ * Get the current username. String must be g_free()d after use.
+ * @return A freshly g_alloc()ed string containing the username,
+ * or "UNKNOWN" on failure.
+ */
+extern gchar *get_cur_username(void);
+
+/**
+ * Get the current group. String must be g_free()d after use.
+ * @return A freshly g_alloc()ed string containing the group,
+ * or "UNKNOWN" on failure.
+ */
+extern gchar *get_cur_groupname(void);
+
+#ifdef _WIN32
+/**
+ * Check to see if npf.sys is running.
+ * @return TRUE if npf.sys is running, FALSE if it's not or if there was
+ * an error checking its status.
+ */
+extern gboolean npf_sys_is_running();
+#endif
diff --git a/wsutil/unicode-utils.c b/wsutil/unicode-utils.c
new file mode 100644
index 0000000000..dfa6eb32d5
--- /dev/null
+++ b/wsutil/unicode-utils.c
@@ -0,0 +1,128 @@
+/* unicode-utils.c
+ * Unicode utility routines
+ *
+ * $Id$
+ *
+ * Wireshark - Network traffic analyzer
+ * By Gerald Combs <gerald@wireshark.org>
+ * Copyright 2006 Gerald Combs
+ *
+ * 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.
+ */
+
+#ifdef _WIN32
+
+#include <glib.h>
+#include "unicode-utils.h"
+
+#include <windows.h>
+#include <tchar.h>
+#include <wchar.h>
+
+/** @file
+ * Unicode utilities (internal interface)
+ *
+ * We define UNICODE and _UNICODE under Windows. This means that
+ * Windows SDK routines expect UTF-16 strings, in contrast to newer
+ * versions of Glib and GTK+ which expect UTF-8. This module provides
+ * convenience routines for converting between UTF-8 and UTF-16.
+ */
+
+#define INITIAL_UTFBUF_SIZE 128
+
+/*
+ * XXX - Should we use g_utf8_to_utf16() and g_utf16_to_utf8()
+ * instead? The goal of the functions below was to provide simple
+ * wrappers for UTF-8 <-> UTF-16 conversion without making the
+ * caller worry about freeing up memory afterward.
+ */
+
+/* Convert from UTF-8 to UTF-16. */
+wchar_t * utf_8to16(const char *utf8str) {
+ static wchar_t *utf16buf[3];
+ static int utf16buf_len[3];
+ static int idx;
+
+ if (utf8str == NULL)
+ return NULL;
+
+ idx = (idx + 1) % 3;
+
+ /*
+ * Allocate the buffer if it's not already allocated.
+ */
+ if (utf16buf[idx] == NULL) {
+ utf16buf_len[idx] = INITIAL_UTFBUF_SIZE;
+ utf16buf[idx] = g_malloc(utf16buf_len[idx] * sizeof(wchar_t));
+ }
+
+ while (MultiByteToWideChar(CP_UTF8, 0, utf8str,
+ -1, NULL, 0) >= utf16buf_len[idx]) {
+ /*
+ * Double the buffer's size if it's not big enough.
+ * The size of the buffer starts at 128, so doubling its size
+ * adds at least another 128 bytes, which is more than enough
+ * for one more character plus a terminating '\0'.
+ */
+ utf16buf_len[idx] *= 2;
+ utf16buf[idx] = g_realloc(utf16buf[idx], utf16buf_len[idx] * sizeof(wchar_t));
+ }
+
+ if (MultiByteToWideChar(CP_UTF8, 0, utf8str,
+ -1, utf16buf[idx], utf16buf_len[idx]) == 0)
+ return NULL;
+
+ return utf16buf[idx];
+}
+
+/* Convert from UTF-16 to UTF-8. */
+gchar * utf_16to8(const wchar_t *utf16str) {
+ static gchar *utf8buf[3];
+ static int utf8buf_len[3];
+ static int idx;
+
+ if (utf16str == NULL)
+ return NULL;
+
+ idx = (idx + 1) % 3;
+
+ /*
+ * Allocate the buffer if it's not already allocated.
+ */
+ if (utf8buf[idx] == NULL) {
+ utf8buf_len[idx] = INITIAL_UTFBUF_SIZE;
+ utf8buf[idx] = g_malloc(utf8buf_len[idx]);
+ }
+
+ while (WideCharToMultiByte(CP_UTF8, 0, utf16str, -1,
+ NULL, 0, NULL, NULL) >= utf8buf_len[idx]) {
+ /*
+ * Double the buffer's size if it's not big enough.
+ * The size of the buffer starts at 128, so doubling its size
+ * adds at least another 128 bytes, which is more than enough
+ * for one more character plus a terminating '\0'.
+ */
+ utf8buf_len[idx] *= 2;
+ utf8buf[idx] = g_realloc(utf8buf[idx], utf8buf_len[idx]);
+ }
+
+ if (WideCharToMultiByte(CP_UTF8, 0, utf16str, -1,
+ utf8buf[idx], utf8buf_len[idx], NULL, NULL) == 0)
+ return NULL;
+
+ return utf8buf[idx];
+}
+
+#endif /* _WIN32 */
diff --git a/wsutil/unicode-utils.h b/wsutil/unicode-utils.h
new file mode 100644
index 0000000000..2a08be71af
--- /dev/null
+++ b/wsutil/unicode-utils.h
@@ -0,0 +1,54 @@
+/* unicode-utils.h
+ * Unicode utility definitions
+ *
+ * $Id$
+ *
+ * Wireshark - Network traffic analyzer
+ * By Gerald Combs <gerald@wireshark.org>
+ * Copyright 2006 Gerald Combs
+ *
+ * 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.
+ */
+
+#ifndef __UNICODEUTIL_H__
+#define __UNICODEUTIL_H__
+
+#ifdef _WIN32
+
+/**
+ * @file Unicode convenience routines.
+ */
+
+/** Given a UTF-8 string, convert it to UTF-16. This is meant to be used
+ * to convert between GTK+ 2.x (UTF-8) to Windows (UTF-16).
+ *
+ * @param utf8str The string to convert. May be NULL.
+ * @return The string converted to UTF-16. If utf8str is NULL, returns
+ * NULL. The return value should NOT be freed by the caller.
+ */
+wchar_t * utf_8to16(const char *utf8str);
+
+/** Given a UTF-16 string, convert it to UTF-8. This is meant to be used
+ * to convert between GTK+ 2.x (UTF-8) to Windows (UTF-16).
+ *
+ * @param utf16str The string to convert. May be NULL.
+ * @return The string converted to UTF-8. If utf16str is NULL, returns
+ * NULL. The return value should NOT be freed by the caller.
+ */
+gchar * utf_16to8(const wchar_t *utf16str);
+
+#endif /* _WIN32 */
+
+#endif /* __UNICODEUTIL_H__ */