--- Spectre2/Mounter.cpp 2004/12/31 06:08:35 406 +++ Spectre2/Mounter.cpp 2005/03/29 21:43:22 433 @@ -6,18 +6,111 @@ #include "Mounter.hpp" -Mounter::Mounter(const ext::String& config) : Daemon(), config(config) {} +#include +#include -void Mounter::load() +void Mounter::operator()() { - api::Cerr << "Mounter::load()" << ios::NewLine; - - loaded = true; -} + // XXX: make sure the directory exists! -void Mounter::run() -{ - api::Cerr << "Mounter::run()" << ios::NewLine; +#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) + { + AppendFileWriter out(Daemon::secret); + 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(); + + { + SecretFileWriter out(credentials); + 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 + + _S 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; + } + + _S 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; + } + + _L options; + +#ifdef __FreeBSD__ + options.InsertLast("-N"); + // XXX: hmm? +// options.InsertLast("-O=" + share.getOwner() + ":"); + options.InsertLast("-u=" + uid); + options.InsertLast("-g=" + gid); +#else + options.InsertLast("credentials=" + credentials); + options.InsertLast("uid=" + uid); + options.InsertLast("gid=" + gid); + options.InsertLast("rw"); +#endif + + _L args(1, "-o"); + + args.InsertLast(ext::JoinAll(options, ",")); + args.InsertLast("-t"); + args.InsertLast("smbfs"); + args.InsertLast(share.getService()); + args.InsertLast(share.getMount()); + + _S mount(Spectre2::mount, args); - running = false; + if (Spectre2::debug) + ios::ReadToWrite(*mount.GetReader(), api::Cout); + else + ios::Discard(*mount.GetReader()); }