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