You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
426 lines
8.3 KiB
426 lines
8.3 KiB
15 years ago
|
/*
|
||
|
* KUser - represent a user/account
|
||
|
* Copyright (C) 2002 Tim Jansen <tim@tjansen.de>
|
||
|
*
|
||
|
*
|
||
|
* This library is free software; you can redistribute it and/or
|
||
|
* modify it under the terms of the GNU Library General Public
|
||
|
* License as published by the Free Software Foundation; either
|
||
|
* version 2 of the License, or (at your option) any later version.
|
||
|
*
|
||
|
* This library is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||
|
* Library General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU Library General Public License
|
||
|
* along with this library; see the file COPYING.LIB. If not, write to
|
||
|
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||
|
* Boston, MA 02110-1301, USA.
|
||
|
*/
|
||
|
|
||
|
#include <kuser.h>
|
||
|
|
||
|
#include "kstringhandler.h"
|
||
15 years ago
|
#include <tqvaluelist.h>
|
||
|
#include <tqstringlist.h>
|
||
15 years ago
|
|
||
|
#include <sys/types.h>
|
||
|
#include <pwd.h>
|
||
|
#include <unistd.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <grp.h>
|
||
|
|
||
|
|
||
|
class KUserPrivate : public KShared
|
||
|
{
|
||
|
public:
|
||
|
bool valid;
|
||
|
long uid, gid;
|
||
15 years ago
|
TQString loginName, fullName;
|
||
|
TQString roomNumber, workPhone, homePhone;
|
||
|
TQString homeDir, shell;
|
||
15 years ago
|
|
||
|
KUserPrivate() : valid(false) {}
|
||
|
|
||
|
KUserPrivate(long _uid,
|
||
|
long _gid,
|
||
15 years ago
|
const TQString &_loginname,
|
||
|
const TQString &_fullname,
|
||
|
const TQString &_room,
|
||
|
const TQString &_workPhone,
|
||
|
const TQString &_homePhone,
|
||
|
const TQString &_homedir,
|
||
|
const TQString &_shell) :
|
||
15 years ago
|
valid(true),
|
||
|
uid(_uid),
|
||
|
gid(_gid),
|
||
|
loginName(_loginname),
|
||
|
fullName(_fullname),
|
||
|
roomNumber(_room),
|
||
|
workPhone(_workPhone),
|
||
|
homePhone(_homePhone),
|
||
|
homeDir(_homedir),
|
||
|
shell(_shell) {}
|
||
|
};
|
||
|
|
||
|
|
||
|
KUser::KUser(UIDMode mode) {
|
||
|
long _uid = ::getuid(), _euid;
|
||
|
if (mode == UseEffectiveUID && (_euid = ::geteuid()) != _uid )
|
||
|
fillPasswd( ::getpwuid( _euid ) );
|
||
|
else {
|
||
|
fillName( ::getenv( "LOGNAME" ) );
|
||
|
if (uid() != _uid) {
|
||
|
fillName( ::getenv( "USER" ) );
|
||
|
if (uid() != _uid)
|
||
|
fillPasswd( ::getpwuid( _uid ) );
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
KUser::KUser(long uid) {
|
||
|
fillPasswd( ::getpwuid( uid ) );
|
||
|
}
|
||
|
|
||
15 years ago
|
KUser::KUser(const TQString& name) {
|
||
15 years ago
|
fillName( name.local8Bit().data() );
|
||
|
}
|
||
|
|
||
|
KUser::KUser(const char *name) {
|
||
|
fillName( name );
|
||
|
}
|
||
|
|
||
|
KUser::KUser(struct passwd *p) {
|
||
|
fillPasswd(p);
|
||
|
}
|
||
|
|
||
|
KUser::KUser(const KUser & user)
|
||
|
: d(user.d)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
KUser& KUser::operator =(const KUser& user)
|
||
|
{
|
||
|
d = user.d;
|
||
|
return *this;
|
||
|
}
|
||
|
|
||
|
bool KUser::operator ==(const KUser& user) const {
|
||
|
if (isValid() != user.isValid())
|
||
|
return false;
|
||
|
if (isValid())
|
||
|
return uid() == user.uid();
|
||
|
else
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
bool KUser::operator !=(const KUser& user) const {
|
||
|
return !operator ==(user);
|
||
|
}
|
||
|
|
||
|
void KUser::fillName(const char *name) {
|
||
|
fillPasswd(name ? ::getpwnam( name ) : 0);
|
||
|
}
|
||
|
|
||
|
void KUser::fillPasswd(struct passwd *p) {
|
||
|
if (p) {
|
||
15 years ago
|
TQString gecos = KStringHandler::from8Bit(p->pw_gecos);
|
||
|
TQStringList gecosList = TQStringList::split(',', gecos, true);
|
||
15 years ago
|
|
||
|
d = new KUserPrivate(p->pw_uid,
|
||
|
p->pw_gid,
|
||
15 years ago
|
TQString::fromLocal8Bit(p->pw_name),
|
||
|
(gecosList.size() > 0) ? gecosList[0] : TQString::null,
|
||
|
(gecosList.size() > 1) ? gecosList[1] : TQString::null,
|
||
|
(gecosList.size() > 2) ? gecosList[2] : TQString::null,
|
||
|
(gecosList.size() > 3) ? gecosList[3] : TQString::null,
|
||
|
TQString::fromLocal8Bit(p->pw_dir),
|
||
|
TQString::fromLocal8Bit(p->pw_shell));
|
||
15 years ago
|
}
|
||
|
else
|
||
|
d = new KUserPrivate();
|
||
|
}
|
||
|
|
||
|
bool KUser::isValid() const {
|
||
|
return d->valid;
|
||
|
}
|
||
|
|
||
|
long KUser::uid() const {
|
||
|
if (d->valid)
|
||
|
return d->uid;
|
||
|
else
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
long KUser::gid() const {
|
||
|
if (d->valid)
|
||
|
return d->gid;
|
||
|
else
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
bool KUser::isSuperUser() const {
|
||
|
return uid() == 0;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString KUser::loginName() const {
|
||
15 years ago
|
if (d->valid)
|
||
|
return d->loginName;
|
||
|
else
|
||
15 years ago
|
return TQString::null;
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
TQString KUser::fullName() const {
|
||
15 years ago
|
if (d->valid)
|
||
|
return d->fullName;
|
||
|
else
|
||
15 years ago
|
return TQString::null;
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
TQString KUser::roomNumber() const {
|
||
15 years ago
|
if (d->valid)
|
||
|
return d->roomNumber;
|
||
|
else
|
||
15 years ago
|
return TQString::null;
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
TQString KUser::workPhone() const {
|
||
15 years ago
|
if (d->valid)
|
||
|
return d->workPhone;
|
||
|
else
|
||
15 years ago
|
return TQString::null;
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
TQString KUser::homePhone() const {
|
||
15 years ago
|
if (d->valid)
|
||
|
return d->homePhone;
|
||
|
else
|
||
15 years ago
|
return TQString::null;
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
TQString KUser::homeDir() const {
|
||
15 years ago
|
if (d->valid)
|
||
|
return d->homeDir;
|
||
|
else
|
||
15 years ago
|
return TQString::null;
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
TQString KUser::shell() const {
|
||
15 years ago
|
if (d->valid)
|
||
|
return d->shell;
|
||
|
else
|
||
15 years ago
|
return TQString::null;
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
TQValueList<KUserGroup> KUser::groups() const {
|
||
|
TQValueList<KUserGroup> result;
|
||
|
TQValueList<KUserGroup> allGroups = KUserGroup::allGroups();
|
||
|
TQValueList<KUserGroup>::const_iterator it;
|
||
15 years ago
|
for ( it = allGroups.begin(); it != allGroups.end(); ++it ) {
|
||
15 years ago
|
TQValueList<KUser> users = (*it).users();
|
||
14 years ago
|
if ( users.find( *this ) != users.end()) {
|
||
15 years ago
|
result.append(*it);
|
||
|
}
|
||
|
}
|
||
|
return result;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQStringList KUser::groupNames() const {
|
||
|
TQStringList result;
|
||
|
TQValueList<KUserGroup> allGroups = KUserGroup::allGroups();
|
||
|
TQValueList<KUserGroup>::const_iterator it;
|
||
15 years ago
|
for ( it = allGroups.begin(); it != allGroups.end(); ++it ) {
|
||
15 years ago
|
TQValueList<KUser> users = (*it).users();
|
||
14 years ago
|
if ( users.find( *this ) != users.end()) {
|
||
15 years ago
|
result.append((*it).name());
|
||
|
}
|
||
|
}
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
|
||
15 years ago
|
TQValueList<KUser> KUser::allUsers() {
|
||
|
TQValueList<KUser> result;
|
||
15 years ago
|
|
||
|
struct passwd* p;
|
||
|
|
||
|
while ((p = getpwent())) {
|
||
|
result.append(KUser(p));
|
||
|
}
|
||
|
|
||
|
endpwent();
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQStringList KUser::allUserNames() {
|
||
|
TQStringList result;
|
||
15 years ago
|
|
||
|
struct passwd* p;
|
||
|
|
||
|
while ((p = getpwent())) {
|
||
15 years ago
|
result.append(TQString::fromLocal8Bit(p->pw_name));
|
||
15 years ago
|
}
|
||
|
|
||
|
endpwent();
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
|
||
|
KUser::~KUser() {
|
||
|
}
|
||
|
|
||
|
class KUserGroupPrivate : public KShared
|
||
|
{
|
||
|
public:
|
||
|
bool valid;
|
||
|
long gid;
|
||
15 years ago
|
TQString name;
|
||
|
TQValueList<KUser> users;
|
||
15 years ago
|
|
||
|
KUserGroupPrivate() : valid(false) {}
|
||
|
|
||
|
KUserGroupPrivate(long _gid,
|
||
15 years ago
|
const TQString & _name,
|
||
|
const TQValueList<KUser> & _users):
|
||
15 years ago
|
valid(true),
|
||
|
gid(_gid),
|
||
|
name(_name),
|
||
|
users(_users) {}
|
||
|
};
|
||
|
|
||
|
KUserGroup::KUserGroup(KUser::UIDMode mode) {
|
||
|
KUser user(mode);
|
||
|
fillGroup(getgrgid(user.gid()));
|
||
|
}
|
||
|
|
||
|
KUserGroup::KUserGroup(long gid) {
|
||
|
fillGroup(getgrgid(gid));
|
||
|
}
|
||
|
|
||
15 years ago
|
KUserGroup::KUserGroup(const TQString& name) {
|
||
15 years ago
|
fillName(name.local8Bit().data());
|
||
|
}
|
||
|
|
||
|
KUserGroup::KUserGroup(const char *name) {
|
||
|
fillName(name);
|
||
|
}
|
||
|
|
||
|
KUserGroup::KUserGroup(struct group *g) {
|
||
|
fillGroup(g);
|
||
|
}
|
||
|
|
||
|
|
||
|
KUserGroup::KUserGroup(const KUserGroup & group)
|
||
|
: d(group.d)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
KUserGroup& KUserGroup::operator =(const KUserGroup& group) {
|
||
|
d = group.d;
|
||
|
return *this;
|
||
|
}
|
||
|
|
||
|
bool KUserGroup::operator ==(const KUserGroup& group) const {
|
||
|
if (isValid() != group.isValid())
|
||
|
return false;
|
||
|
if (isValid())
|
||
|
return gid() == group.gid();
|
||
|
else
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
bool KUserGroup::operator !=(const KUserGroup& user) const {
|
||
|
return !operator ==(user);
|
||
|
}
|
||
|
|
||
|
void KUserGroup::fillName(const char *name) {
|
||
|
fillGroup(name ? ::getgrnam( name ) : 0);
|
||
|
}
|
||
|
|
||
|
void KUserGroup::fillGroup(struct group *p) {
|
||
|
if (!p) {
|
||
|
d = new KUserGroupPrivate();
|
||
|
return;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString name = KStringHandler::from8Bit(p->gr_name);
|
||
|
TQValueList<KUser> users;
|
||
15 years ago
|
|
||
|
char **user = p->gr_mem;
|
||
|
for ( ; *user; user++) {
|
||
15 years ago
|
KUser kUser(TQString::fromLocal8Bit(*user));
|
||
15 years ago
|
users.append(kUser);
|
||
|
}
|
||
|
|
||
|
d = new KUserGroupPrivate(p->gr_gid,
|
||
15 years ago
|
TQString::fromLocal8Bit(p->gr_name),
|
||
15 years ago
|
users);
|
||
|
|
||
|
}
|
||
|
|
||
|
bool KUserGroup::isValid() const {
|
||
|
return d->valid;
|
||
|
}
|
||
|
|
||
|
long KUserGroup::gid() const {
|
||
|
if (d->valid)
|
||
|
return d->gid;
|
||
|
else
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString KUserGroup::name() const {
|
||
15 years ago
|
if (d->valid)
|
||
|
return d->name;
|
||
|
else
|
||
15 years ago
|
return TQString::null;
|
||
15 years ago
|
}
|
||
|
|
||
15 years ago
|
const TQValueList<KUser>& KUserGroup::users() const {
|
||
15 years ago
|
return d->users;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQStringList KUserGroup::userNames() const {
|
||
|
TQStringList result;
|
||
|
TQValueList<KUser>::const_iterator it;
|
||
15 years ago
|
for ( it = d->users.begin(); it != d->users.end(); ++it ) {
|
||
|
result.append((*it).loginName());
|
||
|
}
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
15 years ago
|
TQValueList<KUserGroup> KUserGroup::allGroups() {
|
||
|
TQValueList<KUserGroup> result;
|
||
15 years ago
|
|
||
|
struct group* g;
|
||
|
while ((g = getgrent())) {
|
||
|
result.append(KUserGroup(g));
|
||
|
}
|
||
|
|
||
|
endgrent();
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQStringList KUserGroup::allGroupNames() {
|
||
|
TQStringList result;
|
||
15 years ago
|
|
||
|
struct group* g;
|
||
|
while ((g = getgrent())) {
|
||
15 years ago
|
result.append(TQString::fromLocal8Bit(g->gr_name));
|
||
15 years ago
|
}
|
||
|
|
||
|
endgrent();
|
||
|
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
|
||
|
KUserGroup::~KUserGroup() {
|
||
|
}
|
||
|
|