toolkit/components/startup/nsUserInfoUnix.cpp

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:24bca42c26de
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/. */
5
6 #include "nsUserInfo.h"
7 #include "nsCRT.h"
8
9 #include <pwd.h>
10 #include <sys/types.h>
11 #include <unistd.h>
12 #include <sys/utsname.h>
13
14 #include "nsString.h"
15 #include "nsXPIDLString.h"
16 #include "nsReadableUtils.h"
17 #include "nsNativeCharsetUtils.h"
18
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
25
26 nsUserInfo::nsUserInfo()
27 {
28 }
29
30 nsUserInfo::~nsUserInfo()
31 {
32 }
33
34 NS_IMPL_ISUPPORTS(nsUserInfo,nsIUserInfo)
35
36 NS_IMETHODIMP
37 nsUserInfo::GetFullname(char16_t **aFullname)
38 {
39 struct passwd *pw = nullptr;
40
41 pw = getpwuid (geteuid());
42
43 if (!pw || !pw->PW_GECOS) return NS_ERROR_FAILURE;
44
45 #ifdef DEBUG_sspitzer
46 printf("fullname = %s\n", pw->PW_GECOS);
47 #endif
48
49 nsAutoCString fullname(pw->PW_GECOS);
50
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
55
56 // truncate at first comma (field delimiter)
57 int32_t index;
58 if ((index = fullname.Find(",")) != kNotFound)
59 fullname.Truncate(index);
60
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);
66
67 fullname.ReplaceSubstring("&", username.get());
68 }
69
70 nsAutoString unicodeFullname;
71 NS_CopyNativeToUnicode(fullname, unicodeFullname);
72
73 *aFullname = ToNewUnicode(unicodeFullname);
74
75 if (*aFullname)
76 return NS_OK;
77
78 return NS_ERROR_FAILURE;
79 }
80
81 NS_IMETHODIMP
82 nsUserInfo::GetUsername(char * *aUsername)
83 {
84 struct passwd *pw = nullptr;
85
86 // is this portable? those are POSIX compliant calls, but I need to check
87 pw = getpwuid(geteuid());
88
89 if (!pw || !pw->pw_name) return NS_ERROR_FAILURE;
90
91 #ifdef DEBUG_sspitzer
92 printf("username = %s\n", pw->pw_name);
93 #endif
94
95 *aUsername = strdup(pw->pw_name);
96
97 return NS_OK;
98 }
99
100 NS_IMETHODIMP
101 nsUserInfo::GetDomain(char * *aDomain)
102 {
103 nsresult rv = NS_ERROR_FAILURE;
104
105 struct utsname buf;
106 char *domainname = nullptr;
107
108 // is this portable? that is a POSIX compliant call, but I need to check
109 if (uname(&buf)) {
110 return rv;
111 }
112
113 #if defined(__linux__)
114 domainname = buf.domainname;
115 #endif
116
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 }
134
135 return rv;
136 }
137
138 NS_IMETHODIMP
139 nsUserInfo::GetEmailAddress(char * *aEmailAddress)
140 {
141 // use username + "@" + domain for the email address
142
143 nsresult rv;
144
145 nsAutoCString emailAddress;
146 nsXPIDLCString username;
147 nsXPIDLCString domain;
148
149 rv = GetUsername(getter_Copies(username));
150 if (NS_FAILED(rv)) return rv;
151
152 rv = GetDomain(getter_Copies(domain));
153 if (NS_FAILED(rv)) return rv;
154
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 }
163
164 *aEmailAddress = ToNewCString(emailAddress);
165
166 return NS_OK;
167 }
168

mercurial