summaryrefslogtreecommitdiff
path: root/netfs/daemon/daemon.cpp
blob: cf312bb8ad9d6b44cc6536c5a33c5cf49d271d5c (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
#include <pch.hpp>
#include <Ice/Ice.h>
#include "daemon.h"
#include "daemonService.h"
#include "daemonVolume.h"
#include <slicer/slicer.h>
#include <xml/serializer.h>
#include "ioHelpers.h"
#include <sys/stat.h>

NetFSDaemon::NetFSDaemon(const Ice::CommunicatorPtr & i) :
	ic(i)
{
}

std::string
NetFSDaemon::hostname()
{
	auto props = ic->getProperties();
	auto hostNameOverride = props->getProperty("NetFSD.HostNameOverride");
	if (hostNameOverride.empty()) {
		char buf[128];
		gethostname(buf, sizeof(buf));
		return buf;
	}
	return hostNameOverride;
}

// name = NetFSDaemonAdapter
void
NetFSDaemon::start(const std::string & name, const Ice::CommunicatorPtr &, const Ice::StringSeq&)
{
	Ice::PropertiesPtr props = ic->getProperties();
	LoadConfiguration(props->getProperty("NetFSD.ConfigPath"));

	adapter = ic->createObjectAdapterWithEndpoints(name, dc->Self->Endpoint);
	adapter->add(new ServiceServer(dc->CurrentConfiguration), ic->stringToIdentity("Service"));
	adapter->activate();
}

NetFS::Daemon::ConfigurationPtr
NetFSDaemon::ReadConfiguration(const boost::filesystem::path & path) const
{
	return Slicer::Deserialize<Slicer::XmlFileDeserializer, NetFS::Daemon::Configuration>(path);
}

void
NetFSDaemon::LoadConfiguration(const boost::filesystem::path & path)
{
	dc = new NetFS::Daemon::RuntimeConfiguration();
	dc->CurrentConfiguration = ReadConfiguration(path);
	auto selfItr = dc->CurrentConfiguration->Hosts.find(hostname());
	if (selfItr == dc->CurrentConfiguration->Hosts.end()) {
		throw std::runtime_error("This host is not defined in the configuration.");
	}
	dc->Self = selfItr->second;
}

void
NetFSDaemon::stop()
{
	adapter->deactivate();
}

extern "C" {
	IceBox::Service *
	createNetFSDaemon(Ice::CommunicatorPtr ic)
	{
		return new NetFSDaemon(ic);
	}
}

TempPrivs::TempPrivs(const NetFS::ReqEnv & re, const boost::filesystem::path & r) :
	myu(UserEntCache::instance.getID(re.user)),
	myg(GroupEntCache::instance.getID(re.grp)),
	root(r)
{
}

void
TempPrivs::AssertRead(const boost::filesystem::path & p) const
{
	if (p != root) {
		AssertRead(p.parent_path());
	}
	struct stat s;
	if (::lstat(p.string().c_str(), &s) != 0) {
		throw NetFS::SystemError(errno);
	}
	if (!ReadableBy(s, myu, myg)) {
		throw NetFS::SystemError(EACCES);
	}
}

void
TempPrivs::AssertWrite(const boost::filesystem::path & p) const
{
	if (p != root) {
		AssertRead(p.parent_path());
	}
	struct stat s;
	if (::lstat(p.string().c_str(), &s) != 0) {
		throw NetFS::SystemError(errno);
	}
	if (!WritableBy(s, myu, myg)) {
		throw NetFS::SystemError(EACCES);
	}
}