Home | History | Annotate | Line # | Download | only in rbootd
rmpproto.c revision 1.3
      1 /*	$NetBSD: rmpproto.c,v 1.3 1995/08/21 17:05:22 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1988, 1992 The University of Utah and the Center
      5  *	for Software Science (CSS).
      6  * Copyright (c) 1992, 1993
      7  *	The Regents of the University of California.  All rights reserved.
      8  *
      9  * This code is derived from software contributed to Berkeley by
     10  * the Center for Software Science of the University of Utah Computer
     11  * Science Department.  CSS requests users of this software to return
     12  * to css-dist (at) cs.utah.edu any improvements that they make and grant
     13  * CSS redistribution rights.
     14  *
     15  * Redistribution and use in source and binary forms, with or without
     16  * modification, are permitted provided that the following conditions
     17  * are met:
     18  * 1. Redistributions of source code must retain the above copyright
     19  *    notice, this list of conditions and the following disclaimer.
     20  * 2. Redistributions in binary form must reproduce the above copyright
     21  *    notice, this list of conditions and the following disclaimer in the
     22  *    documentation and/or other materials provided with the distribution.
     23  * 3. All advertising materials mentioning features or use of this software
     24  *    must display the following acknowledgement:
     25  *	This product includes software developed by the University of
     26  *	California, Berkeley and its contributors.
     27  * 4. Neither the name of the University nor the names of its contributors
     28  *    may be used to endorse or promote products derived from this software
     29  *    without specific prior written permission.
     30  *
     31  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     32  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     33  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     34  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     35  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     39  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     40  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     41  * SUCH DAMAGE.
     42  *
     43  *	from: @(#)rmpproto.c	8.1 (Berkeley) 6/4/93
     44  *
     45  * From: Utah Hdr: rmpproto.c 3.1 92/07/06
     46  * Author: Jeff Forys, University of Utah CSS
     47  */
     48 
     49 #ifndef lint
     50 /*static char sccsid[] = "@(#)rmpproto.c	8.1 (Berkeley) 6/4/93";*/
     51 static char rcsid[] = "$NetBSD: rmpproto.c,v 1.3 1995/08/21 17:05:22 thorpej Exp $";
     52 #endif /* not lint */
     53 
     54 #include <sys/param.h>
     55 #include <sys/time.h>
     56 
     57 #include <errno.h>
     58 #include <fcntl.h>
     59 #include <stdio.h>
     60 #include <string.h>
     61 #include <syslog.h>
     62 #include <unistd.h>
     63 #include "defs.h"
     64 
     65 /*
     66 **  ProcessPacket -- determine packet type and do what's required.
     67 **
     68 **	An RMP BOOT packet has been received.  Look at the type field
     69 **	and process Boot Requests, Read Requests, and Boot Complete
     70 **	packets.  Any other type will be dropped with a warning msg.
     71 **
     72 **	Parameters:
     73 **		rconn - the new connection
     74 **		client - list of files available to this host
     75 **
     76 **	Returns:
     77 **		Nothing.
     78 **
     79 **	Side Effects:
     80 **		- If this is a valid boot request, it will be added to
     81 **		  the linked list of outstanding requests (RmpConns).
     82 **		- If this is a valid boot complete, its associated
     83 **		  entry in RmpConns will be deleted.
     84 **		- Also, unless we run out of memory, a reply will be
     85 **		  sent to the host that sent the packet.
     86 */
     87 void
     88 ProcessPacket(rconn, client)
     89 	RMPCONN *rconn;
     90 	CLIENT *client;
     91 {
     92 	struct rmp_packet *rmp;
     93 	RMPCONN *rconnout;
     94 
     95 	rmp = &rconn->rmp;		/* cache pointer to RMP packet */
     96 
     97 	switch(rmp->r_type) {		/* do what we came here to do */
     98 		case RMP_BOOT_REQ:		/* boot request */
     99 			if ((rconnout = NewConn(rconn)) == NULL)
    100 				return;
    101 
    102 			/*
    103 			 *  If the Session ID is 0xffff, this is a "probe"
    104 			 *  packet and we do not want to add the connection
    105 			 *  to the linked list of active connections.  There
    106 			 *  are two types of probe packets, if the Sequence
    107 			 *  Number is 0 they want to know our host name, o/w
    108 			 *  they want the name of the file associated with
    109 			 *  the number spec'd by the Sequence Number.
    110 			 *
    111 			 *  If this is an actual boot request, open the file
    112 			 *  and send a reply.  If SendBootRepl() does not
    113 			 *  return 0, add the connection to the linked list
    114 			 *  of active connections, otherwise delete it since
    115 			 *  an error was encountered.
    116 			 */
    117 			if (ntohs(rmp->r_brq.rmp_session) == RMP_PROBESID) {
    118 				if (WORDZE(rmp->r_brq.rmp_seqno))
    119 					(void) SendServerID(rconnout);
    120 				else
    121 					(void) SendFileNo(rmp, rconnout,
    122 					                  client? client->files:
    123 					                          BootFiles);
    124 				FreeConn(rconnout);
    125 			} else {
    126 				if (SendBootRepl(rmp, rconnout,
    127 				    client? client->files: BootFiles))
    128 					AddConn(rconnout);
    129 				else
    130 					FreeConn(rconnout);
    131 			}
    132 			break;
    133 
    134 		case RMP_BOOT_REPL:		/* boot reply (not valid) */
    135 			syslog(LOG_WARNING, "%s: sent a boot reply",
    136 			       EnetStr(rconn));
    137 			break;
    138 
    139 		case RMP_READ_REQ:		/* read request */
    140 			/*
    141 			 *  Send a portion of the boot file.
    142 			 */
    143 			(void) SendReadRepl(rconn);
    144 			break;
    145 
    146 		case RMP_READ_REPL:		/* read reply (not valid) */
    147 			syslog(LOG_WARNING, "%s: sent a read reply",
    148 			       EnetStr(rconn));
    149 			break;
    150 
    151 		case RMP_BOOT_DONE:		/* boot complete */
    152 			/*
    153 			 *  Remove the entry from the linked list of active
    154 			 *  connections.
    155 			 */
    156 			(void) BootDone(rconn);
    157 			break;
    158 
    159 		default:			/* unknown RMP packet type */
    160 			syslog(LOG_WARNING, "%s: unknown packet type (%u)",
    161 			       EnetStr(rconn), rmp->r_type);
    162 	}
    163 }
    164 
    165 /*
    166 **  SendServerID -- send our host name to who ever requested it.
    167 **
    168 **	Parameters:
    169 **		rconn - the reply packet to be formatted.
    170 **
    171 **	Returns:
    172 **		1 on success, 0 on failure.
    173 **
    174 **	Side Effects:
    175 **		none.
    176 */
    177 int
    178 SendServerID(rconn)
    179 	RMPCONN *rconn;
    180 {
    181 	register struct rmp_packet *rpl;
    182 	register char *src, *dst;
    183 	register u_char *size;
    184 
    185 	rpl = &rconn->rmp;			/* cache ptr to RMP packet */
    186 
    187 	/*
    188 	 *  Set up assorted fields in reply packet.
    189 	 */
    190 	rpl->r_brpl.rmp_type = RMP_BOOT_REPL;
    191 	rpl->r_brpl.rmp_retcode = RMP_E_OKAY;
    192 	ZEROWORD(rpl->r_brpl.rmp_seqno);
    193 	rpl->r_brpl.rmp_session = 0;
    194 	rpl->r_brpl.rmp_version = htons(RMP_VERSION);
    195 
    196 	size = &rpl->r_brpl.rmp_flnmsize;	/* ptr to length of host name */
    197 
    198 	/*
    199 	 *  Copy our host name into the reply packet incrementing the
    200 	 *  length as we go.  Stop at RMP_HOSTLEN or the first dot.
    201 	 */
    202 	src = MyHost;
    203 	dst = (char *) &rpl->r_brpl.rmp_flnm;
    204 	for (*size = 0; *size < RMP_HOSTLEN; (*size)++) {
    205 		if (*src == '.' || *src == '\0')
    206 			break;
    207 		*dst++ = *src++;
    208 	}
    209 
    210 	rconn->rmplen = RMPBOOTSIZE(*size);	/* set packet length */
    211 
    212 	return(SendPacket(rconn));		/* send packet */
    213 }
    214 
    215 /*
    216 **  SendFileNo -- send the name of a bootable file to the requester.
    217 **
    218 **	Parameters:
    219 **		req - RMP BOOT packet containing the request.
    220 **		rconn - the reply packet to be formatted.
    221 **		filelist - list of files available to the requester.
    222 **
    223 **	Returns:
    224 **		1 on success, 0 on failure.
    225 **
    226 **	Side Effects:
    227 **		none.
    228 */
    229 int
    230 SendFileNo(req, rconn, filelist)
    231 	struct rmp_packet *req;
    232 	RMPCONN *rconn;
    233 	char *filelist[];
    234 {
    235 	register struct rmp_packet *rpl;
    236 	register char *src, *dst;
    237 	register u_char *size, i;
    238 
    239 	GETWORD(req->r_brpl.rmp_seqno, i);	/* SeqNo is really FileNo */
    240 	rpl = &rconn->rmp;			/* cache ptr to RMP packet */
    241 
    242 	/*
    243 	 *  Set up assorted fields in reply packet.
    244 	 */
    245 	rpl->r_brpl.rmp_type = RMP_BOOT_REPL;
    246 	PUTWORD(i, rpl->r_brpl.rmp_seqno);
    247 	i--;
    248 	rpl->r_brpl.rmp_session = 0;
    249 	rpl->r_brpl.rmp_version = htons(RMP_VERSION);
    250 
    251 	size = &rpl->r_brpl.rmp_flnmsize;	/* ptr to length of filename */
    252 	*size = 0;				/* init length to zero */
    253 
    254 	/*
    255 	 *  Copy the file name into the reply packet incrementing the
    256 	 *  length as we go.  Stop at end of string or when RMPBOOTDATA
    257 	 *  characters have been copied.  Also, set return code to
    258 	 *  indicate success or "no more files".
    259 	 */
    260 	if (i < C_MAXFILE && filelist[i] != NULL) {
    261 		src = filelist[i];
    262 		dst = (char *)&rpl->r_brpl.rmp_flnm;
    263 		for (; *src && *size < RMPBOOTDATA; (*size)++) {
    264 			if (*src == '\0')
    265 				break;
    266 			*dst++ = *src++;
    267 		}
    268 		rpl->r_brpl.rmp_retcode = RMP_E_OKAY;
    269 	} else
    270 		rpl->r_brpl.rmp_retcode = RMP_E_NODFLT;
    271 
    272 	rconn->rmplen = RMPBOOTSIZE(*size);	/* set packet length */
    273 
    274 	return(SendPacket(rconn));		/* send packet */
    275 }
    276 
    277 /*
    278 **  SendBootRepl -- open boot file and respond to boot request.
    279 **
    280 **	Parameters:
    281 **		req - RMP BOOT packet containing the request.
    282 **		rconn - the reply packet to be formatted.
    283 **		filelist - list of files available to the requester.
    284 **
    285 **	Returns:
    286 **		1 on success, 0 on failure.
    287 **
    288 **	Side Effects:
    289 **		none.
    290 */
    291 int
    292 SendBootRepl(req, rconn, filelist)
    293 	struct rmp_packet *req;
    294 	RMPCONN *rconn;
    295 	char *filelist[];
    296 {
    297 	int retval;
    298 	char *filename, filepath[RMPBOOTDATA+1];
    299 	RMPCONN *oldconn;
    300 	register struct rmp_packet *rpl;
    301 	register char *src, *dst1, *dst2;
    302 	register u_char i;
    303 
    304 	/*
    305 	 *  If another connection already exists, delete it since we
    306 	 *  are obviously starting again.
    307 	 */
    308 	if ((oldconn = FindConn(rconn)) != NULL) {
    309 		syslog(LOG_WARNING, "%s: dropping existing connection",
    310 		       EnetStr(oldconn));
    311 		RemoveConn(oldconn);
    312 	}
    313 
    314 	rpl = &rconn->rmp;			/* cache ptr to RMP packet */
    315 
    316 	/*
    317 	 *  Set up assorted fields in reply packet.
    318 	 */
    319 	rpl->r_brpl.rmp_type = RMP_BOOT_REPL;
    320 	COPYWORD(req->r_brq.rmp_seqno, rpl->r_brpl.rmp_seqno);
    321 	rpl->r_brpl.rmp_session = htons(GenSessID());
    322 	rpl->r_brpl.rmp_version = htons(RMP_VERSION);
    323 	rpl->r_brpl.rmp_flnmsize = req->r_brq.rmp_flnmsize;
    324 
    325 	/*
    326 	 *  Copy file name to `filepath' string, and into reply packet.
    327 	 */
    328 	src = &req->r_brq.rmp_flnm;
    329 	dst1 = filepath;
    330 	dst2 = &rpl->r_brpl.rmp_flnm;
    331 	for (i = 0; i < req->r_brq.rmp_flnmsize; i++)
    332 		*dst1++ = *dst2++ = *src++;
    333 	*dst1 = '\0';
    334 
    335 	/*
    336 	 *  If we are booting HP-UX machines, their secondary loader will
    337 	 *  ask for files like "/hp-ux".  As a security measure, we do not
    338 	 *  allow boot files to lay outside the boot directory (unless they
    339 	 *  are purposely link'd out.  So, make `filename' become the path-
    340 	 *  stripped file name and spoof the client into thinking that it
    341 	 *  really got what it wanted.
    342 	 */
    343 	filename = (filename = rindex(filepath,'/'))? ++filename: filepath;
    344 
    345 	/*
    346 	 *  Check that this is a valid boot file name.
    347 	 */
    348 	for (i = 0; i < C_MAXFILE && filelist[i] != NULL; i++)
    349 		if (STREQN(filename, filelist[i]))
    350 			goto match;
    351 
    352 	/*
    353 	 *  Invalid boot file name, set error and send reply packet.
    354 	 */
    355 	rpl->r_brpl.rmp_retcode = RMP_E_NOFILE;
    356 	retval = 0;
    357 	goto sendpkt;
    358 
    359 match:
    360 	/*
    361 	 *  This is a valid boot file.  Open the file and save the file
    362 	 *  descriptor associated with this connection and set success
    363 	 *  indication.  If the file couldnt be opened, set error:
    364 	 *  	"no such file or dir" - RMP_E_NOFILE
    365 	 *	"file table overflow" - RMP_E_BUSY
    366 	 *	"too many open files" - RMP_E_BUSY
    367 	 *	anything else         - RMP_E_OPENFILE
    368 	 */
    369 	if ((rconn->bootfd = open(filename, O_RDONLY, 0600)) < 0) {
    370 		rpl->r_brpl.rmp_retcode = (errno == ENOENT)? RMP_E_NOFILE:
    371 			(errno == EMFILE || errno == ENFILE)? RMP_E_BUSY:
    372 			RMP_E_OPENFILE;
    373 		retval = 0;
    374 	} else {
    375 		rpl->r_brpl.rmp_retcode = RMP_E_OKAY;
    376 		retval = 1;
    377 	}
    378 
    379 sendpkt:
    380 	syslog(LOG_INFO, "%s: request to boot %s (%s)",
    381 	       EnetStr(rconn), filename, retval? "granted": "denied");
    382 
    383 	rconn->rmplen = RMPBOOTSIZE(rpl->r_brpl.rmp_flnmsize);
    384 
    385 	return (retval & SendPacket(rconn));
    386 }
    387 
    388 /*
    389 **  SendReadRepl -- send a portion of the boot file to the requester.
    390 **
    391 **	Parameters:
    392 **		rconn - the reply packet to be formatted.
    393 **
    394 **	Returns:
    395 **		1 on success, 0 on failure.
    396 **
    397 **	Side Effects:
    398 **		none.
    399 */
    400 int
    401 SendReadRepl(rconn)
    402 	RMPCONN *rconn;
    403 {
    404 	int retval;
    405 	RMPCONN *oldconn;
    406 	register struct rmp_packet *rpl, *req;
    407 	register int size = 0;
    408 	int madeconn = 0;
    409 
    410 	/*
    411 	 *  Find the old connection.  If one doesnt exist, create one only
    412 	 *  to return the error code.
    413 	 */
    414 	if ((oldconn = FindConn(rconn)) == NULL) {
    415 		if ((oldconn = NewConn(rconn)) == NULL)
    416 			return(0);
    417 		syslog(LOG_ERR, "SendReadRepl: no active connection (%s)",
    418 		       EnetStr(rconn));
    419 		madeconn++;
    420 	}
    421 
    422 	req = &rconn->rmp;		/* cache ptr to request packet */
    423 	rpl = &oldconn->rmp;		/* cache ptr to reply packet */
    424 
    425 	if (madeconn) {			/* no active connection above; abort */
    426 		rpl->r_rrpl.rmp_retcode = RMP_E_ABORT;
    427 		retval = 1;
    428 		goto sendpkt;
    429 	}
    430 
    431 	/*
    432 	 *  Make sure Session ID's match.
    433 	 */
    434 	if (ntohs(req->r_rrq.rmp_session) !=
    435 	    ((rpl->r_type == RMP_BOOT_REPL)? ntohs(rpl->r_brpl.rmp_session):
    436 	                                     ntohs(rpl->r_rrpl.rmp_session))) {
    437 		syslog(LOG_ERR, "SendReadRepl: bad session id (%s)",
    438 		       EnetStr(rconn));
    439 		rpl->r_rrpl.rmp_retcode = RMP_E_BADSID;
    440 		retval = 1;
    441 		goto sendpkt;
    442 	}
    443 
    444 	/*
    445 	 *  If the requester asks for more data than we can fit,
    446 	 *  silently clamp the request size down to RMPREADDATA.
    447 	 *
    448 	 *  N.B. I do not know if this is "legal", however it seems
    449 	 *  to work.  This is necessary for bpfwrite() on machines
    450 	 *  with MCLBYTES less than 1514.
    451 	 */
    452 	if (ntohs(req->r_rrq.rmp_size) > RMPREADDATA)
    453 		req->r_rrq.rmp_size = htons(RMPREADDATA);
    454 
    455 	/*
    456 	 *  Position read head on file according to info in request packet.
    457 	 */
    458 	GETWORD(req->r_rrq.rmp_offset, size);
    459 	if (lseek(oldconn->bootfd, (off_t)size, L_SET) < 0) {
    460 		syslog(LOG_ERR, "SendReadRepl: lseek: %m (%s)",
    461 		       EnetStr(rconn));
    462 		rpl->r_rrpl.rmp_retcode = RMP_E_ABORT;
    463 		retval = 1;
    464 		goto sendpkt;
    465 	}
    466 
    467 	/*
    468 	 *  Read data directly into reply packet.
    469 	 */
    470 	if ((size = read(oldconn->bootfd, &rpl->r_rrpl.rmp_data,
    471 	                 (int) req->r_rrq.rmp_size)) <= 0) {
    472 		if (size < 0) {
    473 			syslog(LOG_ERR, "SendReadRepl: read: %m (%s)",
    474 			       EnetStr(rconn));
    475 			rpl->r_rrpl.rmp_retcode = RMP_E_ABORT;
    476 		} else {
    477 			rpl->r_rrpl.rmp_retcode = RMP_E_EOF;
    478 		}
    479 		retval = 1;
    480 		goto sendpkt;
    481 	}
    482 
    483 	/*
    484 	 *  Set success indication.
    485 	 */
    486 	rpl->r_rrpl.rmp_retcode = RMP_E_OKAY;
    487 
    488 sendpkt:
    489 	/*
    490 	 *  Set up assorted fields in reply packet.
    491 	 */
    492 	rpl->r_rrpl.rmp_type = RMP_READ_REPL;
    493 	COPYWORD(req->r_rrq.rmp_offset, rpl->r_rrpl.rmp_offset);
    494 	rpl->r_rrpl.rmp_session = req->r_rrq.rmp_session;
    495 
    496 	oldconn->rmplen = RMPREADSIZE(size);	/* set size of packet */
    497 
    498 	retval &= SendPacket(oldconn);		/* send packet */
    499 
    500 	if (madeconn)				/* clean up after ourself */
    501 		FreeConn(oldconn);
    502 
    503 	return (retval);
    504 }
    505 
    506 /*
    507 **  BootDone -- free up memory allocated for a connection.
    508 **
    509 **	Parameters:
    510 **		rconn - incoming boot complete packet.
    511 **
    512 **	Returns:
    513 **		1 on success, 0 on failure.
    514 **
    515 **	Side Effects:
    516 **		none.
    517 */
    518 int
    519 BootDone(rconn)
    520 	RMPCONN *rconn;
    521 {
    522 	RMPCONN *oldconn;
    523 	struct rmp_packet *rpl;
    524 
    525 	/*
    526 	 *  If we cant find the connection, ignore the request.
    527 	 */
    528 	if ((oldconn = FindConn(rconn)) == NULL) {
    529 		syslog(LOG_ERR, "BootDone: no existing connection (%s)",
    530 		       EnetStr(rconn));
    531 		return(0);
    532 	}
    533 
    534 	rpl = &oldconn->rmp;			/* cache ptr to RMP packet */
    535 
    536 	/*
    537 	 *  Make sure Session ID's match.
    538 	 */
    539 	if (ntohs(rconn->rmp.r_rrq.rmp_session) !=
    540 	    ((rpl->r_type == RMP_BOOT_REPL)? ntohs(rpl->r_brpl.rmp_session):
    541 	                                    ntohs(rpl->r_rrpl.rmp_session))) {
    542 		syslog(LOG_ERR, "BootDone: bad session id (%s)",
    543 		       EnetStr(rconn));
    544 		return(0);
    545 	}
    546 
    547 	RemoveConn(oldconn);			/* remove connection */
    548 
    549 	syslog(LOG_INFO, "%s: boot complete", EnetStr(rconn));
    550 
    551 	return(1);
    552 }
    553 
    554 /*
    555 **  SendPacket -- send an RMP packet to a remote host.
    556 **
    557 **	Parameters:
    558 **		rconn - packet to be sent.
    559 **
    560 **	Returns:
    561 **		1 on success, 0 on failure.
    562 **
    563 **	Side Effects:
    564 **		none.
    565 */
    566 int
    567 SendPacket(rconn)
    568 	register RMPCONN *rconn;
    569 {
    570 	/*
    571 	 *  Set Ethernet Destination address to Source (BPF and the enet
    572 	 *  driver will take care of getting our source address set).
    573 	 */
    574 	bcopy((char *)&rconn->rmp.hp_hdr.saddr[0],
    575 	      (char *)&rconn->rmp.hp_hdr.daddr[0], RMP_ADDRLEN);
    576 	rconn->rmp.hp_hdr.len = rconn->rmplen - sizeof(struct hp_hdr);
    577 
    578 	/*
    579 	 *  Reverse 802.2/HP Extended Source & Destination Access Pts.
    580 	 */
    581 	rconn->rmp.hp_llc.dxsap = HPEXT_SXSAP;
    582 	rconn->rmp.hp_llc.sxsap = HPEXT_DXSAP;
    583 
    584 	/*
    585 	 *  Last time this connection was active.
    586 	 */
    587 	(void) gettimeofday(&rconn->tstamp, (struct timezone *)0);
    588 
    589 	if (DbgFp != NULL)			/* display packet */
    590 		DispPkt(rconn,DIR_SENT);
    591 
    592 	/*
    593 	 *  Send RMP packet to remote host.
    594 	 */
    595 	return(BpfWrite(rconn));
    596 }
    597