| 1 |
// Copyright Martin Dosch. |
| 2 |
// Use of this source code is governed by the BSD-2-clause |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
// TODO: Add logging |
| 6 |
package main |
| 7 |
|
| 8 |
import ( |
| 9 |
"bufio" |
| 10 |
"fmt" |
| 11 |
"log" |
| 12 |
"log/slog" |
| 13 |
"os" |
| 14 |
"os/exec" |
| 15 |
"os/user" |
| 16 |
"runtime" |
| 17 |
"strconv" |
| 18 |
"strings" |
| 19 |
) |
| 20 |
|
| 21 |
func findConfig() (string, error) { |
| 22 |
// Get the current user. |
| 23 |
curUser, err := user.Current() |
| 24 |
if err != nil { |
| 25 |
return "", fmt.Errorf("find config: failed to get current user: %w", err) |
| 26 |
} |
| 27 |
// Get home directory. |
| 28 |
home := curUser.HomeDir |
| 29 |
if home == "" { |
| 30 |
return "", fmt.Errorf("find config: no home directory found") |
| 31 |
} |
| 32 |
osConfigDir := os.Getenv("XDG_CONFIG_HOME") |
| 33 |
if osConfigDir == "" { |
| 34 |
osConfigDir = home + "/.config" |
| 35 |
} |
| 36 |
configFiles := [3]string{ |
| 37 |
osConfigDir + "/go-sendxmpp/config", |
| 38 |
osConfigDir + "/go-sendxmpp/sendxmpprc", |
| 39 |
home + "/.sendxmpprc", |
| 40 |
} |
| 41 |
|
| 42 |
for _, r := range configFiles { |
| 43 |
// Check that the config file is existing. |
| 44 |
_, err := os.Stat(r) |
| 45 |
if err == nil { |
| 46 |
return r, nil |
| 47 |
} |
| 48 |
} |
| 49 |
return "", fmt.Errorf("find config: no configuration file found") |
| 50 |
} |
| 51 |
|
| 52 |
// Opens the config file and returns the specified values |
| 53 |
// for username, server and port. |
| 54 |
func parseConfig(configPath string) (output configuration, err error) { |
| 55 |
// Set default OX trust mode to "blind" |
| 56 |
output.oxTrustMode = "blind" |
| 57 |
// Use $XDG_CONFIG_HOME/.config/go-sendxmpp/config, |
| 58 |
// $XDG_CONFIG_HOME/.config/go-sendxmpp/sendxmpprc or |
| 59 |
// ~/.sendxmpprc if no config path is specified. |
| 60 |
// Get systems user config path. |
| 61 |
if configPath == "" { |
| 62 |
configPath, err = findConfig() |
| 63 |
if err != nil { |
| 64 |
log.Fatal(err) |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
// Only check file permissions if we are not running on windows. |
| 69 |
if runtime.GOOS != "windows" { |
| 70 |
info, err := os.Stat(configPath) |
| 71 |
if err != nil { |
| 72 |
log.Fatal(err) |
| 73 |
} |
| 74 |
// Check for file permissions. Must be 600, 640, 440 or 400. |
| 75 |
perm := info.Mode().Perm() |
| 76 |
permissions := strconv.FormatInt(int64(perm), 8) |
| 77 |
if permissions != "600" && permissions != "640" && permissions != "440" && permissions != "400" { |
| 78 |
return output, fmt.Errorf("parse config: wrong permissions for %s: %s instead of 400 (recommended), 440, 600 or 640", configPath, permissions) |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
// Open config file. |
| 83 |
// #nosec G304 -- config path comes from the local system, it's no untrusted input from the network |
| 84 |
file, err := os.Open(configPath) |
| 85 |
if err != nil { |
| 86 |
return output, fmt.Errorf("parse config: failed to open config file: %w", err) |
| 87 |
} |
| 88 |
scanner := bufio.NewScanner(file) |
| 89 |
scanner.Split(bufio.ScanLines) |
| 90 |
|
| 91 |
// Read config file per line. |
| 92 |
for scanner.Scan() { |
| 93 |
if strings.HasPrefix(scanner.Text(), "#") { |
| 94 |
continue |
| 95 |
} |
| 96 |
|
| 97 |
column := strings.SplitN(scanner.Text(), " ", defaultConfigColumnSep) |
| 98 |
|
| 99 |
if len(column) > 1 { |
| 100 |
switch strings.ToLower(column[0]) { |
| 101 |
case "username:": |
| 102 |
output.username = column[1] |
| 103 |
case "jserver:": |
| 104 |
output.jserver = column[1] |
| 105 |
case "password:": |
| 106 |
output.password = column[1] |
| 107 |
case "eval_password:": |
| 108 |
shell := os.Getenv("SHELL") |
| 109 |
if shell == "" { |
| 110 |
shell = "/bin/sh" |
| 111 |
} |
| 112 |
// #nosec G204,G702 -- command to unlock password manager is supplied by the user |
| 113 |
// in the config and no untrusted data |
| 114 |
out, err := exec.Command(shell, "-c", column[1]).Output() |
| 115 |
if err != nil { |
| 116 |
err2 := file.Close() |
| 117 |
if err2 != nil { |
| 118 |
slog.Warn("closing file failed", "error", err) |
| 119 |
} |
| 120 |
log.Fatal(err) |
| 121 |
} |
| 122 |
output.password = string(out) |
| 123 |
if output.password[len(output.password)-1] == '\n' { |
| 124 |
output.password = output.password[:len(output.password)-1] |
| 125 |
} |
| 126 |
case "port:": |
| 127 |
output.port = column[1] |
| 128 |
case "alias:": |
| 129 |
output.alias = column[1] |
| 130 |
case "allow_plain:": |
| 131 |
if strings.ToLower(column[1]) == "true" { |
| 132 |
output.allowPLAIN = true |
| 133 |
} else { |
| 134 |
output.allowPLAIN = false |
| 135 |
} |
| 136 |
case "no_root_warning:": |
| 137 |
if strings.ToLower(column[1]) == "true" { |
| 138 |
output.noRootWarn = true |
| 139 |
} else { |
| 140 |
output.noRootWarn = false |
| 141 |
} |
| 142 |
case "no_legacy_pgp_warning:": |
| 143 |
if strings.ToLower(column[1]) == "true" { |
| 144 |
output.noLPGPWarn = true |
| 145 |
} else { |
| 146 |
output.noLPGPWarn = false |
| 147 |
} |
| 148 |
case "ox_trust_mode:": |
| 149 |
switch strings.ToLower(column[1]) { |
| 150 |
case "tofu", "blind": |
| 151 |
output.oxTrustMode = strings.ToLower(column[1]) |
| 152 |
} |
| 153 |
default: |
| 154 |
// Try to parse legacy sendxmpp config files. |
| 155 |
if len(column) >= defaultConfigColumnSep && strings.Contains(column[0], "@") { |
| 156 |
if strings.Contains(scanner.Text(), ";") { |
| 157 |
output.username = strings.Split(column[0], ";")[0] |
| 158 |
output.jserver = strings.Split(column[0], ";")[1] |
| 159 |
output.password = column[1] |
| 160 |
} else { |
| 161 |
output.username = strings.Split(column[0], ":")[0] |
| 162 |
if strings.Contains(output.username, "@") { |
| 163 |
jserver := strings.SplitAfter(output.username, "@")[1] |
| 164 |
if len(jserver) < defaultLenServerConf { |
| 165 |
log.Fatal("Couldn't parse config: ", column[0]) |
| 166 |
} |
| 167 |
output.jserver = jserver |
| 168 |
} |
| 169 |
output.password = column[1] |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
// Check if the username is a valid JID |
| 177 |
output.username, err = MarshalJID(output.username) |
| 178 |
if err != nil { |
| 179 |
// Check whether only the local part was used by appending an @ and the |
| 180 |
// server part. |
| 181 |
output.username = output.username + "@" + output.jserver |
| 182 |
// Check if the username is a valid JID now |
| 183 |
output.username, err = MarshalJID(output.username) |
| 184 |
if err != nil { |
| 185 |
return output, fmt.Errorf("parse config: invalid username/JID: %s", output.username) |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
err2 := file.Close() |
| 190 |
if err2 != nil { |
| 191 |
slog.Warn("closing file failed", "error", err) |
| 192 |
} |
| 193 |
return output, err |
| 194 |
} |