1 |
// Spectre Samba Mounter |
2 |
// |
3 |
// Douglas Thrift |
4 |
// |
5 |
// Configurator.cpp |
6 |
|
7 |
#include "Configurator.h" |
8 |
|
9 |
Configurator::Configurator(const string& host) |
10 |
{ |
11 |
this->host = host; |
12 |
|
13 |
configurate(); |
14 |
} |
15 |
|
16 |
string Configurator::escape(const string& phrase) |
17 |
{ |
18 |
istringstream sin(phrase); |
19 |
string escaped; |
20 |
|
21 |
do |
22 |
{ |
23 |
if (sin.peek() == ' ') escaped += '\\'; |
24 |
escaped += sin.get(); |
25 |
if (sin.eof()) escaped.erase(escaped.length() - 1); |
26 |
} |
27 |
while (sin.good()); |
28 |
|
29 |
return escaped; |
30 |
} |
31 |
|
32 |
string Configurator::password(const string& user) |
33 |
{ |
34 |
string password; |
35 |
|
36 |
for (unsigned index = 0; index < passwd.size(); index++) |
37 |
{ |
38 |
switch (passwd[index]) |
39 |
{ |
40 |
case file: |
41 |
{ |
42 |
string file = config.install + "/conf/private/" + host + "." |
43 |
+ user; |
44 |
ifstream fin(file.c_str()); |
45 |
|
46 |
if (fin.is_open()) |
47 |
{ |
48 |
getline(fin, password); |
49 |
} |
50 |
} |
51 |
break; |
52 |
case prompt: |
53 |
{ |
54 |
string prompt = user + "@" + host + "\'s password:"; |
55 |
password = getpass(prompt.c_str()); |
56 |
} |
57 |
break; |
58 |
default: |
59 |
// |
60 |
break; |
61 |
} |
62 |
|
63 |
if (password == "") |
64 |
{ |
65 |
continue; |
66 |
} |
67 |
else |
68 |
{ |
69 |
break; |
70 |
} |
71 |
} |
72 |
|
73 |
return password; |
74 |
} |
75 |
|
76 |
void Configurator::configurate() |
77 |
{ |
78 |
if (config.hosts.count(host) < 2) |
79 |
{ |
80 |
cerr << program << ": No configuration for host: " << host << "\n"; |
81 |
exit(1); |
82 |
} |
83 |
|
84 |
for (multimap<string, string>::iterator itor = |
85 |
config.hosts.lower_bound(host); itor != config.hosts.upper_bound(host); |
86 |
itor++) |
87 |
{ |
88 |
string entry = itor->second; |
89 |
|
90 |
if (entry.find("passwd=") == 0) |
91 |
{ |
92 |
istringstream sin(entry.substr(7)); |
93 |
|
94 |
do |
95 |
{ |
96 |
string item; |
97 |
getline(sin, item, ','); |
98 |
|
99 |
if (item == "file") |
100 |
{ |
101 |
passwd.push_back(file); |
102 |
} |
103 |
else if (item == "prompt") |
104 |
{ |
105 |
passwd.push_back(prompt); |
106 |
} |
107 |
} |
108 |
while (sin.good()); |
109 |
} |
110 |
else if (entry.find("folder=") == 0) |
111 |
{ |
112 |
istringstream sin(entry.substr(7)); |
113 |
|
114 |
string folder; |
115 |
getline(sin, folder, ':'); |
116 |
|
117 |
string local; |
118 |
getline(sin, local, ':'); |
119 |
|
120 |
string remote; |
121 |
getline(sin, remote); |
122 |
|
123 |
if (folder == "*") |
124 |
{ |
125 |
regular.local = local; |
126 |
regular.remote = remote; |
127 |
} |
128 |
else |
129 |
{ |
130 |
Folder special; |
131 |
|
132 |
special.local = local; |
133 |
special.remote = remote; |
134 |
|
135 |
folders.insert(pair<string, Folder>(folder, special)); |
136 |
} |
137 |
} |
138 |
else |
139 |
{ |
140 |
cerr << program << ": Unknown configuration entry: \"" << entry |
141 |
<< "\", ignored\n"; |
142 |
} |
143 |
} |
144 |
|
145 |
if (passwd.size() == 0) |
146 |
{ |
147 |
cerr << program << ": No passwd configuration for host: " << host |
148 |
<< "\n"; |
149 |
exit(1); |
150 |
} |
151 |
|
152 |
if (regular.local == "" || regular.remote == "") |
153 |
{ |
154 |
cerr << program << ": No * folder configuration for host: " << host |
155 |
<< "\n"; |
156 |
exit(1); |
157 |
} |
158 |
|
159 |
if (debug) |
160 |
{ |
161 |
cerr << "host = " << host << "\n" |
162 |
<< "passwd = {\n"; |
163 |
|
164 |
for (unsigned index = 0; index < passwd.size(); index++) |
165 |
{ |
166 |
cerr << " [" << index << "] = "; |
167 |
|
168 |
switch (passwd[index]) |
169 |
{ |
170 |
case file: |
171 |
cerr << "file"; |
172 |
break; |
173 |
case prompt: |
174 |
cerr << "prompt"; |
175 |
break; |
176 |
default: |
177 |
cerr << passwd[index]; |
178 |
break; |
179 |
} |
180 |
|
181 |
cerr << "\n"; |
182 |
} |
183 |
|
184 |
cerr << "}\n" |
185 |
<< "regular = {\n" |
186 |
<< " local = " << regular.local << "\n" |
187 |
<< " remote = " << regular.remote << "\n" |
188 |
<< "}\n" |
189 |
<< "folders = {\n"; |
190 |
|
191 |
for (map<string, Folder>::iterator itor = folders.begin(); itor != |
192 |
folders.end(); itor++) |
193 |
{ |
194 |
cerr << " " << itor->first << " = {\n" |
195 |
<< " local = " << itor->second.local << "\n" |
196 |
<< " remote = " << itor->second.remote << "\n" |
197 |
<< " }\n"; |
198 |
} |
199 |
|
200 |
cerr << "}\n"; |
201 |
} |
202 |
} |