1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/startup/nsUserInfoWin.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,133 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "nsUserInfo.h" 1.10 + 1.11 +#include "mozilla/ArrayUtils.h" // ArrayLength 1.12 +#include "nsString.h" 1.13 +#include "windows.h" 1.14 +#include "nsCRT.h" 1.15 +#include "nsXPIDLString.h" 1.16 + 1.17 +#define SECURITY_WIN32 1.18 +#include "lm.h" 1.19 +#include "security.h" 1.20 + 1.21 +nsUserInfo::nsUserInfo() 1.22 +{ 1.23 +} 1.24 + 1.25 +nsUserInfo::~nsUserInfo() 1.26 +{ 1.27 +} 1.28 + 1.29 +NS_IMPL_ISUPPORTS(nsUserInfo, nsIUserInfo) 1.30 + 1.31 +NS_IMETHODIMP 1.32 +nsUserInfo::GetUsername(char **aUsername) 1.33 +{ 1.34 + NS_ENSURE_ARG_POINTER(aUsername); 1.35 + *aUsername = nullptr; 1.36 + 1.37 + // ULEN is the max username length as defined in lmcons.h 1.38 + wchar_t username[UNLEN +1]; 1.39 + DWORD size = mozilla::ArrayLength(username); 1.40 + if (!GetUserNameW(username, &size)) 1.41 + return NS_ERROR_FAILURE; 1.42 + 1.43 + *aUsername = ToNewUTF8String(nsDependentString(username)); 1.44 + return (*aUsername) ? NS_OK : NS_ERROR_FAILURE; 1.45 +} 1.46 + 1.47 +NS_IMETHODIMP 1.48 +nsUserInfo::GetFullname(char16_t **aFullname) 1.49 +{ 1.50 + NS_ENSURE_ARG_POINTER(aFullname); 1.51 + *aFullname = nullptr; 1.52 + 1.53 + wchar_t fullName[512]; 1.54 + DWORD size = mozilla::ArrayLength(fullName); 1.55 + 1.56 + if (GetUserNameExW(NameDisplay, fullName, &size)) { 1.57 + *aFullname = ToNewUnicode(nsDependentString(fullName)); 1.58 + } else { 1.59 + DWORD getUsernameError = GetLastError(); 1.60 + 1.61 + // Try to use the net APIs regardless of the error because it may be 1.62 + // able to obtain the information. 1.63 + wchar_t username[UNLEN + 1]; 1.64 + size = mozilla::ArrayLength(username); 1.65 + if (!GetUserNameW(username, &size)) { 1.66 + // ERROR_NONE_MAPPED means the user info is not filled out on this computer 1.67 + return getUsernameError == ERROR_NONE_MAPPED ? 1.68 + NS_ERROR_NOT_AVAILABLE : NS_ERROR_FAILURE; 1.69 + } 1.70 + 1.71 + const DWORD level = 2; 1.72 + LPBYTE info; 1.73 + // If the NetUserGetInfo function has no full name info it will return 1.74 + // success with an empty string. 1.75 + NET_API_STATUS status = NetUserGetInfo(nullptr, username, level, &info); 1.76 + if (status != NERR_Success) { 1.77 + // We have an error with NetUserGetInfo but we know the info is not 1.78 + // filled in because GetUserNameExW returned ERROR_NONE_MAPPED. 1.79 + return getUsernameError == ERROR_NONE_MAPPED ? 1.80 + NS_ERROR_NOT_AVAILABLE : NS_ERROR_FAILURE; 1.81 + } 1.82 + 1.83 + nsDependentString fullName = 1.84 + nsDependentString(reinterpret_cast<USER_INFO_2 *>(info)->usri2_full_name); 1.85 + 1.86 + // NetUserGetInfo returns an empty string if the full name is not filled out 1.87 + if (fullName.Length() == 0) { 1.88 + NetApiBufferFree(info); 1.89 + return NS_ERROR_NOT_AVAILABLE; 1.90 + } 1.91 + 1.92 + *aFullname = ToNewUnicode(fullName); 1.93 + NetApiBufferFree(info); 1.94 + } 1.95 + 1.96 + return (*aFullname) ? NS_OK : NS_ERROR_FAILURE; 1.97 +} 1.98 + 1.99 +NS_IMETHODIMP 1.100 +nsUserInfo::GetDomain(char **aDomain) 1.101 +{ 1.102 + NS_ENSURE_ARG_POINTER(aDomain); 1.103 + *aDomain = nullptr; 1.104 + 1.105 + const DWORD level = 100; 1.106 + LPBYTE info; 1.107 + NET_API_STATUS status = NetWkstaGetInfo(nullptr, level, &info); 1.108 + if (status == NERR_Success) { 1.109 + *aDomain = 1.110 + ToNewUTF8String(nsDependentString(reinterpret_cast<WKSTA_INFO_100 *>(info)-> 1.111 + wki100_langroup)); 1.112 + NetApiBufferFree(info); 1.113 + } 1.114 + 1.115 + return (*aDomain) ? NS_OK : NS_ERROR_FAILURE; 1.116 +} 1.117 + 1.118 +NS_IMETHODIMP 1.119 +nsUserInfo::GetEmailAddress(char **aEmailAddress) 1.120 +{ 1.121 + NS_ENSURE_ARG_POINTER(aEmailAddress); 1.122 + *aEmailAddress = nullptr; 1.123 + 1.124 + // RFC3696 says max length of an email address is 254 1.125 + wchar_t emailAddress[255]; 1.126 + DWORD size = mozilla::ArrayLength(emailAddress); 1.127 + 1.128 + if (!GetUserNameExW(NameUserPrincipal, emailAddress, &size)) { 1.129 + DWORD getUsernameError = GetLastError(); 1.130 + return getUsernameError == ERROR_NONE_MAPPED ? 1.131 + NS_ERROR_NOT_AVAILABLE : NS_ERROR_FAILURE; 1.132 + } 1.133 + 1.134 + *aEmailAddress = ToNewUTF8String(nsDependentString(emailAddress)); 1.135 + return (*aEmailAddress) ? NS_OK : NS_ERROR_FAILURE; 1.136 +}