00001 // Copyright (C) 2005, Vesta Free Software Project 00002 // 00003 // This file is part of Vesta. 00004 // 00005 // Vesta is free software; you can redistribute it and/or 00006 // modify it under the terms of the GNU Lesser General Public 00007 // License as published by the Free Software Foundation; either 00008 // version 2.1 of the License, or (at your option) any later version. 00009 // 00010 // Vesta is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 // Lesser General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU Lesser General Public 00016 // License along with Vesta; if not, write to the Free Software 00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00018 00019 /* 00020 getpwent(), getgrent(), getpwuid(), getpwnam(), getgrgid(), getgrnam() 00021 library functions are not thread safe. 00022 00023 In order to make sure that only one thread calls them in a time we 00024 defined the folowing wrappers: 00025 00026 class PasswdIter: wrapper around iterative calls to getpwent() function 00027 class GroupIter: wrapper around iterative calls to getgrent() function 00028 getPwNam: wrapper around getpwnam() function 00029 getGrNam: wrapper around getgrnam() function 00030 getPwUid: wrapper around getpwuid() function 00031 getGrGid: wrapper around getgrgid() function 00032 00033 Note that the constructors of PasswdIter and GroupIter acquire a 00034 mutex. This lock is only released when the object is destroyed, so 00035 you should only ever keep these objects in existence long enough to 00036 loop over the passworg/group information and then destory them. 00037 00038 The same mutex (pwGrp_mu) is acquired by the consturctors of 00039 PasswdIter and GroupIter, as well as getPwNam, getGrNam, getGrGid, 00040 getPwUid. In order to prevent a thread from inadvertenly causing a 00041 deadlock (by creating both iterators at the same time, or calling 00042 one of the functions while an iterator exists) the 00043 PTHREAD_MUTEX_RECURSIVE type of mutex is used. 00044 */ 00045 00046 #ifndef _PwGrp_H 00047 #define _PwGrp_H 00048 00049 #include <Basics.H> 00050 #include <Generics.H> 00051 00052 namespace OS 00053 { 00054 // Represents a single entry in the password database. (Similar to 00055 // the standard struct passwd.) 00056 struct Passwd 00057 { 00058 Text name; // Username 00059 uid_t uid; // User ID. 00060 gid_t gid; // Group ID. 00061 }; 00062 00063 // Represents a single group in the group database. (Similar to the 00064 // standard struct group.) 00065 class Group 00066 { 00067 public: 00068 Text name; // Group name. 00069 gid_t gid; // Group ID. 00070 TextSeq members; // Member list. 00071 00072 Group() {}; 00073 void clear(); 00074 }; 00075 00076 // Iterator for looping over all the entries in the password 00077 // database. 00078 class PasswdIter 00079 { 00080 public: 00081 // Note: constructor acquires lock, destructor releases it. 00082 PasswdIter(); 00083 ~PasswdIter(); 00084 00085 // Get the next password entry. Modifies "passwd" in place. 00086 // Returns true if "passwd" was filled with a valid entry, false if 00087 // there are no more entries. 00088 bool Next(/*OUT*/Passwd& passwd); 00089 }; 00090 00091 // Iterator for looping over all the entries in the group database. 00092 class GroupIter 00093 { 00094 public: 00095 // Note: constructor acquires lock, destructor releases it. 00096 GroupIter(); 00097 ~GroupIter(); 00098 00099 // Get the next group entry. Modifies "group" in place. Returns 00100 // true if "group" was filled with a valid entry, false if there 00101 // are no more entries. 00102 bool Next(/*OUT*/Group& group); 00103 }; 00104 00105 // Thread-safe wrappers for getpwnam(3), getgrnam(3), getpwuid(3), 00106 // getgrgid(3). The second argument ("passwd" or "group") is 00107 // modified in place. The return value indicates whether a matching 00108 // entry was found. 00109 00110 bool getPwNam(const Text& user_name, /*OUT*/Passwd& passwd); 00111 bool getGrNam(const Text& user_name, /*OUT*/Group& group); 00112 bool getPwUid(uid_t uid, /*OUT*/Passwd& passwd); 00113 bool getGrGid(gid_t gid, /*OUT*/Group& group); 00114 00115 }; 00116 00117 #endif //_PwGrp_H