summaryrefslogtreecommitdiff
path: root/netfs/lib/entCache.cpp
blob: 6699a4b83f67ed45dfb69b2a9abf53c3b067ed4e (plain)
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
#include "entCache.h"
#include <exceptions.h>
#include <lockHelpers.h>
#include <pwd.h>
#include <grp.h>
#include <visibility.h>

template<class entry_t>
EntCache<entry_t>::EntCache() :
	fillTime(0)
{
}

template<class entry_t>
EntCache<entry_t>::~EntCache()
{
}

template<class entry_t>
void
EntCache<entry_t>::getID(const EntCache<entry_t>::name_t & u, EntCache<entry_t>::id_t * target) const
{
	auto e = getEntry(u);
	*target = e->id;
}

template<class entry_t>
void
EntCache<entry_t>::getName(const EntCache<entry_t>::id_t & u, EntCache<entry_t>::name_t * target) const
{
	auto e = getEntry(u);
	*target = e->name;
}

template<class entry_t>
template<class key_t>
typename EntCache<entry_t>::entry_ptr
EntCache<entry_t>::getEntry(const key_t & key) const
{
	typename EntCache<entry_t>::entry_ptr ent;
	if (fillTime + 60 > time(NULL)) {
		ent = getEntryNoFill<key_t>(key);
	}
	if (!ent) {
		fillCache();
		ent = getEntryNoFill<key_t>(key);
	}
	if (!ent) {
		throw NetFS::SystemError(EPERM);
	}
	return ent;
}

template<class entry_t>
template<class key_t>
typename EntCache<entry_t>::entry_ptr
EntCache<entry_t>::getEntryNoFill(const key_t & key) const
{
	SharedLock(lock);
	auto & collection = idcache.template get<key_t>();
	auto i = collection.find(key);
	if (i != collection.end()) {
		return *i;
	}
	return NULL;
}

User::User(uid_t u, const std::string & n, gid_t g) :
	id(u),
	name(n),
	group(g)
{
}

const int BUFLEN = 8196;

template<>
void
EntCache<User>::fillCache() const
{
	Lock(lock);
	setpwent();
	idcache.clear();
	char buf[BUFLEN];
	struct passwd pwbuf, * pwp;
	while (getpwent_r(&pwbuf, buf, BUFLEN, &pwp) == 0) {
		idcache.insert(new User(pwp->pw_uid, pwp->pw_name, pwp->pw_gid));
	}
	endpwent();
	time(&fillTime);
}

Group::Group(gid_t g, const std::string & n) :
	id(g),
	name(n)
{
}

template<>
void
EntCache<Group>::fillCache() const
{
	Lock(lock);
	setgrent();
	char buf[BUFLEN];
	idcache.clear();
	struct group grpbuf, * grp;
	EntCache<User> instance;
	while (getgrent_r(&grpbuf, buf, BUFLEN, &grp) == 0) {
		auto g = new Group(grp->gr_gid, grp->gr_name);
		for (auto member = grp->gr_mem; *member; member++) {
			try {
				g->members.insert(instance.getEntry((const name_t &)*member)->id);
			}
			catch (const NetFS::SystemError &) {
			}
		}
		idcache.insert(g);
	}
	endgrent();
	time(&fillTime);
}

bool
DLL_PUBLIC Group::hasMember(uid_t u) const
{
	return (members.find(u) != members.end());
}

template DLL_PUBLIC EntCache<User>::EntCache();
template DLL_PUBLIC EntCache<User>::~EntCache();
template IceUtil::Handle<User> DLL_PUBLIC EntCache<User>::getEntry<std::string>(const std::string &) const;
template IceUtil::Handle<User> DLL_PUBLIC EntCache<User>::getEntry<uid_t>(const uid_t &) const;
template DLL_PUBLIC void EntCache<User>::getName(const gid_t &, std::string *) const;
template DLL_PUBLIC void EntCache<User>::getID(const std::string &, gid_t *) const;

template DLL_PUBLIC EntCache<Group>::EntCache();
template DLL_PUBLIC EntCache<Group>::~EntCache();
template IceUtil::Handle<Group> DLL_PUBLIC EntCache<Group>::getEntry<std::string>(const std::string &) const;
template IceUtil::Handle<Group> DLL_PUBLIC EntCache<Group>::getEntry<gid_t>(const gid_t &) const;
template DLL_PUBLIC void EntCache<Group>::getName(const gid_t &, std::string *) const;
template DLL_PUBLIC void EntCache<Group>::getID(const std::string &, gid_t *) const;