// Spectre Samba Mounter // // Douglas Thrift // // Spectre.cpp #include "Spectre.h" #include "Maker.h" #include "Mounter.h" string program; string programName = "Spectre Samba Mounter"; string programVersion = "1.0beta"; bool debug = false; Config config; int spectre(vector& args); void automake(set& makes); void automake(set& makes, ipstream& pin); void automount(set& mounts); void configure(); int main(int argc, char* argv[]) { vector args(argc); for (int index = 0; index < argc; index++) { args[index] = argv[index]; } return spectre(args); } int spectre(vector& args) { program = args[0]; #include "configure.h" bool make = false; bool mount = false; bool automake = false; bool automount = false; set makes; set mounts; for (int index = 1; index < args.size(); index++) { if (args[index] == "-help") { usage(); return 0; } else if (args[index] == "-version") { version(); return 0; } else if (args[index] == "-make") { if (!make) make = true; if (++index < args.size()) { makes.insert(args[index]); } else { cerr << program << ": Bad arguments\n"; usage(); return 1; } } else if (args[index] == "-mount") { if (!mount) mount = true; if (++index < args.size()) { mounts.insert(args[index]); } else { cerr << program << ": Bad arguments\n"; usage(); return 1; } } else if (args[index] == "-automake") { if (!automake) automake = true; } else if (args[index] == "-automount") { if (!automount) automount = true; } else if (args[index] == "-D") { if (!debug) { debug = true; cerr.setf(ios_base::boolalpha); } } } if (!make && !mount && !automake && !automount) { usage(); return 0; } if (debug) { cerr << "make = " << make << "\n" << (make ? "makes = {\n" : ""); for (set::iterator itor = makes.begin(); itor != makes.end(); itor++) { cerr << " " << *itor << "\n"; } cerr << (make ? "}\n" : "") << "mount = " << mount << "\n" << (mount ? "mounts = {\n" : ""); for (set::iterator itor = mounts.begin(); itor != mounts.end(); itor++) { cerr << " " << *itor << "\n"; } cerr << (mount ? "}\n" : "") << "automake = " << automake << "\n" << "automount = " << automount << "\n" << "config.install = " << config.install << "\n"; } configure(); if (debug) { cerr << "config.findsmb = " << config.findsmb << "\n" << "config.smbclient = " << config.smbclient << "\n" << "config.mount = " << config.mount << "\n" << "config.root = " << config.root << "\n" << (!config.hosts.empty() ? "config.hosts = {\n" : ""); for (multimap::iterator itor = config.hosts.begin(); itor != config.hosts.end(); itor++) { cerr << " " << itor->first << " = " << itor->second << "\n"; } cerr << (!config.hosts.empty() ? "}\n" : ""); } if (automake) ::automake(makes); if (make || automake) { for (set::iterator itor = makes.begin(); itor != makes.end(); itor++) { Maker maker(*itor); maker.make(); } } if (automount) ::automount(mounts); if (mount || automount) { for (set::iterator itor = mounts.begin(); itor != mounts.end(); itor++) { Mounter mounter(*itor); mounter.mount(); } } return 0; } string platform() { utsname* computer = new utsname; uname(computer); string os = computer->sysname; string version = computer->release; string architecture = computer->machine; delete computer; string platform = "(" + os + " " + version + " " + architecture + ")"; return platform; } void usage() { string tab(8 + program.length(), ' '); cout << "Usage: " << program << " [-make host ...] [-mount host ...]\n" << tab << "[-automake] [-automount]\n" << tab << "[-D] [-version] [-help]\n" << "Options:\n" << " -make host Make the mount tree for host\n" << " -mount host Mount the shares on host to its tree\n" << " -automake Automagically make the mount tree\n" << " -automount Automagically mount shares to the tree\n" << " -D Display debug information\n" << " -version Display version information and exit\n" << " -help Display this message and exit\n"; } void version() { cout << programName << " " << programVersion << " "<< platform() << "\n\n" << " Copyright (C) 2003, Douglas Thrift. All Rights Reserved.\n\n" << " This product includes software developed by Douglas Thrift\n" << " (http://computers.douglasthrift.net/).\n"; } void automake(set& makes) { vector args; args.push_back("spectre"); ipstream findsmb(config.findsmb, args); if (debug) cerr << "findsmb = {\n"; for (unsigned index = 0; index < 5; index++) { string line; getline(findsmb, line); if (debug) cerr << line << "\n"; } automake(makes, findsmb); if (debug) cerr << "}\n"; findsmb.close(); if (debug) { cerr << "makes = {\n"; for (set::iterator itor = makes.begin(); itor != makes.end(); itor++) { cerr << " " << *itor << "\n"; } cerr << "}\n"; } } void automake(set& makes, ipstream& pin) { while (pin.good()) { if (!isdigit(pin.peek())) break; char ip[17]; char host[16]; string info; pin.get(ip, 17); pin.ignore(); pin.get(host, 16); getline(pin, info); if (debug) cerr << ip << "\t" << host << info << "\n"; strip(host); makes.insert(tolower(host)); } string line; getline(pin, line); if (debug) cerr << line << "\n"; if (line != "") { cerr << program << ": Unknown error\n"; } } void automount(set& mounts) { vector args; args.push_back("spectre"); ipstream findsmb(config.findsmb, args); if (debug) cerr << "findsmb = {\n"; for (unsigned index = 0; index < 5; index++) { string line; getline(findsmb, line); if (debug) cerr << line << "\n"; } set hosts; automake(hosts, findsmb); if (debug) cerr << "}\n"; findsmb.close(); DIR* dir = opendir(config.root.c_str()); dirent* file; while((file = readdir(dir)) != NULL) { struct stat dir; string folder = config.root + "/" + file->d_name; stat(folder.c_str(), &dir); if (S_ISDIR(dir.st_mode)) { string folder = file->d_name; if (folder == "." || folder == ".." || hosts.find(folder) == hosts.end()) continue; mounts.insert(folder); } } closedir(dir); if (debug) { cerr << "mounts = {\n"; for (set::iterator itor = mounts.begin(); itor != mounts.end(); itor++) { cerr << " " << *itor << "\n"; } cerr << "}\n"; } } void configure() { string conf = config.install + "/conf/spectre.conf"; ifstream fin(conf.c_str()); if (!fin.is_open()) { cerr << program << ": Could not open " << conf << "\n"; exit(1); } do { string line; getline(fin, line); if (line.find('#') == 0 || line == "") { // ignore } else if (line.find("findsmb=") == 0) { config.findsmb = line.substr(8); } else if (line.find("smbclient=") == 0) { config.smbclient = line.substr(10); } else if (line.find("mount=") == 0) { config.mount = line.substr(6); } else if (line.find("root=") == 0) { config.root = line.substr(5); } else if (line.find("host=") == 0) { string host = line.substr(5); getline(fin, line); if (line != "{") { cerr << program << ": Munged configuration for " << host << "\n"; exit(1); } do { getline(fin, line); if (line.find("\t#") == 0) { // ignore } else if (line.find('\t') == 0) { config.hosts.insert(pair(host, line.substr(1))); } else if (line == "}") { break; } } while (fin.good()); } } while (fin.good()); fin.close(); }