Repository navigation
Expand file tree
/
Copy pathftpd.c
More file actions
405 lines (340 loc) · 7.33 KB
/
Copy pathftpd.c
File metadata and controls
405 lines (340 loc) · 7.33 KB
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/* ftpd.c Copyright 1992-2000 by Michael Temari All Rights Reserved
*
* ftpd An FTP server program for use with Minix.
*
* Usage: Minix usage: tcpd ftp ftpd
*
* 06/14/92 Tnet Release Michael Temari
* 01/15/96 0.30 Michael Temari
* 01/25/96 0.90 Michael Temari
* 03/17/96 0.91 Michael Temari
* 06/27/96 0.92 Michael Temari
* 07/02/96 0.93 Michael Temari
* 07/15/96 0.94 Michael Temari
* 08/27/96 0.95 Michael Temari
* 02/09/97 0.96 Michael Temari
* 02/10/97 0.97 Michael Temari
* 09/25/97 0.98 Michael Temari
* 03/10/00 0.99 Michael Temari, <Michael@TemWare.Com>
* 12/12/03 1.00 Michael Temari, <Michael@TemWare.Com>
* 02/06/05 1.01 Michael Temari, <Michael@TemWare.Com>
* 02/12/05 2.00 Michael Temari, <Michael@TemWare.Com>
*/
char *FtpdVersion = "2.00";
#include <sys/types.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <net/gen/in.h>
#include <net/gen/tcp.h>
#include "ftpd.h"
#include "access.h"
#include "file.h"
#include "net.h"
_PROTOTYPE(static void init, (void));
_PROTOTYPE(static int doHELP, (char *buff));
_PROTOTYPE(static int doNOOP, (char *buff));
_PROTOTYPE(static int doUNIMP, (char *buff));
_PROTOTYPE(static int getline, (char *line, int len));
FILE *msgfile = (FILE *)NULL;
/* The following defines the inactivity timeout in seconds */
#define INACTIVITY_TIMEOUT 60*5
char *days[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
char line[512];
int type, format, mode, structure;
int ftpdata_fd = -1;
int loggedin, gotuser, anonymous;
char username[80];
char anonpass[128];
char newroot[128];
ipaddr_t myipaddr, rmtipaddr, dataaddr;
tcpport_t myport, rmtport, dataport;
char myhostname[256], rmthostname[256];
#define FTPD_LOG "/usr/adm/ftpd.log"
#define FTPD_MSG "/etc/ftpd_msg"
FILE *logfile;
int timeout = 0;
_PROTOTYPE(static int doHELP, (char *buff));
_PROTOTYPE(int readline, (char **args));
_PROTOTYPE(void Timeout, (int sig));
_PROTOTYPE(int main, (int argc, char *argv[]));
struct commands {
char *name;
_PROTOTYPE(int (*func), (char *buff));
};
struct commands commands[] = {
"ABOR", doUNIMP,
"ACCT", doUNIMP,
"ALLO", doALLO,
"APPE", doAPPE,
"CDUP", doCDUP,
"CWD", doCWD,
"DELE", doDELE,
"HELP", doHELP,
"LIST", doLIST,
"MDTM", doMDTM,
"MKD", doMKD,
"MODE", doMODE,
"NLST", doNLST,
"NOOP", doNOOP,
"PASS", doPASS,
"PASV", doPASV,
"PORT", doPORT,
"PWD", doPWD,
"QUIT", doQUIT,
"REIN", doUNIMP,
"REST", doREST,
"RETR", doRETR,
"RMD", doRMD,
"RNFR", doRNFR,
"RNTO", doRNTO,
"SITE", doSITE,
"SIZE", doSIZE,
"SMNT", doUNIMP,
"STAT", doSTAT,
"STOR", doSTOR,
"STOU", doSTOU,
"STRU", doSTRU,
"SYST", doSYST,
"TYPE", doTYPE,
"USER", doUSER,
"XCUP", doCDUP,
"XCWD", doCWD,
"XMKD", doMKD,
"XPWD", doPWD,
"XRMD", doRMD,
"", (int (*)())0
};
static void init()
{
loggedin = 0;
gotuser = 0;
anonymous = 0;
newroot[0] = '\0';
type = TYPE_A;
format = 0;
mode = MODE_S;
structure = 0;
ftpdata_fd = -1;
username[0] = '\0';
anonpass[0] = '\0';
}
/* nothing, nada, zilch... */
static int doNOOP(buff)
char *buff;
{
printf("200 NOOP to you too!\r\n");
return(GOOD);
}
/* giv'em help, what a USER! */
static int doHELP(buff)
char *buff;
{
struct commands *cmd;
char star;
int i;
char *space = " ";
printf("214-Here is a list of available ftp commands\r\n");
printf(" Those with '*' are not yet implemented.\r\n");
i = 0;
for(cmd = commands; *cmd->name != '\0'; cmd++) {
if(cmd->func == doUNIMP)
star = '*';
else
star = ' ';
printf(" %s%c%s", cmd->name, star, space + strlen(cmd->name));
if(++i == 6) {
printf("\r\n");
i = 0;
}
}
if(i)
printf("\r\n");
printf("214 That's all the help you get.\r\n");
return(GOOD);
}
/* not implemented */
static int doUNIMP(buff)
char *buff;
{
printf("502 Command \"%s\" not implemented!\r\n", line);
return(GOOD);
}
/* convert line for use */
void cvtline(args)
char **args;
{
char *p;
p = line + strlen(line);
while(--p >= line)
if(*p == '\r' || *p == '\n' || isspace(*p))
*p = '\0';
else
break;
p = line;
#ifdef DEBUG
logit("COMMAND", line);
#endif
while(*p && !isspace(*p)) {
*p = toupper(*p);
p++;
}
if(*p) {
*p = '\0';
p++;
while(*p && isspace(*p))
p++;
}
*args = p;
return;
}
static int getline(line, len)
char *line;
int len;
{
int s;
int gotcr;
/* leave room for at end for null */
len--;
/* got to be able to put in at least 1 character */
if(len < 1)
return(-1);
gotcr = 0;
while(len-- > 0) {
s = read(0, line, 1);
if(s != 1)
return(-1);
if(*line == '\n')
break;
gotcr = (*line == '\r');
line++;
}
if(gotcr)
--line;
*line = '\0';
return(0);
}
int readline(args)
char **args;
{
if(getline(line, sizeof(line)))
return(BAD);
cvtline(args);
return(GOOD);
}
/* signal handler for inactivity timeout */
void Timeout(sig)
int sig;
{
timeout = 1;
printf("421 Inactivity timer expired.\r\n");
}
/* logit */
void logit(type, parm)
char *type;
char *parm;
{
time_t now;
struct tm *tm;
if(logfile == (FILE *)NULL)
return;
time(&now);
tm = localtime(&now);
fprintf(logfile, "%4d%02d%02d%02d%02d%02d ",
1900+tm->tm_year,
tm->tm_mon + 1,
tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec);
fprintf(logfile, "%s %s %s %s %s\n",
rmthostname, username, anonymous ? anonpass : username, type, parm);
fflush(logfile);
}
void showmsg(reply, filename)
char *reply;
char *filename;
{
FILE *mfp;
char *pe;
static char mline[256];
if(filename == (char *)NULL)
mfp = msgfile;
else
mfp = fopen(filename, "r");
if(mfp == (FILE *)NULL)
return;
while(fgets(mline, sizeof(mline), mfp) != (char *)NULL) {
pe = mline + strlen(mline);
while(--pe >= mline)
if(*pe == '\r' || *pe == '\n')
*pe = '\0';
else
break;
printf("%s- %s\r\n", reply, mline);
}
if(filename != (char *)NULL)
fclose(mfp);
}
int main(argc, argv)
int argc;
char *argv[];
{
struct commands *cmd;
char *args;
int status;
time_t now;
struct tm *tm;
int s;
GetNetInfo();
/* open transfer log file if it exists */
if((logfile = fopen(FTPD_LOG, "r")) != (FILE *)NULL) {
fclose(logfile);
logfile = fopen(FTPD_LOG, "a");
}
/* open login msg file */
msgfile = fopen(FTPD_MSG, "r");
/* Let's initialize some stuff */
init();
/* Log the connection */
logit("CONNECT", "");
/* Tell 'em we are ready */
time(&now);
tm = localtime(&now);
printf("220 FTP service (Ftpd %s) ready on %s at ",
FtpdVersion, myhostname);
printf("%s, %02d %s %d %02d:%02d:%02d %s\r\n", days[tm->tm_wday],
tm->tm_mday, months[tm->tm_mon], 1900+tm->tm_year,
tm->tm_hour, tm->tm_min, tm->tm_sec,
tzname[tm->tm_isdst]);
fflush(stdout);
/* Loop here getting commands */
while(1) {
signal(SIGALRM, Timeout);
alarm(INACTIVITY_TIMEOUT);
if(readline(&args) != GOOD) {
if(!timeout)
printf("221 Control connection closing (EOF).\r\n");
break;
}
alarm(0);
for(cmd = commands; *cmd->name != '\0'; cmd++)
if(!strcmp(line, cmd->name))
break;
if(*cmd->name != '\0')
status = (*cmd->func)(args);
else {
printf("500 Command \"%s\" not recognized.\r\n", line);
status = GOOD;
}
fflush(stdout);
if(status != GOOD)
break;
}
CleanUpPasv();
return(-1);
}