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-04-26 SM
40 #include <sys/socket.h>
45 /* chanegd by unit test */
46 char *ptsc_command = "/usr/sbin/ptsc -m";
51 * opens a communication channel (a socket) to a target using ssh.
53 * @param host host name of the target. Is used as the SSH host name parameter.
54 * @param ssh_username If not NULL, specifies the SSH user name to login as
55 * (defaults to the current user).
56 * @param ssh_port If not 0, specifiesd the port of the remote SSH daemon.
57 * @param key_file If not NULL, specifies the key to use.
58 * @param socket Filed with the result socket. Use it for later communication.
59 * @result the PID of the child SSH process or -1 in case of an error.
61 pid_t ssh_connect(char *host, char *ssh_username, char *ssh_port, char *key_file, int *socket) {
63 int socket_pair[2]; // socket[1] is the SSH side
66 if (socketpair(AF_UNIX, SOCK_STREAM, 0, socket_pair) == -1) {
67 ERROR("socketpair()");
71 if ((pid = fork()) == -1) {
80 char identity_string[PATH_MAX + /* "IdentityFile " */ 13];
82 // these belong to father
83 close(socket_pair[0]);
87 // replace stdin and stdout with the socket end
88 dup2(socket_pair[1], 0);
89 dup2(socket_pair[1], 1);
90 close(socket_pair[1]); // no longer needed
92 arguments[arg_idx++] = "ssh";
93 arguments[arg_idx++] = "-2";
94 if (ssh_username != NULL) {
95 arguments[arg_idx++] = "-l";
96 arguments[arg_idx++] = ssh_username;
98 if (ssh_port != NULL) {
99 arguments[arg_idx++] = "-p";
100 arguments[arg_idx++] = ssh_port;
102 /* // should be specified in the ssh_conf file
103 arguments[arg_idx++] = "-o";
104 arguments[arg_idx++] = "BatchMode yes";
106 if (key_file != NULL) {
107 snprintf(identity_string, PATH_MAX + 13, "IdentityFile %s", key_file);
108 arguments[arg_idx++] = "-o";
109 arguments[arg_idx++] = identity_string;
111 arguments[arg_idx++] = host;
112 arguments[arg_idx++] = ptsc_command;
113 // arguments[arg_idx++] = "ptsc";
114 // arguments[arg_idx++] = "-m";
116 /* Sync verbose level between verifier and collector? */
119 for ( verboseLevel = 0; (verboseLevel < getVerbosity()) && (arg_idx < 15); verboseLevel++ ) {
120 arguments[arg_idx++] = "-v";
124 arguments[arg_idx++] = NULL;
126 DEBUG("ptsc_command %s\n", ptsc_command);
128 execvp("ssh", arguments);
129 ERROR("execvp(ssh)");
133 close(socket_pair[1]);
134 *socket = socket_pair[0];
136 fcntl(*socket, F_SETFD, FD_CLOEXEC);
142 close(socket_pair[0]);
143 close(socket_pair[1]);