]> Sergey Matveev's repositories - tofuproxy.git/blob - auth.go
HTTP authorization
[tofuproxy.git] / auth.go
1 /*
2 tofuproxy -- HTTP proxy with TLS certificates management
3 Copyright (C) 2021 Sergey Matveev <stargrave@stargrave.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 package tofuproxy
19
20 import (
21         "bytes"
22         "errors"
23         "fmt"
24         "io/ioutil"
25         "log"
26         "os"
27         "os/exec"
28         "path/filepath"
29         "strings"
30         "sync"
31 )
32
33 var (
34         authCache  = make(map[string][2]string)
35         authCacheM sync.Mutex
36 )
37
38 func findInNetrc(host string) (string, string) {
39         netrcPath, ok := os.LookupEnv("NETRC")
40         if !ok {
41                 netrcPath = filepath.Join(os.Getenv("HOME"), ".netrc")
42         }
43         data, err := ioutil.ReadFile(netrcPath)
44         if err != nil {
45                 if os.IsNotExist(err) {
46                         return "", ""
47                 }
48                 log.Fatalln(err)
49         }
50         var login string
51         var password string
52         for _, line := range strings.Split(string(data), "\n") {
53                 if i := strings.Index(line, "#"); i >= 0 {
54                         line = line[:i]
55                 }
56                 f := strings.Fields(line)
57                 if len(f) >= 6 &&
58                         f[0] == "machine" && f[1] == host &&
59                         f[2] == "login" && f[4] == "password" {
60                         login, password = f[3], f[5]
61                         break
62                 }
63         }
64         return login, password
65 }
66
67 func authDialog(host, realm string) (string, string, error) {
68         var b bytes.Buffer
69         userInit, passInit := findInNetrc(host)
70         b.WriteString(fmt.Sprintf(`
71 wm title . "Unauthorized: %s"
72
73 label .luser -text "User"
74 set userinit "%s"
75 set u [entry .user -textvariable userinit]
76 grid .luser .user
77
78 label .lpass -text "Password"
79 set passinit "%s"
80 set p [entry .pass -show "*" -textvariable passinit]
81 grid .lpass .pass
82
83 proc submit {} {
84     global u p
85     puts [$u get]
86     puts [$p get]
87     exit
88 }
89
90 button .submit -text "Submit" -command submit
91 grid .submit
92 `, realm, userInit, passInit))
93         cmd := exec.Command(CmdWish)
94         cmd.Stdin = &b
95         out, err := cmd.Output()
96         if err != nil {
97                 return "", "", err
98         }
99         lines := strings.Split(string(out), "\n")
100         if len(lines) < 2 {
101                 return "", "", errors.New("invalid output from authorization form")
102         }
103         return lines[0], lines[1], nil
104 }