2 * This file is part of the OpenPTS project.
4 * The Initial Developer of the Original Code is International
5 * Business Machines Corporation. Portions created by IBM
6 * Corporation are Copyright (C) 2011 International Business
7 * Machines Corporation. All Rights Reserved.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the Common Public License as published by
11 * IBM Corporation; either version 1 of the License, or (at your option)
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * Common Public License for more details.
19 * You should have received a copy of the Common Public License
20 * along with this program; if not, a copy can be viewed at
21 * http://www.opensource.org/licenses/cpl1.0.php.
26 * \brief SSH conenction
27 * @author Olivier Valentin <olivier.valentin@us.ibm.com>
28 * @author Alexandre Ratchov <alexandre.ratchov@bull.net>
30 * cleanup 2011-12-31 SM
40 #include <sys/socket.h>
44 /* chanegd by unit test */
45 char *ptsc_command = "/usr/sbin/ptsc -m";
50 * opens a communication channel (a socket) to a target using ssh.
52 * @param host host name of the target. Is used as the SSH host name parameter.
53 * @param ssh_username If not NULL, specifies the SSH user name to login as
54 * (defaults to the current user).
55 * @param ssh_port If not 0, specifiesd the port of the remote SSH daemon.
56 * @param key_file If not NULL, specifies the key to use.
57 * @param socket Filed with the result socket. Use it for later communication.
58 * @result the PID of the child SSH process or -1 in case of an error.
60 pid_t ssh_connect(char *host, char *ssh_username, char *ssh_port, char *key_file, int *socket) {
62 int socket_pair[2]; // socket[1] is the SSH side
72 if (socketpair(AF_UNIX, SOCK_STREAM, 0, socket_pair) == -1) {
73 ERROR("socketpair() fail");
77 if ((pid = fork()) == -1) {
86 char identity_string[PATH_MAX + /* "IdentityFile " */ 13];
88 // these belong to father
89 close(socket_pair[0]);
93 // replace stdin and stdout with the socket end
94 dup2(socket_pair[1], 0);
95 dup2(socket_pair[1], 1);
96 close(socket_pair[1]); // no longer needed
98 arguments[arg_idx++] = "ssh";
99 arguments[arg_idx++] = "-2";
100 if (ssh_username != NULL) {
101 arguments[arg_idx++] = "-l";
102 arguments[arg_idx++] = ssh_username;
104 if (ssh_port != NULL) {
105 arguments[arg_idx++] = "-p";
106 arguments[arg_idx++] = ssh_port;
108 /* // should be specified in the ssh_conf file
109 arguments[arg_idx++] = "-o";
110 arguments[arg_idx++] = "BatchMode yes";
112 if (key_file != NULL) {
113 snprintf(identity_string, PATH_MAX + 13, "IdentityFile %s", key_file);
114 arguments[arg_idx++] = "-o";
115 arguments[arg_idx++] = identity_string;
117 arguments[arg_idx++] = host;
118 arguments[arg_idx++] = ptsc_command;
120 /* Sync verbose level between verifier and collector? */
123 for ( verboseLevel = 0; (verboseLevel < getVerbosity()) && (arg_idx < 15); verboseLevel++ ) {
124 arguments[arg_idx++] = "-v";
128 arguments[arg_idx++] = NULL;
130 DEBUG("ptsc_command %s\n", ptsc_command);
132 execvp("ssh", arguments);
133 ERROR("execvp(ssh)");
137 close(socket_pair[1]);
138 *socket = socket_pair[0];
140 fcntl(*socket, F_SETFD, FD_CLOEXEC);
146 close(socket_pair[0]);
147 close(socket_pair[1]);