--- /dev/null
+/** @file
+ *
+ * (c) Copyright 2007-2008 by Intra2net AG
+ *
+ * info@intra2net.com
+ */
+
+#include "userfunc.hxx"
+
+#include <algorithm>
+#include <fstream>
+
+#include <unistd.h>
+#include <signal.h>
+#include <errno.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <dirent.h>
+#include <errno.h>
+#include <pwd.h>
+#include <grp.h>
+
+#include <boost/scoped_array.hpp>
+
+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 <anonymous>
+
+
+/*************************************************************************\
+\*************************************************************************/
+
+/*
+** 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