Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/toolkit/components/startup/nsUserInfoUnix.cpp
Line
Count
Source (jump to first uncovered line)
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 "nsReadableUtils.h"
16
#include "nsNativeCharsetUtils.h"
17
18
/* Some UNIXy platforms don't have pw_gecos. In this case we use pw_name */
19
#if defined(NO_PW_GECOS)
20
#define PW_GECOS pw_name
21
#else
22
0
#define PW_GECOS pw_gecos
23
#endif
24
25
nsUserInfo::nsUserInfo()
26
0
{
27
0
}
28
29
nsUserInfo::~nsUserInfo()
30
0
{
31
0
}
32
33
NS_IMPL_ISUPPORTS(nsUserInfo,nsIUserInfo)
34
35
NS_IMETHODIMP
36
nsUserInfo::GetFullname(nsAString& aFullname)
37
0
{
38
0
    struct passwd *pw = nullptr;
39
0
40
0
    pw = getpwuid (geteuid());
41
0
42
0
    if (!pw || !pw->PW_GECOS) return NS_ERROR_FAILURE;
43
0
44
#ifdef DEBUG_sspitzer
45
    printf("fullname = %s\n", pw->PW_GECOS);
46
#endif
47
48
0
    nsAutoCString fullname(pw->PW_GECOS);
49
0
50
0
    // now try to parse the GECOS information, which will be in the form
51
0
    // Full Name, <other stuff> - eliminate the ", <other stuff>
52
0
    // also, sometimes GECOS uses "&" to mean "the user name" so do
53
0
    // the appropriate substitution
54
0
55
0
    // truncate at first comma (field delimiter)
56
0
    int32_t index;
57
0
    if ((index = fullname.Find(",")) != kNotFound)
58
0
        fullname.Truncate(index);
59
0
60
0
    // replace ampersand with username
61
0
    if (pw->pw_name) {
62
0
        nsAutoCString username(pw->pw_name);
63
0
        if (!username.IsEmpty() && nsCRT::IsLower(username.CharAt(0)))
64
0
            username.SetCharAt(nsCRT::ToUpper(username.CharAt(0)), 0);
65
0
66
0
        fullname.ReplaceSubstring("&", username.get());
67
0
    }
68
0
69
0
    NS_CopyNativeToUnicode(fullname, aFullname);
70
0
71
0
    return NS_OK;
72
0
}
73
74
NS_IMETHODIMP
75
nsUserInfo::GetUsername(nsACString& aUsername)
76
0
{
77
0
    struct passwd *pw = nullptr;
78
0
79
0
    // is this portable?  those are POSIX compliant calls, but I need to check
80
0
    pw = getpwuid(geteuid());
81
0
82
0
    if (!pw || !pw->pw_name) return NS_ERROR_FAILURE;
83
0
84
#ifdef DEBUG_sspitzer
85
    printf("username = %s\n", pw->pw_name);
86
#endif
87
88
0
    aUsername.Assign(pw->pw_name);
89
0
90
0
    return NS_OK;
91
0
}
92
93
NS_IMETHODIMP
94
nsUserInfo::GetDomain(nsACString& aDomain)
95
0
{
96
0
    nsresult rv = NS_ERROR_FAILURE;
97
0
98
0
    struct utsname buf;
99
0
    char *domainname = nullptr;
100
0
101
0
    if (uname(&buf) < 0) {
102
0
        return rv;
103
0
    }
104
0
105
0
#if defined(__linux__)
106
0
    domainname = buf.domainname;
107
0
#endif
108
0
109
0
    if (domainname && domainname[0]) {
110
0
        aDomain.Assign(domainname);
111
0
        rv = NS_OK;
112
0
    }
113
0
    else {
114
0
        // try to get the hostname from the nodename
115
0
        // on machines that use DHCP, domainname may not be set
116
0
        // but the nodename might.
117
0
        if (buf.nodename[0]) {
118
0
            // if the nodename is foo.bar.org, use bar.org as the domain
119
0
            char *pos = strchr(buf.nodename,'.');
120
0
            if (pos) {
121
0
                aDomain.Assign(pos + 1);
122
0
                rv = NS_OK;
123
0
            }
124
0
        }
125
0
    }
126
0
127
0
    return rv;
128
0
}
129
130
NS_IMETHODIMP
131
nsUserInfo::GetEmailAddress(nsACString& aEmailAddress)
132
0
{
133
0
    // use username + "@" + domain for the email address
134
0
    nsresult rv;
135
0
136
0
    nsCString username;
137
0
    nsCString domain;
138
0
139
0
    rv = GetUsername(username);
140
0
    if (NS_FAILED(rv)) return rv;
141
0
142
0
    rv = GetDomain(domain);
143
0
    if (NS_FAILED(rv)) return rv;
144
0
145
0
    if (username.IsEmpty() || domain.IsEmpty()) {
146
0
        return NS_ERROR_FAILURE;
147
0
    }
148
0
149
0
    aEmailAddress = username + NS_LITERAL_CSTRING("@") + domain;
150
0
    return NS_OK;
151
0
}
152