2011-04-01 19:47:07 +00:00
|
|
|
/*
|
|
|
|
Copyright 2011 Google Inc.
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2012-03-27 22:54:34 +00:00
|
|
|
"bytes"
|
2012-03-28 00:28:42 +00:00
|
|
|
"encoding/binary"
|
2012-04-27 23:27:25 +00:00
|
|
|
"errors"
|
2011-04-01 19:47:07 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
"net"
|
2011-04-01 19:47:07 +00:00
|
|
|
"os"
|
2012-03-27 22:54:34 +00:00
|
|
|
"os/exec"
|
|
|
|
"os/user"
|
|
|
|
"runtime"
|
2011-04-01 19:47:07 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2012-04-27 23:27:25 +00:00
|
|
|
var ErrNotFound = errors.New("netutil: connection not found")
|
|
|
|
|
2012-03-27 22:54:34 +00:00
|
|
|
// ConnUserid returns the uid that owns the given localhost connection.
|
2012-04-27 23:27:25 +00:00
|
|
|
// The returned error is ErrNotFound if the connection wasn't found.
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
func ConnUserid(conn net.Conn) (uid int, err error) {
|
2012-11-08 23:40:13 +00:00
|
|
|
return AddrPairUserid(conn.LocalAddr(), conn.RemoteAddr())
|
2011-04-01 19:47:07 +00:00
|
|
|
}
|
|
|
|
|
2012-11-08 23:40:13 +00:00
|
|
|
// This fonction allows parsing of a TCPAddr without resolving names
|
|
|
|
// other than localhost. It will return an error instead of resolving.
|
|
|
|
func HostPortToIP(hostport string) (hostaddr *net.TCPAddr, err error) {
|
|
|
|
host, port, err := net.SplitHostPort(hostport)
|
2011-04-01 19:47:07 +00:00
|
|
|
if err != nil {
|
2012-11-08 23:40:13 +00:00
|
|
|
return nil, err
|
2011-07-02 16:09:50 +00:00
|
|
|
}
|
2012-11-08 23:40:13 +00:00
|
|
|
iport, err := strconv.Atoi(port)
|
|
|
|
if err != nil || iport < 0 || iport > 0xFFFF {
|
|
|
|
return nil, fmt.Errorf("invalid port %s", iport)
|
2011-04-01 19:47:07 +00:00
|
|
|
}
|
2012-11-08 23:40:13 +00:00
|
|
|
var addr net.IP
|
|
|
|
if host == "localhost" {
|
|
|
|
addr = net.IPv4(127, 0, 0, 1)
|
|
|
|
} else if addr = net.ParseIP(host); addr == nil {
|
|
|
|
return nil, fmt.Errorf("could not parse IP %s", host)
|
2011-04-01 19:47:07 +00:00
|
|
|
}
|
2012-11-08 23:40:13 +00:00
|
|
|
|
|
|
|
return &net.TCPAddr{IP: addr, Port: iport}, nil
|
2011-04-01 19:47:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddrPairUserid returns the local userid who owns the TCP connection
|
|
|
|
// given by the local and remote ip:port (lipport and ripport,
|
2012-04-27 23:27:25 +00:00
|
|
|
// respectively). Returns ErrNotFound for the error if the TCP connection
|
2011-04-01 19:47:07 +00:00
|
|
|
// isn't found.
|
2012-11-08 23:40:13 +00:00
|
|
|
func AddrPairUserid(local, remote net.Addr) (uid int, err error) {
|
|
|
|
lAddr, lOk := local.(*net.TCPAddr)
|
|
|
|
rAddr, rOk := remote.(*net.TCPAddr)
|
|
|
|
if !(lOk && rOk) {
|
|
|
|
return -1, fmt.Errorf("netutil: Could not convert Addr to TCPAddr.")
|
2011-04-01 19:47:07 +00:00
|
|
|
}
|
2012-11-08 23:40:13 +00:00
|
|
|
|
|
|
|
localv4 := (lAddr.IP.To4() != nil)
|
|
|
|
remotev4 := (rAddr.IP.To4() != nil)
|
2011-04-01 19:47:07 +00:00
|
|
|
if localv4 != remotev4 {
|
|
|
|
return -1, fmt.Errorf("netutil: address pairs of different families; localv4=%v, remotev4=%v",
|
|
|
|
localv4, remotev4)
|
|
|
|
}
|
|
|
|
|
2012-03-27 22:54:34 +00:00
|
|
|
if runtime.GOOS == "darwin" {
|
2012-11-08 23:40:13 +00:00
|
|
|
return uidFromDarwinLsof(lAddr.IP, lAddr.Port, rAddr.IP, rAddr.Port)
|
2012-03-27 22:54:34 +00:00
|
|
|
}
|
|
|
|
|
2011-04-01 19:47:07 +00:00
|
|
|
file := "/proc/net/tcp"
|
|
|
|
if !localv4 {
|
|
|
|
file = "/proc/net/tcp6"
|
|
|
|
}
|
2011-04-07 17:58:29 +00:00
|
|
|
f, err := os.Open(file)
|
2011-04-01 19:47:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return -1, fmt.Errorf("Error opening %s: %v", file, err)
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2012-11-08 23:40:13 +00:00
|
|
|
return uidFromReader(lAddr.IP, lAddr.Port, rAddr.IP, rAddr.Port, f)
|
2011-04-01 19:47:07 +00:00
|
|
|
}
|
|
|
|
|
2012-03-28 00:28:42 +00:00
|
|
|
func toLinuxIPv4Order(b []byte) []byte {
|
|
|
|
binary.BigEndian.PutUint32(b, binary.LittleEndian.Uint32(b))
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func toLinuxIPv6Order(b []byte) []byte {
|
|
|
|
for i := 0; i < 16; i += 4 {
|
|
|
|
sb := b[i : i+4]
|
|
|
|
binary.BigEndian.PutUint32(sb, binary.LittleEndian.Uint32(sb))
|
2011-04-01 19:47:07 +00:00
|
|
|
}
|
2012-03-28 00:28:42 +00:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
type maybeBrackets net.IP
|
|
|
|
|
|
|
|
func (p maybeBrackets) String() string {
|
|
|
|
s := net.IP(p).String()
|
|
|
|
if strings.Contains(s, ":") {
|
|
|
|
return "[" + s + "]"
|
|
|
|
}
|
|
|
|
return s
|
2011-04-01 19:47:07 +00:00
|
|
|
}
|
|
|
|
|
2012-03-27 22:54:34 +00:00
|
|
|
func uidFromDarwinLsof(lip net.IP, lport int, rip net.IP, rport int) (uid int, err error) {
|
2012-03-28 00:28:42 +00:00
|
|
|
seek := fmt.Sprintf("%s:%d->%s:%d", maybeBrackets(lip), lport, maybeBrackets(rip), rport)
|
2012-03-27 22:54:34 +00:00
|
|
|
seekb := []byte(seek)
|
2012-04-28 01:51:07 +00:00
|
|
|
cmd := exec.Command("lsof",
|
2012-10-15 22:16:36 +00:00
|
|
|
"-b", // avoid system calls that could block
|
|
|
|
"-w", // and don't warn about cases where -b fails
|
|
|
|
"-n", // don't resolve network names
|
|
|
|
"-P", // don't resolve network ports,
|
2012-04-28 01:51:07 +00:00
|
|
|
// TODO(bradfitz): pass down the uid we care about, then do: ?
|
|
|
|
//"-a", // AND the following together:
|
|
|
|
// "-u", strconv.Itoa(uid) // just this uid
|
|
|
|
"-itcp") // we only care about TCP connections
|
2012-03-27 22:54:34 +00:00
|
|
|
stdout, err := cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer cmd.Wait()
|
|
|
|
defer stdout.Close()
|
|
|
|
err = cmd.Start()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer cmd.Process.Kill()
|
|
|
|
br := bufio.NewReader(stdout)
|
|
|
|
for {
|
|
|
|
line, err := br.ReadSlice('\n')
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
if !bytes.Contains(line, seekb) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// SystemUIS 276 bradfitz 15u IPv4 0xffffff801a7c74e0 0t0 TCP 127.0.0.1:56718->127.0.0.1:5204 (ESTABLISHED)
|
|
|
|
f := bytes.Fields(line)
|
|
|
|
if len(f) < 8 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
username := string(f[2])
|
|
|
|
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
|
|
|
|
}
|
2012-04-27 23:27:25 +00:00
|
|
|
return -1, ErrNotFound
|
2012-03-27 22:54:34 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
func uidFromReader(lip net.IP, lport int, rip net.IP, rport int, r io.Reader) (uid int, err error) {
|
2011-04-01 19:47:07 +00:00
|
|
|
buf := bufio.NewReader(r)
|
|
|
|
|
|
|
|
localHex := ""
|
|
|
|
remoteHex := ""
|
2012-03-28 00:28:42 +00:00
|
|
|
ipv4 := lip.To4() != nil
|
|
|
|
if ipv4 {
|
2011-04-01 19:47:07 +00:00
|
|
|
// In the kernel, the port is run through ntohs(), and
|
|
|
|
// the inet_request_socket in
|
|
|
|
// include/net/inet_socket.h says the "loc_addr" and
|
|
|
|
// "rmt_addr" fields are __be32, but get_openreq4's
|
|
|
|
// printf of them is raw, without byte order
|
|
|
|
// converstion.
|
2012-03-28 00:28:42 +00:00
|
|
|
localHex = fmt.Sprintf("%08X:%04X", toLinuxIPv4Order([]byte(lip.To4())), lport)
|
|
|
|
remoteHex = fmt.Sprintf("%08X:%04X", toLinuxIPv4Order([]byte(rip.To4())), rport)
|
2011-04-01 19:47:07 +00:00
|
|
|
} else {
|
2012-03-28 00:28:42 +00:00
|
|
|
localHex = fmt.Sprintf("%032X:%04X", toLinuxIPv6Order([]byte(lip.To16())), lport)
|
|
|
|
remoteHex = fmt.Sprintf("%032X:%04X", toLinuxIPv6Order([]byte(rip.To16())), rport)
|
2011-04-01 19:47:07 +00:00
|
|
|
}
|
2011-07-02 16:09:50 +00:00
|
|
|
|
2011-04-01 19:47:07 +00:00
|
|
|
for {
|
|
|
|
line, err := buf.ReadString('\n')
|
|
|
|
if err != nil {
|
2012-04-27 23:27:25 +00:00
|
|
|
return -1, ErrNotFound
|
2011-04-01 19:47:07 +00:00
|
|
|
}
|
|
|
|
parts := strings.Fields(strings.TrimSpace(line))
|
|
|
|
if len(parts) < 8 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// log.Printf("parts[1] = %q; localHex = %q", parts[1], localHex)
|
|
|
|
if parts[1] == localHex && parts[2] == remoteHex {
|
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
|
|
|
uid, err = strconv.Atoi(parts[7])
|
|
|
|
return uid, err
|
2011-04-01 19:47:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
panic("unreachable")
|
|
|
|
}
|
2012-10-15 22:16:36 +00:00
|
|
|
|
|
|
|
// Localhost returns the first address found when
|
2012-11-08 23:40:13 +00:00
|
|
|
// doing a lookup on "localhost".
|
|
|
|
func Localhost() (net.IP, error) {
|
|
|
|
ips, err := net.LookupIP("localhost")
|
2012-10-15 22:16:36 +00:00
|
|
|
if err != nil {
|
2012-11-08 23:40:13 +00:00
|
|
|
return nil, err
|
2012-10-15 22:16:36 +00:00
|
|
|
}
|
2012-11-08 23:40:13 +00:00
|
|
|
if len(ips) < 1 {
|
|
|
|
return nil, errors.New("IP lookup for localhost returned no result")
|
2012-10-15 22:16:36 +00:00
|
|
|
}
|
2012-11-08 23:40:13 +00:00
|
|
|
return ips[0], nil
|
2012-10-15 22:16:36 +00:00
|
|
|
}
|