// Spectre Samba Mounter // // Douglas Thrift // // Maker.cpp #include "Maker.h" void Maker::make() { 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()); } } for (map::iterator itor = folders.begin(); itor != folders.end(); itor++) { make(itor->first, itor->second.local); } } 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 (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"; } 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; // } } string line; getline(pin, line); if (debug) cerr << line << "\n"; if (line != "") { cerr << program << ": Error returning browse list\n"; } } void Maker::strip(char* name) { for (unsigned index = strlen(name); index > 0; index--) { if (name[index - 1] == ' ') { name[index - 1] = '\0'; } else { break; } } } 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()); } }