2 * @brief provides wrapper and tools for (system) user and group information.
4 * @copyright Intra2net AG
8 * (c) Copyright 2007-2008 by Intra2net AG
14 #include "userfunc.hpp"
23 #include <sys/types.h>
29 #include <boost/scoped_array.hpp>
41 bool copy_data ( struct passwd* from, User& to )
44 if (!from) return false;
45 to.Name = from->pw_name;
46 to.Password = from->pw_passwd;
47 to.Uid = from->pw_uid;
48 to.Gid = from->pw_gid;
49 to.Gecos = from->pw_gecos;
50 to.Homedir = from->pw_dir;
51 to.Shell = from->pw_shell;
53 } // eo copy_data(struct passwd*,Passwd&)
56 bool copy_data ( struct group* from , Group& to)
59 if (!from) return false;
60 to.Name = from->gr_name;
61 to.Password = from->gr_passwd;
62 to.Gid = from->gr_gid;
64 for ( char **ptr= from->gr_mem;
68 to.Members.push_back ( *ptr );
71 } // eo copy_data(struct group*,Group&)
74 } // eo namespace <anonymous>
77 /*************************************************************************\
78 \*************************************************************************/
81 ** implementation of User
92 * @brief constructs and fills with user data.
93 * @param name the name of the user to search for.
95 User::User(const std::string& name)
98 } // eo User::User(const std::string&)
102 * @brief constructs and fills with user data.
103 * @param name the name of the user to search for.
105 User::User(const char* name)
107 get_user(std::string (name),*this);
108 } // eo User::User(const char*)
112 * @brief constructs and fills with user data.
113 * @param uid the uid of the user to search for.
115 User::User(uid_t uid)
119 } // eo User::User(uid_t)
124 * @brief clears the data.
135 } // eo User::clear()
139 * @brief returns if the structure content looks ok.
140 * @return @a true if the content looks ok.
142 * This functions checks if mandatory values are existing.
143 * @note It does not check if content is consistent with any system database!
145 bool User::is_valid() const
147 return (Uid != (uid_t)-1) and (Gid != (gid_t)-1)
150 } // eo User::is_valid() const
154 ** implementation of Group
161 } // eo Group::Group()
165 * @brief constructs and fills with group data.
166 * @param name the name of the group to search for.
168 Group::Group(const std::string& name)
170 get_group(name,*this);
171 } // eo group::Group(const std::strring&)
175 * @brief constructs and fills with group data.
176 * @param name the name of the group to search for.
178 Group::Group(const char* name)
180 get_group(std::string (name),*this);
181 } // eo group::Group(const char*)
185 * @brief constructs and fills with group data.
186 * @param gid the gid of the group to search for.
188 Group::Group(gid_t gid)
190 get_group(gid,*this);
192 } // eo Group::Group(gid_t);
196 * @brief clears the data.
204 } // eo Group::clear()
208 * @brief returns if the structure content looks ok.
209 * @return @a true if the content looks ok.
211 * This functions checks if mandatory values are existing.
212 * @note It does not check if content is consistent with any system database!
214 bool Group::is_valid() const
216 return (Gid != (gid_t)-1) and not Name.empty();
217 } // eo Group::is_valid() const
220 * @brief get the (system) user data by name.
221 * @param[in] name the name of the user to search for.
222 * @param[out] result the user info.
223 * @return @a true iff the user was found and the result structure contains data.
225 bool get_user(const std::string& name, User& result)
228 struct passwd *pw_ptr= NULL;
229 size_t buffer_size= sysconf (_SC_GETPW_R_SIZE_MAX);
230 boost::scoped_array< char > buffer ( new char[ buffer_size ] );
232 int res= ::getpwnam_r(name.c_str(), &pw, buffer.get(), buffer_size, &pw_ptr);
234 if (not (0 == res) or not pw_ptr)
239 return copy_data(pw_ptr, result);
240 } // eo get_user(const std::string&,User&)
244 * @brief get the (system) user data by uid.
245 * @param[in] uid the uid of the user to search for.
246 * @param[out] result the user info.
247 * @return @a true iff the user was found and the result structure contains data.
249 bool get_user(uid_t uid, User& result)
252 struct passwd *pw_ptr= NULL;
253 size_t buffer_size= sysconf (_SC_GETPW_R_SIZE_MAX);
254 boost::scoped_array< char > buffer ( new char[ buffer_size ] );
256 int res= ::getpwuid_r(uid, &pw, buffer.get(), buffer_size, &pw_ptr);
258 if (not (0 == res) or not pw_ptr)
263 return copy_data(pw_ptr, result);
264 } // eo get_user(uid_t,User&)
268 * @brief get user data by name.
269 * @param name name of the user
270 * @return the user data (invalid if user not found or on error).
272 User get_user(const std::string& name)
275 get_user(name,result);
277 } // eo get_user(const std::string&)
281 * @brief get user data by uid.
282 * @param uid uid of the user
283 * @return the user data (invalid if user not found or on error).
285 User get_user(uid_t uid)
288 get_user(uid,result);
290 } // eo get_user(const std::string&)
295 * @brief get the (system) group data by name.
296 * @param[in] name the name of the group to search for.
297 * @param[out] result the group info.
298 * @return @a true iff the group was found and the result structure contains data.
300 bool get_group (const std::string& name, Group& result)
303 struct group *gr_ptr= NULL;
304 size_t buffer_size= sysconf (_SC_GETGR_R_SIZE_MAX);
305 boost::scoped_array< char > buffer ( new char[ buffer_size ] );
307 int res= ::getgrnam_r(name.c_str(), &gr, buffer.get(), buffer_size, &gr_ptr);
309 if (not (0 == res) or not gr_ptr)
314 return copy_data(gr_ptr, result);
315 } // eo get_group(const std::string&,Group&)
319 * @brief get the (system) group data by gid.
320 * @param[in] gid the gid of the group to search for.
321 * @param[out] result the group info.
322 * @return @a true iff the group was found and the result structure contains data.
324 bool get_group(gid_t gid, Group& result)
327 struct group *gr_ptr= NULL;
328 size_t buffer_size= sysconf (_SC_GETGR_R_SIZE_MAX);
329 boost::scoped_array< char > buffer ( new char[ buffer_size ] );
331 int res= ::getgrgid_r(gid, &gr, buffer.get(), buffer_size, &gr_ptr);
333 if (not (0 == res) or not gr_ptr)
338 return copy_data(gr_ptr, result);
339 } // eo get_group(const std::string&,Group&)
343 * @brief get group data by name.
344 * @param name name of the group
345 * @return the group data (invalid if group not found or on error).
347 Group get_group(const std::string& name)
350 get_group(name, result);
352 } // eo get_group(const std::string&)
356 * @brief get group data by gid.
357 * @param gid gid of the group
358 * @return the group data (invalid if group not found or on error).
360 Group get_group(gid_t gid)
363 get_group(gid, result);
365 } // eo get_group(const std::string&)
367 } // eo namespace I2n