ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/repos/Spectre2/Mounter.cpp
(Generate patch)

Comparing Spectre2/Mounter.cpp (file contents):
Revision 428 by douglas, 2005-03-24T17:38:00-08:00 vs.
Revision 439 by douglas, 2005-04-07T15:56:53-07:00

# Line 6 | Line 6
6  
7   #include "Mounter.hpp"
8  
9 < #include <menes-api/environment.hpp>
10 < #include <menes-api/socket.hpp>
9 > #include <grp.h>
10 > #include <pwd.h>
11  
12 < #include <arpa/inet.h>
13 <
14 < Mounter::Mounter(const api::Path& config) : Daemon(), config(config) {}
15 <
16 < Mounter::~Mounter()
12 > void Mounter::operator()()
13   {
14 < #ifdef __FreeBSD__
19 <        api::Posix::CheckError(::unlink(secret.NullTerminate()));
20 < #endif
21 < }
14 >        // XXX: make sure the directory exists!
15  
16   #ifdef __FreeBSD__
17 < ext::String Mounter::secret(api::TheEnvironment.Get("HOME") + "/.nsmbrc");
18 < #endif
17 >        {
18 >                bool password(false);
19  
20 < void Mounter::load()
21 < {
22 <        _synchronized (Spectre2::sharesLock)
23 <                Spectre2::shares.Clear();
20 >                // XXX: _unsynchronized
21 >                _synchronized (Daemon::secretLock)
22 >                {
23 >                        api::FileReader in(Daemon::secret);
24 >                        ext::String line;
25  
26 <        _synchronized (Share::passwordsLock)
27 <                Share::passwords.Clear();
26 >                        while (ios::ReadLine(in, line)) if (line == "[" + share.getHost() + ":" + share.getOwner() + "]")
27 >                        {
28 >                                password = true;
29  
30 <        _H<xml::Document> document(xml::Parse(config.GetPath()));
31 <        _H<xml::Node> spectre(*document/"spectre");
32 < #ifdef __FreeBSD__
33 <        SecretFileWriter out(secret);
34 <        ios::FormatWriter fout(out);
35 < #endif
30 >                                break;
31 >                        }
32 >                }
33 >
34 >                if (!password) _synchronized (Daemon::secretLock)
35 >                {
36 >                        api::FileWriter out(Daemon::secret, O_WRONLY | O_APPEND, 0);
37 >                        ios::FormatWriter fout(out);
38 >
39 >                        fout << "[" << share.getHost() << ":" << share.getOwner() << "]" << ios::NewLine << "password=" << share.getPassword() << ios::NewLine;
40 >                }
41 >        }
42 > #else
43 >        _S<ios::String> credentials;
44 >
45 >        // XXX: _unsynchronized
46 >        _synchronized (Daemon::secretsLock) if (Daemon::secrets.Contains(share.getOwner() + "@" + share.getHost()))
47 >                credentials = Daemon::secrets[share.getOwner() + "@" + share.getHost()];
48  
49 <        _foreach (const xml::NodeSet, host_, *spectre/"host")
49 >        if (credentials.IsEmpty())
50          {
51 <                ext::String host(**host_/"name");
51 >                credentials << "/tmp/.spectre" << api::Uuid::Create();
52  
46                _foreach (const xml::NodeSet, share, **host_/"share")
53                  {
54 <                        ext::String name(**share/"name"), owner(**share/"owner"), user(**share/"user"), group(**share/"group");
54 >                        api::FileWriter out(credentials, O_WRONLY | O_CREAT | O_TRUNC, 0600);
55 >                        ios::FormatWriter fout(out);
56  
57 <                        _synchronized (Spectre2::sharesLock)
51 <                                Spectre2::shares.Insert(Share(host, name, owner, user, group));
57 >                        fout << "username = " << share.getOwner() << ios::NewLine << "password = " << share.getPassword() << ios::NewLine;
58                  }
59  
60 < #ifdef __FreeBSD__
61 <                ::addrinfo* info;
60 >                _synchronized (Daemon::secretsLock)
61 >                        Daemon::secrets[share.getOwner() + "@" + share.getHost()];
62 >        }
63 > #endif
64  
65 <                api::Posix::CheckGaiError(::getaddrinfo(host.NullTerminate(), NULL, NULL, &info));
65 >        _S<ios::String> uid;
66  
67 <                ::sockaddr_in& sock(*reinterpret_cast< ::sockaddr_in*>(info->ai_addr));
68 <                ext::Buffer buffer(128);
67 >        {
68 >                ::passwd pwd, * result;
69 >                ext::Buffer buffer(1024);
70  
71 <                fout << "[" << host << "]" << ios::NewLine << "addr=" << ::inet_ntop(sock.sin_family, &sock.sin_addr, buffer.Begin(), buffer.GetSize()) << ios::NewLine;
71 >                api::Posix::CheckError(::getpwnam_r(share.getUser().NullTerminate(), &pwd, buffer.Begin(), buffer.GetSize(), &result));
72  
73 <                ::freeaddrinfo(info);
65 < #endif
73 >                uid << pwd.pw_uid;
74          }
75  
76 <        _synchronized (Spectre2::sharesLock)
69 <                Spectre2::shares.Output(api::Cout);
70 <        
71 <        loaded = true;
72 < }
73 <
74 < void Mounter::run()
75 < {
76 <        ext::ThreadSet<> workers;
76 >        _S<ios::String> gid;
77  
78        _synchronized (Spectre2::sharesLock) _foreach (const ext::RedBlackSet<Share>, share, Spectre2::shares) if (!share->mounted() && share->mountable())
78          {
79 <                api::Cerr << "STUB: mount " << *share << ios::NewLine;
79 >                ::group grp, * result;
80 >                ext::Buffer buffer(1024);
81 >
82 >                api::Posix::CheckError(::getgrnam_r(share.getGroup().NullTerminate(), &grp, buffer.Begin(), buffer.GetSize(), &result));
83 >
84 >                gid << grp.gr_gid;
85          }
86  
87 <        workers.Join();
87 >        _L<ext::String> options;
88 >
89 > #ifdef __FreeBSD__
90 >        options.InsertLast("-N");
91 >        // XXX: hmm?
92 > //      options.InsertLast("-O=" + share.getOwner() + ":");
93 >        options.InsertLast("-u=" + uid);
94 >        options.InsertLast("-g=" + gid);
95 > #else
96 >        options.InsertLast("credentials=" + credentials);
97 >        options.InsertLast("uid=" + uid);
98 >        options.InsertLast("gid=" + gid);
99 >        options.InsertLast("rw");
100 > #endif
101 >
102 >        _L<ext::String> args(1, "-o");
103  
104 <        ::sleep(5);
104 >        args.InsertLast(ext::JoinAll<ext::String>(options, ","));
105 >        args.InsertLast("-t");
106 >        args.InsertLast("smbfs");
107 >        args.InsertLast(share.getService());
108 >        args.InsertLast(share.getMount());
109 >
110 >        _S<api::Process> mount(Spectre2::mount, args);
111 >
112 >        if (Spectre2::debug)
113 >                ios::ReadToWrite(*mount.GetReader(), api::Cout);
114 >        else
115 >                ios::Discard(*mount.GetReader());
116   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines