// Spectre 2 // // Douglas Thrift // // $Id$ #include "Mounter.hpp" #include #include #include void Mounter::operator()() { ext::String service(share.getService()); { ext::Stack stack; for (api::Path path(service); !path.Exists(); path = path.GetParent()) stack.Push(path.GetPath()); while (!stack.IsEmpty()) { try { api::Posix::CheckError(::mkdir(stack.Top().NullTerminate(), 0755)); } catch (const api::Posix::Error&) {} stack.Pop(); } } uid_t uid; { ::passwd pwd, * result; ext::Buffer buffer(1024); api::Posix::CheckError(::getpwnam_r(share.getUser().NullTerminate(), &pwd, buffer.Begin(), buffer.GetSize(), &result)); uid = pwd.pw_uid; } gid_t gid; { ::group grp, * result; ext::Buffer buffer(1024); api::Posix::CheckError(::getgrnam_r(share.getGroup().NullTerminate(), &grp, buffer.Begin(), buffer.GetSize(), &result)); gid = grp.gr_gid; } try { api::Posix::CheckError(::chown(service.NullTerminate(), uid, gid)); } catch (const api::Posix::Error&) {} #ifdef __FreeBSD__ { bool password(false); // XXX: _unsynchronized _synchronized (Daemon::secretLock) { api::FileReader in(Daemon::secret); ext::String line; while (ios::ReadLine(in, line)) if (line == "[" + share.getHost() + ":" + share.getOwner() + "]") { password = true; break; } } if (!password) _synchronized (Daemon::secretLock) { api::FileWriter out(Daemon::secret, O_WRONLY | O_APPEND, 0); ios::FormatWriter fout(out); fout << "[" << share.getHost() << ":" << share.getOwner() << "]" << ios::NewLine << "password=" << share.getPassword() << ios::NewLine; } } #else _S credentials; // XXX: _unsynchronized _synchronized (Daemon::secretsLock) if (Daemon::secrets.Contains(share.getOwner() + "@" + share.getHost())) credentials = Daemon::secrets[share.getOwner() + "@" + share.getHost()]; if (credentials.IsEmpty()) { credentials << "/tmp/.spectre" << api::Uuid::Create(); { api::FileWriter out(credentials, O_WRONLY | O_CREAT | O_TRUNC, 0600); ios::FormatWriter fout(out); fout << "username = " << share.getOwner() << ios::NewLine << "password = " << share.getPassword() << ios::NewLine; } _synchronized (Daemon::secretsLock) Daemon::secrets[share.getOwner() + "@" + share.getHost()]; } #endif _L options; #ifdef __FreeBSD__ options.InsertLast("-N"); // XXX: hmm? // options.InsertLast("-O=" + share.getOwner() + ":"); options.InsertLast("-u=" + lexical_cast(uid)); options.InsertLast("-g=" + lexical_cast(gid)); #else options.InsertLast("credentials=" + credentials); options.InsertLast("uid=" + lexical_cast(uid)); options.InsertLast("gid=" + lexical_cast(gid)); options.InsertLast("rw"); #endif _L args(1, "-o"); args.InsertLast(ext::JoinAll(options, ",")); args.InsertLast("-t"); args.InsertLast("smbfs"); args.InsertLast(service); args.InsertLast(share.getMount()); _S mount(Spectre2::mount, args); if (Spectre2::debug) ios::ReadToWrite(*mount.GetReader(), api::Cout); else ios::Discard(*mount.GetReader()); }