2 tofuproxy -- HTTP proxy with TLS certificates management
3 Copyright (C) 2021 Sergey Matveev <stargrave@stargrave.org>
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.
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.
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/>.
32 func findInNetrc(host string) (string, string) {
33 netrcPath, ok := os.LookupEnv("NETRC")
35 netrcPath = filepath.Join(os.Getenv("HOME"), ".netrc")
37 data, err := ioutil.ReadFile(netrcPath)
39 if os.IsNotExist(err) {
46 for _, line := range strings.Split(string(data), "\n") {
47 if i := strings.Index(line, "#"); i >= 0 {
50 f := strings.Fields(line)
52 f[0] == "machine" && f[1] == host &&
53 f[2] == "login" && f[4] == "password" {
54 login, password = f[3], f[5]
58 return login, password
61 func authDialog(host, realm string) (string, string, error) {
63 userInit, passInit := findInNetrc(host)
64 b.WriteString(fmt.Sprintf(`
66 wm title . "Unauthorized: %s"
68 label .luser -text "User"
70 set u [entry .user -textvariable userinit]
73 label .lpass -text "Password"
75 set p [entry .pass -show "*" -textvariable passinit]
85 button .login -text "Login" -command login
88 bind . <KeyPress> {switch -exact %%K {
89 q {exit 0} ; # reject once
92 `, realm, userInit, passInit))
93 cmd := exec.Command(CmdWish)
95 out, err := cmd.Output()
99 lines := strings.Split(string(out), "\n")
101 return "", "", errors.New("invalid output from authorization form")
103 return lines[0], lines[1], nil