2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07: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 00:40:54 -08:00
|
|
|
#include "nsNativeCharsetUtils.h"
|
2007-03-22 10:30:00 -07: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
|
|
|
|
nsUserInfo::GetFullname(PRUnichar **aFullname)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
struct passwd *pw = nullptr;
|
2007-03-22 10:30:00 -07: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-01 19:35:17 -07:00
|
|
|
nsAutoCString fullname(pw->PW_GECOS);
|
2007-03-22 10:30:00 -07: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 08:56:38 -07:00
|
|
|
int32_t index;
|
2007-03-22 10:30:00 -07:00
|
|
|
if ((index = fullname.Find(",")) != kNotFound)
|
|
|
|
fullname.Truncate(index);
|
|
|
|
|
|
|
|
// replace ampersand with username
|
|
|
|
if (pw->pw_name) {
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString username(pw->pw_name);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!username.IsEmpty() && nsCRT::IsLower(username.CharAt(0)))
|
|
|
|
username.SetCharAt(nsCRT::ToUpper(username.CharAt(0)), 0);
|
|
|
|
|
|
|
|
fullname.ReplaceSubstring("&", username.get());
|
|
|
|
}
|
|
|
|
|
2009-01-06 00:40:54 -08:00
|
|
|
nsAutoString unicodeFullname;
|
|
|
|
NS_CopyNativeToUnicode(fullname, unicodeFullname);
|
|
|
|
|
|
|
|
*aFullname = ToNewUnicode(unicodeFullname);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (*aFullname)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsUserInfo::GetUsername(char * *aUsername)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
struct passwd *pw = nullptr;
|
2007-03-22 10:30:00 -07: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
|
|
|
|
|
|
|
|
*aUsername = nsCRT::strdup(pw->pw_name);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsUserInfo::GetDomain(char * *aDomain)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
struct utsname buf;
|
2012-07-30 07:20:58 -07:00
|
|
|
char *domainname = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// is this portable? that is a POSIX compliant call, but I need to check
|
|
|
|
if (uname(&buf)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(HAVE_UNAME_DOMAINNAME_FIELD)
|
|
|
|
domainname = buf.domainname;
|
|
|
|
#elif defined(HAVE_UNAME_US_DOMAINNAME_FIELD)
|
|
|
|
domainname = buf.__domainname;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (domainname && domainname[0]) {
|
|
|
|
*aDomain = nsCRT::strdup(domainname);
|
|
|
|
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) {
|
|
|
|
*aDomain = nsCRT::strdup(pos+1);
|
|
|
|
rv = NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsUserInfo::GetEmailAddress(char * *aEmailAddress)
|
|
|
|
{
|
|
|
|
// use username + "@" + domain for the email address
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString emailAddress;
|
2007-03-22 10:30:00 -07: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;
|
|
|
|
}
|
|
|
|
|