socket.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2012 Google Inc. All Rights Reserved.
  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. package main
  15. import (
  16. "fmt"
  17. "log"
  18. "net"
  19. "os"
  20. "path/filepath"
  21. "syscall"
  22. )
  23. // A Socket is a wrapper around a Unix socket that verifies directory
  24. // permissions.
  25. type Socket struct {
  26. Dir string
  27. }
  28. func defaultDir() string {
  29. sockPath := ".git-credential-cache"
  30. if home := os.Getenv("HOME"); home != "" {
  31. return filepath.Join(home, sockPath)
  32. }
  33. log.Printf("socket: cannot find HOME path. using relative directory %q for socket", sockPath)
  34. return sockPath
  35. }
  36. // DefaultSocket is a Socket in the $HOME/.git-credential-cache directory.
  37. var DefaultSocket = Socket{Dir: defaultDir()}
  38. // Listen announces the local network address of the unix socket. The
  39. // permissions on the socket directory are verified before attempting
  40. // the actual listen.
  41. func (s Socket) Listen() (net.Listener, error) {
  42. network, addr := "unix", s.Path()
  43. if err := s.mkdir(); err != nil {
  44. return nil, &net.OpError{Op: "listen", Net: network, Addr: &net.UnixAddr{Name: addr, Net: network}, Err: err}
  45. }
  46. return net.Listen(network, addr)
  47. }
  48. // Dial connects to the unix socket. The permissions on the socket directory
  49. // are verified before attempting the actual dial.
  50. func (s Socket) Dial() (net.Conn, error) {
  51. network, addr := "unix", s.Path()
  52. if err := s.checkPermissions(); err != nil {
  53. return nil, &net.OpError{Op: "dial", Net: network, Addr: &net.UnixAddr{Name: addr, Net: network}, Err: err}
  54. }
  55. return net.Dial(network, addr)
  56. }
  57. // Path returns the fully specified file name of the unix socket.
  58. func (s Socket) Path() string {
  59. return filepath.Join(s.Dir, "persistent-https-proxy-socket")
  60. }
  61. func (s Socket) mkdir() error {
  62. if err := s.checkPermissions(); err == nil {
  63. return nil
  64. } else if !os.IsNotExist(err) {
  65. return err
  66. }
  67. if err := os.MkdirAll(s.Dir, 0700); err != nil {
  68. return err
  69. }
  70. return s.checkPermissions()
  71. }
  72. func (s Socket) checkPermissions() error {
  73. fi, err := os.Stat(s.Dir)
  74. if err != nil {
  75. return err
  76. }
  77. if !fi.IsDir() {
  78. return fmt.Errorf("socket: got file, want directory for %q", s.Dir)
  79. }
  80. if fi.Mode().Perm() != 0700 {
  81. return fmt.Errorf("socket: got perm %o, want 700 for %q", fi.Mode().Perm(), s.Dir)
  82. }
  83. if st := fi.Sys().(*syscall.Stat_t); int(st.Uid) != os.Getuid() {
  84. return fmt.Errorf("socket: got uid %d, want %d for %q", st.Uid, os.Getuid(), s.Dir)
  85. }
  86. return nil
  87. }