-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsneaky_server.c
232 lines (206 loc) · 6.66 KB
/
sneaky_server.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
221
222
223
224
225
226
227
228
229
230
231
232
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define BUFFSIZE 10000
// string tracking the shell's working directory
char workDir[200];
// string tracking the previous working directory
char prevDir[200];
void runCmd(char *cmd, char *buff){
char command[500] = "cd ";
strcat(command, workDir);
strcat(command, " && ");
strcat(command, cmd);
FILE* cmdOut = popen(command, "r");
if(cmdOut == NULL){
perror("popen");
exit(1);
}
int n=0;
char *buffStart = buff;
while(fgets(buff, BUFFSIZE, cmdOut) != NULL){
puts(buff);
n = strlen(buff);
buff = buff+n;
if(buffStart+BUFFSIZE < buff){
buff=buffStart;
}
}
//buff = buffStart;
pclose(cmdOut);
}
void checkCmd(char* cmd, char*buff){
// define strings for client strings to match
char cd[2] = "cd";
char upDir[2] = "..";
char rootDir[1] = "~";
char explDir[1] = "/";
char blankLine[1] = "\0";
char backDir[1] = "-";
// parse first two client command tokens
char * cmdType = strtok(cmd, " ");
char * cmdCrit = strtok(NULL, " ");
// parse client command tokens
if (cmdType == NULL){
printf("Client entered a blank line command\n");
} else if (strncmp(cd, cmdType, 2) == 0){
// parse second token of the cd command
if ( (cmdCrit == NULL) || (strncmp(rootDir, cmdCrit, 1) == 0) ){
printf("change wd to root directory\n");
// save previous wd in prevDir
memset(prevDir, 0, 200);
strcpy(prevDir, workDir);
// clear workDir to set to root folder string
memset(workDir, 0, 200);
runCmd("pwd", workDir);
int lastChar = strlen(workDir) - 1;
if (workDir[lastChar] == '\n'){
workDir[lastChar] = '\0';
}
} else if (strncmp(upDir, cmdCrit, 2) == 0){
printf("change wd up a directory\n");
// save previous wd in prevDir
memset(prevDir, 0, 200);
strcpy(prevDir, workDir);
// get total length of wd
int dirLen = strlen(workDir);
// isolate the string to be removed from the dir path
char * rem = strrchr(workDir, '/');
// get length of the isolated string
int remLen = strlen(rem);
// get the index to put the blank line character at
int newDirLen = dirLen - remLen;
// remove the last folder name from the directory path
workDir[newDirLen] = '\0';
} else if (strncmp(explDir, cmdCrit, 1) == 0){
printf("Explicit workDir: %s\n", cmdCrit);
// save previous wd in prevDir
memset(prevDir, 0, 200);
strcpy(prevDir, workDir);
// copy explicit directory declaration into workDir
strcpy(workDir, cmdCrit);
} else if(strncmp(backDir, cmdCrit, 1) == 0){
// command to go lo the last visited directory
char temp[200];
// save current wd into temp
memset(temp, 0, 200);
strcpy(temp, workDir);
// save prevDir into workDir
memset(workDir, 0, 200);
strcpy(workDir, prevDir);
// save previous dir into prevDir from temp
memset(prevDir, 0, 200);
strcpy(prevDir, temp);
} else {
printf("Subdirectory: %s\n", cmdCrit);
// save previous wd in prevDir
memset(prevDir, 0, 200);
strcpy(prevDir, workDir);
// add subdirectory name on to wd
strcat(workDir, "/");
strcat(workDir, cmdCrit);
}
char * cmdConcat = strtok(NULL, " ");
char and[2] = "&&";
if ( (cmdConcat != NULL) && (strncmp(and, cmdConcat, 2) == 0) ){
char * rawRemCmd = strstr(workDir, "&&");
char * remCmd = &rawRemCmd[2];
checkCmd(remCmd, buff);
}
} else {
runCmd(cmd, buff);
}
}
int main()
{
int sd; /* socket descriptor */
int connected_sd; /* socket descriptor */
int rc; /* return code from recvfrom */
struct sockaddr_in server_address;
struct sockaddr_in from_address;
char clientCmd[BUFFSIZE];
//char wdBuff[BUFFSIZE];
char cmdMsg[BUFFSIZE];
int flags = 0;
socklen_t fromLength;
//int clientSDList[MAXCLIENTS] = {0}; // NEW
int clientSD = 0;
fd_set socketFDS; // NEW
int maxSD = 0;//NEW
int i;
sd = socket (AF_INET, SOCK_STREAM, 0);
server_address.sin_family = AF_INET;
server_address.sin_port = htons(24000);
server_address.sin_addr.s_addr = INADDR_ANY;
if((bind (sd, (struct sockaddr *)&server_address, sizeof(server_address))) != 0){
printf("socket bind failed...\n");
exit(0);
} else printf("Socket successfully binded..\n");
if((listen (sd, 5)) != 0){
printf("Listen failed...\n");
exit(0);
} else printf("Server listening..\n");
// get working directory to send to client
runCmd("pwd", workDir);
int lastChar = strlen(workDir) - 1;
if (workDir[lastChar] == '\n'){
workDir[lastChar] = '\0';
}
strcpy(prevDir, workDir);
maxSD = sd;
for(;;){
// clear relevent buffers
memset (clientCmd, 0, BUFFSIZE);
memset (cmdMsg, 0, BUFFSIZE);
FD_ZERO(&socketFDS);
FD_SET(sd, &socketFDS); //Sets the bit for the initial SD
rc = select (maxSD+1, &socketFDS, NULL, NULL, NULL); // NEW block until something arrives
if (FD_ISSET(sd, &socketFDS)){
connected_sd = accept (sd, (struct sockaddr *) &from_address, &fromLength);
if (connected_sd < 0) {
printf("Server connection acccept failed...\n");
exit(0);
} else printf("Server acccepted the client...\n");
//printf("Client connected with descriptor: %i\n", connected_sd);
clientSD = connected_sd;
FD_SET(clientSD, &socketFDS);
if (clientSD > maxSD)
maxSD = clientSD;
if (FD_ISSET(clientSD, &socketFDS)){
for(;;){
memset (clientCmd, 0, BUFFSIZE);
//memset (wdBuff, 0, BUFFSIZE);
memset (cmdMsg, 0, BUFFSIZE);
// write dir to client socket
write(clientSD, workDir, 200);
memset (clientCmd, 0, BUFFSIZE);
// read client comand off socket
rc = read(clientSD, clientCmd, BUFFSIZE);
// check if the client disconnected
if (rc == 0 ){
printf("Client has disconnected\n");
FD_CLR(clientSD, &socketFDS);
close (clientSD); // close the socket
break;
}
// run client command and get the terminal return
checkCmd(clientCmd, cmdMsg);
//write the command return to the client socket
write(clientSD, cmdMsg, BUFFSIZE);
printf ("Client issued command: [%s] to the terminal\n", clientCmd);
printf ("Terminal responded with:\n %s\n", cmdMsg);
}
}else {
printf ("Client socket not set in socketFDS\n");
}
} else {
printf ("Listening socket not set in socketFDS\n");
}
}
close(sd);
return 0;
}