CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/external/source/ipwn/cmd_fd.c
Views: 11766
1
/*
2
* Copyright (c) 2004-2005 vlad902 <vlad902 [at] gmail.com>
3
* Copyright (c) 2007 H D Moore <hdm [at] metasploit.com>
4
* This file is part of the Metasploit Framework.
5
* $Revision$
6
*/
7
8
#include <sys/types.h>
9
#include <sys/stat.h>
10
#include <stdlib.h>
11
#include <unistd.h>
12
#include <sys/socket.h>
13
#include <netinet/in.h>
14
#include <arpa/inet.h>
15
#include <string.h>
16
#include <errno.h>
17
#include <stdio.h>
18
#include <fcntl.h>
19
20
#include "cmd.h"
21
22
23
void cmd_open(int argc, char * argv[])
24
{
25
int fd;
26
27
fd = open(argv[1], O_RDWR | O_CREAT | O_APPEND, S_IRWXU);
28
if(fd == -1)
29
fd = open(argv[1], O_RDONLY);
30
31
if(fd == -1)
32
perror("open");
33
else
34
printf("open: %d\n", fd);
35
}
36
37
void cmd_lseek(int argc, char * argv[])
38
{
39
int fd, offset, whence;
40
int ret;
41
42
fd = atoi(argv[1]);
43
offset = atoi(argv[2]);
44
whence = -1;
45
46
if(strcasecmp(argv[3], "SEEK_SET") == 0)
47
whence = SEEK_SET;
48
if(strcasecmp(argv[3], "SEEK_CUR") == 0)
49
whence = SEEK_CUR;
50
if(strcasecmp(argv[3], "SEEK_END") == 0)
51
whence = SEEK_END;
52
53
if(whence == -1)
54
{
55
printf("whence was not SEEK_SET, SEEK_CUR, or SEEK_END\n");
56
return;
57
}
58
59
if((ret = lseek(fd, offset, whence)) == -1)
60
perror("lseek");
61
else
62
printf("lseek: %i\n", ret);
63
}
64
65
void cmd_read(int argc, char * argv[])
66
{
67
int fd, size;
68
int read_out, rsz;
69
char buf[512];
70
71
fd = atoi(argv[1]);
72
{ /* Get max length to read... ugly. */
73
int cur, end;
74
75
cur = lseek(fd, 0, SEEK_CUR);
76
end = lseek(fd, 0, SEEK_END);
77
78
size = end - cur;
79
lseek(fd, cur, SEEK_SET);
80
}
81
if(argc > 2)
82
size = atoi(argv[2]);
83
84
for(rsz = 0; rsz < size;)
85
{
86
read_out = read(fd, buf, __MIN_NUM(sizeof(buf), size - rsz));
87
if(read_out == -1)
88
return;
89
write(1, buf, read_out);
90
rsz += read_out;
91
}
92
}
93
94
void cmd_write(int argc, char * argv[])
95
{
96
int fd, size = -1;
97
int read_in, rsz;
98
char buf[512];
99
100
fd = atoi(argv[1]);
101
if(argc > 2)
102
size = atoi(argv[2]);
103
104
for(rsz = 0; rsz < size || size == -1;)
105
{
106
if(size != -1)
107
read_in = read(1, buf, __MIN_NUM(sizeof(buf), size - rsz));
108
else
109
read_in = read(1, buf, sizeof(buf));
110
111
if(read_in == -1)
112
return;
113
114
if(size == -1 && read_in >= 3)
115
{
116
char local[sizeof(buf)];
117
118
memcpy(local, buf, sizeof(buf));
119
if(local[read_in - 1] == '\n')
120
local[read_in - 1] = '\0';
121
if(local[read_in - 1] == '\r')
122
local[read_in - 1] = '\0';
123
124
if(strcmp(local, "EOF") == 0)
125
return;
126
}
127
128
write(fd, buf, read_in);
129
rsz += read_in;
130
}
131
}
132
133
void cmd_close(int argc, char * argv[])
134
{
135
if(close(atoi(argv[1])) == -1)
136
perror("close");
137
}
138
139
void cmd_dup(int argc, char * argv[])
140
{
141
int new_fd;
142
143
if((new_fd = dup(atoi(argv[1]))) == -1)
144
perror("dup");
145
146
printf("%i\n", new_fd);
147
}
148
149
void cmd_dup2(int argc, char * argv[])
150
{
151
if(dup2(atoi(argv[1]), atoi(argv[2])) == -1)
152
perror("dup2");
153
}
154
155