warmstart.c revision 224001
151974Smsmith/*
265245Smsmith * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
365245Smsmith * unrestricted use provided that this legend is included on all tape
451974Smsmith * media and as a part of the software program in whole or part.  Users
551974Smsmith * may copy or modify Sun RPC without charge, but are not authorized
651974Smsmith * to license or distribute it to anyone else except as part of a product or
751974Smsmith * program developed by the user.
851974Smsmith *
951974Smsmith * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
1051974Smsmith * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
1151974Smsmith * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
1251974Smsmith *
1351974Smsmith * Sun RPC is provided with no support and without any obligation on the
1451974Smsmith * part of Sun Microsystems, Inc. to assist in its use, correction,
1551974Smsmith * modification or enhancement.
1651974Smsmith *
1751974Smsmith * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
1851974Smsmith * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
1951974Smsmith * OR ANY PART THEREOF.
2051974Smsmith *
2151974Smsmith * In no event will Sun Microsystems, Inc. be liable for any lost revenue
2251974Smsmith * or profits or other special, indirect and consequential damages, even if
2351974Smsmith * Sun has been advised of the possibility of such damages.
2451974Smsmith *
2551974Smsmith * Sun Microsystems, Inc.
2651974Smsmith * 2550 Garcia Avenue
27106225Semoore * Mountain View, California  94043
28106225Semoore */
29106225Semoore/*
30106225Semoore * warmstart.c
31106225Semoore * Allows for gathering of registrations from an earlier dumped file.
32106225Semoore *
33106225Semoore * Copyright (c) 1990 by Sun Microsystems, Inc.
34106225Semoore */
35106225Semoore
36106225Semoore/*
37106225Semoore * #ident	"@(#)warmstart.c	1.7	93/07/05 SMI"
38106225Semoore * $FreeBSD: head/usr.sbin/rpcbind/warmstart.c 224001 2011-07-14 07:28:49Z delphij $/
39105419Semoore */
40106225Semoore#include <sys/types.h>
41105419Semoore#include <sys/stat.h>
42105419Semoore#include <stdio.h>
43106225Semoore#include <rpc/rpc.h>
44106225Semoore#include <rpc/rpcb_prot.h>
45106225Semoore#include <rpc/xdr.h>
46106225Semoore#ifdef PORTMAP
47106225Semoore#include <netinet/in.h>
48106225Semoore#include <rpc/pmap_prot.h>
49106225Semoore#endif
50106225Semoore#include <syslog.h>
51106225Semoore#include <unistd.h>
52106225Semoore
53106225Semoore#include "rpcbind.h"
54105419Semoore
55106225Semoore/*
5651974Smsmith * XXX this code is unsafe and is not used. It should be made safe.
5751974Smsmith */
5851974Smsmith
5951974Smsmith
6065245Smsmith/* These files keep the pmap_list and rpcb_list in XDR format */
6151974Smsmith#define	RPCBFILE	"/tmp/rpcbind.file"
6251974Smsmith#ifdef PORTMAP
6351974Smsmith#define	PMAPFILE	"/tmp/portmap.file"
6451974Smsmith#endif
6551974Smsmith
6651974Smsmithstatic bool_t write_struct(char *, xdrproc_t, void *);
6751974Smsmithstatic bool_t read_struct(char *, xdrproc_t, void *);
6865245Smsmith
6951974Smsmithstatic bool_t
7051974Smsmithwrite_struct(char *filename, xdrproc_t structproc, void *list)
7165245Smsmith{
7251974Smsmith	FILE *fp;
7365245Smsmith	XDR xdrs;
7465245Smsmith	mode_t omask;
7565245Smsmith
7651974Smsmith	omask = umask(077);
7751974Smsmith	fp = fopen(filename, "w");
7851974Smsmith	if (fp == NULL) {
79119277Simp		int i;
80119277Simp
8165245Smsmith		for (i = 0; i < 10; i++)
8251974Smsmith			close(i);
8351974Smsmith		fp = fopen(filename, "w");
8451974Smsmith		if (fp == NULL) {
8565245Smsmith			syslog(LOG_ERR,
8665245Smsmith				"cannot open file = %s for writing", filename);
8751974Smsmith			syslog(LOG_ERR, "cannot save any registration");
8851974Smsmith			return (FALSE);
8951974Smsmith		}
9065245Smsmith	}
9165245Smsmith	(void) umask(omask);
9265245Smsmith	xdrstdio_create(&xdrs, fp, XDR_ENCODE);
9365245Smsmith
9451974Smsmith	if (structproc(&xdrs, list) == FALSE) {
95111815Sphk		syslog(LOG_ERR, "rpcbind: xdr_%s: failed", filename);
96111815Sphk		fclose(fp);
97111815Sphk		return (FALSE);
98111815Sphk	}
99111815Sphk	XDR_DESTROY(&xdrs);
10051974Smsmith	fclose(fp);
10151974Smsmith	return (TRUE);
10265245Smsmith}
10365245Smsmith
10465245Smsmithstatic bool_t
10565245Smsmithread_struct(char *filename, xdrproc_t structproc, void *list)
10651974Smsmith{
10751974Smsmith	FILE *fp;
10851974Smsmith	XDR xdrs;
10951974Smsmith	struct stat sbuf;
11065245Smsmith
11165245Smsmith	if (stat(filename, &sbuf) != 0) {
11265245Smsmith		fprintf(stderr,
11365245Smsmith		"rpcbind: cannot stat file = %s for reading\n", filename);
114106225Semoore		goto error;
11551974Smsmith	}
11651974Smsmith	if ((sbuf.st_uid != 0) || (sbuf.st_mode & S_IRWXG) ||
11765245Smsmith	    (sbuf.st_mode & S_IRWXO)) {
11851974Smsmith		fprintf(stderr,
11965245Smsmith		"rpcbind: invalid permissions on file = %s for reading\n",
12065245Smsmith			filename);
12151974Smsmith		goto error;
12251974Smsmith	}
12365245Smsmith	fp = fopen(filename, "r");
12451974Smsmith	if (fp == NULL) {
12565245Smsmith		fprintf(stderr,
12665245Smsmith		"rpcbind: cannot open file = %s for reading\n", filename);
12765245Smsmith		goto error;
12865245Smsmith	}
12965245Smsmith	xdrstdio_create(&xdrs, fp, XDR_DECODE);
13065245Smsmith
13165245Smsmith	if (structproc(&xdrs, list) == FALSE) {
13251974Smsmith		fprintf(stderr, "rpcbind: xdr_%s: failed\n", filename);
13351974Smsmith		fclose(fp);
13458883Smsmith		goto error;
13558883Smsmith	}
13665245Smsmith	XDR_DESTROY(&xdrs);
13758883Smsmith	fclose(fp);
13858883Smsmith	return (TRUE);
13951974Smsmith
14051974Smsmitherror:	fprintf(stderr, "rpcbind: will start from scratch\n");
14165245Smsmith	return (FALSE);
14265245Smsmith}
143107756Semoore
14451974Smsmithvoid
14565245Smsmithwrite_warmstart(void)
14665245Smsmith{
147107756Semoore	(void) write_struct(RPCBFILE, (xdrproc_t)xdr_rpcblist_ptr, &list_rbl);
14865245Smsmith#ifdef PORTMAP
14951974Smsmith	(void) write_struct(PMAPFILE, (xdrproc_t)xdr_pmaplist_ptr, &list_pml);
15065245Smsmith#endif
15165245Smsmith
15265245Smsmith}
15365245Smsmith
15465245Smsmithvoid
15551974Smsmithread_warmstart(void)
15651974Smsmith{
15751974Smsmith	rpcblist_ptr tmp_rpcbl = NULL;
15865245Smsmith#ifdef PORTMAP
15965245Smsmith	struct pmaplist *tmp_pmapl = NULL;
160107756Semoore#endif
16165245Smsmith	int ok1, ok2 = TRUE;
16265245Smsmith
163107756Semoore	ok1 = read_struct(RPCBFILE, (xdrproc_t)xdr_rpcblist_ptr, &tmp_rpcbl);
16451974Smsmith	if (ok1 == FALSE)
16551974Smsmith		return;
16651974Smsmith#ifdef PORTMAP
16765245Smsmith	ok2 = read_struct(PMAPFILE, (xdrproc_t)xdr_pmaplist_ptr, &tmp_pmapl);
16851974Smsmith#endif
16951974Smsmith	if (ok2 == FALSE) {
17051974Smsmith		xdr_free((xdrproc_t) xdr_rpcblist_ptr, (char *)&tmp_rpcbl);
17151974Smsmith		return;
17265245Smsmith	}
17365245Smsmith	xdr_free((xdrproc_t) xdr_rpcblist_ptr, (char *)&list_rbl);
17465245Smsmith	list_rbl = tmp_rpcbl;
17565245Smsmith#ifdef PORTMAP
17651974Smsmith	xdr_free((xdrproc_t) xdr_pmaplist_ptr, (char *)&list_pml);
17751974Smsmith	list_pml = tmp_pmapl;
17851974Smsmith#endif
17951974Smsmith}
18051974Smsmith