xref: /freebsd/usr.sbin/rpcbind/warmstart.c (revision 5f4c09dd85bff675e0ca63c55ea3c517e0fddfcc)
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  */
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <stdio.h>
43 #include <rpc/rpc.h>
44 #include <rpc/rpcb_prot.h>
45 #include <rpc/xdr.h>
46 #ifdef PORTMAP
47 #include <netinet/in.h>
48 #include <rpc/pmap_prot.h>
49 #endif
50 #include <syslog.h>
51 #include <unistd.h>
52 
53 #include "rpcbind.h"
54 
55 /*
56  * XXX this code is unsafe and is not used. It should be made safe.
57  */
58 
59 
60 /* These files keep the pmap_list and rpcb_list in XDR format */
61 #define	RPCBFILE	"/tmp/rpcbind.file"
62 #ifdef PORTMAP
63 #define	PMAPFILE	"/tmp/portmap.file"
64 #endif
65 
66 static bool_t write_struct(char *, xdrproc_t, void *);
67 static bool_t read_struct(char *, xdrproc_t, void *);
68 
69 static bool_t
70 write_struct(char *filename, xdrproc_t structproc, void *list)
71 {
72 	FILE *fp;
73 	XDR xdrs;
74 	mode_t omask;
75 
76 	omask = umask(077);
77 	fp = fopen(filename, "w");
78 	if (fp == NULL) {
79 		int i;
80 
81 		for (i = 0; i < 10; i++)
82 			close(i);
83 		fp = fopen(filename, "w");
84 		if (fp == NULL) {
85 			syslog(LOG_ERR,
86 				"cannot open file = %s for writing", filename);
87 			syslog(LOG_ERR, "cannot save any registration");
88 			return (FALSE);
89 		}
90 	}
91 	(void) umask(omask);
92 	xdrstdio_create(&xdrs, fp, XDR_ENCODE);
93 
94 	if (structproc(&xdrs, list) == FALSE) {
95 		syslog(LOG_ERR, "rpcbind: xdr_%s: failed", filename);
96 		fclose(fp);
97 		return (FALSE);
98 	}
99 	XDR_DESTROY(&xdrs);
100 	fclose(fp);
101 	return (TRUE);
102 }
103 
104 static bool_t
105 read_struct(char *filename, xdrproc_t structproc, void *list)
106 {
107 	FILE *fp;
108 	XDR xdrs;
109 	struct stat sbuf;
110 
111 	if (stat(filename, &sbuf) != 0) {
112 		fprintf(stderr,
113 		"rpcbind: cannot stat file = %s for reading\n", filename);
114 		goto error;
115 	}
116 	if ((sbuf.st_uid != 0) || (sbuf.st_mode & S_IRWXG) ||
117 	    (sbuf.st_mode & S_IRWXO)) {
118 		fprintf(stderr,
119 		"rpcbind: invalid permissions on file = %s for reading\n",
120 			filename);
121 		goto error;
122 	}
123 	fp = fopen(filename, "r");
124 	if (fp == NULL) {
125 		fprintf(stderr,
126 		"rpcbind: cannot open file = %s for reading\n", filename);
127 		goto error;
128 	}
129 	xdrstdio_create(&xdrs, fp, XDR_DECODE);
130 
131 	if (structproc(&xdrs, list) == FALSE) {
132 		fprintf(stderr, "rpcbind: xdr_%s: failed\n", filename);
133 		fclose(fp);
134 		goto error;
135 	}
136 	XDR_DESTROY(&xdrs);
137 	fclose(fp);
138 	return (TRUE);
139 
140 error:	fprintf(stderr, "rpcbind: will start from scratch\n");
141 	return (FALSE);
142 }
143 
144 void
145 write_warmstart(void)
146 {
147 	(void) write_struct(RPCBFILE, (xdrproc_t)xdr_rpcblist_ptr, &list_rbl);
148 #ifdef PORTMAP
149 	(void) write_struct(PMAPFILE, (xdrproc_t)xdr_pmaplist_ptr, &list_pml);
150 #endif
151 
152 }
153 
154 void
155 read_warmstart(void)
156 {
157 	rpcblist_ptr tmp_rpcbl = NULL;
158 #ifdef PORTMAP
159 	struct pmaplist *tmp_pmapl = NULL;
160 #endif
161 	int ok1, ok2 = TRUE;
162 
163 	ok1 = read_struct(RPCBFILE, (xdrproc_t)xdr_rpcblist_ptr, &tmp_rpcbl);
164 	if (ok1 == FALSE)
165 		return;
166 #ifdef PORTMAP
167 	ok2 = read_struct(PMAPFILE, (xdrproc_t)xdr_pmaplist_ptr, &tmp_pmapl);
168 #endif
169 	if (ok2 == FALSE) {
170 		xdr_free((xdrproc_t) xdr_rpcblist_ptr, (char *)&tmp_rpcbl);
171 		return;
172 	}
173 	xdr_free((xdrproc_t) xdr_rpcblist_ptr, (char *)&list_rbl);
174 	list_rbl = tmp_rpcbl;
175 #ifdef PORTMAP
176 	xdr_free((xdrproc_t) xdr_pmaplist_ptr, (char *)&list_pml);
177 	list_pml = tmp_pmapl;
178 #endif
179 }
180