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