-
Notifications
You must be signed in to change notification settings - Fork 660
Expand file tree
/
Copy pathucrednet.go
More file actions
120 lines (101 loc) · 2.67 KB
/
ucrednet.go
File metadata and controls
120 lines (101 loc) · 2.67 KB
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
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2015 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package daemon
import (
"errors"
"fmt"
"net"
"strconv"
"strings"
sys "syscall"
)
var errNoID = errors.New("no pid/uid found")
const (
ucrednetNoProcess = uint32(0)
ucrednetNobody = uint32((1 << 32) - 1)
)
func ucrednetGet(remoteAddr string) (pid uint32, uid uint32, socket string, err error) {
pid = ucrednetNoProcess
uid = ucrednetNobody
for _, token := range strings.Split(remoteAddr, ";") {
var v uint64
if strings.HasPrefix(token, "pid=") {
if v, err = strconv.ParseUint(token[4:], 10, 32); err == nil {
pid = uint32(v)
} else {
break
}
} else if strings.HasPrefix(token, "uid=") {
if v, err = strconv.ParseUint(token[4:], 10, 32); err == nil {
uid = uint32(v)
} else {
break
}
}
if strings.HasPrefix(token, "socket=") {
socket = token[7:]
}
}
if pid == ucrednetNoProcess || uid == ucrednetNobody {
err = errNoID
}
return pid, uid, socket, err
}
type ucrednetAddr struct {
net.Addr
pid string
uid string
socket string
}
func (wa *ucrednetAddr) String() string {
return fmt.Sprintf("pid=%s;uid=%s;socket=%s;%s", wa.pid, wa.uid, wa.socket, wa.Addr)
}
type ucrednetConn struct {
net.Conn
pid string
uid string
socket string
}
func (wc *ucrednetConn) RemoteAddr() net.Addr {
return &ucrednetAddr{wc.Conn.RemoteAddr(), wc.pid, wc.uid, wc.socket}
}
type ucrednetListener struct{ net.Listener }
var getUcred = sys.GetsockoptUcred
func (wl *ucrednetListener) Accept() (net.Conn, error) {
con, err := wl.Listener.Accept()
if err != nil {
return nil, err
}
var pid, uid, socket string
if ucon, ok := con.(*net.UnixConn); ok {
f, err := ucon.File()
if err != nil {
return nil, err
}
// File() is a dup(); needs closing
defer f.Close()
ucred, err := getUcred(int(f.Fd()), sys.SOL_SOCKET, sys.SO_PEERCRED)
if err != nil {
return nil, err
}
pid = strconv.FormatUint(uint64(ucred.Pid), 10)
uid = strconv.FormatUint(uint64(ucred.Uid), 10)
socket = ucon.LocalAddr().String()
}
return &ucrednetConn{con, pid, uid, socket}, err
}