-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
214 lines (170 loc) · 6.58 KB
/
main.cpp
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
/*
Copyright Jordan Sissel, 2018
This file is part of jordansissel/ipmi.
jordansissel/ipmi is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
jordansissel/ipmi 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with jordansissel/ipmi. If not, see <http://www.gnu.org/licenses/>.
*/
#include "insist.h"
#include "ipmi_mongoose.h"
#include "mongoose.h"
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include "client.h"
#include "ipmi.h"
int mgos(int argc, char **argv) {
if (argc != 3) {
printf("Usage: %s <host> <password>\n", argv[0]);
return 1;
}
const auto hostname = argv[1];
uint8_t password[16] = {};
strncpy((char *)password, argv[2], 16);
auto client = new IPMI::Client(password);
struct mg_mgr mgr;
mg_mgr_init(&mgr, NULL);
struct mg_connect_opts opts = {.user_data = client};
char *address;
asprintf(&address, "udp://%s:623", hostname);
auto conn =
mg_connect_opt(&mgr, address, ipmi_client_connection_handler, opts);
free(address);
client->setConnection(conn);
client->chassisControl(IPMI::ChassisControlCommand::PowerUp);
for (;;) { // Start infinite event loop
// if (client->getState() == IPMI::ClientState::NeedChassisControlResponse)
// { break;
// }
mg_mgr_poll(&mgr, 1000);
}
mg_mgr_free(&mgr);
delete client;
return 0;
}
int posix(int argc, char **argv) {
if (argc != 3) {
printf("Usage: %s <host> <password>\n", argv[0]);
return 1;
}
srandom(time(NULL));
const auto hostname = argv[1];
uint8_t password[16] = {};
strncpy((char *)password, argv[2], 16);
struct addrinfo *addresses, hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = 0;
const int rc = getaddrinfo(hostname, "623", &hints, &addresses);
insist_return(rc == 0, 1, "getaddrinfo() failed: %d: %s", rc,
gai_strerror(rc));
int fd = socket(addresses->ai_family, addresses->ai_socktype,
addresses->ai_protocol);
insist_return(fd >= 0, 1, "socket() failed: %d: %s", errno, strerror(errno));
char name[50];
inet_ntop(addresses->ai_family,
(addresses->ai_family == AF_INET)
? (const void *)&(
((struct sockaddr_in *)addresses->ai_addr)->sin_addr)
: (const void *)&(
((struct sockaddr_in6 *)addresses->ai_addr)->sin6_addr),
name, 50);
printf("%s == %s\n", hostname, name);
struct mbuf buf;
mbuf_init(&buf, 20);
// Send Get Channel Authentication Capabilities
IPMI::getChannelAuthenticationCapabilities(buf);
printf("Sending: \n");
mg_hexdumpf(stdout, buf.buf, buf.len);
sendto(fd, buf.buf, buf.len, 0, addresses->ai_addr, addresses->ai_addrlen);
mbuf_remove(&buf, buf.len);
// Receive Get Channel Authentication Capabilities Response
char recv[1500];
int b = recvfrom(fd, recv, 1500, 0, NULL, NULL);
mbuf_append(&buf, recv, b);
printf("Received: \n");
mg_hexdumpf(stdout, buf.buf, buf.len);
IPMI::RMCP rmcp;
IPMI::IPMB ipmb;
IPMI::Session session;
IPMI::GetChannelAuthenticationCapabilities::Response response;
IPMI::decode(buf, rmcp, ipmb, session, response);
if (response.completion_code != 0) {
printf("ChannelAuthenticationCapabilities request failed.\n");
return 1;
}
if (!response.hasMD5()) {
printf("Remote claims no support for MD5 authcode. Cannot continue.\n");
return 1;
}
// Send Get Session Challenge
IPMI::getSessionChallenge(buf);
printf("Sending: \n");
mg_hexdumpf(stdout, buf.buf, buf.len);
sendto(fd, buf.buf, buf.len, 0, addresses->ai_addr, addresses->ai_addrlen);
mbuf_remove(&buf, buf.len);
// Receive Get Session Challenge Response
b = recvfrom(fd, recv, 1500, 0, NULL, NULL);
printf("Before read len: %zd\n", buf.len);
mbuf_append(&buf, recv, b);
printf("After read len: %zd\n", buf.len);
mg_hexdumpf(stdout, buf.buf, buf.len);
IPMI::GetSessionChallenge::Response challengeResponse;
IPMI::decode(buf, rmcp, ipmb, session, challengeResponse);
// All future commands need an auth_code of:
// md5(password + session id + data + sequence + password)
// XXX: Add password.
uint32_t sequence = (uint32_t)random();
IPMI::activateSession(buf, password, sequence, challengeResponse.session_id,
challengeResponse.challenge);
printf("! Sending: \n");
mg_hexdumpf(stdout, buf.buf, buf.len);
sendto(fd, buf.buf, buf.len, 0, addresses->ai_addr, addresses->ai_addrlen);
mbuf_remove(&buf, buf.len);
// Receive ActivateSession Response
b = recvfrom(fd, recv, 1500, 0, NULL, NULL);
mbuf_append(&buf, recv, b);
printf("Received: \n");
mg_hexdumpf(stdout, buf.buf, buf.len);
IPMI::ActivateSession::Response activateResponse;
IPMI::decode(buf, password, rmcp, ipmb, session, activateResponse);
uint32_t session_id = activateResponse.session;
sequence = activateResponse.sequence;
// Send SetSessionPrivilege
IPMI::setSessionPrivilege(buf, session_id, sequence, password,
IPMI::AuthenticationCapability::Administrator);
sequence++;
printf("!! Sending: \n");
mg_hexdumpf(stdout, buf.buf, buf.len);
sendto(fd, buf.buf, buf.len, 0, addresses->ai_addr, addresses->ai_addrlen);
mbuf_remove(&buf, buf.len);
// Receive SetSessionPrivilege response
b = recvfrom(fd, recv, 1500, 0, NULL, NULL);
mbuf_append(&buf, recv, b);
printf("Received: \n");
mg_hexdumpf(stdout, buf.buf, buf.len);
IPMI::SetSessionPrivilege::Response privilegeResponse;
IPMI::decode(buf, password, rmcp, ipmb, session, privilegeResponse);
// Send Chassis Control
IPMI::chassisControl(buf, challengeResponse.session_id, sequence, password,
IPMI::ChassisControlCommand::PowerUp);
sequence++;
printf("!! Sending: \n");
mg_hexdumpf(stdout, buf.buf, buf.len);
sendto(fd, buf.buf, buf.len, 0, addresses->ai_addr, addresses->ai_addrlen);
// Receive Chassis Control response
b = recvfrom(fd, recv, 1500, 0, NULL, NULL);
mbuf_append(&buf, recv, b);
printf("Received: \n");
mg_hexdumpf(stdout, buf.buf, buf.len);
return 0;
}
int main(int argc, char **argv) { return mgos(argc, argv); }