Home | History | Annotate | Line # | Download | only in rpcbind
warmstart.c revision 1.6
      1  1.6  christos /*	$NetBSD: warmstart.c,v 1.6 2017/08/16 08:44:40 christos Exp $	*/
      2  1.6  christos /* $FreeBSD: head/usr.sbin/rpcbind/warmstart.c 258564 2013-11-25 16:44:02Z hrs $*/
      3  1.6  christos 
      4  1.6  christos /*-
      5  1.6  christos  * Copyright (c) 2009, Sun Microsystems, Inc.
      6  1.6  christos  * All rights reserved.
      7  1.6  christos  *
      8  1.6  christos  * Redistribution and use in source and binary forms, with or without
      9  1.6  christos  * modification, are permitted provided that the following conditions are met:
     10  1.6  christos  * - Redistributions of source code must retain the above copyright notice,
     11  1.6  christos  *   this list of conditions and the following disclaimer.
     12  1.6  christos  * - Redistributions in binary form must reproduce the above copyright notice,
     13  1.6  christos  *   this list of conditions and the following disclaimer in the documentation
     14  1.6  christos  *   and/or other materials provided with the distribution.
     15  1.6  christos  * - Neither the name of Sun Microsystems, Inc. nor the names of its
     16  1.6  christos  *   contributors may be used to endorse or promote products derived
     17  1.6  christos  *   from this software without specific prior written permission.
     18  1.6  christos  *
     19  1.6  christos  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     20  1.6  christos  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  1.6  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  1.6  christos  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
     23  1.6  christos  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.6  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.6  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.6  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.6  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.6  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.6  christos  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1      fvdl  */
     31  1.1      fvdl /*
     32  1.1      fvdl  * warmstart.c
     33  1.6  christos  * Allows for gathering of registrations from an earlier dumped file.
     34  1.1      fvdl  *
     35  1.1      fvdl  * Copyright (c) 1990 by Sun Microsystems, Inc.
     36  1.1      fvdl  */
     37  1.1      fvdl 
     38  1.1      fvdl #ident	"@(#)warmstart.c	1.7	93/07/05 SMI"
     39  1.1      fvdl 
     40  1.1      fvdl #include <sys/types.h>
     41  1.1      fvdl #include <sys/stat.h>
     42  1.1      fvdl #include <stdio.h>
     43  1.2  christos #include <fcntl.h>
     44  1.2  christos #include <err.h>
     45  1.1      fvdl #include <rpc/rpc.h>
     46  1.1      fvdl #include <rpc/rpcb_prot.h>
     47  1.1      fvdl #include <rpc/xdr.h>
     48  1.1      fvdl #ifdef PORTMAP
     49  1.1      fvdl #include <netinet/in.h>
     50  1.1      fvdl #include <rpc/pmap_prot.h>
     51  1.1      fvdl #endif
     52  1.1      fvdl #include <syslog.h>
     53  1.1      fvdl #include <unistd.h>
     54  1.1      fvdl 
     55  1.1      fvdl #include "rpcbind.h"
     56  1.1      fvdl 
     57  1.1      fvdl /*
     58  1.1      fvdl  * XXX this code is unsafe and is not used. It should be made safe.
     59  1.1      fvdl  */
     60  1.3  christos #ifdef WARMSTART
     61  1.1      fvdl 
     62  1.1      fvdl 
     63  1.1      fvdl /* These files keep the pmap_list and rpcb_list in XDR format */
     64  1.6  christos #define	RPCBFILE	"/tmp/rpcbind.file"
     65  1.1      fvdl #ifdef PORTMAP
     66  1.6  christos #define	PMAPFILE	"/tmp/portmap.file"
     67  1.1      fvdl #endif
     68  1.1      fvdl 
     69  1.2  christos static bool_t write_struct(const char *, xdrproc_t, void *);
     70  1.2  christos static bool_t read_struct(const char *, xdrproc_t, void *);
     71  1.1      fvdl 
     72  1.1      fvdl static bool_t
     73  1.2  christos write_struct(const char *filename, xdrproc_t structproc, void *list)
     74  1.1      fvdl {
     75  1.1      fvdl 	FILE *fp;
     76  1.2  christos 	int fd;
     77  1.1      fvdl 	XDR xdrs;
     78  1.1      fvdl 
     79  1.3  christos 	(void)unlink(filename);
     80  1.3  christos 	fd = open(filename, O_WRONLY|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR);
     81  1.3  christos 	if (fd == -1 || (fp = fdopen(fd, "w")) == NULL) {
     82  1.2  christos 		syslog(LOG_ERR, "Cannot open `%s' (%m)", filename);
     83  1.2  christos 		syslog(LOG_ERR, "Cannot save any registration");
     84  1.2  christos 		return FALSE;
     85  1.2  christos 	}
     86  1.1      fvdl 
     87  1.1      fvdl 	xdrstdio_create(&xdrs, fp, XDR_ENCODE);
     88  1.1      fvdl 
     89  1.1      fvdl 	if (structproc(&xdrs, list) == FALSE) {
     90  1.2  christos 		syslog(LOG_ERR, "xdr_%s: failed", filename);
     91  1.2  christos 		(void)fclose(fp);
     92  1.1      fvdl 		return (FALSE);
     93  1.1      fvdl 	}
     94  1.1      fvdl 	XDR_DESTROY(&xdrs);
     95  1.2  christos 	(void)fclose(fp);
     96  1.2  christos 	return TRUE;
     97  1.1      fvdl }
     98  1.1      fvdl 
     99  1.1      fvdl static bool_t
    100  1.2  christos read_struct(const char *filename, xdrproc_t structproc, void *list)
    101  1.1      fvdl {
    102  1.1      fvdl 	FILE *fp;
    103  1.1      fvdl 	XDR xdrs;
    104  1.1      fvdl 	struct stat sbuf;
    105  1.1      fvdl 
    106  1.1      fvdl 	if (stat(filename, &sbuf) != 0) {
    107  1.2  christos 		warn("Cannot stat `%s'", filename);
    108  1.1      fvdl 		goto error;
    109  1.1      fvdl 	}
    110  1.1      fvdl 	if ((sbuf.st_uid != 0) || (sbuf.st_mode & S_IRWXG) ||
    111  1.1      fvdl 	    (sbuf.st_mode & S_IRWXO)) {
    112  1.2  christos 		warnx("Invalid permissions on `%s'", filename);
    113  1.1      fvdl 		goto error;
    114  1.1      fvdl 	}
    115  1.1      fvdl 	fp = fopen(filename, "r");
    116  1.1      fvdl 	if (fp == NULL) {
    117  1.2  christos 		warn("cannot open `%s'", filename);
    118  1.1      fvdl 		goto error;
    119  1.1      fvdl 	}
    120  1.1      fvdl 	xdrstdio_create(&xdrs, fp, XDR_DECODE);
    121  1.1      fvdl 
    122  1.1      fvdl 	if (structproc(&xdrs, list) == FALSE) {
    123  1.2  christos 		warnx("xdr_%s failed", filename);
    124  1.2  christos 		(void)fclose(fp);
    125  1.1      fvdl 		goto error;
    126  1.1      fvdl 	}
    127  1.1      fvdl 	XDR_DESTROY(&xdrs);
    128  1.2  christos 	(void)fclose(fp);
    129  1.2  christos 	return TRUE;
    130  1.1      fvdl 
    131  1.2  christos error:	warnx("Will start from scratch");
    132  1.2  christos 	return FALSE;
    133  1.1      fvdl }
    134  1.1      fvdl 
    135  1.1      fvdl void
    136  1.2  christos write_warmstart(void)
    137  1.1      fvdl {
    138  1.6  christos 	(void)write_struct(RPCBFILE, xdr_rpcblist_ptr, &list_rbl);
    139  1.1      fvdl #ifdef PORTMAP
    140  1.6  christos 	(void)write_struct(PMAPFILE, xdr_pmaplist_ptr, &list_pml);
    141  1.1      fvdl #endif
    142  1.1      fvdl 
    143  1.1      fvdl }
    144  1.1      fvdl 
    145  1.1      fvdl void
    146  1.2  christos read_warmstart(void)
    147  1.1      fvdl {
    148  1.1      fvdl 	rpcblist_ptr tmp_rpcbl = NULL;
    149  1.1      fvdl #ifdef PORTMAP
    150  1.1      fvdl 	struct pmaplist *tmp_pmapl = NULL;
    151  1.1      fvdl #endif
    152  1.1      fvdl 	int ok1, ok2 = TRUE;
    153  1.1      fvdl 
    154  1.6  christos 	ok1 = read_struct(RPCBFILE, xdr_rpcblist_ptr, &tmp_rpcbl);
    155  1.1      fvdl 	if (ok1 == FALSE)
    156  1.1      fvdl 		return;
    157  1.1      fvdl #ifdef PORTMAP
    158  1.6  christos 	ok2 = read_struct(PMAPFILE, xdr_pmaplist_ptr, &tmp_pmapl);
    159  1.1      fvdl #endif
    160  1.1      fvdl 	if (ok2 == FALSE) {
    161  1.1      fvdl 		xdr_free((xdrproc_t) xdr_rpcblist_ptr, (char *)&tmp_rpcbl);
    162  1.1      fvdl 		return;
    163  1.1      fvdl 	}
    164  1.1      fvdl 	xdr_free((xdrproc_t) xdr_rpcblist_ptr, (char *)&list_rbl);
    165  1.1      fvdl 	list_rbl = tmp_rpcbl;
    166  1.1      fvdl #ifdef PORTMAP
    167  1.1      fvdl 	xdr_free((xdrproc_t) xdr_pmaplist_ptr, (char *)&list_pml);
    168  1.1      fvdl 	list_pml = tmp_pmapl;
    169  1.1      fvdl #endif
    170  1.1      fvdl }
    171  1.3  christos #endif
    172