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