toolkit/components/startup/nsUserInfoWin.cpp

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

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

mercurial