github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/pkg/httputil/auth.go (about) 1 /* 2 Copyright 2013 The Camlistore Authors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package httputil 18 19 import ( 20 "encoding/base64" 21 "fmt" 22 "log" 23 "net/http" 24 "os" 25 "regexp" 26 "runtime" 27 "strings" 28 29 "camlistore.org/pkg/netutil" 30 ) 31 32 var kBasicAuthPattern = regexp.MustCompile(`^Basic ([a-zA-Z0-9\+/=]+)`) 33 34 // IsLocalhost reports whether the requesting connection is from this machine 35 // and has the same owner as this process. 36 func IsLocalhost(req *http.Request) bool { 37 uid := os.Getuid() 38 from, err := netutil.HostPortToIP(req.RemoteAddr, nil) 39 if err != nil { 40 return false 41 } 42 to, err := netutil.HostPortToIP(req.Host, from) 43 if err != nil { 44 return false 45 } 46 47 // If our OS doesn't support uid. 48 // TODO(bradfitz): netutil on OS X uses "lsof" to figure out 49 // ownership of tcp connections, but when fuse is mounted and a 50 // request is outstanding (for instance, a fuse request that's 51 // making a request to camlistored and landing in this code 52 // path), lsof then blocks forever waiting on a lock held by the 53 // VFS, leading to a deadlock. Instead, on darwin, just trust 54 // any localhost connection here, which is kinda lame, but 55 // whatever. Macs aren't very multi-user anyway. 56 if uid == -1 || runtime.GOOS == "darwin" { 57 return from.IP.IsLoopback() && to.IP.IsLoopback() 58 } 59 if uid == 0 { 60 log.Printf("camlistored running as root. Don't do that.") 61 return false 62 } 63 if uid > 0 { 64 connUid, err := netutil.AddrPairUserid(from, to) 65 if err == nil { 66 if uid == connUid { 67 return true 68 } 69 log.Printf("auth: local connection uid %d doesn't match server uid %d", connUid, uid) 70 } 71 } 72 return false 73 } 74 75 // BasicAuth parses the Authorization header on req 76 // If absent or invalid, an error is returned. 77 func BasicAuth(req *http.Request) (username, password string, err error) { 78 auth := req.Header.Get("Authorization") 79 if auth == "" { 80 err = fmt.Errorf("Missing \"Authorization\" in header") 81 return 82 } 83 matches := kBasicAuthPattern.FindStringSubmatch(auth) 84 if len(matches) != 2 { 85 err = fmt.Errorf("Bogus Authorization header") 86 return 87 } 88 encoded := matches[1] 89 enc := base64.StdEncoding 90 decBuf := make([]byte, enc.DecodedLen(len(encoded))) 91 n, err := enc.Decode(decBuf, []byte(encoded)) 92 if err != nil { 93 return 94 } 95 pieces := strings.SplitN(string(decBuf[0:n]), ":", 2) 96 if len(pieces) != 2 { 97 err = fmt.Errorf("didn't get two pieces") 98 return 99 } 100 return pieces[0], pieces[1], nil 101 }