4 /*****************************************************************************\
6 * Name : win32_security *
7 * Author : Sue Richeson *
8 * Author : Chris Koeritz *
10 *******************************************************************************
11 * Copyright (c) 2000-$now By Author. This program is free software; you can *
12 * redistribute it and/or modify it under the terms of the GNU General Public *
13 * License as published by the Free Software Foundation; either version 2 of *
14 * the License or (at your option) any later version. This is online at: *
15 * http://www.fsf.org/copyleft/gpl.html *
16 * Please send any updates to: fred@gruntose.com *
17 \*****************************************************************************/
21 #include "win32_security.h"
23 #include <basis/utf_conversion.h>
24 #include <basis/astring.h>
29 // This piece of code is borrowed from the following July 1999 MSDN article
30 // HOWTO: Look Up Current User Name and Domain Name, ID: Q155698
31 //NOTE: It has been modified for inclusion here and it is Win32-specific.
33 bool win32_security::GetUserAndDomainName(astring &UserName, astring &DomainName)
37 // Initialize the return parameters.
41 #define MY_BUFSIZE 512 // highly unlikely to exceed 512 bytes
42 UCHAR InfoBuffer[ MY_BUFSIZE + 1 ];
43 DWORD cbInfoBuffer = MY_BUFSIZE;
55 if(GetLastError() == ERROR_NO_TOKEN) {
58 // attempt to open the process token, since no thread token
71 // error trying to get thread token
78 bSuccess = GetTokenInformation( hToken,
85 if(GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
88 // alloc buffer and try GetTokenInformation() again
97 // error getting token info
107 TCHAR User[MY_BUFSIZE + 1];;
108 DWORD cchUserName = MY_BUFSIZE;
109 TCHAR Domain[MY_BUFSIZE + 1];
110 DWORD cchDomainName = MY_BUFSIZE;
112 bSuccess = LookupAccountSid(NULL,
113 ((PTOKEN_USER)InfoBuffer)->User.Sid,
122 UserName = from_unicode_temp(User);
123 DomainName = from_unicode_temp(Domain);
129 astring win32_security::full_user()
131 astring user, temp_domain;
132 GetUserAndDomainName(user, temp_domain);
133 user += astring("[") + temp_domain + "]";