Home | History | Annotate | Line # | Download | only in rpcbind
warmstart.c revision 1.2.4.1
      1      1.1      fvdl /*
      2      1.1      fvdl  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
      3      1.1      fvdl  * unrestricted use provided that this legend is included on all tape
      4      1.1      fvdl  * media and as a part of the software program in whole or part.  Users
      5      1.1      fvdl  * may copy or modify Sun RPC without charge, but are not authorized
      6      1.1      fvdl  * to license or distribute it to anyone else except as part of a product or
      7      1.1      fvdl  * program developed by the user.
      8      1.1      fvdl  *
      9      1.1      fvdl  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
     10      1.1      fvdl  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
     11      1.1      fvdl  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
     12      1.1      fvdl  *
     13      1.1      fvdl  * Sun RPC is provided with no support and without any obligation on the
     14      1.1      fvdl  * part of Sun Microsystems, Inc. to assist in its use, correction,
     15      1.1      fvdl  * modification or enhancement.
     16      1.1      fvdl  *
     17      1.1      fvdl  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
     18      1.1      fvdl  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
     19      1.1      fvdl  * OR ANY PART THEREOF.
     20      1.1      fvdl  *
     21      1.1      fvdl  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
     22      1.1      fvdl  * or profits or other special, indirect and consequential damages, even if
     23      1.1      fvdl  * Sun has been advised of the possibility of such damages.
     24      1.1      fvdl  *
     25      1.1      fvdl  * Sun Microsystems, Inc.
     26      1.1      fvdl  * 2550 Garcia Avenue
     27      1.1      fvdl  * Mountain View, California  94043
     28      1.1      fvdl  */
     29      1.1      fvdl /*
     30      1.1      fvdl  * warmstart.c
     31      1.1      fvdl  * Allows for gathering of registrations from a earlier dumped file.
     32      1.1      fvdl  *
     33      1.1      fvdl  * Copyright (c) 1990 by Sun Microsystems, Inc.
     34      1.1      fvdl  */
     35      1.1      fvdl 
     36      1.1      fvdl #ident	"@(#)warmstart.c	1.7	93/07/05 SMI"
     37      1.1      fvdl 
     38      1.1      fvdl #include <sys/types.h>
     39      1.1      fvdl #include <sys/stat.h>
     40      1.1      fvdl #include <stdio.h>
     41      1.2  christos #include <fcntl.h>
     42      1.2  christos #include <err.h>
     43      1.1      fvdl #include <rpc/rpc.h>
     44      1.1      fvdl #include <rpc/rpcb_prot.h>
     45      1.1      fvdl #include <rpc/xdr.h>
     46      1.1      fvdl #ifdef PORTMAP
     47      1.1      fvdl #include <netinet/in.h>
     48      1.1      fvdl #include <rpc/pmap_prot.h>
     49      1.1      fvdl #endif
     50      1.1      fvdl #include <syslog.h>
     51      1.1      fvdl #include <unistd.h>
     52      1.1      fvdl 
     53      1.1      fvdl #include "rpcbind.h"
     54      1.1      fvdl 
     55      1.1      fvdl /*
     56      1.1      fvdl  * XXX this code is unsafe and is not used. It should be made safe.
     57      1.1      fvdl  */
     58  1.2.4.1      matt #ifdef WARMSTART
     59      1.1      fvdl 
     60      1.1      fvdl 
     61      1.1      fvdl /* These files keep the pmap_list and rpcb_list in XDR format */
     62      1.1      fvdl #define	RPCBFILE	"/tmp/rpcbind.file"
     63      1.1      fvdl #ifdef PORTMAP
     64      1.1      fvdl #define	PMAPFILE	"/tmp/portmap.file"
     65      1.1      fvdl #endif
     66      1.1      fvdl 
     67      1.2  christos static bool_t write_struct(const char *, xdrproc_t, void *);
     68      1.2  christos static bool_t read_struct(const char *, xdrproc_t, void *);
     69      1.1      fvdl 
     70      1.1      fvdl static bool_t
     71      1.2  christos write_struct(const char *filename, xdrproc_t structproc, void *list)
     72      1.1      fvdl {
     73      1.1      fvdl 	FILE *fp;
     74      1.2  christos 	int fd;
     75      1.1      fvdl 	XDR xdrs;
     76      1.1      fvdl 
     77  1.2.4.1      matt 	(void)unlink(filename);
     78  1.2.4.1      matt 	fd = open(filename, O_WRONLY|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR);
     79  1.2.4.1      matt 	if (fd == -1 || (fp = fdopen(fd, "w")) == NULL) {
     80      1.2  christos 		syslog(LOG_ERR, "Cannot open `%s' (%m)", filename);
     81      1.2  christos 		syslog(LOG_ERR, "Cannot save any registration");
     82      1.2  christos 		return FALSE;
     83      1.2  christos 	}
     84      1.1      fvdl 
     85      1.1      fvdl 	xdrstdio_create(&xdrs, fp, XDR_ENCODE);
     86      1.1      fvdl 
     87      1.1      fvdl 	if (structproc(&xdrs, list) == FALSE) {
     88      1.2  christos 		syslog(LOG_ERR, "xdr_%s: failed", filename);
     89      1.2  christos 		(void)fclose(fp);
     90      1.1      fvdl 		return (FALSE);
     91      1.1      fvdl 	}
     92      1.1      fvdl 	XDR_DESTROY(&xdrs);
     93      1.2  christos 	(void)fclose(fp);
     94      1.2  christos 	return TRUE;
     95      1.1      fvdl }
     96      1.1      fvdl 
     97      1.1      fvdl static bool_t
     98      1.2  christos read_struct(const char *filename, xdrproc_t structproc, void *list)
     99      1.1      fvdl {
    100      1.1      fvdl 	FILE *fp;
    101      1.1      fvdl 	XDR xdrs;
    102      1.1      fvdl 	struct stat sbuf;
    103      1.1      fvdl 
    104      1.1      fvdl 	if (stat(filename, &sbuf) != 0) {
    105      1.2  christos 		warn("Cannot stat `%s'", filename);
    106      1.1      fvdl 		goto error;
    107      1.1      fvdl 	}
    108      1.1      fvdl 	if ((sbuf.st_uid != 0) || (sbuf.st_mode & S_IRWXG) ||
    109      1.1      fvdl 	    (sbuf.st_mode & S_IRWXO)) {
    110      1.2  christos 		warnx("Invalid permissions on `%s'", filename);
    111      1.1      fvdl 		goto error;
    112      1.1      fvdl 	}
    113      1.1      fvdl 	fp = fopen(filename, "r");
    114      1.1      fvdl 	if (fp == NULL) {
    115      1.2  christos 		warn("cannot open `%s'", filename);
    116      1.1      fvdl 		goto error;
    117      1.1      fvdl 	}
    118      1.1      fvdl 	xdrstdio_create(&xdrs, fp, XDR_DECODE);
    119      1.1      fvdl 
    120      1.1      fvdl 	if (structproc(&xdrs, list) == FALSE) {
    121      1.2  christos 		warnx("xdr_%s failed", filename);
    122      1.2  christos 		(void)fclose(fp);
    123      1.1      fvdl 		goto error;
    124      1.1      fvdl 	}
    125      1.1      fvdl 	XDR_DESTROY(&xdrs);
    126      1.2  christos 	(void)fclose(fp);
    127      1.2  christos 	return TRUE;
    128      1.1      fvdl 
    129      1.2  christos error:	warnx("Will start from scratch");
    130      1.2  christos 	return FALSE;
    131      1.1      fvdl }
    132      1.1      fvdl 
    133      1.1      fvdl void
    134      1.2  christos write_warmstart(void)
    135      1.1      fvdl {
    136      1.2  christos 	(void)write_struct(RPCBFILE, xdr_rpcblist_ptr, &list_rbl);
    137      1.1      fvdl #ifdef PORTMAP
    138      1.2  christos 	(void)write_struct(PMAPFILE, xdr_pmaplist_ptr, &list_pml);
    139      1.1      fvdl #endif
    140      1.1      fvdl 
    141      1.1      fvdl }
    142      1.1      fvdl 
    143      1.1      fvdl void
    144      1.2  christos read_warmstart(void)
    145      1.1      fvdl {
    146      1.1      fvdl 	rpcblist_ptr tmp_rpcbl = NULL;
    147      1.1      fvdl #ifdef PORTMAP
    148      1.1      fvdl 	struct pmaplist *tmp_pmapl = NULL;
    149      1.1      fvdl #endif
    150      1.1      fvdl 	int ok1, ok2 = TRUE;
    151      1.1      fvdl 
    152      1.1      fvdl 	ok1 = read_struct(RPCBFILE, xdr_rpcblist_ptr, &tmp_rpcbl);
    153      1.1      fvdl 	if (ok1 == FALSE)
    154      1.1      fvdl 		return;
    155      1.1      fvdl #ifdef PORTMAP
    156      1.1      fvdl 	ok2 = read_struct(PMAPFILE, xdr_pmaplist_ptr, &tmp_pmapl);
    157      1.1      fvdl #endif
    158      1.1      fvdl 	if (ok2 == FALSE) {
    159      1.1      fvdl 		xdr_free((xdrproc_t) xdr_rpcblist_ptr, (char *)&tmp_rpcbl);
    160      1.1      fvdl 		return;
    161      1.1      fvdl 	}
    162      1.1      fvdl 	xdr_free((xdrproc_t) xdr_rpcblist_ptr, (char *)&list_rbl);
    163      1.1      fvdl 	list_rbl = tmp_rpcbl;
    164      1.1      fvdl #ifdef PORTMAP
    165      1.1      fvdl 	xdr_free((xdrproc_t) xdr_pmaplist_ptr, (char *)&list_pml);
    166      1.1      fvdl 	list_pml = tmp_pmapl;
    167      1.1      fvdl #endif
    168      1.1      fvdl }
    169  1.2.4.1      matt #endif
    170