-
Notifications
You must be signed in to change notification settings - Fork 1
/
honeydctl.c
373 lines (332 loc) · 9.08 KB
/
honeydctl.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
* Copyright (c) 2000, 2004 Niels Provos <provos@citi.umich.edu>
* All rights reserved.
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* Copyright (c) 1997 Brian Somers <brian@Awfulhak.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/un.h>
#include <netdb.h>
#include "config.h"
#include <sys/time.h>
#include <err.h>
#include <errno.h>
#ifdef HAVE_LIBEDIT
#include <histedit.h>
#endif
#include <setjmp.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#ifdef HAVE_LIBREADLINE
#include <readline/readline.h>
#include <readline/history.h>
#endif
#if !defined(HAVE_LIBEDIT) && !defined(HAVE_LIBREADLINE)
#error "Need either libedit or libreadline"
#endif
#define LINELEN 2048
char Buffer[LINELEN];
#define CTLNAME "honeydctl"
#define HONEYD_SOCK "/var/run/honeyd.sock"
int
usage()
{
fprintf(stderr,
"usage: %s [-v] [-t n] [LocalSock]\n"
"\t -v tells %s to output all conversation\n"
"\t -t n specifies a timeout of n seconds when connecting\n",
CTLNAME, CTLNAME);
exit(1);
}
int TimedOut = 0;
void
Timeout(int Sig)
{
TimedOut = 1;
}
#define REC_SHOW (2)
#define REC_VERBOSE (4)
char *passwd;
char *prompt;
#ifdef HAVE_LIBEDIT
char *
getprompt(EditLine *e)
{
if (prompt == NULL)
prompt = "";
return prompt;
}
#endif
int
receive(int fd, int display)
{
int Result;
int len;
char *last;
prompt = Buffer;
len = 0;
while (Result = read(fd, Buffer+len, sizeof(Buffer)-len-1), Result != -1) {
if (Result == 0 && errno != EINTR) {
Result = -1;
break;
}
len += Result;
Buffer[len] = '\0';
if (len > 2 && !strcmp(Buffer+len-2, "> ")) {
prompt = strrchr(Buffer, '\n');
if (display & (REC_SHOW|REC_VERBOSE)) {
if (display & REC_VERBOSE)
last = Buffer+len-1;
else
last = prompt;
if (last) {
last++;
write(1, Buffer, last-Buffer);
}
}
prompt = prompt == NULL ? Buffer : prompt+1;
for (last = Buffer+len-2; last > Buffer && *last != ' '; last--)
;
if (last > Buffer+3 && !strncmp(last-3, " on", 3)) {
/* a password is required ! */
Result = 1;
} else
Result = 0;
break;
}
if (len == sizeof (Buffer) - 1) {
int flush;
if ((last = strrchr(Buffer, '\n')) == NULL)
/* Yeuch - this is one mother of a line ! */
flush = sizeof (Buffer) / 2;
else
flush = last - Buffer + 1;
write(1, Buffer, flush);
strlcpy(Buffer, Buffer + flush, sizeof(Buffer));
len -= flush;
}
}
return Result;
}
int data = -1;
jmp_buf pppdead;
void
check_fd(int sig)
{
if (data != -1) {
struct timeval t;
fd_set f;
static char buf[LINELEN];
int len;
FD_ZERO(&f);
FD_SET(data, &f);
t.tv_sec = t.tv_usec = 0;
if (select(data+1, &f, NULL, NULL, &t) > 0) {
len = read(data, buf, sizeof (buf));
if (len > 0)
write(fileno(stdout), buf, len);
else
longjmp(pppdead, -1);
}
}
}
const char *
#ifdef HAVE_LIBEDIT
smartgets(EditLine *e, int *count, int fd)
#else
smartgets(int *count, int fd)
#endif
{
const char *result;
data = fd;
signal(SIGALRM, check_fd);
ualarm(500000, 500000);
result = setjmp(pppdead) ? NULL :
#ifdef HAVE_LIBEDIT
el_gets(e, count);
#else
readline(prompt);
if (result != NULL)
*count = strlen(result);
#endif
ualarm(0,0);
signal(SIGALRM, SIG_DFL);
data = -1;
return result;
}
int
main(int argc, char **argv)
{
struct sockaddr *sock;
struct sockaddr_un ifsun;
int fd, len, verbose, save_errno;
unsigned TimeoutVal;
struct sigaction act, oact;
char *sockname = HONEYD_SOCK;
#ifdef HAVE_LIBEDIT
EditLine *edit;
History *hist;
HistEvent hev = { 0, "" };
#endif
const char *l;
int ch;
verbose = 0;
TimeoutVal = 2;
while ((ch = getopt(argc, argv, "t:v")) != -1) {
switch (ch) {
case 't':
TimeoutVal = (unsigned)atoi(optarg);
break;
case 'v':
verbose = REC_VERBOSE;
break;
default:
usage();
}
}
argc -= optind;
argv += optind;
if (argc > 0)
sockname = argv[0];
sock = (struct sockaddr *)&ifsun;
memset(&ifsun, '\0', sizeof (ifsun));
#ifdef HAVE_SUN_LEN
ifsun.sun_len = strlen(sockname);
if (ifsun.sun_len > sizeof (ifsun.sun_path) - 1)
errx(1, "%s: path too long", sockname);
#endif /* HAVE_SUN_LEN */
ifsun.sun_family = AF_UNIX;
strlcpy(ifsun.sun_path, sockname, sizeof(ifsun.sun_path));
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
errx(2, "cannot create local domain socket");
TimedOut = 0;
if (TimeoutVal) {
act.sa_handler = Timeout;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGALRM, &act, &oact);
alarm(TimeoutVal);
}
if (connect(fd, sock, sizeof (ifsun)) == -1) {
if (TimeoutVal) {
save_errno = errno;
alarm(0);
sigaction(SIGALRM, &oact, 0);
errno = save_errno;
}
if (TimedOut)
warnx("timeout: cannot connect to socket %s",
sockname);
else
warn("cannot connect to socket %s", sockname);
close(fd);
return 3;
}
if (TimeoutVal) {
alarm(0);
sigaction(SIGALRM, &oact, 0);
}
/* Get Prompt ? */
receive(fd, REC_SHOW);
#ifdef HAVE_LIBEDIT
hist = history_init();
# ifdef H_SETSIZE
history(hist, &hev, H_SETSIZE, 20);
edit = el_init("honeydctl", stdin, stdout, stderr);
# else /* !H_SETSIZE */
history(hist, H_EVENT, 20);
edit = el_init("honeydctl", stdin, stdout);
# endif
el_source(edit, NULL);
el_set(edit, EL_PROMPT, getprompt);
el_set(edit, EL_EDITOR, "emacs");
el_set(edit, EL_SIGNAL, 1);
el_set(edit, EL_HIST, history, (const char *)hist);
#endif
#ifdef HAVE_LIBEDIT
while ((l = smartgets(edit, &len, fd))) {
if (len > 1)
# ifdef H_SETSIZE
history(hist, &hev, H_ENTER, l);
# else
history(hist, H_ENTER, l);
# endif /* H_SETSIZE */
#else /* !HAVE_LIBEDIT */
while ((l = smartgets(&len, fd))) {
char line[128];
if (len > 0) {
add_history((char *)l);
snprintf(line, sizeof(line), "%s\n", l);
l = line;
len++;
}
#endif
write(fd, l, len);
if (receive(fd, REC_SHOW) != 0)
break;
}
fprintf(stderr, "Connection closed\n");
#ifdef HAVE_LIBEDIT
el_end(edit);
history_end(hist);
#endif
close(fd);
return 0;
}