-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsshserver.c
297 lines (264 loc) · 6.53 KB
/
sshserver.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#include <stdio.h>
#include <stdlib.h>
#include "config.h"
#include "sshserver.h"
#include <libssh/libssh.h>
#include <libssh/server.h>
#include <errno.h>
#include <signal.h>
#include <string.h>
#include <assert.h>
#include <openssl/bio.h>
#include <openssl/evp.h>
#include <openssl/sha.h>
#include <openssl/buffer.h>
ssh_session session;
ssh_bind sshbind;
int run_ssh_server(int port){
signal(SIGINT, (void (*)())free_glob);
int timeout = 30;
char * ip = "::";
sshbind = ssh_bind_new();
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDADDR, ip);
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_BINDPORT, &port);
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_HOSTKEY, "ssh-rsa");
char *key_path = getpath("private.pem");
ssh_bind_options_set(sshbind, SSH_BIND_OPTIONS_RSAKEY,key_path);
// bind to the given port
if (ssh_bind_listen(sshbind) < 0) {
printf("Error listening to socket: %s\n", ssh_get_error(sshbind));
free(key_path);
return -1;
}
print_remote_help(port, ip);
print_fingerprint("public.pem");
// wait for incoming connections for forever
while (1) {
session = ssh_new();
if (session == NULL) {
fprintf(stderr, "Failed to allocate session\n");
continue;
}
ssh_options_set(session, SSH_OPTIONS_TIMEOUT, &timeout);
if (ssh_bind_accept(sshbind, session) == SSH_ERROR) {
printf("Error: Connection could not get accepted: '%s'.\n",
ssh_get_error(sshbind));
return -1;
}
switch (fork()) {
case -1:
fprintf(stderr, "Fork returned error: '%d'.\n", -1);
exit(-1);
case 0:
process_client();
ssh_disconnect(session);
ssh_free(session);
exit(0);
default:
break;
}
ssh_disconnect(session);
ssh_free(session);
}
ssh_bind_free(sshbind);
ssh_finalize();
return EXIT_SUCCESS;
}
/*
* Function: print_remote_help
*
* give a copiable line to the user, which he may pass to the remote machines owner
* if the ip address is the wildcard '::' replace its occurence with '<this machines ip>'
*
* port: the port on which the remote host should connect
*
* ip: the ip-address to which the remote host should connect
*/
void print_remote_help(int port, char * ip){
printf("remote command:\n");
if (strcmp(ip, "::")) {
printf("ssh-copy-id -p %d %s\n", port, ip);
} else {
printf("ssh-copy-id -p %d <this machines ip>\n", port);
}
}
/*
* Function: print_fingerprint
*
* print the sha256 fingerprint of a ssh-public-key in OpenSSH format
*
* file: the filename of the public key within the config-directory to print
*/
void print_fingerprint(const char * filename){
unsigned char *hash = NULL;
ssh_key srv_pubkey = NULL;
int rc = 0;
char *pubpath;
size_t hlen;
pubpath = getpath(filename);
rc = ssh_pki_import_pubkey_file(pubpath, &srv_pubkey);
free(pubpath);
if (rc < 0) {
return;
}
rc = ssh_get_publickey_hash(srv_pubkey,
SSH_PUBLICKEY_HASH_SHA256,
&hash,
&hlen);
ssh_key_free(srv_pubkey);
if (rc < 0) {
return;
}
ssh_print_hash(SSH_PUBLICKEY_HASH_SHA256, hash, hlen);
free(hash);
}
/*
* Function: free_glob
*
* cleans up the to globals ssh session and the bind
* is meant to be registered as interrupt handler
*/
void free_glob(void){
ssh_disconnect(session);
ssh_free(session);
ssh_bind_free(sshbind);
ssh_finalize();
exit(EXIT_SUCCESS);
}
int process_client() {
struct connection con;
con.session = session;
/* Perform key exchange. */
if (ssh_handle_key_exchange(con.session)) {
printf("Error exchanging keys: '%s'.\n", ssh_get_error(con.session));
return -1;
}
ssh_message message;
ssh_channel chan=0;
char buffer[2048];
int auth=0;
int exec=0;
int i;
/* proceed to authentication */
auth = authenticate(&con);
if(!auth) {
ssh_disconnect(con.session);
return 1;
}
do {
message = ssh_message_get(con.session);
if(message) {
if(ssh_message_type(message) == SSH_REQUEST_CHANNEL_OPEN
&& ssh_message_subtype(message) == SSH_CHANNEL_SESSION) {
chan = ssh_message_channel_request_open_reply_accept(message);
ssh_message_free(message);
break;
} else {
ssh_message_reply_default(message);
ssh_message_free(message);
}
} else {
break;
}
} while(!chan);
if(!chan) {
printf("Error: Client did not ask for a channel session (%s)\n",
ssh_get_error(con.session));
ssh_finalize();
return 1;
}
do {
message = ssh_message_get(con.session);
if(message != NULL) {
if(ssh_message_type(message) == SSH_REQUEST_CHANNEL) {
if (ssh_message_subtype(message) == SSH_CHANNEL_REQUEST_EXEC) {
exec = 1;
ssh_message_channel_request_reply_success(message);
ssh_message_free(message);
continue;
}
}
ssh_message_reply_default(message);
ssh_message_free(message);
} else {
break;
}
} while(!exec);
if (exec) {
if ((i = ReadExec(chan, buffer, 1024)) > 0) {
char buf[i];
snprintf(buf, sizeof buf, "%s", buffer);
show_content(&con, buf);
ssh_channel_write(chan,
"INFO: Received keys, thanks for using heimdallr!\r\n",
50);
ssh_channel_close(chan);
ssh_disconnect(session);
ssh_finalize();
return 0;
}
}
ssh_disconnect(con.session);
return 0;
}
int authenticate(struct connection *c) {
ssh_message message;
do {
message = ssh_message_get(session);
if(!message)
break;
switch(ssh_message_type(message)) {
case SSH_REQUEST_AUTH:
switch(ssh_message_subtype(message)) {
case SSH_AUTH_METHOD_INTERACTIVE:
ssh_message_auth_reply_success(message,0);
ssh_message_free(message);
return 1;
case SSH_AUTH_METHOD_NONE:
default:
ssh_message_auth_set_methods(message,
SSH_AUTH_METHOD_INTERACTIVE);
ssh_message_reply_default(message);
break;
}
break;
default:
ssh_message_auth_set_methods(message,
SSH_AUTH_METHOD_INTERACTIVE);
ssh_message_reply_default(message);
}
ssh_message_free(message);
} while (ssh_get_status(session) != SSH_CLOSED ||
ssh_get_status(session) != SSH_CLOSED_ERROR);
return 0;
}
int ReadExec(ssh_channel chan, void *vptr, int maxlen) {
int n=0, rc=0;
char c, *buffer;
buffer = vptr;
for ( n = 1; n < maxlen; n++ ) {
if ( (rc = ssh_channel_read(chan, &c, 1, 0)) == 1 ) {
if ( c == '\r' || c == '\n' ) {
break;
}
if(c != '\r' || c != '\n' || c != '\0') {
*buffer++ = c;
}
} else if ( rc == 0 ) {
if ( n == 1 )
return 0;
else
break;
} else {
if ( errno == EINTR )
continue;
return -1;
}
}
*buffer = 0;
return n;
}
int show_content(struct connection *c, char* command) {
printf("%s\n", command);
return 0;
}