xref: /dflybsd-src/usr.sbin/rpcbind/warmstart.c (revision bd91f5c31d05935d34ffa5befc63a95350bbf000)
1ce0e08e2SPeter Avalos /*
2*bd91f5c3SJustin C. Sherrill  * Copyright (c) 2009, Sun Microsystems, Inc.
3*bd91f5c3SJustin C. Sherrill  * All rights reserved.
4ce0e08e2SPeter Avalos  *
5*bd91f5c3SJustin C. Sherrill  * Redistribution and use in source and binary forms, with or without
6*bd91f5c3SJustin C. Sherrill  * modification, are permitted provided that the following conditions are met:
7*bd91f5c3SJustin C. Sherrill  * - Redistributions of source code must retain the above copyright notice,
8*bd91f5c3SJustin C. Sherrill  *   this list of conditions and the following disclaimer.
9*bd91f5c3SJustin C. Sherrill  * - Redistributions in binary form must reproduce the above copyright notice,
10*bd91f5c3SJustin C. Sherrill  *   this list of conditions and the following disclaimer in the documentation
11*bd91f5c3SJustin C. Sherrill  *   and/or other materials provided with the distribution.
12*bd91f5c3SJustin C. Sherrill  * - Neither the name of Sun Microsystems, Inc. nor the names of its
13*bd91f5c3SJustin C. Sherrill  *   contributors may be used to endorse or promote products derived
14*bd91f5c3SJustin C. Sherrill  *   from this software without specific prior written permission.
15ce0e08e2SPeter Avalos  *
16*bd91f5c3SJustin C. Sherrill  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17*bd91f5c3SJustin C. Sherrill  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*bd91f5c3SJustin C. Sherrill  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*bd91f5c3SJustin C. Sherrill  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20*bd91f5c3SJustin C. Sherrill  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*bd91f5c3SJustin C. Sherrill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*bd91f5c3SJustin C. Sherrill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*bd91f5c3SJustin C. Sherrill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*bd91f5c3SJustin C. Sherrill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*bd91f5c3SJustin C. Sherrill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*bd91f5c3SJustin C. Sherrill  * POSSIBILITY OF SUCH DAMAGE.
27ce0e08e2SPeter Avalos  */
28ce0e08e2SPeter Avalos /*
29ce0e08e2SPeter Avalos  * warmstart.c
30ce0e08e2SPeter Avalos  * Allows for gathering of registrations from an earlier dumped file.
31ce0e08e2SPeter Avalos  *
32ce0e08e2SPeter Avalos  * Copyright (c) 1990 by Sun Microsystems, Inc.
33ce0e08e2SPeter Avalos  */
34ce0e08e2SPeter Avalos 
35ce0e08e2SPeter Avalos /*
36ce0e08e2SPeter Avalos  * @(#)warmstart.c	1.7	93/07/05 SMI
37ce0e08e2SPeter Avalos  * $FreeBSD: src/usr.sbin/rpcbind/warmstart.c,v 1.4 2007/11/07 10:53:39 kevlo Exp $
38ce0e08e2SPeter Avalos  */
39ce0e08e2SPeter Avalos #include <sys/types.h>
40ce0e08e2SPeter Avalos #include <sys/stat.h>
41ce0e08e2SPeter Avalos #include <stdio.h>
42ce0e08e2SPeter Avalos #include <rpc/rpc.h>
43ce0e08e2SPeter Avalos #include <rpc/rpcb_prot.h>
44ce0e08e2SPeter Avalos #include <rpc/xdr.h>
45ce0e08e2SPeter Avalos #ifdef PORTMAP
46ce0e08e2SPeter Avalos #include <netinet/in.h>
47ce0e08e2SPeter Avalos #include <rpc/pmap_prot.h>
48ce0e08e2SPeter Avalos #endif
49ce0e08e2SPeter Avalos #include <syslog.h>
50ce0e08e2SPeter Avalos #include <unistd.h>
51ce0e08e2SPeter Avalos 
52ce0e08e2SPeter Avalos #include "rpcbind.h"
53ce0e08e2SPeter Avalos 
54ce0e08e2SPeter Avalos /*
55ce0e08e2SPeter Avalos  * XXX this code is unsafe and is not used. It should be made safe.
56ce0e08e2SPeter Avalos  */
57ce0e08e2SPeter Avalos 
58ce0e08e2SPeter Avalos 
59ce0e08e2SPeter Avalos /* These files keep the pmap_list and rpcb_list in XDR format */
60ce0e08e2SPeter Avalos #define	RPCBFILE	"/tmp/rpcbind.file"
61ce0e08e2SPeter Avalos #ifdef PORTMAP
62ce0e08e2SPeter Avalos #define	PMAPFILE	"/tmp/portmap.file"
63ce0e08e2SPeter Avalos #endif
64ce0e08e2SPeter Avalos 
65ce0e08e2SPeter Avalos static bool_t write_struct(char *, xdrproc_t, void *);
66ce0e08e2SPeter Avalos static bool_t read_struct(char *, xdrproc_t, void *);
67ce0e08e2SPeter Avalos 
68ce0e08e2SPeter Avalos static bool_t
write_struct(char * filename,xdrproc_t structproc,void * list)69ce0e08e2SPeter Avalos write_struct(char *filename, xdrproc_t structproc, void *list)
70ce0e08e2SPeter Avalos {
71ce0e08e2SPeter Avalos 	FILE *fp;
72ce0e08e2SPeter Avalos 	XDR xdrs;
73ce0e08e2SPeter Avalos 	mode_t omask;
74ce0e08e2SPeter Avalos 
75ce0e08e2SPeter Avalos 	omask = umask(077);
76ce0e08e2SPeter Avalos 	fp = fopen(filename, "w");
77ce0e08e2SPeter Avalos 	if (fp == NULL) {
78ce0e08e2SPeter Avalos 		int i;
79ce0e08e2SPeter Avalos 
80ce0e08e2SPeter Avalos 		for (i = 0; i < 10; i++)
81ce0e08e2SPeter Avalos 			close(i);
82ce0e08e2SPeter Avalos 		fp = fopen(filename, "w");
83ce0e08e2SPeter Avalos 		if (fp == NULL) {
84ce0e08e2SPeter Avalos 			syslog(LOG_ERR,
85ce0e08e2SPeter Avalos 				"cannot open file = %s for writing", filename);
86ce0e08e2SPeter Avalos 			syslog(LOG_ERR, "cannot save any registration");
87ce0e08e2SPeter Avalos 			return (FALSE);
88ce0e08e2SPeter Avalos 		}
89ce0e08e2SPeter Avalos 	}
90ce0e08e2SPeter Avalos 	umask(omask);
91ce0e08e2SPeter Avalos 	xdrstdio_create(&xdrs, fp, XDR_ENCODE);
92ce0e08e2SPeter Avalos 
93ce0e08e2SPeter Avalos 	if (structproc(&xdrs, list) == FALSE) {
94ce0e08e2SPeter Avalos 		syslog(LOG_ERR, "rpcbind: xdr_%s: failed", filename);
95ce0e08e2SPeter Avalos 		fclose(fp);
96ce0e08e2SPeter Avalos 		return (FALSE);
97ce0e08e2SPeter Avalos 	}
98ce0e08e2SPeter Avalos 	XDR_DESTROY(&xdrs);
99ce0e08e2SPeter Avalos 	fclose(fp);
100ce0e08e2SPeter Avalos 	return (TRUE);
101ce0e08e2SPeter Avalos }
102ce0e08e2SPeter Avalos 
103ce0e08e2SPeter Avalos static bool_t
read_struct(char * filename,xdrproc_t structproc,void * list)104ce0e08e2SPeter Avalos read_struct(char *filename, xdrproc_t structproc, void *list)
105ce0e08e2SPeter Avalos {
106ce0e08e2SPeter Avalos 	FILE *fp;
107ce0e08e2SPeter Avalos 	XDR xdrs;
108ce0e08e2SPeter Avalos 	struct stat sbuf;
109ce0e08e2SPeter Avalos 
110ce0e08e2SPeter Avalos 	if (stat(filename, &sbuf) != 0) {
111ce0e08e2SPeter Avalos 		fprintf(stderr,
112ce0e08e2SPeter Avalos 		"rpcbind: cannot stat file = %s for reading\n", filename);
113ce0e08e2SPeter Avalos 		goto error;
114ce0e08e2SPeter Avalos 	}
115ce0e08e2SPeter Avalos 	if ((sbuf.st_uid != 0) || (sbuf.st_mode & S_IRWXG) ||
116ce0e08e2SPeter Avalos 	    (sbuf.st_mode & S_IRWXO)) {
117ce0e08e2SPeter Avalos 		fprintf(stderr,
118ce0e08e2SPeter Avalos 		"rpcbind: invalid permissions on file = %s for reading\n",
119ce0e08e2SPeter Avalos 			filename);
120ce0e08e2SPeter Avalos 		goto error;
121ce0e08e2SPeter Avalos 	}
122ce0e08e2SPeter Avalos 	fp = fopen(filename, "r");
123ce0e08e2SPeter Avalos 	if (fp == NULL) {
124ce0e08e2SPeter Avalos 		fprintf(stderr,
125ce0e08e2SPeter Avalos 		"rpcbind: cannot open file = %s for reading\n", filename);
126ce0e08e2SPeter Avalos 		goto error;
127ce0e08e2SPeter Avalos 	}
128ce0e08e2SPeter Avalos 	xdrstdio_create(&xdrs, fp, XDR_DECODE);
129ce0e08e2SPeter Avalos 
130ce0e08e2SPeter Avalos 	if (structproc(&xdrs, list) == FALSE) {
131ce0e08e2SPeter Avalos 		fprintf(stderr, "rpcbind: xdr_%s: failed\n", filename);
132ce0e08e2SPeter Avalos 		fclose(fp);
133ce0e08e2SPeter Avalos 		goto error;
134ce0e08e2SPeter Avalos 	}
135ce0e08e2SPeter Avalos 	XDR_DESTROY(&xdrs);
136ce0e08e2SPeter Avalos 	fclose(fp);
137ce0e08e2SPeter Avalos 	return (TRUE);
138ce0e08e2SPeter Avalos 
139ce0e08e2SPeter Avalos error:	fprintf(stderr, "rpcbind: will start from scratch\n");
140ce0e08e2SPeter Avalos 	return (FALSE);
141ce0e08e2SPeter Avalos }
142ce0e08e2SPeter Avalos 
143ce0e08e2SPeter Avalos void
write_warmstart(void)144ce0e08e2SPeter Avalos write_warmstart(void)
145ce0e08e2SPeter Avalos {
146ce0e08e2SPeter Avalos 	write_struct(RPCBFILE, (xdrproc_t)xdr_rpcblist_ptr, &list_rbl);
147ce0e08e2SPeter Avalos #ifdef PORTMAP
148ce0e08e2SPeter Avalos 	write_struct(PMAPFILE, (xdrproc_t)xdr_pmaplist_ptr, &list_pml);
149ce0e08e2SPeter Avalos #endif
150ce0e08e2SPeter Avalos 
151ce0e08e2SPeter Avalos }
152ce0e08e2SPeter Avalos 
153ce0e08e2SPeter Avalos void
read_warmstart(void)154ce0e08e2SPeter Avalos read_warmstart(void)
155ce0e08e2SPeter Avalos {
156ce0e08e2SPeter Avalos 	rpcblist_ptr tmp_rpcbl = NULL;
157ce0e08e2SPeter Avalos #ifdef PORTMAP
158ce0e08e2SPeter Avalos 	struct pmaplist *tmp_pmapl = NULL;
159ce0e08e2SPeter Avalos #endif
160ce0e08e2SPeter Avalos 	int ok1, ok2 = TRUE;
161ce0e08e2SPeter Avalos 
162ce0e08e2SPeter Avalos 	ok1 = read_struct(RPCBFILE, (xdrproc_t)xdr_rpcblist_ptr, &tmp_rpcbl);
163ce0e08e2SPeter Avalos 	if (ok1 == FALSE)
164ce0e08e2SPeter Avalos 		return;
165ce0e08e2SPeter Avalos #ifdef PORTMAP
166ce0e08e2SPeter Avalos 	ok2 = read_struct(PMAPFILE, (xdrproc_t)xdr_pmaplist_ptr, &tmp_pmapl);
167ce0e08e2SPeter Avalos #endif
168ce0e08e2SPeter Avalos 	if (ok2 == FALSE) {
169ce0e08e2SPeter Avalos 		xdr_free((xdrproc_t) xdr_rpcblist_ptr, (char *)&tmp_rpcbl);
170ce0e08e2SPeter Avalos 		return;
171ce0e08e2SPeter Avalos 	}
172ce0e08e2SPeter Avalos 	xdr_free((xdrproc_t) xdr_rpcblist_ptr, (char *)&list_rbl);
173ce0e08e2SPeter Avalos 	list_rbl = tmp_rpcbl;
174ce0e08e2SPeter Avalos #ifdef PORTMAP
175ce0e08e2SPeter Avalos 	xdr_free((xdrproc_t) xdr_pmaplist_ptr, (char *)&list_pml);
176ce0e08e2SPeter Avalos 	list_pml = tmp_pmapl;
177ce0e08e2SPeter Avalos #endif
178ce0e08e2SPeter Avalos }
179