Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

homeDirSafe option for startProcess() method #63

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ public interface OverthereConnection extends Closeable {
* connection.
*/
OverthereProcess startProcess(CmdLine commandLine);

OverthereProcess startProcess(CmdLine commandLine, boolean homeDirSafe);

/**
* Checks whether a process can be started on this connection.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,13 @@ public void run() {
public OverthereProcess startProcess(CmdLine commandLine) {
throw new UnsupportedOperationException("Cannot start a process on " + this);
}


public OverthereProcess startProcess(CmdLine commandLine,
boolean homeDirSafe) {
throw new UnsupportedOperationException("Cannot start a process on " + this);
}


/**
* Checks whether a process can be started on this connection.
Expand All @@ -411,4 +418,5 @@ public final boolean canStartProcess() {

private static Logger logger = LoggerFactory.getLogger(BaseOverthereConnection.class);


}
47 changes: 46 additions & 1 deletion src/main/java/com/xebialabs/overthere/ssh/SshConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import net.schmizz.sshj.connection.ConnectionException;
import net.schmizz.sshj.connection.channel.direct.PTYMode;
import net.schmizz.sshj.connection.channel.direct.Session;
import net.schmizz.sshj.connection.channel.direct.Session.Shell;
import net.schmizz.sshj.transport.TransportException;
import net.schmizz.sshj.transport.verification.PromiscuousVerifier;
import net.schmizz.sshj.userauth.keyprovider.KeyProvider;
Expand Down Expand Up @@ -263,9 +264,53 @@ public OverthereProcess startProcess(final CmdLine commandLine) {
} catch (SSHException e) {
throw new RuntimeIOException("Cannot execute remote command \"" + cmd.toCommandLine(getHostOperatingSystem(), true) + "\" on " + this, e);
}

}

@Override
public OverthereProcess startProcess(final CmdLine commandLine, boolean homeDirSafe) {
checkNotNull(commandLine, "Cannot execute null command line");
checkArgument(commandLine.getArguments().size() > 0, "Cannot execute empty command line");

CmdLine cmd = processCommandLine(commandLine);
try {

// necessary to invoke PAM to create home dir
// Ozgur Demir <ozgurcd@gmail.com>
if (homeDirSafe) {
logger.debug("Attempting to create a shell to invoke home dir creation");
Shell shell = getSshClient().startSession().startShell();
logger.debug("Shell opened, now closing it");
shell.close();
logger.debug("Shell closed");
}

Session session = getSshClient().startSession();
if (allocatePty != null && !allocatePty.isEmpty()) {
if (allocateDefaultPty) {
logger.warn("The " + ALLOCATE_PTY + " and " + ALLOCATE_DEFAULT_PTY
+ " connection options have both been set for the connection {}. Ignoring "
+ ALLOCATE_DEFAULT_PTY + " and using " + ALLOCATE_PTY + ".", this);
}
Matcher matcher = ptyPattern.matcher(allocatePty);
checkArgument(matcher.matches(), "Value for allocatePty [%s] does not match pattern \"" + PTY_PATTERN + "\"", allocateDefaultPty);

String term = matcher.group(1);
int cols = Integer.valueOf(matcher.group(2));
int rows = Integer.valueOf(matcher.group(3));
int width = Integer.valueOf(matcher.group(4));
int height = Integer.valueOf(matcher.group(5));
logger.debug("Allocating PTY {}:{}:{}:{}:{}", new Object[] { term, cols, rows, width, height });
session.allocatePTY(term, cols, rows, width, height, Collections.<PTYMode, Integer> emptyMap());
} else if (allocateDefaultPty) {
logger.debug("Allocating default PTY");
session.allocateDefaultPTY();
}
return createProcess(session, cmd);
} catch (SSHException e) {
throw new RuntimeIOException("Cannot execute remote command \"" + cmd.toCommandLine(getHostOperatingSystem(), true) + "\" on " + this, e);
}
}

protected CmdLine processCommandLine(final CmdLine commandLine) {
if (startsWithPseudoCommand(commandLine, NOCD_PSEUDO_COMMAND)) {
logger.trace("Not prefixing command line with cd statement because the " + NOCD_PSEUDO_COMMAND
Expand Down