initial commit before refactor switch

This commit is contained in:
brent saner
2024-12-12 02:22:54 -05:00
commit db081e2699
33 changed files with 3101 additions and 0 deletions

75
args/args_funcs.go Normal file
View File

@@ -0,0 +1,75 @@
package args
import (
`errors`
`os/user`
`strconv`
)
// ModesAndOwners returns the evaluated file UID, file GID, dir UID, dir GID, file mode, and dir mode for a UDS socket.
func (a *Args) ModesAndOwners() (perms UdsPerms, err error) {
var idInt int
var uGid int
var u *user.User
var g *user.Group
var nErr *strconv.NumError = new(strconv.NumError)
perms.FMode = a.SockMode
perms.DMode = a.SockDirMode
// UID is always this user
if u, err = user.Current(); err != nil {
return
}
if perms.UID, err = strconv.Atoi(u.Uid); err != nil {
return
}
if g, err = user.LookupGroupId(u.Gid); err != nil {
return
}
if uGid, err = strconv.Atoi(g.Gid); err != nil {
return
}
perms.FGID = uGid
if a.SockGrp != nil {
// First try a direct GID.
if idInt, err = strconv.Atoi(*a.SockGrp); err != nil {
if errors.As(err, &nErr) {
err = nil
// And then try a group name.
if g, err = user.LookupGroup(*a.SockGrp); err != nil {
return
}
if idInt, err = strconv.Atoi(g.Gid); err != nil {
return
}
} else {
return
}
}
perms.FGID = idInt
}
perms.DGID = uGid
if a.SockDirGrp != nil {
// First try a direct GID.
if idInt, err = strconv.Atoi(*a.SockDirGrp); err != nil {
if errors.As(err, &nErr) {
err = nil
// And then try a group name.
if g, err = user.LookupGroup(*a.SockDirGrp); err != nil {
return
}
if idInt, err = strconv.Atoi(g.Gid); err != nil {
return
}
} else {
return
}
}
perms.DGID = idInt
}
return
}

28
args/types.go Normal file
View File

@@ -0,0 +1,28 @@
package args
import (
`io/fs`
)
type Args struct {
Version bool `short:"v" long:"version" description:"Print the version and exit."`
DetailVersion bool `short:"V" long:"detail" description:"Print detailed version info and exit."`
DoDebug bool `env:"CINFO_DEBUG" short:"d" long:"debug" description:"If specified, enable debug logging. This may log a LOT of information."`
SockMode fs.FileMode `env:"CINFO_FMODE" short:"m" long:"fmode" default:"0o0600" description:"If using a UDS, set the socket file to this permission. This should probably be either 0o0600 or 0o0660."`
SockDirMode fs.FileMode `env:"CINFO_DMODE" short:"M" long:"dmode" default:"0o0700" description:"If using a UDS, attempt to set the directory containing the socket to use this permission. This should probably be either 0o0700 or 0o0770."`
SockGrp *string `env:"CINFO_FGRP" short:"g" long:"fgroup" description:"If specified and using a UDS, attempt to set the socket to this GID/group name. (If unspecified, the default is current user's primary group.)"`
SockDirGrp *string `env:"CINFO_DGRP" short:"G" long:"dgroup" description:"If specified and using a UDS, attempt to set the directory containing the socket to this GID/group name. (If unspecified, the default is current user's primary group.)"`
Listen ListenArgs `positional-args:"true"`
}
type ListenArgs struct {
Listen string `env:"CINFO_URI" positional-arg-name:"LISTEN_URI" default:"unix:///var/run/clientinfo/fcgi.sock" description:"The specification to listen on.\nIf the scheme is 'unix', a FastCGI UDS/IPC socket is used (default); any host, query parameters, etc. component is ignored and the URI path is used to specify the socket.\nIf 'tcp', a FastCGI socket over TCP is opened on the <host:port>.\nIf 'http', an HTTP listener is opened on the <host:port>; any path, query parameters, etc. components are ignored.\nHTTPS is unsupported; terminate with a reverse proxy. All other schemes will cause a fatal error.\nThe default is 'unix:///var/run/clientinfo/fcgi.sock'." validate:"required,uri"`
}
type UdsPerms struct {
UID int
FGID int
DGID int
FMode fs.FileMode
DMode fs.FileMode
}