/
userinfo.go
251 lines (203 loc) · 5.83 KB
/
userinfo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
//
// Copyright © 2016-2021 Solus Project <copyright@getsol.us>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package builder
import (
"fmt"
"log/slog"
"os"
"os/user"
"path/filepath"
"strconv"
"gopkg.in/ini.v1"
)
// UserInfo is required for ypkg builds, to set the .config/solus/package internally
// and propagate the author details.
type UserInfo struct {
Name string // Actual name
Email string // Actual email
UID int // Unix User Id
GID int // Unix Group ID
HomeDir string // Home directory of the user
Username string // Textual username
}
const (
// FallbackUserName is what we fallback to if everything else fails.
FallbackUserName = "Automated Package Build"
// FallbackUserEmail is what we fallback to if everything else fails.
FallbackUserEmail = "no.email.set.in.config"
)
// SetFromSudo will attempt to set our details from sudo user environment.
func (u *UserInfo) SetFromSudo() bool {
var uid, gid int
sudoUID := os.Getenv("SUDO_UID")
sudoGID := os.Getenv("SUDO_GID")
var err error
if sudoGID == "" {
sudoGID = sudoUID
}
if sudoUID == "" {
return false
}
if uid, err = strconv.Atoi(sudoUID); err != nil {
slog.Error("Malformed SUDO_UID in environment", "sudu_uid", sudoUID, "err", err)
return false
}
if gid, err = strconv.Atoi(sudoGID); err != nil {
slog.Error("Malformed SUDO_GID in environment", "sudu_gid", sudoGID, "err", err)
return false
}
u.UID = uid
u.GID = gid
// Try to set the home directory
usr, err := user.LookupId(sudoUID)
if err != nil {
slog.Error("Failed to lookup SUDO_USER entry", "uid", uid, "err", err)
return false
}
// Now store the home directory for that user
u.HomeDir = usr.HomeDir
u.Username = usr.Username
// In case of future fails
u.Name = usr.Name
return true
}
// SetFromCurrent will set the UserInfo details from the current user.
func (u *UserInfo) SetFromCurrent() {
u.UID = os.Getuid()
u.GID = os.Getgid()
if usr, err := user.Current(); err != nil {
u.HomeDir = usr.HomeDir
u.Username = usr.Username
u.Name = usr.Name
} else {
slog.Error("Failed to lookup current user", "uid", u.UID, "err", err)
u.Username = os.Getenv("USERNAME")
u.Name = u.Username
u.HomeDir = filepath.Join("/home", u.Username)
}
}
// SetFromPackager will set the username/email fields from one of our packager files.
func (u *UserInfo) SetFromPackager() bool {
candidatePaths := []string{
filepath.Join(u.HomeDir, ".config", "solus", "packager"),
filepath.Join(u.HomeDir, ".solus", "packager"),
filepath.Join(u.HomeDir, ".evolveos", "packager"),
}
// Attempt to parse one of the packager files
for _, p := range candidatePaths {
if !PathExists(p) {
continue
}
cfg, err := ini.Load(p)
if err != nil {
slog.Error("Error loading INI file", "path", p, "err", err)
continue
}
section, err := cfg.GetSection("Packager")
if err != nil {
slog.Error("Missing [Packager] section in file", "path", p)
continue
}
uname, err := section.GetKey("Name")
if err != nil {
slog.Error("Packager file has missing Name", "path", p, "err", err)
continue
}
email, err := section.GetKey("Email")
if err != nil {
slog.Error("Packager file has missing Email", "path", p, "err", err)
continue
}
u.Name = uname.String()
u.Email = email.String()
slog.Debug("Setting packager details from packager INI file")
return true
}
return false
}
// SetFromGit will set the username/email fields from the git config file.
func (u *UserInfo) SetFromGit() bool {
gitConfPath := filepath.Join(u.HomeDir, ".gitconfig")
if !PathExists(gitConfPath) {
return false
}
cfg, err := ini.Load(gitConfPath)
if err != nil {
slog.Error("Error loading gitconfig", "path", gitConfPath, "err", err)
return false
}
section, err := cfg.GetSection("user")
if err != nil {
slog.Error("Missing [user] section in gitconfig", "path", gitConfPath, "err", err)
return false
}
uname, err := section.GetKey("name")
if err != nil {
slog.Error("gitconfig file has missing name", "path", gitConfPath, "err", err)
return false
}
email, err := section.GetKey("email")
if err != nil {
slog.Error("gitconfig file has missing email", "path", gitConfPath, "err", err)
return false
}
u.Name = uname.String()
u.Email = email.String()
slog.Debug("Setting packager details from git config")
return true
}
// GetUserInfo will always succeed, as it will use a fallback policy until it
// finally comes up with a valid combination of name/email to use.
func GetUserInfo() *UserInfo {
uinfo := &UserInfo{}
// First up try to set the uid/gid
if !uinfo.SetFromSudo() {
uinfo.SetFromCurrent()
}
attempts := []func() bool{
uinfo.SetFromPackager,
uinfo.SetFromGit,
}
for _, a := range attempts {
if a() {
return uinfo
}
}
if uinfo.Name == "" {
uinfo.Name = FallbackUserName
}
if uinfo.Email == "" {
if ho, err := os.Hostname(); err != nil {
uinfo.Email = fmt.Sprintf("%s@%s", uinfo.Username, ho)
} else {
uinfo.Email = FallbackUserEmail
}
}
return uinfo
}
// WritePackager will attempt to write the packager file to given path.
func (u *UserInfo) WritePackager(path string) error {
fi, err := os.Create(path)
if err != nil {
return err
}
defer fi.Close()
contents := fmt.Sprintf("[Packager]\nName=%s\nEmail=%s\n", u.Name, u.Email)
if _, err := fi.WriteString(contents); err != nil {
return err
}
return nil
}