/** @file
+ * @brief provides wrapper and tools for (system) user and group information.
+ *
+ * @copyright Intra2net AG
+ *
+ * @license commercial
*
* (c) Copyright 2007-2008 by Intra2net AG
- *
+ *
* info@intra2net.com
+ *
*/
#include "userfunc.hxx"
#include <boost/scoped_array.hpp>
-namespace i2n {
+namespace I2n
+{
-namespace {
+namespace
+{
/*
** tool functions
*/
-bool copyData( struct passwd* from, User& to )
+bool copy_data ( 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->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 copy_data(struct passwd*,Passwd&)
+
+
+bool copy_data ( 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;
+ 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&)
+ {
+ to.Members.push_back ( *ptr );
+ }
+ return true;
+} // eo copy_data(struct group*,Group&)
} // eo namespace <anonymous>
*/
User::User()
-: uid(-1)
-, gid(-1)
+: 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)
+User::User (const std::string& name)
{
- getUser(name,*this);
+ get_user (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)
+User::User (const char* name)
{
- getUser(std::string(name),*this);
+ get_user (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)
+User::User (uid_t uid)
{
- getUser(uid,*this);
- this->uid = uid;
+ get_user (uid,*this);
+ this->Uid = uid;
} // eo User::User(uid_t)
*/
void User::clear()
{
- name.clear();
- password.clear();
- uid= -1;
- gid= -1;
- gecos.clear();
- homedir.clear();
- shell.clear();
+ Name.clear();
+ Password.clear();
+ Uid= -1;
+ Gid= -1;
+ Gecos.clear();
+ Homedir.clear();
+ Shell.clear();
} // eo User::clear()
* 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
+bool User::is_valid() const
{
- return (uid >= 0) and (gid >= 0)
- and not name.empty()
- ;
-} // eo User::isValid() const
+ return (Uid >= 0) and (Gid >= 0)
+ and not Name.empty()
+ ;
+} // eo User::is_valid() const
/*
Group::Group()
-: gid(-1)
+: 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)
+Group::Group (const std::string& name)
{
- getGroup(name,*this);
+ get_group (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)
+Group::Group (const char* name)
{
- getGroup(std::string(name),*this);
+ get_group (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)
+Group::Group (gid_t gid)
{
- getGroup(gid,*this);
- this->gid= gid;
+ get_group (gid,*this);
+ this->Gid= gid;
} // eo Group::Group(gid_t);
*/
void Group::clear()
{
- name.clear();
- password.clear();
- gid= -1;
- members.clear();
+ Name.clear();
+ Password.clear();
+ Gid= -1;
+ Members.clear();
} // eo Group::clear()
* 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
+bool Group::is_valid() const
{
- return (gid >= 0) and not name.empty();
-} // eo Group::isValid() const
+ return (Gid >= 0) and not Name.empty();
+} // eo Group::is_valid() const
/**
* @brief get the (system) user data by name.
* @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)
+bool get_user (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&)
+ 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 copy_data (pw_ptr, result);
+} // eo get_user(const std::string&,User&)
/**
* @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)
+bool get_user (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&)
+ 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 copy_data (pw_ptr, result);
+} // eo get_user(uid_t,User&)
/**
* @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 get_user (const std::string& name)
{
- User result;
- getUser(name,result);
- return result;
-} // eo getUser(const std::string&)
+ User result;
+ get_user (name,result);
+ return result;
+} // eo get_user(const std::string&)
/**
* @param uid uid of the user
* @return the user data (invalid if user not found or on error).
*/
-User getUser(uid_t uid)
+User get_user (uid_t uid)
{
- User result;
- getUser(uid,result);
- return result;
-} // eo getUser(const std::string&)
+ User result;
+ get_user (uid,result);
+ return result;
+} // eo get_user(const std::string&)
* @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)
+bool get_group (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 ] );
+ 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);
+ int res= ::getgrnam_r ( name.c_str(), &gr, buffer.get(), buffer_size, &gr_ptr);
- if (not (0 == res) or not gr_ptr)
- {
- return false;
- }
+ if (not (0 == res) or not gr_ptr)
+ {
+ return false;
+ }
- return copyData(gr_ptr, result);
-} // eo getGroup(const std::string&,Group&)
+ return copy_data (gr_ptr, result);
+} // eo get_group(const std::string&,Group&)
/**
* @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)
+bool get_group (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&)
+ 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 copy_data (gr_ptr, result);
+} // eo get_group(const std::string&,Group&)
/**
* @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 get_group (const std::string& name)
{
- Group result;
- getGroup(name, result);
- return result;
-} // eo getGroup(const std::string&)
+ Group result;
+ get_group (name, result);
+ return result;
+} // eo get_group(const std::string&)
/**
* @param gid gid of the group
* @return the group data (invalid if group not found or on error).
*/
-Group getGroup(gid_t gid)
+Group get_group (gid_t gid)
{
- Group result;
- getGroup(gid, result);
- return result;
-} // eo getGroup(const std::string&)
+ Group result;
+ get_group (gid, result);
+ return result;
+} // eo get_group(const std::string&)
-} // eo namespace i2n
+} // eo namespace I2n
/** @file
- * @brief provides wrapper and tools for system calls and system related stuff.
+ * @brief provides wrapper and tools for (system) user and group information.
*
+ * @copyright Intra2net AG
+ *
+ * @license commercial
*
* (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_
#include <time.h>
#include <sys/types.h>
-namespace i2n {
+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(); }
+ 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 is_valid() const;
+
+ operator bool() const { return is_valid(); }
}; // eo struct User
*/
struct Group
{
- std::string name;
- std::string password;
- gid_t gid;
- std::vector< std::string > members;
+ 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();
+ Group();
+ Group (const std::string& name);
+ Group (const char* name);
+ Group (gid_t gid);
+ void clear();
- bool isValid() const;
+ bool is_valid() const;
- operator bool() const { return isValid(); }
+ operator bool() const { return is_valid(); }
}; // eo struct Group
-bool getUser(const std::string& name, User& result);
-bool getUser(uid_t uid, User& result);
+bool get_user (const std::string& name, User& result);
+bool get_user (uid_t uid, User& result);
-User getUser(const std::string& name);
-User getUser(uid_t uid);
+User get_user (const std::string& name);
+User get_user (uid_t uid);
-bool getGroup(const std::string& name, Group& result);
-bool getGroup(gid_t gid, Group& result);
+bool get_group (const std::string& name, Group& result);
+bool get_group (gid_t gid, Group& result);
-Group getGroup(const std::string& name);
-Group getGroup(gid_t gid);
+Group get_group (const std::string& name);
+Group get_group (gid_t gid);
-} // eo namespace i2n
+} // eo namespace I2n
#endif