/* ============================================================================ * 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$ #include "Mounter.hpp" #ifdef _FreeBSD_ #include #endif void Mounter::mount() { if (!good) return; samba(); if (debug) { cerr << "mounted = {\n"; for (set::iterator itor = mounted.begin(); itor != mounted.end(); itor++) { cerr << " " << *itor << "\n"; } cerr << "}\n"; } 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"; mount(itor->first, itor->second.local, itor->second.remote); } if (debug) cerr << "}\n"; } void Mounter::samba() { string path = config.root + "/" + host; DIR* dir = opendir(path.c_str()); dirent* file; if (dir == NULL) { string prefix = program + ": " + path; perror(prefix.c_str()); return; } while ((file = readdir(dir)) != NULL) { struct stat dir; string folder = path + "/" + file->d_name; stat(folder.c_str(), &dir); if (S_ISDIR(dir.st_mode)) { string folder = file->d_name; if (folder == "." || folder == "..") continue; string share = folder.find('.') == 0 ? folder.erase(0, 1) + "$" : folder; if (folders.find(share) == folders.end()) { folders.insert(pair(share, regular)); } } } closedir(dir); vector args; args.push_back("spectre"); args.push_back("-t"); args.push_back("smbfs"); ipstream mount(config.mount, args); if (debug) cerr << "mount = {\n"; samba(mount); if (debug) cerr << "}\n"; mount.close(); } void Mounter::samba(ipstream& pin) { while (pin.good()) { string line; getline(pin, line); if (debug) cerr << line << "\n"; if (line == "") continue; unsigned begin = line.find(" on ") + 4; #if defined _Linux_ unsigned end = line.rfind(" type smbfs"); #elif defined _FreeBSD_ unsigned end = line.rfind(" (smbfs)"); #endif string path = line.substr(begin, end - begin); if (mounted.find(path) == mounted.end()) { mounted.insert(path); } } } void Mounter::mount(const string& folder, const string& user, const string& owner) { string path = config.root + "/" + host + "/" + (folder[folder.length() - 1] == '$' ? "." + folder.substr(0, folder.length() - 1) : folder); if (mounted.find(path) != mounted.end()) return; struct passwd* item = getpwnam(user.c_str()); if (item == NULL) { cerr << program << ": " << user << ": Unknown user\n"; return; } ostringstream options; #if defined _Linux_ char credentials[17]; sprintf(credentials, "%s", "/tmp/spectre.XXXX"); mkstemp(credentials); ofstream fout(credentials); fout << "username = " << owner << "\n" << "password = " << password(owner) << "\n"; fout.close(); options << "credentials=" << credentials << "," << "uid=" << item->pw_uid << "," << "gid=" << item->pw_gid; string smb = "//" + host + "/" + folder; #elif defined _FreeBSD_ char* credentials = new char[strlen(getenv("HOME")) + 9]; sprintf(credentials, "%s/.nsmbrc", getenv("HOME")); int dot = open(credentials, O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); close(dot); ofstream fout(credentials); fout << "[" << toupper(host) << ":" << toupper(owner) << "]\n" << "password=" << password(owner) << "\n"; fout.close(); options << "-N," << "-u=" << item->pw_uid << "," << "-g=" << item->pw_gid; string smb = "//" + owner + "@" + host + "/" + folder; #endif vector args; args.push_back("spectre"); #ifdef _FreeBSD_ args.push_back("-o"); args.push_back(options.str()); #endif args.push_back("-t"); args.push_back("smbfs"); #ifdef _Linux_ args.push_back("-o"); args.push_back(options.str()); #endif args.push_back(smb); args.push_back(path); ipstream mount(config.mount, args, pstreambuf::pstderr); while (mount.good()) { string line; getline(mount, line); if (line != "") cerr << line << "\n"; } mount.close(); unlink(credentials); #ifdef _FreeBSD_ delete [] credentials; #endif }