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