|
| 1 | +// Copyright 2022-2023 Tigris Data, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +//go:build darwin |
| 16 | + |
| 17 | +package util |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "net" |
| 22 | + |
| 23 | + "golang.org/x/sys/unix" |
| 24 | +) |
| 25 | + |
| 26 | +func ReadPeerCreds(c net.Conn) (uint32, error) { |
| 27 | + var cred *unix.Xucred |
| 28 | + |
| 29 | + uc, ok := c.(*net.UnixConn) |
| 30 | + if !ok { |
| 31 | + return 0, ErrNotUnixConn |
| 32 | + } |
| 33 | + |
| 34 | + raw, err := uc.SyscallConn() |
| 35 | + if err != nil { |
| 36 | + return 0, fmt.Errorf("error getting raw connection: %s", err) |
| 37 | + } |
| 38 | + |
| 39 | + err1 := raw.Control(func(fd uintptr) { |
| 40 | + cred, err = unix.GetsockoptXucred(int(fd), unix.SOL_LOCAL, unix.LOCAL_PEERCRED) |
| 41 | + }) |
| 42 | + |
| 43 | + if err != nil { |
| 44 | + return 0, fmt.Errorf("getsockoptxucred error: %s", err) |
| 45 | + } |
| 46 | + |
| 47 | + if err1 != nil { |
| 48 | + return 0, fmt.Errorf("control error: %s", err1) |
| 49 | + } |
| 50 | + |
| 51 | + return cred.Uid, nil |
| 52 | +} |
0 commit comments