ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/proj/trunk/Spectre/Mounter.cpp
Revision: 227
Committed: 2003-07-30T17:56:29-07:00 (21 years, 10 months ago) by douglas
File size: 4086 byte(s)
Log Message:
Thought about the meaning of life, added $Id$ tags.

File Contents

# Content
1 // Spectre Samba Mounter
2 //
3 // Douglas Thrift
4 //
5 // $Id: Mounter.cpp,v 1.10 2003/07/31 00:56:29 douglas Exp $
6
7 #include "Mounter.h"
8
9 #ifdef _FreeBSD_
10 #include <fcntl.h>
11 #endif
12
13 void Mounter::mount()
14 {
15 if (!good) return;
16
17 samba();
18
19 if (debug)
20 {
21 cerr << "mounted = {\n";
22
23 for (set<string>::iterator itor = mounted.begin();
24 itor != mounted.end(); itor++)
25 {
26 cerr << " " << *itor << "\n";
27 }
28
29 cerr << "}\n";
30 }
31
32 if (debug) cerr << "folders = {\n";
33
34 for (map<string, Folder>::iterator itor = folders.begin(); itor !=
35 folders.end(); itor++)
36 {
37 if (debug) cerr << " " << itor->first << " = {\n"
38 << " local = " << itor->second.local << "\n"
39 << " remote = " << itor->second.remote << "\n"
40 << " }\n";
41
42 mount(itor->first, itor->second.local, itor->second.remote);
43 }
44
45 if (debug) cerr << "}\n";
46 }
47
48 void Mounter::samba()
49 {
50 string path = config.root + "/" + host;
51 DIR* dir = opendir(path.c_str());
52 dirent* file;
53
54 while ((file = readdir(dir)) != NULL)
55 {
56 struct stat dir;
57 string folder = path + "/" + file->d_name;
58
59 stat(folder.c_str(), &dir);
60
61 if (S_ISDIR(dir.st_mode))
62 {
63 string folder = file->d_name;
64
65 if (folder == "." || folder == "..") continue;
66
67 string share = folder.find('.') == 0 ? folder.erase(0, 1) + "$" :
68 folder;
69
70 if (folders.find(share) == folders.end())
71 {
72 folders.insert(pair<string, Folder>(share, regular));
73 }
74 }
75 }
76
77 closedir(dir);
78
79 vector<string> args;
80
81 args.push_back("spectre");
82 args.push_back("-t");
83 args.push_back("smbfs");
84
85 ipstream mount(config.mount, args);
86
87 if (debug) cerr << "mount = {\n";
88
89 samba(mount);
90
91 if (debug) cerr << "}\n";
92
93 mount.close();
94 }
95
96 void Mounter::samba(ipstream& pin)
97 {
98 while (pin.good())
99 {
100 string line;
101
102 getline(pin, line);
103
104 if (debug) cerr << line << "\n";
105
106 if (line == "") continue;
107
108 unsigned begin = line.find(" on ") + 4;
109 #if defined _Linux_
110 unsigned end = line.rfind(" type smbfs");
111 #elif defined _FreeBSD_
112 unsigned end = line.rfind(" (smbfs)");
113 #endif
114
115 string path = line.substr(begin, end - begin);
116
117 if (mounted.find(path) == mounted.end())
118 {
119 mounted.insert(path);
120 }
121 }
122 }
123
124 void Mounter::mount(const string& folder, const string& user, const string&
125 owner)
126 {
127 string path = config.root + "/" + host + "/" + (folder[folder.length() -
128 1] == '$' ? "." + folder.substr(0, folder.length() - 1) : folder);
129
130 if (mounted.find(path) != mounted.end()) return;
131
132 ostringstream options;
133
134 struct passwd* item = getpwnam(user.c_str());
135
136 if (item == NULL)
137 {
138 cerr << program << ": " << user << ": Unknown user\n";
139 return;
140 }
141
142 #if defined _Linux_
143 char credentials[17];
144
145 sprintf(credentials, "%s", "/tmp/spectre.XXXX");
146 mkstemp(credentials);
147
148 ofstream fout(credentials);
149
150 fout << "username = " << owner << "\n"
151 << "password = " << password(owner) << "\n";
152
153 fout.close();
154
155 options << "credentials=" << credentials << ","
156 << "uid=" << item->pw_uid << ","
157 << "gid=" << item->pw_gid;
158
159 string smb = "//" + host + "/" + folder;
160 #elif defined _FreeBSD_
161 char* credentials = new char[strlen(getenv("HOME")) + 9];
162
163 sprintf(credentials, "%s/.nsmbrc", getenv("HOME"));
164 int dot = open(credentials, O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
165 close(dot);
166
167 ofstream fout(credentials);
168
169 fout << "[" << toupper(host) << ":" << toupper(owner) << "]\n"
170 << "password=" << password(owner) << "\n";
171
172 fout.close();
173
174 options << "-N,"
175 << "-u=" << item->pw_uid << ","
176 << "-g=" << item->pw_gid;
177
178 string smb = "//" + owner + "@" + host + "/" + folder;
179 #endif
180
181 vector<string> args;
182
183 args.push_back("spectre");
184 #ifdef _FreeBSD_
185 args.push_back("-o");
186 args.push_back(options.str());
187 #endif
188 args.push_back("-t");
189 args.push_back("smbfs");
190 #ifdef _Linux_
191 args.push_back("-o");
192 args.push_back(options.str());
193 #endif
194 args.push_back(smb);
195 args.push_back(path);
196
197 ipstream mount(config.mount, args, pstreambuf::pstderr);
198
199 while (mount.good())
200 {
201 string line;
202
203 getline(mount, line);
204
205 if (line != "") cerr << line << "\n";
206 }
207
208 mount.close();
209
210 unlink(credentials);
211 #ifdef _FreeBSD_
212 delete [] credentials;
213 #endif
214 }