summaryrefslogtreecommitdiffstats
path: root/src/input/pvsCheckPrivileges.h
blob: 37c4c04fdfc3ac210af37a0e31d876883d842019 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 # Copyright (c) 2009,2010 - OpenSLX Project, Computer Center University of
 # Freiburg
 #
 # This program is free software distributed under the GPL version 2.
 # See http://openslx.org/COPYING
 #
 # If you have any feedback please consult http://openslx.org/feedback and
 # send your suggestions, praise, or complaints to feedback@openslx.org
 #
 # General information about OpenSLX can be found at http://openslx.org/
 # -----------------------------------------------------------------------------
 # src/net/pvsCheckPrivileges_linux.h
 #    - Linux implementation of privilege checking
 # -----------------------------------------------------------------------------
 */

#ifndef PVSCHECKPRIVILEGES_H_
#define PVSCHECKPRIVILEGES_H_

#include <sys/types.h>
#include <QObject>
#include <QString>
#include <QList>
#include <QMultiMap>
#include <QHash>
#include "inputEventHandler.h"

/**
 * Store the information in an InputEventContext as a plain old datatype.
 */
struct CachedInputContext
{
	CachedInputContext(InputEventContext const* source)
	{
		if(source)
		{
			pid = source->senderPid();
			uid = source->senderUid();
			gid = source->senderGid();
		}
		else
		{
			pid = (pid_t)-1;
			uid = (uid_t)-1;
			gid = (gid_t)-1;
		}
	}

	CachedInputContext()
	{
		pid = (pid_t)-1;
		uid = (uid_t)-1;
		gid = (gid_t)-1;
	}

	pid_t pid;
	uid_t uid;
	gid_t gid;

	bool isValid() const
	{
		return (pid != (pid_t)-1) && (uid != (uid_t)-1) && (gid != (gid_t)-1);
	}

	bool operator==(CachedInputContext const& other) const
	{
		return (other.pid == pid) && (other.uid == uid) && (other.gid == gid);
	}
};
uint qHash(CachedInputContext const& p);

class QFileSystemWatcher;

/**
 * Check user privileges and handle communications with ConsoleKit and PolicyKit.
 * This is a singleton class.
 */
class PVSCheckPrivileges : public QObject
{
	Q_OBJECT
public:
	/**
	 * SessionKind distinguishes between local and remote users.
	 */
	typedef enum {
		SESSION_LOCAL,				/**< User is local */
		SESSION_NONLOCAL,			/**< User is remote */
		SESSION_LOOKUP_FAILURE,		/**< Failure to look up whether the user is local or remote */
		SESSION_UNKNOWN				/**< User session kind not (yet) known */
	} SessionKind;

	/**
	 * UserPrivilege distinguishes between privileged and unprivileged users.
	 */
	typedef enum {
		USER_PRIVILEGED,			/**< User is privileged */
		USER_UNPRIVILEGED,			/**< User is unprivileged */
		USER_LOOKUP_FAILURE,		/**< Failure to look up whether the user is privileged or unprivileged */
		USER_UNKNOWN				/**< User privilege level not (yet) known */
	} UserPrivilege;

	/** \name Conversion Functions */
	/* @{ */

	static QString toString(SessionKind k)
	{
		switch(k)
		{
		case SESSION_LOOKUP_FAILURE: return "SESSION_LOOKUP_FAILURE";
		case SESSION_LOCAL: return "SESSION_LOCAL";
		case SESSION_NONLOCAL: return "SESSION_NONLOCAL";
		case SESSION_UNKNOWN: return "SESSION_UNKNOWN";
		default: return QString("unknown value (%1)").arg(k);
		}
	}

	static QString toString(UserPrivilege k)
	{
		switch(k)
		{
		case USER_PRIVILEGED: return "USER_PRIVILEGED";
		case USER_UNPRIVILEGED: return "USER_UNPRIVILEGED";
		case USER_LOOKUP_FAILURE: return "USER_LOOKUP_FAILURE";
		case USER_UNKNOWN: return "USER_UNKNOWN";
		default: return QString("unknown value (%1)").arg(k);
		}
	}

	/* @} */

	/** \name Singleton pattern */
	/* @{ */

	/** Retrieve the singleton instance. */
	static PVSCheckPrivileges* instance();

	/** Delete the singleton instance. */
	static void deleteInstance();

	/* @} */

	/** \name Privilege Checks */
	/* @{ */

	/** Check for a minimum SessionKind level. \return true if the requirement is fulfilled. */
	bool require(SessionKind sessionKind, CachedInputContext const& sender);

	/** Check for a minimum UserPrivilege level. \return true if the requirement is fulfilled. */
	bool require(UserPrivilege userPrivilege, CachedInputContext const& sender);

	/** Check for a minimum SessionKind and UserPrivilege level. \return true if both requirements are fulfilled. */
	bool require(SessionKind sessionKind, UserPrivilege userPrivilege, CachedInputContext const& sender);

	/* @} */

	/** \name Session Information */
	/* @{ */

	/** Retrieve the name of the user's X session, according to ConsoleKit. */
	QString getX11SessionName(CachedInputContext const& sender);

	/** Retrieve the TTY device of the user's X session, according to ConsoleKit. */
	QString getX11DisplayDevice(CachedInputContext const& sender);

public slots:
	void updateCachedUserDatabase();
	void rereadConfiguration();

private:
	PVSCheckPrivileges(QObject* parent = 0);
	virtual ~PVSCheckPrivileges();

	typedef QPair<pid_t, QPair<uid_t, gid_t> > piduidgid;
	piduidgid makePidUidGid(pid_t pid, uid_t uid, gid_t gid)
	{
		return qMakePair(pid, qMakePair(uid, gid));
	}

	QString getSessionReference(CachedInputContext const& sender);
	SessionKind getSessionKind(CachedInputContext const& sender);
	UserPrivilege getUserPrivilege(CachedInputContext const& sender);

	static PVSCheckPrivileges* _instance;

	QHash<CachedInputContext, UserPrivilege> _savedUserPrivilege;
	QHash<CachedInputContext, SessionKind> _savedSessionKind;
	QHash<CachedInputContext, QString> _savedConsoleKitSession;

	QList<uid_t> _privilegedUsers;
	QList<gid_t> _privilegedGroups;
	QMultiMap<uid_t, gid_t> _userGroupMap;
	QFileSystemWatcher* _watcher;
};

#endif /* PVSCHECKPRIVILEGES_H_ */