toolkit/components/startup/nsUserInfoUnix.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

     1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 #include "nsUserInfo.h"
     7 #include "nsCRT.h"
     9 #include <pwd.h>
    10 #include <sys/types.h>
    11 #include <unistd.h>
    12 #include <sys/utsname.h>
    14 #include "nsString.h"
    15 #include "nsXPIDLString.h"
    16 #include "nsReadableUtils.h"
    17 #include "nsNativeCharsetUtils.h"
    19 /* Some UNIXy platforms don't have pw_gecos. In this case we use pw_name */
    20 #if defined(NO_PW_GECOS)
    21 #define PW_GECOS pw_name
    22 #else
    23 #define PW_GECOS pw_gecos
    24 #endif
    26 nsUserInfo::nsUserInfo()
    27 {
    28 }
    30 nsUserInfo::~nsUserInfo()
    31 {
    32 }
    34 NS_IMPL_ISUPPORTS(nsUserInfo,nsIUserInfo)
    36 NS_IMETHODIMP
    37 nsUserInfo::GetFullname(char16_t **aFullname)
    38 {
    39     struct passwd *pw = nullptr;
    41     pw = getpwuid (geteuid());
    43     if (!pw || !pw->PW_GECOS) return NS_ERROR_FAILURE;
    45 #ifdef DEBUG_sspitzer
    46     printf("fullname = %s\n", pw->PW_GECOS);
    47 #endif
    49     nsAutoCString fullname(pw->PW_GECOS);
    51     // now try to parse the GECOS information, which will be in the form
    52     // Full Name, <other stuff> - eliminate the ", <other stuff>
    53     // also, sometimes GECOS uses "&" to mean "the user name" so do
    54     // the appropriate substitution
    56     // truncate at first comma (field delimiter)
    57     int32_t index;
    58     if ((index = fullname.Find(",")) != kNotFound)
    59         fullname.Truncate(index);
    61     // replace ampersand with username
    62     if (pw->pw_name) {
    63         nsAutoCString username(pw->pw_name);
    64         if (!username.IsEmpty() && nsCRT::IsLower(username.CharAt(0)))
    65             username.SetCharAt(nsCRT::ToUpper(username.CharAt(0)), 0);
    67         fullname.ReplaceSubstring("&", username.get());
    68     }
    70     nsAutoString unicodeFullname;
    71     NS_CopyNativeToUnicode(fullname, unicodeFullname);
    73     *aFullname = ToNewUnicode(unicodeFullname);
    75     if (*aFullname)
    76         return NS_OK;
    78     return NS_ERROR_FAILURE;
    79 }
    81 NS_IMETHODIMP 
    82 nsUserInfo::GetUsername(char * *aUsername)
    83 {
    84     struct passwd *pw = nullptr;
    86     // is this portable?  those are POSIX compliant calls, but I need to check
    87     pw = getpwuid(geteuid());
    89     if (!pw || !pw->pw_name) return NS_ERROR_FAILURE;
    91 #ifdef DEBUG_sspitzer
    92     printf("username = %s\n", pw->pw_name);
    93 #endif
    95     *aUsername = strdup(pw->pw_name);
    97     return NS_OK;
    98 }
   100 NS_IMETHODIMP 
   101 nsUserInfo::GetDomain(char * *aDomain)
   102 {
   103     nsresult rv = NS_ERROR_FAILURE;
   105     struct utsname buf;
   106     char *domainname = nullptr;
   108     // is this portable?  that is a POSIX compliant call, but I need to check
   109     if (uname(&buf)) { 
   110         return rv;
   111     }
   113 #if defined(__linux__)
   114     domainname = buf.domainname;
   115 #endif
   117     if (domainname && domainname[0]) {   
   118         *aDomain = strdup(domainname);
   119         rv = NS_OK;
   120     }
   121     else {
   122         // try to get the hostname from the nodename
   123         // on machines that use DHCP, domainname may not be set
   124         // but the nodename might.
   125         if (buf.nodename && buf.nodename[0]) {
   126             // if the nodename is foo.bar.org, use bar.org as the domain
   127             char *pos = strchr(buf.nodename,'.');
   128             if (pos) {
   129                 *aDomain = strdup(pos+1);
   130                 rv = NS_OK;
   131             }
   132         }
   133     }
   135     return rv;
   136 }
   138 NS_IMETHODIMP 
   139 nsUserInfo::GetEmailAddress(char * *aEmailAddress)
   140 {
   141     // use username + "@" + domain for the email address
   143     nsresult rv;
   145     nsAutoCString emailAddress;
   146     nsXPIDLCString username;
   147     nsXPIDLCString domain;
   149     rv = GetUsername(getter_Copies(username));
   150     if (NS_FAILED(rv)) return rv;
   152     rv = GetDomain(getter_Copies(domain));
   153     if (NS_FAILED(rv)) return rv;
   155     if (!username.IsEmpty() && !domain.IsEmpty()) {
   156         emailAddress = (const char *)username;
   157         emailAddress += "@";
   158         emailAddress += (const char *)domain;
   159     }
   160     else {
   161         return NS_ERROR_FAILURE;
   162     }
   164     *aEmailAddress = ToNewCString(emailAddress);
   166     return NS_OK;
   167 }

mercurial