2004-10-29 23:28:38 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2004-10-29 23:28:38 +04:00
|
|
|
|
|
|
|
#include "nsUserInfo.h"
|
|
|
|
#include "nsCRT.h"
|
|
|
|
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/utsname.h>
|
|
|
|
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsXPIDLString.h"
|
|
|
|
#include "nsReadableUtils.h"
|
2009-01-06 11:40:54 +03:00
|
|
|
#include "nsNativeCharsetUtils.h"
|
2004-10-29 23:28:38 +04:00
|
|
|
|
|
|
|
/* Some UNIXy platforms don't have pw_gecos. In this case we use pw_name */
|
|
|
|
#if defined(NO_PW_GECOS)
|
|
|
|
#define PW_GECOS pw_name
|
|
|
|
#else
|
|
|
|
#define PW_GECOS pw_gecos
|
|
|
|
#endif
|
|
|
|
|
|
|
|
nsUserInfo::nsUserInfo()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsUserInfo::~nsUserInfo()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(nsUserInfo,nsIUserInfo)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-01-04 19:02:17 +04:00
|
|
|
nsUserInfo::GetFullname(char16_t **aFullname)
|
2004-10-29 23:28:38 +04:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
struct passwd *pw = nullptr;
|
2004-10-29 23:28:38 +04:00
|
|
|
|
|
|
|
pw = getpwuid (geteuid());
|
|
|
|
|
|
|
|
if (!pw || !pw->PW_GECOS) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
#ifdef DEBUG_sspitzer
|
|
|
|
printf("fullname = %s\n", pw->PW_GECOS);
|
|
|
|
#endif
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString fullname(pw->PW_GECOS);
|
2004-10-29 23:28:38 +04:00
|
|
|
|
|
|
|
// now try to parse the GECOS information, which will be in the form
|
|
|
|
// Full Name, <other stuff> - eliminate the ", <other stuff>
|
|
|
|
// also, sometimes GECOS uses "&" to mean "the user name" so do
|
|
|
|
// the appropriate substitution
|
|
|
|
|
|
|
|
// truncate at first comma (field delimiter)
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t index;
|
2004-10-29 23:28:38 +04:00
|
|
|
if ((index = fullname.Find(",")) != kNotFound)
|
|
|
|
fullname.Truncate(index);
|
|
|
|
|
|
|
|
// replace ampersand with username
|
|
|
|
if (pw->pw_name) {
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString username(pw->pw_name);
|
2004-10-29 23:28:38 +04:00
|
|
|
if (!username.IsEmpty() && nsCRT::IsLower(username.CharAt(0)))
|
|
|
|
username.SetCharAt(nsCRT::ToUpper(username.CharAt(0)), 0);
|
|
|
|
|
|
|
|
fullname.ReplaceSubstring("&", username.get());
|
|
|
|
}
|
|
|
|
|
2009-01-06 11:40:54 +03:00
|
|
|
nsAutoString unicodeFullname;
|
|
|
|
NS_CopyNativeToUnicode(fullname, unicodeFullname);
|
|
|
|
|
|
|
|
*aFullname = ToNewUnicode(unicodeFullname);
|
2004-10-29 23:28:38 +04:00
|
|
|
|
|
|
|
if (*aFullname)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsUserInfo::GetUsername(char * *aUsername)
|
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
struct passwd *pw = nullptr;
|
2004-10-29 23:28:38 +04:00
|
|
|
|
|
|
|
// is this portable? those are POSIX compliant calls, but I need to check
|
|
|
|
pw = getpwuid(geteuid());
|
|
|
|
|
|
|
|
if (!pw || !pw->pw_name) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
#ifdef DEBUG_sspitzer
|
|
|
|
printf("username = %s\n", pw->pw_name);
|
|
|
|
#endif
|
|
|
|
|
2013-08-22 14:31:48 +04:00
|
|
|
*aUsername = strdup(pw->pw_name);
|
2004-10-29 23:28:38 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsUserInfo::GetDomain(char * *aDomain)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
struct utsname buf;
|
2012-07-30 18:20:58 +04:00
|
|
|
char *domainname = nullptr;
|
2004-10-29 23:28:38 +04:00
|
|
|
|
|
|
|
// is this portable? that is a POSIX compliant call, but I need to check
|
|
|
|
if (uname(&buf)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2013-10-10 05:36:45 +04:00
|
|
|
#if defined(__linux__)
|
2004-10-29 23:28:38 +04:00
|
|
|
domainname = buf.domainname;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (domainname && domainname[0]) {
|
2013-08-22 14:31:48 +04:00
|
|
|
*aDomain = strdup(domainname);
|
2004-10-29 23:28:38 +04:00
|
|
|
rv = NS_OK;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// try to get the hostname from the nodename
|
|
|
|
// on machines that use DHCP, domainname may not be set
|
|
|
|
// but the nodename might.
|
|
|
|
if (buf.nodename && buf.nodename[0]) {
|
|
|
|
// if the nodename is foo.bar.org, use bar.org as the domain
|
|
|
|
char *pos = strchr(buf.nodename,'.');
|
|
|
|
if (pos) {
|
2013-08-22 14:31:48 +04:00
|
|
|
*aDomain = strdup(pos+1);
|
2004-10-29 23:28:38 +04:00
|
|
|
rv = NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsUserInfo::GetEmailAddress(char * *aEmailAddress)
|
|
|
|
{
|
|
|
|
// use username + "@" + domain for the email address
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString emailAddress;
|
2004-10-29 23:28:38 +04:00
|
|
|
nsXPIDLCString username;
|
|
|
|
nsXPIDLCString domain;
|
|
|
|
|
|
|
|
rv = GetUsername(getter_Copies(username));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
rv = GetDomain(getter_Copies(domain));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
if (!username.IsEmpty() && !domain.IsEmpty()) {
|
|
|
|
emailAddress = (const char *)username;
|
|
|
|
emailAddress += "@";
|
|
|
|
emailAddress += (const char *)domain;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aEmailAddress = ToNewCString(emailAddress);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|