1 |
douglas |
161 |
// Spectre Samba Mounter |
2 |
|
|
// |
3 |
|
|
// Douglas Thrift |
4 |
|
|
// |
5 |
|
|
// Maker.cpp |
6 |
douglas |
148 |
|
7 |
douglas |
161 |
#include "Maker.h" |
8 |
|
|
|
9 |
|
|
void Maker::make() |
10 |
|
|
{ |
11 |
douglas |
165 |
samba(); |
12 |
|
|
|
13 |
|
|
struct stat dir; |
14 |
|
|
|
15 |
|
|
if (stat(config.root.c_str(), &dir) != 0) |
16 |
|
|
{ |
17 |
|
|
if (mkdir(config.root.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH |
18 |
|
|
| S_IXOTH) != 0) |
19 |
|
|
{ |
20 |
|
|
string prefix = program + ": " + config.root; |
21 |
|
|
perror(prefix.c_str()); |
22 |
|
|
} |
23 |
|
|
} |
24 |
|
|
|
25 |
|
|
string path = config.root + "/" + host; |
26 |
|
|
|
27 |
|
|
if (stat(path.c_str(), &dir) != 0) |
28 |
|
|
{ |
29 |
|
|
if (mkdir(path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH |
30 |
|
|
| S_IXOTH) != 0) |
31 |
|
|
{ |
32 |
|
|
string prefix = program + ": " + path; |
33 |
|
|
perror(prefix.c_str()); |
34 |
|
|
} |
35 |
|
|
} |
36 |
|
|
|
37 |
douglas |
170 |
if (debug) cerr << "folders = {\n"; |
38 |
|
|
|
39 |
douglas |
165 |
for (map<string, Folder>::iterator itor = folders.begin(); itor |
40 |
|
|
!= folders.end(); itor++) |
41 |
|
|
{ |
42 |
douglas |
170 |
if (debug) cerr << " " << itor->first << " = {\n" |
43 |
|
|
<< " local = " << itor->second.local << "\n" |
44 |
|
|
<< " remote = " << itor->second.remote << "\n" |
45 |
|
|
<< " }\n"; |
46 |
|
|
|
47 |
douglas |
165 |
make(itor->first, itor->second.local); |
48 |
|
|
} |
49 |
douglas |
170 |
|
50 |
|
|
if (debug) cerr << "}\n"; |
51 |
douglas |
161 |
} |
52 |
douglas |
165 |
|
53 |
|
|
void Maker::samba() |
54 |
|
|
{ |
55 |
douglas |
166 |
char credentials[17]; |
56 |
|
|
|
57 |
|
|
sprintf(credentials, "%s", "/tmp/spectre.XXXX"); |
58 |
|
|
mkstemp(credentials); |
59 |
|
|
|
60 |
|
|
ofstream fout(credentials); |
61 |
|
|
|
62 |
|
|
fout << "username = " << regular.remote << "\n" |
63 |
|
|
<< "password = " << password(regular.remote) << "\n"; |
64 |
|
|
|
65 |
|
|
fout.close(); |
66 |
|
|
|
67 |
douglas |
165 |
vector<string> args; |
68 |
|
|
|
69 |
douglas |
166 |
args.push_back("spectre"); |
70 |
douglas |
165 |
args.push_back("-N"); |
71 |
|
|
args.push_back("-L"); |
72 |
|
|
args.push_back(host); |
73 |
douglas |
166 |
args.push_back("-A"); |
74 |
|
|
args.push_back(credentials); |
75 |
douglas |
165 |
|
76 |
|
|
ipstream smbclient(config.smbclient, args); |
77 |
|
|
|
78 |
douglas |
170 |
if (debug) cerr << "smbclient = {\n"; |
79 |
|
|
|
80 |
douglas |
169 |
if (connect(smbclient)) |
81 |
|
|
{ |
82 |
|
|
for (unsigned index = 0; index < 2; index++) |
83 |
|
|
{ |
84 |
|
|
string line; |
85 |
douglas |
166 |
|
86 |
douglas |
169 |
getline(smbclient, line); |
87 |
|
|
|
88 |
|
|
if (debug) cerr << line << "\n"; |
89 |
|
|
} |
90 |
|
|
|
91 |
|
|
samba(smbclient); |
92 |
|
|
} |
93 |
|
|
else |
94 |
|
|
{ |
95 |
|
|
cerr << program << ": Connection to " << host << " failed\n"; |
96 |
|
|
} |
97 |
|
|
|
98 |
douglas |
170 |
if (debug) cerr << "}\n"; |
99 |
|
|
|
100 |
douglas |
169 |
smbclient.close(); |
101 |
|
|
|
102 |
douglas |
166 |
unlink(credentials); |
103 |
douglas |
165 |
} |
104 |
|
|
|
105 |
douglas |
169 |
bool Maker::connect(ipstream& pin) |
106 |
|
|
{ |
107 |
|
|
while (pin.good()) |
108 |
|
|
{ |
109 |
|
|
string line; |
110 |
|
|
|
111 |
|
|
getline(pin, line); |
112 |
|
|
|
113 |
|
|
if (debug) cerr << line << "\n"; |
114 |
|
|
|
115 |
|
|
if (line == "") |
116 |
|
|
{ |
117 |
|
|
return true; |
118 |
|
|
} |
119 |
|
|
else if (line == "Connection to " + host + " failed") |
120 |
|
|
{ |
121 |
|
|
return false; |
122 |
|
|
} |
123 |
|
|
} |
124 |
|
|
|
125 |
|
|
return false; |
126 |
|
|
} |
127 |
|
|
|
128 |
|
|
void Maker::samba(ipstream& pin) |
129 |
|
|
{ |
130 |
|
|
while (pin.good()) |
131 |
|
|
{ |
132 |
|
|
if (pin.peek() != '\t') break; |
133 |
|
|
|
134 |
|
|
pin.ignore(); |
135 |
|
|
|
136 |
|
|
char share[16]; |
137 |
|
|
char type[11]; |
138 |
|
|
string comment; |
139 |
|
|
|
140 |
|
|
pin.get(share, 16); |
141 |
|
|
pin.get(type, 11); |
142 |
|
|
getline(pin, comment); |
143 |
|
|
|
144 |
|
|
if (debug) cerr << "\t" << share << type << comment << "\n"; |
145 |
|
|
|
146 |
|
|
strip(share); |
147 |
|
|
strip(type); |
148 |
|
|
|
149 |
|
|
if (string(type) == "Disk") |
150 |
|
|
{ |
151 |
|
|
unsigned end = strlen(share) - 1; |
152 |
|
|
|
153 |
|
|
if (share[end] == '$') continue; |
154 |
|
|
|
155 |
douglas |
170 |
if (folders.find(share) == folders.end()) |
156 |
|
|
{ |
157 |
|
|
folders.insert(pair<string, Folder>(share, regular)); |
158 |
|
|
} |
159 |
douglas |
169 |
} |
160 |
|
|
} |
161 |
|
|
|
162 |
|
|
string line; |
163 |
|
|
|
164 |
|
|
getline(pin, line); |
165 |
|
|
|
166 |
|
|
if (debug) cerr << line << "\n"; |
167 |
|
|
|
168 |
|
|
if (line != "") |
169 |
|
|
{ |
170 |
|
|
cerr << program << ": Error returning browse list\n"; |
171 |
|
|
} |
172 |
|
|
} |
173 |
|
|
|
174 |
|
|
void Maker::strip(char* name) |
175 |
|
|
{ |
176 |
|
|
for (unsigned index = strlen(name); index > 0; index--) |
177 |
|
|
{ |
178 |
|
|
if (name[index - 1] == ' ') |
179 |
|
|
{ |
180 |
|
|
name[index - 1] = '\0'; |
181 |
|
|
} |
182 |
|
|
else |
183 |
|
|
{ |
184 |
|
|
break; |
185 |
|
|
} |
186 |
|
|
} |
187 |
|
|
} |
188 |
|
|
|
189 |
douglas |
165 |
void Maker::make(const string& folder, const string& user) |
190 |
|
|
{ |
191 |
|
|
struct stat dir; |
192 |
|
|
string path = config.root + "/" + host + "/" + folder; |
193 |
|
|
|
194 |
|
|
if (stat(path.c_str(), &dir) != 0) |
195 |
|
|
{ |
196 |
|
|
if (mkdir(path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH |
197 |
|
|
| S_IXOTH) != 0) |
198 |
|
|
{ |
199 |
|
|
string prefix = program + ": " + path; |
200 |
|
|
perror(prefix.c_str()); |
201 |
|
|
} |
202 |
|
|
} |
203 |
|
|
|
204 |
|
|
struct passwd* item = getpwnam(user.c_str()); |
205 |
|
|
|
206 |
|
|
if (item == NULL) |
207 |
|
|
{ |
208 |
|
|
cerr << program << ": " << user << ": Unknown user\n"; |
209 |
|
|
return; |
210 |
|
|
} |
211 |
|
|
|
212 |
|
|
if (dir.st_uid == item->pw_uid && dir.st_gid == item->pw_gid) return; |
213 |
|
|
|
214 |
|
|
if (chown(path.c_str(), item->pw_uid, item->pw_gid) != 0) |
215 |
|
|
{ |
216 |
|
|
string prefix = program + ": " + path; |
217 |
|
|
perror(prefix.c_str()); |
218 |
|
|
} |
219 |
|
|
} |