add api and tool for query mxpassfiles, fixes

This commit is contained in:
saces 2026-03-13 13:02:30 +01:00
parent 41fa795971
commit 8df08611a9
7 changed files with 95 additions and 20 deletions

View file

@ -87,28 +87,20 @@ func parseLine(line string) *Entry {
} }
} }
func isWild(s string) bool { func superCMP(fileitem, filteritem string) bool {
if s == "" || s == "*" { if filteritem == "*" {
return true return true
} }
return false return fileitem == filteritem
}
func superCMP(s1, s2 string) bool {
if isWild(s1) || isWild(s2) {
return true
}
//fmt.Printf("sCMP: '%s' '%s'\n", s1, s2)
return s1 == s2
} }
// FindPassword finds the password for the provided synapsehost, localpart, and domain. An empty // FindPassword finds the password for the provided synapsehost, localpart, and domain. An empty
// string will be returned if no match is found. // string will be returned if no match is found.
func (pf *Passfile) FindPassword(matrixhost, localpart, domain string) string { func (pf *Passfile) FindPassword(matrixhost, localpart, domain string) string {
for _, e := range pf.Entries { for _, e := range pf.Entries {
if (e.Matrixhost == "*" || e.Matrixhost == matrixhost) && if superCMP(e.Matrixhost, matrixhost) &&
(e.Localpart == "*" || e.Localpart == localpart) && superCMP(e.Localpart, localpart) &&
(e.Domain == "*" || e.Domain == domain) { superCMP(e.Domain, domain) {
return e.Token return e.Token
} }
} }

View file

@ -170,6 +170,20 @@ func cliv0_serverinfo(url *C.char) *C.char {
return C.CString(result) return C.CString(result)
} }
//export cliv0_mxpassitem
func cliv0_mxpassitem(mxpassfile_path *C.char, url *C.char, localpart *C.char, domain *C.char) *C.char {
item, err := mxutils.GetMXPassItem(C.GoString(mxpassfile_path), C.GoString(url), C.GoString(localpart), C.GoString(domain))
if err != nil {
return C.CString(fmt.Sprintf("ERR: %v", err))
}
out, err := json.Marshal(item)
if err != nil {
return C.CString(fmt.Sprintf("ERR: %v", err))
}
s := string(out)
return C.CString(s)
}
/* /*
high level api, supports multiple clients high level api, supports multiple clients
the same handler can be attached to multiple clients the same handler can be attached to multiple clients

View file

@ -0,0 +1,20 @@
// Copyright (C) 2026 saces@c-base.org
// SPDX-License-Identifier: AGPL-3.0-only
package mxutils
import (
"errors"
"mxclientlib/determinant/mxpassfile"
)
func GetMXPassItem(mxpassfile_path string, url string, localpart string, domain string) (*mxpassfile.Entry, error) {
pf, err := mxpassfile.ReadPassfile(mxpassfile_path)
if err != nil {
return nil, err
}
e := pf.FindPasswordFill(url, localpart, domain)
if e == nil {
return nil, errors.New("no item found.")
}
return e, nil
}

View file

@ -41,6 +41,7 @@ ffibuilder.cdef(
extern char* cliv0_accountinfo(char* hs, char* accessToken); extern char* cliv0_accountinfo(char* hs, char* accessToken);
extern char* cliv0_clearaccount(char* hs, char* accessToken); extern char* cliv0_clearaccount(char* hs, char* accessToken);
extern char* cliv0_serverinfo(char* url); extern char* cliv0_serverinfo(char* url);
extern char* cliv0_mxpassitem(char* mxpassfile, char* hs, char* localpart, char* domain);
extern int apiv0_initialize(); extern int apiv0_initialize();
extern int apiv0_deinitialize(); extern int apiv0_deinitialize();
extern char* apiv0_discover(char* mxid); extern char* apiv0_discover(char* mxid);

View file

@ -38,6 +38,7 @@ mxtoken = "pymxutils.mxutils:mktoken"
mxaccountinfo = "pymxutils.mxutils:accountinfo" mxaccountinfo = "pymxutils.mxutils:accountinfo"
mxclearaccount = "pymxutils.mxutils:clearaccount" mxclearaccount = "pymxutils.mxutils:clearaccount"
mxserverinfo = "pymxutils.mxutils:serverinfo" mxserverinfo = "pymxutils.mxutils:serverinfo"
mxpassitem = "pymxutils.mxutils:passitem"
smalsetup = "smal.smalsetup:smalsetup" smalsetup = "smal.smalsetup:smalsetup"
demobot = "demobot:main" demobot = "demobot:main"
simplebot = "demobot.simple:main" simplebot = "demobot.simple:main"

View file

@ -1,6 +1,7 @@
from .discoverhs import discoverhs from .discoverhs import discoverhs as discoverhs
from .mktoken import mktoken from .mktoken import mktoken as mktoken
from .whoami import whoami from .whoami import whoami as whoami
from .accountinfo import accountinfo from .accountinfo import accountinfo as accountinfo
from .clearaccount import clearaccount from .clearaccount import clearaccount as clearaccount
from .serverinfo import serverinfo from .serverinfo import serverinfo as serverinfo
from .passitem import passitem as passitem

View file

@ -0,0 +1,46 @@
# Copyright (C) 2026 saces@c-base.org
# SPDX-License-Identifier: AGPL-3.0-only
from _pygomx import lib, ffi
import click
import json
@click.command()
@click.option(
"-s", "--secret", "show_secret", is_flag=True, help="print only the secret"
)
@click.option("-u", "--url", "hs_url", metavar="url", help="url selector")
@click.option(
"-l", "--localpart", "localpart", metavar="localpart", help="localpart selector"
)
@click.option("-d", "--domain", "domain", metavar="domain", help="domain selector")
@click.argument("mxpassfile", metavar="mxpassfilepath", required=False)
def passitem(mxpassfile, show_secret, hs_url, localpart, domain):
"""utility to get items from mxpasss files"""
# defaults
if mxpassfile is None:
mxpassfile = ".mxpass"
if hs_url is None:
hs_url = "*"
if localpart is None:
localpart = "*"
if domain is None:
domain = "*"
r = lib.cliv0_mxpassitem(
mxpassfile.encode(encoding="utf-8"),
hs_url.encode(encoding="utf-8"),
localpart.encode(encoding="utf-8"),
domain.encode(encoding="utf-8"),
)
result = ffi.string(r).decode("utf-8")
lib.FreeCString(r)
result_dict = json.loads(result)
if show_secret:
print(result_dict["Token"])
else:
result_dict["Token"] = "***"
print(json.dumps(result_dict))