From 264cd7947d7291f78065f12824523ba6178a9936 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Fri, 22 May 2009 00:23:59 +0200 Subject: GroupListRequest hinzugef?gt --- src/Client/CommandManager.cpp | 26 ++++++++ src/Client/CommandManager.h | 1 + src/Client/CommandParser.cpp | 9 +++ src/Client/CommandParser.h | 1 + src/Common/GroupInfo.h | 46 ++++++++++++++ src/Common/Requests/GroupListRequest.h | 39 ++++++++++++ src/Common/UserInfo.h | 4 +- src/Server/ConnectionManager.cpp | 5 +- src/Server/RequestHandlers/CMakeLists.txt | 2 +- .../RequestHandlers/GroupListRequestHandler.cpp | 73 ++++++++++++++++++++++ .../RequestHandlers/GroupListRequestHandler.h | 42 +++++++++++++ src/Server/UserBackend.h | 5 ++ src/Server/UserManager.cpp | 10 +++ src/Server/UserManager.h | 3 + src/mad-server.conf | 2 +- src/modules/UserBackendMysql/UserBackendMysql.cpp | 22 +++++++ src/modules/UserBackendMysql/UserBackendMysql.h | 2 + 17 files changed, 287 insertions(+), 5 deletions(-) create mode 100644 src/Common/GroupInfo.h create mode 100644 src/Common/Requests/GroupListRequest.h create mode 100644 src/Server/RequestHandlers/GroupListRequestHandler.cpp create mode 100644 src/Server/RequestHandlers/GroupListRequestHandler.h diff --git a/src/Client/CommandManager.cpp b/src/Client/CommandManager.cpp index f46bdcd..b427db7 100644 --- a/src/Client/CommandManager.cpp +++ b/src/Client/CommandManager.cpp @@ -251,5 +251,31 @@ void CommandManager::userListRequestFinished(const Common::Request &request) { requestFinished(); } +void CommandManager::groupListRequestFinished(const Common::Request &request) { + try { + const Common::XmlPacket &packet = request.getResult(); + + const Common::XmlPacket::Element &groups = packet["groups"]; + + if(groups.isEmpty()) { + std::cout << "Group list is empty." << std::endl; + } + else { + std::cout << "Found " << groups.getSize() << " groups:" << std::endl; + + + for(size_t i = 0; i < groups.getSize(); ++i) + std::cout << " " << (unsigned long)groups[i]["gid"] << ", " << (const std::string&)groups[i]["name"] << std::endl; + } + + std::cout << std::endl; + } + catch(Net::Exception &exception) { + Common::Logger::logf(Common::Logger::ERROR, "An error occurred during your request: %s.", exception.strerror().c_str()); + } + + requestFinished(); +} + } } diff --git a/src/Client/CommandManager.h b/src/Client/CommandManager.h index 7635542..3cfc502 100644 --- a/src/Client/CommandManager.h +++ b/src/Client/CommandManager.h @@ -60,6 +60,7 @@ class CommandManager { void statusRequestFinished(const Common::Request &request); void userInfoRequestFinished(const Common::Request &request); void userListRequestFinished(const Common::Request &request); + void groupListRequestFinished(const Common::Request &request); CommandManager() : activeRequests(0), disconnect(false) {} diff --git a/src/Client/CommandParser.cpp b/src/Client/CommandParser.cpp index 78b3570..044a64c 100644 --- a/src/Client/CommandParser.cpp +++ b/src/Client/CommandParser.cpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include @@ -48,6 +49,7 @@ const CommandParser::Command CommandParser::commands[] = { {{"status", "st", 0}, "status [host]", "Display status information", "Display host status information. If no host is given, display server status information.", &CommandParser::statusCommand}, {{"user_info", "user", 0}, "user_info uid", "Search for a user id", "Search for a user id.", &CommandParser::userInfoCommand}, {{"list_users", "users", 0}, "list_users", "Show the user account database", "Show the user account database.", &CommandParser::listUsersCommand}, + {{"list_groups", "groups", 0}, "list_groups", "Show the user group database", "Show the user group database.", &CommandParser::listGroupsCommand}, {{"exit", "quit", 0}, "exit", "Close the connection and quit the client", "Closes the connection and quits the client.", &CommandParser::exitCommand}, {{0}, 0, 0, 0, 0} }; @@ -290,6 +292,13 @@ void CommandParser::listUsersCommand(const std::vector&) { boost::bind(&CommandManager::userListRequestFinished, CommandManager::get(), _1)); } +void CommandParser::listGroupsCommand(const std::vector&) { + ++CommandManager::get()->activeRequests; + + Common::RequestManager::get()->sendRequest(connection, + boost::bind(&CommandManager::groupListRequestFinished, CommandManager::get(), _1)); +} + void CommandParser::exitCommand(const std::vector&) { ++CommandManager::get()->activeRequests; diff --git a/src/Client/CommandParser.h b/src/Client/CommandParser.h index 4b44656..727181e 100644 --- a/src/Client/CommandParser.h +++ b/src/Client/CommandParser.h @@ -65,6 +65,7 @@ class CommandParser { void statusCommand(const std::vector &args); void userInfoCommand(const std::vector &args); void listUsersCommand(const std::vector &args); + void listGroupsCommand(const std::vector &args); void exitCommand(const std::vector&); CommandParser() : connection(0) {} diff --git a/src/Common/GroupInfo.h b/src/Common/GroupInfo.h new file mode 100644 index 0000000..e32648e --- /dev/null +++ b/src/Common/GroupInfo.h @@ -0,0 +1,46 @@ +/* + * GroupInfo.h + * + * Copyright (C) 2009 Matthias Schiffer + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef MAD_COMMON_GROUPINFO_H_ +#define MAD_COMMON_GROUPINFO_H_ + +#include + +namespace Mad { +namespace Common { + +class GroupInfo { + private: + unsigned long gid; + std::string name; + + public: + GroupInfo(unsigned long gid0 = 0, const std::string& name0 = std::string()) : gid(gid0), name(name0) {} + + void setGid(unsigned long newGid) {gid = newGid;} + unsigned long getGid() const {return gid;} + + void setName(const std::string& newName) {name = newName;} + const std::string& getName() const {return name;} +}; + +} +} + +#endif /* MAD_COMMON_GROUPINFO_H_ */ diff --git a/src/Common/Requests/GroupListRequest.h b/src/Common/Requests/GroupListRequest.h new file mode 100644 index 0000000..86322ad --- /dev/null +++ b/src/Common/Requests/GroupListRequest.h @@ -0,0 +1,39 @@ +/* + * UserListRequest.h + * + * Copyright (C) 2009 Matthias Schiffer + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef MAD_COMMON_REQUESTS_GROUPLISTREQUEST_H_ +#define MAD_COMMON_REQUESTS_GROUPLISTREQUEST_H_ + +#include "SimpleRequest.h" + +namespace Mad { +namespace Common { +namespace Requests { + +class GroupListRequest : public SimpleRequest { + public: + GroupListRequest(Connection *connection, uint16_t requestId, slot_type slot) + : SimpleRequest(connection, requestId, slot, "ListGroups") {} +}; + +} +} +} + +#endif /* MAD_COMMON_REQUESTS_GROUPLISTREQUEST_H_ */ diff --git a/src/Common/UserInfo.h b/src/Common/UserInfo.h index 94c0c60..b8897e3 100644 --- a/src/Common/UserInfo.h +++ b/src/Common/UserInfo.h @@ -39,8 +39,8 @@ class UserInfo { void setUid(unsigned long newUid) {uid = newUid;} unsigned long getUid() const {return uid;} - void setGid(unsigned long newUid) {uid = newUid;} - unsigned long getGid() const {return uid;} + void setGid(unsigned long newGid) {gid = newGid;} + unsigned long getGid() const {return gid;} void setUsername(const std::string& newUsername) {username = newUsername;} const std::string& getUsername() const {return username;} diff --git a/src/Server/ConnectionManager.cpp b/src/Server/ConnectionManager.cpp index abb66b0..3065c05 100644 --- a/src/Server/ConnectionManager.cpp +++ b/src/Server/ConnectionManager.cpp @@ -28,7 +28,8 @@ #include "RequestHandlers/DaemonFSInfoRequestHandler.h" #include "RequestHandlers/DaemonListRequestHandler.h" #include "RequestHandlers/DaemonStatusRequestHandler.h" -#include "RequestHandlers/GSSAPIAuthRequestHandler.h" +//#include "RequestHandlers/GSSAPIAuthRequestHandler.h" +#include "RequestHandlers/GroupListRequestHandler.h" #include "RequestHandlers/IdentifyRequestHandler.h" #include "RequestHandlers/LogRequestHandler.h" #include "RequestHandlers/UserInfoRequestHandler.h" @@ -194,6 +195,7 @@ void ConnectionManager::doInit() { Common::RequestManager::get()->registerPacketType("ListHosts"); Common::RequestManager::get()->registerPacketType("GetUserInfo"); Common::RequestManager::get()->registerPacketType("ListUsers"); + Common::RequestManager::get()->registerPacketType("ListGroups"); Common::RequestManager::get()->registerPacketType("Log"); } @@ -210,6 +212,7 @@ void ConnectionManager::doDeinit() { Common::RequestManager::get()->unregisterPacketType("ListHosts"); Common::RequestManager::get()->unregisterPacketType("GetUserInfo"); Common::RequestManager::get()->unregisterPacketType("ListUsers"); + Common::RequestManager::get()->unregisterPacketType("ListGroups"); Common::RequestManager::get()->unregisterPacketType("Log"); } diff --git a/src/Server/RequestHandlers/CMakeLists.txt b/src/Server/RequestHandlers/CMakeLists.txt index e340a1e..9215348 100644 --- a/src/Server/RequestHandlers/CMakeLists.txt +++ b/src/Server/RequestHandlers/CMakeLists.txt @@ -3,7 +3,7 @@ include_directories(${INCLUDES}) add_library(ServerRequestHandlers DaemonCommandRequestHandler.cpp DaemonFSInfoRequestHandler.cpp DaemonListRequestHandler.cpp DaemonStatusRequestHandler.cpp - IdentifyRequestHandler.cpp + GroupListRequestHandler.cpp IdentifyRequestHandler.cpp LogRequestHandler.cpp UserInfoRequestHandler.cpp UserListRequestHandler.cpp ) diff --git a/src/Server/RequestHandlers/GroupListRequestHandler.cpp b/src/Server/RequestHandlers/GroupListRequestHandler.cpp new file mode 100644 index 0000000..1ca2b92 --- /dev/null +++ b/src/Server/RequestHandlers/GroupListRequestHandler.cpp @@ -0,0 +1,73 @@ +/* + * GroupListRequestHandler.cpp + * + * Copyright (C) 2009 Matthias Schiffer + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#include "GroupListRequestHandler.h" +#include "../UserManager.h" +#include +#include + +namespace Mad { +namespace Server { +namespace RequestHandlers { + +void GroupListRequestHandler::handlePacket(const Common::XmlPacket &packet) { + if(packet.getType() != "ListGroups") { + Common::Logger::log(Common::Logger::ERROR, "Received an unexpected packet."); + + Common::XmlPacket ret; + ret.setType("Error"); + ret.add("ErrorCode", Net::Exception::UNEXPECTED_PACKET); + + sendPacket(ret); + + signalFinished()(); + return; + } + + // TODO Require authentication + + boost::shared_ptr > info = UserManager::get()->getGroupList(); + + Common::XmlPacket ret; + + if(info) { + ret.setType("OK"); + ret.addList("groups"); + + for(std::map::iterator group = info->begin(); group != info->end(); ++group) { + ret["groups"].addEntry(); + Common::XmlPacket::Entry &entry = ret["groups"].back(); + + entry.add("gid", group->second.getGid()); + entry.add("name", group->second.getName()); + } + } + + else { + ret.setType("Error"); + ret.add("ErrorCode", Net::Exception::NOT_IMPLEMENTED); + } + + sendPacket(ret); + signalFinished()(); +} + +} +} +} diff --git a/src/Server/RequestHandlers/GroupListRequestHandler.h b/src/Server/RequestHandlers/GroupListRequestHandler.h new file mode 100644 index 0000000..d7d6a9c --- /dev/null +++ b/src/Server/RequestHandlers/GroupListRequestHandler.h @@ -0,0 +1,42 @@ +/* + * GroupListRequestHandler.h + * + * Copyright (C) 2009 Matthias Schiffer + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#ifndef MAD_SERVER_REQUESTHANDLERS_GROUPLISTREQUESTHANDLER_H_ +#define MAD_SERVER_REQUESTHANDLERS_GROUPLISTREQUESTHANDLER_H_ + +#include + +namespace Mad { +namespace Server { +namespace RequestHandlers { + +class GroupListRequestHandler : public Common::RequestHandler { + protected: + virtual void handlePacket(const Common::XmlPacket &packet); + + public: + GroupListRequestHandler(Common::Connection *connection, boost::uint16_t requestId) + : RequestHandler(connection, requestId) {} +}; + +} +} +} + +#endif /* MAD_SERVER_REQUESTHANDLERS_GROUPLISTREQUESTHANDLER_H_ */ diff --git a/src/Server/UserBackend.h b/src/Server/UserBackend.h index a673fa9..6d59615 100644 --- a/src/Server/UserBackend.h +++ b/src/Server/UserBackend.h @@ -23,6 +23,7 @@ #include #include +#include #include #include @@ -49,6 +50,10 @@ class UserBackend { return boost::shared_ptr(); } + virtual boost::shared_ptr > getGroupList() { + return boost::shared_ptr >(); + } + // TODO Better interface... virtual bool setPassword(unsigned long uid _UNUSED_PARAMETER_, const std::string &password _UNUSED_PARAMETER_) { return false; diff --git a/src/Server/UserManager.cpp b/src/Server/UserManager.cpp index 3b5887a..e32cb57 100644 --- a/src/Server/UserManager.cpp +++ b/src/Server/UserManager.cpp @@ -54,6 +54,16 @@ boost::shared_ptr UserManager::getUserInfo(unsigned long uid) return boost::shared_ptr(); } +boost::shared_ptr > UserManager::getGroupList() { + for(std::set >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { + boost::shared_ptr > ret = (*backend)->getGroupList(); + if(ret) + return ret; + } + + return boost::shared_ptr >(); +} + bool UserManager::setPassword(unsigned long uid, const std::string &password) { for(std::set >::iterator backend = backends.begin(); backend != backends.end(); ++backend) { if((*backend)->setPassword(uid, password)) diff --git a/src/Server/UserManager.h b/src/Server/UserManager.h index c3d990d..e33b678 100644 --- a/src/Server/UserManager.h +++ b/src/Server/UserManager.h @@ -21,6 +21,7 @@ #define MAD_SERVER_USERMANAGER_H_ #include +#include #include #include @@ -57,6 +58,8 @@ class UserManager : boost::noncopyable { boost::shared_ptr > getUserList(); boost::shared_ptr getUserInfo(unsigned long uid); + boost::shared_ptr > getGroupList(); + bool setPassword(unsigned long uid, const std::string &password); bool addUser(const Common::UserInfo &userInfo); diff --git a/src/mad-server.conf b/src/mad-server.conf index 45927cf..188833b 100644 --- a/src/mad-server.conf +++ b/src/mad-server.conf @@ -17,7 +17,7 @@ UserBackendMysql { Queries { ListUsers "SELECT id, gid, username, fullname FROM users" - #ListGroups + ListGroups "SELECT id, name FROM groups" #ListUserGroups #ListGroupUsers UserById "SELECT id, gid, username, fullname FROM users WHERE id = {ID}" diff --git a/src/modules/UserBackendMysql/UserBackendMysql.cpp b/src/modules/UserBackendMysql/UserBackendMysql.cpp index a8751ee..139a416 100644 --- a/src/modules/UserBackendMysql/UserBackendMysql.cpp +++ b/src/modules/UserBackendMysql/UserBackendMysql.cpp @@ -193,6 +193,28 @@ boost::shared_ptr UserBackendMysql::getUserInfo(unsigned long return boost::shared_ptr(); } +boost::shared_ptr > UserBackendMysql::getGroupList() { + Net::ThreadManager::get()->detach(); + + mysql_ping(mysql); + + mysql_real_query(mysql, queryListGroups.c_str(), queryListGroups.length()); + MYSQL_RES *result = mysql_use_result(mysql); + + if(mysql_num_fields(result) < 2) + return boost::shared_ptr >(); // TODO Error + + boost::shared_ptr > groups(new std::map()); + + while(MYSQL_ROW row = mysql_fetch_row(result)) { + Common::GroupInfo group(strtoul(row[0], 0, 10), row[1]); + + groups->insert(std::make_pair(group.getGid(), group)); + } + + return groups; +} + void UserBackendMysql::registerBackend() { if(backend) diff --git a/src/modules/UserBackendMysql/UserBackendMysql.h b/src/modules/UserBackendMysql/UserBackendMysql.h index de28069..8a75a6b 100644 --- a/src/modules/UserBackendMysql/UserBackendMysql.h +++ b/src/modules/UserBackendMysql/UserBackendMysql.h @@ -53,6 +53,8 @@ class UserBackendMysql : public Server::UserBackend, private Common::Configurabl virtual boost::shared_ptr > getUserList(); virtual boost::shared_ptr getUserInfo(unsigned long uid); + virtual boost::shared_ptr > getGroupList(); + public: virtual ~UserBackendMysql() { if(mysql) { -- cgit v1.2.3