49 lines
1.1 KiB
Go
49 lines
1.1 KiB
Go
|
package finger
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"context"
|
||
|
"errors"
|
||
|
"os/exec"
|
||
|
|
||
|
"tildegit.org/tjp/gus"
|
||
|
)
|
||
|
|
||
|
// ListingDenied is returned to reject online user listing requests.
|
||
|
var ListingDenied = errors.New("Finger online user list denied.")
|
||
|
|
||
|
// SystemFinger handles finger requests by invoking the finger(1) command-line utility.
|
||
|
func SystemFinger(allowListings bool) gus.Handler {
|
||
|
return func(ctx context.Context, request *gus.Request) *gus.Response {
|
||
|
fingerPath, err := exec.LookPath("finger")
|
||
|
if err != nil {
|
||
|
_ = request.Server.LogError(
|
||
|
"msg", "handler failure",
|
||
|
"ctx", "exec.LookPath(\"finger\")",
|
||
|
"err", err,
|
||
|
)
|
||
|
return Error("Could not resolve request.")
|
||
|
}
|
||
|
|
||
|
path := request.Path[1:]
|
||
|
|
||
|
if len(path) == 0 && !allowListings {
|
||
|
return Error(ListingDenied.Error())
|
||
|
}
|
||
|
|
||
|
args := make([]string, 0, 1)
|
||
|
if len(path) > 0 {
|
||
|
args = append(args, path)
|
||
|
}
|
||
|
|
||
|
cmd := exec.CommandContext(ctx, fingerPath, args...)
|
||
|
outbuf := &bytes.Buffer{}
|
||
|
cmd.Stdout = outbuf
|
||
|
|
||
|
if err := cmd.Run(); err != nil {
|
||
|
return Error(err.Error())
|
||
|
}
|
||
|
return Success(outbuf)
|
||
|
}
|
||
|
}
|