-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/enter: honor user’s configured shell inside toolbox
Signed-off-by: Yann Soubeyrand <yann.soubeyrand@gmx.fr>
- Loading branch information
1 parent
e80cba4
commit 8d78eef
Showing
8 changed files
with
122 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ manuals = { | |
'toolbox-rm', | ||
'toolbox-rmi', | ||
'toolbox-run', | ||
'toolbox-sh', | ||
], | ||
'5': [ | ||
'toolbox.conf', | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
% toolbox-sh(1) | ||
|
||
## NAME | ||
toolbox\-sh - Launch user configured shell inside container | ||
|
||
## SYNOPSIS | ||
**toolbox sh** | ||
|
||
## DESCRIPTION | ||
|
||
Launches the user’s configured shell when entering the toolbox. It is meant to | ||
be the command executed when entering a toolbox and must be run inside the | ||
toolbox container. | ||
|
||
A user willing to configure its shell inside the toolbox can do so using | ||
regular tools like `chsh` or `usermod`. | ||
|
||
## SEE ALSO | ||
|
||
`toolbox(1)`, `toolbox-enter(1)`, | ||
`podman(1)`, `podman-create(1)`, `podman-start(1)` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright © 2019 – 2021 Red Hat Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"os/user" | ||
"strings" | ||
"syscall" | ||
|
||
"github.com/containers/toolbox/pkg/utils" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var shCmd = &cobra.Command{ | ||
Use: "sh", | ||
Short: "Launch user configured shell inside container", | ||
Hidden: true, | ||
RunE: sh, | ||
} | ||
|
||
func init() { | ||
shCmd.SetHelpFunc(shHelp) | ||
rootCmd.AddCommand(shCmd) | ||
} | ||
|
||
func sh(cmd *cobra.Command, args []string) error { | ||
if !utils.IsInsideContainer() { | ||
var builder strings.Builder | ||
fmt.Fprintf(&builder, "the 'sh' command can only be used inside containers\n") | ||
fmt.Fprintf(&builder, "Run '%s --help' for usage.", executableBase) | ||
|
||
errMsg := builder.String() | ||
return errors.New(errMsg) | ||
} | ||
|
||
u, err := user.Current() | ||
if err != nil { | ||
return fmt.Errorf("failed to get current user: %w", err) | ||
} | ||
|
||
out, err := exec.Command("getent", "passwd", u.Username).Output() | ||
if err != nil { | ||
if err, ok := err.(*exec.ExitError); ok { | ||
return fmt.Errorf("failed to get user shell: %w", err) | ||
} | ||
return err | ||
} | ||
|
||
s := strings.Split(strings.TrimSuffix(string(out), "\n"), ":")[6] | ||
|
||
err = syscall.Exec(s, []string{"-l"}, os.Environ()) | ||
if err != nil { | ||
return fmt.Errorf("failed to execute user shell: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func shHelp(cmd *cobra.Command, args []string) { | ||
if utils.IsInsideContainer() { | ||
if !utils.IsInsideToolboxContainer() { | ||
fmt.Fprintf(os.Stderr, "Error: this is not a toolbox container\n") | ||
return | ||
} | ||
|
||
if _, err := utils.ForwardToHost(); err != nil { | ||
fmt.Fprintf(os.Stderr, "Error: %s\n", err) | ||
return | ||
} | ||
|
||
return | ||
} | ||
|
||
if err := showManual("toolbox-sh"); err != nil { | ||
fmt.Fprintf(os.Stderr, "Error: %s\n", err) | ||
return | ||
} | ||
} |