]>
git.zerfleddert.de Git - amt/blob - amtterm.c
2 * amtterm -- Intel AMT serial-over-lan client, console version.
4 * Copyright (C) 2007 Gerd Hoffmann <kraxel@redhat.com
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #include <sys/ioctl.h>
33 #define APPNAME "amtterm"
36 /* ------------------------------------------------------------------ */
38 static int recv_tty(void *cb_data
, unsigned char *buf
, int len
)
40 // struct redir *r = cb_data;
42 return write(0, buf
, len
);
45 static void state_tty(void *cb_data
, enum redir_state old
, enum redir_state
new)
47 struct redir
*r
= cb_data
;
50 fprintf(stderr
, APPNAME
": %s -> %s (%s)\n",
51 redir_state_name(old
), redir_state_name(new),
52 redir_state_desc(new));
57 "serial-over-lan redirection ok\n"
58 "connected now, use ^] to escape\n");
61 fprintf(stderr
, APPNAME
": ERROR: %s\n", r
->err
);
68 static int redir_loop(struct redir
*r
)
70 unsigned char buf
[BUFSIZE
+1];
76 if (r
->state
== REDIR_CLOSED
||
77 r
->state
== REDIR_ERROR
)
81 if (r
->state
== REDIR_RUN_SOL
)
84 tv
.tv_sec
= HEARTBEAT_INTERVAL
* 4 / 1000;
86 switch (select(r
->sock
+1,&set
,NULL
,NULL
,&tv
)) {
91 fprintf(stderr
,"select: timeout\n");
95 if (FD_ISSET(0,&set
)) {
97 rc
= read(0,buf
,BUFSIZE
);
100 perror("read(stdin)");
103 fprintf(stderr
,"EOF from stdin\n");
106 if (buf
[0] == 0x1d) {
108 fprintf(stderr
, "\n" APPNAME
": saw ^], exiting\n");
111 if (-1 == redir_sol_send(r
, buf
, rc
))
117 if (FD_ISSET(r
->sock
,&set
)) {
118 if (-1 == redir_data(r
))
125 /* ------------------------------------------------------------------ */
127 struct termios saved_attributes
;
130 static void tty_save(void)
132 fcntl(0,F_GETFL
,&saved_fl
);
133 tcgetattr (0, &saved_attributes
);
136 static void tty_noecho(void)
138 struct termios tattr
;
140 memcpy(&tattr
,&saved_attributes
,sizeof(struct termios
));
141 tattr
.c_lflag
&= ~(ECHO
);
142 tcsetattr (0, TCSAFLUSH
, &tattr
);
145 static void tty_raw(void)
147 struct termios tattr
;
149 fcntl(0,F_SETFL
,O_NONBLOCK
);
150 memcpy(&tattr
,&saved_attributes
,sizeof(struct termios
));
151 tattr
.c_lflag
&= ~(ISIG
|ICANON
|ECHO
);
152 tattr
.c_cc
[VMIN
] = 1;
153 tattr
.c_cc
[VTIME
] = 0;
154 tcsetattr (0, TCSAFLUSH
, &tattr
);
157 static void tty_restore(void)
159 fcntl(0,F_SETFL
,saved_fl
);
160 tcsetattr (0, TCSANOW
, &saved_attributes
);
163 /* ------------------------------------------------------------------ */
165 static void usage(FILE *fp
)
169 "This is " APPNAME
", release " VERSION
", I'll establish\n"
170 "serial-over-lan (sol) connections to your Intel AMT boxes.\n"
172 "usage: " APPNAME
" [options] host [port]\n"
174 " -h print this text\n"
175 " -v verbose (default)\n"
177 " -u user username (default: admin)\n"
178 " -p pass password (default: $AMT_PASSWORD)\n"
180 "By default port 16994 is used.\n"
181 "If no password is given " APPNAME
" will ask for one.\n"
184 "(c) 2007 Gerd Hoffmann <kraxel@redhat.com>\n"
188 int main(int argc
, char *argv
[])
194 memset(&r
, 0, sizeof(r
));
196 memcpy(r
.type
, "SOL ", 4);
197 strcpy(r
.user
, "admin");
200 r
.cb_recv
= recv_tty
;
201 r
.cb_state
= state_tty
;
203 if (NULL
!= (h
= getenv("AMT_PASSWORD")))
204 snprintf(r
.pass
, sizeof(r
.pass
), "%s", h
);
207 if (-1 == (c
= getopt(argc
, argv
, "hvqu:p:")))
217 snprintf(r
.user
, sizeof(r
.user
), "%s", optarg
);
220 snprintf(r
.pass
, sizeof(r
.pass
), "%s", optarg
);
221 memset(optarg
,'*',strlen(optarg
)); /* rm passwd from ps list */
234 snprintf(r
.host
, sizeof(r
.host
), "%s", argv
[optind
]);
236 snprintf(r
.port
, sizeof(r
.port
), "%s", argv
[optind
+1]);
237 if (0 == strlen(r
.host
)) {
243 if (0 == strlen(r
.pass
)) {
245 fprintf(stderr
, "AMT password for host %s: ", r
.host
);
246 fgets(r
.pass
, sizeof(r
.pass
), stdin
);
247 fprintf(stderr
, "\n");
248 if (NULL
!= (h
= strchr(r
.pass
, '\r')))
250 if (NULL
!= (h
= strchr(r
.pass
, '\n')))
254 if (-1 == redir_connect(&r
)) {