diff options
Diffstat (limited to 'src/util/util.cpp')
| -rw-r--r-- | src/util/util.cpp | 416 |
1 files changed, 416 insertions, 0 deletions
diff --git a/src/util/util.cpp b/src/util/util.cpp new file mode 100644 index 0000000..c2d06a0 --- /dev/null +++ b/src/util/util.cpp @@ -0,0 +1,416 @@ +#include "util.h" +#include <QtGui/QDesktopServices> +#include "consoleLogger.h" +#include "TextFile.h" +#include <ctime> +#include <cstdlib> +#include <QStringList> +#include <iostream> + + +PVSServerEntry::PVSServerEntry(QString name) +{ + name_ = name; +} +void PVSServerEntry::addClient(QString client) +{ + clientList_.push_back(client); +} +bool PVSServerEntry::hasClient(QString client) +{ + for (std::list<QString>::iterator it = clientList_.begin(); it != clientList_.end(); it++) + { + if ((*it).compare(client) == 0) + return true; + } + return false; +} +QString PVSServerEntry::getName() +{ + return name_; +} + +int PVSServerEntry::getSize() +{ + return clientList_.size(); +} + + +//namespace util +//{ + +int getRandom(int min, int max) +{ + static bool init = true; + if (init) + { + init = false; + srand ( time(NULL) + getpid() ); + } + if (min >= max) return rand(); + return rand() % (max-min+1) + min; +} + +std::list<PVSServerEntry> getPVSServerEntryList(QString fileName) +{ + QString line; + TextFile file(fileName); + std::list<PVSServerEntry> entryList; + + if (file.good()) + { +// std::cout << "reading table file \"" << fileName <<"\"" << std::endl; + PVSServerEntry* tmpEntry = NULL; + while (!file.eof()) + { + line = file.readLine(); + + if (!(line.length() <=1)) // ignore blank + if (!(line[0] == '#' || line[0] == '/')) // ignore comments + { + if (line.mid(0, 5).compare(QString("start")) == 0) + { + if (tmpEntry != NULL) + { + // unclean file... but no reason to break down + //PVSServerEntry tmpEntry2 = *tmpEntry; + entryList.push_back(*tmpEntry); + delete tmpEntry; + tmpEntry = NULL; + } + QString tmpName(colonSplitter(line, false)); + if (tmpName.size()<1) + tmpName = QString("localhost"); + + tmpEntry = new PVSServerEntry(tmpName); + continue; + } + if (line.mid(0, 3).compare(QString("end")) == 0) + { + if (tmpEntry!=NULL) + { + entryList.push_back(*tmpEntry); + delete tmpEntry; + tmpEntry = NULL; + } + else + ;// ignore + + continue; + } + if (tmpEntry != NULL) + { + tmpEntry->addClient(line); + } + } + } + if (tmpEntry != NULL) + { + delete tmpEntry; + tmpEntry = NULL; + } + + return entryList; + } + else + { + std::cout << "ERROR: no file \"" << fileName.toStdString() <<"\" found or file corrupt" << std::endl; + } + return std::list<PVSServerEntry>(); +} +QString getFilenameFromPath(QString line) +{ + int lastSlash = 0; + for (int i = 0; i < line.length(); i++) + { + if (line.at(i) == '/') + { + lastSlash = i; + } + } + QString result; + if (lastSlash != 0 && lastSlash != line.length() -1) + { + result = line.mid(lastSlash+1, line.length()-(lastSlash+1)); + } + return result; +} + +QString lineSplitter(QString line, const char* signs, bool first) +{ + char *pch; + char* cLine = new char[line.length()+1]; + strcpy(cLine, line.toUtf8().data()); + pch = strtok (cLine,signs); + if (first) + { + QString tmp(pch); + delete cLine; + return tmp; + } + else + { + QString tmp; + char* tmpp = strtok(NULL, ";,\t\n"); + if (tmpp) + tmp = QString(tmpp); + delete cLine; + return tmp; + } +} + +QString colonSplitter(QString line, bool first) +{ + char *pch; + char* cLine = new char[line.length()+1]; + strcpy(cLine, line.toUtf8().data()); + pch = strtok (cLine," :\t\n"); + if (first) + { + QString tmp(pch); + delete[] cLine; + return tmp; + } + else + { + QString tmp; + char* tmpp = strtok(NULL, " ;,\t\n"); + if (tmpp) + tmp = QString(tmpp); + delete[] cLine; + return tmp; + } +} + +QString getUserName() +{ + struct passwd* passUser = getpwuid(getuid()); + QString username; + if (passUser) + { + username = QString(passUser->pw_name); + } + if (username.isEmpty()) + { + printf("USERNAME COULDNT BE RETRIEVED!\n"); + username = QString("USERNAMEERROR"); + } + return username; +} + +// Get full username. +QString getFullUsername() +{ + QString fullname = getUserName(); + struct passwd *pd; + + if (NULL == (pd = getpwuid(getuid()))) + {ConsoleLog writeError("getpwuid() error.");} + else + { + QString tmp = pd->pw_gecos; + QStringList userData = tmp.split(","); + if(userData[0].length() > 0 ) + { + fullname = userData[0]; + } + } + return fullname; +} + +bool fileExists(QString fileName) +{ + std::ifstream file(fileName.toLocal8Bit().data()); + if (file.good()) + { +#ifdef verbose + printf("fileExists(): file good.\n"); +#endif + file.close(); + return true; + } + return false; + +} + +QString getHomeDir() +{ + return QDesktopServices::storageLocation(QDesktopServices::HomeLocation); +} + +QString getPolicyDir() +{ + QString policyDir = getHomeDir(); + QString subPath("/.pvs/"); + policyDir.append(subPath); + return policyDir; +} + +QString getPolicyFilePath(QString fileName) +{ + + QString fullPath = getPolicyDir(); + fullPath.append(fileName); + return fullPath; +} +bool policyFileExists(QString fileName) +{ + std::ifstream file(getPolicyFilePath(fileName).toUtf8().data()); + if (file.good()) + { + file.close(); + return true; + } + return false; +} + +void createPolicyDir() +{ + mkdir(getPolicyDir().toUtf8().data(), 0777); +} + +void createPolicyFiles() +{ + if (!policyFileExists(QString(".allow"))) + { + std::ofstream file(getPolicyFilePath(QString(".allow")).toUtf8().data()); + file.close(); + } +#ifdef old_method + if (!policyFileExists(QString(".pass"))) + { + std::ofstream file(getPolicyFilePath(QString(".pass")).toUtf8().data()); + file.close(); + } +#endif +} + +QString readPassFromPassFile() +{ + TextFile file(getPolicyFilePath(".pass")); + if (file.good()) // should have been checked via exists before, but better be safe + { + QString pass; + pass = file.readLine(); // we expect a password in correct format. otherwise their fault + return pass; + } + else + return QString(); +} +bool getAllowed() +{ + printf("Checking %s\n", getPolicyFilePath(QString(".allow")).toUtf8().data()); + TextFile file(getPolicyFilePath(".allow")); + if (file.good()) // should have been checked via exists before, but better be safe + { + QString allowed; + allowed = file.readLine(); + if ( (allowed.compare(QString("1")) == 0) || + (allowed.compare(QString("true")) == 0) || + (allowed.compare(QString("t")) == 0) || + (allowed.compare(QString("T")) == 0) || + (allowed.compare(QString("true")) == 0) || + (allowed.compare(QString("allow")) == 0) || + (allowed.compare(QString("TRUE")) == 0) ) + return true; + } + printf("...negative\n"); + return false; +} + +QString int2String(int intInt) +{ + char tmp[50]; + snprintf(tmp, 49, "%d", intInt); + return QString(tmp); +} + +int string2Int(QString string) +{ + return atoi(string.toUtf8().data()); +} + +//}//end namespace util + + +#ifdef neverever +std::list<VNCConnectInfo*> readFile(char* fileName) +{ + QString line; + std::ifstream file(fileName); + std::list<VNCConnectInfo*> infoList; + + if (file.good()) + { +// std::cout << "reading clients file \"" << fileName <<"\"" << std::endl; + while (!file.eof()) + { + getline(file, line); + + if (!(line.length() <=1)) // ignore blank + if (!(line[0] == '#' || line[0] == '/')) // ignore comments + infoList.push_back(getConInfo(line)); + } + } + else + { + std::cout << "ERROR: no file \"" << fileName <<"\" found or file corrupt" << std::endl; + } + return infoList; +} + +VNCConnectInfo* getConInfo(QString line) +{ + char** arguments = new char*[100]; + char* cLine = new char[line.length()+1]; + strcpy(cLine, line.toUtf8().data()); + int count; + makeArgs(cLine , &count , arguments); + std::cout << "found " << count << " arguments" << std::endl; + if (count > 2) // assume that a password would be the second argument in the line, therefore the third argument in the argv + { + QString pass = QString(arguments[2]); + std::cout << "think i found a password" << std::endl; + count--; + delete arguments[2]; + // no changes to the arguments though, since the vnc-lib will ignore most of it anyway + return new VNCConnectInfo(count, arguments, pass); + } + + return new VNCConnectInfo(count, arguments); +} + +void makeArgs(char* line, int* count, char** arguments) +{ + (*count) = 1; + + arguments[(*count)-1] = new char[strlen("dummy")+1]; // get program name from somewhere + strcpy(arguments[(*count)-1], "dummy"); + + /*// include encodings + arguments[(*count)++] = new char[strlen("-encodings")+1]; + strcpy(arguments[(*count)], "-encodings"); + (*count)++; + arguments[(*count)++] = new char[strlen("tight")+1]; + strcpy(arguments[(*count)], "tight"); + (*count)++; + */ + + + if (line[strlen(line)-1] == '\n' || line[strlen(line)-1] == '\r') + line[strlen(line)-1] = '\0'; // remove newline + + char *pch; + pch = strtok (line," ,\t"); + while (pch != NULL && (*count) <= 3) + { + (*count)++; // count args + arguments[(*count)-1] = new char[strlen(pch)+1]; + strcpy(arguments[(*count)-1], pch); + pch = strtok (NULL, " ,\t"); + } +} + + +#endif + |
