|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #include "nsUserInfo.h" |
|
7 |
|
8 #include "mozilla/ArrayUtils.h" // ArrayLength |
|
9 #include "nsString.h" |
|
10 #include "windows.h" |
|
11 #include "nsCRT.h" |
|
12 #include "nsXPIDLString.h" |
|
13 |
|
14 #define SECURITY_WIN32 |
|
15 #include "lm.h" |
|
16 #include "security.h" |
|
17 |
|
18 nsUserInfo::nsUserInfo() |
|
19 { |
|
20 } |
|
21 |
|
22 nsUserInfo::~nsUserInfo() |
|
23 { |
|
24 } |
|
25 |
|
26 NS_IMPL_ISUPPORTS(nsUserInfo, nsIUserInfo) |
|
27 |
|
28 NS_IMETHODIMP |
|
29 nsUserInfo::GetUsername(char **aUsername) |
|
30 { |
|
31 NS_ENSURE_ARG_POINTER(aUsername); |
|
32 *aUsername = nullptr; |
|
33 |
|
34 // ULEN is the max username length as defined in lmcons.h |
|
35 wchar_t username[UNLEN +1]; |
|
36 DWORD size = mozilla::ArrayLength(username); |
|
37 if (!GetUserNameW(username, &size)) |
|
38 return NS_ERROR_FAILURE; |
|
39 |
|
40 *aUsername = ToNewUTF8String(nsDependentString(username)); |
|
41 return (*aUsername) ? NS_OK : NS_ERROR_FAILURE; |
|
42 } |
|
43 |
|
44 NS_IMETHODIMP |
|
45 nsUserInfo::GetFullname(char16_t **aFullname) |
|
46 { |
|
47 NS_ENSURE_ARG_POINTER(aFullname); |
|
48 *aFullname = nullptr; |
|
49 |
|
50 wchar_t fullName[512]; |
|
51 DWORD size = mozilla::ArrayLength(fullName); |
|
52 |
|
53 if (GetUserNameExW(NameDisplay, fullName, &size)) { |
|
54 *aFullname = ToNewUnicode(nsDependentString(fullName)); |
|
55 } else { |
|
56 DWORD getUsernameError = GetLastError(); |
|
57 |
|
58 // Try to use the net APIs regardless of the error because it may be |
|
59 // able to obtain the information. |
|
60 wchar_t username[UNLEN + 1]; |
|
61 size = mozilla::ArrayLength(username); |
|
62 if (!GetUserNameW(username, &size)) { |
|
63 // ERROR_NONE_MAPPED means the user info is not filled out on this computer |
|
64 return getUsernameError == ERROR_NONE_MAPPED ? |
|
65 NS_ERROR_NOT_AVAILABLE : NS_ERROR_FAILURE; |
|
66 } |
|
67 |
|
68 const DWORD level = 2; |
|
69 LPBYTE info; |
|
70 // If the NetUserGetInfo function has no full name info it will return |
|
71 // success with an empty string. |
|
72 NET_API_STATUS status = NetUserGetInfo(nullptr, username, level, &info); |
|
73 if (status != NERR_Success) { |
|
74 // We have an error with NetUserGetInfo but we know the info is not |
|
75 // filled in because GetUserNameExW returned ERROR_NONE_MAPPED. |
|
76 return getUsernameError == ERROR_NONE_MAPPED ? |
|
77 NS_ERROR_NOT_AVAILABLE : NS_ERROR_FAILURE; |
|
78 } |
|
79 |
|
80 nsDependentString fullName = |
|
81 nsDependentString(reinterpret_cast<USER_INFO_2 *>(info)->usri2_full_name); |
|
82 |
|
83 // NetUserGetInfo returns an empty string if the full name is not filled out |
|
84 if (fullName.Length() == 0) { |
|
85 NetApiBufferFree(info); |
|
86 return NS_ERROR_NOT_AVAILABLE; |
|
87 } |
|
88 |
|
89 *aFullname = ToNewUnicode(fullName); |
|
90 NetApiBufferFree(info); |
|
91 } |
|
92 |
|
93 return (*aFullname) ? NS_OK : NS_ERROR_FAILURE; |
|
94 } |
|
95 |
|
96 NS_IMETHODIMP |
|
97 nsUserInfo::GetDomain(char **aDomain) |
|
98 { |
|
99 NS_ENSURE_ARG_POINTER(aDomain); |
|
100 *aDomain = nullptr; |
|
101 |
|
102 const DWORD level = 100; |
|
103 LPBYTE info; |
|
104 NET_API_STATUS status = NetWkstaGetInfo(nullptr, level, &info); |
|
105 if (status == NERR_Success) { |
|
106 *aDomain = |
|
107 ToNewUTF8String(nsDependentString(reinterpret_cast<WKSTA_INFO_100 *>(info)-> |
|
108 wki100_langroup)); |
|
109 NetApiBufferFree(info); |
|
110 } |
|
111 |
|
112 return (*aDomain) ? NS_OK : NS_ERROR_FAILURE; |
|
113 } |
|
114 |
|
115 NS_IMETHODIMP |
|
116 nsUserInfo::GetEmailAddress(char **aEmailAddress) |
|
117 { |
|
118 NS_ENSURE_ARG_POINTER(aEmailAddress); |
|
119 *aEmailAddress = nullptr; |
|
120 |
|
121 // RFC3696 says max length of an email address is 254 |
|
122 wchar_t emailAddress[255]; |
|
123 DWORD size = mozilla::ArrayLength(emailAddress); |
|
124 |
|
125 if (!GetUserNameExW(NameUserPrincipal, emailAddress, &size)) { |
|
126 DWORD getUsernameError = GetLastError(); |
|
127 return getUsernameError == ERROR_NONE_MAPPED ? |
|
128 NS_ERROR_NOT_AVAILABLE : NS_ERROR_FAILURE; |
|
129 } |
|
130 |
|
131 *aEmailAddress = ToNewUTF8String(nsDependentString(emailAddress)); |
|
132 return (*aEmailAddress) ? NS_OK : NS_ERROR_FAILURE; |
|
133 } |