Home | History | Annotate | Line # | Download | only in rpcbind
warmstart.c revision 1.3
      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 <rpc/rpc.h>
     44 #include <rpc/rpcb_prot.h>
     45 #include <rpc/xdr.h>
     46 #ifdef PORTMAP
     47 #include <netinet/in.h>
     48 #include <rpc/pmap_prot.h>
     49 #endif
     50 #include <syslog.h>
     51 #include <unistd.h>
     52 
     53 #include "rpcbind.h"
     54 
     55 /*
     56  * XXX this code is unsafe and is not used. It should be made safe.
     57  */
     58 #ifdef WARMSTART
     59 
     60 
     61 /* These files keep the pmap_list and rpcb_list in XDR format */
     62 #define	RPCBFILE	"/tmp/rpcbind.file"
     63 #ifdef PORTMAP
     64 #define	PMAPFILE	"/tmp/portmap.file"
     65 #endif
     66 
     67 static bool_t write_struct(const char *, xdrproc_t, void *);
     68 static bool_t read_struct(const char *, xdrproc_t, void *);
     69 
     70 static bool_t
     71 write_struct(const char *filename, xdrproc_t structproc, void *list)
     72 {
     73 	FILE *fp;
     74 	int fd;
     75 	XDR xdrs;
     76 
     77 	(void)unlink(filename);
     78 	fd = open(filename, O_WRONLY|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR);
     79 	if (fd == -1 || (fp = fdopen(fd, "w")) == NULL) {
     80 		syslog(LOG_ERR, "Cannot open `%s' (%m)", filename);
     81 		syslog(LOG_ERR, "Cannot save any registration");
     82 		return FALSE;
     83 	}
     84 
     85 	xdrstdio_create(&xdrs, fp, XDR_ENCODE);
     86 
     87 	if (structproc(&xdrs, list) == FALSE) {
     88 		syslog(LOG_ERR, "xdr_%s: failed", filename);
     89 		(void)fclose(fp);
     90 		return (FALSE);
     91 	}
     92 	XDR_DESTROY(&xdrs);
     93 	(void)fclose(fp);
     94 	return TRUE;
     95 }
     96 
     97 static bool_t
     98 read_struct(const char *filename, xdrproc_t structproc, void *list)
     99 {
    100 	FILE *fp;
    101 	XDR xdrs;
    102 	struct stat sbuf;
    103 
    104 	if (stat(filename, &sbuf) != 0) {
    105 		warn("Cannot stat `%s'", filename);
    106 		goto error;
    107 	}
    108 	if ((sbuf.st_uid != 0) || (sbuf.st_mode & S_IRWXG) ||
    109 	    (sbuf.st_mode & S_IRWXO)) {
    110 		warnx("Invalid permissions on `%s'", filename);
    111 		goto error;
    112 	}
    113 	fp = fopen(filename, "r");
    114 	if (fp == NULL) {
    115 		warn("cannot open `%s'", filename);
    116 		goto error;
    117 	}
    118 	xdrstdio_create(&xdrs, fp, XDR_DECODE);
    119 
    120 	if (structproc(&xdrs, list) == FALSE) {
    121 		warnx("xdr_%s failed", filename);
    122 		(void)fclose(fp);
    123 		goto error;
    124 	}
    125 	XDR_DESTROY(&xdrs);
    126 	(void)fclose(fp);
    127 	return TRUE;
    128 
    129 error:	warnx("Will start from scratch");
    130 	return FALSE;
    131 }
    132 
    133 void
    134 write_warmstart(void)
    135 {
    136 	(void)write_struct(RPCBFILE, xdr_rpcblist_ptr, &list_rbl);
    137 #ifdef PORTMAP
    138 	(void)write_struct(PMAPFILE, xdr_pmaplist_ptr, &list_pml);
    139 #endif
    140 
    141 }
    142 
    143 void
    144 read_warmstart(void)
    145 {
    146 	rpcblist_ptr tmp_rpcbl = NULL;
    147 #ifdef PORTMAP
    148 	struct pmaplist *tmp_pmapl = NULL;
    149 #endif
    150 	int ok1, ok2 = TRUE;
    151 
    152 	ok1 = read_struct(RPCBFILE, xdr_rpcblist_ptr, &tmp_rpcbl);
    153 	if (ok1 == FALSE)
    154 		return;
    155 #ifdef PORTMAP
    156 	ok2 = read_struct(PMAPFILE, xdr_pmaplist_ptr, &tmp_pmapl);
    157 #endif
    158 	if (ok2 == FALSE) {
    159 		xdr_free((xdrproc_t) xdr_rpcblist_ptr, (char *)&tmp_rpcbl);
    160 		return;
    161 	}
    162 	xdr_free((xdrproc_t) xdr_rpcblist_ptr, (char *)&list_rbl);
    163 	list_rbl = tmp_rpcbl;
    164 #ifdef PORTMAP
    165 	xdr_free((xdrproc_t) xdr_pmaplist_ptr, (char *)&list_pml);
    166 	list_pml = tmp_pmapl;
    167 #endif
    168 }
    169 #endif
    170