• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KDECore

  • sources
  • kde-4.14
  • kdelibs
  • kdecore
  • util
kuser_unix.cpp
Go to the documentation of this file.
1 /*
2  * KUser - represent a user/account
3  * Copyright (C) 2002 Tim Jansen <tim@tjansen.de>
4  *
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * along with this library; see the file COPYING.LIB. If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21 
22 #include <kuser.h>
23 
24 #include <QtCore/QMutableStringListIterator>
25 #include <QtCore/QDir>
26 
27 #include <pwd.h>
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <grp.h>
31 
32 class KUser::Private : public KShared
33 {
34 public:
35  uid_t uid;
36  gid_t gid;
37  QString loginName;
38  QString homeDir, shell;
39  QMap<UserProperty, QVariant> properties;
40 
41  Private() : uid(uid_t(-1)), gid(gid_t(-1)) {}
42  Private(const char *name) : uid(uid_t(-1)), gid(gid_t(-1))
43  {
44  fillPasswd(name ? ::getpwnam( name ) : 0);
45  }
46  Private(const passwd *p) : uid(uid_t(-1)), gid(gid_t(-1))
47  {
48  fillPasswd(p);
49  }
50 
51  void fillPasswd(const passwd *p)
52  {
53  if (p) {
54  QString gecos = QString::fromLocal8Bit(p->pw_gecos);
55  QStringList gecosList = gecos.split(QLatin1Char(','));
56  // fill up the list, should be at least 4 entries
57  while (gecosList.size() < 4)
58  gecosList << QString();
59 
60  uid = p->pw_uid;
61  gid = p->pw_gid;
62  loginName = QString::fromLocal8Bit(p->pw_name);
63  properties[KUser::FullName] = QVariant(gecosList[0]);
64  properties[KUser::RoomNumber] = QVariant(gecosList[1]);
65  properties[KUser::WorkPhone] = QVariant(gecosList[2]);
66  properties[KUser::HomePhone] = QVariant(gecosList[3]);
67  homeDir = QString::fromLocal8Bit(p->pw_dir);
68  shell = QString::fromLocal8Bit(p->pw_shell);
69  }
70  }
71 };
72 
73 
74 KUser::KUser(UIDMode mode)
75 {
76  uid_t _uid = ::getuid(), _euid;
77  if (mode == UseEffectiveUID && (_euid = ::geteuid()) != _uid )
78  d = new Private( ::getpwuid( _euid ) );
79  else {
80  d = new Private( qgetenv( "LOGNAME" ) );
81  if (uid() != _uid) {
82  d = new Private( qgetenv( "USER" ) );
83  if (uid() != _uid)
84  d = new Private( ::getpwuid( _uid ) );
85  }
86  }
87 }
88 
89 KUser::KUser(K_UID _uid)
90  : d(new Private( ::getpwuid( _uid ) ))
91 {
92 }
93 
94 KUser::KUser(const QString& name)
95  : d(new Private( name.toLocal8Bit().data() ))
96 {
97 }
98 
99 KUser::KUser(const char *name)
100  : d(new Private( name ))
101 {
102 }
103 
104 KUser::KUser(const passwd *p)
105  : d(new Private( p ))
106 {
107 }
108 
109 KUser::KUser(const KUser & user)
110  : d(user.d)
111 {
112 }
113 
114 KUser& KUser::operator =(const KUser& user)
115 {
116  d = user.d;
117  return *this;
118 }
119 
120 bool KUser::operator ==(const KUser& user) const {
121  return (uid() == user.uid()) && (uid() != uid_t(-1));
122 }
123 
124 bool KUser::operator !=(const KUser& user) const {
125  return (uid() != user.uid()) || (uid() == uid_t(-1));
126 }
127 
128 bool KUser::isValid() const {
129  return uid() != uid_t(-1);
130 }
131 
132 K_UID KUser::uid() const {
133  return d->uid;
134 }
135 
136 K_GID KUser::gid() const {
137  return d->gid;
138 }
139 
140 bool KUser::isSuperUser() const {
141  return uid() == 0;
142 }
143 
144 QString KUser::loginName() const {
145  return d->loginName;
146 }
147 
148 #ifndef KDE_NO_DEPRECATED
149 QString KUser::fullName() const {
150  return d->properties[FullName].toString();
151 }
152 #endif
153 
154 QString KUser::homeDir() const {
155  return d->homeDir;
156 }
157 
158 QString KUser::faceIconPath() const
159 {
160  QString pathToFaceIcon(homeDir() + QDir::separator() + QLatin1String(".face.icon"));
161 
162  if (QFile::exists(pathToFaceIcon)) {
163  return pathToFaceIcon;
164  }
165 
166  return QString();
167 }
168 
169 QString KUser::shell() const {
170  return d->shell;
171 }
172 
173 QList<KUserGroup> KUser::groups() const {
174  QList<KUserGroup> result;
175  const QList<KUserGroup> allGroups = KUserGroup::allGroups();
176  QList<KUserGroup>::const_iterator it;
177  for ( it = allGroups.begin(); it != allGroups.end(); ++it ) {
178  const QList<KUser> users = (*it).users();
179  if ( users.contains(*this) ) {
180  result.append(*it);
181  }
182  }
183  return result;
184 }
185 
186 QStringList KUser::groupNames() const {
187  QStringList result;
188  const QList<KUserGroup> allGroups = KUserGroup::allGroups();
189  QList<KUserGroup>::const_iterator it;
190  for ( it = allGroups.begin(); it != allGroups.end(); ++it ) {
191  const QList<KUser> users = (*it).users();
192  if ( users.contains(*this) ) {
193  result.append((*it).name());
194  }
195  }
196  return result;
197 }
198 
199 QVariant KUser::property(UserProperty which) const
200 {
201  return d->properties.value(which);
202 }
203 
204 QList<KUser> KUser::allUsers() {
205  QList<KUser> result;
206 
207  passwd* p;
208 
209  while ((p = getpwent())) {
210  result.append(KUser(p));
211  }
212 
213  endpwent();
214 
215  return result;
216 }
217 
218 QStringList KUser::allUserNames() {
219  QStringList result;
220 
221  passwd* p;
222 
223  while ((p = getpwent())) {
224  result.append(QString::fromLocal8Bit(p->pw_name));
225  }
226 
227  endpwent();
228  return result;
229 }
230 
231 KUser::~KUser() {
232 }
233 
234 class KUserGroup::Private : public KShared
235 {
236 public:
237  gid_t gid;
238  QString name;
239  QList<KUser> users;
240 
241  Private() : gid(gid_t(-1)) {}
242  Private(const char *_name) : gid(gid_t(-1))
243  {
244  fillGroup(_name ? ::getgrnam( _name ) : 0);
245  }
246  Private(const ::group *p) : gid(gid_t(-1))
247  {
248  fillGroup(p);
249  }
250 
251  void fillGroup(const ::group *p) {
252  if (p) {
253  gid = p->gr_gid;
254  name = QString::fromLocal8Bit(p->gr_name);
255  for (char **user = p->gr_mem; *user; user++)
256  users.append(KUser(*user));
257  }
258  }
259 };
260 
261 KUserGroup::KUserGroup(KUser::UIDMode mode)
262 {
263  d = new Private(getgrgid(KUser(mode).gid()));
264 }
265 
266 KUserGroup::KUserGroup(K_GID _gid)
267  : d(new Private(getgrgid(_gid)))
268 {
269 }
270 
271 KUserGroup::KUserGroup(const QString& _name)
272  : d(new Private(_name.toLocal8Bit().data()))
273 {
274 }
275 
276 KUserGroup::KUserGroup(const char *_name)
277  : d(new Private(_name))
278 {
279 }
280 
281 KUserGroup::KUserGroup(const ::group *g)
282  : d(new Private(g))
283 {
284 }
285 
286 KUserGroup::KUserGroup(const KUserGroup & group)
287  : d(group.d)
288 {
289 }
290 
291 KUserGroup& KUserGroup::operator =(const KUserGroup& group) {
292  d = group.d;
293  return *this;
294 }
295 
296 bool KUserGroup::operator ==(const KUserGroup& group) const {
297  return (gid() == group.gid()) && (gid() != gid_t(-1));
298 }
299 
300 bool KUserGroup::operator !=(const KUserGroup& user) const {
301  return (gid() != user.gid()) || (gid() == gid_t(-1));
302 }
303 
304 bool KUserGroup::isValid() const {
305  return gid() != gid_t(-1);
306 }
307 
308 K_GID KUserGroup::gid() const {
309  return d->gid;
310 }
311 
312 QString KUserGroup::name() const {
313  return d->name;
314 }
315 
316 QList<KUser> KUserGroup::users() const {
317  return d->users;
318 }
319 
320 QStringList KUserGroup::userNames() const {
321  QStringList result;
322  QList<KUser>::const_iterator it;
323  for ( it = d->users.begin(); it != d->users.end(); ++it ) {
324  result.append((*it).loginName());
325  }
326  return result;
327 }
328 
329 QList<KUserGroup> KUserGroup::allGroups() {
330  QList<KUserGroup> result;
331 
332  ::group* g;
333  while ((g = getgrent())) {
334  result.append(KUserGroup(g));
335  }
336 
337  endgrent();
338 
339  return result;
340 }
341 
342 QStringList KUserGroup::allGroupNames() {
343  QStringList result;
344 
345  ::group* g;
346  while ((g = getgrent())) {
347  result.append(QString::fromLocal8Bit(g->gr_name));
348  }
349 
350  endgrent();
351 
352  return result;
353 }
354 
355 KUserGroup::~KUserGroup() {
356 }
KUserGroup::KUserGroup
KUserGroup(const QString &name)
Create an object from a group name.
Definition: kuser_unix.cpp:271
KUserGroup::allGroupNames
static QStringList allGroupNames()
Returns a list of all group names on this system.
Definition: kuser_unix.cpp:342
kuser.h
KUserGroup::users
QList< KUser > users() const
Returns a list of all users of the group.
Definition: kuser_unix.cpp:316
KUserGroup::isValid
bool isValid() const
Returns whether the group is valid.
Definition: kuser_unix.cpp:304
KUserGroup::name
QString name() const
The name of the group.
Definition: kuser_unix.cpp:312
KUser::UseEffectiveUID
Use the effective user id.
Definition: kuser.h:64
KUser::uid
K_UID uid() const
Returns the user id of the user.
Definition: kuser_unix.cpp:132
KMacroExpander::group
Definition: kmacroexpander_unix.cpp:34
QString::split
QStringList split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
KUser::property
QVariant property(UserProperty which) const
Returns an extended property.
Definition: kuser_unix.cpp:199
QMap< UserProperty, QVariant >
KUserGroup
Represents a group on your system.
Definition: kuser.h:262
KUser::allUsers
static QList< KUser > allUsers()
Returns all users of the system.
Definition: kuser_unix.cpp:204
KUser::fullName
QString fullName() const
The full name of the user.
Definition: kuser_unix.cpp:149
QFile::exists
bool exists() const
KUser::operator!=
bool operator!=(const KUser &user) const
Two KUser objects are not equal if uid() are not identical.
Definition: kuser_unix.cpp:124
QDir::separator
QChar separator()
QList::const_iterator
QList::size
int size() const
KUser
Represents a user on your system.
Definition: kuser.h:59
KUser::operator==
bool operator==(const KUser &user) const
Two KUser objects are equal if the uid() are identical.
Definition: kuser_unix.cpp:120
KUser::RoomNumber
Definition: kuser.h:216
QSharedData
QString::fromLocal8Bit
QString fromLocal8Bit(const char *str, int size)
K_GID
void * K_GID
Definition: kuser.h:37
QList::append
void append(const T &value)
KUser::allUserNames
static QStringList allUserNames()
Returns all user names of the system.
Definition: kuser_unix.cpp:218
KUser::HomePhone
Definition: kuser.h:216
KUser::loginName
QString loginName() const
The login name of the user.
Definition: kuser_unix.cpp:144
KUser::FullName
Definition: kuser.h:216
KUserGroup::operator==
bool operator==(const KUserGroup &group) const
Two KUserGroup objects are equal if their gid()s are identical.
Definition: kuser_unix.cpp:296
KUser::UserProperty
UserProperty
Definition: kuser.h:216
KUserGroup::operator=
KUserGroup & operator=(const KUserGroup &group)
Copies a group.
Definition: kuser_unix.cpp:291
KUser::groupNames
QStringList groupNames() const
Returns all group names of the user.
Definition: kuser_unix.cpp:186
QString
QList
Definition: kaboutdata.h:33
QStringList
QList::end
iterator end()
KUserGroup::userNames
QStringList userNames() const
Returns a list of all user login names of the group.
Definition: kuser_unix.cpp:320
QLatin1Char
KShell::homeDir
QString homeDir(const QString &user)
Definition: kshell.cpp:29
KUser::UIDMode
UIDMode
Definition: kuser.h:63
QList::contains
bool contains(const T &value) const
KUser::homeDir
QString homeDir() const
The path to the user's home directory.
Definition: kuser_unix.cpp:154
KUser::shell
QString shell() const
The path to the user's login shell.
Definition: kuser_unix.cpp:169
QLatin1String
KUser::isSuperUser
bool isSuperUser() const
Checks whether the user is the super user (root).
Definition: kuser_unix.cpp:140
KUserGroup::~KUserGroup
~KUserGroup()
Destructor.
Definition: kuser_unix.cpp:355
K_UID
void * K_UID
Definition: kuser.h:33
KUser::~KUser
~KUser()
Destructor.
Definition: kuser_unix.cpp:231
KUser::groups
QList< KUserGroup > groups() const
Returns all groups of the user.
Definition: kuser_unix.cpp:173
KUser::WorkPhone
Definition: kuser.h:216
KUser::operator=
KUser & operator=(const KUser &user)
Copies a user.
Definition: kuser_unix.cpp:114
KUser::isValid
bool isValid() const
Returns true if the user is valid.
Definition: kuser_unix.cpp:128
KUserGroup::allGroups
static QList< KUserGroup > allGroups()
Returns a list of all groups on this system.
Definition: kuser_unix.cpp:329
KUser::KUser
KUser(UIDMode mode=UseEffectiveUID)
Creates an object that contains information about the current user.
Definition: kuser_unix.cpp:74
QList::begin
iterator begin()
KUserGroup::operator!=
bool operator!=(const KUserGroup &group) const
Two KUserGroup objects are not equal if their gid()s are not identical.
Definition: kuser_unix.cpp:300
KUser::faceIconPath
QString faceIconPath() const
The path to the user's face file.
Definition: kuser_unix.cpp:158
QVariant
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:22:12 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDECore

Skip menu "KDECore"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal