1 |
// Spectre Samba Mounter |
2 |
// |
3 |
// Douglas Thrift |
4 |
// |
5 |
// Maker.cpp |
6 |
|
7 |
#include "Maker.h" |
8 |
|
9 |
void Maker::make() |
10 |
{ |
11 |
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 |
} |
43 |
|
44 |
void Maker::samba() |
45 |
{ |
46 |
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 |
vector<string> args; |
59 |
|
60 |
args.push_back("spectre"); |
61 |
args.push_back("-N"); |
62 |
args.push_back("-L"); |
63 |
args.push_back(host); |
64 |
args.push_back("-A"); |
65 |
args.push_back(credentials); |
66 |
|
67 |
ipstream smbclient(config.smbclient, args); |
68 |
|
69 |
// |
70 |
|
71 |
unlink(credentials); |
72 |
} |
73 |
|
74 |
void Maker::make(const string& folder, const string& user) |
75 |
{ |
76 |
struct stat dir; |
77 |
string path = config.root + "/" + host + "/" + folder; |
78 |
|
79 |
if (stat(path.c_str(), &dir) != 0) |
80 |
{ |
81 |
if (mkdir(path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH |
82 |
| S_IXOTH) != 0) |
83 |
{ |
84 |
string prefix = program + ": " + path; |
85 |
perror(prefix.c_str()); |
86 |
} |
87 |
} |
88 |
|
89 |
struct passwd* item = getpwnam(user.c_str()); |
90 |
|
91 |
if (item == NULL) |
92 |
{ |
93 |
cerr << program << ": " << user << ": Unknown user\n"; |
94 |
return; |
95 |
} |
96 |
|
97 |
if (dir.st_uid == item->pw_uid && dir.st_gid == item->pw_gid) return; |
98 |
|
99 |
if (chown(path.c_str(), item->pw_uid, item->pw_gid) != 0) |
100 |
{ |
101 |
string prefix = program + ": " + path; |
102 |
perror(prefix.c_str()); |
103 |
} |
104 |
} |