-
Notifications
You must be signed in to change notification settings - Fork 0
/
proccess.c
425 lines (385 loc) · 11 KB
/
proccess.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
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/**
* @author Jorge Teixeira Crespo (jorge.teixeira)
* @author Miguel Juncal Paz (miguel.juncalp)
*
*/
#include <stdio.h>
#include <stdlib.h>
#include "proccess.h"
#include <sys/resource.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <pwd.h>
#include <wait.h>
#define GETPRIORITY 20
#define SETPRIORITY 21
#define GETUID 22
#define SETUID 23
#define FORK 24
#define EXECUTE 25
#define FOREGROUND 26
#define BACKGROUND 27
#define RUNAS 28
#define EXECUTEAS 29
#define LISTPROCS 30
#define PROC 31
#define DELETEPROCS 32
#define EXITED_NORMALY 1111
#define EXITED_SIGNAL 1000
void getpriority_cmd(char *pid, int flag_pid);
void setpriority_cmd(char **tokens, int ntokens);
void getuid_cmd();
void setuid_cmd(char **tokens);
void fork_cmd();
void execute_cmd(char **tokens, int ntokens);
void foreground_cmd(char **tokens, int ntokens);
void background_cmd(char **tokens, int ntokens, plist pl);
void runas_cmd(char **tokens, int ntokens, plist pl);
void executeas_cmd(char **tokens, int ntokens);
void listprocs_cmd(plist pl);
void proc_cmd(char **tokens, int ntokens, plist pl);
void deleteprocs_cmd(char *token, plist pl);
/** COPIED FUNCTIONS **/
char *NombreUsuario(uid_t uid) {
struct passwd *p;
if ((p = getpwuid(uid)) == NULL)
return (" ??????");
return p->pw_name;
}
uid_t UidUsuario(char *nombre) {
struct passwd *p;
if ((p = getpwnam(nombre)) == NULL)
return (uid_t) -1;
return p->pw_uid;
}
void Cmd_getuid(char *tr[]) {
uid_t real = getuid(), efec = geteuid();
printf("Credencial real: %d, (%s)\n", real, NombreUsuario(real));
printf("Credencial efectiva: %d, (%s)\n", efec, NombreUsuario(efec));
}
void Cmd_setuid(char *tr[]) {
uid_t uid;
int u;
if (tr[0] == NULL || (!strcmp(tr[0], "-l") && tr[1] == NULL)) {
Cmd_getuid(tr);
return;
}
if (!strcmp(tr[0], "-l")) {
if ((uid = UidUsuario(tr[1])) == (uid_t) -1) {
printf("Usuario no existente %s\n", tr[1]);
return;
}
} else if ((uid = (uid_t) ((u = atoi(tr[0])) < 0) ? -1 : u) == (uid_t) -1) {
printf("Valor no valido de la credencial %s\n", tr[0]);
return;
}
if (setuid(uid) == -1)
printf("Imposible cambiar credencial: %s\n", strerror(errno));
}
int proccess_router(char **tokens, int ntokens, int cmd_index, plist pl) {
switch (cmd_index) {
case GETPRIORITY:
if (ntokens > 1)
printf("Error, check the arguments.");
else
getpriority_cmd(tokens[0], ntokens);
break;
case SETPRIORITY:
if (ntokens > 2)
printf("Error, check the arguments.");
else
setpriority_cmd(tokens, ntokens);
break;
case GETUID:
if (ntokens)
printf("Error, check the arguments.");
else
getuid_cmd();
break;
case SETUID:
if (ntokens == 0 || ntokens > 2)
printf("Error, check the arguments.");
else
setuid_cmd(tokens);
break;
case FORK:
if (ntokens)
printf("Error, check the arguments.");
else
fork_cmd();
break;
case EXECUTE:
if (ntokens == 0)
printf("Error, check the arguments.");
else
execute_cmd(tokens, ntokens);
break;
case FOREGROUND:
if (ntokens == 0)
printf("Error, check the arguments.");
else
foreground_cmd(tokens, ntokens);
break;
case BACKGROUND:
if (ntokens == 0)
printf("Error, check the arguments.");
else
background_cmd(tokens, ntokens, pl);
break;
case RUNAS:
if (ntokens < 2)
printf("Error, check the arguments.");
else
runas_cmd(tokens, ntokens, pl);
break;
case EXECUTEAS:
if (ntokens < 2)
printf("Error, check the arguments.");
else
executeas_cmd(tokens, ntokens);
break;
case LISTPROCS:
if (ntokens)
printf("Error, check the arguments.");
else
listprocs_cmd(pl);
break;
case PROC:
if (ntokens == 0 || ntokens > 2)
printf("Error, check the arguments.");
else
proc_cmd(tokens, ntokens, pl);
break;
case DELETEPROCS:
if (ntokens != 1)
printf("Error, check the arguments.");
else
deleteprocs_cmd(tokens[0],pl);
break;
}
return 0;
}
void exec_default(char** tokens, int ntokens, plist pl) {
int back_flag = 0;
if (tokens[ntokens - 1][0] == '&') {
back_flag = 1;
tokens[ntokens - 1] = NULL;
ntokens--;
}
pid_t pid = fork();
if (pid < 0) {
printf("something went wrong :(\n");
return;
}
if (pid > 0) {
if (back_flag) {
char* caller= (char*) malloc(sizeof(char)*90);
strcpy(caller,tokens[0]);
for(int i =1 ; i<ntokens ; i++){
if(strstr(tokens[i],"@")==NULL) {
strcat(caller, tokens[i]);
strcat(caller, " ");
}
}
insertItem(createInfo(pid,caller),&pl);
sleep(1);
}else
waitpid(pid, NULL, 0);
}else
execute_cmd(tokens, ntokens);
}
void deleteprocs_cmd(char *token, plist pl){
posPL pos;
pos = pl;
if(isEmptyList(pl)) {
printf("empty list, nothing to remove");
return;
} else {
if (0 == strcmp(token, "-term")) {
while(pos!=NULL){
int newstatus = check_status(pos->info.pid);
if (newstatus != -1)
pos->info.end_status = newstatus;
if(pos->info.end_status == 2){
deleteAtPosition(pos,&pl);
}
pos=pos->next;
}
}
if (0 == strcmp(token, "-sig")) {
while(pos!=NULL){
int newstatus = check_status(pos->info.pid);
if (newstatus != -1)
pos->info.end_status = newstatus;
if(pos->info.end_status==1){
deleteAtPosition(pos,&pl);
}
pos=pos->next;
}
}
}
}
void proc_cmd(char **tokens, int ntokens, plist pl){
posPL pos;
if(ntokens==1){
pos=findItemByPID(pl,atoi(tokens[0]));
if (pos==NULL){
printf("this PID doesnt exit :(\n");
showList(pl);
return;
}
printInfo(pos->info);
}else{
if(strcmp(tokens[0],"-fg")==0){
//TODO
}else{
printf("check the params!");
}
}
}
void listprocs_cmd(plist pl) {
showList(pl);
}
void executeas_cmd(char **tokens, int ntokens) {
uid_t uid;
if ((uid = UidUsuario(tokens[0])) == (uid_t) -1) {
printf("Usuario no existente %s\n", tokens[0]);
return;
}
if (setuid(uid) == -1) {
printf("Imposible cambiar credencial: %s\n", strerror(errno));
return;
}
execute_cmd(tokens + 1, ntokens - 1);
}
void runas_cmd(char **tokens, int ntokens, plist pl) {
int back_flag = 0;
if (tokens[ntokens - 1][0] == '&') {
back_flag = 1;
tokens[ntokens - 1] = NULL;
ntokens--;
}
pid_t pid = fork();
if (pid < 0) {
printf("something went wrong :(\n");
return;
}
if (pid > 0) {
if (back_flag) {//add to list
char* caller= (char*) malloc(sizeof(char)*90);
strcpy(caller,tokens[0]);
for(int i =1 ; i<ntokens ; i++){
if(strstr(tokens[i],"@")==NULL) {
strcat(caller, tokens[i]);
strcat(caller, " ");
}
}
insertItem(createInfo(pid,caller),&pl);
sleep(1);
}else
waitpid(pid, NULL, 0);
} else {
uid_t uid;
if ((uid = UidUsuario(tokens[0])) == (uid_t) -1) {
printf("Usuario no existente %s\n", tokens[0]);
exit(-1);
}
if (setuid(uid) == -1) {
printf("Imposible cambiar credencial: %s\n", strerror(errno));
exit(-1);
}
execute_cmd(tokens + 1, ntokens - 1);
}
}
void background_cmd(char **tokens, int ntokens, plist pl) {
pid_t pid = fork();
if (pid < 0) {
printf("something went wrong :(\n");
return;
}
if (pid == 0)
execute_cmd(tokens, ntokens);
char* caller= (char*) malloc(sizeof(char)*90);
strcpy(caller,tokens[0]);
for(int i =1 ; i<ntokens ; i++){
if(strstr(tokens[i],"@")==NULL) {
strcat(caller, tokens[i]);
strcat(caller, " ");
}
}
insertItem(createInfo(pid,caller),&pl);
sleep(1);
}
void foreground_cmd(char **tokens, int ntokens) {
pid_t pid = fork();
if (pid < 0) {
printf("something went wrong :(\n");
return;
}
if (pid > 0)
waitpid(pid, NULL, 0);
else
execute_cmd(tokens, ntokens);
}
void execute_cmd(char **tokens, int ntokens) {
if (tokens[ntokens - 1][0] == '@') {
setpriority_cmd(tokens + ntokens - 1, 1);
tokens[ntokens - 1] = NULL;
}
execvp(tokens[0], tokens);
printf("I'm not a magician, try a command that exists.\n");
exit(1);
}
void fork_cmd() {
pid_t pid = fork();
if (pid < 0) {
printf("something went wrong :(\n");
return;
}
if (pid > 0)
waitpid(pid, NULL, 0);
}
void setuid_cmd(char **tokens) {
Cmd_setuid(tokens);
}
void getuid_cmd() {
uid_t real = getuid();
uid_t eff = geteuid();
printf("real uid: %d (%s)\n", real, NombreUsuario(real));
printf("eff. uid: %d (%s)\n", eff, NombreUsuario(eff));
}
void setpriority_cmd(char **tokens, int ntokens) {
int i_pid, prio, which = PRIO_PROCESS;
if (ntokens == 0) {
getpriority_cmd(NULL, 0);
return;
} else if (ntokens == 1) {
i_pid = getpid();
if (tokens[0][0] == '@')
prio = atoi(tokens[0] + 1);
else
prio = atoi(tokens[0]);
} else {
i_pid = atoi(tokens[0]);
prio = atoi(tokens[1]);
}
int err = setpriority(which, i_pid, prio);
if (errno == 0 && err == 0)
printf("priority changed pid=%d to prio=%d\n", i_pid, prio);
else
printf("priority not changed: %s\n", strerror(errno));
}
void getpriority_cmd(char *pid, int flag_pid) {
int i_pid, which = PRIO_PROCESS;
if (flag_pid)
i_pid = atoi(pid);
else
i_pid = getpid();
int prio = getpriority(which, i_pid);
if (prio == -1 && errno != 0) {
printf("something went wrong\n");
return;
}
printf("priority of pid=%d is=%d\n", i_pid, prio);
}