-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrsh.c
More file actions
205 lines (168 loc) · 4.63 KB
/
rsh.c
File metadata and controls
205 lines (168 loc) · 4.63 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
#include <stdio.h>
#include <stdlib.h>
#include <spawn.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <pthread.h>
#include <signal.h>
#define N 13
extern char **environ;
char uName[20];
char *allowed[N] = {"cp","touch","mkdir","ls","pwd","cat","grep","chmod","diff","cd","exit","help","sendmsg"};
struct message {
char source[50];
char target[50];
char msg[200];
};
void terminate(int sig) {
printf("Exiting....\n");
fflush(stdout);
exit(0);
}
void sendmsg (char *user, char *target, char *msg) {
// Send a request to the server to send the message (msg) to the target user (target)
// by creating the message structure and writing it to server's FIFO
int server = open("serverFIFO", O_WRONLY);
struct message req;
strncpy(req.source, user, 50);
strncpy(req.target, target, 50);
strncpy(req.msg, msg, 200);
write(server, &req, sizeof(struct message));
close(server);
}
void* messageListener(void *arg) {
// Read user's own FIFO in an infinite loop for incoming messages
// The logic is similar to a server listening to requests
// print the incoming message to the standard output in the
// following format
// Incoming message from [source]: [message]
// put an end of line at the end of the message
// printf("im a thread :D\n"); fflush(stdout);
int self_fifo = open(uName, O_RDONLY);
// printf("opened!"); fflush(stdout);
struct message req;
while (1) {
if (read(self_fifo, &req, sizeof(struct message))!=sizeof(struct message)) {
continue;
}
printf("Incoming message from %s: %s\n", req.source, req.msg);
}
close(self_fifo);
pthread_exit((void*)0);
}
int isAllowed(const char*cmd) {
int i;
for (i=0;i<N;i++) {
if (strcmp(cmd,allowed[i])==0) {
return 1;
}
}
return 0;
}
int main(int argc, char **argv) {
pid_t pid;
char **cargv;
char *path;
char line[256];
int status;
posix_spawnattr_t attr;
if (argc!=2) {
printf("Usage: ./rsh <username>\n");
exit(1);
}
signal(SIGINT,terminate);
strcpy(uName,argv[1]);
// create the message listener thread
pthread_t tid;
pthread_create(&tid, NULL, messageListener, NULL);
while (1) {
fprintf(stderr,"rsh>");
if (fgets(line,256,stdin)==NULL) continue;
if (strcmp(line,"\n")==0) continue;
line[strlen(line)-1]='\0';
char cmd[256];
char line2[256];
strcpy(line2,line);
strcpy(cmd,strtok(line," "));
if (!isAllowed(cmd)) {
printf("NOT ALLOWED!\n");
continue;
}
if (strcmp(cmd,"sendmsg")==0) {
// Create the target user and
// the message string and call the sendmsg function
// NOTE: The message itself can contain spaces
// If the user types: "sendmsg user1 hello there"
// target should be "user1"
// and the message should be "hello there"
// if no argument is specified, you should print the following
// printf("sendmsg: you have to specify target user\n");
// if no message is specified, you should print the followingA
// printf("sendmsg: you have to enter a message\n");
char *target=strtok(NULL," ");
if(target == NULL) {
printf("sendmsg: you have to specify target user\n");
continue;
}
char *message=strtok(NULL,"");
if(message == NULL) {
printf("sendmsg: you have to enter a message\n");
continue;
}
sendmsg(uName, target, message);
continue;
}
if (strcmp(cmd,"exit")==0) break;
if (strcmp(cmd,"cd")==0) {
char *targetDir=strtok(NULL," ");
if (strtok(NULL," ")!=NULL) {
printf("-rsh: cd: too many arguments\n");
}
else {
chdir(targetDir);
}
continue;
}
if (strcmp(cmd,"help")==0) {
printf("The allowed commands are:\n");
for (int i=0;i<N;i++) {
printf("%d: %s\n",i+1,allowed[i]);
}
continue;
}
cargv = (char**)malloc(sizeof(char*));
cargv[0] = (char *)malloc(strlen(cmd)+1);
path = (char *)malloc(9+strlen(cmd)+1);
strcpy(path,cmd);
strcpy(cargv[0],cmd);
char *attrToken = strtok(line2," "); /* skip cargv[0] which is completed already */
attrToken = strtok(NULL, " ");
int n = 1;
while (attrToken!=NULL) {
n++;
cargv = (char**)realloc(cargv,sizeof(char*)*n);
cargv[n-1] = (char *)malloc(strlen(attrToken)+1);
strcpy(cargv[n-1],attrToken);
attrToken = strtok(NULL, " ");
}
cargv = (char**)realloc(cargv,sizeof(char*)*(n+1));
cargv[n] = NULL;
// Initialize spawn attributes
posix_spawnattr_init(&attr);
// Spawn a new process
if (posix_spawnp(&pid, path, NULL, &attr, cargv, environ) != 0) {
perror("spawn failed");
exit(EXIT_FAILURE);
}
// Wait for the spawned process to terminate
if (waitpid(pid, &status, 0) == -1) {
perror("waitpid failed");
exit(EXIT_FAILURE);
}
// Destroy spawn attributes
posix_spawnattr_destroy(&attr);
}
return 0;
}