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"
38 #include <sys/types.h>
43 #include <boost/scoped_array.hpp>
55 bool copy_data ( struct passwd* from, User& to )
58 if (!from) return false;
59 to.Name = from->pw_name;
60 to.Password = from->pw_passwd;
61 to.Uid = from->pw_uid;
62 to.Gid = from->pw_gid;
63 to.Gecos = from->pw_gecos;
64 to.Homedir = from->pw_dir;
65 to.Shell = from->pw_shell;
67 } // eo copy_data(struct passwd*,Passwd&)
70 bool copy_data ( struct group* from , Group& to)
73 if (!from) return false;
74 to.Name = from->gr_name;
75 to.Password = from->gr_passwd;
76 to.Gid = from->gr_gid;
78 for ( char **ptr= from->gr_mem;
82 to.Members.push_back ( *ptr );
85 } // eo copy_data(struct group*,Group&)
88 } // eo namespace <anonymous>
91 /*************************************************************************\
92 \*************************************************************************/
95 ** implementation of User
106 * @brief constructs and fills with user data.
107 * @param name the name of the user to search for.
109 User::User(const std::string& name)
111 get_user(name,*this);
112 } // eo User::User(const std::string&)
116 * @brief constructs and fills with user data.
117 * @param name the name of the user to search for.
119 User::User(const char* name)
121 get_user(std::string (name),*this);
122 } // eo User::User(const char*)
126 * @brief constructs and fills with user data.
127 * @param uid the uid of the user to search for.
129 User::User(uid_t uid)
133 } // eo User::User(uid_t)
138 * @brief clears the data.
149 } // eo User::clear()
153 * @brief returns if the structure content looks ok.
154 * @return @a true if the content looks ok.
156 * This functions checks if mandatory values are existing.
157 * @note It does not check if content is consistent with any system database!
159 bool User::is_valid() const
161 return (Uid != (uid_t)-1) and (Gid != (gid_t)-1)
164 } // eo User::is_valid() const
168 ** implementation of Group
175 } // eo Group::Group()
179 * @brief constructs and fills with group data.
180 * @param name the name of the group to search for.
182 Group::Group(const std::string& name)
184 get_group(name,*this);
185 } // eo group::Group(const std::strring&)
189 * @brief constructs and fills with group data.
190 * @param name the name of the group to search for.
192 Group::Group(const char* name)
194 get_group(std::string (name),*this);
195 } // eo group::Group(const char*)
199 * @brief constructs and fills with group data.
200 * @param gid the gid of the group to search for.
202 Group::Group(gid_t gid)
204 get_group(gid,*this);
206 } // eo Group::Group(gid_t);
210 * @brief clears the data.
218 } // eo Group::clear()
222 * @brief returns if the structure content looks ok.
223 * @return @a true if the content looks ok.
225 * This functions checks if mandatory values are existing.
226 * @note It does not check if content is consistent with any system database!
228 bool Group::is_valid() const
230 return (Gid != (gid_t)-1) and not Name.empty();
231 } // eo Group::is_valid() const
234 * @brief get the (system) user data by name.
235 * @param[in] name the name of the user to search for.
236 * @param[out] result the user info.
237 * @return @a true iff the user was found and the result structure contains data.
239 bool get_user(const std::string& name, User& result)
242 struct passwd *pw_ptr= NULL;
243 size_t buffer_size= sysconf (_SC_GETPW_R_SIZE_MAX);
244 boost::scoped_array< char > buffer ( new char[ buffer_size ] );
246 int res= ::getpwnam_r(name.c_str(), &pw, buffer.get(), buffer_size, &pw_ptr);
248 if (not (0 == res) or not pw_ptr)
253 return copy_data(pw_ptr, result);
254 } // eo get_user(const std::string&,User&)
258 * @brief get the (system) user data by uid.
259 * @param[in] uid the uid of the user to search for.
260 * @param[out] result the user info.
261 * @return @a true iff the user was found and the result structure contains data.
263 bool get_user(uid_t uid, User& result)
266 struct passwd *pw_ptr= NULL;
267 size_t buffer_size= sysconf (_SC_GETPW_R_SIZE_MAX);
268 boost::scoped_array< char > buffer ( new char[ buffer_size ] );
270 int res= ::getpwuid_r(uid, &pw, buffer.get(), buffer_size, &pw_ptr);
272 if (not (0 == res) or not pw_ptr)
277 return copy_data(pw_ptr, result);
278 } // eo get_user(uid_t,User&)
282 * @brief get user data by name.
283 * @param name name of the user
284 * @return the user data (invalid if user not found or on error).
286 User get_user(const std::string& name)
289 get_user(name,result);
291 } // eo get_user(const std::string&)
295 * @brief get user data by uid.
296 * @param uid uid of the user
297 * @return the user data (invalid if user not found or on error).
299 User get_user(uid_t uid)
302 get_user(uid,result);
304 } // eo get_user(const std::string&)
309 * @brief get the (system) group data by name.
310 * @param[in] name the name of the group to search for.
311 * @param[out] result the group info.
312 * @return @a true iff the group was found and the result structure contains data.
314 bool get_group (const std::string& name, Group& result)
317 struct group *gr_ptr= NULL;
318 size_t buffer_size= sysconf (_SC_GETGR_R_SIZE_MAX);
319 boost::scoped_array< char > buffer ( new char[ buffer_size ] );
321 int res= ::getgrnam_r(name.c_str(), &gr, buffer.get(), buffer_size, &gr_ptr);
323 if (not (0 == res) or not gr_ptr)
328 return copy_data(gr_ptr, result);
329 } // eo get_group(const std::string&,Group&)
333 * @brief get the (system) group data by gid.
334 * @param[in] gid the gid of the group to search for.
335 * @param[out] result the group info.
336 * @return @a true iff the group was found and the result structure contains data.
338 bool get_group(gid_t gid, Group& result)
341 struct group *gr_ptr= NULL;
342 size_t buffer_size= sysconf (_SC_GETGR_R_SIZE_MAX);
343 boost::scoped_array< char > buffer ( new char[ buffer_size ] );
345 int res= ::getgrgid_r(gid, &gr, buffer.get(), buffer_size, &gr_ptr);
347 if (not (0 == res) or not gr_ptr)
352 return copy_data(gr_ptr, result);
353 } // eo get_group(const std::string&,Group&)
357 * @brief get group data by name.
358 * @param name name of the group
359 * @return the group data (invalid if group not found or on error).
361 Group get_group(const std::string& name)
364 get_group(name, result);
366 } // eo get_group(const std::string&)
370 * @brief get group data by gid.
371 * @param gid gid of the group
372 * @return the group data (invalid if group not found or on error).
374 Group get_group(gid_t gid)
377 get_group(gid, result);
379 } // eo get_group(const std::string&)
381 } // eo namespace I2n