-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp_proc.c
107 lines (100 loc) · 2.35 KB
/
tcp_proc.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <poll.h>
#include <fcntl.h>
#define BUFFSIZE 1024
int main(int argc, char **argv)
{
int csock = atoi(argv[2]);
int uid = atoi(argv[3]);
struct pollfd pollset[2];
int numpoll = 2;
int len, err;
char buf[BUFFSIZE];
printf("Process létrehozva a %d. számú socketre\n", csock);
pollset[0].fd = csock;
pollset[0].events = POLLIN;
pollset[1].fd = STDIN_FILENO;
pollset[1].events = POLLIN;
if(chroot("./htdocs") != 0)
{
fprintf(stderr, "Error in chroot().\n");
return 1;
}
if(setuid(uid) != 0)
{
fprintf(stderr, "Error in setuid()\n");
return 1;
}
printf("%d\n", getuid());
while(1)
{
if( poll(pollset, numpoll, -1) < 0 )
{
perror("poll");
return -1;
}
/* hiba a socket-tel */
if(pollset[0].revents & (POLLERR|POLLNVAL))
{
printf("HIBA A SOCKET-TEL\n");
/* lezárjuk a szerver socketet */
close(csock);
return -1;
}
/* adat jött a socketen */
if(pollset[0].revents & (POLLIN))
{
len = read(csock, buf, BUFFSIZE);
buf[len] = 0;
if(len < 0)
{
printf("Socket olvasási hiba\n");
/* lezárjuk a szerver socketet */
close(csock);
return -1;
}
if(len == 0)
{
printf("Socket lezárult\n");
/* lezárjuk a szerver socketet */
close(csock);
return 0;
}
if(len > 0)
{
printf("%s\n", buf);
}
}
/* adat a standard bemeneten */
if(pollset[1].revents & (POLLIN))
{
len = read(STDIN_FILENO, buf, BUFFSIZE);
buf[len] = 0;
if(len < 0)
{
printf("Standard bemeneti hiba\n");
/* lezárjuk a kliens socketet */
close(csock);
return 1;
}
if(len>0)
{
err = write(csock, buf, len);
if(err < 0)
{
printf("Hiba socket írásakor\n");
/* lezárjuk a szerver socketet */
close(csock);
return 1;
}
}
}
}
return 0;
}