/* ============================================================================ * Spectre Samba Mounter * * Copyright © 2003-2004, Douglas Thrift. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * 3. The end-user documentation included with the redistribution, if any, must * include the following acknowledgment: * * "This product includes software developed by Douglas Thrift * (http://computers.douglasthrift.net/spectre.xml)." * * Alternately, this acknowledgment may appear in the software itself, if * and wherever such third-party acknowledgments normally appear. * * 4. The names "Douglas Thrift" and "Spectre Samba Mounter" must not be used * to endorse or promote products derived from this software without * specific prior written permission. For written permission, please visit * http://www.douglasthrift.net/contact.cgi for contact information. * * 5. Products derived from this software may not be called "Spectre Samba * Mounter", nor may "Spectre Samba Mounter" appear in their name, without * prior written permission. * * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ============================================================================ */ // Spectre Samba Mounter // // Douglas Thrift // // $Id: Maker.cpp,v 1.11 2004/03/23 04:22:17 douglas Exp $ #include "Maker.hpp" void Maker::make() { if (!good) return; samba(); struct stat dir; if (stat(config.root.c_str(), &dir) != 0) { if (mkdir(config.root.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0) { string prefix = program + ": " + config.root; perror(prefix.c_str()); } } string path = config.root + "/" + host; if (stat(path.c_str(), &dir) != 0) { if (mkdir(path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0) { string prefix = program + ": " + path; perror(prefix.c_str()); } } if (debug) cerr << "folders = {\n"; for (map::iterator itor = folders.begin(); itor != folders.end(); itor++) { if (debug) cerr << " " << itor->first << " = {\n" << " local = " << itor->second.local << "\n" << " remote = " << itor->second.remote << "\n" << " }\n"; make(itor->first, itor->second.local); } if (debug) cerr << "}\n"; } void Maker::samba() { char credentials[17]; sprintf(credentials, "%s", "/tmp/spectre.XXXX"); mkstemp(credentials); ofstream fout(credentials); fout << "username = " << regular.remote << "\n" << "password = " << password(regular.remote) << "\n"; fout.close(); vector args; args.push_back("spectre"); args.push_back("-N"); args.push_back("-L"); args.push_back(host); args.push_back("-A"); args.push_back(credentials); ipstream smbclient(config.smbclient, args); if (debug) cerr << "smbclient = {\n"; if (connect(smbclient)) { for (unsigned index = 0; index < 2; index++) { string line; getline(smbclient, line); if (debug) cerr << line << "\n"; } samba(smbclient); } else { cerr << program << ": Connection to " << host << " failed\n"; } if (debug) cerr << "}\n"; smbclient.close(); unlink(credentials); } bool Maker::connect(ipstream& pin) { while (pin.good()) { string line; getline(pin, line); if (debug) cerr << line << "\n"; if (line == "") { return true; } else if (line == "Connection to " + host + " failed") { return false; } } return false; } void Maker::samba(ipstream& pin) { while (pin.good()) { if (pin.peek() != '\t') break; pin.ignore(); char share[16]; char type[11]; string comment; pin.get(share, 16); pin.get(type, 11); getline(pin, comment); if (debug) cerr << "\t" << share << type << comment << "\n"; strip(share); strip(type); if (string(type) == "Disk") { unsigned end = strlen(share) - 1; if (share[end] == '$') continue; if (folders.find(share) == folders.end()) { folders.insert(pair(share, regular)); } } } string line; getline(pin, line); if (debug) cerr << line << "\n"; if (line != "") { cerr << program << ": Error returning browse list\n"; } } void Maker::make(const string& folder, const string& user) { struct stat dir; string path = config.root + "/" + host + "/" + folder; if (stat(path.c_str(), &dir) != 0) { if (mkdir(path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0) { string prefix = program + ": " + path; perror(prefix.c_str()); } } struct passwd* item = getpwnam(user.c_str()); if (item == NULL) { cerr << program << ": " << user << ": Unknown user\n"; return; } if (dir.st_uid == item->pw_uid && dir.st_gid == item->pw_gid) return; if (chown(path.c_str(), item->pw_uid, item->pw_gid) != 0) { string prefix = program + ": " + path; perror(prefix.c_str()); } }