Blame opensm/osm_console_io.c

Packit 13e616
/*
Packit 13e616
 * Copyright (c) 2005-2009 Voltaire, Inc. All rights reserved.
Packit 13e616
 * Copyright (c) 2008 HNR Consulting. All rights reserved.
Packit 13e616
 * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
Packit 13e616
 *
Packit 13e616
 * This software is available to you under a choice of one of two
Packit 13e616
 * licenses.  You may choose to be licensed under the terms of the GNU
Packit 13e616
 * General Public License (GPL) Version 2, available from the file
Packit 13e616
 * COPYING in the main directory of this source tree, or the
Packit 13e616
 * OpenIB.org BSD license below:
Packit 13e616
 *
Packit 13e616
 *     Redistribution and use in source and binary forms, with or
Packit 13e616
 *     without modification, are permitted provided that the following
Packit 13e616
 *     conditions are met:
Packit 13e616
 *
Packit 13e616
 *      - Redistributions of source code must retain the above
Packit 13e616
 *        copyright notice, this list of conditions and the following
Packit 13e616
 *        disclaimer.
Packit 13e616
 *
Packit 13e616
 *      - Redistributions in binary form must reproduce the above
Packit 13e616
 *        copyright notice, this list of conditions and the following
Packit 13e616
 *        disclaimer in the documentation and/or other materials
Packit 13e616
 *        provided with the distribution.
Packit 13e616
 *
Packit 13e616
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Packit 13e616
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Packit 13e616
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Packit 13e616
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
Packit 13e616
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
Packit 13e616
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
Packit 13e616
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
Packit 13e616
 * SOFTWARE.
Packit 13e616
 *
Packit 13e616
 */
Packit 13e616
Packit 13e616
/*
Packit 13e616
 * Abstract:
Packit 13e616
 *    Provide a framework for the Console which decouples the connection
Packit 13e616
 *    or I/O from the functionality, or commands.
Packit 13e616
 *
Packit 13e616
 *    Extensible - allows a variety of connection methods independent of
Packit 13e616
 *    the console commands.
Packit 13e616
 */
Packit 13e616
Packit 13e616
#if HAVE_CONFIG_H
Packit 13e616
#  include <config.h>
Packit 13e616
#endif				/* HAVE_CONFIG_H */
Packit 13e616
Packit 13e616
#ifdef ENABLE_OSM_CONSOLE_LOOPBACK
Packit 13e616
#include <tcpd.h>
Packit 13e616
#include <arpa/inet.h>
Packit 13e616
#include <netinet/in.h>
Packit 13e616
#include <sys/socket.h>
Packit 13e616
#endif
Packit 13e616
#include <unistd.h>
Packit 13e616
#include <errno.h>
Packit 13e616
#include <signal.h>
Packit 13e616
#include <opensm/osm_file_ids.h>
Packit 13e616
#define FILE_ID OSM_FILE_CONSOLE_IO_C
Packit 13e616
#include <opensm/osm_console_io.h>
Packit 13e616
#include <stdlib.h>
Packit 13e616
Packit 13e616
static int is_local(char *str)
Packit 13e616
{
Packit 13e616
	/* convenience - checks if just stdin/stdout */
Packit 13e616
	if (str)
Packit 13e616
		return (strcmp(str, OSM_LOCAL_CONSOLE) == 0);
Packit 13e616
	return 0;
Packit 13e616
}
Packit 13e616
Packit 13e616
#ifdef ENABLE_OSM_CONSOLE_LOOPBACK
Packit 13e616
static int is_loopback(char *str)
Packit 13e616
{
Packit 13e616
	/* convenience - checks if socket based connection */
Packit 13e616
	if (str)
Packit 13e616
		return (strcmp(str, OSM_LOOPBACK_CONSOLE) == 0);
Packit 13e616
	return 0;
Packit 13e616
}
Packit 13e616
#else
Packit 13e616
#define is_loopback is_local
Packit 13e616
#endif
Packit 13e616
Packit 13e616
#ifdef ENABLE_OSM_CONSOLE_SOCKET
Packit 13e616
static int is_remote(char *str)
Packit 13e616
{
Packit 13e616
	/* convenience - checks if socket based connection */
Packit 13e616
	if (str)
Packit 13e616
		return strcmp(str, OSM_REMOTE_CONSOLE) == 0 || is_loopback(str);
Packit 13e616
	return 0;
Packit 13e616
}
Packit 13e616
#else
Packit 13e616
#define is_remote is_loopback
Packit 13e616
#endif
Packit 13e616
Packit 13e616
int is_console_enabled(osm_subn_opt_t * p_opt)
Packit 13e616
{
Packit 13e616
	/* checks for a variety of types of consoles - default is off or 0 */
Packit 13e616
	if (p_opt)
Packit 13e616
		return is_local(p_opt->console) || is_loopback(p_opt->console)
Packit 13e616
			|| is_remote(p_opt->console);
Packit 13e616
	return 0;
Packit 13e616
}
Packit 13e616
Packit 13e616
Packit 13e616
#ifdef ENABLE_OSM_CONSOLE_LOOPBACK
Packit 13e616
int cio_close(osm_console_t * p_oct, osm_log_t * p_log)
Packit 13e616
{
Packit 13e616
	int rtnval = -1;
Packit 13e616
	if (p_oct && p_oct->in_fd > 0) {
Packit 13e616
		OSM_LOG(p_log, OSM_LOG_VERBOSE,
Packit 13e616
			"Console connection closed: %s (%s)\n",
Packit 13e616
			p_oct->client_hn, p_oct->client_ip);
Packit 13e616
		rtnval = fclose(p_oct->in);
Packit 13e616
		p_oct->in_fd = -1;
Packit 13e616
		p_oct->out_fd = -1;
Packit 13e616
		p_oct->in = NULL;
Packit 13e616
		p_oct->out = NULL;
Packit 13e616
	}
Packit 13e616
	return rtnval;
Packit 13e616
}
Packit 13e616
Packit 13e616
int cio_open(osm_console_t * p_oct, int new_fd, osm_log_t * p_log)
Packit 13e616
{
Packit 13e616
	/* returns zero if opened fine, -1 otherwise */
Packit 13e616
	char *p_line;
Packit 13e616
	size_t len;
Packit 13e616
	ssize_t n;
Packit 13e616
Packit 13e616
	if (p_oct->in_fd >= 0) {
Packit 13e616
		FILE *file = fdopen(new_fd, "w+");
Packit 13e616
Packit 13e616
		fprintf(file, "OpenSM Console connection already in use\n"
Packit 13e616
			"   kill other session (y/n)? ");
Packit 13e616
		fflush(file);
Packit 13e616
		p_line = NULL;
Packit 13e616
		n = getline(&p_line, &len, file);
Packit 13e616
		if (n > 0 && (p_line[0] == 'y' || p_line[0] == 'Y'))
Packit 13e616
			cio_close(p_oct, p_log);
Packit 13e616
		else {
Packit 13e616
			OSM_LOG(p_log, OSM_LOG_INFO,
Packit 13e616
				"Console connection aborted: %s (%s) - "
Packit 13e616
				"already in use\n",
Packit 13e616
				p_oct->client_hn, p_oct->client_ip);
Packit 13e616
			fclose(file);
Packit 13e616
			free(p_line);
Packit 13e616
			return -1;
Packit 13e616
		}
Packit 13e616
		free(p_line);
Packit 13e616
	}
Packit 13e616
	p_oct->in_fd = new_fd;
Packit 13e616
	p_oct->out_fd = p_oct->in_fd;
Packit 13e616
	p_oct->in = fdopen(p_oct->in_fd, "w+");
Packit 13e616
	p_oct->out = p_oct->in;
Packit 13e616
	osm_console_prompt(p_oct->out);
Packit 13e616
	OSM_LOG(p_log, OSM_LOG_VERBOSE, "Console connection accepted: %s (%s)\n",
Packit 13e616
		p_oct->client_hn, p_oct->client_ip);
Packit 13e616
Packit 13e616
	return (p_oct->in == NULL) ? -1 : 0;
Packit 13e616
}
Packit 13e616
Packit 13e616
/**********************************************************************
Packit 13e616
 * Do authentication & authorization check
Packit 13e616
 **********************************************************************/
Packit 13e616
int is_authorized(osm_console_t * p_oct)
Packit 13e616
{
Packit 13e616
	/* allowed to use the console? */
Packit 13e616
	p_oct->authorized = !is_remote(p_oct->client_type) ||
Packit 13e616
	    hosts_ctl((char *)OSM_DAEMON_NAME, p_oct->client_hn, p_oct->client_ip,
Packit 13e616
		      (char *)STRING_UNKNOWN);
Packit 13e616
	return p_oct->authorized;
Packit 13e616
}
Packit 13e616
#endif
Packit 13e616
Packit 13e616
void osm_console_prompt(FILE * out)
Packit 13e616
{
Packit 13e616
	if (out) {
Packit 13e616
		fprintf(out, "OpenSM %s", OSM_COMMAND_PROMPT);
Packit 13e616
		fflush(out);
Packit 13e616
	}
Packit 13e616
}
Packit 13e616
Packit 13e616
int osm_console_init(osm_subn_opt_t * opt, osm_console_t * p_oct, osm_log_t * p_log)
Packit 13e616
{
Packit 13e616
	p_oct->socket = -1;
Packit 13e616
	strncpy(p_oct->client_type, opt->console, sizeof(p_oct->client_type) - 1);
Packit 13e616
	p_oct->client_type[sizeof(p_oct->client_type) - 1] = '\0';
Packit 13e616
Packit 13e616
	/* set up the file descriptors for the console */
Packit 13e616
	if (strcmp(opt->console, OSM_LOCAL_CONSOLE) == 0) {
Packit 13e616
		p_oct->in = stdin;
Packit 13e616
		p_oct->out = stdout;
Packit 13e616
		p_oct->in_fd = fileno(stdin);
Packit 13e616
		p_oct->out_fd = fileno(stdout);
Packit 13e616
Packit 13e616
		osm_console_prompt(p_oct->out);
Packit 13e616
#ifdef ENABLE_OSM_CONSOLE_LOOPBACK
Packit 13e616
	} else if (strcmp(opt->console, OSM_LOOPBACK_CONSOLE) == 0
Packit 13e616
#ifdef ENABLE_OSM_CONSOLE_SOCKET
Packit 13e616
		   || strcmp(opt->console, OSM_REMOTE_CONSOLE) == 0
Packit 13e616
#endif
Packit 13e616
		   ) {
Packit 13e616
		struct sockaddr_in sin;
Packit 13e616
		int optval = 1;
Packit 13e616
Packit 13e616
		if ((p_oct->socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
Packit 13e616
			OSM_LOG(p_log, OSM_LOG_ERROR,
Packit 13e616
				"ERR 4B01: Failed to open console socket: %s\n",
Packit 13e616
				strerror(errno));
Packit 13e616
			return -1;
Packit 13e616
		}
Packit 13e616
Packit 13e616
		if (setsockopt(p_oct->socket, SOL_SOCKET, SO_REUSEADDR,
Packit 13e616
			       &optval, sizeof(optval))) {
Packit 13e616
			OSM_LOG(p_log, OSM_LOG_ERROR,
Packit 13e616
		                "ERR 4B06: Failed to set socket option: %s\n",
Packit 13e616
		                strerror(errno));
Packit 13e616
		        return -1;
Packit 13e616
		}
Packit 13e616
Packit 13e616
		sin.sin_family = AF_INET;
Packit 13e616
		sin.sin_port = htons(opt->console_port);
Packit 13e616
#ifdef ENABLE_OSM_CONSOLE_SOCKET
Packit 13e616
		if (strcmp(opt->console, OSM_REMOTE_CONSOLE) == 0)
Packit 13e616
			sin.sin_addr.s_addr = htonl(INADDR_ANY);
Packit 13e616
		else
Packit 13e616
#endif
Packit 13e616
			sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
Packit 13e616
		if (bind(p_oct->socket, &sin, sizeof(sin)) < 0) {
Packit 13e616
			OSM_LOG(p_log, OSM_LOG_ERROR,
Packit 13e616
				"ERR 4B02: Failed to bind console socket: %s\n",
Packit 13e616
				strerror(errno));
Packit 13e616
			return -1;
Packit 13e616
		}
Packit 13e616
		if (listen(p_oct->socket, 1) < 0) {
Packit 13e616
			OSM_LOG(p_log, OSM_LOG_ERROR,
Packit 13e616
				"ERR 4B03: Failed to listen on console socket: %s\n",
Packit 13e616
				strerror(errno));
Packit 13e616
			return -1;
Packit 13e616
		}
Packit 13e616
Packit 13e616
		signal(SIGPIPE, SIG_IGN);	/* protect ourselves from closed pipes */
Packit 13e616
		p_oct->in = NULL;
Packit 13e616
		p_oct->out = NULL;
Packit 13e616
		p_oct->in_fd = -1;
Packit 13e616
		p_oct->out_fd = -1;
Packit 13e616
		OSM_LOG(p_log, OSM_LOG_INFO,
Packit 13e616
			"Console listening on port %d\n", opt->console_port);
Packit 13e616
#endif
Packit 13e616
	}
Packit 13e616
Packit 13e616
	return 0;
Packit 13e616
}
Packit 13e616
Packit 13e616
/* clean up and release resources */
Packit 13e616
void osm_console_exit(osm_console_t * p_oct, osm_log_t * p_log)
Packit 13e616
{
Packit 13e616
#ifdef ENABLE_OSM_CONSOLE_LOOPBACK
Packit 13e616
	cio_close(p_oct, p_log);
Packit 13e616
	if (p_oct->socket > 0) {
Packit 13e616
		OSM_LOG(p_log, OSM_LOG_INFO, "Closing console socket\n");
Packit 13e616
		close(p_oct->socket);
Packit 13e616
		p_oct->socket = -1;
Packit 13e616
	}
Packit 13e616
#endif
Packit 13e616
}