warmstart.c revision 302408
174340Sache/*-
274340Sache * Copyright (c) 2009, Sun Microsystems, Inc.
374340Sache * All rights reserved.
474340Sache *
574340Sache * Redistribution and use in source and binary forms, with or without
674340Sache * modification, are permitted provided that the following conditions are met:
774340Sache * - Redistributions of source code must retain the above copyright notice,
874340Sache *   this list of conditions and the following disclaimer.
974340Sache * - Redistributions in binary form must reproduce the above copyright notice,
1074340Sache *   this list of conditions and the following disclaimer in the documentation
1174340Sache *   and/or other materials provided with the distribution.
1274340Sache * - Neither the name of Sun Microsystems, Inc. nor the names of its
1374340Sache *   contributors may be used to endorse or promote products derived
1474340Sache *   from this software without specific prior written permission.
1574340Sache *
1674340Sache * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
1774340Sache * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1874340Sache * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1974340Sache * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
2074340Sache * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2174340Sache * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2274340Sache * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2374340Sache * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2474340Sache * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2574340Sache * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2674340Sache * POSSIBILITY OF SUCH DAMAGE.
2774340Sache */
2874340Sache/*
2974340Sache * warmstart.c
3074340Sache * Allows for gathering of registrations from an earlier dumped file.
3174340Sache *
3274340Sache * Copyright (c) 1990 by Sun Microsystems, Inc.
3374340Sache */
3474340Sache
3574340Sache/*
3674340Sache * #ident	"@(#)warmstart.c	1.7	93/07/05 SMI"
3774340Sache * $FreeBSD: stable/11/usr.sbin/rpcbind/warmstart.c 258564 2013-11-25 16:44:02Z hrs $/
3874340Sache */
3974340Sache#include <sys/types.h>
4074340Sache#include <sys/stat.h>
4174340Sache#include <stdio.h>
4274340Sache#include <rpc/rpc.h>
4374340Sache#include <rpc/rpcb_prot.h>
4474340Sache#include <rpc/xdr.h>
4574340Sache#ifdef PORTMAP
4674340Sache#include <netinet/in.h>
4774340Sache#include <rpc/pmap_prot.h>
4874340Sache#endif
4974340Sache#include <syslog.h>
5074340Sache#include <unistd.h>
5174340Sache
5274340Sache#include "rpcbind.h"
5374340Sache
5474340Sache/*
5574340Sache * XXX this code is unsafe and is not used. It should be made safe.
5674340Sache */
5774340Sache
5874340Sache
5974340Sache/* These files keep the pmap_list and rpcb_list in XDR format */
6074340Sache#define	RPCBFILE	"/tmp/rpcbind.file"
6174340Sache#ifdef PORTMAP
6274340Sache#define	PMAPFILE	"/tmp/portmap.file"
6374340Sache#endif
6474340Sache
6574340Sachestatic bool_t write_struct(char *, xdrproc_t, void *);
6674340Sachestatic bool_t read_struct(char *, xdrproc_t, void *);
6774340Sache
6874340Sachestatic bool_t
6974340Sachewrite_struct(char *filename, xdrproc_t structproc, void *list)
7074340Sache{
7174340Sache	FILE *fp;
7274340Sache	XDR xdrs;
7374340Sache	mode_t omask;
7474340Sache
7574340Sache	omask = umask(077);
7674340Sache	fp = fopen(filename, "w");
7774340Sache	if (fp == NULL) {
7874340Sache		int i;
7974340Sache
8074340Sache		for (i = 0; i < 10; i++)
8174340Sache			close(i);
8274340Sache		fp = fopen(filename, "w");
8374340Sache		if (fp == NULL) {
8474340Sache			syslog(LOG_ERR,
8574340Sache				"cannot open file = %s for writing", filename);
8674340Sache			syslog(LOG_ERR, "cannot save any registration");
8774340Sache			return (FALSE);
8874340Sache		}
8974340Sache	}
9074340Sache	(void) umask(omask);
9174340Sache	xdrstdio_create(&xdrs, fp, XDR_ENCODE);
9274340Sache
9374340Sache	if (structproc(&xdrs, list) == FALSE) {
9474340Sache		syslog(LOG_ERR, "rpcbind: xdr_%s: failed", filename);
9574340Sache		fclose(fp);
9674340Sache		return (FALSE);
9774340Sache	}
9874340Sache	XDR_DESTROY(&xdrs);
9974340Sache	fclose(fp);
10074340Sache	return (TRUE);
10174340Sache}
10274340Sache
10374340Sachestatic bool_t
10474340Sacheread_struct(char *filename, xdrproc_t structproc, void *list)
10574340Sache{
10674340Sache	FILE *fp;
10774340Sache	XDR xdrs;
10874340Sache	struct stat sbuf;
10974340Sache
11074340Sache	if (stat(filename, &sbuf) != 0) {
11174340Sache		fprintf(stderr,
11274340Sache		"rpcbind: cannot stat file = %s for reading\n", filename);
11374340Sache		goto error;
11474340Sache	}
11574340Sache	if ((sbuf.st_uid != 0) || (sbuf.st_mode & S_IRWXG) ||
11674340Sache	    (sbuf.st_mode & S_IRWXO)) {
11774340Sache		fprintf(stderr,
11874340Sache		"rpcbind: invalid permissions on file = %s for reading\n",
11974340Sache			filename);
12074340Sache		goto error;
12174340Sache	}
12274340Sache	fp = fopen(filename, "r");
12374340Sache	if (fp == NULL) {
12474340Sache		fprintf(stderr,
125		"rpcbind: cannot open file = %s for reading\n", filename);
126		goto error;
127	}
128	xdrstdio_create(&xdrs, fp, XDR_DECODE);
129
130	if (structproc(&xdrs, list) == FALSE) {
131		fprintf(stderr, "rpcbind: xdr_%s: failed\n", filename);
132		fclose(fp);
133		goto error;
134	}
135	XDR_DESTROY(&xdrs);
136	fclose(fp);
137	return (TRUE);
138
139error:	fprintf(stderr, "rpcbind: will start from scratch\n");
140	return (FALSE);
141}
142
143void
144write_warmstart(void)
145{
146	(void) write_struct(RPCBFILE, (xdrproc_t)xdr_rpcblist_ptr, &list_rbl);
147#ifdef PORTMAP
148	(void) write_struct(PMAPFILE, (xdrproc_t)xdr_pmaplist_ptr, &list_pml);
149#endif
150
151}
152
153void
154read_warmstart(void)
155{
156	rpcblist_ptr tmp_rpcbl = NULL;
157#ifdef PORTMAP
158	struct pmaplist *tmp_pmapl = NULL;
159#endif
160	int ok1, ok2 = TRUE;
161
162	ok1 = read_struct(RPCBFILE, (xdrproc_t)xdr_rpcblist_ptr, &tmp_rpcbl);
163	if (ok1 == FALSE)
164		return;
165#ifdef PORTMAP
166	ok2 = read_struct(PMAPFILE, (xdrproc_t)xdr_pmaplist_ptr, &tmp_pmapl);
167#endif
168	if (ok2 == FALSE) {
169		xdr_free((xdrproc_t) xdr_rpcblist_ptr, (char *)&tmp_rpcbl);
170		return;
171	}
172	xdr_free((xdrproc_t) xdr_rpcblist_ptr, (char *)&list_rbl);
173	list_rbl = tmp_rpcbl;
174#ifdef PORTMAP
175	xdr_free((xdrproc_t) xdr_pmaplist_ptr, (char *)&list_pml);
176	list_pml = tmp_pmapl;
177#endif
178}
179