]> Sergey Matveev's repositories - tofuproxy.git/blob - httpauth.go
Darkmode
[tofuproxy.git] / httpauth.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 )
31
32 func findInNetrc(host string) (string, string) {
33         netrcPath, ok := os.LookupEnv("NETRC")
34         if !ok {
35                 netrcPath = filepath.Join(os.Getenv("HOME"), ".netrc")
36         }
37         data, err := ioutil.ReadFile(netrcPath)
38         if err != nil {
39                 if os.IsNotExist(err) {
40                         return "", ""
41                 }
42                 log.Fatalln(err)
43         }
44         var login string
45         var password string
46         for _, line := range strings.Split(string(data), "\n") {
47                 if i := strings.Index(line, "#"); i >= 0 {
48                         line = line[:i]
49                 }
50                 f := strings.Fields(line)
51                 if len(f) >= 6 &&
52                         f[0] == "machine" && f[1] == host &&
53                         f[2] == "login" && f[4] == "password" {
54                         login, password = f[3], f[5]
55                         break
56                 }
57         }
58         return login, password
59 }
60
61 func authDialog(host, realm string) (string, string, error) {
62         var b bytes.Buffer
63         userInit, passInit := findInNetrc(host)
64         b.WriteString(fmt.Sprintf(`
65 tk_setPalette grey
66 wm title . "Unauthorized: %s"
67
68 label .luser -text "User"
69 set userinit "%s"
70 set u [entry .user -textvariable userinit]
71 grid .luser .user
72
73 label .lpass -text "Password"
74 set passinit "%s"
75 set p [entry .pass -show "*" -textvariable passinit]
76 grid .lpass .pass
77
78 proc submit {} {
79     global u p
80     puts [$u get]
81     puts [$p get]
82     exit
83 }
84
85 button .submit -text "Submit" -command submit
86 grid .submit
87 `, realm, userInit, passInit))
88         cmd := exec.Command(CmdWish)
89         cmd.Stdin = &b
90         out, err := cmd.Output()
91         if err != nil {
92                 return "", "", err
93         }
94         lines := strings.Split(string(out), "\n")
95         if len(lines) < 2 {
96                 return "", "", errors.New("invalid output from authorization form")
97         }
98         return lines[0], lines[1], nil
99 }