2013-07-25 22:53:43 -05:00
|
|
|
/* NetworkManager -- Network link manager
|
|
|
|
|
*
|
|
|
|
|
* 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.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
|
*
|
2014-08-14 13:34:57 +02:00
|
|
|
* Copyright (C) 2013 - 2014 Red Hat, Inc.
|
2013-07-25 22:53:43 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* SECTION:nm-auth-subject
|
|
|
|
|
* @short_description: Encapsulates authentication information about a requestor
|
|
|
|
|
*
|
|
|
|
|
* #NMAuthSubject encpasulates identifying information about an entity that
|
|
|
|
|
* makes requests, like process identifier and user UID.
|
|
|
|
|
*/
|
|
|
|
|
|
2016-02-19 14:57:48 +01:00
|
|
|
#include "nm-default.h"
|
2014-11-13 10:07:02 -05:00
|
|
|
|
2014-08-14 13:34:57 +02:00
|
|
|
#include "nm-auth-subject.h"
|
2013-07-25 22:53:43 -05:00
|
|
|
|
2014-08-14 13:34:57 +02:00
|
|
|
#include <stdlib.h>
|
2013-07-25 22:53:43 -05:00
|
|
|
|
2018-03-02 05:55:21 +01:00
|
|
|
#include "nm-dbus-manager.h"
|
2013-07-25 22:53:43 -05:00
|
|
|
|
2014-08-14 13:34:57 +02:00
|
|
|
enum {
|
|
|
|
|
PROP_0,
|
|
|
|
|
PROP_SUBJECT_TYPE,
|
|
|
|
|
PROP_UNIX_PROCESS_DBUS_SENDER,
|
|
|
|
|
PROP_UNIX_PROCESS_PID,
|
|
|
|
|
PROP_UNIX_PROCESS_UID,
|
|
|
|
|
|
|
|
|
|
PROP_LAST,
|
|
|
|
|
};
|
2013-07-25 22:53:43 -05:00
|
|
|
|
2014-08-14 13:34:57 +02:00
|
|
|
typedef struct {
|
|
|
|
|
NMAuthSubjectType subject_type;
|
|
|
|
|
struct {
|
|
|
|
|
gulong pid;
|
|
|
|
|
gulong uid;
|
|
|
|
|
guint64 start_time;
|
|
|
|
|
char *dbus_sender;
|
|
|
|
|
} unix_process;
|
2013-07-25 22:53:43 -05:00
|
|
|
} NMAuthSubjectPrivate;
|
|
|
|
|
|
2016-05-11 18:12:02 +02:00
|
|
|
struct _NMAuthSubject {
|
|
|
|
|
GObject parent;
|
|
|
|
|
NMAuthSubjectPrivate _priv;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct _NMAuthSubjectClass {
|
|
|
|
|
GObjectClass parent;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (NMAuthSubject, nm_auth_subject, G_TYPE_OBJECT)
|
|
|
|
|
|
2016-09-05 16:55:07 +02:00
|
|
|
#define NM_AUTH_SUBJECT_GET_PRIVATE(self) _NM_GET_PRIVATE(self, NMAuthSubject, NM_IS_AUTH_SUBJECT)
|
2016-05-11 18:12:02 +02:00
|
|
|
|
2016-10-02 18:22:50 +02:00
|
|
|
/*****************************************************************************/
|
2013-07-25 22:53:43 -05:00
|
|
|
|
2014-08-14 13:34:57 +02:00
|
|
|
#define CHECK_SUBJECT(self, error_value) \
|
|
|
|
|
NMAuthSubjectPrivate *priv; \
|
|
|
|
|
g_return_val_if_fail (NM_IS_AUTH_SUBJECT (self), error_value); \
|
|
|
|
|
priv = NM_AUTH_SUBJECT_GET_PRIVATE (self); \
|
2013-07-25 22:53:43 -05:00
|
|
|
|
2014-08-14 13:34:57 +02:00
|
|
|
#define CHECK_SUBJECT_TYPED(self, expected_subject_type, error_value) \
|
|
|
|
|
CHECK_SUBJECT (self, error_value); \
|
|
|
|
|
g_return_val_if_fail (priv->subject_type == (expected_subject_type), error_value);
|
|
|
|
|
|
|
|
|
|
const char *
|
|
|
|
|
nm_auth_subject_to_string (NMAuthSubject *self, char *buf, gsize buf_len)
|
|
|
|
|
{
|
|
|
|
|
CHECK_SUBJECT (self, NULL);
|
|
|
|
|
|
|
|
|
|
switch (priv->subject_type) {
|
|
|
|
|
case NM_AUTH_SUBJECT_TYPE_UNIX_PROCESS:
|
|
|
|
|
g_snprintf (buf, buf_len, "unix-process[pid=%lu, uid=%lu, start=%llu]",
|
2017-03-14 11:15:05 +01:00
|
|
|
(unsigned long) priv->unix_process.pid,
|
|
|
|
|
(unsigned long) priv->unix_process.uid,
|
|
|
|
|
(unsigned long long) priv->unix_process.start_time);
|
2014-08-14 13:34:57 +02:00
|
|
|
break;
|
|
|
|
|
case NM_AUTH_SUBJECT_TYPE_INTERNAL:
|
2018-04-13 11:31:45 +02:00
|
|
|
g_strlcpy (buf, "internal", buf_len);
|
2014-08-14 13:34:57 +02:00
|
|
|
break;
|
|
|
|
|
default:
|
2018-04-13 11:31:45 +02:00
|
|
|
g_strlcpy (buf, "invalid", buf_len);
|
2014-08-14 13:34:57 +02:00
|
|
|
break;
|
2013-07-25 22:53:43 -05:00
|
|
|
}
|
2014-08-14 13:34:57 +02:00
|
|
|
return buf;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* returns a floating variant */
|
|
|
|
|
GVariant *
|
|
|
|
|
nm_auth_subject_unix_process_to_polkit_gvariant (NMAuthSubject *self)
|
|
|
|
|
{
|
|
|
|
|
GVariantBuilder builder;
|
|
|
|
|
GVariant *dict;
|
|
|
|
|
GVariant *ret;
|
|
|
|
|
CHECK_SUBJECT_TYPED (self, NM_AUTH_SUBJECT_TYPE_UNIX_PROCESS, NULL);
|
|
|
|
|
|
|
|
|
|
g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{sv}"));
|
|
|
|
|
g_variant_builder_add (&builder, "{sv}", "pid",
|
|
|
|
|
g_variant_new_uint32 (priv->unix_process.pid));
|
|
|
|
|
g_variant_builder_add (&builder, "{sv}", "start-time",
|
|
|
|
|
g_variant_new_uint64 (priv->unix_process.start_time));
|
|
|
|
|
g_variant_builder_add (&builder, "{sv}", "uid",
|
|
|
|
|
g_variant_new_int32 (priv->unix_process.uid));
|
|
|
|
|
dict = g_variant_builder_end (&builder);
|
|
|
|
|
ret = g_variant_new ("(s@a{sv})", "unix-process", dict);
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NMAuthSubjectType
|
|
|
|
|
nm_auth_subject_get_subject_type (NMAuthSubject *subject)
|
|
|
|
|
{
|
|
|
|
|
CHECK_SUBJECT (subject, NM_AUTH_SUBJECT_TYPE_INVALID);
|
|
|
|
|
|
|
|
|
|
return priv->subject_type;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
|
nm_auth_subject_is_internal (NMAuthSubject *subject)
|
|
|
|
|
{
|
|
|
|
|
return nm_auth_subject_get_subject_type (subject) == NM_AUTH_SUBJECT_TYPE_INTERNAL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
|
nm_auth_subject_is_unix_process (NMAuthSubject *subject)
|
|
|
|
|
{
|
|
|
|
|
return nm_auth_subject_get_subject_type (subject) == NM_AUTH_SUBJECT_TYPE_UNIX_PROCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gulong
|
|
|
|
|
nm_auth_subject_get_unix_process_pid (NMAuthSubject *subject)
|
|
|
|
|
{
|
|
|
|
|
CHECK_SUBJECT_TYPED (subject, NM_AUTH_SUBJECT_TYPE_UNIX_PROCESS, G_MAXULONG);
|
|
|
|
|
|
|
|
|
|
return priv->unix_process.pid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gulong
|
|
|
|
|
nm_auth_subject_get_unix_process_uid (NMAuthSubject *subject)
|
|
|
|
|
{
|
|
|
|
|
CHECK_SUBJECT_TYPED (subject, NM_AUTH_SUBJECT_TYPE_UNIX_PROCESS, G_MAXULONG);
|
|
|
|
|
|
|
|
|
|
return priv->unix_process.uid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const char *
|
|
|
|
|
nm_auth_subject_get_unix_process_dbus_sender (NMAuthSubject *subject)
|
|
|
|
|
{
|
|
|
|
|
CHECK_SUBJECT_TYPED (subject, NM_AUTH_SUBJECT_TYPE_UNIX_PROCESS, NULL);
|
|
|
|
|
|
|
|
|
|
return priv->unix_process.dbus_sender;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-02 18:22:50 +02:00
|
|
|
/*****************************************************************************/
|
2014-08-14 13:34:57 +02:00
|
|
|
|
|
|
|
|
static NMAuthSubject *
|
2015-04-16 12:34:55 -04:00
|
|
|
_new_unix_process (GDBusMethodInvocation *context,
|
|
|
|
|
GDBusConnection *connection,
|
|
|
|
|
GDBusMessage *message)
|
2014-08-14 13:34:57 +02:00
|
|
|
{
|
|
|
|
|
NMAuthSubject *self;
|
2019-04-08 11:57:55 +02:00
|
|
|
const char *dbus_sender = NULL;
|
|
|
|
|
gulong uid = 0;
|
|
|
|
|
gulong pid = 0;
|
|
|
|
|
gboolean success;
|
2014-08-14 13:34:57 +02:00
|
|
|
|
|
|
|
|
g_return_val_if_fail (context || (connection && message), NULL);
|
2013-07-25 22:53:43 -05:00
|
|
|
|
|
|
|
|
if (context) {
|
core/dbus: rework D-Bus implementation to use lower layer GDBusConnection API
Previously, we used the generated GDBusInterfaceSkeleton types and glued
them via the NMExportedObject base class to our NM types. We also used
GDBusObjectManagerServer.
Don't do that anymore. The resulting code was more complicated despite (or
because?) using generated classes. It was hard to understand, complex, had
ordering-issues, and had a runtime and memory overhead.
This patch refactors this entirely and uses the lower layer API GDBusConnection
directly. It replaces the generated code, GDBusInterfaceSkeleton, and
GDBusObjectManagerServer. All this is now done by NMDbusObject and NMDBusManager
and static descriptor instances of type GDBusInterfaceInfo.
This adds a net plus of more then 1300 lines of hand written code. I claim
that this implementation is easier to understand. Note that previously we
also required extensive and complex glue code to bind our objects to the
generated skeleton objects. Instead, now glue our objects directly to
GDBusConnection. The result is more immediate and gets rid of layers of
code in between.
Now that the D-Bus glue us more under our control, we can address issus and
bottlenecks better, instead of adding code to bend the generated skeletons
to our needs.
Note that the current implementation now only supports one D-Bus connection.
That was effectively the case already, although there were places (and still are)
where the code pretends it could also support connections from a private socket.
We dropped private socket support mainly because it was unused, untested and
buggy, but also because GDBusObjectManagerServer could not export the same
objects on multiple connections. Now, it would be rather straight forward to
fix that and re-introduce ObjectManager on each private connection. But this
commit doesn't do that yet, and the new code intentionally supports only one
D-Bus connection.
Also, the D-Bus startup was simplified. There is no retry, either nm_dbus_manager_start()
succeeds, or it detects the initrd case. In the initrd case, bus manager never tries to
connect to D-Bus. Since the initrd scenario is not yet used/tested, this is good enough
for the moment. It could be easily extended later, for example with polling whether the
system bus appears (like was done previously). Also, restart of D-Bus daemon isn't
supported either -- just like before.
Note how NMDBusManager now implements the ObjectManager D-Bus interface
directly.
Also, this fixes race issues in the server, by no longer delaying
PropertiesChanged signals. NMExportedObject would collect changed
properties and send the signal out in idle_emit_properties_changed()
on idle. This messes up the ordering of change events w.r.t. other
signals and events on the bus. Note that not only NMExportedObject
messed up the ordering. Also the generated code would hook into
notify() and process change events in and idle handle, exhibiting the
same ordering issue too.
No longer do that. PropertiesChanged signals will be sent right away
by hooking into dispatch_properties_changed(). This means, changing
a property in quick succession will no longer be combined and is
guaranteed to emit signals for each individual state. Quite possibly
we emit now more PropertiesChanged signals then before.
However, we are now able to group a set of changes by using standard
g_object_freeze_notify()/g_object_thaw_notify(). We probably should
make more use of that.
Also, now that our signals are all handled in the right order, we
might find places where we still emit them in the wrong order. But that
is then due to the order in which our GObjects emit signals, not due
to an ill behavior of the D-Bus glue. Possibly we need to identify
such ordering issues and fix them.
Numbers (for contrib/rpm --without debug on x86_64):
- the patch changes the code size of NetworkManager by
- 2809360 bytes
+ 2537528 bytes (-9.7%)
- Runtime measurements are harder because there is a large variance
during testing. In other words, the numbers are not reproducible.
Currently, the implementation performs no caching of GVariants at all,
but it would be rather simple to add it, if that turns out to be
useful.
Anyway, without strong claim, it seems that the new form tends to
perform slightly better. That would be no surprise.
$ time (for i in {1..1000}; do nmcli >/dev/null || break; echo -n .; done)
- real 1m39.355s
+ real 1m37.432s
$ time (for i in {1..2000}; do busctl call org.freedesktop.NetworkManager /org/freedesktop org.freedesktop.DBus.ObjectManager GetManagedObjects > /dev/null || break; echo -n .; done)
- real 0m26.843s
+ real 0m25.281s
- Regarding RSS size, just looking at the processes in similar
conditions, doesn't give a large difference. On my system they
consume about 19MB RSS. It seems that the new version has a
slightly smaller RSS size.
- 19356 RSS
+ 18660 RSS
2018-02-26 13:51:52 +01:00
|
|
|
success = nm_dbus_manager_get_caller_info (nm_dbus_manager_get (),
|
|
|
|
|
context,
|
|
|
|
|
&dbus_sender,
|
|
|
|
|
&uid,
|
|
|
|
|
&pid);
|
2018-04-06 23:40:16 +02:00
|
|
|
} else {
|
|
|
|
|
nm_assert (message);
|
core/dbus: rework D-Bus implementation to use lower layer GDBusConnection API
Previously, we used the generated GDBusInterfaceSkeleton types and glued
them via the NMExportedObject base class to our NM types. We also used
GDBusObjectManagerServer.
Don't do that anymore. The resulting code was more complicated despite (or
because?) using generated classes. It was hard to understand, complex, had
ordering-issues, and had a runtime and memory overhead.
This patch refactors this entirely and uses the lower layer API GDBusConnection
directly. It replaces the generated code, GDBusInterfaceSkeleton, and
GDBusObjectManagerServer. All this is now done by NMDbusObject and NMDBusManager
and static descriptor instances of type GDBusInterfaceInfo.
This adds a net plus of more then 1300 lines of hand written code. I claim
that this implementation is easier to understand. Note that previously we
also required extensive and complex glue code to bind our objects to the
generated skeleton objects. Instead, now glue our objects directly to
GDBusConnection. The result is more immediate and gets rid of layers of
code in between.
Now that the D-Bus glue us more under our control, we can address issus and
bottlenecks better, instead of adding code to bend the generated skeletons
to our needs.
Note that the current implementation now only supports one D-Bus connection.
That was effectively the case already, although there were places (and still are)
where the code pretends it could also support connections from a private socket.
We dropped private socket support mainly because it was unused, untested and
buggy, but also because GDBusObjectManagerServer could not export the same
objects on multiple connections. Now, it would be rather straight forward to
fix that and re-introduce ObjectManager on each private connection. But this
commit doesn't do that yet, and the new code intentionally supports only one
D-Bus connection.
Also, the D-Bus startup was simplified. There is no retry, either nm_dbus_manager_start()
succeeds, or it detects the initrd case. In the initrd case, bus manager never tries to
connect to D-Bus. Since the initrd scenario is not yet used/tested, this is good enough
for the moment. It could be easily extended later, for example with polling whether the
system bus appears (like was done previously). Also, restart of D-Bus daemon isn't
supported either -- just like before.
Note how NMDBusManager now implements the ObjectManager D-Bus interface
directly.
Also, this fixes race issues in the server, by no longer delaying
PropertiesChanged signals. NMExportedObject would collect changed
properties and send the signal out in idle_emit_properties_changed()
on idle. This messes up the ordering of change events w.r.t. other
signals and events on the bus. Note that not only NMExportedObject
messed up the ordering. Also the generated code would hook into
notify() and process change events in and idle handle, exhibiting the
same ordering issue too.
No longer do that. PropertiesChanged signals will be sent right away
by hooking into dispatch_properties_changed(). This means, changing
a property in quick succession will no longer be combined and is
guaranteed to emit signals for each individual state. Quite possibly
we emit now more PropertiesChanged signals then before.
However, we are now able to group a set of changes by using standard
g_object_freeze_notify()/g_object_thaw_notify(). We probably should
make more use of that.
Also, now that our signals are all handled in the right order, we
might find places where we still emit them in the wrong order. But that
is then due to the order in which our GObjects emit signals, not due
to an ill behavior of the D-Bus glue. Possibly we need to identify
such ordering issues and fix them.
Numbers (for contrib/rpm --without debug on x86_64):
- the patch changes the code size of NetworkManager by
- 2809360 bytes
+ 2537528 bytes (-9.7%)
- Runtime measurements are harder because there is a large variance
during testing. In other words, the numbers are not reproducible.
Currently, the implementation performs no caching of GVariants at all,
but it would be rather simple to add it, if that turns out to be
useful.
Anyway, without strong claim, it seems that the new form tends to
perform slightly better. That would be no surprise.
$ time (for i in {1..1000}; do nmcli >/dev/null || break; echo -n .; done)
- real 1m39.355s
+ real 1m37.432s
$ time (for i in {1..2000}; do busctl call org.freedesktop.NetworkManager /org/freedesktop org.freedesktop.DBus.ObjectManager GetManagedObjects > /dev/null || break; echo -n .; done)
- real 0m26.843s
+ real 0m25.281s
- Regarding RSS size, just looking at the processes in similar
conditions, doesn't give a large difference. On my system they
consume about 19MB RSS. It seems that the new version has a
slightly smaller RSS size.
- 19356 RSS
+ 18660 RSS
2018-02-26 13:51:52 +01:00
|
|
|
success = nm_dbus_manager_get_caller_info_from_message (nm_dbus_manager_get (),
|
|
|
|
|
connection,
|
|
|
|
|
message,
|
|
|
|
|
&dbus_sender,
|
|
|
|
|
&uid,
|
|
|
|
|
&pid);
|
2018-04-06 23:40:16 +02:00
|
|
|
}
|
2013-07-25 22:53:43 -05:00
|
|
|
|
2014-08-14 13:34:57 +02:00
|
|
|
if (!success)
|
2013-07-25 22:53:43 -05:00
|
|
|
return NULL;
|
|
|
|
|
|
2014-08-14 13:34:57 +02:00
|
|
|
g_return_val_if_fail (dbus_sender && *dbus_sender, NULL);
|
all: don't use gchar/gshort/gint/glong but C types
We commonly don't use the glib typedefs for char/short/int/long,
but their C types directly.
$ git grep '\<g\(char\|short\|int\|long\|float\|double\)\>' | wc -l
587
$ git grep '\<\(char\|short\|int\|long\|float\|double\)\>' | wc -l
21114
One could argue that using the glib typedefs is preferable in
public API (of our glib based libnm library) or where it clearly
is related to glib, like during
g_object_set (obj, PROPERTY, (gint) value, NULL);
However, that argument does not seem strong, because in practice we don't
follow that argument today, and seldomly use the glib typedefs.
Also, the style guide for this would be hard to formalize, because
"using them where clearly related to a glib" is a very loose suggestion.
Also note that glib typedefs will always just be typedefs of the
underlying C types. There is no danger of glib changing the meaning
of these typedefs (because that would be a major API break of glib).
A simple style guide is instead: don't use these typedefs.
No manual actions, I only ran the bash script:
FILES=($(git ls-files '*.[hc]'))
sed -i \
-e 's/\<g\(char\|short\|int\|long\|float\|double\)\>\( [^ ]\)/\1\2/g' \
-e 's/\<g\(char\|short\|int\|long\|float\|double\)\> /\1 /g' \
-e 's/\<g\(char\|short\|int\|long\|float\|double\)\>/\1/g' \
"${FILES[@]}"
2018-07-11 07:40:19 +02:00
|
|
|
/* polkit glib library stores uid and pid as int. There might be some
|
2014-08-14 13:34:57 +02:00
|
|
|
* pitfalls if the id ever happens to be larger then that. Just assert against
|
|
|
|
|
* it here. */
|
|
|
|
|
g_return_val_if_fail (uid <= MIN (G_MAXINT, G_MAXINT32), NULL);
|
|
|
|
|
g_return_val_if_fail (pid > 0 && pid <= MIN (G_MAXINT, G_MAXINT32), NULL);
|
|
|
|
|
|
|
|
|
|
self = NM_AUTH_SUBJECT (g_object_new (NM_TYPE_AUTH_SUBJECT,
|
2016-10-22 13:02:15 +02:00
|
|
|
NM_AUTH_SUBJECT_SUBJECT_TYPE, (int) NM_AUTH_SUBJECT_TYPE_UNIX_PROCESS,
|
2014-08-14 13:34:57 +02:00
|
|
|
NM_AUTH_SUBJECT_UNIX_PROCESS_DBUS_SENDER, dbus_sender,
|
|
|
|
|
NM_AUTH_SUBJECT_UNIX_PROCESS_PID, (gulong) pid,
|
|
|
|
|
NM_AUTH_SUBJECT_UNIX_PROCESS_UID, (gulong) uid,
|
|
|
|
|
NULL));
|
|
|
|
|
|
|
|
|
|
if (NM_AUTH_SUBJECT_GET_PRIVATE (self)->subject_type != NM_AUTH_SUBJECT_TYPE_UNIX_PROCESS) {
|
|
|
|
|
/* this most likely happened because the process is gone (start_time==0).
|
|
|
|
|
* Either that is not assert-worthy, or constructed() already asserted.
|
|
|
|
|
* Just return NULL. */
|
|
|
|
|
g_clear_object (&self);
|
|
|
|
|
}
|
|
|
|
|
return self;
|
2013-07-25 22:53:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NMAuthSubject *
|
2015-04-16 12:34:55 -04:00
|
|
|
nm_auth_subject_new_unix_process_from_context (GDBusMethodInvocation *context)
|
2013-07-25 22:53:43 -05:00
|
|
|
{
|
2014-08-14 13:34:57 +02:00
|
|
|
return _new_unix_process (context, NULL, NULL);
|
2013-07-25 22:53:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NMAuthSubject *
|
2015-04-16 12:34:55 -04:00
|
|
|
nm_auth_subject_new_unix_process_from_message (GDBusConnection *connection,
|
|
|
|
|
GDBusMessage *message)
|
2013-07-25 22:53:43 -05:00
|
|
|
{
|
2014-08-14 13:34:57 +02:00
|
|
|
return _new_unix_process (NULL, connection, message);
|
2013-07-25 22:53:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* nm_auth_subject_new_internal():
|
|
|
|
|
*
|
|
|
|
|
* Creates a new auth subject representing the NetworkManager process itself.
|
|
|
|
|
*
|
|
|
|
|
* Returns: the new #NMAuthSubject
|
|
|
|
|
*/
|
|
|
|
|
NMAuthSubject *
|
|
|
|
|
nm_auth_subject_new_internal (void)
|
|
|
|
|
{
|
2014-08-14 13:34:57 +02:00
|
|
|
return NM_AUTH_SUBJECT (g_object_new (NM_TYPE_AUTH_SUBJECT,
|
2016-10-22 13:02:15 +02:00
|
|
|
NM_AUTH_SUBJECT_SUBJECT_TYPE, (int) NM_AUTH_SUBJECT_TYPE_INTERNAL,
|
2014-08-14 13:34:57 +02:00
|
|
|
NULL));
|
2013-07-25 22:53:43 -05:00
|
|
|
}
|
|
|
|
|
|
2016-10-02 18:22:50 +02:00
|
|
|
/*****************************************************************************/
|
2013-07-25 22:53:43 -05:00
|
|
|
|
2014-08-14 13:34:57 +02:00
|
|
|
static void
|
|
|
|
|
get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
|
2013-07-25 22:53:43 -05:00
|
|
|
{
|
2016-05-11 18:12:02 +02:00
|
|
|
NMAuthSubjectPrivate *priv = NM_AUTH_SUBJECT_GET_PRIVATE ((NMAuthSubject *) object);
|
2013-07-25 22:53:43 -05:00
|
|
|
|
2014-08-14 13:34:57 +02:00
|
|
|
switch (prop_id) {
|
|
|
|
|
case PROP_SUBJECT_TYPE:
|
2016-10-22 13:02:15 +02:00
|
|
|
g_value_set_int (value, priv->subject_type);
|
2014-08-14 13:34:57 +02:00
|
|
|
break;
|
|
|
|
|
case PROP_UNIX_PROCESS_DBUS_SENDER:
|
|
|
|
|
g_value_set_string (value, priv->unix_process.dbus_sender);
|
|
|
|
|
break;
|
|
|
|
|
case PROP_UNIX_PROCESS_PID:
|
|
|
|
|
g_value_set_ulong (value, priv->unix_process.pid);
|
|
|
|
|
break;
|
|
|
|
|
case PROP_UNIX_PROCESS_UID:
|
|
|
|
|
g_value_set_ulong (value, priv->unix_process.uid);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2013-11-05 14:48:23 -05:00
|
|
|
}
|
|
|
|
|
|
2014-08-14 13:34:57 +02:00
|
|
|
static void
|
|
|
|
|
set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
|
2013-07-25 22:53:43 -05:00
|
|
|
{
|
2016-05-11 18:12:02 +02:00
|
|
|
NMAuthSubjectPrivate *priv = NM_AUTH_SUBJECT_GET_PRIVATE ((NMAuthSubject *) object);
|
2014-08-14 13:34:57 +02:00
|
|
|
NMAuthSubjectType subject_type;
|
2016-10-22 13:02:15 +02:00
|
|
|
int i;
|
2014-08-14 13:34:57 +02:00
|
|
|
const char *str;
|
|
|
|
|
gulong id;
|
|
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
|
case PROP_SUBJECT_TYPE:
|
2017-03-08 13:43:56 +01:00
|
|
|
/* construct-only */
|
2016-10-22 13:02:15 +02:00
|
|
|
i = g_value_get_int (value);
|
|
|
|
|
g_return_if_fail (NM_IN_SET (i, (int) NM_AUTH_SUBJECT_TYPE_INTERNAL, (int) NM_AUTH_SUBJECT_TYPE_UNIX_PROCESS));
|
|
|
|
|
subject_type = i;
|
2014-08-14 13:34:57 +02:00
|
|
|
priv->subject_type |= subject_type;
|
|
|
|
|
g_return_if_fail (priv->subject_type == subject_type);
|
|
|
|
|
break;
|
|
|
|
|
case PROP_UNIX_PROCESS_DBUS_SENDER:
|
2017-03-08 13:43:56 +01:00
|
|
|
/* construct-only */
|
2014-08-14 13:34:57 +02:00
|
|
|
if ((str = g_value_get_string (value))) {
|
|
|
|
|
priv->subject_type |= NM_AUTH_SUBJECT_TYPE_UNIX_PROCESS;
|
|
|
|
|
g_return_if_fail (priv->subject_type == NM_AUTH_SUBJECT_TYPE_UNIX_PROCESS);
|
|
|
|
|
priv->unix_process.dbus_sender = g_strdup (str);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case PROP_UNIX_PROCESS_PID:
|
2017-03-08 13:43:56 +01:00
|
|
|
/* construct-only */
|
2014-08-14 13:34:57 +02:00
|
|
|
if ((id = g_value_get_ulong (value)) != G_MAXULONG) {
|
|
|
|
|
priv->subject_type |= NM_AUTH_SUBJECT_TYPE_UNIX_PROCESS;
|
|
|
|
|
g_return_if_fail (priv->subject_type == NM_AUTH_SUBJECT_TYPE_UNIX_PROCESS);
|
|
|
|
|
priv->unix_process.pid = id;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case PROP_UNIX_PROCESS_UID:
|
2017-03-08 13:43:56 +01:00
|
|
|
/* construct-only */
|
2014-08-14 13:34:57 +02:00
|
|
|
if ((id = g_value_get_ulong (value)) != G_MAXULONG) {
|
|
|
|
|
priv->subject_type |= NM_AUTH_SUBJECT_TYPE_UNIX_PROCESS;
|
|
|
|
|
g_return_if_fail (priv->subject_type == NM_AUTH_SUBJECT_TYPE_UNIX_PROCESS);
|
|
|
|
|
priv->unix_process.uid = id;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2013-07-25 22:53:43 -05:00
|
|
|
}
|
|
|
|
|
|
2014-08-14 13:34:57 +02:00
|
|
|
static void
|
2016-05-11 18:12:02 +02:00
|
|
|
_clear_private (NMAuthSubject *self)
|
2013-07-25 22:53:43 -05:00
|
|
|
{
|
2016-05-11 18:12:02 +02:00
|
|
|
NMAuthSubjectPrivate *priv = NM_AUTH_SUBJECT_GET_PRIVATE (self);
|
|
|
|
|
|
2014-08-14 13:34:57 +02:00
|
|
|
priv->subject_type = NM_AUTH_SUBJECT_TYPE_INVALID;
|
|
|
|
|
priv->unix_process.pid = G_MAXULONG;
|
|
|
|
|
priv->unix_process.uid = G_MAXULONG;
|
|
|
|
|
g_clear_pointer (&priv->unix_process.dbus_sender, g_free);
|
2013-07-25 22:53:43 -05:00
|
|
|
}
|
|
|
|
|
|
2014-08-14 13:34:57 +02:00
|
|
|
static void
|
|
|
|
|
nm_auth_subject_init (NMAuthSubject *self)
|
2013-07-25 22:53:43 -05:00
|
|
|
{
|
2016-05-11 18:12:02 +02:00
|
|
|
_clear_private (self);
|
2013-07-25 22:53:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2014-08-14 13:34:57 +02:00
|
|
|
constructed (GObject *object)
|
2013-07-25 22:53:43 -05:00
|
|
|
{
|
2014-08-14 13:34:57 +02:00
|
|
|
NMAuthSubject *self = NM_AUTH_SUBJECT (object);
|
2013-07-25 22:53:43 -05:00
|
|
|
NMAuthSubjectPrivate *priv = NM_AUTH_SUBJECT_GET_PRIVATE (self);
|
|
|
|
|
|
2014-08-14 13:34:57 +02:00
|
|
|
/* validate that the created instance. */
|
|
|
|
|
|
|
|
|
|
switch (priv->subject_type) {
|
|
|
|
|
case NM_AUTH_SUBJECT_TYPE_INTERNAL:
|
|
|
|
|
priv->unix_process.pid = G_MAXULONG;
|
|
|
|
|
priv->unix_process.uid = 0; /* internal uses 'root' user */
|
|
|
|
|
return;
|
|
|
|
|
case NM_AUTH_SUBJECT_TYPE_UNIX_PROCESS:
|
|
|
|
|
/* Ensure pid and uid to be representable as int32.
|
all: don't use gchar/gshort/gint/glong but C types
We commonly don't use the glib typedefs for char/short/int/long,
but their C types directly.
$ git grep '\<g\(char\|short\|int\|long\|float\|double\)\>' | wc -l
587
$ git grep '\<\(char\|short\|int\|long\|float\|double\)\>' | wc -l
21114
One could argue that using the glib typedefs is preferable in
public API (of our glib based libnm library) or where it clearly
is related to glib, like during
g_object_set (obj, PROPERTY, (gint) value, NULL);
However, that argument does not seem strong, because in practice we don't
follow that argument today, and seldomly use the glib typedefs.
Also, the style guide for this would be hard to formalize, because
"using them where clearly related to a glib" is a very loose suggestion.
Also note that glib typedefs will always just be typedefs of the
underlying C types. There is no danger of glib changing the meaning
of these typedefs (because that would be a major API break of glib).
A simple style guide is instead: don't use these typedefs.
No manual actions, I only ran the bash script:
FILES=($(git ls-files '*.[hc]'))
sed -i \
-e 's/\<g\(char\|short\|int\|long\|float\|double\)\>\( [^ ]\)/\1\2/g' \
-e 's/\<g\(char\|short\|int\|long\|float\|double\)\> /\1 /g' \
-e 's/\<g\(char\|short\|int\|long\|float\|double\)\>/\1/g' \
"${FILES[@]}"
2018-07-11 07:40:19 +02:00
|
|
|
* DBUS treats them as uint32, polkit library as int. */
|
2014-08-14 13:34:57 +02:00
|
|
|
if (priv->unix_process.pid > MIN (G_MAXINT, G_MAXINT32))
|
|
|
|
|
break;
|
|
|
|
|
if (priv->unix_process.uid > MIN (G_MAXINT, G_MAXINT32)) {
|
|
|
|
|
/* for uid==-1, libpolkit-gobject-1 detects the user based on the process id.
|
|
|
|
|
* Don't bother and require the user id as parameter. */
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (!priv->unix_process.dbus_sender || !*priv->unix_process.dbus_sender)
|
|
|
|
|
break;
|
|
|
|
|
|
2015-06-30 14:51:42 +02:00
|
|
|
priv->unix_process.start_time = nm_utils_get_start_time_for_pid (priv->unix_process.pid, NULL, NULL);
|
2014-08-14 13:34:57 +02:00
|
|
|
|
|
|
|
|
if (!priv->unix_process.start_time) {
|
2016-04-22 11:36:26 +02:00
|
|
|
/* Is the process already gone? Then fail creation of the auth subject
|
|
|
|
|
* by clearing the type. */
|
|
|
|
|
if (kill (priv->unix_process.pid, 0) != 0)
|
2016-05-11 18:12:02 +02:00
|
|
|
_clear_private (self);
|
2016-04-22 11:36:26 +02:00
|
|
|
|
|
|
|
|
/* Otherwise, although we didn't detect a start_time, the process is still around.
|
|
|
|
|
* That could be due to procfs mounted with hidepid. So just accept the request.
|
|
|
|
|
*
|
|
|
|
|
* Polkit on the other side, will accept 0 and try to lookup /proc/$PID/stat
|
|
|
|
|
* itself (and if it fails to do so, assume a start-time of 0 and proceed).
|
|
|
|
|
* The only combination that would fail here, is when NM is able to read the
|
|
|
|
|
* start-time, but polkit is not. */
|
2014-08-14 13:34:57 +02:00
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-11 18:12:02 +02:00
|
|
|
_clear_private (self);
|
2014-08-14 13:34:57 +02:00
|
|
|
g_return_if_reached ();
|
2013-07-25 22:53:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
finalize (GObject *object)
|
|
|
|
|
{
|
2016-05-11 18:12:02 +02:00
|
|
|
_clear_private ((NMAuthSubject *) object);
|
2013-07-25 22:53:43 -05:00
|
|
|
|
|
|
|
|
G_OBJECT_CLASS (nm_auth_subject_parent_class)->finalize (object);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
nm_auth_subject_class_init (NMAuthSubjectClass *config_class)
|
|
|
|
|
{
|
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (config_class);
|
|
|
|
|
|
2014-08-14 13:34:57 +02:00
|
|
|
object_class->get_property = get_property;
|
|
|
|
|
object_class->set_property = set_property;
|
|
|
|
|
object_class->constructed = constructed;
|
2013-07-25 22:53:43 -05:00
|
|
|
object_class->finalize = finalize;
|
2014-08-14 13:34:57 +02:00
|
|
|
|
|
|
|
|
g_object_class_install_property
|
|
|
|
|
(object_class, PROP_SUBJECT_TYPE,
|
2016-10-22 13:02:15 +02:00
|
|
|
g_param_spec_int (NM_AUTH_SUBJECT_SUBJECT_TYPE, "", "",
|
|
|
|
|
NM_AUTH_SUBJECT_TYPE_INVALID,
|
|
|
|
|
NM_AUTH_SUBJECT_TYPE_UNIX_PROCESS,
|
|
|
|
|
NM_AUTH_SUBJECT_TYPE_INVALID,
|
|
|
|
|
G_PARAM_READWRITE |
|
|
|
|
|
G_PARAM_CONSTRUCT_ONLY |
|
|
|
|
|
G_PARAM_STATIC_STRINGS));
|
2014-08-14 13:34:57 +02:00
|
|
|
|
|
|
|
|
g_object_class_install_property
|
|
|
|
|
(object_class, PROP_UNIX_PROCESS_DBUS_SENDER,
|
|
|
|
|
g_param_spec_string (NM_AUTH_SUBJECT_UNIX_PROCESS_DBUS_SENDER, "", "",
|
|
|
|
|
NULL,
|
|
|
|
|
G_PARAM_READWRITE |
|
|
|
|
|
G_PARAM_CONSTRUCT_ONLY |
|
|
|
|
|
G_PARAM_STATIC_STRINGS));
|
|
|
|
|
|
|
|
|
|
g_object_class_install_property
|
|
|
|
|
(object_class, PROP_UNIX_PROCESS_PID,
|
|
|
|
|
g_param_spec_ulong (NM_AUTH_SUBJECT_UNIX_PROCESS_PID, "", "",
|
|
|
|
|
0, G_MAXULONG, G_MAXULONG,
|
|
|
|
|
G_PARAM_READWRITE |
|
|
|
|
|
G_PARAM_CONSTRUCT_ONLY |
|
|
|
|
|
G_PARAM_STATIC_STRINGS));
|
|
|
|
|
|
|
|
|
|
g_object_class_install_property
|
|
|
|
|
(object_class, PROP_UNIX_PROCESS_UID,
|
|
|
|
|
g_param_spec_ulong (NM_AUTH_SUBJECT_UNIX_PROCESS_UID, "", "",
|
|
|
|
|
0, G_MAXULONG, G_MAXULONG,
|
|
|
|
|
G_PARAM_READWRITE |
|
|
|
|
|
G_PARAM_CONSTRUCT_ONLY |
|
|
|
|
|
G_PARAM_STATIC_STRINGS));
|
|
|
|
|
|
2013-07-25 22:53:43 -05:00
|
|
|
}
|