xref: /freebsd/usr.sbin/rpcbind/warmstart.c (revision 0b3105a37d7adcadcb720112fed4dc4e8040be99)
1 /*-
2  * Copyright (c) 2009, Sun Microsystems, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * - Redistributions of source code must retain the above copyright notice,
8  *   this list of conditions and the following disclaimer.
9  * - Redistributions in binary form must reproduce the above copyright notice,
10  *   this list of conditions and the following disclaimer in the documentation
11  *   and/or other materials provided with the distribution.
12  * - Neither the name of Sun Microsystems, Inc. nor the names of its
13  *   contributors may be used to endorse or promote products derived
14  *   from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 /*
29  * warmstart.c
30  * Allows for gathering of registrations from an earlier dumped file.
31  *
32  * Copyright (c) 1990 by Sun Microsystems, Inc.
33  */
34 
35 /*
36  * #ident	"@(#)warmstart.c	1.7	93/07/05 SMI"
37  * $FreeBSD$/
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