Setting up a service using inetd
Background
Setting up a service on a unix machine does not require a great deal of
knowledge about writing client server applications. There are only
two basic requirements:
-
root permissions on the machine where the service will be installed
-
an ability to write an application in ANY language which reads from STANDARD
INPUT and writes to STANDARD OUTPUT which can even include simple unix
shell commands
Then it becomes a simple matter of making a few entries in a few text files.
Granted, this approach is not very sophisticated and will only satisfy
the requirements of the simplest services, it can still solve the requirements
of numerous types of problems. The program you will write represents
the service, and telnet (a common application available on any internet
enabled machine) will serve as the client for your service. The use
of telnet as the client side limits significantly what you are able to
do in the client-server interaction, nonetheless, it provides a simple
text interface to your service. As long as the client only needs
to use keyboard entries for the interaction with the service (not a complicated
protocol), telnet will work fine.
Writing the service
Once you have written the service program and tested it so that it works
using the standard input and standard output devices. move on and set it
up to run on the internet.
A sample service written as a unix script is included.
Setting up the service
step 1 - choosing a port number
Select a port number on which your service will run. The /etc/services
file contains a list of the services and port numbers associated with those
services. In choosing a port number, you need to be careful not to choose
a number that conflicts with other services. In order to avoid going
into a lot of details at this point, just choose a number over 10000 which
is not already in use (listed in the file). Edit the /etc/services
file to include your service. The format should be self-explanatory
from the file but would look like this for a service named myservice
at port 10001.
myservice 10001/tcp
tcp represents the protocol the interaction will use and tcp is the
most reliable and simple one to use.
step 2 - configuring inetd for that service
An entry must be made in /etc/xinetd.conf and once again, the format follows
the other entries in the file. Most of the following entries are
a simple consequence of the previous service specifications. A few
of them require a little investigation to fully understand, but detailed
knowledge is not generally important; simply copy the entry. Replace
all bold-faced entries appropriately for your service.
service myservice
{
port = 10001
socket_type = stream
protocol = tcp
user = root
server = /sbin/myservice.sh
type = UNLISTED
wait = no
}
-
servicename is obvious
-
nobody is the name of the owner of the service as it runs. Using
root is dangerous and should be used sparingly. nobody has no permissions
or you could use your own username. The bottom line is that the service
can do anything this user is allowed to do.
-
/PathNameToServer is the full path including the program
name
-
ProgramName is the command name plus any commandline options for your service
if any exist. YEs this IS somewhat redundant in that the command
name is part of this AND the previous entry.
Example:
myservice stream tcp nowait dgame /home/dgame/service service
where the user is dgame and the program named service is in the
root directory of dgame.
Making inetd reboot
You can force inetd to read the changes you just made to inetd.conf by
-
sending it a hangup signal OR
-
rebooting the machine
Generally the former is better.
-
Use the unix ps command to find the process number of inetd.
-
send (x)inetd a HANGUP signal
E.g. if (x)inetd is running on process 231
kill -HUP 231
Testing the service
Use telnet to test the service. Go to another machine and use telnet,
but use the port number you set up in your /etc/services file. For
example, to access the service on 137.155.2.199 at port 10001
telnet 137.155.2.199 10001
Your server application should come up and interact with the telnet
client you are running.
Special Considerations
There are a few glitches associated with using shell scripts for
your service:
-
Do not use the unix more command.
- Be sure to include the !#/bin/sh ( or whatever command shell is appropriate
for you ) on the first line of a script.
- There may be some firewall issues which prevent the telnet
access to your service, if this appears to be the problem, stop at
this point and we will review the problem in class.
-
When reading input, you may need to use the following code to avoid getting
some extraneous characters from the client.
read answer
.... `echo $answer | cut -c1` ......
instead of using $answer directly