--- Spectre2/Mounter.cpp 2005/03/25 01:38:00 428 +++ Spectre2/Mounter.cpp 2005/04/07 22:56:53 439 @@ -6,81 +6,111 @@ #include "Mounter.hpp" -#include -#include +#include +#include -#include - -Mounter::Mounter(const api::Path& config) : Daemon(), config(config) {} - -Mounter::~Mounter() +void Mounter::operator()() { -#ifdef __FreeBSD__ - api::Posix::CheckError(::unlink(secret.NullTerminate())); -#endif -} + // XXX: make sure the directory exists! #ifdef __FreeBSD__ -ext::String Mounter::secret(api::TheEnvironment.Get("HOME") + "/.nsmbrc"); -#endif + { + bool password(false); -void Mounter::load() -{ - _synchronized (Spectre2::sharesLock) - Spectre2::shares.Clear(); + // XXX: _unsynchronized + _synchronized (Daemon::secretLock) + { + api::FileReader in(Daemon::secret); + ext::String line; - _synchronized (Share::passwordsLock) - Share::passwords.Clear(); + while (ios::ReadLine(in, line)) if (line == "[" + share.getHost() + ":" + share.getOwner() + "]") + { + password = true; - _H document(xml::Parse(config.GetPath())); - _H spectre(*document/"spectre"); -#ifdef __FreeBSD__ - SecretFileWriter out(secret); - ios::FormatWriter fout(out); -#endif + 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()]; - _foreach (const xml::NodeSet, host_, *spectre/"host") + if (credentials.IsEmpty()) { - ext::String host(**host_/"name"); + credentials << "/tmp/.spectre" << api::Uuid::Create(); - _foreach (const xml::NodeSet, share, **host_/"share") { - ext::String name(**share/"name"), owner(**share/"owner"), user(**share/"user"), group(**share/"group"); + api::FileWriter out(credentials, O_WRONLY | O_CREAT | O_TRUNC, 0600); + ios::FormatWriter fout(out); - _synchronized (Spectre2::sharesLock) - Spectre2::shares.Insert(Share(host, name, owner, user, group)); + fout << "username = " << share.getOwner() << ios::NewLine << "password = " << share.getPassword() << ios::NewLine; } -#ifdef __FreeBSD__ - ::addrinfo* info; + _synchronized (Daemon::secretsLock) + Daemon::secrets[share.getOwner() + "@" + share.getHost()]; + } +#endif - api::Posix::CheckGaiError(::getaddrinfo(host.NullTerminate(), NULL, NULL, &info)); + _S uid; - ::sockaddr_in& sock(*reinterpret_cast< ::sockaddr_in*>(info->ai_addr)); - ext::Buffer buffer(128); + { + ::passwd pwd, * result; + ext::Buffer buffer(1024); - fout << "[" << host << "]" << ios::NewLine << "addr=" << ::inet_ntop(sock.sin_family, &sock.sin_addr, buffer.Begin(), buffer.GetSize()) << ios::NewLine; + api::Posix::CheckError(::getpwnam_r(share.getUser().NullTerminate(), &pwd, buffer.Begin(), buffer.GetSize(), &result)); - ::freeaddrinfo(info); -#endif + uid << pwd.pw_uid; } - _synchronized (Spectre2::sharesLock) - Spectre2::shares.Output(api::Cout); - - loaded = true; -} - -void Mounter::run() -{ - ext::ThreadSet<> workers; + _S gid; - _synchronized (Spectre2::sharesLock) _foreach (const ext::RedBlackSet, share, Spectre2::shares) if (!share->mounted() && share->mountable()) { - api::Cerr << "STUB: mount " << *share << ios::NewLine; + ::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; } - workers.Join(); + _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"); - ::sleep(5); + args.InsertLast(ext::JoinAll(options, ",")); + args.InsertLast("-t"); + args.InsertLast("smbfs"); + args.InsertLast(share.getService()); + args.InsertLast(share.getMount()); + + _S mount(Spectre2::mount, args); + + if (Spectre2::debug) + ios::ReadToWrite(*mount.GetReader(), api::Cout); + else + ios::Discard(*mount.GetReader()); }