diff options
author | Hannes Reinecke <hare@suse.de> | 2022-04-11 11:02:49 +0200 |
---|---|---|
committer | Gerd Hoffmann <kraxel@redhat.com> | 2022-04-22 14:52:30 +0200 |
commit | d9d6f3de9777d8263b593323586da994b3c69912 (patch) | |
tree | 4914eeaa4486c0496595023d5931820b1b2634e9 | |
parent | 0903fb38ea0b8fb42bb039be363dd80eb61a8957 (diff) | |
download | amtterm-d9d6f3de9777d8263b593323586da994b3c69912.tar.gz |
amtider: IDE-redirection client
Preliminary stub only.
Signed-off-by: Hannes Reinecke <hare@suse.de>
-rw-r--r-- | GNUmakefile | 3 | ||||
-rw-r--r-- | RedirectionConstants.h | 1 | ||||
-rw-r--r-- | amtider.c | 242 | ||||
-rw-r--r-- | redir.c | 17 | ||||
-rw-r--r-- | redir.h | 7 |
5 files changed, 269 insertions, 1 deletions
diff --git a/GNUmakefile b/GNUmakefile index fb5684b..b348505 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -20,7 +20,7 @@ CFLAGS += -Wall -Wno-pointer-sign CFLAGS += -DVERSION='"$(VERSION)"' CFLAGS += $(SSL_DEFS) -TARGETS := amtterm +TARGETS := amtterm amtider DESKTOP := $(wildcard *.desktop) all: build @@ -74,6 +74,7 @@ distclean: clean ################################################################# amtterm: amtterm.o redir.o tcp.o auth.o ssl.o +amtider: amtider.o redir.o tcp.o auth.o ssl.o gamt: gamt.o redir.o tcp.o parseconfig.o auth.o ssl.o ################################################################# diff --git a/RedirectionConstants.h b/RedirectionConstants.h index 0ad7fc0..b05060f 100644 --- a/RedirectionConstants.h +++ b/RedirectionConstants.h @@ -59,6 +59,7 @@ #define IDER_RESET_OCCURED_RESPONSE 0x47 #define IDER_DISABLE_ENABLE_FEATURES 0x48 #define IDER_DISABLE_ENABLE_FEATURES_REPLY 0x49 +#define IDER_ERROR_OCCURED 0x4A #define IDER_HEARTBEAT 0x4B #define IDER_COMMAND_WRITTEN 0x50 #define IDER_COMMAND_END_RESPONSE 0x51 diff --git a/amtider.c b/amtider.c new file mode 100644 index 0000000..6393804 --- /dev/null +++ b/amtider.c @@ -0,0 +1,242 @@ +/* + * amtider -- Intel AMT IDE-redirection client, console version. + * + * Copyright (C) 2022 Hannes Reinecke <hare@suse.de> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <string.h> +#include <errno.h> +#include <fcntl.h> +#include <termios.h> +#include <signal.h> +#include <sys/ioctl.h> + +#include "redir.h" + +#define APPNAME "amtider" +#define BUFSIZE 512 + +/* ------------------------------------------------------------------ */ + +static int recv_ider(void *cb_data, unsigned char *buf, int len) +{ + struct redir *r = cb_data; + + return write(STDOUT_FILENO, buf, len); +} + +static void state_ider(void *cb_data, enum redir_state old, + enum redir_state new) +{ + struct redir *r = cb_data; + + if (r->verbose) + fprintf(stderr, APPNAME ": %s -> %s (%s)\n", + redir_state_name(old), redir_state_name(new), + redir_state_desc(new)); + switch (new) { + case REDIR_RUN_IDER: + if (r->verbose) + fprintf(stderr, + "IDE redirection ok\n" + "connected now, use ^] to escape\n"); + break; + case REDIR_ERROR: + fprintf(stderr, APPNAME ": ERROR: %s\n", r->err); + break; + default: + break; + } +} + +static int redir_loop(struct redir *r) +{ + struct timeval tv; + fd_set set; + + for(;;) { + if (r->state == REDIR_CLOSED || + r->state == REDIR_ERROR) + break; + + FD_ZERO(&set); + FD_SET(r->sock,&set); + tv.tv_sec = HEARTBEAT_INTERVAL * 4 / 1000; + tv.tv_usec = 0; + switch (select(r->sock+1,&set,NULL,NULL,&tv)) { + case -1: + perror("select"); + return -1; + case 0: + fprintf(stderr,"select: timeout\n"); + return -1; + } + + if (FD_ISSET(r->sock,&set)) { + if (-1 == redir_data(r)) + return -1; + } + } + return 0; +} + +/* ------------------------------------------------------------------ */ + +struct termios saved_attributes; +int saved_fl; + +static void tty_save(void) +{ + fcntl(STDIN_FILENO,F_GETFL,&saved_fl); + tcgetattr (STDIN_FILENO, &saved_attributes); +} + +static void tty_noecho(void) +{ + struct termios tattr; + + memcpy(&tattr,&saved_attributes,sizeof(struct termios)); + tattr.c_lflag &= ~(ECHO); + tcsetattr (STDIN_FILENO, TCSAFLUSH, &tattr); +} + +static void tty_restore(void) +{ + fcntl(STDIN_FILENO,F_SETFL,saved_fl); + tcsetattr (STDIN_FILENO, TCSANOW, &saved_attributes); +} + +/* ------------------------------------------------------------------ */ + +static void usage(FILE *fp) +{ + fprintf(fp, + "\n" + "This is " APPNAME ", release " VERSION ", it'll establish\n" + "ide-redirection (ider) connections to your Intel AMT boxes.\n" + "\n" + "usage: " APPNAME " [options] host [port]\n" + "options:\n" + " -h print this text\n" + " -v verbose (default)\n" + " -q quiet\n" + " -f file file to use as device data\n" + " -L use legacy authentication\n" +#if defined(USE_OPENSSL) || defined(USE_GNUTLS) + " -C cacert enable SSL and use PEM cacert file\n" +#endif + " -u user username (default: admin)\n" + " -p pass password (default: $AMT_PASSWORD)\n" + "\n" +#if defined(USE_OPENSSL) || defined(USE_GNUTLS) + "By default port 16994 (SSL: 16995) is used.\n" +#else + "By default port 16994 is used.\n" +#endif + "If no password is given " APPNAME " will ask for one.\n"); +} + +int main(int argc, char *argv[]) +{ + struct redir r; + char *h; + int c; + + memset(&r, 0, sizeof(r)); + r.verbose = 1; + memcpy(r.type, "IDER", 4); + strcpy(r.user, "admin"); + + r.cb_data = &r; + r.cb_recv = recv_ider; + r.cb_state = state_ider; + + if (NULL != (h = getenv("AMT_PASSWORD"))) + snprintf(r.pass, sizeof(r.pass), "%s", h); + + for (;;) { + if (-1 == (c = getopt(argc, argv, "hvqu:p:LC:"))) + break; + switch (c) { + case 'v': + r.trace = 1; + break; + case 'q': + r.verbose = 0; + break; + case 'f': + snprintf(r.filename, sizeof(r.filename), "%s", optarg); + break; + case 'u': + snprintf(r.user, sizeof(r.user), "%s", optarg); + break; + case 'p': + snprintf(r.pass, sizeof(r.pass), "%s", optarg); + memset(optarg,'*',strlen(optarg)); /* rm passwd from ps list */ + break; + case 'L': + r.legacy = 1; + break; +#if defined(USE_OPENSSL) || defined(USE_GNUTLS) + case 'C': + r.cacert = optarg; + break; +#endif + + case 'h': + usage(stdout); + exit(0); + default: + usage(stderr); + exit(1); + } + } + + if (optind < argc) + snprintf(r.host, sizeof(r.host), "%s", argv[optind]); + if (optind+1 < argc) + snprintf(r.port, sizeof(r.port), "%s", argv[optind+1]); + if (0 == strlen(r.host)) { + usage(stderr); + exit(1); + } + + if (0 == strlen(r.pass)) { + tty_save(); + tty_noecho(); + fprintf(stderr, "AMT password for host %s: ", r.host); + fgets(r.pass, sizeof(r.pass), stdin); + fprintf(stderr, "\n"); + if (NULL != (h = strchr(r.pass, '\r'))) + *h = 0; + if (NULL != (h = strchr(r.pass, '\n'))) + *h = 0; + tty_restore(); + } + + if (-1 == redir_connect(&r)) { + exit(1); + } + + redir_start(&r); + redir_loop(&r); + + exit(0); +} @@ -314,6 +314,23 @@ int redir_sol_start(struct redir *r) return redir_write(r, request, sizeof(request)); } +int redir_ider_start(struct redir *r) +{ + unsigned char request[START_IDER_REDIRECTION_LENGTH] = { + START_IDER_REDIRECTION, + TRANSMIT_BUFFER_TIMEOUT & 0xff, + TRANSMIT_BUFFER_TIMEOUT >> 8, + HOST_SESSION_RX_TIMEOUT & 0xff, + HOST_SESSION_RX_TIMEOUT >> 8, + HEARTBEAT_INTERVAL & 0xff, + HEARTBEAT_INTERVAL >> 8, + 1, 0, 0, 0 + }; + + redir_state(r, REDIR_INIT_IDER); + return redir_write(r, request, sizeof(request)); +} + int redir_sol_stop(struct redir *r) { unsigned char request[END_SOL_REDIRECTION_LENGTH] = { @@ -30,6 +30,9 @@ struct redir { enum redir_state state; unsigned char err[128]; // state == REDIR_ERROR + /* ide-redirection */ + unsigned char filename[256]; + int sock; unsigned char buf[64]; unsigned int blen; @@ -62,4 +65,8 @@ int redir_sol_start(struct redir *r); int redir_sol_stop(struct redir *r); int redir_sol_send(struct redir *r, unsigned char *buf, int blen); int redir_sol_recv(struct redir *r); +int redir_ider_start(struct redir *r); +int redir_ider_stop(struct redir *r); +int redir_ider_send(struct redir *r, unsigned char *buf, int blen); +int redir_ider_recv(struct redir *r); int redir_data(struct redir *r); |