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