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

KDECore

  • sources
  • kde-4.12
  • kdelibs
  • kdecore
  • util
kuser_win.cpp
Go to the documentation of this file.
1 /*
2  * KUser - represent a user/account (Windows)
3  * Copyright (C) 2007 Bernhard Loos <nhuh.put@web.de>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB. If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 
21 #include "kuser.h"
22 
23 #include <QtCore/QMutableStringListIterator>
24 #include <QtCore/QDebug>
25 #include <QtCore/QDir>
26 
27 #include <windows.h>
28 #include <lm.h>
29 #include <sddl.h>
30 
31 class KUser::Private : public KShared
32 {
33  public:
34  PUSER_INFO_11 userInfo;
35  PSID sid;
36 
37  Private() : userInfo(0), sid(0) {}
38 
39  Private(PUSER_INFO_11 userInfo_, PSID sid_ = 0) : userInfo(userInfo_) {}
40 
41  Private(const QString &name, PSID sid_ = 0) : userInfo(0), sid(NULL)
42  {
43  LPBYTE servername;
44  NET_API_STATUS status = NetGetAnyDCName(0, 0, &servername);
45  if (status != NERR_Success)
46  {
47  servername = NULL;
48  }
49 
50  if (NetUserGetInfo((LPCWSTR) servername, (LPCWSTR) name.utf16(), 11, (LPBYTE *) &userInfo) != NERR_Success) {
51  goto error;
52  }
53  if (servername)
54  {
55  NetApiBufferFree(servername);
56  servername = 0;
57  }
58 
59  if (!sid_) {
60  DWORD size = 0;
61  SID_NAME_USE nameuse;
62  DWORD cchReferencedDomainName = 0;
63  WCHAR* referencedDomainName = NULL;
64 
65  // the following line definitely fails:
66  // both the sizes for sid and for referencedDomainName are Null
67  // the needed sizes are set in size and cchReferencedDomainName
68  LookupAccountNameW(NULL, (LPCWSTR) name.utf16(), sid, &size, referencedDomainName, &cchReferencedDomainName, &nameuse);
69  sid = (PSID) new SID[size + 1];
70  referencedDomainName = new WCHAR[cchReferencedDomainName + 1];
71  if (!LookupAccountNameW(NULL, (LPCWSTR) name.utf16(), sid, &size, referencedDomainName, &cchReferencedDomainName, &nameuse)) {
72  delete[] referencedDomainName;
73  goto error;
74  }
75 
76  // if you want to see both the DomainName and the sid of the user
77  // uncomment the following lines
78 /* LPWSTR sidstring;
79  ConvertSidToStringSidW(sid, &sidstring);
80  qDebug() << QString("\\\\") + QString::fromUtf16(reinterpret_cast<ushort*>(referencedDomainName)) + \
81  "\\" + name + "(" + QString::fromUtf16(reinterpret_cast<ushort*>(sidstring)) + ")";
82 
83  LocalFree(sidstring);*/
84  delete[] referencedDomainName;
85  }
86  else {
87  if (!IsValidSid(sid_))
88  goto error;
89 
90  DWORD sidlength = GetLengthSid(sid_);
91  sid = (PSID) new BYTE[sidlength];
92  if (!CopySid(sidlength, sid, sid_))
93  goto error;
94  }
95 
96  return;
97 
98  error:
99  delete[] sid;
100  sid = 0;
101  if (userInfo) {
102  NetApiBufferFree(userInfo);
103  userInfo = 0;
104  }
105  if (servername)
106  {
107  NetApiBufferFree(servername);
108  servername = 0;
109  }
110  }
111 
112  ~Private()
113  {
114  if (userInfo)
115  NetApiBufferFree(userInfo);
116 
117  delete[] sid;
118  }
119 };
120 
121 KUser::KUser(UIDMode mode)
122  : d(0)
123 {
124  Q_UNUSED(mode)
125 
126  DWORD bufferLen = UNLEN + 1;
127  ushort buffer[UNLEN + 1];
128 
129  if (GetUserNameW((LPWSTR) buffer, &bufferLen))
130  d = new Private(QString::fromUtf16(buffer));
131 }
132 
133 KUser::KUser(K_UID uid)
134  : d(0)
135 {
136  DWORD bufferLen = UNLEN + 1;
137  ushort buffer[UNLEN + 1];
138  SID_NAME_USE eUse;
139 
140  if (LookupAccountSidW(NULL, uid, (LPWSTR) buffer, &bufferLen, NULL, NULL, &eUse))
141  d = new Private(QString::fromUtf16(buffer), uid);
142 }
143 
144 KUser::KUser(const QString &name)
145  : d(new Private(name))
146 {
147 }
148 
149 KUser::KUser(const char *name)
150  :d(new Private(QString::fromLocal8Bit(name)))
151 {
152 }
153 
154 KUser::KUser(const KUser &user)
155  : d(user.d)
156 {
157 }
158 
159 KUser &KUser::operator=(const KUser &user)
160 {
161  d = user.d;
162  return *this;
163 }
164 
165 bool KUser::operator==(const KUser &user) const
166 {
167  if (!isValid() || !user.isValid())
168  return false;
169  return EqualSid(d->sid, user.d->sid);
170 }
171 
172 bool KUser::operator !=(const KUser &user) const
173 {
174  return !operator==(user);
175 }
176 
177 bool KUser::isValid() const
178 {
179  return d->userInfo != 0 && d->sid != 0;
180 }
181 
182 bool KUser::isSuperUser() const
183 {
184  return d->userInfo && d->userInfo->usri11_priv == USER_PRIV_ADMIN;
185 }
186 
187 QString KUser::loginName() const
188 {
189  return (d->userInfo ? QString::fromUtf16((ushort *) d->userInfo->usri11_name) : QString());
190 }
191 
192 #ifndef KDE_NO_DEPRECATED
193 QString KUser::fullName() const
194 {
195  return (d->userInfo ? QString::fromUtf16((ushort *) d->userInfo->usri11_full_name) : QString());
196 }
197 #endif
198 
199 QString KUser::homeDir() const
200 {
201  return QDir::fromNativeSeparators(QString::fromLocal8Bit(qgetenv("USERPROFILE")));
202 }
203 
204 QString KUser::faceIconPath() const
205 {
206  // FIXME: this needs to be adapted to windows systems (BC changes)
207  return QString();
208 }
209 
210 QString KUser::shell() const
211 {
212  return QString::fromLatin1("cmd.exe");
213 }
214 
215 QList<KUserGroup> KUser::groups() const
216 {
217  QList<KUserGroup> result;
218 
219  Q_FOREACH (const QString &name, groupNames()) {
220  result.append(KUserGroup(name));
221  }
222 
223  return result;
224 }
225 
226 QStringList KUser::groupNames() const
227 {
228  QStringList result;
229 
230  if (!d->userInfo) {
231  return result;
232  }
233 
234  PGROUP_USERS_INFO_0 pGroups = NULL;
235  DWORD dwEntriesRead = 0;
236  DWORD dwTotalEntries = 0;
237  NET_API_STATUS nStatus;
238 
239  nStatus = NetUserGetGroups(NULL, d->userInfo->usri11_name, 0, (LPBYTE *) &pGroups, MAX_PREFERRED_LENGTH, &dwEntriesRead, &dwTotalEntries);
240 
241  if (nStatus == NERR_Success) {
242  for (DWORD i = 0; i < dwEntriesRead; ++i) {
243  result.append(QString::fromUtf16((ushort *) pGroups[i].grui0_name));
244  }
245  }
246 
247  if (pGroups) {
248  NetApiBufferFree(pGroups);
249  }
250 
251  return result;
252 }
253 
254 K_UID KUser::uid() const
255 {
256  return d->sid;
257 }
258 
259 QVariant KUser::property(UserProperty which) const
260 {
261  if (which == FullName)
262  return QVariant(d->userInfo ? QString::fromUtf16((ushort *) d->userInfo->usri11_full_name) : QString());
263 
264  return QVariant();
265 }
266 
267 QList<KUser> KUser::allUsers()
268 {
269  QList<KUser> result;
270 
271  NET_API_STATUS nStatus;
272  PUSER_INFO_11 pUser = NULL;
273  DWORD dwEntriesRead = 0;
274  DWORD dwTotalEntries = 0;
275  DWORD dwResumeHandle = 0;
276 
277  KUser tmp;
278 
279  do {
280  nStatus = NetUserEnum(NULL, 11, 0, (LPBYTE*) &pUser, 1, &dwEntriesRead, &dwTotalEntries, &dwResumeHandle);
281 
282  if ((nStatus == NERR_Success || nStatus == ERROR_MORE_DATA) && dwEntriesRead > 0) {
283  tmp.d = new Private(pUser);
284  result.append(tmp);
285  }
286  } while (nStatus == ERROR_MORE_DATA);
287 
288  return result;
289 }
290 
291 QStringList KUser::allUserNames()
292 {
293  QStringList result;
294 
295  NET_API_STATUS nStatus;
296  PUSER_INFO_0 pUsers = NULL;
297  DWORD dwEntriesRead = 0;
298  DWORD dwTotalEntries = 0;
299 
300  nStatus = NetUserEnum(NULL, 0, 0, (LPBYTE*) &pUsers, MAX_PREFERRED_LENGTH, &dwEntriesRead, &dwTotalEntries, NULL);
301 
302  if (nStatus == NERR_Success) {
303  for (DWORD i = 0; i < dwEntriesRead; ++i) {
304  result.append(QString::fromUtf16((ushort *) pUsers[i].usri0_name));
305  }
306  }
307 
308  if (pUsers) {
309  NetApiBufferFree(pUsers);
310  }
311 
312  return result;
313 }
314 
315 KUser::~KUser()
316 {
317 }
318 
319 class KUserGroup::Private : public KShared
320 {
321  public:
322  PGROUP_INFO_0 groupInfo;
323 
324  Private() : groupInfo(NULL) {}
325  Private(PGROUP_INFO_0 groupInfo_) : groupInfo(groupInfo_) {}
326  Private(const QString &Name) : groupInfo(NULL)
327  {
328  NetGroupGetInfo(NULL, (PCWSTR) Name.utf16(), 0, (PBYTE *) &groupInfo);
329  }
330 
331  ~Private()
332  {
333  if (groupInfo) {
334  NetApiBufferFree(groupInfo);
335  }
336  }
337 };
338 
339 KUserGroup::KUserGroup(const QString &_name)
340  : d(new Private(_name))
341 {
342 }
343 
344 KUserGroup::KUserGroup(const char *_name)
345  : d(new Private(QLatin1String(_name)))
346 {
347 }
348 
349 KUserGroup::KUserGroup(const KUserGroup &group)
350  : d(group.d)
351 {
352 }
353 
354 KUserGroup& KUserGroup::operator =(const KUserGroup &group)
355 {
356  d = group.d;
357  return *this;
358 }
359 
360 bool KUserGroup::operator==(const KUserGroup &group) const
361 {
362  if (d->groupInfo == NULL || group.d->groupInfo == NULL) {
363  return false;
364  }
365  return wcscmp(d->groupInfo->grpi0_name, group.d->groupInfo->grpi0_name) == 0;
366 }
367 
368 bool KUserGroup::operator!=(const KUserGroup &group) const
369 {
370  return !operator==(group);
371 }
372 
373 bool KUserGroup::isValid() const
374 {
375  return d->groupInfo != NULL;
376 }
377 
378 QString KUserGroup::name() const
379 {
380  if(d && d->groupInfo)
381  return QString::fromUtf16((ushort *) d->groupInfo->grpi0_name);
382  return QString();
383 }
384 
385 QList<KUser> KUserGroup::users() const
386 {
387  QList<KUser> Result;
388 
389  Q_FOREACH(const QString &user, userNames()) {
390  Result.append(KUser(user));
391  }
392 
393  return Result;
394 }
395 
396 QStringList KUserGroup::userNames() const
397 {
398  QStringList result;
399 
400  if (!d->groupInfo) {
401  return result;
402  }
403 
404  PGROUP_USERS_INFO_0 pUsers = NULL;
405  DWORD dwEntriesRead = 0;
406  DWORD dwTotalEntries = 0;
407  NET_API_STATUS nStatus;
408 
409  nStatus = NetGroupGetUsers(NULL, d->groupInfo->grpi0_name, 0, (LPBYTE *) &pUsers, MAX_PREFERRED_LENGTH, &dwEntriesRead, &dwTotalEntries, NULL);
410 
411  if (nStatus == NERR_Success) {
412  for (DWORD i = 0; i < dwEntriesRead; ++i) {
413  result.append(QString::fromUtf16((ushort *) pUsers[i].grui0_name));
414  }
415  }
416 
417  if (pUsers) {
418  NetApiBufferFree(pUsers);
419  }
420 
421  return result;
422 }
423 
424 QList<KUserGroup> KUserGroup::allGroups()
425 {
426  QList<KUserGroup> result;
427 
428  NET_API_STATUS nStatus;
429  PGROUP_INFO_0 pGroup=NULL;
430  DWORD dwEntriesRead=0;
431  DWORD dwTotalEntries=0;
432  DWORD dwResumeHandle=0;
433 
434  KUserGroup tmp("");
435 
436  do {
437  nStatus = NetGroupEnum(NULL, 0, (LPBYTE*) &pGroup, 1, &dwEntriesRead, &dwTotalEntries, (PDWORD_PTR)&dwResumeHandle);
438 
439  if ((nStatus == NERR_Success || nStatus == ERROR_MORE_DATA) && dwEntriesRead > 0) {
440  tmp.d = new Private(pGroup);
441  result.append(tmp);
442  }
443  } while (nStatus == ERROR_MORE_DATA);
444 
445  return result;
446 }
447 
448 QStringList KUserGroup::allGroupNames()
449 {
450  QStringList result;
451 
452  NET_API_STATUS nStatus;
453  PGROUP_INFO_0 pGroups=NULL;
454  DWORD dwEntriesRead=0;
455  DWORD dwTotalEntries=0;
456 
457  nStatus = NetGroupEnum(NULL, 0, (LPBYTE*) &pGroups, MAX_PREFERRED_LENGTH, &dwEntriesRead, &dwTotalEntries, NULL);
458 
459  if (nStatus == NERR_Success) {
460  for (DWORD i = 0; i < dwEntriesRead; ++i) {
461  result.append(QString::fromUtf16((ushort *) pGroups[i].grpi0_name));
462  }
463  }
464 
465  if (pGroups) {
466  NetApiBufferFree(pGroups);
467  }
468 
469  return result;
470 }
471 
472 KUserGroup::~KUserGroup()
473 {
474 }
QVariant
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::uid
K_UID uid() const
Returns the user id of the user.
Definition: kuser_unix.cpp:132
KMacroExpander::group
Definition: kmacroexpander_unix.cpp:34
KUser::property
QVariant property(UserProperty which) const
Returns an extended property.
Definition: kuser_unix.cpp:199
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
QString
KUser::operator!=
bool operator!=(const KUser &user) const
Two KUser objects are not equal if uid() are not identical.
Definition: kuser_unix.cpp:124
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::allUserNames
static QStringList allUserNames()
Returns all user names of the system.
Definition: kuser_unix.cpp:218
KUser::loginName
QString loginName() const
The login name of the user.
Definition: kuser_unix.cpp:144
QStringList
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
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
KUserGroup::userNames
QStringList userNames() const
Returns a list of all user login names of the group.
Definition: kuser_unix.cpp:320
KUser::homeDir
QString homeDir() const
The path to the user's home directory.
Definition: kuser_unix.cpp:154
QSharedData
KUser::shell
QString shell() const
The path to the user's login shell.
Definition: kuser_unix.cpp:169
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::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
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
QList
Definition: kaboutdata.h:33
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:47:09 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
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • 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