KDECore
kuser.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <kuser.h>
00023
00024 #include "kstringhandler.h"
00025 #include <qvaluelist.h>
00026 #include <qstringlist.h>
00027
00028 #include <sys/types.h>
00029 #include <pwd.h>
00030 #include <unistd.h>
00031 #include <stdlib.h>
00032 #include <grp.h>
00033
00034
00035 class KUserPrivate : public KShared
00036 {
00037 public:
00038 bool valid;
00039 long uid, gid;
00040 QString loginName, fullName;
00041 QString roomNumber, workPhone, homePhone;
00042 QString homeDir, shell;
00043
00044 KUserPrivate() : valid(false) {}
00045
00046 KUserPrivate(long _uid,
00047 long _gid,
00048 const QString &_loginname,
00049 const QString &_fullname,
00050 const QString &_room,
00051 const QString &_workPhone,
00052 const QString &_homePhone,
00053 const QString &_homedir,
00054 const QString &_shell) :
00055 valid(true),
00056 uid(_uid),
00057 gid(_gid),
00058 loginName(_loginname),
00059 fullName(_fullname),
00060 roomNumber(_room),
00061 workPhone(_workPhone),
00062 homePhone(_homePhone),
00063 homeDir(_homedir),
00064 shell(_shell) {}
00065 };
00066
00067
00068 KUser::KUser(UIDMode mode) {
00069 long _uid = ::getuid(), _euid;
00070 if (mode == UseEffectiveUID && (_euid = ::geteuid()) != _uid )
00071 fillPasswd( ::getpwuid( _euid ) );
00072 else {
00073 fillName( ::getenv( "LOGNAME" ) );
00074 if (uid() != _uid) {
00075 fillName( ::getenv( "USER" ) );
00076 if (uid() != _uid)
00077 fillPasswd( ::getpwuid( _uid ) );
00078 }
00079 }
00080 }
00081
00082 KUser::KUser(long uid) {
00083 fillPasswd( ::getpwuid( uid ) );
00084 }
00085
00086 KUser::KUser(const QString& name) {
00087 fillName( name.local8Bit().data() );
00088 }
00089
00090 KUser::KUser(const char *name) {
00091 fillName( name );
00092 }
00093
00094 KUser::KUser(struct passwd *p) {
00095 fillPasswd(p);
00096 }
00097
00098 KUser::KUser(const KUser & user)
00099 : d(user.d)
00100 {
00101 }
00102
00103 KUser& KUser::operator =(const KUser& user)
00104 {
00105 d = user.d;
00106 return *this;
00107 }
00108
00109 bool KUser::operator ==(const KUser& user) const {
00110 if (isValid() != user.isValid())
00111 return false;
00112 if (isValid())
00113 return uid() == user.uid();
00114 else
00115 return true;
00116 }
00117
00118 bool KUser::operator !=(const KUser& user) const {
00119 return !operator ==(user);
00120 }
00121
00122 void KUser::fillName(const char *name) {
00123 fillPasswd(name ? ::getpwnam( name ) : 0);
00124 }
00125
00126 void KUser::fillPasswd(struct passwd *p) {
00127 if (p) {
00128 QString gecos = KStringHandler::from8Bit(p->pw_gecos);
00129 QStringList gecosList = QStringList::split(',', gecos, true);
00130
00131 d = new KUserPrivate(p->pw_uid,
00132 p->pw_gid,
00133 QString::fromLocal8Bit(p->pw_name),
00134 (gecosList.size() > 0) ? gecosList[0] : QString::null,
00135 (gecosList.size() > 1) ? gecosList[1] : QString::null,
00136 (gecosList.size() > 2) ? gecosList[2] : QString::null,
00137 (gecosList.size() > 3) ? gecosList[3] : QString::null,
00138 QString::fromLocal8Bit(p->pw_dir),
00139 QString::fromLocal8Bit(p->pw_shell));
00140 }
00141 else
00142 d = new KUserPrivate();
00143 }
00144
00145 bool KUser::isValid() const {
00146 return d->valid;
00147 }
00148
00149 long KUser::uid() const {
00150 if (d->valid)
00151 return d->uid;
00152 else
00153 return -1;
00154 }
00155
00156 long KUser::gid() const {
00157 if (d->valid)
00158 return d->gid;
00159 else
00160 return -1;
00161 }
00162
00163 bool KUser::isSuperUser() const {
00164 return uid() == 0;
00165 }
00166
00167 QString KUser::loginName() const {
00168 if (d->valid)
00169 return d->loginName;
00170 else
00171 return QString::null;
00172 }
00173
00174 QString KUser::fullName() const {
00175 if (d->valid)
00176 return d->fullName;
00177 else
00178 return QString::null;
00179 }
00180
00181 QString KUser::roomNumber() const {
00182 if (d->valid)
00183 return d->roomNumber;
00184 else
00185 return QString::null;
00186 }
00187
00188 QString KUser::workPhone() const {
00189 if (d->valid)
00190 return d->workPhone;
00191 else
00192 return QString::null;
00193 }
00194
00195 QString KUser::homePhone() const {
00196 if (d->valid)
00197 return d->homePhone;
00198 else
00199 return QString::null;
00200 }
00201
00202 QString KUser::homeDir() const {
00203 if (d->valid)
00204 return d->homeDir;
00205 else
00206 return QString::null;
00207 }
00208
00209 QString KUser::shell() const {
00210 if (d->valid)
00211 return d->shell;
00212 else
00213 return QString::null;
00214 }
00215
00216 QValueList<KUserGroup> KUser::groups() const {
00217 QValueList<KUserGroup> result;
00218 QValueList<KUserGroup> allGroups = KUserGroup::allGroups();
00219 QValueList<KUserGroup>::const_iterator it;
00220 for ( it = allGroups.begin(); it != allGroups.end(); ++it ) {
00221 QValueList<KUser> users = (*it).users();
00222 if ( users.find( *this ) != users.end()) {
00223 result.append(*it);
00224 }
00225 }
00226 return result;
00227 }
00228
00229 QStringList KUser::groupNames() const {
00230 QStringList result;
00231 QValueList<KUserGroup> allGroups = KUserGroup::allGroups();
00232 QValueList<KUserGroup>::const_iterator it;
00233 for ( it = allGroups.begin(); it != allGroups.end(); ++it ) {
00234 QValueList<KUser> users = (*it).users();
00235 if ( users.find( *this ) != users.end()) {
00236 result.append((*it).name());
00237 }
00238 }
00239 return result;
00240 }
00241
00242
00243 QValueList<KUser> KUser::allUsers() {
00244 QValueList<KUser> result;
00245
00246 struct passwd* p;
00247
00248 while ((p = getpwent())) {
00249 result.append(KUser(p));
00250 }
00251
00252 endpwent();
00253
00254 return result;
00255 }
00256
00257 QStringList KUser::allUserNames() {
00258 QStringList result;
00259
00260 struct passwd* p;
00261
00262 while ((p = getpwent())) {
00263 result.append(QString::fromLocal8Bit(p->pw_name));
00264 }
00265
00266 endpwent();
00267 return result;
00268 }
00269
00270
00271 KUser::~KUser() {
00272 }
00273
00274 class KUserGroupPrivate : public KShared
00275 {
00276 public:
00277 bool valid;
00278 long gid;
00279 QString name;
00280 QValueList<KUser> users;
00281
00282 KUserGroupPrivate() : valid(false) {}
00283
00284 KUserGroupPrivate(long _gid,
00285 const QString & _name,
00286 const QValueList<KUser> & _users):
00287 valid(true),
00288 gid(_gid),
00289 name(_name),
00290 users(_users) {}
00291 };
00292
00293 KUserGroup::KUserGroup(KUser::UIDMode mode) {
00294 KUser user(mode);
00295 fillGroup(getgrgid(user.gid()));
00296 }
00297
00298 KUserGroup::KUserGroup(long gid) {
00299 fillGroup(getgrgid(gid));
00300 }
00301
00302 KUserGroup::KUserGroup(const QString& name) {
00303 fillName(name.local8Bit().data());
00304 }
00305
00306 KUserGroup::KUserGroup(const char *name) {
00307 fillName(name);
00308 }
00309
00310 KUserGroup::KUserGroup(struct group *g) {
00311 fillGroup(g);
00312 }
00313
00314
00315 KUserGroup::KUserGroup(const KUserGroup & group)
00316 : d(group.d)
00317 {
00318 }
00319
00320 KUserGroup& KUserGroup::operator =(const KUserGroup& group) {
00321 d = group.d;
00322 return *this;
00323 }
00324
00325 bool KUserGroup::operator ==(const KUserGroup& group) const {
00326 if (isValid() != group.isValid())
00327 return false;
00328 if (isValid())
00329 return gid() == group.gid();
00330 else
00331 return true;
00332 }
00333
00334 bool KUserGroup::operator !=(const KUserGroup& user) const {
00335 return !operator ==(user);
00336 }
00337
00338 void KUserGroup::fillName(const char *name) {
00339 fillGroup(name ? ::getgrnam( name ) : 0);
00340 }
00341
00342 void KUserGroup::fillGroup(struct group *p) {
00343 if (!p) {
00344 d = new KUserGroupPrivate();
00345 return;
00346 }
00347
00348 QString name = KStringHandler::from8Bit(p->gr_name);
00349 QValueList<KUser> users;
00350
00351 char **user = p->gr_mem;
00352 for ( ; *user; user++) {
00353 KUser kUser(QString::fromLocal8Bit(*user));
00354 users.append(kUser);
00355 }
00356
00357 d = new KUserGroupPrivate(p->gr_gid,
00358 QString::fromLocal8Bit(p->gr_name),
00359 users);
00360
00361 }
00362
00363 bool KUserGroup::isValid() const {
00364 return d->valid;
00365 }
00366
00367 long KUserGroup::gid() const {
00368 if (d->valid)
00369 return d->gid;
00370 else
00371 return -1;
00372 }
00373
00374 QString KUserGroup::name() const {
00375 if (d->valid)
00376 return d->name;
00377 else
00378 return QString::null;
00379 }
00380
00381 const QValueList<KUser>& KUserGroup::users() const {
00382 return d->users;
00383 }
00384
00385 QStringList KUserGroup::userNames() const {
00386 QStringList result;
00387 QValueList<KUser>::const_iterator it;
00388 for ( it = d->users.begin(); it != d->users.end(); ++it ) {
00389 result.append((*it).loginName());
00390 }
00391 return result;
00392 }
00393
00394
00395
00396 QValueList<KUserGroup> KUserGroup::allGroups() {
00397 QValueList<KUserGroup> result;
00398
00399 struct group* g;
00400 while ((g = getgrent())) {
00401 result.append(KUserGroup(g));
00402 }
00403
00404 endgrent();
00405
00406 return result;
00407 }
00408
00409 QStringList KUserGroup::allGroupNames() {
00410 QStringList result;
00411
00412 struct group* g;
00413 while ((g = getgrent())) {
00414 result.append(QString::fromLocal8Bit(g->gr_name));
00415 }
00416
00417 endgrent();
00418
00419 return result;
00420 }
00421
00422
00423 KUserGroup::~KUserGroup() {
00424 }
00425