1 |
douglas |
402 |
// Spectre 2 |
2 |
|
|
// |
3 |
|
|
// Douglas Thrift |
4 |
|
|
// |
5 |
|
|
// $Id$ |
6 |
|
|
|
7 |
|
|
#include "Mounter.hpp" |
8 |
|
|
|
9 |
douglas |
441 |
#include <menes-ext/stack.hpp> |
10 |
|
|
|
11 |
douglas |
432 |
#include <grp.h> |
12 |
|
|
#include <pwd.h> |
13 |
|
|
|
14 |
douglas |
431 |
void Mounter::operator()() |
15 |
douglas |
408 |
{ |
16 |
douglas |
441 |
ext::String service(share.getService()); |
17 |
douglas |
432 |
|
18 |
douglas |
441 |
{ |
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 |
douglas |
432 |
#ifdef __FreeBSD__ |
65 |
|
|
{ |
66 |
|
|
bool password(false); |
67 |
|
|
|
68 |
douglas |
433 |
// XXX: _unsynchronized |
69 |
|
|
_synchronized (Daemon::secretLock) |
70 |
douglas |
432 |
{ |
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 |
douglas |
433 |
if (!password) _synchronized (Daemon::secretLock) |
83 |
douglas |
432 |
{ |
84 |
douglas |
439 |
api::FileWriter out(Daemon::secret, O_WRONLY | O_APPEND, 0); |
85 |
douglas |
432 |
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 |
douglas |
439 |
api::FileWriter out(credentials, O_WRONLY | O_CREAT | O_TRUNC, 0600); |
103 |
douglas |
432 |
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 |
douglas |
441 |
options.InsertLast("-u=" + lexical_cast<ext::String>(uid)); |
120 |
|
|
options.InsertLast("-g=" + lexical_cast<ext::String>(gid)); |
121 |
douglas |
432 |
#else |
122 |
|
|
options.InsertLast("credentials=" + credentials); |
123 |
douglas |
441 |
options.InsertLast("uid=" + lexical_cast<ext::String>(uid)); |
124 |
|
|
options.InsertLast("gid=" + lexical_cast<ext::String>(gid)); |
125 |
douglas |
432 |
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 |
douglas |
441 |
args.InsertLast(service); |
134 |
douglas |
432 |
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 |
douglas |
402 |
} |