From: Thomas Jarosch Date: Mon, 7 Apr 2008 12:45:32 +0000 (+0000) Subject: libi2ncommon: (tomj) Added missing files X-Git-Tag: v2.6~192 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=a85f476127ec8f649a3dd59e307d470eafeaecc5;p=libi2ncommon libi2ncommon: (tomj) Added missing files --- diff --git a/src/userfunc.cpp b/src/userfunc.cpp new file mode 100644 index 0000000..d6345da --- /dev/null +++ b/src/userfunc.cpp @@ -0,0 +1,359 @@ +/** @file + * + * (c) Copyright 2007-2008 by Intra2net AG + * + * info@intra2net.com + */ + +#include "userfunc.hxx" + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +namespace i2n { + +namespace { + +/* +** tool functions +*/ + +bool copyData( struct passwd* from, User& to ) +{ + to.clear(); + if (!from) return false; + to.name = from->pw_name; + to.password = from->pw_passwd; + to.uid = from->pw_uid; + to.gid = from->pw_gid; + to.gecos = from->pw_gecos; + to.homedir = from->pw_dir; + to.shell = from->pw_shell; + return true; +} // eo copyData(struct passwd*,Passwd&) + + +bool copyData( struct group* from , Group& to) +{ + to.clear(); + if (!from) return false; + to.name = from->gr_name; + to.password = from->gr_passwd; + to.gid = from->gr_gid; + to.members.clear(); + for (char **ptr= from->gr_mem; + ptr && *ptr; + ++ptr) + { + to.members.push_back( *ptr ); + } + return true; +} // eo copyData(struct group*,Group&) + + +} // eo namespace + + +/*************************************************************************\ +\*************************************************************************/ + +/* +** implementation of User +*/ + +User::User() +: uid(-1) +, gid(-1) +{ +} // User::User() + + +/** + * @brief constructs and fills with user data. + * @param name the name of the user to search for. + */ +User::User(const std::string& name) +{ + getUser(name,*this); +} // eo User::User(const std::string&) + + +/** + * @brief constructs and fills with user data. + * @param name the name of the user to search for. + */ +User::User(const char* name) +{ + getUser(std::string(name),*this); +} // eo User::User(const char*) + + +/** + * @brief constructs and fills with user data. + * @param uid the uid of the user to search for. + */ +User::User(uid_t uid) +{ + getUser(uid,*this); + this->uid = uid; +} // eo User::User(uid_t) + + + +/** + * @brief clears the data. + */ +void User::clear() +{ + name.clear(); + password.clear(); + uid= -1; + gid= -1; + gecos.clear(); + homedir.clear(); + shell.clear(); +} // eo User::clear() + + +/** + * @brief returns if the structure content looks ok. + * @return @a true if the content looks ok. + * + * This functions checks if mandatory values are existing. + * @note It does not check if content is consistent with any system database! + */ +bool User::isValid() const +{ + return (uid >= 0) and (gid >= 0) + and not name.empty() + ; +} // eo User::isValid() const + + +/* +** implementation of Group +*/ + + +Group::Group() +: gid(-1) +{ +} // eo Group::Group() + + +/** + * @brief constructs and fills with group data. + * @param name the name of the group to search for. + */ +Group::Group(const std::string& name) +{ + getGroup(name,*this); +} // eo group::Group(const std::strring&) + + +/** + * @brief constructs and fills with group data. + * @param name the name of the group to search for. + */ +Group::Group(const char* name) +{ + getGroup(std::string(name),*this); +} // eo group::Group(const char*) + + +/** + * @brief constructs and fills with group data. + * @param gid the gid of the group to search for. + */ +Group::Group(gid_t gid) +{ + getGroup(gid,*this); + this->gid= gid; +} // eo Group::Group(gid_t); + + +/** + * @brief clears the data. + */ +void Group::clear() +{ + name.clear(); + password.clear(); + gid= -1; + members.clear(); +} // eo Group::clear() + + +/** + * @brief returns if the structure content looks ok. + * @return @a true if the content looks ok. + * + * This functions checks if mandatory values are existing. + * @note It does not check if content is consistent with any system database! + */ +bool Group::isValid() const +{ + return (gid >= 0) and not name.empty(); +} // eo Group::isValid() const + +/** + * @brief get the (system) user data by name. + * @param[in] name the name of the user to search for. + * @param[out] result the user info. + * @return @a true iff the user was found and the result structure contains data. + */ +bool getUser(const std::string& name, User& result) +{ + struct passwd pw; + struct passwd *pw_ptr= NULL; + size_t buffer_size= sysconf(_SC_GETPW_R_SIZE_MAX); + boost::scoped_array< char > buffer( new char[ buffer_size ] ); + + int res= ::getpwnam_r( name.c_str(), &pw, buffer.get(), buffer_size, &pw_ptr); + + if (not (0 == res) or not pw_ptr) + { + return false; + } + + return copyData(pw_ptr, result); +} // eo getUser(const std::string&,User&) + + +/** + * @brief get the (system) user data by uid. + * @param[in] uid the uid of the user to search for. + * @param[out] result the user info. + * @return @a true iff the user was found and the result structure contains data. + */ +bool getUser(uid_t uid, User& result) +{ + struct passwd pw; + struct passwd *pw_ptr= NULL; + size_t buffer_size= sysconf(_SC_GETPW_R_SIZE_MAX); + boost::scoped_array< char > buffer( new char[ buffer_size ] ); + + int res= ::getpwuid_r( uid, &pw, buffer.get(), buffer_size, &pw_ptr); + + if (not (0 == res) or not pw_ptr) + { + return false; + } + + return copyData(pw_ptr, result); +} // eo getUser(uid_t,User&) + + +/** + * @brief get user data by name. + * @param name name of the user + * @return the user data (invalid if user not found or on error). + */ +User getUser(const std::string& name) +{ + User result; + getUser(name,result); + return result; +} // eo getUser(const std::string&) + + +/** + * @brief get user data by uid. + * @param uid uid of the user + * @return the user data (invalid if user not found or on error). + */ +User getUser(uid_t uid) +{ + User result; + getUser(uid,result); + return result; +} // eo getUser(const std::string&) + + + +/** + * @brief get the (system) group data by name. + * @param[in] name the name of the group to search for. + * @param[out] result the group info. + * @return @a true iff the group was found and the result structure contains data. + */ +bool getGroup(const std::string& name, Group& result) +{ + struct group gr; + struct group *gr_ptr= NULL; + size_t buffer_size= sysconf(_SC_GETGR_R_SIZE_MAX); + boost::scoped_array< char > buffer( new char[ buffer_size ] ); + + int res= ::getgrnam_r( name.c_str(), &gr, buffer.get(), buffer_size, &gr_ptr); + + if (not (0 == res) or not gr_ptr) + { + return false; + } + + return copyData(gr_ptr, result); +} // eo getGroup(const std::string&,Group&) + + +/** + * @brief get the (system) group data by gid. + * @param[in] gid the gid of the group to search for. + * @param[out] result the group info. + * @return @a true iff the group was found and the result structure contains data. + */ +bool getGroup(gid_t gid, Group& result) +{ + struct group gr; + struct group *gr_ptr= NULL; + size_t buffer_size= sysconf(_SC_GETGR_R_SIZE_MAX); + boost::scoped_array< char > buffer( new char[ buffer_size ] ); + + int res= ::getgrgid_r( gid, &gr, buffer.get(), buffer_size, &gr_ptr); + + if (not (0 == res) or not gr_ptr) + { + return false; + } + + return copyData(gr_ptr, result); +} // eo getGroup(const std::string&,Group&) + + +/** + * @brief get group data by name. + * @param name name of the group + * @return the group data (invalid if group not found or on error). + */ +Group getGroup(const std::string& name) +{ + Group result; + getGroup(name, result); + return result; +} // eo getGroup(const std::string&) + + +/** + * @brief get group data by gid. + * @param gid gid of the group + * @return the group data (invalid if group not found or on error). + */ +Group getGroup(gid_t gid) +{ + Group result; + getGroup(gid, result); + return result; +} // eo getGroup(const std::string&) + +} // eo namespace i2n diff --git a/src/userfunc.hxx b/src/userfunc.hxx new file mode 100644 index 0000000..3172255 --- /dev/null +++ b/src/userfunc.hxx @@ -0,0 +1,87 @@ +/** @file + * @brief provides wrapper and tools for system calls and system related stuff. + * + * + * (c) Copyright 2007-2008 by Intra2net AG + * + * info@intra2net.com + * + * @bug + * Although most stuff should work under most POSIX like systems; + * some funcs might be very linux related. + * (But at least we use that lib currently under linux only.) + */ + +#ifndef _I2N_USERFUNC_HPP_ +#define _I2N_USERFUNC_HPP_ + +#include +#include + +#include +#include + +namespace i2n { + +/** + * @brief structure similar to "struct passwd", but more C++ like. + */ +struct User +{ + std::string name; + std::string password; + uid_t uid; + gid_t gid; + std::string gecos; + std::string homedir; + std::string shell; + + User(); + User(const std::string& name); + User(const char* name); + User(uid_t uid); + void clear(); + + bool isValid() const; + + operator bool() const { return isValid(); } +}; // eo struct User + + + +/** + * @brief structure similar to "struct group", but more C++ like. + */ +struct Group +{ + std::string name; + std::string password; + gid_t gid; + std::vector< std::string > members; + + Group(); + Group(const std::string& name); + Group(const char* name); + Group(gid_t gid); + void clear(); + + bool isValid() const; + + operator bool() const { return isValid(); } +}; // eo struct Group + +bool getUser(const std::string& name, User& result); +bool getUser(uid_t uid, User& result); + +User getUser(const std::string& name); +User getUser(uid_t uid); + +bool getGroup(const std::string& name, Group& result); +bool getGroup(gid_t gid, Group& result); + +Group getGroup(const std::string& name); +Group getGroup(gid_t gid); + +} // eo namespace i2n + +#endif