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