ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/proj/trunk/Spectre/Maker.cpp
Revision: 227
Committed: 2003-07-30T17:56:29-07:00 (21 years, 10 months ago) by douglas
File size: 3616 byte(s)
Log Message:
Thought about the meaning of life, added $Id$ tags.

File Contents

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