6 |
|
|
7 |
|
#include "Mounter.hpp" |
8 |
|
|
9 |
< |
#include <menes-api/environment.hpp> |
10 |
< |
#include <menes-api/socket.hpp> |
9 |
> |
#include <menes-ext/stack.hpp> |
10 |
|
|
11 |
< |
#include <arpa/inet.h> |
11 |
> |
#include <grp.h> |
12 |
> |
#include <pwd.h> |
13 |
|
|
14 |
< |
Mounter::Mounter(const api::Path& config) : Daemon(), config(config) {} |
15 |
< |
|
16 |
< |
Mounter::~Mounter() |
14 |
> |
void Mounter::operator()() |
15 |
|
{ |
16 |
< |
#ifdef __FreeBSD__ |
19 |
< |
api::Posix::CheckError(::unlink(secret.NullTerminate())); |
20 |
< |
#endif |
21 |
< |
} |
16 |
> |
ext::String service(share.getService()); |
17 |
|
|
18 |
< |
#ifdef __FreeBSD__ |
19 |
< |
ext::String Mounter::secret(api::TheEnvironment.Get("HOME") + "/.nsmbrc"); |
25 |
< |
#endif |
18 |
> |
{ |
19 |
> |
ext::Stack<ext::String> stack; |
20 |
|
|
21 |
< |
void Mounter::load() |
22 |
< |
{ |
29 |
< |
_synchronized (Spectre2::sharesLock) |
30 |
< |
Spectre2::shares.Clear(); |
21 |
> |
for (api::Path path(service); !path.Exists(); path = path.GetParent()) |
22 |
> |
stack.Push(path.GetPath()); |
23 |
|
|
24 |
< |
_synchronized (Share::passwordsLock) |
25 |
< |
Share::passwords.Clear(); |
24 |
> |
while (!stack.IsEmpty()) |
25 |
> |
{ |
26 |
> |
try |
27 |
> |
{ |
28 |
> |
api::Posix::CheckError(::mkdir(stack.Top().NullTerminate(), 0755)); |
29 |
> |
} |
30 |
> |
catch (const api::Posix::Error&) {} |
31 |
|
|
32 |
< |
_H<xml::Document> document(xml::Parse(config.GetPath())); |
33 |
< |
_H<xml::Node> spectre(*document/"spectre"); |
34 |
< |
#ifdef __FreeBSD__ |
35 |
< |
SecretFileWriter out(secret); |
36 |
< |
ios::FormatWriter fout(out); |
40 |
< |
#endif |
32 |
> |
stack.Pop(); |
33 |
> |
} |
34 |
> |
} |
35 |
> |
|
36 |
> |
uid_t uid; |
37 |
|
|
42 |
– |
_foreach (const xml::NodeSet, host_, *spectre/"host") |
38 |
|
{ |
39 |
< |
ext::String host(**host_/"name"); |
39 |
> |
::passwd pwd, * result; |
40 |
> |
ext::Buffer buffer(1024); |
41 |
|
|
42 |
< |
_foreach (const xml::NodeSet, share, **host_/"share") |
47 |
< |
{ |
48 |
< |
ext::String name(**share/"name"), owner(**share/"owner"), user(**share/"user"), group(**share/"group"); |
42 |
> |
api::Posix::CheckError(::getpwnam_r(share.getUser().NullTerminate(), &pwd, buffer.Begin(), buffer.GetSize(), &result)); |
43 |
|
|
44 |
< |
_synchronized (Spectre2::sharesLock) |
45 |
< |
Spectre2::shares.Insert(Share(host, name, owner, user, group)); |
52 |
< |
} |
44 |
> |
uid = pwd.pw_uid; |
45 |
> |
} |
46 |
|
|
47 |
< |
#ifdef __FreeBSD__ |
55 |
< |
::addrinfo* info; |
47 |
> |
gid_t gid; |
48 |
|
|
49 |
< |
api::Posix::CheckGaiError(::getaddrinfo(host.NullTerminate(), NULL, NULL, &info)); |
49 |
> |
{ |
50 |
> |
::group grp, * result; |
51 |
> |
ext::Buffer buffer(1024); |
52 |
|
|
53 |
< |
::sockaddr_in& sock(*reinterpret_cast< ::sockaddr_in*>(info->ai_addr)); |
60 |
< |
ext::Buffer buffer(128); |
53 |
> |
api::Posix::CheckError(::getgrnam_r(share.getGroup().NullTerminate(), &grp, buffer.Begin(), buffer.GetSize(), &result)); |
54 |
|
|
55 |
< |
fout << "[" << host << "]" << ios::NewLine << "addr=" << ::inet_ntop(sock.sin_family, &sock.sin_addr, buffer.Begin(), buffer.GetSize()) << ios::NewLine; |
55 |
> |
gid = grp.gr_gid; |
56 |
> |
} |
57 |
|
|
58 |
< |
::freeaddrinfo(info); |
59 |
< |
#endif |
58 |
> |
try |
59 |
> |
{ |
60 |
> |
api::Posix::CheckError(::chown(service.NullTerminate(), uid, gid)); |
61 |
|
} |
62 |
+ |
catch (const api::Posix::Error&) {} |
63 |
|
|
64 |
< |
_synchronized (Spectre2::sharesLock) |
65 |
< |
Spectre2::shares.Output(api::Cout); |
66 |
< |
|
71 |
< |
loaded = true; |
72 |
< |
} |
64 |
> |
#ifdef __FreeBSD__ |
65 |
> |
{ |
66 |
> |
bool password(false); |
67 |
|
|
68 |
< |
void Mounter::run() |
69 |
< |
{ |
70 |
< |
ext::ThreadSet<> workers; |
68 |
> |
// XXX: _unsynchronized |
69 |
> |
_synchronized (Daemon::secretLock) |
70 |
> |
{ |
71 |
> |
api::FileReader in(Daemon::secret); |
72 |
> |
ext::String line; |
73 |
> |
|
74 |
> |
while (ios::ReadLine(in, line)) if (line == "[" + share.getHost() + ":" + share.getOwner() + "]") |
75 |
> |
{ |
76 |
> |
password = true; |
77 |
> |
|
78 |
> |
break; |
79 |
> |
} |
80 |
> |
} |
81 |
> |
|
82 |
> |
if (!password) _synchronized (Daemon::secretLock) |
83 |
> |
{ |
84 |
> |
api::FileWriter out(Daemon::secret, O_WRONLY | O_APPEND, 0); |
85 |
> |
ios::FormatWriter fout(out); |
86 |
> |
|
87 |
> |
fout << "[" << share.getHost() << ":" << share.getOwner() << "]" << ios::NewLine << "password=" << share.getPassword() << ios::NewLine; |
88 |
> |
} |
89 |
> |
} |
90 |
> |
#else |
91 |
> |
_S<ios::String> credentials; |
92 |
|
|
93 |
< |
_synchronized (Spectre2::sharesLock) _foreach (const ext::RedBlackSet<Share>, share, Spectre2::shares) if (!share->mounted() && share->mountable()) |
93 |
> |
// XXX: _unsynchronized |
94 |
> |
_synchronized (Daemon::secretsLock) if (Daemon::secrets.Contains(share.getOwner() + "@" + share.getHost())) |
95 |
> |
credentials = Daemon::secrets[share.getOwner() + "@" + share.getHost()]; |
96 |
> |
|
97 |
> |
if (credentials.IsEmpty()) |
98 |
|
{ |
99 |
< |
api::Cerr << "STUB: mount " << *share << ios::NewLine; |
99 |
> |
credentials << "/tmp/.spectre" << api::Uuid::Create(); |
100 |
> |
|
101 |
> |
{ |
102 |
> |
api::FileWriter out(credentials, O_WRONLY | O_CREAT | O_TRUNC, 0600); |
103 |
> |
ios::FormatWriter fout(out); |
104 |
> |
|
105 |
> |
fout << "username = " << share.getOwner() << ios::NewLine << "password = " << share.getPassword() << ios::NewLine; |
106 |
> |
} |
107 |
> |
|
108 |
> |
_synchronized (Daemon::secretsLock) |
109 |
> |
Daemon::secrets[share.getOwner() + "@" + share.getHost()]; |
110 |
|
} |
111 |
+ |
#endif |
112 |
+ |
|
113 |
+ |
_L<ext::String> options; |
114 |
+ |
|
115 |
+ |
#ifdef __FreeBSD__ |
116 |
+ |
options.InsertLast("-N"); |
117 |
+ |
// XXX: hmm? |
118 |
+ |
// options.InsertLast("-O=" + share.getOwner() + ":"); |
119 |
+ |
options.InsertLast("-u=" + lexical_cast<ext::String>(uid)); |
120 |
+ |
options.InsertLast("-g=" + lexical_cast<ext::String>(gid)); |
121 |
+ |
#else |
122 |
+ |
options.InsertLast("credentials=" + credentials); |
123 |
+ |
options.InsertLast("uid=" + lexical_cast<ext::String>(uid)); |
124 |
+ |
options.InsertLast("gid=" + lexical_cast<ext::String>(gid)); |
125 |
+ |
options.InsertLast("rw"); |
126 |
+ |
#endif |
127 |
|
|
128 |
< |
workers.Join(); |
128 |
> |
_L<ext::String> args(1, "-o"); |
129 |
|
|
130 |
< |
::sleep(5); |
130 |
> |
args.InsertLast(ext::JoinAll<ext::String>(options, ",")); |
131 |
> |
args.InsertLast("-t"); |
132 |
> |
args.InsertLast("smbfs"); |
133 |
> |
args.InsertLast(service); |
134 |
> |
args.InsertLast(share.getMount()); |
135 |
> |
|
136 |
> |
_S<api::Process> mount(Spectre2::mount, args); |
137 |
> |
|
138 |
> |
if (Spectre2::debug) |
139 |
> |
ios::ReadToWrite(*mount.GetReader(), api::Cout); |
140 |
> |
else |
141 |
> |
ios::Discard(*mount.GetReader()); |
142 |
|
} |