summaryrefslogtreecommitdiff
path: root/server.cgi
blob: ca7c2bb26aac0bdade963f086bb3d63f7fbb126c (plain)
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
#!/bin/bash

incomingData=''
startPortRange=9000 #Ports to be released for ssh communications
endPortRange=9010
firstAvailablePort=''

# --------------------------------------------------------------------------

function readRequest() {
# To avoid internal server error (500), minimum text to echo
echo ''
# Read the number of bytes(mentioned in CONTENT_LENGTH header) and
# store the content in incomingData variable
read -n $CONTENT_LENGTH incomingData
# To view the content of the incoming data, try a test URL with POST using curl
# curl --data "requestSshCredentials" http://localhost/server
# This will send the data to server(to this cgi script) and based on availablity
# of the ports it will allocate ssh credentials
}

# --------------------------------------------------------------------------

function checkRequest() {
if [ "$incomingData" == "requestSshCredentials" ];
    then
	firstAvailablePort=$(nmap -p $startPortRange-$endPortRange localhost \
			    | grep -m 1 closed \
			    | cut -d '/' -f 1)
	# Exit if no ports available
	if [ $(echo -n $firstAvailablePort | wc -m) == "0" ];
	    then
		echo "Sorry, NO ports available, please try again!"
		exit 0
	fi
fi
}

# -------------------------------------------------------------------------

function usernamePasswdforSSH() {
	randomUser=$(date | md5sum | cut -c-5)
	randomPasswd=$(date | md5sum | cut -c6-16) #just in case :)
	encryptedPasswd=$(openssl passwd $randomPasswd)
	sudo useradd -p $encryptedPasswd -r -s /bin/false $randomUser
	echo $firstAvailablePort,$randomPasswd,$randomUser
}

# -------------------------------------------------------------------------

readRequest
checkRequest
usernamePasswdforSSH