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

KDECore

  • 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()) && (