xref: /freebsd/usr.sbin/rpcbind/warmstart.c (revision 8c2f6c3be0125142d3c1782e4b0ee0634c584b9e)
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  */
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <stdio.h>
42 #include <rpc/rpc.h>
43 #include <rpc/rpcb_prot.h>
44 #include <rpc/xdr.h>
45 #ifdef PORTMAP
46 #include <netinet/in.h>
47 #include <rpc/pmap_prot.h>
48 #endif
49 #include <syslog.h>
50 #include <unistd.h>
51 
52 #include "rpcbind.h"
53 
54 /*
55  * XXX this code is unsafe and is not used. It should be made safe.
56  */
57 
58 
59 /* These files keep the pmap_list and rpcb_list in XDR format */
60 #define	RPCBFILE	"/tmp/rpcbind.file"
61 #ifdef PORTMAP
62 #define	PMAPFILE	"/tmp/portmap.file"
63 #endif
64 
65 static bool_t write_struct(char *, xdrproc_t, void *);
66 static bool_t read_struct(char *, xdrproc_t, void *);
67 
68 static bool_t
69 write_struct(char *filename, xdrproc_t structproc, void *list)
70 {
71 	FILE *fp;
72 	XDR xdrs;
73 	mode_t omask;
74 
75 	omask = umask(077);
76 	fp = fopen(filename, "w");
77 	if (fp == NULL) {
78 		int i;
79 
80 		for (i = 0; i < 10; i++)
81 			close(i);
82 		fp = fopen(filename, "w");
83 		if (fp == NULL) {
84 			syslog(LOG_ERR,
85 				"cannot open file = %s for writing", filename);
86 			syslog(LOG_ERR, "cannot save any registration");
87 			return (FALSE);
88 		}
89 	}
90 	(void) umask(omask);
91 	xdrstdio_create(&xdrs, fp, XDR_ENCODE);
92 
93 	if (structproc(&xdrs, list) == FALSE) {
94 		syslog(LOG_ERR, "rpcbind: xdr_%s: failed", filename);
95 		fclose(fp);
96 		return (FALSE);
97 	}
98 	XDR_DESTROY(&xdrs);
99 	fclose(fp);
100 	return (TRUE);
101 }
102 
103 static bool_t
104 read_struct(char *filename, xdrproc_t structproc, void *list)
105 {
106 	FILE *fp;
107 	XDR xdrs;
108 	struct stat sbuf;
109 
110 	if (stat(filename, &sbuf) != 0) {
111 		fprintf(stderr,
112 		"rpcbind: cannot stat file = %s for reading\n", filename);
113 		goto error;
114 	}
115 	if ((sbuf.st_uid != 0) || (sbuf.st_mode & S_IRWXG) ||
116 	    (sbuf.st_mode & S_IRWXO)) {
117 		fprintf(stderr,
118 		"rpcbind: invalid permissions on file = %s for reading\n",
119 			filename);
120 		goto error;
121 	}
122 	fp = fopen(filename, "r");
123 	if (fp == NULL) {
124 		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 
139 error:	fprintf(stderr, "rpcbind: will start from scratch\n");
140 	return (FALSE);
141 }
142 
143 void
144 write_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 
153 void
154 read_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