xref: /netbsd-src/usr.sbin/rpcbind/warmstart.c (revision b683e9a94afa5658a1a4f68c2135948d2c319eba)
1*b683e9a9Sginsbach /*	$NetBSD: warmstart.c,v 1.8 2017/08/17 00:08:10 ginsbach Exp $	*/
234b7ffd9Schristos /* $FreeBSD: head/usr.sbin/rpcbind/warmstart.c 258564 2013-11-25 16:44:02Z hrs $*/
334b7ffd9Schristos 
434b7ffd9Schristos /*-
534b7ffd9Schristos  * Copyright (c) 2009, Sun Microsystems, Inc.
634b7ffd9Schristos  * All rights reserved.
7d687de29Sfvdl  *
834b7ffd9Schristos  * Redistribution and use in source and binary forms, with or without
934b7ffd9Schristos  * modification, are permitted provided that the following conditions are met:
1034b7ffd9Schristos  * - Redistributions of source code must retain the above copyright notice,
1134b7ffd9Schristos  *   this list of conditions and the following disclaimer.
1234b7ffd9Schristos  * - Redistributions in binary form must reproduce the above copyright notice,
1334b7ffd9Schristos  *   this list of conditions and the following disclaimer in the documentation
1434b7ffd9Schristos  *   and/or other materials provided with the distribution.
1534b7ffd9Schristos  * - Neither the name of Sun Microsystems, Inc. nor the names of its
1634b7ffd9Schristos  *   contributors may be used to endorse or promote products derived
1734b7ffd9Schristos  *   from this software without specific prior written permission.
18d687de29Sfvdl  *
1934b7ffd9Schristos  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
2034b7ffd9Schristos  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2134b7ffd9Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2234b7ffd9Schristos  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
2334b7ffd9Schristos  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2434b7ffd9Schristos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2534b7ffd9Schristos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2634b7ffd9Schristos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2734b7ffd9Schristos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2834b7ffd9Schristos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2934b7ffd9Schristos  * POSSIBILITY OF SUCH DAMAGE.
30d687de29Sfvdl  */
31d687de29Sfvdl /*
32d687de29Sfvdl  * warmstart.c
3334b7ffd9Schristos  * Allows for gathering of registrations from an earlier dumped file.
34d687de29Sfvdl  *
35d687de29Sfvdl  * Copyright (c) 1990 by Sun Microsystems, Inc.
36d687de29Sfvdl  */
37d687de29Sfvdl 
38d687de29Sfvdl #ident	"@(#)warmstart.c	1.7	93/07/05 SMI"
39d687de29Sfvdl 
40d687de29Sfvdl #include <sys/types.h>
41d687de29Sfvdl #include <sys/stat.h>
42d687de29Sfvdl #include <stdio.h>
43de327a01Schristos #include <fcntl.h>
44de327a01Schristos #include <err.h>
455f912a84Sginsbach #include <paths.h>
46d687de29Sfvdl #include <rpc/rpc.h>
47d687de29Sfvdl #include <rpc/rpcb_prot.h>
48d687de29Sfvdl #include <rpc/xdr.h>
49d687de29Sfvdl #ifdef PORTMAP
50d687de29Sfvdl #include <netinet/in.h>
51d687de29Sfvdl #include <rpc/pmap_prot.h>
52d687de29Sfvdl #endif
53d687de29Sfvdl #include <syslog.h>
54d687de29Sfvdl #include <unistd.h>
55d687de29Sfvdl 
56d687de29Sfvdl #include "rpcbind.h"
57d687de29Sfvdl 
58d687de29Sfvdl /*
59d687de29Sfvdl  * XXX this code is unsafe and is not used. It should be made safe.
60d687de29Sfvdl  */
61019362ecSchristos #ifdef WARMSTART
62d687de29Sfvdl 
63d687de29Sfvdl 
64d687de29Sfvdl /* These files keep the pmap_list and rpcb_list in XDR format */
655f912a84Sginsbach #define	RPCBFILE	_PATH_VARRUN "rpcbind.file"
66d687de29Sfvdl #ifdef PORTMAP
675f912a84Sginsbach #define	PMAPFILE	_PATH_VARRUN "portmap.file"
68d687de29Sfvdl #endif
69d687de29Sfvdl 
70de327a01Schristos static bool_t write_struct(const char *, xdrproc_t, void *);
71de327a01Schristos static bool_t read_struct(const char *, xdrproc_t, void *);
72d687de29Sfvdl 
73d687de29Sfvdl static bool_t
write_struct(const char * filename,xdrproc_t structproc,void * list)74de327a01Schristos write_struct(const char *filename, xdrproc_t structproc, void *list)
75d687de29Sfvdl {
76d687de29Sfvdl 	FILE *fp;
77de327a01Schristos 	int fd;
78d687de29Sfvdl 	XDR xdrs;
79d687de29Sfvdl 
80019362ecSchristos 	(void)unlink(filename);
81019362ecSchristos 	fd = open(filename, O_WRONLY|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR);
82019362ecSchristos 	if (fd == -1 || (fp = fdopen(fd, "w")) == NULL) {
83de327a01Schristos 		syslog(LOG_ERR, "Cannot open `%s' (%m)", filename);
84de327a01Schristos 		syslog(LOG_ERR, "Cannot save any registration");
85de327a01Schristos 		return FALSE;
86de327a01Schristos 	}
87d687de29Sfvdl 
88d687de29Sfvdl 	xdrstdio_create(&xdrs, fp, XDR_ENCODE);
89d687de29Sfvdl 
90d687de29Sfvdl 	if (structproc(&xdrs, list) == FALSE) {
91de327a01Schristos 		syslog(LOG_ERR, "xdr_%s: failed", filename);
92de327a01Schristos 		(void)fclose(fp);
93d687de29Sfvdl 		return (FALSE);
94d687de29Sfvdl 	}
95d687de29Sfvdl 	XDR_DESTROY(&xdrs);
96de327a01Schristos 	(void)fclose(fp);
97de327a01Schristos 	return TRUE;
98d687de29Sfvdl }
99d687de29Sfvdl 
100d687de29Sfvdl static bool_t
read_struct(const char * filename,xdrproc_t structproc,void * list)101de327a01Schristos read_struct(const char *filename, xdrproc_t structproc, void *list)
102d687de29Sfvdl {
103d687de29Sfvdl 	FILE *fp;
104d687de29Sfvdl 	XDR xdrs;
105d687de29Sfvdl 	struct stat sbuf;
106d687de29Sfvdl 
107d687de29Sfvdl 	if (stat(filename, &sbuf) != 0) {
108de327a01Schristos 		warn("Cannot stat `%s'", filename);
109d687de29Sfvdl 		goto error;
110d687de29Sfvdl 	}
111d687de29Sfvdl 	if ((sbuf.st_uid != 0) || (sbuf.st_mode & S_IRWXG) ||
112d687de29Sfvdl 	    (sbuf.st_mode & S_IRWXO)) {
113de327a01Schristos 		warnx("Invalid permissions on `%s'", filename);
114d687de29Sfvdl 		goto error;
115d687de29Sfvdl 	}
116d687de29Sfvdl 	fp = fopen(filename, "r");
117d687de29Sfvdl 	if (fp == NULL) {
118de327a01Schristos 		warn("cannot open `%s'", filename);
119d687de29Sfvdl 		goto error;
120d687de29Sfvdl 	}
121d687de29Sfvdl 	xdrstdio_create(&xdrs, fp, XDR_DECODE);
122d687de29Sfvdl 
123d687de29Sfvdl 	if (structproc(&xdrs, list) == FALSE) {
124de327a01Schristos 		warnx("xdr_%s failed", filename);
125de327a01Schristos 		(void)fclose(fp);
126d687de29Sfvdl 		goto error;
127d687de29Sfvdl 	}
128d687de29Sfvdl 	XDR_DESTROY(&xdrs);
129de327a01Schristos 	(void)fclose(fp);
130de327a01Schristos 	return TRUE;
131d687de29Sfvdl 
132de327a01Schristos error:	warnx("Will start from scratch");
133de327a01Schristos 	return FALSE;
134d687de29Sfvdl }
135d687de29Sfvdl 
136d687de29Sfvdl void
write_warmstart(void)137de327a01Schristos write_warmstart(void)
138d687de29Sfvdl {
139*b683e9a9Sginsbach 	(void)write_struct(RPCBFILE, (xdrproc_t) xdr_rpcblist_ptr, &list_rbl);
140d687de29Sfvdl #ifdef PORTMAP
141*b683e9a9Sginsbach 	(void)write_struct(PMAPFILE, (xdrproc_t) xdr_pmaplist_ptr, &list_pml);
142d687de29Sfvdl #endif
143d687de29Sfvdl 
144d687de29Sfvdl }
145d687de29Sfvdl 
146d687de29Sfvdl void
read_warmstart(void)147de327a01Schristos read_warmstart(void)
148d687de29Sfvdl {
149d687de29Sfvdl 	rpcblist_ptr tmp_rpcbl = NULL;
150d687de29Sfvdl #ifdef PORTMAP
151d687de29Sfvdl 	struct pmaplist *tmp_pmapl = NULL;
152d687de29Sfvdl #endif
153d687de29Sfvdl 	int ok1, ok2 = TRUE;
154d687de29Sfvdl 
155*b683e9a9Sginsbach 	ok1 = read_struct(RPCBFILE, (xdrproc_t) xdr_rpcblist_ptr, &tmp_rpcbl);
156d687de29Sfvdl 	if (ok1 == FALSE)
157d687de29Sfvdl 		return;
158d687de29Sfvdl #ifdef PORTMAP
159*b683e9a9Sginsbach 	ok2 = read_struct(PMAPFILE, (xdrproc_t) xdr_pmaplist_ptr, &tmp_pmapl);
160d687de29Sfvdl #endif
161d687de29Sfvdl 	if (ok2 == FALSE) {
162d687de29Sfvdl 		xdr_free((xdrproc_t) xdr_rpcblist_ptr, (char *)&tmp_rpcbl);
163d687de29Sfvdl 		return;
164d687de29Sfvdl 	}
165d687de29Sfvdl 	xdr_free((xdrproc_t) xdr_rpcblist_ptr, (char *)&list_rbl);
166d687de29Sfvdl 	list_rbl = tmp_rpcbl;
167d687de29Sfvdl #ifdef PORTMAP
168d687de29Sfvdl 	xdr_free((xdrproc_t) xdr_pmaplist_ptr, (char *)&list_pml);
169d687de29Sfvdl 	list_pml = tmp_pmapl;
170d687de29Sfvdl #endif
171d687de29Sfvdl }
172019362ecSchristos #endif
173