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.

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

mercurial