1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/startup/nsUserInfoUnix.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,168 @@ 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 +#include "nsCRT.h" 1.11 + 1.12 +#include <pwd.h> 1.13 +#include <sys/types.h> 1.14 +#include <unistd.h> 1.15 +#include <sys/utsname.h> 1.16 + 1.17 +#include "nsString.h" 1.18 +#include "nsXPIDLString.h" 1.19 +#include "nsReadableUtils.h" 1.20 +#include "nsNativeCharsetUtils.h" 1.21 + 1.22 +/* Some UNIXy platforms don't have pw_gecos. In this case we use pw_name */ 1.23 +#if defined(NO_PW_GECOS) 1.24 +#define PW_GECOS pw_name 1.25 +#else 1.26 +#define PW_GECOS pw_gecos 1.27 +#endif 1.28 + 1.29 +nsUserInfo::nsUserInfo() 1.30 +{ 1.31 +} 1.32 + 1.33 +nsUserInfo::~nsUserInfo() 1.34 +{ 1.35 +} 1.36 + 1.37 +NS_IMPL_ISUPPORTS(nsUserInfo,nsIUserInfo) 1.38 + 1.39 +NS_IMETHODIMP 1.40 +nsUserInfo::GetFullname(char16_t **aFullname) 1.41 +{ 1.42 + struct passwd *pw = nullptr; 1.43 + 1.44 + pw = getpwuid (geteuid()); 1.45 + 1.46 + if (!pw || !pw->PW_GECOS) return NS_ERROR_FAILURE; 1.47 + 1.48 +#ifdef DEBUG_sspitzer 1.49 + printf("fullname = %s\n", pw->PW_GECOS); 1.50 +#endif 1.51 + 1.52 + nsAutoCString fullname(pw->PW_GECOS); 1.53 + 1.54 + // now try to parse the GECOS information, which will be in the form 1.55 + // Full Name, <other stuff> - eliminate the ", <other stuff> 1.56 + // also, sometimes GECOS uses "&" to mean "the user name" so do 1.57 + // the appropriate substitution 1.58 + 1.59 + // truncate at first comma (field delimiter) 1.60 + int32_t index; 1.61 + if ((index = fullname.Find(",")) != kNotFound) 1.62 + fullname.Truncate(index); 1.63 + 1.64 + // replace ampersand with username 1.65 + if (pw->pw_name) { 1.66 + nsAutoCString username(pw->pw_name); 1.67 + if (!username.IsEmpty() && nsCRT::IsLower(username.CharAt(0))) 1.68 + username.SetCharAt(nsCRT::ToUpper(username.CharAt(0)), 0); 1.69 + 1.70 + fullname.ReplaceSubstring("&", username.get()); 1.71 + } 1.72 + 1.73 + nsAutoString unicodeFullname; 1.74 + NS_CopyNativeToUnicode(fullname, unicodeFullname); 1.75 + 1.76 + *aFullname = ToNewUnicode(unicodeFullname); 1.77 + 1.78 + if (*aFullname) 1.79 + return NS_OK; 1.80 + 1.81 + return NS_ERROR_FAILURE; 1.82 +} 1.83 + 1.84 +NS_IMETHODIMP 1.85 +nsUserInfo::GetUsername(char * *aUsername) 1.86 +{ 1.87 + struct passwd *pw = nullptr; 1.88 + 1.89 + // is this portable? those are POSIX compliant calls, but I need to check 1.90 + pw = getpwuid(geteuid()); 1.91 + 1.92 + if (!pw || !pw->pw_name) return NS_ERROR_FAILURE; 1.93 + 1.94 +#ifdef DEBUG_sspitzer 1.95 + printf("username = %s\n", pw->pw_name); 1.96 +#endif 1.97 + 1.98 + *aUsername = strdup(pw->pw_name); 1.99 + 1.100 + return NS_OK; 1.101 +} 1.102 + 1.103 +NS_IMETHODIMP 1.104 +nsUserInfo::GetDomain(char * *aDomain) 1.105 +{ 1.106 + nsresult rv = NS_ERROR_FAILURE; 1.107 + 1.108 + struct utsname buf; 1.109 + char *domainname = nullptr; 1.110 + 1.111 + // is this portable? that is a POSIX compliant call, but I need to check 1.112 + if (uname(&buf)) { 1.113 + return rv; 1.114 + } 1.115 + 1.116 +#if defined(__linux__) 1.117 + domainname = buf.domainname; 1.118 +#endif 1.119 + 1.120 + if (domainname && domainname[0]) { 1.121 + *aDomain = strdup(domainname); 1.122 + rv = NS_OK; 1.123 + } 1.124 + else { 1.125 + // try to get the hostname from the nodename 1.126 + // on machines that use DHCP, domainname may not be set 1.127 + // but the nodename might. 1.128 + if (buf.nodename && buf.nodename[0]) { 1.129 + // if the nodename is foo.bar.org, use bar.org as the domain 1.130 + char *pos = strchr(buf.nodename,'.'); 1.131 + if (pos) { 1.132 + *aDomain = strdup(pos+1); 1.133 + rv = NS_OK; 1.134 + } 1.135 + } 1.136 + } 1.137 + 1.138 + return rv; 1.139 +} 1.140 + 1.141 +NS_IMETHODIMP 1.142 +nsUserInfo::GetEmailAddress(char * *aEmailAddress) 1.143 +{ 1.144 + // use username + "@" + domain for the email address 1.145 + 1.146 + nsresult rv; 1.147 + 1.148 + nsAutoCString emailAddress; 1.149 + nsXPIDLCString username; 1.150 + nsXPIDLCString domain; 1.151 + 1.152 + rv = GetUsername(getter_Copies(username)); 1.153 + if (NS_FAILED(rv)) return rv; 1.154 + 1.155 + rv = GetDomain(getter_Copies(domain)); 1.156 + if (NS_FAILED(rv)) return rv; 1.157 + 1.158 + if (!username.IsEmpty() && !domain.IsEmpty()) { 1.159 + emailAddress = (const char *)username; 1.160 + emailAddress += "@"; 1.161 + emailAddress += (const char *)domain; 1.162 + } 1.163 + else { 1.164 + return NS_ERROR_FAILURE; 1.165 + } 1.166 + 1.167 + *aEmailAddress = ToNewCString(emailAddress); 1.168 + 1.169 + return NS_OK; 1.170 +} 1.171 +