1 |
// Spectre 2 |
2 |
// |
3 |
// Douglas Thrift |
4 |
// |
5 |
// $Id$ |
6 |
|
7 |
#include "Mounter.hpp" |
8 |
|
9 |
#include <menes-ext/stack.hpp> |
10 |
|
11 |
#include <grp.h> |
12 |
#include <pwd.h> |
13 |
|
14 |
void Mounter::operator()() |
15 |
{ |
16 |
ext::String service(share.getService()); |
17 |
|
18 |
{ |
19 |
ext::Stack<ext::String> stack; |
20 |
|
21 |
for (api::Path path(service); !path.Exists(); path = path.GetParent()) |
22 |
stack.Push(path.GetPath()); |
23 |
|
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 |
stack.Pop(); |
33 |
} |
34 |
} |
35 |
|
36 |
uid_t uid; |
37 |
|
38 |
{ |
39 |
::passwd pwd, * result; |
40 |
ext::Buffer buffer(1024); |
41 |
|
42 |
api::Posix::CheckError(::getpwnam_r(share.getUser().NullTerminate(), &pwd, buffer.Begin(), buffer.GetSize(), &result)); |
43 |
|
44 |
uid = pwd.pw_uid; |
45 |
} |
46 |
|
47 |
gid_t gid; |
48 |
|
49 |
{ |
50 |
::group grp, * result; |
51 |
ext::Buffer buffer(1024); |
52 |
|
53 |
api::Posix::CheckError(::getgrnam_r(share.getGroup().NullTerminate(), &grp, buffer.Begin(), buffer.GetSize(), &result)); |
54 |
|
55 |
gid = grp.gr_gid; |
56 |
} |
57 |
|
58 |
try |
59 |
{ |
60 |
api::Posix::CheckError(::chown(service.NullTerminate(), uid, gid)); |
61 |
} |
62 |
catch (const api::Posix::Error&) {} |
63 |
|
64 |
#ifdef __FreeBSD__ |
65 |
{ |
66 |
bool password(false); |
67 |
|
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 |
// 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 |
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 |
_L<ext::String> args(1, "-o"); |
129 |
|
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 |
} |