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