internal/netutil, pkg/schema: remove nocgo tag; os/user has its own nowadays

The os/user package nowadays says:

    When cgo is available, and the required routines are implemented
    in libc for a particular platform, cgo-based (libc-backed) code
    is used. This can be overridden by using osusergo build tag,
    which enforces the pure Go implementation.

So we don't need to work around it anymore.

Signed-off-by: Brad Fitzpatrick <brad@danga.com>
This commit is contained in:
Brad Fitzpatrick 2024-01-11 13:59:46 -08:00
parent 31f3941a00
commit c2334642fb
6 changed files with 45 additions and 154 deletions

View File

@ -28,6 +28,7 @@ import (
"net"
"os"
"os/exec"
"os/user"
"regexp"
"runtime"
"strconv"
@ -134,6 +135,18 @@ func (p maybeBrackets) String() string {
// Changed by tests.
var uidFromUsername = uidFromUsernameFn
func uidFromUsernameFn(username string) (uid int, err error) {
if uid := os.Getuid(); uid != 0 && username == os.Getenv("USER") {
return uid, nil
}
u, err := user.Lookup(username)
if err == nil {
uid, err := strconv.Atoi(u.Uid)
return uid, err
}
return 0, err
}
func uidFromLsof(lip net.IP, lport int, rip net.IP, rport int) (uid int, err error) {
seek := fmt.Sprintf("%s:%d->%s:%d", maybeBrackets(lip), lport, maybeBrackets(rip), rport)
seekb := []byte(seek)

View File

@ -1,39 +0,0 @@
//go:build !nocgo
/*
Copyright 2016 The Perkeep Authors.
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 netutil identifies the system userid responsible for
// localhost TCP connections.
package netutil // import "perkeep.org/internal/netutil"
import (
"os"
"os/user"
"strconv"
)
func uidFromUsernameFn(username string) (uid int, err error) {
if uid := os.Getuid(); uid != 0 && username == os.Getenv("USER") {
return uid, nil
}
u, err := user.Lookup(username)
if err == nil {
uid, err := strconv.Atoi(u.Uid)
return uid, err
}
return 0, err
}

View File

@ -1,32 +0,0 @@
//go:build nocgo
/*
Copyright 2016 The Perkeep Authors.
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 netutil identifies the system userid responsible for
// localhost TCP connections.
package netutil // import "perkeep.org/internal/netutil"
import (
"os"
)
func uidFromUsernameFn(username string) (uid int, err error) {
if uid := os.Getuid(); uid != 0 && username == os.Getenv("USER") {
return uid, nil
}
return 0, err
}

View File

@ -19,6 +19,7 @@ package schema
import (
"bufio"
"os"
"os/user"
"strconv"
"strings"
"sync"
@ -94,6 +95,36 @@ func cachedId(name string, m map[string]intBool, fn func(string) (int, bool)) (i
return id, ok
}
// lookupMu must be held.
func lookupUserToId(name string) (uid int, ok bool) {
u, err := user.Lookup(name)
if err == nil {
uid, err := strconv.Atoi(u.Uid)
if err == nil {
return uid, true
}
}
return
}
// lookupMu must be held.
func lookupUserid(id int) string {
u, err := user.LookupId(strconv.Itoa(id))
if err == nil {
return u.Username
}
if _, ok := err.(user.UnknownUserIdError); ok {
return ""
}
if parsedPasswd {
return ""
}
parsedPasswd = true
populateMap(uidName, nil, "/etc/passwd")
return uidName[id]
}
// lookupMu must be held.
func lookupGroupToId(group string) (gid int, ok bool) {
if !parsedGroups {
lookupGroupId(0) // force them to be loaded
@ -102,7 +133,7 @@ func lookupGroupToId(group string) (gid int, ok bool) {
return intb.int, intb.bool
}
// lookupMu is held
// lookupMu must be held.
func lookupGroupId(id int) string {
if parsedGroups {
return ""

View File

@ -1,52 +0,0 @@
//go:build !nocgo
/*
Copyright 2016 The Perkeep Authors.
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 schema
import (
"os/user"
"strconv"
)
func lookupUserToId(name string) (uid int, ok bool) {
u, err := user.Lookup(name)
if err == nil {
uid, err := strconv.Atoi(u.Uid)
if err == nil {
return uid, true
}
}
return
}
// lookupMu is held
func lookupUserid(id int) string {
u, err := user.LookupId(strconv.Itoa(id))
if err == nil {
return u.Username
}
if _, ok := err.(user.UnknownUserIdError); ok {
return ""
}
if parsedPasswd {
return ""
}
parsedPasswd = true
populateMap(uidName, nil, "/etc/passwd")
return uidName[id]
}

View File

@ -1,30 +0,0 @@
//go:build nocgo
/*
Copyright 2016 The Perkeep Authors.
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 schema
func lookupUserToId(name string) (uid int, ok bool) {
return
}
// lookupMu is held
func lookupUserid(id int) string {
parsedPasswd = true
populateMap(uidName, nil, "/etc/passwd")
return uidName[id]
}