From 3559b6400ee17f9b176ce4e82559a79320c7a08a Mon Sep 17 00:00:00 2001 From: David Zeuthen Date: Tue, 6 Jan 2009 19:15:26 -0500 Subject: [PATCH] add subject interface and some classes implementing this interface --- data/org.freedesktop.PolicyKit1.Authority.xml | 5 - docs/polkit/polkit-docs.xml | 8 +- src/polkit/Makefile.am | 10 + src/polkit/polkit.h | 5 + src/polkit/polkitsubject.c | 71 +++++ src/polkit/polkitsubject.h | 55 ++++ src/polkit/polkitsystembusname.c | 183 +++++++++++++ src/polkit/polkitsystembusname.h | 53 ++++ src/polkit/polkittypes.h | 14 + src/polkit/polkitunixgroup.c | 175 +++++++++++++ src/polkit/polkitunixgroup.h | 53 ++++ src/polkit/polkitunixprocess.c | 358 ++++++++++++++++++++++++++ src/polkit/polkitunixprocess.h | 56 ++++ src/polkit/polkitunixuser.c | 175 +++++++++++++ src/polkit/polkitunixuser.h | 53 ++++ 15 files changed, 1267 insertions(+), 7 deletions(-) create mode 100644 src/polkit/polkitsubject.c create mode 100644 src/polkit/polkitsubject.h create mode 100644 src/polkit/polkitsystembusname.c create mode 100644 src/polkit/polkitsystembusname.h create mode 100644 src/polkit/polkitunixgroup.c create mode 100644 src/polkit/polkitunixgroup.h create mode 100644 src/polkit/polkitunixprocess.c create mode 100644 src/polkit/polkitunixprocess.h create mode 100644 src/polkit/polkitunixuser.c create mode 100644 src/polkit/polkitunixuser.h diff --git a/data/org.freedesktop.PolicyKit1.Authority.xml b/data/org.freedesktop.PolicyKit1.Authority.xml index 90f405d..9fd3621 100644 --- a/data/org.freedesktop.PolicyKit1.Authority.xml +++ b/data/org.freedesktop.PolicyKit1.Authority.xml @@ -88,11 +88,6 @@ - - - - - diff --git a/docs/polkit/polkit-docs.xml b/docs/polkit/polkit-docs.xml index 1edfaf0..bb9d379 100644 --- a/docs/polkit/polkit-docs.xml +++ b/docs/polkit/polkit-docs.xml @@ -64,15 +64,19 @@ - - + + + + Extending PolicyKit + + diff --git a/src/polkit/Makefile.am b/src/polkit/Makefile.am index 9c69b5f..1761076 100644 --- a/src/polkit/Makefile.am +++ b/src/polkit/Makefile.am @@ -43,6 +43,11 @@ libpolkit_gobject_1include_HEADERS = \ polkitactiondescription.h \ polkitauthority.h \ polkiterror.h \ + polkitsubject.h \ + polkitunixuser.h \ + polkitunixgroup.h \ + polkitunixprocess.h \ + polkitsystembusname.h \ $(NULL) libpolkit_gobject_1_la_SOURCES = \ @@ -51,6 +56,11 @@ libpolkit_gobject_1_la_SOURCES = \ polkitactiondescription.c polkitactiondescription.h \ polkitauthority.c polkitauthority.h \ polkiterror.c polkiterror.h \ + polkitsubject.c polkitsubject.h \ + polkitunixuser.c polkitunixuser.h \ + polkitunixgroup.c polkitunixgroup.h \ + polkitunixprocess.c polkitunixprocess.h \ + polkitsystembusname.c polkitsystembusname.h \ $(NULL) libpolkit_gobject_1_la_CFLAGS = \ diff --git a/src/polkit/polkit.h b/src/polkit/polkit.h index 6cab9b1..1520fed 100644 --- a/src/polkit/polkit.h +++ b/src/polkit/polkit.h @@ -27,6 +27,11 @@ #include #include #include +#include +#include +#include +#include +#include #undef _POLKIT_INSIDE_POLKIT_H diff --git a/src/polkit/polkitsubject.c b/src/polkit/polkitsubject.c new file mode 100644 index 0000000..d950fb1 --- /dev/null +++ b/src/polkit/polkitsubject.c @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2008 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General + * Public License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307, USA. + * + * Author: David Zeuthen + */ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include "polkitsubject.h" + +static void +base_init (gpointer g_iface) +{ +} + +GType +polkit_subject_get_type (void) +{ + static GType iface_type = 0; + + if (iface_type == 0) + { + static const GTypeInfo info = + { + sizeof (PolkitSubjectIface), + base_init, /* base_init */ + NULL, /* base_finalize */ + NULL, /* class_init */ + NULL, /* class_finalize */ + NULL, /* class_data */ + 0, /* instance_size */ + 0, /* n_preallocs */ + NULL, /* instance_init */ + NULL /* value_table */ + }; + + iface_type = g_type_register_static (G_TYPE_INTERFACE, "PolkitSubject", &info, 0); + + g_type_interface_add_prerequisite (iface_type, G_TYPE_OBJECT); + } + + return iface_type; +} + +gboolean +polkit_subject_equal (PolkitSubject *a, + PolkitSubject *b) +{ + if (!g_type_is_a (G_TYPE_FROM_INSTANCE (a), G_TYPE_FROM_INSTANCE (b))) + return FALSE; + + return POLKIT_SUBJECT_GET_IFACE (a)->equal (a, b); +} + diff --git a/src/polkit/polkitsubject.h b/src/polkit/polkitsubject.h new file mode 100644 index 0000000..8106b0e --- /dev/null +++ b/src/polkit/polkitsubject.h @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2008 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General + * Public License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307, USA. + * + * Author: David Zeuthen + */ + +#ifndef __POLKIT_SUBJECT_H +#define __POLKIT_SUBJECT_H + +#include +#include +#include + +G_BEGIN_DECLS + +#define POLKIT_TYPE_SUBJECT (polkit_subject_get_type()) +#define POLKIT_SUBJECT(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), POLKIT_TYPE_SUBJECT, PolkitSubject)) +#define POLKIT_IS_SUBJECT(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), POLKIT_TYPE_SUBJECT)) +#define POLKIT_SUBJECT_GET_IFACE(o) (G_TYPE_INSTANCE_GET_INTERFACE((o), POLKIT_TYPE_SUBJECT, PolkitSubjectIface)) + +#if 0 +typedef struct _PolkitSubject PolkitSubject; /* Dummy typedef */ +#endif +typedef struct _PolkitSubjectIface PolkitSubjectIface; + +struct _PolkitSubjectIface +{ + GTypeInterface parent_iface; + + gboolean (*equal) (PolkitSubject *a, + PolkitSubject *b); +}; + +GType polkit_subject_get_type (void) G_GNUC_CONST; +gboolean polkit_subject_equal (PolkitSubject *a, + PolkitSubject *b); + +G_END_DECLS + +#endif /* __POLKIT_SUBJECT_H */ diff --git a/src/polkit/polkitsystembusname.c b/src/polkit/polkitsystembusname.c new file mode 100644 index 0000000..4583ccc --- /dev/null +++ b/src/polkit/polkitsystembusname.c @@ -0,0 +1,183 @@ +/* + * Copyright (C) 2008 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General + * Public License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307, USA. + * + * Author: David Zeuthen + */ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include +#include "polkitsystembusname.h" +#include "polkitsubject.h" +#include "polkitprivate.h" + +/** + * SECTION:polkitsystembusname + * @title: PolkitSystemBusName + * @short_description: Unique system bus name + * + * Encapsulates a process with a unique name on the system bus. + */ + +struct _PolkitSystemBusName +{ + GObject parent_instance; + + gchar *name; +}; + +struct _PolkitSystemBusNameClass +{ + GObjectClass parent_class; +}; + +enum +{ + PROP_0, + PROP_NAME, +}; + +static void subject_iface_init (PolkitSubjectIface *subject_iface); + +G_DEFINE_TYPE_WITH_CODE (PolkitSystemBusName, polkit_system_bus_name, G_TYPE_OBJECT, + G_IMPLEMENT_INTERFACE (POLKIT_TYPE_SUBJECT, subject_iface_init) + ); + +static void +polkit_system_bus_name_init (PolkitSystemBusName *system_bus_name) +{ +} + +static void +polkit_system_bus_name_finalize (GObject *object) +{ + PolkitSystemBusName *system_bus_name = POLKIT_SYSTEM_BUS_NAME (object); + + g_free (system_bus_name->name); +} + +static void +polkit_system_bus_name_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + PolkitSystemBusName *system_bus_name = POLKIT_SYSTEM_BUS_NAME (object); + + switch (prop_id) + { + case PROP_NAME: + g_value_set_string (value, system_bus_name->name); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +polkit_system_bus_name_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + PolkitSystemBusName *system_bus_name = POLKIT_SYSTEM_BUS_NAME (object); + + switch (prop_id) + { + case PROP_NAME: + polkit_system_bus_name_set_name (system_bus_name, g_value_get_string (value)); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +polkit_system_bus_name_class_init (PolkitSystemBusNameClass *klass) +{ + GObjectClass *gobject_class = G_OBJECT_CLASS (klass); + + gobject_class->get_property = polkit_system_bus_name_get_property; + gobject_class->set_property = polkit_system_bus_name_set_property; + gobject_class->finalize = polkit_system_bus_name_finalize; + + /** + * PolkitSystemBusName:name: + * + * The unique name on the system message bus. + */ + g_object_class_install_property (gobject_class, + PROP_NAME, + g_param_spec_string ("name", + "Name", + "The unique name on the system message bus", + NULL, + G_PARAM_CONSTRUCT | + G_PARAM_READWRITE | + G_PARAM_STATIC_NAME | + G_PARAM_STATIC_BLURB | + G_PARAM_STATIC_NICK)); + +} + +const gchar * +polkit_system_bus_name_get_name (PolkitSystemBusName *system_bus_name) +{ + return system_bus_name->name; +} + +void +polkit_system_bus_name_set_name (PolkitSystemBusName *system_bus_name, + const gchar *name) +{ + g_free (system_bus_name->name); + system_bus_name->name = g_strdup (name); +} + +PolkitSubject * +polkit_system_bus_name_new (const gchar *name) +{ + return POLKIT_SUBJECT (g_object_new (POLKIT_TYPE_SYSTEM_BUS_NAME, + "name", name, + NULL)); +} + +static gboolean +polkit_system_bus_name_equal (PolkitSubject *a, + PolkitSubject *b) +{ + PolkitSystemBusName *name_a; + PolkitSystemBusName *name_b; + + name_a = POLKIT_SYSTEM_BUS_NAME (a); + name_b = POLKIT_SYSTEM_BUS_NAME (b); + + return strcmp (name_a->name, name_b->name) == 0; +} + +static void +subject_iface_init (PolkitSubjectIface *subject_iface) +{ + subject_iface->equal = polkit_system_bus_name_equal; +} diff --git a/src/polkit/polkitsystembusname.h b/src/polkit/polkitsystembusname.h new file mode 100644 index 0000000..7894a1e --- /dev/null +++ b/src/polkit/polkitsystembusname.h @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2008 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General + * Public License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307, USA. + * + * Author: David Zeuthen + */ + +#ifndef __POLKIT_SYSTEM_BUS_NAME_H +#define __POLKIT_SYSTEM_BUS_NAME_H + +#include +#include +#include +#include +#include + +G_BEGIN_DECLS + +#define POLKIT_TYPE_SYSTEM_BUS_NAME (polkit_system_bus_name_get_type()) +#define POLKIT_SYSTEM_BUS_NAME(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), POLKIT_TYPE_SYSTEM_BUS_NAME, PolkitSystemBusName)) +#define POLKIT_SYSTEM_BUS_NAME_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), POLKIT_TYPE_SYSTEM_BUS_NAME, PolkitSystemBusNameClass)) +#define POLKIT_SYSTEM_BUS_NAME_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), POLKIT_TYPE_SYSTEM_BUS_NAME, PolkitSystemBusNameClass)) +#define POLKIT_IS_SYSTEM_BUS_NAME(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), POLKIT_TYPE_SYSTEM_BUS_NAME)) +#define POLKIT_IS_SYSTEM_BUS_NAME_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), POLKIT_TYPE_SYSTEM_BUS_NAME)) + +#if 0 +typedef struct _PolkitSystemBusName PolkitSystemBusName; +#endif +typedef struct _PolkitSystemBusNameClass PolkitSystemBusNameClass; + +GType polkit_system_bus_name_get_type (void) G_GNUC_CONST; +PolkitSubject *polkit_system_bus_name_new (const gchar *name); +const gchar *polkit_system_bus_name_get_name (PolkitSystemBusName *system_bus_name); +void polkit_system_bus_name_set_name (PolkitSystemBusName *system_bus_name, + const gchar *name); + +G_END_DECLS + +#endif /* __POLKIT_SYSTEM_BUS_NAME_H */ diff --git a/src/polkit/polkittypes.h b/src/polkit/polkittypes.h index 9d1dab8..8e0c3f5 100644 --- a/src/polkit/polkittypes.h +++ b/src/polkit/polkittypes.h @@ -30,5 +30,19 @@ typedef struct _PolkitAuthority PolkitAuthority; struct _PolkitActionDescription; typedef struct _PolkitActionDescription PolkitActionDescription; +typedef struct _PolkitSubject PolkitSubject; /* Dummy typedef */ + +struct _PolkitUnixUser; +typedef struct _PolkitUnixUser PolkitUnixUser; + +struct _PolkitUnixGroup; +typedef struct _PolkitUnixGroup PolkitUnixGroup; + +struct _PolkitUnixProcess; +typedef struct _PolkitUnixProcess PolkitUnixProcess; + +struct _PolkitSystemBusName; +typedef struct _PolkitSystemBusName PolkitSystemBusName; + #endif /* __POLKIT_TYPES_H */ diff --git a/src/polkit/polkitunixgroup.c b/src/polkit/polkitunixgroup.c new file mode 100644 index 0000000..d70da5f --- /dev/null +++ b/src/polkit/polkitunixgroup.c @@ -0,0 +1,175 @@ +/* + * Copyright (C) 2008 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General + * Public License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307, USA. + * + * Author: David Zeuthen + */ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include +#include "polkitunixgroup.h" +#include "polkitsubject.h" +#include "polkitprivate.h" + +/** + * SECTION:polkitunixgroup + * @title: PolkitUnixGroup + * @short_description: Unix groups + * + * Encapsulates a UNIX group. + */ + +struct _PolkitUnixGroup +{ + GObject parent_instance; + + gid_t gid; +}; + +struct _PolkitUnixGroupClass +{ + GObjectClass parent_class; +}; + +enum +{ + PROP_0, + PROP_GID, +}; + +static void subject_iface_init (PolkitSubjectIface *subject_iface); + +G_DEFINE_TYPE_WITH_CODE (PolkitUnixGroup, polkit_unix_group, G_TYPE_OBJECT, + G_IMPLEMENT_INTERFACE (POLKIT_TYPE_SUBJECT, subject_iface_init) + ); + +static void +polkit_unix_group_init (PolkitUnixGroup *unix_group) +{ +} + +static void +polkit_unix_group_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + PolkitUnixGroup *unix_group = POLKIT_UNIX_GROUP (object); + + switch (prop_id) + { + case PROP_GID: + g_value_set_uint (value, unix_group->gid); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +polkit_unix_group_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + PolkitUnixGroup *unix_group = POLKIT_UNIX_GROUP (object); + + switch (prop_id) + { + case PROP_GID: + unix_group->gid = g_value_get_uint (value); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +polkit_unix_group_class_init (PolkitUnixGroupClass *klass) +{ + GObjectClass *gobject_class = G_OBJECT_CLASS (klass); + + gobject_class->get_property = polkit_unix_group_get_property; + gobject_class->set_property = polkit_unix_group_set_property; + + /** + * PolkitUnixGroup:gid: + * + * The UNIX group id. + */ + g_object_class_install_property (gobject_class, + PROP_GID, + g_param_spec_uint ("gid", + "Group ID", + "The UNIX group ID", + 0, + G_MAXUINT, + 0, + G_PARAM_CONSTRUCT | + G_PARAM_READWRITE | + G_PARAM_STATIC_NAME | + G_PARAM_STATIC_BLURB | + G_PARAM_STATIC_NICK)); + +} + +gid_t +polkit_unix_group_get_gid (PolkitUnixGroup *group) +{ + return group->gid; +} + +void +polkit_unix_group_set_gid (PolkitUnixGroup *group, + gid_t gid) +{ + group->gid = gid; +} + +PolkitSubject * +polkit_unix_group_new (gid_t gid) +{ + return POLKIT_SUBJECT (g_object_new (POLKIT_TYPE_UNIX_GROUP, + "gid", gid, + NULL)); +} + +static gboolean +polkit_unix_group_equal (PolkitSubject *a, + PolkitSubject *b) +{ + PolkitUnixGroup *group_a; + PolkitUnixGroup *group_b; + + group_a = POLKIT_UNIX_GROUP (a); + group_b = POLKIT_UNIX_GROUP (b); + + return group_a->gid == group_b->gid; +} + +static void +subject_iface_init (PolkitSubjectIface *subject_iface) +{ + subject_iface->equal = polkit_unix_group_equal; +} diff --git a/src/polkit/polkitunixgroup.h b/src/polkit/polkitunixgroup.h new file mode 100644 index 0000000..34c6fcf --- /dev/null +++ b/src/polkit/polkitunixgroup.h @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2008 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General + * Public License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307, USA. + * + * Author: David Zeuthen + */ + +#ifndef __POLKIT_UNIX_GROUP_H +#define __POLKIT_UNIX_GROUP_H + +#include +#include +#include +#include +#include + +G_BEGIN_DECLS + +#define POLKIT_TYPE_UNIX_GROUP (polkit_unix_group_get_type()) +#define POLKIT_UNIX_GROUP(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), POLKIT_TYPE_UNIX_GROUP, PolkitUnixGroup)) +#define POLKIT_UNIX_GROUP_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), POLKIT_TYPE_UNIX_GROUP, PolkitUnixGroupClass)) +#define POLKIT_UNIX_GROUP_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), POLKIT_TYPE_UNIX_GROUP, PolkitUnixGroupClass)) +#define POLKIT_IS_UNIX_GROUP(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), POLKIT_TYPE_UNIX_GROUP)) +#define POLKIT_IS_UNIX_GROUP_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), POLKIT_TYPE_UNIX_GROUP)) + +#if 0 +typedef struct _PolkitUnixGroup PolkitUnixGroup; +#endif +typedef struct _PolkitUnixGroupClass PolkitUnixGroupClass; + +GType polkit_unix_group_get_type (void) G_GNUC_CONST; +PolkitSubject *polkit_unix_group_new (gid_t gid); +gid_t polkit_unix_group_get_gid (PolkitUnixGroup *group); +void polkit_unix_group_set_gid (PolkitUnixGroup *group, + gid_t gid); + +G_END_DECLS + +#endif /* __POLKIT_UNIX_GROUP_H */ diff --git a/src/polkit/polkitunixprocess.c b/src/polkit/polkitunixprocess.c new file mode 100644 index 0000000..4991a75 --- /dev/null +++ b/src/polkit/polkitunixprocess.c @@ -0,0 +1,358 @@ +/* + * Copyright (C) 2008 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General + * Public License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307, USA. + * + * Author: David Zeuthen + */ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include +#include +#include "polkitunixprocess.h" +#include "polkitsubject.h" +#include "polkitprivate.h" + +/** + * SECTION:polkitunixprocess + * @title: PolkitUnixProcess + * @short_description: Unix processs + * + * Encapsulates a UNIX process. + */ + +struct _PolkitUnixProcess +{ + GObject parent_instance; + + pid_t pid; + guint64 start_time; +}; + +struct _PolkitUnixProcessClass +{ + GObjectClass parent_class; +}; + +enum +{ + PROP_0, + PROP_PID, + PROP_START_TIME, +}; + +static void subject_iface_init (PolkitSubjectIface *subject_iface); + +static guint64 get_start_time_for_pid (pid_t pid); + +G_DEFINE_TYPE_WITH_CODE (PolkitUnixProcess, polkit_unix_process, G_TYPE_OBJECT, + G_IMPLEMENT_INTERFACE (POLKIT_TYPE_SUBJECT, subject_iface_init) + ); + +static void +polkit_unix_process_init (PolkitUnixProcess *unix_process) +{ +} + +static void +polkit_unix_process_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + PolkitUnixProcess *unix_process = POLKIT_UNIX_PROCESS (object); + + switch (prop_id) + { + case PROP_PID: + g_value_set_uint (value, unix_process->pid); + break; + + case PROP_START_TIME: + g_value_set_uint64 (value, unix_process->start_time); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +polkit_unix_process_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + PolkitUnixProcess *unix_process = POLKIT_UNIX_PROCESS (object); + + switch (prop_id) + { + case PROP_PID: + polkit_unix_process_set_pid (unix_process, g_value_get_uint (value)); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +polkit_unix_process_class_init (PolkitUnixProcessClass *klass) +{ + GObjectClass *gobject_class = G_OBJECT_CLASS (klass); + + gobject_class->get_property = polkit_unix_process_get_property; + gobject_class->set_property = polkit_unix_process_set_property; + + /** + * PolkitUnixProcess:pid: + * + * The UNIX process id. + */ + g_object_class_install_property (gobject_class, + PROP_PID, + g_param_spec_uint ("pid", + "Process ID", + "The UNIX process ID", + 0, + G_MAXUINT, + 0, + G_PARAM_CONSTRUCT | + G_PARAM_READWRITE | + G_PARAM_STATIC_NAME | + G_PARAM_STATIC_BLURB | + G_PARAM_STATIC_NICK)); + + /** + * PolkitUnixProcess:start-time: + * + * The start time of the process. + */ + g_object_class_install_property (gobject_class, + PROP_START_TIME, + g_param_spec_uint64 ("start-time", + "Start Time", + "The start time of the process, since the machine booted", + 0, + G_MAXUINT64, + 0, + G_PARAM_READABLE | + G_PARAM_STATIC_NAME | + G_PARAM_STATIC_BLURB | + G_PARAM_STATIC_NICK)); + +} + +pid_t +polkit_unix_process_get_pid (PolkitUnixProcess *process) +{ + return process->pid; +} + +guint64 +polkit_unix_process_get_start_time (PolkitUnixProcess *process) +{ + return process->start_time; +} + +void +polkit_unix_process_set_pid (PolkitUnixProcess *process, + pid_t pid) +{ + process->pid = pid; + process->start_time = get_start_time_for_pid (pid); +} + +PolkitSubject * +polkit_unix_process_new (pid_t pid) +{ + return POLKIT_SUBJECT (g_object_new (POLKIT_TYPE_UNIX_PROCESS, + "pid", pid, + NULL)); +} + +static gboolean +polkit_unix_process_equal (PolkitSubject *a, + PolkitSubject *b) +{ + PolkitUnixProcess *process_a; + PolkitUnixProcess *process_b; + + process_a = POLKIT_UNIX_PROCESS (a); + process_b = POLKIT_UNIX_PROCESS (b); + + return + (process_a->pid == process_b->pid) && + (process_a->start_time == process_b->start_time); +} + +static void +subject_iface_init (PolkitSubjectIface *subject_iface) +{ + subject_iface->equal = polkit_unix_process_equal; +} + +#ifdef HAVE_SOLARIS +static int +get_pid_psinfo (pid_t pid, struct psinfo *ps) +{ + char pname[32]; + int procfd; + + (void) snprintf(pname, sizeof(pname), "/proc/%d/psinfo", pid); + if ((procfd = open(pname, O_RDONLY)) == -1) + { + return -1; + } + if (read(procfd, ps, sizeof(struct psinfo)) < 0) + { + (void) close(procfd); + return -1; + } + (void) close(procfd); + return 0; +} +#endif + +static guint64 +get_start_time_for_pid (pid_t pid) +{ + gchar *filename; + gchar *contents; + size_t length; + guint64 start_time; + GError *error; +#ifdef HAVE_SOLARIS + struct psinfo info; +#else + gchar **tokens; + guint num_tokens; + gchar *p; + gchar *endp; +#endif + + start_time = 0; + contents = NULL; + +#ifdef HAVE_SOLARIS + if (polkit_sysdeps_pid_psinfo (pid, &info)) + { + goto out; + } + start_time = (unsigned long long) (info.pr_start.tv_sec); +#else +#ifdef __FreeBSD__ + filename = g_strdup_printf ("/proc/%d/status", pid); +#else + filename = g_strdup_printf ("/proc/%d/stat", pid); +#endif + + error = NULL; + if (!g_file_get_contents (filename, &contents, &length, &error)) + { + g_warning ("Cannot get contents of '%s': %s\n", filename, error->message); + goto out; + } + +#ifdef __FreeBSD__ + tokens = kit_strsplit (contents, " ", &num_tokens); + if (tokens == NULL) + goto out; + + if (num_tokens < 8) + { + g_strfreev (tokens); + goto out; + } + + p = g_strdup (tokens[7]); + g_strfreev (tokens); + + tokens = g_strsplit (p, ",", 0); + g_free (p); + + if (tokens == NULL) + goto out; + + num_tokens = g_strv_length (tokens); + + if (num_tokens >= 1) + { + start_time = strtoll (tokens[0], &endp, 10); + if (endp == tokens[0]) + { + g_strfreev (tokens); + goto out; + } + } + else + { + g_strfreev (tokens); + goto out; + } + + g_strfreev (tokens); + +#else + + /* start time is the 19th token after the '(process name)' entry */ + p = strchr (contents, ')'); + if (p == NULL) + { + goto out; + } + p += 2; /* skip ') ' */ + if (p - contents >= (int) length) + { + goto out; + } + + tokens = g_strsplit (p, " ", 0); + + if (tokens == NULL) + goto out; + + num_tokens = g_strv_length (tokens); + + if (num_tokens < 20) + goto out; + + start_time = strtoll (tokens[19], &endp, 10); + if (endp == tokens[19]) + goto out; + + g_strfreev (tokens); +#endif +#endif + + out: +#ifndef HAVE_SOLARIS + g_free (filename); + g_free (contents); +#endif + + if (start_time == 0) + { + g_warning ("Cannot lookup start-time for pid %d", pid); + } + + return start_time; +} diff --git a/src/polkit/polkitunixprocess.h b/src/polkit/polkitunixprocess.h new file mode 100644 index 0000000..74102b2 --- /dev/null +++ b/src/polkit/polkitunixprocess.h @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2008 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General + * Public License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307, USA. + * + * Author: David Zeuthen + */ + +#ifndef __POLKIT_UNIX_PROCESS_H +#define __POLKIT_UNIX_PROCESS_H + +#include +#include +#include +#include +#include + +G_BEGIN_DECLS + +#define POLKIT_TYPE_UNIX_PROCESS (polkit_unix_process_get_type()) +#define POLKIT_UNIX_PROCESS(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), POLKIT_TYPE_UNIX_PROCESS, PolkitUnixProcess)) +#define POLKIT_UNIX_PROCESS_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), POLKIT_TYPE_UNIX_PROCESS, PolkitUnixProcessClass)) +#define POLKIT_UNIX_PROCESS_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), POLKIT_TYPE_UNIX_PROCESS, PolkitUnixProcessClass)) +#define POLKIT_IS_UNIX_PROCESS(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), POLKIT_TYPE_UNIX_PROCESS)) +#define POLKIT_IS_UNIX_PROCESS_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), POLKIT_TYPE_UNIX_PROCESS)) + +#if 0 +typedef struct _PolkitUnixProcess PolkitUnixProcess; +#endif +typedef struct _PolkitUnixProcessClass PolkitUnixProcessClass; + +GType polkit_unix_process_get_type (void) G_GNUC_CONST; +PolkitSubject *polkit_unix_process_new (pid_t pid); + +pid_t polkit_unix_process_get_pid (PolkitUnixProcess *process); +guint64 polkit_unix_process_get_start_time (PolkitUnixProcess *process); + +void polkit_unix_process_set_pid (PolkitUnixProcess *process, + pid_t pid); + +G_END_DECLS + +#endif /* __POLKIT_UNIX_PROCESS_H */ diff --git a/src/polkit/polkitunixuser.c b/src/polkit/polkitunixuser.c new file mode 100644 index 0000000..07801f7 --- /dev/null +++ b/src/polkit/polkitunixuser.c @@ -0,0 +1,175 @@ +/* + * Copyright (C) 2008 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General + * Public License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307, USA. + * + * Author: David Zeuthen + */ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include +#include "polkitunixuser.h" +#include "polkitsubject.h" +#include "polkitprivate.h" + +/** + * SECTION:polkitunixuser + * @title: PolkitUnixUser + * @short_description: Unix users + * + * Encapsulates a UNIX user. + */ + +struct _PolkitUnixUser +{ + GObject parent_instance; + + uid_t uid; +}; + +struct _PolkitUnixUserClass +{ + GObjectClass parent_class; +}; + +enum +{ + PROP_0, + PROP_UID, +}; + +static void subject_iface_init (PolkitSubjectIface *subject_iface); + +G_DEFINE_TYPE_WITH_CODE (PolkitUnixUser, polkit_unix_user, G_TYPE_OBJECT, + G_IMPLEMENT_INTERFACE (POLKIT_TYPE_SUBJECT, subject_iface_init) + ); + +static void +polkit_unix_user_init (PolkitUnixUser *unix_user) +{ +} + +static void +polkit_unix_user_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + PolkitUnixUser *unix_user = POLKIT_UNIX_USER (object); + + switch (prop_id) + { + case PROP_UID: + g_value_set_uint (value, unix_user->uid); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +polkit_unix_user_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + PolkitUnixUser *unix_user = POLKIT_UNIX_USER (object); + + switch (prop_id) + { + case PROP_UID: + unix_user->uid = g_value_get_uint (value); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +polkit_unix_user_class_init (PolkitUnixUserClass *klass) +{ + GObjectClass *gobject_class = G_OBJECT_CLASS (klass); + + gobject_class->get_property = polkit_unix_user_get_property; + gobject_class->set_property = polkit_unix_user_set_property; + + /** + * PolkitUnixUser:uid: + * + * The UNIX user id. + */ + g_object_class_install_property (gobject_class, + PROP_UID, + g_param_spec_uint ("uid", + "User ID", + "The UNIX user ID", + 0, + G_MAXUINT, + 0, + G_PARAM_CONSTRUCT | + G_PARAM_READWRITE | + G_PARAM_STATIC_NAME | + G_PARAM_STATIC_BLURB | + G_PARAM_STATIC_NICK)); + +} + +uid_t +polkit_unix_user_get_uid (PolkitUnixUser *user) +{ + return user->uid; +} + +void +polkit_unix_user_set_uid (PolkitUnixUser *user, + uid_t uid) +{ + user->uid = uid; +} + +PolkitSubject * +polkit_unix_user_new (uid_t uid) +{ + return POLKIT_SUBJECT (g_object_new (POLKIT_TYPE_UNIX_USER, + "uid", uid, + NULL)); +} + +static gboolean +polkit_unix_user_equal (PolkitSubject *a, + PolkitSubject *b) +{ + PolkitUnixUser *user_a; + PolkitUnixUser *user_b; + + user_a = POLKIT_UNIX_USER (a); + user_b = POLKIT_UNIX_USER (b); + + return user_a->uid == user_b->uid; +} + +static void +subject_iface_init (PolkitSubjectIface *subject_iface) +{ + subject_iface->equal = polkit_unix_user_equal; +} diff --git a/src/polkit/polkitunixuser.h b/src/polkit/polkitunixuser.h new file mode 100644 index 0000000..c6a1233 --- /dev/null +++ b/src/polkit/polkitunixuser.h @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2008 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General + * Public License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place, Suite 330, + * Boston, MA 02111-1307, USA. + * + * Author: David Zeuthen + */ + +#ifndef __POLKIT_UNIX_USER_H +#define __POLKIT_UNIX_USER_H + +#include +#include +#include +#include +#include + +G_BEGIN_DECLS + +#define POLKIT_TYPE_UNIX_USER (polkit_unix_user_get_type()) +#define POLKIT_UNIX_USER(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), POLKIT_TYPE_UNIX_USER, PolkitUnixUser)) +#define POLKIT_UNIX_USER_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), POLKIT_TYPE_UNIX_USER, PolkitUnixUserClass)) +#define POLKIT_UNIX_USER_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), POLKIT_TYPE_UNIX_USER, PolkitUnixUserClass)) +#define POLKIT_IS_UNIX_USER(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), POLKIT_TYPE_UNIX_USER)) +#define POLKIT_IS_UNIX_USER_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), POLKIT_TYPE_UNIX_USER)) + +#if 0 +typedef struct _PolkitUnixUser PolkitUnixUser; +#endif +typedef struct _PolkitUnixUserClass PolkitUnixUserClass; + +GType polkit_unix_user_get_type (void) G_GNUC_CONST; +PolkitSubject *polkit_unix_user_new (uid_t uid); +uid_t polkit_unix_user_get_uid (PolkitUnixUser *user); +void polkit_unix_user_set_uid (PolkitUnixUser *user, + uid_t uid); + +G_END_DECLS + +#endif /* __POLKIT_UNIX_USER_H */ -- 2.7.4