summaryrefslogtreecommitdiff
path: root/netfs/lib/entCache.impl.h
blob: 37aecfdc760f0f8f712032857c365ff11e6ea2a2 (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
144
145
146
147
148
149
150
151
152
153
154
155
#ifndef ENTCACHE_IMPL_H
#define ENTCACHE_IMPL_H

#include "entCache.h"
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index_container.hpp>
#include <exceptions.h>
#include <grp.h>
#include <lockHelpers.h>
#include <pwd.h>
#include <type_traits>

template<class entry_t>
class EntCache<entry_t>::Ids :
	public boost::multi_index::multi_index_container<std::shared_ptr<entry_t>,
			boost::multi_index::indexed_by<boost::multi_index::ordered_unique<boost::multi_index::tag<id_t>,
												   BOOST_MULTI_INDEX_MEMBER(entry_t, const id_t, id), std::less<>>,
					boost::multi_index::ordered_unique<boost::multi_index::tag<std::string>,
							BOOST_MULTI_INDEX_MEMBER(entry_t, const std::string, name), std::less<>>>> {
};

template<class entry_t> EntCache<entry_t>::EntCache() : idcache(std::make_unique<Ids>()) { }

template<class entry_t> EntCache<entry_t>::EntCache(entry_t fb) : idcache(std::make_unique<Ids>())
{
	setFallback(std::move(fb));
}

template<class entry_t> EntCache<entry_t>::~EntCache() = default;

template<class entry_t>
ResolvedAs
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;
	return e.resolution;
}

template<class entry_t>
ResolvedAs
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;
	return e.resolution;
}

template<class entry_t>
EntCache<entry_t>::Resolution::Resolution(entry_ptr e, ResolvedAs res) : entry_ptr(std::move(e)), resolution(res)
{
}

template<class entry_t>
template<class key_t>
typename EntCache<entry_t>::Resolution
EntCache<entry_t>::getEntry(const key_t & key) const
{
	SharedScopeLock(lock) {
		if (fillTime + 60 > time(nullptr)) {
			if (auto ent = getEntryNoFill<key_t>(key)) {
				return ent;
			}
		}
	}
	ScopeLock(lock) {
		fillCache();
		if (auto ent = getEntryNoFill<key_t>(key)) {
			return ent;
		}
	}
	if (fallback) {
		return {fallback, ResolvedAs::Fallback};
	}
	throw NetFS::SystemError(EPERM);
}

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

template<typename entry_t>
void
EntCache<entry_t>::setFallback(entry_t fb)
{
	fallback = std::make_shared<entry_t>(std::move(fb));
}

template<typename entry_t>
void
EntCache<entry_t>::clearFallback()
{
	fallback.reset();
}

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

template<>
void
EntCache<User>::fillCache() const
{
	setpwent();
	idcache->clear();
	std::array<char, BUFSIZ> buf {};
	passwd pwbuf {}, *pwp;
	while (getpwent_r(&pwbuf, buf.data(), buf.size(), &pwp) == 0) {
		idcache->insert(std::make_shared<User>(pwp->pw_uid, pwp->pw_name, pwp->pw_gid));
	}
	endpwent();
	time(&fillTime);
}

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

template<>
void
EntCache<Group>::fillCache() const
{
	setgrent();
	std::array<char, BUFSIZ> buf {};
	idcache->clear();
	group grpbuf {}, *grp;
	EntCache<User> instance;
	while (getgrent_r(&grpbuf, buf.data(), buf.size(), &grp) == 0) {
		auto g = std::make_shared<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(std::move(g));
	}
	endgrent();
	time(&fillTime);
}

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

#endif