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