@@ -18,8 +18,10 @@ import (
1818 "log"
1919 "net"
2020 "os"
21+ "os/user"
2122 "sort"
2223 "strconv"
24+ "strings"
2325 "time"
2426
2527 "github.com/cea-hpc/sshproxy/pkg/utils"
@@ -443,6 +445,7 @@ The commands are:
443445 forget forget a host in etcd
444446 disable disable a host in etcd
445447 error_banner set the error banner in etcd
448+ get_config display the calculated configuration
446449
447450The common options are:
448451` , os .Args [0 ])
@@ -552,6 +555,24 @@ The options are:
552555 return fs
553556}
554557
558+ func newGetConfigParser (userFlag * string , groupsFlag * string ) * flag.FlagSet {
559+ fs := flag .NewFlagSet ("get_config" , flag .ExitOnError )
560+ fs .StringVar (userFlag , "user" , "" , "get the config for this specific user" )
561+ fs .StringVar (groupsFlag , "groups" , "" , "get the config for these specific groups (comma separated)" )
562+ fs .Usage = func () {
563+ fmt .Fprintf (flag .CommandLine .Output (), `Usage: %s get_config [-user USER] [-groups GROUPS]
564+
565+ Display the calculated configuration. If a user is given, its system groups (if
566+ any) are added to the given groups.
567+
568+ The options are:
569+ ` , os .Args [0 ])
570+ fs .PrintDefaults ()
571+ os .Exit (2 )
572+ }
573+ return fs
574+ }
575+
555576func getHostPortFromCommandLine (args []string ) ([]string , []string , error ) {
556577 hostsNodeset , portsNodeset := "" , defaultHostPort
557578 switch len (args ) {
@@ -645,6 +666,8 @@ func main() {
645666 var jsonFlag bool
646667 var allFlag bool
647668 var expire string
669+ var userString string
670+ var groupsString string
648671
649672 parsers := map [string ]* flag.FlagSet {
650673 "help" : newHelpParser (),
@@ -654,6 +677,7 @@ func main() {
654677 "forget" : newForgetParser (),
655678 "disable" : newDisableParser (),
656679 "error_banner" : newErrorBannerParser (& expire ),
680+ "get_config" : newGetConfigParser (& userString , & groupsString ),
657681 }
658682
659683 cmd := flag .Arg (0 )
@@ -759,6 +783,33 @@ func main() {
759783 p .Usage ()
760784 }
761785 setErrorBanner (errorBanner , t , * configFile )
786+ case "get_config" :
787+ p := parsers [cmd ]
788+ p .Parse (args )
789+ groupsMap := make (map [string ]bool )
790+ userComment := ""
791+ // get system groups of given user, if it exists
792+ userObject , err := user .Lookup (userString )
793+ if err != nil {
794+ userComment = " (unknown on this system)"
795+ } else {
796+ groupsMap , _ = utils .GetGroupUser (userObject )
797+ }
798+ // add given groups to system groups
799+ for _ , group := range strings .Split (groupsString , "," ) {
800+ if group != "" {
801+ groupsMap [group ] = true
802+ }
803+ }
804+ // get config for given user / groups
805+ config , err := utils .LoadConfig (* configFile , userString , "" , time .Now (), groupsMap )
806+ if err != nil {
807+ log .Fatalf ("reading configuration file %s: %v" , * configFile , err )
808+ }
809+ fmt .Fprintf (os .Stdout , "user = %s%s\n " , userString , userComment )
810+ for _ , configLine := range utils .PrintConfig (config , groupsMap ) {
811+ fmt .Fprintln (os .Stdout , configLine )
812+ }
762813 default :
763814 fmt .Fprintf (os .Stderr , "ERROR: unknown command: %s\n \n " , cmd )
764815 usage ()
0 commit comments