From c2334642fb2947699dc9f9fad0a8d796b2c55f38 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Thu, 11 Jan 2024 13:59:46 -0800 Subject: [PATCH] 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 --- internal/netutil/ident.go | 13 +++++++++ internal/netutil/ident_cgo.go | 39 ------------------------- internal/netutil/ident_nocgo.go | 32 -------------------- pkg/schema/lookup.go | 33 ++++++++++++++++++++- pkg/schema/lookup_cgo.go | 52 --------------------------------- pkg/schema/lookup_nocgo.go | 30 ------------------- 6 files changed, 45 insertions(+), 154 deletions(-) delete mode 100644 internal/netutil/ident_cgo.go delete mode 100644 internal/netutil/ident_nocgo.go delete mode 100644 pkg/schema/lookup_cgo.go delete mode 100644 pkg/schema/lookup_nocgo.go diff --git a/internal/netutil/ident.go b/internal/netutil/ident.go index a79c36188..98e0e34d2 100644 --- a/internal/netutil/ident.go +++ b/internal/netutil/ident.go @@ -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) diff --git a/internal/netutil/ident_cgo.go b/internal/netutil/ident_cgo.go deleted file mode 100644 index 6bf764e3d..000000000 --- a/internal/netutil/ident_cgo.go +++ /dev/null @@ -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 -} diff --git a/internal/netutil/ident_nocgo.go b/internal/netutil/ident_nocgo.go deleted file mode 100644 index 10a86faa4..000000000 --- a/internal/netutil/ident_nocgo.go +++ /dev/null @@ -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 -} diff --git a/pkg/schema/lookup.go b/pkg/schema/lookup.go index ddc12542c..177e6fefa 100644 --- a/pkg/schema/lookup.go +++ b/pkg/schema/lookup.go @@ -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 "" diff --git a/pkg/schema/lookup_cgo.go b/pkg/schema/lookup_cgo.go deleted file mode 100644 index 88c2ab3b2..000000000 --- a/pkg/schema/lookup_cgo.go +++ /dev/null @@ -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] -} diff --git a/pkg/schema/lookup_nocgo.go b/pkg/schema/lookup_nocgo.go deleted file mode 100644 index ebb50c4d8..000000000 --- a/pkg/schema/lookup_nocgo.go +++ /dev/null @@ -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] -}