fix for RC bug relating to lacking an update-inetd dependency
[debian/p10cfgd] / p10cfgd
1 #!/usr/bin/perl
2 'di ';
3 'ig00 ';
4 #
5 #*
6 #* Copyright (c) 1993, 1995 by Colorado Digital Eclectics (CODE)
7 #* All rights reserved.
8 #*
9 #* This code is derived from software contributed to CODE by
10 #* John Conner, WD0FHG.
11 #*
12 #* Redistribution and use in source and binary forms, with or without
13 #* modification, are permitted provided that the following conditions
14 #* are met:
15 #* 1. Redistributions of source code must retain the above copyright
16 #*    notice, this list of conditions and the following disclaimer.
17 #* 2. Redistributions in binary form must reproduce the above copyright
18 #*    notice, this list of conditions and the following disclaimer in the
19 #*    documentation and/or other materials provided with the distribution.
20 #* 3. All advertising materials mentioning features or use of this software
21 #*    must display the following acknowledgement:
22 #*      This product includes software developed by Colorado Digital Eclectics
23 #*      and its contributors.
24 #* 4. Neither the CODE name nor the names of its contributors may be used 
25 #*    to endorse or promote products derived from this software without 
26 #*    specific prior written permission.
27 #*
28 #* THIS SOFTWARE IS PROVIDED BY CODE AND CONTRIBUTORS ``AS IS'' AND
29 #* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 #* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 #* ARE DISCLAIMED.  IN NO EVENT SHALL CODE OR THE CONTRIBUTORS BE LIABLE
32 #* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 #* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 #* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 #* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 #* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 #* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #* SUCH DAMAGE.
39 #*
40 #
41
42 $FILEDIR = "/etc/p10cfgd/";
43 $LOGFILE = "/var/log/p10cfgd.log";
44  
45 $AF_INET = 2;
46 $SOCK_STREAM = 1;
47 $IPPORT_RMTCFG = 1236;
48 $sockaddr = 'S n a4 x8';
49 $fileaddr = 'S n a4';
50
51 # just incase someone wants to set on a different port
52 ($port) = @ARGV;
53 $port = $IPPORT_RMTCFG unless $port;
54
55 open(LOG,">>$LOGFILE") || die "Can't open log file: $!\n";
56
57 ($sec,$min,$hour,$mday,$mon,$year) = gmtime(time);
58 ++$mon;
59 print LOG "Starting p10cfgd: $mon/$mday/$year $hour:$min:$sec\n";
60
61 # if we were started by inetd then STDIN will be our
62 # socket and getsockname will return $myport == $port
63 $mysockaddr = getsockname(STDIN);
64 ($family, $myport, $myaddr) = unpack($fileaddr,$mysockaddr);
65 print LOG "Started by inetd  port=$myport\n";
66 if ( $myport == $port ) {
67     # we were started by inetd
68     open(NS,"+<&STDIN");
69     $addr = getpeername(NS);
70     $con = "inetd";
71     &ServiceConnection;
72     exit;       #should never get here
73 }
74
75 # we were started normally
76
77 ($name, $aliases, $proto) = getprotobyname('tcp');
78 if ($port !~ /^\d+$/) {
79     ($name, $aliases, $port) = getservbyport($port, 'tcp');
80 }
81
82 $this = pack($sockaddr, $AF_INET, $port, "\0\0\0\0");
83
84 select(NS); $| = 1; select(stdout);
85
86 socket(S, $AF_INET, $SOCK_STREAM, $proto) || die "socket: $!";
87 bind(S,$this) || die "bind: $!";
88 listen(S,5) || die "connect: $!";
89
90 select(S); $| = 1; select(stdout);
91
92 $con = 0;
93 print LOG "Listening for connection on Port = $port\n";
94 close(LOG);
95
96 for(;;) {
97     # wait here for connection
98     ($addr = accept(NS,S)) || die $!;
99
100     $con++;
101     # now process the remote's file request
102     if (($child[$con] = fork()) == 0) {
103         unless (fork) {  # child[$con]'s child
104             &ServiceConnection;
105         }
106         #first child exits quickly
107         exit;
108     }
109     wait;       #parent reaps first child quickly
110     close(NS);
111 }
112
113 sub     ServiceConnection  {
114     open(LOG,">>$LOGFILE") || die "Can't open log file: $!\n";
115     ($sec,$min,$hour,$mday,$mon,$year) = gmtime(time);
116     ++$mon;
117     print LOG "$con: $mon/$mday/$year $hour:$min:$sec\n";
118
119     ($af,$port,$inetaddr) = unpack($sockaddr,$addr);
120     @inetaddr = unpack('C4',$inetaddr);
121     print LOG "$con: accept ok connection from @inetaddr\n";
122
123     # the only thing we are expecting from the remote is a file name
124     $file = <NS>;
125     chop( $file );      # get rid of the newline
126     $file =~ s/\r$//;   # get rid of the return that may be at the end
127
128     print LOG "$con: Requested file - $file\n";
129     print NS "# Config file: $file\n";  #Send a comment line
130
131     $file = "$FILEDIR$file";
132
133     do {
134         print NS "# Unable to open requested config file.\n";
135         close( NS );
136         print LOG "$con: Could not open file $file: $!\n";
137         exit;
138         } unless open( FILE, $file );
139
140     # now copy the requested file to the socket
141     while ( <FILE> ) {
142         chop;
143         s/^$/  /;       #make sure that all lines have at least one char
144         s/^_$//;        #except for lines starting with a underscore
145         print NS "$_\n";
146         }
147
148     # now send the time (MMDDYYHHMM)  this should work for all files and
149     # set the switch time when any command file is read.  It should just
150     # add a time stamp to the info files.
151
152     ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime( time );
153     $mon++;
154
155     printf (NS "time %2.2d%2.2.2d%2.2d%2.2d%2.2d \n",$mon,$mday,$year,$hour,$min);
156
157     close(FILE);
158     close(NS);
159     close(LOG);
160     exit;
161 }  # end-ServiceConnection
162
163
164 ###############################################################
165
166     # These next few lines are legal in both Perl and nroff.
167
168 .00;                       # finish .ig
169  
170 'di           \" finish diversion--previous line must be blank
171 .nr nl 0-1    \" fake up transition to first page again
172 .nr % 0         \" start at page 1
173 '; __END__ ##### From here on it's a standard manual page #####
174
175 .TH P10CFGD 1 "July 29, 1993"
176 .AT 3
177 .SH NAME
178 p10cfgd \- remote configuration daemom for Gracilis PackeTen
179 .SH SYNOPSIS
180 .B p10cfgd [IP_PORT] 
181 .SH COPYRIGHT
182 Copyright (c) 1993 by Colorado Digital Eclectics (CODE)
183
184 All rights reserved.
185
186 .SH DESCRIPTION
187 .I Rmtcfgd
188 is a server program used to send configuration
189 files to a Gracilis PackeTen.  It can be run in 
190 background or it can be run by inetd.
191 If 
192 .I p10cfgd
193 is to be run by inetd, add the following line to 
194 .I /etc/services
195
196 .nf
197 rmtcfg  1236/tcp        # Remote Configuration of PackeTen
198 .fi
199
200 and add the following to
201 .I /etc/inetd.conf
202
203 .nf
204 # Remote configuration service for PackeTen
205 rmtcfg  stream  tcp  nowait  root  /usr/bin/p10cfgd  p10cfgd
206 .fi
207
208 .I P10cfgd 
209 normally does not require the IP_PORT parameter. It
210 listens for connections on IP Port 1236 as a default. 
211
212 The program will send on a line by line basis the file requested
213 by the client that connects on the IP Port.  The only expected
214 input from the client is the file name.  When the file has been
215 sent, the connection is closed. The requested file must be in 
216 the directory /etc/p10cfgd.
217
218 .I Rmtcfgd 
219 maintains a log of its activity in the file
220 /var/log/p10cfgd.log.  Program startup and all connections are 
221 logged in the file with GMT time stamps.  The file is opened 
222 in append mode.
223
224 The protocol used by the PackeTen is very simple.
225 The PackeTen opens a tcp connection to port 1236 on 
226 a host system. Once the connection is made, the PackeTen
227 sends the name of the file it wants to the host.  The 
228 host then opens the file and sends it line by line to 
229 the PackeTen until all lines have been sent.  The files
230 to be transfered must be plain text files and each line
231 must have at least one character in addition to the newline
232 at the end. 
233 .I P10cfgd 
234 replaces all blank lines with lines that contain two spaces.
235 If you wish to send a blank line, to stop loading of part
236 of a LocalInfo file for example, place an underscore '_' at 
237 the begining of the line. The entire contents of the line will
238 be deleted and a blank line will be sent to the client.
239 At the end of the file,
240 the server sends a time command 
241 ( time MMDDYYHHMM ) with the UTC time on the server. 
242 This will either set the time on the PackeTen or add a time stamp
243 to the LocalInfo file.
244 The server then closes the connection.
245
246 The remote configuration command used on the PackeTen is
247 .nf
248         rmtcfg <server_host> <filename> [options]
249         The valid options are
250                 -t  <timeout in seconds>  default is 60 sec
251                 -p  <IP port #>           default is 1236
252                 -i                        the file is for LocalInfo 
253                 -h  <secondary server>    if the primary does not answer
254 .fi
255
256 The rmtcfg  commands would be placed in to the EEPROM on the 
257 PackeTen so that they would be executed during a reboot.  The
258 information stored in the EEPROM should include enough information
259 to set the PackeTen up to allow communications with the primary and 
260 the secondary server.
261 This would include commands to set MYCALL and the IP_ADDRESS, to attach an
262 interface to the channel with the server, routing information and
263 so forth.  A sample EEPROM configuration for a PackeTen, 
264 empire.wd0fhg.ampr.org [44.32.0.32], 
265 that gets its configuration from a remote system 
266 winfree.n3eua.ampr.org [44.32.0.2],
267 over a 9600 baud ax25 path would be:
268
269 .nf
270         # first the general setup of the PackeTen
271         ee hostname gw.wd0fhg
272         ee sitename empire
273         ee ipaddr  44.32.0.32
274         ee mycall WD0FHG
275         # Then we attach a 9600 ax25 interface as ax1
276         ee attach sync302 1 hdx ax25 ax1 8192 256 9600 ext ext nrzi
277         # and set the default route to send every thing out ax1
278         ee route add default ax1 44.32.0.1
279         ee password sure-thing
280         #
281         # Now we will ask for the remainder of the setup
282         # from the server winfree.n3eua.ampr.org [44.32.0.2]
283         # Note that we have a secondary server available at 
284         # [44.20.0.195] that would be used if winfree is down.
285         # first the Local Information file
286         ee com 0 rmtcfg 44.32.0.2 empire.info -i -h 44.20.0.195
287         # and then the remaining configuration command file
288         ee com 1 rmtcfg 44.32.0.2 empire.cfg -h 44.20.0.195
289         #
290         ee save
291         #       end-of-eeprom setup
292 .fi
293
294 .I Rmtcfgd 
295 started life as the simple server program in PERL by
296 Larry Wall and R.L. Schwartz published by ORA,Inc.
297
298 .SH ENVIRONMENT
299 No environment variables are used.
300 .SH FILES
301 .nf
302 /etc/p10cfgd/*          - files that can be accessed remotely
303 /var/log/p10cfgd.log    - p10cfgd log file
304 .fi
305 .SH AUTHOR
306 .nf
307 John Conner, WD0FHG,  conner@empire.com
308 hacked on by Bdale Garbee, N3EUA, bdale@gag.com.
309 .fi
310 .SH "SEE ALSO"
311 Gracilis, Inc. PackeTen Manual and NOS User's Manual
312      
313 .SH DIAGNOSTICS
314 Complains if it can't open a file to remote client and to log file.