Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

feat(rexctl): read and atomically write .env files

Hampuse956f5e648be
diff --git a/fluxer_rexctl/internal/envfile/atomic.go b/fluxer_rexctl/internal/envfile/atomic.go
new file mode 100644
index 000000000..479b7ec39
--- /dev/null
+++ b/fluxer_rexctl/internal/envfile/atomic.go
@@ -0,0 +1,59 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+package envfile
+
+import (
+ "os"
+ "path/filepath"
+)
+
+func WriteFileAtomic(path string, data []byte, private bool) error {
+ dir := filepath.Dir(path)
+ old, statErr := os.Stat(path)
+ mode := os.FileMode(0o644)
+ if private {
+ mode = 0o600
+ } else if statErr == nil {
+ mode = old.Mode().Perm()
+ }
+ tmp, err := os.CreateTemp(dir, "."+filepath.Base(path)+".tmp-*")
+ if err != nil {
+ return err
+ }
+ name := tmp.Name()
+ done := false
+ defer func() {
+ if !done {
+ tmp.Close()
+ os.Remove(name)
+ }
+ }()
+ if err := tmp.Chmod(mode); err != nil {
+ return err
+ }
+ if private {
+ if err := restrictToOwner(name); err != nil {
+ return err
+ }
+ }
+ if statErr == nil {
+ keepOwner(tmp, old)
+ }
+ if _, err := tmp.Write(data); err != nil {
+ return err
+ }
+ if err := tmp.Sync(); err != nil {
+ return err
+ }
+ if err := tmp.Close(); err != nil {
+ return err
+ }
+ if err := os.Rename(name, path); err != nil {
+ os.Remove(name)
+ done = true
+ return err
+ }
+ done = true
+ syncDir(dir)
+ return nil
+}
diff --git a/fluxer_rexctl/internal/envfile/envfile.go b/fluxer_rexctl/internal/envfile/envfile.go
new file mode 100644
index 000000000..0f5090e8f
--- /dev/null
+++ b/fluxer_rexctl/internal/envfile/envfile.go
@@ -0,0 +1,347 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+package envfile
+
+import (
+ "bytes"
+ "errors"
+ "fmt"
+ "os"
+ "regexp"
+ "strings"
+)
+
+var ErrDuplicateKey = errors.New("envfile: duplicate key")
+
+type DuplicateKeyError struct {
+ Key string
+ Lines []int
+}
+
+func (e *DuplicateKeyError) Error() string {
+ return fmt.Sprintf("%s is set more than once", e.Key)
+}
+
+func (e *DuplicateKeyError) Unwrap() error { return ErrDuplicateKey }
+
+type line struct {
+ raw string
+ key string
+ value string
+ num int
+}
+
+type File struct {
+ path string
+ lines []line
+}
+
+func New(path string) *File { return &File{path: path} }
+
+func Load(path string) (*File, error) {
+ data, err := os.ReadFile(path)
+ if err != nil {
+ return nil, err
+ }
+ return Parse(path, data)
+}
+
+func Parse(path string, data []byte) (*File, error) {
+ text := strings.ReplaceAll(strings.TrimPrefix(string(data), "\uFEFF"), "\r", "")
+ f := &File{path: path}
+ num := 1
+ for text != "" {
+ start := num
+ raw, key, value, rest := splitEntry(text)
+ num += strings.Count(raw, "\n") + 1
+ f.lines = append(f.lines, line{raw: raw, key: key, value: value, num: start})
+ text = rest
+ }
+ if dups := f.Duplicates(); len(dups) > 0 {
+ return f, dups[0]
+ }
+ return f, nil
+}
+
+func splitEntry(text string) (raw, key, value, rest string) {
+ first, after, _ := strings.Cut(text, "\n")
+ body := strings.TrimLeft(first, " \t")
+ if body == "" || body[0] == '#' {
+ return first, "", "", after
+ }
+ body = strings.TrimPrefix(body, "export ")
+ body = strings.TrimLeft(body, " \t")
+ name, val, ok := strings.Cut(body, "=")
+ name = strings.TrimRight(name, " \t")
+ if !ok || !ValidKey(name) {
+ return first, "", "", after
+ }
+ val = strings.TrimLeft(val, " \t")
+ offset := len(first) - len(val)
+ if val != "" && (val[0] == '\'' || val[0] == '"') {
+ if v, n, closed := readQuoted(text[offset:]); closed {
+ end := offset + n
+ tail, next, _ := strings.Cut(text[end:], "\n")
+ return text[:end+len(tail)], name, v, next
+ }
+ }
+ v, _, _ := strings.Cut(val, " #")
+ v = strings.TrimRight(v, " \t")
+ return first, name, strings.ReplaceAll(v, "$$", "$"), after
+}
+
+func readQuoted(s string) (value string, n int, closed bool) {
+ quote := s[0]
+ var b strings.Builder
+ escaped := false
+ for i := 1; i < len(s); i++ {
+ c := s[i]
+ if c != quote {
+ if !escaped && c == '\\' {
+ escaped = true
+ continue
+ }
+ if escaped {
+ escaped = false
+ b.WriteByte('\\')
+ }
+ b.WriteByte(c)
+ continue
+ }
+ if escaped {
+ escaped = false
+ b.WriteByte(c)
+ continue
+ }
+ if quote == '"' {
+ return unescapeDouble(b.String()), i + 1, true
+ }
+ return b.String(), i + 1, true
+ }
+ return "", 0, false
+}
+
+func unescapeDouble(s string) string {
+ var b strings.Builder
+ for i := 0; i < len(s); i++ {
+ c := s[i]
+ if c == '$' && i+1 < len(s) && s[i+1] == '$' {
+ b.WriteByte('$')
+ i++
+ continue
+ }
+ if c != '\\' || i+1 >= len(s) {
+ b.WriteByte(c)
+ continue
+ }
+ switch s[i+1] {
+ case 'n':
+ b.WriteByte('\n')
+ case 't':
+ b.WriteByte('\t')
+ case 'r':
+ b.WriteByte('\r')
+ case '\\', '"', '$':
+ b.WriteByte(s[i+1])
+ default:
+ b.WriteByte(c)
+ continue
+ }
+ i++
+ }
+ return b.String()
+}
+
+func ValidKey(k string) bool {
+ if k == "" {
+ return false
+ }
+ for i, c := range k {
+ switch {
+ case c == '_', c >= 'A' && c <= 'Z', c >= 'a' && c <= 'z':
+ case c >= '0' && c <= '9' && i > 0:
+ case c == '.' || c == '-':
+ if i == 0 {
+ return false
+ }
+ default:
+ return false
+ }
+ }
+ return true
+}
+
+func (f *File) Get(key string) (string, bool) {
+ for i := len(f.lines) - 1; i >= 0; i-- {
+ if f.lines[i].key == key {
+ return f.lines[i].value, true
+ }
+ }
+ return "", false
+}
+
+func (f *File) Value(key string) string {
+ v, _ := f.Get(key)
+ return v
+}
+
+func (f *File) Set(key, value string) {
+ set := line{raw: key + "=" + Quote(value), key: key, value: value}
+ out := f.lines[:0]
+ done := false
+ for _, l := range f.lines {
+ if l.key != key {
+ out = append(out, l)
+ continue
+ }
+ if !done {
+ out = append(out, set)
+ done = true
+ }
+ }
+ f.lines = out
+ if !done {
+ f.lines = append(f.lines, set)
+ }
+}
+
+func (f *File) Delete(key string) {
+ out := f.lines[:0]
+ for _, l := range f.lines {
+ if l.key != key {
+ out = append(out, l)
+ }
+ }
+ f.lines = out
+}
+
+func (f *File) Keys() []string {
+ keys := []string{}
+ seen := map[string]bool{}
+ for _, l := range f.lines {
+ if l.key != "" && !seen[l.key] {
+ seen[l.key] = true
+ keys = append(keys, l.key)
+ }
+ }
+ return keys
+}
+
+func (f *File) Map() map[string]string {
+ m := make(map[string]string)
+ for _, l := range f.lines {
+ if l.key != "" {
+ m[l.key] = l.value
+ }
+ }
+ return m
+}
+
+func (f *File) Duplicates() []*DuplicateKeyError {
+ var out []*DuplicateKeyError
+ byKey := map[string]*DuplicateKeyError{}
+ for _, l := range f.lines {
+ if l.key == "" {
+ continue
+ }
+ d, ok := byKey[l.key]
+ if !ok {
+ d = &DuplicateKeyError{Key: l.key}
+ byKey[l.key] = d
+ }
+ d.Lines = append(d.Lines, l.num)
+ if len(d.Lines) == 2 {
+ out = append(out, d)
+ }
+ }
+ return out
+}
+
+func (f *File) Bytes() []byte {
+ var b bytes.Buffer
+ for _, l := range f.lines {
+ b.WriteString(l.raw)
+ b.WriteByte('\n')
+ }
+ return b.Bytes()
+}
+
+func (f *File) Save() error {
+ return WriteFileAtomic(f.path, f.Bytes(), true)
+}
+
+func Quote(value string) string {
+ if plain(value) {
+ return value
+ }
+ if !strings.ContainsAny(value, "'\n\r") && !strings.HasSuffix(value, `\`) {
+ return "'" + value + "'"
+ }
+ var b strings.Builder
+ b.WriteByte('"')
+ for i := 0; i < len(value); i++ {
+ switch c := value[i]; c {
+ case '\\':
+ b.WriteString(`\\`)
+ case '"':
+ b.WriteString(`\"`)
+ case '$':
+ b.WriteString(`$$`)
+ case '\n':
+ b.WriteString(`\n`)
+ case '\r':
+ b.WriteString(`\r`)
+ default:
+ b.WriteByte(c)
+ }
+ }
+ b.WriteByte('"')
+ return b.String()
+}
+
+func plain(v string) bool {
+ for _, c := range v {
+ switch {
+ case c >= 'a' && c <= 'z', c >= 'A' && c <= 'Z', c >= '0' && c <= '9':
+ case strings.ContainsRune("-_.,:/@+=%~^", c):
+ default:
+ return false
+ }
+ }
+ return true
+}
+
+var refPattern = regexp.MustCompile(`\$\$|\$\{([A-Za-z_][A-Za-z0-9_]*)(?:(:?[-?+])([^}]*))?\}|\$([A-Za-z_][A-Za-z0-9_]*)`)
+
+func Expand(value string, lookup func(string) (string, bool)) (string, bool) {
+ ok := !strings.Contains(refPattern.ReplaceAllString(value, ""), "$")
+ out := refPattern.ReplaceAllStringFunc(value, func(m string) string {
+ if m == "$$" {
+ return m
+ }
+ sub := refPattern.FindStringSubmatch(m)
+ name, op, def := sub[1], sub[2], sub[3]
+ if name == "" {
+ name = sub[4]
+ }
+ v, found := lookup(name)
+ switch {
+ case !found && (op == ":-" || op == "-"):
+ return def
+ case !found:
+ ok = false
+ return m
+ case op == ":-" && v == "":
+ return def
+ case op == ":+":
+ if v != "" {
+ return def
+ }
+ return ""
+ case op == "+":
+ return def
+ }
+ return v
+ })
+ return out, ok
+}
diff --git a/fluxer_rexctl/internal/envfile/perm_unix.go b/fluxer_rexctl/internal/envfile/perm_unix.go
new file mode 100644
index 000000000..dac9aba4a
--- /dev/null
+++ b/fluxer_rexctl/internal/envfile/perm_unix.go
@@ -0,0 +1,40 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+//go:build !windows
+
+package envfile
+
+import (
+ "os"
+ "syscall"
+)
+
+func restrictToOwner(string) error { return nil }
+
+func keepOwner(f *os.File, old os.FileInfo) {
+ st, ok := old.Sys().(*syscall.Stat_t)
+ if !ok || os.Geteuid() != 0 {
+ return
+ }
+ _ = f.Chown(int(st.Uid), int(st.Gid))
+}
+
+func syncDir(dir string) {
+ d, err := os.Open(dir)
+ if err != nil {
+ return
+ }
+ _ = d.Sync()
+ _ = d.Close()
+}
+
+func RestrictToOwner(path string) error {
+ info, err := os.Stat(path)
+ if err != nil {
+ return err
+ }
+ if info.IsDir() {
+ return os.Chmod(path, 0o700)
+ }
+ return os.Chmod(path, 0o600)
+}
diff --git a/fluxer_rexctl/internal/envfile/perm_windows.go b/fluxer_rexctl/internal/envfile/perm_windows.go
new file mode 100644
index 000000000..3801bd2b1
--- /dev/null
+++ b/fluxer_rexctl/internal/envfile/perm_windows.go
@@ -0,0 +1,77 @@
+// SPDX-License-Identifier: AGPL-3.0-or-later
+
+//go:build windows
+
+package envfile
+
+import (
+ "os"
+
+ "golang.org/x/sys/windows"
+)
+
+func currentUser() (*windows.SID, error) {
+ u, err := windows.GetCurrentProcessToken().GetTokenUser()
+ if err != nil {
+ return nil, err
+ }
+ return u.User.Sid.Copy()
+}
+
+func trustedSIDs() ([]*windows.SID, error) {
+ user, err := currentUser()
+ if err != nil {
+ return nil, err
+ }
+ system, err := windows.CreateWellKnownSid(windows.WinLocalSystemSid)
+ if err != nil {
+ return nil, err
+ }
+ admins, err := windows.CreateWellKnownSid(windows.WinBuiltinAdministratorsSid)
+ if err != nil {
+ return nil, err
+ }
+ return []*windows.SID{user, system, admins}, nil
+}
+
+func restrictToOwner(path string) error {
+ sids, err := trustedSIDs()
+ if err != nil {
+ return err
+ }
+ inherit := uint32(windows.NO_INHERITANCE)
+ if info, err := os.Stat(path); err == nil && info.IsDir() {
+ inherit = windows.SUB_CONTAINERS_AND_OBJECTS_INHERIT
+ }
+ entries := make([]windows.EXPLICIT_ACCESS, len(sids))
+ for i, sid := range sids {
+ kind := windows.TRUSTEE_TYPE(windows.TRUSTEE_IS_WELL_KNOWN_GROUP)
+ if i == 0 {
+ kind = windows.TRUSTEE_IS_USER
+ }
+ entries[i] = windows.EXPLICIT_ACCESS{
+ AccessPermissions: windows.GENERIC_ALL,
+ AccessMode: windows.SET_ACCESS,
+ Inheritance: inherit,
+ Trustee: windows.TRUSTEE{
+ TrusteeForm: windows.TRUSTEE_IS_SID,
+ TrusteeType: kind,
+ TrusteeValue: windows.TrusteeValueFromSID(sid),
+ },
+ }
+ }
+ acl, err := windows.ACLFromEntries(entries, nil)
+ if err != nil {
+ return err
+ }
+ return windows.SetNamedSecurityInfo(path, windows.SE_FILE_OBJECT,
+ windows.DACL_SECURITY_INFORMATION|windows.PROTECTED_DACL_SECURITY_INFORMATION, nil, nil, acl, nil)
+}
+
+func keepOwner(*os.File, os.FileInfo) {}
+
+func syncDir(string) {}
+
+func RestrictToOwner(path string) error {
+ return restrictToOwner(path)
+}