-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathftpc.c
220 lines (173 loc) · 4.57 KB
/
ftpc.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
/*
* Copyright (c) 2021 Jan Klemkow <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <err.h>
#include <limits.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
FILE *in;
FILE *out;
bool debug = false;
#define DEBUG(...) if (debug) warnx(__VA_ARGS__)
int
reply(char **str)
{
char line[BUFSIZ];
char replstr[5]; /* 123- */
int repl; /* reply code */
char ch;
if (fgets(line, sizeof line, in) == NULL)
err(EXIT_FAILURE, "fgets");
line[strcspn(line, "\r\n")] = '\0';
DEBUG("%s", line);
if (sscanf(line, "%d%c", &repl, &ch) != 2)
err(EXIT_FAILURE, "sscanf");
if (ch == ' ')
goto out;
if (ch != '-')
err(EXIT_FAILURE, "protocol error: invaild reply string");
if (snprintf(replstr, sizeof replstr, "%d ", repl) != 5)
err(EXIT_FAILURE, "protocol error: invaild reply string");
do {
if (fgets(line, sizeof line, in) == NULL)
err(EXIT_FAILURE, "fgets");
line[strcspn(line, "\r\n")] = '\0';
DEBUG("%s", line);
} while (strncmp(line, replstr, 4));
out:
if (str != NULL)
*str = strdup(line);
return repl;
}
int
vcmd(char **replstr, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(out, fmt, ap);
va_end(ap);
fputs("\r\n", out);
return reply(replstr);
}
FILE *
pasv(const char *path, char dir)
{
FILE *fh;
char cmd[BUFSIZ];
char *replstr;
unsigned char addr[4];
unsigned char port[2];
unsigned short serv;
if ((vcmd(&replstr, "PASV")) != 227)
err(EXIT_FAILURE, "PASV");
DEBUG("pasv: %s", replstr);
if (sscanf(replstr, "%*d %*[^(](%hhu,%hhu,%hhu,%hhu,%hhu,%hhu)",
&addr[0], &addr[1], &addr[2], &addr[3], &port[0], &port[1]) != 6)
err(EXIT_FAILURE, "parsing error: %s", replstr);
free(replstr);
serv = port[0] << 8 | port[1];
snprintf(cmd, sizeof cmd, "nc -N %hhu.%hhu.%hhu.%hhu %hu",
addr[0], addr[1], addr[2], addr[3], serv);
if (dir == '<' || dir == '>')
snprintf(cmd + strlen(cmd), sizeof(cmd) - strlen(cmd),
" %c '%s'", dir, path);
DEBUG("cmd: %s", cmd);
if ((fh = popen(cmd, "r")) == NULL)
err(EXIT_FAILURE, "pasv: %s", cmd);
return fh;
}
void
usage(void)
{
fprintf(stderr, "ftpc [put|get|ls] path\n");
exit(EXIT_FAILURE);
}
int
main(int argc, char *argv[])
{
char *cmd = NULL;
char *path = NULL;
FILE *fh;
char ch;
while ((ch = getopt(argc, argv, "d")) != -1) {
switch (ch) {
case 'd':
debug = true;
break;
default:
usage();
}
}
argc -= optind;
argv += optind;
if (argc < 1)
usage();
cmd = argv[0];
if (argc < 2)
path = argv[1];
/* Open UCSPI fds as streams */
if ((in = fdopen(6, "r")) == NULL)
err(EXIT_FAILURE, "fdopen");
if ((out = fdopen(7, "w")) == NULL)
err(EXIT_FAILURE, "fdopen");
/* Set input and output streams line buffered */
setvbuf(in, NULL, _IOLBF, 0);
setvbuf(out, NULL, _IOLBF, 0);
if (reply(NULL) != 220)
err(EXIT_FAILURE, "init error");
/* Login */
switch (vcmd(NULL, "USER %s", "anonymous")) {
case 230:
goto auth;
case 331:
goto pass;
default:
errx(EXIT_FAILURE, "user");
}
pass:
if (vcmd(NULL, "PASS %s", "[email protected]") != 230)
err(EXIT_FAILURE, "pass");
auth: /* Authenticated */
/* Set binary mode */
if (vcmd(NULL, "TYPE I") != 200)
errx(EXIT_FAILURE, "TYPE");
if (strcmp(cmd, "put") == 0) {
fh = pasv(path, '<');
if (vcmd(NULL, "STOR %s", path) != 150)
errx(EXIT_FAILURE, "STOR");
} else if (strcmp(cmd, "get") == 0) {
fh = pasv(path, '>');
if (vcmd(NULL, "RETR %s", path) != 150)
errx(EXIT_FAILURE, "RETR");
} else if (strcmp(cmd, "ls") == 0) {
char line[PATH_MAX];
fh = pasv(path, '|');
if (vcmd(NULL, "NLST") != 150)
errx(EXIT_FAILURE, "RETR");
while (fgets(line, sizeof line, fh) != NULL)
fputs(line, stdout);
} else {
errx(EXIT_FAILURE, "cmd %s is not implementet", cmd);
}
if (pclose(fh) != 0)
err(EXIT_FAILURE, "pclose");
if (reply(NULL) != 226)
errx(EXIT_FAILURE, "finish");
return EXIT_SUCCESS;
}