summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/base/src/nsUserInfoWin.cpp
blob: a5b69cf9a2c9957aad44e061dfc0bcdb86c5600a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "nsUserInfo.h"

#include "mozilla/ArrayUtils.h"  // ArrayLength
#include "nsString.h"
#include "windows.h"
#include "nsCRT.h"

#define SECURITY_WIN32
#include "lm.h"
#include "security.h"

nsUserInfo::nsUserInfo() {}

nsUserInfo::~nsUserInfo() {}

NS_IMPL_ISUPPORTS(nsUserInfo, nsIUserInfo)

NS_IMETHODIMP
nsUserInfo::GetUsername(nsAString& aUsername) {
  aUsername.Truncate();

  // UNLEN is the max username length as defined in lmcons.h
  wchar_t username[UNLEN + 1];
  DWORD size = mozilla::ArrayLength(username);
  if (!GetUserNameW(username, &size)) return NS_OK;

  aUsername.Assign(username);
  return NS_OK;
}

NS_IMETHODIMP
nsUserInfo::GetFullname(nsAString& aFullname) {
  aFullname.Truncate();

  wchar_t fullName[512];
  DWORD size = mozilla::ArrayLength(fullName);

  if (GetUserNameExW(NameDisplay, fullName, &size)) {
    aFullname.Assign(fullName);
  } else {
    // Try to use the net APIs regardless of the error because it may be
    // able to obtain the information.
    wchar_t username[UNLEN + 1];
    size = mozilla::ArrayLength(username);
    if (!GetUserNameW(username, &size)) {
      return NS_OK;
    }

    const DWORD level = 2;
    LPBYTE info;
    // If the NetUserGetInfo function has no full name info it will return
    // success with an empty string.
    NET_API_STATUS status = NetUserGetInfo(nullptr, username, level, &info);
    if (status != NERR_Success) {
      return NS_OK;
    }

    aFullname.Assign(reinterpret_cast<USER_INFO_2*>(info)->usri2_full_name);
    NetApiBufferFree(info);
  }

  return NS_OK;
}

NS_IMETHODIMP
nsUserInfo::GetDomain(nsAString& aDomain) {
  aDomain.Truncate();

  const DWORD level = 100;
  LPBYTE info;
  NET_API_STATUS status = NetWkstaGetInfo(nullptr, level, &info);
  if (status == NERR_Success) {
    aDomain.Assign(reinterpret_cast<WKSTA_INFO_100*>(info)->wki100_langroup);
    NetApiBufferFree(info);
  }

  return NS_OK;
}

NS_IMETHODIMP
nsUserInfo::GetEmailAddress(nsAString& aEmailAddress) {
  aEmailAddress.Truncate();

  // RFC3696 says max length of an email address is 254
  wchar_t emailAddress[255];
  DWORD size = mozilla::ArrayLength(emailAddress);

  if (!GetUserNameExW(NameUserPrincipal, emailAddress, &size)) {
    return NS_OK;
  }

  aEmailAddress.Assign(emailAddress);
  return NS_OK;
}