diff --git a/cmd/utils.go b/cmd/utils.go index 545fd76..dd19b38 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -365,9 +365,83 @@ func executeCommandWithOutputToStdout(cmdline string) (error) { return fmt.Errorf("Unknown error: you shouldn't be here!\n") } +/* Execute command plugging stdin and stdout to those of the running command. + * Blocking while the command run + */ +func executeCommandWithStdinStdoutStderr(cmdline string) (error) { + var cmd []string + + if gUseSudo { + cmd = append(cmd, "sudo") + } + + var word string + var in_escaped bool + // Split by words, or " enclosed words + for i, c := range (cmdline) { + if string(c) == "\"" { + if in_escaped { + // This is the closing " + cmd = append(cmd, word) + in_escaped = false + } else { + in_escaped = true + } + continue + } + if string(c) == " " { + if in_escaped { + word = word + string(c) + continue + } else { + cmd = append(cmd, word) + word = "" + continue + } + } + if i == (len(cmdline) - 1) { + word = word + string(c) + cmd = append(cmd, word) + break + } + + // else + word = word + string(c) + } + + var command *exec.Cmd + if len(cmd) > 1 { + command = exec.Command(cmd[0], cmd[1:]...) + } else { + command = exec.Command(cmd[0]) + } + + // Get environment + command.Env = os.Environ() + + // Connect command to current stdin/out/err + command.Stdin = os.Stdin + command.Stdout = os.Stdout + command.Stderr = os.Stderr + + if err := command.Start(); err != nil { + return err + } + + err := command.Wait() + + return err +} + + func executeCommandInJail(jail *Jail, cmdline string) (string, error) { var cmd []string + // We can't execute on non-running jail + if jail.Running == false { + return "", errors.New("Can't execute command on stopped jail") + } + if gUseSudo { cmd = append(cmd, "sudo") } @@ -414,6 +488,10 @@ func executeCommandInJail(jail *Jail, cmdline string) (string, error) { // else word = word + string(c) } + + if gDebug { + fmt.Printf("DEBUG: executeCommandInJail: prepare to execute \"%s\"\n", cmd) + } out, err := exec.Command(cmd[0], cmd[1:]...).CombinedOutput() @@ -702,6 +780,33 @@ func copyDevfsRuleset(ruleset int, srcrs int) error { return nil } +/******************************************************************************** + * Add a rule to specified ruleset + * Ex.: addDevfsRuleToRuleset("path bpf* unhide", 1002) + *******************************************************************************/ +func addDevfsRuleToRuleset(rule string, ruleset int) error { + // TODO: Check if rule not already enabled. We will need to recurse into includes. + // Get last rule index + rules := getDevfsRuleset(ruleset) + if len(rules) == 0 { + fmt.Printf("Error listing ruleset %d\n", ruleset) + return errors.New(fmt.Sprintf("Error listing rueset %d\n", ruleset)) + } + + f := strings.Fields(rules[(len(rules)-1)]) + //fmt.Printf("Dernier index du ruleset %d: %s\n", ruleset, f[0]) + index, _ := strconv.Atoi(f[0]) + index += 100 + + cmd := fmt.Sprintf("/sbin/devfs rule -s %d add %d %s", ruleset, index, rule) + out, err := executeCommand(cmd) + if err != nil { + return errors.New(fmt.Sprintf("Error adding rule \"%s\" to ruleset %d: %s", rule, ruleset, out)) + } + + return nil +} + /******************************************************************************** * Returns value of parameter as read in /var/run/jail.$InternalName.conf * Directives without value will return "true" if found @@ -730,32 +835,6 @@ func getValueFromRunningConfig(jname string, param string) (string, error) { return "", fmt.Errorf("Parameter not found: %s", param) } -/******************************************************************************** - * Add a rule to specified ruleset - * Ex.: addDevfsRuleToRuleset("path bpf* unhide", 1002) - *******************************************************************************/ -func addDevfsRuleToRuleset(rule string, ruleset int) error { - // TODO: Check if rule not already enabled. We will need to recurse into includes. - // Get last rule index - rules := getDevfsRuleset(ruleset) - if len(rules) == 0 { - fmt.Printf("Error listing ruleset %d\n", ruleset) - return errors.New(fmt.Sprintf("Error listing rueset %d\n", ruleset)) - } - - f := strings.Fields(rules[(len(rules)-1)]) - //fmt.Printf("Dernier index du ruleset %d: %s\n", ruleset, f[0]) - index, _ := strconv.Atoi(f[0]) - index += 100 - - cmd := fmt.Sprintf("/sbin/devfs rule -s %d add %d %s", ruleset, index, rule) - out, err := executeCommand(cmd) - if err != nil { - return errors.New(fmt.Sprintf("Error adding rule \"%s\" to ruleset %d: %s", rule, ruleset, out)) - } - - return nil -} /******************************************************************************