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_fs.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/param.h>
10
#include <sys/stat.h>
11
#include <stdlib.h>
12
#include <limits.h>
13
#include <unistd.h>
14
#include <string.h>
15
#include <errno.h>
16
#include <stdio.h>
17
#include <dirent.h>
18
#include <pwd.h>
19
#include <grp.h>
20
#include <sys/fcntl.h>
21
22
#include "cmd.h"
23
24
25
void ls_dofile(struct stat, char *);
26
27
void cmd_ls(int argc, char * argv[])
28
{
29
char * path = ".";
30
DIR * dirp;
31
struct dirent * dp;
32
struct stat sb;
33
34
if(argc > 1)
35
path = argv[1];
36
37
if(stat(path, &sb) == -1)
38
{
39
perror("stat");
40
return;
41
}
42
43
if(!S_ISDIR(sb.st_mode))
44
{
45
ls_dofile(sb, path);
46
}
47
else
48
{
49
if((dirp = opendir(path)) == NULL)
50
{
51
perror("opendir");
52
return;
53
}
54
55
while((dp = readdir(dirp)) != NULL)
56
{
57
char buf[MAXPATHLEN+1];
58
59
if(strlen(path) + strlen(dp->d_name) + 1 > MAXPATHLEN)
60
continue;
61
snprintf(buf, MAXPATHLEN, "%s/%s", path, dp->d_name);
62
if(stat(buf, &sb) == -1)
63
continue;
64
65
ls_dofile(sb, dp->d_name);
66
}
67
}
68
}
69
70
void ls_dofile(struct stat sb, char * file_name)
71
{
72
char perm[11] = "----------";
73
74
if(sb.st_mode & 0400) perm[1] = 'r';
75
if(sb.st_mode & 0200) perm[2] = 'w';
76
if(sb.st_mode & 0100) perm[3] = 'x';
77
if(sb.st_mode & 0040) perm[4] = 'r';
78
if(sb.st_mode & 0020) perm[5] = 'w';
79
if(sb.st_mode & 0010) perm[6] = 'x';
80
if(sb.st_mode & 0004) perm[7] = 'r';
81
if(sb.st_mode & 0002) perm[8] = 'w';
82
if(sb.st_mode & 0001) perm[9] = 'x';
83
if(sb.st_mode & S_ISVTX)
84
{
85
if(sb.st_mode & 0001)
86
perm[9] = 't';
87
else
88
perm[9] = 'T';
89
}
90
if(sb.st_mode & S_ISGID)
91
{
92
if(sb.st_mode & 0010)
93
perm[6] = 'S';
94
else
95
perm[6] = 's';
96
}
97
if(sb.st_mode & S_ISUID)
98
{
99
if(sb.st_mode & 0100)
100
perm[3] = 'S';
101
else
102
perm[3] = 's';
103
}
104
if(S_ISBLK(sb.st_mode)) perm[0] = 'b';
105
if(S_ISCHR(sb.st_mode)) perm[0] = 'c';
106
if(S_ISDIR(sb.st_mode)) perm[0] = 'd';
107
if(S_ISLNK(sb.st_mode)) perm[0] = 'l'; /* XXX: works? */
108
if(S_ISFIFO(sb.st_mode)) perm[0] = 'p';
109
if(S_ISSOCK(sb.st_mode)) perm[0] = 's';
110
111
printf("%s %3i %s %s %6i %s %s\n", perm, (int)sb.st_nlink, \
112
get_uid_str(sb.st_uid), get_gid_str(sb.st_gid), \
113
(int)sb.st_size, get_time_str("%b %d %H:%M"), file_name);
114
}
115
116
void cmd_getcwd(int argc, char * argv[])
117
{
118
/* This should be big enough to accomodate all cases. */
119
char buf[MAXPATHLEN + 1];
120
121
if(getcwd(buf, sizeof(buf)) == NULL)
122
perror("getcwd");
123
else
124
printf("%s\n", buf);
125
}
126
127
void cmd_setcwd(int argc, char * argv[])
128
{
129
if(argc < 2)
130
cmd_getcwd(argc, argv);
131
else
132
if(chdir(argv[1]))
133
perror("chdir");
134
}
135
136
137
void cmd_chmod(int argc, char * argv[])
138
{
139
int perm;
140
141
errno = 0;
142
perm = (int)strtol(argv[1], (char **)NULL, 8);
143
if(errno)
144
{
145
perror("strtol");
146
return;
147
}
148
149
if(chmod(argv[2], perm) == -1)
150
perror("chmod");
151
}
152
153
void cmd_chown(int argc, char * argv[])
154
{
155
struct passwd * pwd;
156
int uid;
157
158
errno = 0;
159
uid = (int)strtol(argv[1], (char **)NULL, 10);
160
if(errno)
161
{
162
if((pwd = getpwnam(argv[1])) == NULL)
163
{
164
perror("getpwnam");
165
return;
166
}
167
168
uid = pwd->pw_uid;
169
}
170
171
if(chown(argv[2], uid, -1) == -1)
172
perror("chown");
173
}
174
175
void cmd_chgrp(int argc, char * argv[])
176
{
177
struct group * grp;
178
int gid;
179
180
errno = 0;
181
gid = (int)strtol(argv[1], (char **)NULL, 10);
182
if(errno)
183
{
184
if((grp = getgrnam(argv[1])) == NULL)
185
{
186
perror("getgrnam");
187
return;
188
}
189
190
gid = grp->gr_gid;
191
}
192
193
if(chown(argv[2], -1, gid) == -1)
194
perror("chown");
195
}
196
197
void cmd_chdir(int argc, char * argv[])
198
{
199
if(chdir(argv[1]) == -1)
200
perror("chdir");
201
}
202
203
void cmd_mkdir(int argc, char * argv[])
204
{
205
int perm = 0755;
206
207
if(argc > 2)
208
{
209
errno = 0;
210
perm = (int)strtol(argv[2], (char **)NULL, 8);
211
if(errno)
212
{
213
perror("strtol");
214
return;
215
}
216
}
217
218
if(mkdir(argv[1], perm) == -1)
219
perror("mkdir");
220
}
221
222
void cmd_rmdir(int argc, char * argv[])
223
{
224
if(rmdir(argv[1]) == -1)
225
perror("rmdir");
226
}
227
228
void cmd_rename(int argc, char * argv[])
229
{
230
if(rename(argv[1], argv[2]) == -1)
231
perror("rename");
232
}
233
234
void cmd_unlink(int argc, char * argv[])
235
{
236
if(unlink(argv[1]) == -1)
237
perror("unlink");
238
}
239
240
void cmd_chroot(int argc, char * argv[])
241
{
242
if(chroot(argv[1]) == -1)
243
perror("chroot");
244
}
245
246
void cmd_link(int argc, char * argv[])
247
{
248
if(link(argv[1], argv[2]) == -1)
249
perror("link");
250
}
251
252
void cmd_symlink(int argc, char * argv[])
253
{
254
if(symlink(argv[1], argv[2]) == -1)
255
perror("symlink");
256
}
257
258
void cmd_cp(int argc, char * argv[])
259
{
260
int src, dst, len;
261
char buff[4096];
262
struct stat s;
263
char *path, *p, *t;
264
265
path = strdup(argv[2]);
266
267
src = open(argv[1], O_RDONLY);
268
if(src == -1) {
269
free(path);
270
perror("open(src)");
271
}
272
273
dst = open(path, O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);
274
if (dst == -1) {
275
276
if(errno == EISDIR) {
277
t = strrchr(argv[1], '/');
278
if (t != NULL) {
279
t++;
280
} else {
281
t = argv[1];
282
}
283
284
p = malloc(strlen(path) + strlen(t) + 2);
285
sprintf(p, "%s/%s", path, t);
286
free(path);
287
path = p;
288
289
dst = open(path, O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);
290
if ( dst == -1 ) {
291
close(src);
292
free(path);
293
perror("open(dst)");
294
}
295
296
} else {
297
close(src);
298
free(path);
299
perror("open(dst)");
300
}
301
}
302
303
stat(argv[1], &s);
304
305
while(1)
306
{
307
len = read(src, buff, sizeof(buff));
308
if (len == -1) break;
309
if (len == 0) break;
310
311
write(dst, buff, len);
312
if (len < sizeof(buff)) break;
313
}
314
close(src);
315
close(dst);
316
317
chmod(path, s.st_mode);
318
free(path);
319
}
320
321