-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkftp_mod.c
176 lines (155 loc) · 4.13 KB
/
kftp_mod.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
#include <linux/init.h>
#include <linux/module.h>
#include <linux/uaccess.h>
#include <linux/fs.h>
#include <linux/string.h>
#include <net/net_namespace.h>
#include <linux/net.h>
#include <linux/in.h>
#include <linux/socket.h>
#include <net/sock.h>
#include "ftp_serv.h"
#include "ftp_utils.h"
MODULE_LICENSE("GPL");
static char *startmsg = "220 kFTP - the kernel level FTP server nobody asked for!\r\n";
static struct socket *ftpsock;
int processFTP(struct client_context* ctx,struct socket* clientsock,char* buf)
{
printk(KERN_INFO "kftp: recieved FTP command: %s",buf);
if (!strncmp(buf,"USER",4))
{
ftpUSER(ctx, clientsock);
}
else if (!strncmp(buf,"PASS",4))
{
ftpPASS(ctx, clientsock);
}
else if (!strncmp(buf,"SYST",4))
{
ftpSYST(ctx, clientsock);
}
else if (!strncmp(buf,"PASV",4))
{
ftpPASV(ctx, clientsock);
}
else if (!strncmp(buf,"EPSV",4))
{
ftpEPSV(ctx, clientsock);
}
else if (!strncmp(buf,"QUIT",4))
{
ftpQUIT(ctx, clientsock);
}
else if (!strncmp(buf,"TYPE",4))
{
ftpTYPE(ctx, clientsock);
}
else if (!strncmp(buf,"RETR",4))
{
const char* path = strrchr(buf,' ')+1;
if (strrchr(buf,'\r'))
*strrchr(buf,'\r') = 0;
ftpRETR(ctx, clientsock, path);
}
else if (!strncmp(buf,"LIST",4))
{
const char* pos = strrchr(buf,' ');
const char* path;
if (strrchr(buf,'\r'))
*strrchr(buf,'\r') = 0;
if (!pos)
path = "/";
else
path = pos+1;
ftpLIST(ctx, clientsock, path);
}
else
{
printk(KERN_INFO "kftp: Unrecognized Command\n");
char* response = "500 UNRECOGNIZED COMMAND\r\n";
kernel_send(clientsock,response,strlen(response));
}
return 0;
}
int recieve_loop(struct socket* clientsock)
{
printk(KERN_INFO "Allocating client socket object.\n");
struct client_context *ctx = kmalloc(sizeof(struct client_context),GFP_KERNEL);
ctx->passive = 0;
ctx->authorized = 0;
ctx->dataconnclient = NULL;
ctx->dataconnserver = NULL;
ctx->controlconnclient = clientsock;
printk("kftp: beginning client loop\n");
kernel_send(clientsock,startmsg,strlen(startmsg));
char buf[256];
while (1)
{
memset(buf,0,256);
int index = 0;
while (index < 256)
{
int r = kernel_recv(clientsock,buf+index,1);
printk("%x\n",buf[index]);
if (r <= 0)
{
printk(KERN_ERR "Message recv fail!\n");
return -1;
}
if (buf[index] == 0xa){
break;
}
index++;
}
if (processFTP(ctx,clientsock,buf) != 0){
break;
}
}
printk(KERN_INFO "Freeing client socket object.\n");
sock_release(ctx->controlconnclient);
kfree(ctx);
return 0;
}
static int kftp_start(void)
{
printk(KERN_INFO "kftp: starting kftp server at 2121\n");
sock_create(PF_INET, SOCK_STREAM,IPPROTO_TCP,&ftpsock);
struct sockaddr_in serv;
serv.sin_addr.s_addr = 0;
serv.sin_family = AF_INET;
serv.sin_port = htons(2121);
int* optval = 1;
sock_set_reuseaddr(ftpsock->sk);
sock_set_reuseport(ftpsock->sk);
int bind = kernel_bind(ftpsock,(struct sockaddr*) &serv,sizeof(serv));
if (bind < 0)
{
printk(KERN_ERR "kftp: failed to bind at port 2121\n");
return -1;
}
kernel_listen(ftpsock, 10);
struct socket *clientsock;
printk(KERN_INFO "Acceping connections!\n");
while (1)
{
int err = kernel_accept(ftpsock,&clientsock,0);
if (err < 0)
{
printk(KERN_ERR "kftp: failed to register new client!\n");
break;
}
else
{
printk(KERN_INFO "kftp: new client accepted: %p!",clientsock);
recieve_loop(clientsock);
}
}
return 0;
}
static void kftp_stop(void)
{
printk(KERN_INFO "kftp: stopping kftp server\n");
sock_release(ftpsock);
}
module_init(kftp_start);
module_exit(kftp_stop);