2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
8 As a special exception, if other files instantiate templates or use macros
9 or inline functions from this file, or you compile this file and link it
10 with other works to produce a work based on this file, this file
11 does not by itself cause the resulting work to be covered
12 by the GNU General Public License.
14 However the source code for this file must still be made available
15 in accordance with section (3) of the GNU General Public License.
17 This exception does not invalidate any other reasons why a work based
18 on this file might be covered by the GNU General Public License.
21 * @brief provides wrapper and tools for (system) user and group information.
23 * @copyright Intra2net AG
27 * (c) Copyright 2007-2008 by Intra2net AG
30 #include "userfunc.hpp"
39 #include <sys/types.h>
45 #include <boost/scoped_array.hpp>
57 bool copy_data ( struct passwd* from, User& to )
60 if (!from) return false;
61 to.Name = from->pw_name;
62 to.Password = from->pw_passwd;
63 to.Uid = from->pw_uid;
64 to.Gid = from->pw_gid;
65 to.Gecos = from->pw_gecos;
66 to.Homedir = from->pw_dir;
67 to.Shell = from->pw_shell;
69 } // eo copy_data(struct passwd*,Passwd&)
72 bool copy_data ( struct group* from , Group& to)
75 if (!from) return false;
76 to.Name = from->gr_name;
77 to.Password = from->gr_passwd;
78 to.Gid = from->gr_gid;
80 for ( char **ptr= from->gr_mem;
84 to.Members.push_back ( *ptr );
87 } // eo copy_data(struct group*,Group&)
90 } // eo namespace <anonymous>
93 /*************************************************************************\
94 \*************************************************************************/
97 ** implementation of User
108 * @brief constructs and fills with user data.
109 * @param name the name of the user to search for.
111 User::User(const std::string& name)
113 get_user(name,*this);
114 } // eo User::User(const std::string&)
118 * @brief constructs and fills with user data.
119 * @param name the name of the user to search for.
121 User::User(const char* name)
123 get_user(std::string (name),*this);
124 } // eo User::User(const char*)
128 * @brief constructs and fills with user data.
129 * @param uid the uid of the user to search for.
131 User::User(uid_t uid)
135 } // eo User::User(uid_t)
140 * @brief clears the data.
151 } // eo User::clear()
155 * @brief returns if the structure content looks ok.
156 * @return @a true if the content looks ok.
158 * This functions checks if mandatory values are existing.
159 * @note It does not check if content is consistent with any system database!
161 bool User::is_valid() const
163 return (Uid != (uid_t)-1) and (Gid != (gid_t)-1)
166 } // eo User::is_valid() const
170 ** implementation of Group
177 } // eo Group::Group()
181 * @brief constructs and fills with group data.
182 * @param name the name of the group to search for.
184 Group::Group(const std::string& name)
186 get_group(name,*this);
187 } // eo group::Group(const std::strring&)
191 * @brief constructs and fills with group data.
192 * @param name the name of the group to search for.
194 Group::Group(const char* name)
196 get_group(std::string (name),*this);
197 } // eo group::Group(const char*)
201 * @brief constructs and fills with group data.
202 * @param gid the gid of the group to search for.
204 Group::Group(gid_t gid)
206 get_group(gid,*this);
208 } // eo Group::Group(gid_t);
212 * @brief clears the data.
220 } // eo Group::clear()
224 * @brief returns if the structure content looks ok.
225 * @return @a true if the content looks ok.
227 * This functions checks if mandatory values are existing.
228 * @note It does not check if content is consistent with any system database!
230 bool Group::is_valid() const
232 return (Gid != (gid_t)-1) and not Name.empty();
233 } // eo Group::is_valid() const
236 * @brief get the (system) user data by name.
237 * @param[in] name the name of the user to search for.
238 * @param[out] result the user info.
239 * @return @a true iff the user was found and the result structure contains data.
241 bool get_user(const std::string& name, User& result)
244 struct passwd *pw_ptr= NULL;
245 size_t buffer_size= sysconf (_SC_GETPW_R_SIZE_MAX);
246 boost::scoped_array< char > buffer ( new char[ buffer_size ] );
248 int res= ::getpwnam_r(name.c_str(), &pw, buffer.get(), buffer_size, &pw_ptr);
250 if (not (0 == res) or not pw_ptr)
255 return copy_data(pw_ptr, result);
256 } // eo get_user(const std::string&,User&)
260 * @brief get the (system) user data by uid.
261 * @param[in] uid the uid of the user to search for.
262 * @param[out] result the user info.
263 * @return @a true iff the user was found and the result structure contains data.
265 bool get_user(uid_t uid, User& result)
268 struct passwd *pw_ptr= NULL;
269 size_t buffer_size= sysconf (_SC_GETPW_R_SIZE_MAX);
270 boost::scoped_array< char > buffer ( new char[ buffer_size ] );
272 int res= ::getpwuid_r(uid, &pw, buffer.get(), buffer_size, &pw_ptr);
274 if (not (0 == res) or not pw_ptr)
279 return copy_data(pw_ptr, result);
280 } // eo get_user(uid_t,User&)
284 * @brief get user data by name.
285 * @param name name of the user
286 * @return the user data (invalid if user not found or on error).
288 User get_user(const std::string& name)
291 get_user(name,result);
293 } // eo get_user(const std::string&)
297 * @brief get user data by uid.
298 * @param uid uid of the user
299 * @return the user data (invalid if user not found or on error).
301 User get_user(uid_t uid)
304 get_user(uid,result);
306 } // eo get_user(const std::string&)
311 * @brief get the (system) group data by name.
312 * @param[in] name the name of the group to search for.
313 * @param[out] result the group info.
314 * @return @a true iff the group was found and the result structure contains data.
316 bool get_group (const std::string& name, Group& result)
319 struct group *gr_ptr= NULL;
320 size_t buffer_size= sysconf (_SC_GETGR_R_SIZE_MAX);
321 boost::scoped_array< char > buffer ( new char[ buffer_size ] );
323 int res= ::getgrnam_r(name.c_str(), &gr, buffer.get(), buffer_size, &gr_ptr);
325 if (not (0 == res) or not gr_ptr)
330 return copy_data(gr_ptr, result);
331 } // eo get_group(const std::string&,Group&)
335 * @brief get the (system) group data by gid.
336 * @param[in] gid the gid of the group to search for.
337 * @param[out] result the group info.
338 * @return @a true iff the group was found and the result structure contains data.
340 bool get_group(gid_t gid, Group& result)
343 struct group *gr_ptr= NULL;
344 size_t buffer_size= sysconf (_SC_GETGR_R_SIZE_MAX);
345 boost::scoped_array< char > buffer ( new char[ buffer_size ] );
347 int res= ::getgrgid_r(gid, &gr, buffer.get(), buffer_size, &gr_ptr);
349 if (not (0 == res) or not gr_ptr)
354 return copy_data(gr_ptr, result);
355 } // eo get_group(const std::string&,Group&)
359 * @brief get group data by name.
360 * @param name name of the group
361 * @return the group data (invalid if group not found or on error).
363 Group get_group(const std::string& name)
366 get_group(name, result);
368 } // eo get_group(const std::string&)
372 * @brief get group data by gid.
373 * @param gid gid of the group
374 * @return the group data (invalid if group not found or on error).
376 Group get_group(gid_t gid)
379 get_group(gid, result);
381 } // eo get_group(const std::string&)
383 } // eo namespace I2n