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