00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #if defined (__linux__)
00020
00021 #define _XOPEN_SOURCE 600
00022 #include <features.h>
00023 #endif
00024
00025 #include <pwd.h>
00026 #include <grp.h>
00027 #include <sys/types.h>
00028
00029 #include "PwGrp.H"
00030
00031
00032
00033 static Basics::mutex pwGrp_mu(PTHREAD_MUTEX_RECURSIVE);
00034
00035
00036
00037 void OS::Group::clear()
00038 {
00039 int gr_mem_num = members.size();
00040 for(int i = 0; i < gr_mem_num; i++) {
00041 members.remhi();
00042 }
00043 }
00044
00045
00046
00047 OS::PasswdIter::PasswdIter()
00048 {
00049 pwGrp_mu.lock();
00050 setpwent();
00051 }
00052
00053 OS::PasswdIter::~PasswdIter()
00054 {
00055 endpwent();
00056 pwGrp_mu.unlock();
00057 }
00058
00059 bool OS::PasswdIter::Next(OS::Passwd& passwd)
00060 {
00061 struct passwd* pwent;
00062 pwent = getpwent();
00063 if(pwent != 0) {
00064 passwd.name = pwent->pw_name;
00065 passwd.uid = pwent->pw_uid;
00066 passwd.gid = pwent->pw_gid;
00067 return true;
00068 }
00069 return false;
00070 }
00071
00072
00073
00074 OS::GroupIter::GroupIter()
00075 {
00076 pwGrp_mu.lock();
00077 setgrent();
00078 }
00079
00080 OS::GroupIter::~GroupIter()
00081 {
00082 endgrent();
00083 pwGrp_mu.unlock();
00084 }
00085
00086 bool OS::GroupIter::Next(OS::Group& group)
00087 {
00088 struct group* grent;
00089 grent = getgrent();
00090 if(grent != 0) {
00091 group.clear();
00092 group.name = grent->gr_name;
00093 group.gid = grent->gr_gid;
00094 char** grmem;
00095 for(grmem = grent->gr_mem; *grmem != NULL; grmem++) {
00096 group.members.addhi(*grmem);
00097 }
00098 return true;
00099 }
00100 return false;
00101 }
00102
00103
00104 bool OS::getPwNam(const Text& user_name, OS::Passwd& passwd)
00105 {
00106 pwGrp_mu.lock();
00107 bool result = false;
00108 struct passwd* pwent;
00109 pwent = getpwnam(user_name.cchars());
00110 if(pwent != 0) {
00111 passwd.name = pwent->pw_name;
00112 passwd.uid = pwent->pw_uid;
00113 passwd.gid = pwent->pw_gid;
00114 result = true;
00115 }
00116 pwGrp_mu.unlock();
00117 return result;
00118 }
00119
00120
00121 bool OS::getPwUid(uid_t uid, OS::Passwd& passwd)
00122 {
00123 pwGrp_mu.lock();
00124 bool result = false;
00125 struct passwd* pwent;
00126 pwent = getpwuid(uid);
00127 if(pwent != 0) {
00128 passwd.name = pwent->pw_name;
00129 passwd.uid = pwent->pw_uid;
00130 passwd.gid = pwent->pw_gid;
00131 result = true;
00132 }
00133 pwGrp_mu.unlock();
00134 return result;
00135 }
00136
00137
00138 bool OS::getGrNam(const Text& user_name, OS::Group& group)
00139 {
00140 pwGrp_mu.lock();
00141 bool result = false;
00142 struct group* grent;
00143 grent = getgrnam(user_name.cchars());
00144 if(grent != 0) {
00145 group.clear();
00146 group.name = grent->gr_name;
00147 group.gid = grent->gr_gid;
00148 char** grmem;
00149 for(grmem = grent->gr_mem; *grmem != NULL; grmem++) {
00150 group.members.addhi(*grmem);
00151 }
00152 result = true;
00153 }
00154 pwGrp_mu.unlock();
00155 return result;
00156 }
00157
00158
00159 bool OS::getGrGid(gid_t gid, OS::Group& group)
00160 {
00161 pwGrp_mu.lock();
00162 bool result = false;
00163 struct group* grent;
00164 grent = getgrgid(gid);
00165 if(grent != 0) {
00166 group.clear();
00167 group.name = grent->gr_name;
00168 group.gid = grent->gr_gid;
00169 char** grmem;
00170 for(grmem = grent->gr_mem; *grmem != NULL; grmem++) {
00171 group.members.addhi(*grmem);
00172 }
00173 result = true;
00174 }
00175 pwGrp_mu.unlock();
00176 return result;
00177 }
00178