Repository navigation
Expand file tree
/
Copy pathfunc.cpp
More file actions
350 lines (325 loc) · 7.15 KB
/
Copy pathfunc.cpp
File metadata and controls
350 lines (325 loc) · 7.15 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
#include<iostream>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<string.h>
#include<fstream>
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<errno.h>
#include<sys/types.h>
#include<syslog.h>
#include<fcntl.h>
#include<signal.h>
#include<sys/wait.h>
#include<dirent.h>
#include<string.h>
#include <vector>
#include "include/userauth.h"
#define SIZE 200
using namespace std;
//Check whether the username and password are valid or not
bool validate(char* uname,char* pass)
{
ifstream fin;
fin.open("users.txt");
if(!fin)
{
perror("Error in opening file");
exit(1);
}
else
{
char name[30],password[30];
fin>>name>>password;
cout<<"From validate"<<endl;
cout<<name<<" "<<uname<<endl;
cout<<pass<<" "<<password<<endl;
do
{
if((strcmp(name,uname)==0) && (strcmp(pass,password)==0))
{
return true;
}
fin>>name>>password;
}while(!fin.eof());
return false;
}
fin.close();
}
//Checking the IP address is blacklisted or not
bool validip(string ip)
{
//ofstream log=logger.getofsLog();
//log << "[INFO]: validating the ipnaddress \n";
fstream newfile;
int flag=0;
string line;
newfile.open("blacklist_ip.txt",ios::in);
if (newfile.is_open())
{
while(getline(newfile, line))
{
if (line.compare(ip)== 0)
{
flag=1;
break;
}
}
if(flag==1)
{
return false;
}
else
{
return true;
}
newfile.close();
}
else
{ // log << "[ERROR]: validating the ipnaddress \n";
perror(" error in opening the file");
exit(1);
}
}
//Listing the files present in pub directory
vector<string> explore(char *dirname)
{
DIR *dir;
struct dirent *entry;
struct stat info;
dir=opendir(dirname);
vector<string> filevector;
cout << "explore" << dirname << endl;
if(!dir)
{
cout<<"directory was not found\n"<<endl;
}
while((entry=readdir(dir))!=NULL)
{
//cout << entry->d_name << endl;
if(entry->d_name[0]!='.')
filevector.push_back(entry->d_name);
}
closedir(dir);
// for(string s:filevector)
// cout << s << endl;
return filevector;
}
//Creating files in Upload directory
void upload()
{
DIR *dir;
struct dirent *entry;
fstream file;
string s,s1,s2,s3;
cout<<"enter name of file to be created"<<endl;
cin>>s;
s2="/home/anonymous/ftp/var/pub";
s3=s2.append(s);
dir=opendir("/home/anonymous/ftp/var/pub");
if(!dir)
{
cout<<"directory was not found\n"<<endl;
return;
}
file.open(s3,ios::out);
if(file.is_open())
{
cout<<"enter the content in file"<<endl;
cin>>s1;
file<<s1<<endl;
file.close();
cout<<"file write operation completed"<<endl<<endl;
}
else
{
cout<<"unable to create a file"<<endl;
}
while((entry=readdir(dir))!=NULL)
{
if(entry->d_name[0]!='.')
{
cout<<"/home/anonymous/ftp/var/pub"<<endl;
cout<<entry->d_name<<" "<<endl;
}
}
closedir(dir);
}
//Listing the files present in home directory
void explorehome()
{
DIR *dir;
struct dirent *entry;
struct stat info;
dir=opendir("/home/kailash");
if(!dir)
{
cout<<"directory was not found\n"<<endl;
return;
}
while((entry=readdir(dir))!=NULL)
{
if(entry->d_name[0]!=NULL)
{
cout<<"/home/kailash"<<endl;
cout<<entry->d_name<<" "<<endl<<endl;
}
}
closedir(dir);
}
//Creating files in home directory of registered user
void uploadhome()
{
DIR *dir;
struct dirent *entry;
fstream file;
string s,s1,s2,s3;
cout<<"enter name of file to be created"<<endl;
cin>>s;
s2="/home/kailash";
s3=s2.append(s);
dir=opendir("/home/kailash");
if(!dir)
{
cout<<"directory was not found\n"<<endl;
return;
}
file.open(s3,ios::out);
if(file.is_open())
{
cout<<"enter the content in file"<<endl;
cin>>s1;
file<<s1<<endl;
file.close();
cout<<"file write operation completed"<<endl<<endl;
}
else
{
cout<<"unable to create a file"<<endl;
}
while((entry=readdir(dir))!=NULL)
{
if(entry->d_name[0]!='.')
{
cout<<"/home/kailash"<<endl;
cout<<entry->d_name<<" "<<endl;
}
}
closedir(dir);
}
//FTP client function implementation
void impl(int sockfd,int argc)
{
char command[30]="";
char cmd[SIZE];
char ack[20]="";
strcpy(ack,"received");
char buf[SIZE];
int n=0;
bzero(buf,sizeof(buf));
while(1)
{
cout << "ftp>";
bzero(cmd,sizeof(cmd));
bzero(command,sizeof(command));
cin.getline(cmd,SIZE,'\n');
strncpy(command,cmd,3);
//sending the command to the server
if (strcmp(command,"put")==0)
write(sockfd,cmd,strlen(cmd));
else if (strcmp(command,"get")==0)
{
if (argc==1)
cout << "invalid command " << endl;
else
write(sockfd,cmd,strlen(cmd));
}
else
write(sockfd,command,strlen(command));
if(strcmp(command,"pwd")==0)
{
bzero(buf,sizeof(buf));
n=read(sockfd,buf,sizeof(buf));//recv3
buf[n]='\0';
cout << buf << endl;
}
else if (strcmp(command,"ls")==0)
{
while(1)
{
bzero(buf,sizeof(buf));
n=read(sockfd,buf,sizeof(buf));//reading the files sent by server
write(sockfd,ack,strlen(ack));
if(strcmp(buf,"end")!=0)
{
cout << buf << endl;
bzero(buf,sizeof(buf));
}
else
{
break;
}
}
}
else if (strcmp(command,"bye")==0)
{
exit(0);
}
else if (strcmp(command,"put")==0)
{
char *cmd1=strtok(cmd," ");
char *fn=strtok(NULL,"");
ifstream ifs;
ifs.open(fn);
string data;
read(sockfd,buf,sizeof(buf));
while(!ifs.eof())
{
getline(ifs, data);
data=data+"\n";
write(sockfd,data.c_str(),data.length());
read(sockfd,buf,sizeof(buf));
bzero(buf,sizeof(buf));
}
write(sockfd,"end",3);
ifs.close();
}
else if ((strcmp(command,"get")==0) && argc!=1)
{
char *cmd1=strtok(cmd," ");
char *fn=strtok(NULL,"");
ofstream ofs;
ofs.open(fn);
char buf[SIZE];
while(1)
{
n=read(sockfd,buf,sizeof(buf));
buf[n]='\0';
if (strcmp(buf,"end")==0)
break;
else
{
ofs<<buf;
cout<<buf<<endl;
write(sockfd,"received",8);
}
}
ofs.close();
bzero(buf,sizeof(buf));
bzero(command,sizeof(command));
}
}
}
//FTP server function for printing directory
void printdir(int clifd, char* dir)
{
int n=write(clifd,dir,strlen(dir)); //send3
//bzero(dir,sizeof(dir));
}
//To avoid zombie process
void sighandler(int signum)
{
cout << strsignal(signum) << endl;
wait(NULL);
}