added kdenlive
[feisty_meow.git] / nucleus / library / security / win32_security.cpp
1
2
3
4 /*****************************************************************************\
5 *                                                                             *
6 *  Name   : win32_security                                                    *
7 *  Author : Sue Richeson                                                      *
8 *  Author : Chris Koeritz                                                     *
9 *                                                                             *
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 \*****************************************************************************/
18
19 #ifdef _MSC_VER
20
21 #include "win32_security.h"
22
23 #include <basis/utf_conversion.h>
24 #include <basis/astring.h>
25
26 #include <comdef.h>
27 #include <lm.h>
28
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.
32
33 bool win32_security::GetUserAndDomainName(astring &UserName, astring &DomainName)
34 {
35    HANDLE hToken;
36
37    // Initialize the return parameters.
38    UserName = "";
39    DomainName = "";
40
41    #define MY_BUFSIZE 512  // highly unlikely to exceed 512 bytes
42    UCHAR InfoBuffer[ MY_BUFSIZE + 1 ];
43    DWORD cbInfoBuffer = MY_BUFSIZE;
44    SID_NAME_USE snu;
45
46    BOOL bSuccess;
47
48    if(!OpenThreadToken(
49        GetCurrentThread(),
50        TOKEN_QUERY,
51        TRUE,
52        &hToken
53        )) {
54
55        if(GetLastError() == ERROR_NO_TOKEN) {
56
57            // 
58            // attempt to open the process token, since no thread token
59            // exists
60            // 
61
62            if(!OpenProcessToken(
63                GetCurrentProcess(),
64                TOKEN_QUERY,
65                &hToken
66                )) return FALSE;
67
68        } else {
69
70            // 
71            // error trying to get thread token
72            // 
73
74            return FALSE;
75        }
76    }
77
78    bSuccess = GetTokenInformation( hToken,
79                                    TokenUser,
80                                    InfoBuffer,
81                                    cbInfoBuffer,
82                                    &cbInfoBuffer);
83
84    if(!bSuccess) {
85        if(GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
86
87            // 
88            // alloc buffer and try GetTokenInformation() again
89            // 
90
91            CloseHandle(hToken);
92            return FALSE;
93
94        } else {
95
96            // 
97            // error getting token info
98            // 
99
100            CloseHandle(hToken);
101            return FALSE;
102        }
103    }
104
105    CloseHandle(hToken);
106
107    TCHAR User[MY_BUFSIZE + 1];;
108    DWORD cchUserName = MY_BUFSIZE;
109    TCHAR Domain[MY_BUFSIZE +  1];
110    DWORD cchDomainName = MY_BUFSIZE;
111                       
112     bSuccess = LookupAccountSid(NULL,
113                            ((PTOKEN_USER)InfoBuffer)->User.Sid,
114                            User,
115                            &cchUserName,
116                            Domain,
117                            &cchDomainName,
118                            &snu);
119
120     if (bSuccess)
121     {
122         UserName = from_unicode_temp(User);
123         DomainName = from_unicode_temp(Domain);
124     }
125
126    return bSuccess;
127 }
128
129 astring win32_security::full_user()
130 {
131   astring user, temp_domain;
132   GetUserAndDomainName(user, temp_domain);
133   user += astring("[") + temp_domain + "]";
134   return user;
135 }
136
137 #endif // windows.
138
139
140
141