summaryrefslogtreecommitdiffstats
path: root/src/input/pvsCheckPrivileges.h
blob: 62b463ce0a2911f4b4b33219c94d451d9c2f1bf5 (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
/*
 # 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"

struct CachedInputContext
{
	CachedInputContext(InputEventContext const* source)
	{
		if(source)
		{
			pid = source->getSenderPid();
			uid = source->getSenderUid();
			gid = source->getSenderGid();
		}
		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;

class PVSCheckPrivileges : public QObject
{
	Q_OBJECT
public:
	typedef enum {
		SESSION_LOCAL,
		SESSION_NONLOCAL,
		SESSION_LOOKUP_FAILURE,
		SESSION_UNKNOWN
	} SessionKind;
	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);
		}
	}

	typedef enum {
		USER_PRIVILEGED,
		USER_UNPRIVILEGED,
		USER_LOOKUP_FAILURE,
		USER_UNKNOWN
	} UserPrivilege;
	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);
		}
	}

	static PVSCheckPrivileges* instance();
	static void deleteInstance();

	bool require(SessionKind sessionKind, CachedInputContext const& sender);
	bool require(UserPrivilege userPrivilege, CachedInputContext const& sender);
	bool require(SessionKind sessionKind, UserPrivilege userPrivilege, CachedInputContext const& sender);
	QString getX11SessionName(CachedInputContext const& sender);
	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_ */