-
Notifications
You must be signed in to change notification settings - Fork 3
/
amtider.c
222 lines (203 loc) · 5.28 KB
/
amtider.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#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 "ider"
#define BUFSIZE 512
static void usage(FILE *fp)
{
fprintf(fp,
"\n"
"This is " APPNAME ", release " VERSION ", I'll redirect\n"
"iso image of CD and img image of floppy to your Intel AMT boxes.\n"
"\n"
"usage: " APPNAME " [options] host [port]\n"
"options:\n"
" -h print this text\n"
" -q quiet\n"
" -u user username (default: admin)\n"
" -p pass password (default: $AMT_PASSWORD)\n"
" -c path path to *.iso image of redirected CD\n"
" -f path path to *.img image of redirected FD\n"
"\n"
"By default port 16994 is used.\n"
"If no password is given " APPNAME " will ask for one.\n"
"-c and -f must both be used. Cannot redirect only one of CD/FD."
"\n"
"-- \n"
"Based on amtterm by Gerd Hoffmann.\n"
"(c) 2014 Václav Fanfule <[email protected]>\n"
"\n");
}
static int recv_tty(void *cb_data, unsigned char *buf, int len)
{
// struct redir *r = cb_data;
return write(STDOUT_FILENO, buf, len);
}
static void state_tty(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_SOL:
if (r->verbose)
fprintf(stderr,
"serial-over-lan 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)
{
//unsigned char buf[BUFSIZE+1];
struct timeval tv;
int i;
fd_set set;
printf("%i\n",r->state);
//int i=0;
ider_state.state24=0;
ider_state.state28=0;
ider_state.state0000=0;
ider_state.state10a0=0;
for(i=0;;i++) {
if (r->state == REDIR_CLOSED ||
r->state == REDIR_ERROR){
printf("%i\n",r->state);
//break;
}
FD_ZERO(&set);
//if (r->state == REDIR_RUN_IDER)
// FD_SET(STDIN_FILENO,&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)){
printf("why??\n");
return -1;
}
}
}
return 0;
}
int check_images(struct redir *r){
fileCD = fopen(r->cd, "rb");
fileFD = fopen(r->fd, "rb");
if (fileCD==0||fileFD==0){
return -1;
}
fseek ( fileCD , 0 , SEEK_END );
fseek ( fileFD , 0 , SEEK_END );
if( ftell(fileCD)<IDER_MAX_DATA_SIZE || ftell(fileFD)<IDER_MAX_DATA_SIZE){
return -1;
}
return 0;
}
//***********************************************************************//
int main(int argc, char *argv[])
{
struct redir r;
char *h;
//int c;
memset(&r, 0, sizeof(r));
r.verbose = 1;
r.trace= 0;
memcpy(r.type, "IDER", 4);
strcpy(r.user, "admin");
r.cb_data = &r;
r.cb_recv = recv_tty;
r.cb_state = state_tty;
if (NULL != (h = getenv("AMT_PASSWORD")))
snprintf(r.pass, sizeof(r.pass), "%s", h);
int c;
for (;;) {
if (-1 == (c = getopt(argc, argv, "hvqc:f:u:p:")))
break;
switch (c) {
case 'v':
r.verbose = 1;
break;
case 'q':
r.verbose = 0;
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 'c':
snprintf(r.cd, sizeof(r.cd), "%s", optarg);
break;
case 'f':
snprintf(r.fd, sizeof(r.fd), "%s", optarg);
break;
case 'h':
usage(stdout);
exit(0);
default:
usage(stderr);
exit(1);
}
}
//snprintf(r.host, sizeof(r.host), "%s", "10.0.0.100");
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);
}
//tty_save();
if (0 == strlen(r.pass)) {
//tty_noecho();
fprintf(stderr, "AMT password for host %s: ", r.host);
if(!fgets(r.pass, sizeof(r.pass), stdin)) exit(1);
fprintf(stderr, "\n");
if (NULL != (h = strchr(r.pass, '\r')))
*h = 0;
if (NULL != (h = strchr(r.pass, '\n')))
*h = 0;
}
snprintf(r.err, sizeof(r.err), "%s", "stdout");
//r.err="stdout";
if (-1 == redir_connect(&r)) {
//tty_restore();
exit(1);
}
//tty_raw();
if (-1 == check_images(&r)) {
fprintf(stderr,"Cannot open CD or FD image or file too small");
exit(1);
}
redir_start(&r);
redir_loop(&r);
//tty_restore();
exit(0);
//tty_restore();
exit(0);
}