| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package main |
| 5 |
|
| 6 |
import ( |
| 7 |
"bufio" |
| 8 |
"errors" |
| 9 |
"fmt" |
| 10 |
"os" |
| 11 |
"path/filepath" |
| 12 |
"slices" |
| 13 |
"strings" |
| 14 |
) |
| 15 |
|
| 16 |
var ( |
| 17 |
commands = map[string]func(){ |
| 18 |
"get": fill, |
| 19 |
"store": log, |
| 20 |
"erase": log, |
| 21 |
} |
| 22 |
|
| 23 |
delim = '\n' |
| 24 |
credsDir = "" |
| 25 |
) |
| 26 |
|
| 27 |
type credential struct { |
| 28 |
authtype string |
| 29 |
username string |
| 30 |
password string |
| 31 |
credential string |
| 32 |
matchState string |
| 33 |
state string |
| 34 |
multistage bool |
| 35 |
skip bool |
| 36 |
} |
| 37 |
|
| 38 |
func (c *credential) Serialize(capabilities map[string]struct{}, state []string, username []string) map[string][]string { |
| 39 |
formattedState := fmt.Sprintf("lfstest:%s", c.state) |
| 40 |
formattedMatchState := fmt.Sprintf("lfstest:%s", c.matchState) |
| 41 |
creds := make(map[string][]string) |
| 42 |
if c.skip { |
| 43 |
|
| 44 |
} else if _, ok := capabilities["authtype"]; ok && len(c.authtype) != 0 && len(c.credential) != 0 { |
| 45 |
if _, ok := capabilities["state"]; len(c.matchState) == 0 || (ok && slices.Contains(state, formattedMatchState)) { |
| 46 |
creds["authtype"] = []string{c.authtype} |
| 47 |
creds["credential"] = []string{c.credential} |
| 48 |
if ok { |
| 49 |
creds["state[]"] = []string{formattedState} |
| 50 |
if c.multistage { |
| 51 |
creds["continue"] = []string{"1"} |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
} else if len(c.authtype) == 0 && (len(username) == 0 || username[0] == c.username) { |
| 56 |
if len(username) == 0 { |
| 57 |
creds["username"] = []string{c.username} |
| 58 |
} |
| 59 |
creds["password"] = []string{c.password} |
| 60 |
} |
| 61 |
return creds |
| 62 |
} |
| 63 |
|
| 64 |
func init() { |
| 65 |
if len(credsDir) == 0 { |
| 66 |
credsDir = os.Getenv("CREDSDIR") |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
func main() { |
| 71 |
if argsize := len(os.Args); argsize != 2 { |
| 72 |
fmt.Fprintf(os.Stderr, "wrong number of args: %d\n", argsize) |
| 73 |
os.Exit(1) |
| 74 |
} |
| 75 |
|
| 76 |
arg := os.Args[1] |
| 77 |
cmd := commands[arg] |
| 78 |
|
| 79 |
if cmd == nil { |
| 80 |
fmt.Fprintf(os.Stderr, "bad cmd: %s\n", arg) |
| 81 |
os.Exit(1) |
| 82 |
} |
| 83 |
|
| 84 |
cmd() |
| 85 |
} |
| 86 |
|
| 87 |
func fill() { |
| 88 |
scanner := bufio.NewScanner(os.Stdin) |
| 89 |
creds := map[string][]string{} |
| 90 |
for scanner.Scan() { |
| 91 |
line := scanner.Text() |
| 92 |
parts := strings.SplitN(line, "=", 2) |
| 93 |
if len(parts) != 2 { |
| 94 |
fmt.Fprintf(os.Stderr, "bad line: %s\n", line) |
| 95 |
os.Exit(1) |
| 96 |
} |
| 97 |
|
| 98 |
fmt.Fprintf(os.Stderr, "CREDS RECV: %s\n", line) |
| 99 |
if _, ok := creds[parts[0]]; ok { |
| 100 |
creds[parts[0]] = append(creds[parts[0]], strings.TrimSpace(parts[1])) |
| 101 |
} else { |
| 102 |
creds[parts[0]] = []string{strings.TrimSpace(parts[1])} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
if err := scanner.Err(); err != nil { |
| 107 |
fmt.Fprintf(os.Stderr, "reading standard input: %v", err) |
| 108 |
os.Exit(1) |
| 109 |
} |
| 110 |
|
| 111 |
hostPieces := strings.SplitN(firstEntryForKey(creds, "host"), ":", 2) |
| 112 |
credentials, err := credsForHostAndPath(hostPieces[0], firstEntryForKey(creds, "path")) |
| 113 |
if err != nil { |
| 114 |
fmt.Fprintln(os.Stderr, err.Error()) |
| 115 |
os.Exit(1) |
| 116 |
} |
| 117 |
|
| 118 |
result := map[string][]string{} |
| 119 |
capas := discoverCapabilities(creds) |
| 120 |
for _, cred := range credentials { |
| 121 |
result = cred.Serialize(capas, creds["state[]"], creds["username"]) |
| 122 |
if len(result) != 0 { |
| 123 |
break |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
mode := os.Getenv("LFS_TEST_CREDS_WWWAUTH") |
| 128 |
wwwauth := firstEntryForKey(creds, "wwwauth[]") |
| 129 |
if mode == "required" && !strings.HasPrefix(wwwauth, "Basic ") { |
| 130 |
fmt.Fprintf(os.Stderr, "Missing required 'wwwauth[]' key in credentials\n") |
| 131 |
os.Exit(1) |
| 132 |
} else if mode == "forbidden" && wwwauth != "" { |
| 133 |
fmt.Fprintf(os.Stderr, "Unexpected 'wwwauth[]' key in credentials\n") |
| 134 |
os.Exit(1) |
| 135 |
} |
| 136 |
|
| 137 |
if len(result) == 0 { |
| 138 |
os.Exit(0) |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
key := "capability[]" |
| 144 |
for entry, _ := range capas { |
| 145 |
fmt.Fprintf(os.Stderr, "CREDS SEND: %s=%s\n", key, entry) |
| 146 |
fmt.Fprintf(os.Stdout, "%s=%s\n", key, entry) |
| 147 |
} |
| 148 |
for key, value := range result { |
| 149 |
for _, entry := range value { |
| 150 |
fmt.Fprintf(os.Stderr, "CREDS SEND: %s=%s\n", key, entry) |
| 151 |
fmt.Fprintf(os.Stdout, "%s=%s\n", key, entry) |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
func discoverCapabilities(creds map[string][]string) map[string]struct{} { |
| 157 |
capas := make(map[string]struct{}) |
| 158 |
supportedCapas := map[string]struct{}{ |
| 159 |
"authtype": struct{}{}, |
| 160 |
"state": struct{}{}, |
| 161 |
} |
| 162 |
for _, capa := range creds["capability[]"] { |
| 163 |
|
| 164 |
if _, ok := supportedCapas[capa]; ok { |
| 165 |
capas[capa] = struct{}{} |
| 166 |
} |
| 167 |
} |
| 168 |
return capas |
| 169 |
} |
| 170 |
|
| 171 |
func credsForHostAndPath(host, path string) ([]credential, error) { |
| 172 |
if len(path) > 0 { |
| 173 |
pathFilename := fmt.Sprintf("%s--%s", host, strings.Replace(path, "/", "-", -1)) |
| 174 |
cred, err := credsFromFilename(filepath.Join(credsDir, pathFilename)) |
| 175 |
if err == nil { |
| 176 |
return cred, err |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
if len(path) > 2 && path[0] >= 'A' && path[0] <= 'Z' && path[1] == ':' { |
| 184 |
path = "/" + strings.ToLower(string(path[0])) + path[2:] |
| 185 |
pathFilename := fmt.Sprintf("%s--%s", host, strings.Replace(path, "/", "-", -1)) |
| 186 |
cred, err := credsFromFilename(filepath.Join(credsDir, pathFilename)) |
| 187 |
if err == nil { |
| 188 |
return cred, err |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
if len(host) == 0 { |
| 194 |
return nil, errors.New("No file available; empty 'host' key in credentials") |
| 195 |
} |
| 196 |
|
| 197 |
return credsFromFilename(filepath.Join(credsDir, host)) |
| 198 |
} |
| 199 |
|
| 200 |
func parseOneCredential(s, file string) (credential, error) { |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
credsPieces := strings.Split(strings.TrimSpace(s), ":") |
| 216 |
if len(credsPieces) != 3 && len(credsPieces) != 6 { |
| 217 |
return credential{}, fmt.Errorf("Invalid data %q while reading %q", string(s), file) |
| 218 |
} |
| 219 |
if credsPieces[0] == "skip" { |
| 220 |
return credential{skip: true}, nil |
| 221 |
} else if len(credsPieces[0]) == 0 { |
| 222 |
return credential{username: credsPieces[1], password: credsPieces[2]}, nil |
| 223 |
} else if len(credsPieces) == 3 { |
| 224 |
return credential{authtype: credsPieces[0], credential: credsPieces[2]}, nil |
| 225 |
} else { |
| 226 |
return credential{ |
| 227 |
authtype: credsPieces[0], |
| 228 |
credential: credsPieces[2], |
| 229 |
matchState: credsPieces[3], |
| 230 |
state: credsPieces[4], |
| 231 |
multistage: credsPieces[5] == "true", |
| 232 |
}, nil |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
func credsFromFilename(file string) ([]credential, error) { |
| 237 |
fileContents, err := os.ReadFile(file) |
| 238 |
if err != nil { |
| 239 |
return nil, fmt.Errorf("Error opening %q: %s", file, err) |
| 240 |
} |
| 241 |
lines := strings.Split(strings.TrimSpace(string(fileContents)), "\n") |
| 242 |
creds := make([]credential, 0, len(lines)) |
| 243 |
for _, line := range lines { |
| 244 |
cred, err := parseOneCredential(line, file) |
| 245 |
if err != nil { |
| 246 |
return nil, err |
| 247 |
} |
| 248 |
creds = append(creds, cred) |
| 249 |
} |
| 250 |
return creds, nil |
| 251 |
} |
| 252 |
|
| 253 |
func log() { |
| 254 |
fmt.Fprintf(os.Stderr, "CREDS received command: %s (ignored)\n", os.Args[1]) |
| 255 |
} |
| 256 |
|
| 257 |
func firstEntryForKey(input map[string][]string, key string) string { |
| 258 |
if val, ok := input[key]; ok && len(val) > 0 { |
| 259 |
return val[0] |
| 260 |
} |
| 261 |
return "" |
| 262 |
} |