warmstart.c revision 1.5 1 1.1 fvdl /*
2 1.1 fvdl * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 1.1 fvdl * unrestricted use provided that this legend is included on all tape
4 1.1 fvdl * media and as a part of the software program in whole or part. Users
5 1.1 fvdl * may copy or modify Sun RPC without charge, but are not authorized
6 1.1 fvdl * to license or distribute it to anyone else except as part of a product or
7 1.1 fvdl * program developed by the user.
8 1.1 fvdl *
9 1.1 fvdl * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 1.1 fvdl * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 1.1 fvdl * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
12 1.1 fvdl *
13 1.1 fvdl * Sun RPC is provided with no support and without any obligation on the
14 1.1 fvdl * part of Sun Microsystems, Inc. to assist in its use, correction,
15 1.1 fvdl * modification or enhancement.
16 1.1 fvdl *
17 1.1 fvdl * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 1.1 fvdl * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 1.1 fvdl * OR ANY PART THEREOF.
20 1.1 fvdl *
21 1.1 fvdl * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 1.1 fvdl * or profits or other special, indirect and consequential damages, even if
23 1.1 fvdl * Sun has been advised of the possibility of such damages.
24 1.1 fvdl *
25 1.1 fvdl * Sun Microsystems, Inc.
26 1.1 fvdl * 2550 Garcia Avenue
27 1.1 fvdl * Mountain View, California 94043
28 1.1 fvdl */
29 1.1 fvdl /*
30 1.1 fvdl * warmstart.c
31 1.1 fvdl * Allows for gathering of registrations from a earlier dumped file.
32 1.1 fvdl *
33 1.1 fvdl * Copyright (c) 1990 by Sun Microsystems, Inc.
34 1.1 fvdl */
35 1.1 fvdl
36 1.1 fvdl #ident "@(#)warmstart.c 1.7 93/07/05 SMI"
37 1.1 fvdl
38 1.1 fvdl #include <sys/types.h>
39 1.1 fvdl #include <sys/stat.h>
40 1.1 fvdl #include <stdio.h>
41 1.2 christos #include <fcntl.h>
42 1.2 christos #include <err.h>
43 1.4 ginsbach #include <paths.h>
44 1.1 fvdl #include <rpc/rpc.h>
45 1.1 fvdl #include <rpc/rpcb_prot.h>
46 1.1 fvdl #include <rpc/xdr.h>
47 1.1 fvdl #ifdef PORTMAP
48 1.1 fvdl #include <netinet/in.h>
49 1.1 fvdl #include <rpc/pmap_prot.h>
50 1.1 fvdl #endif
51 1.1 fvdl #include <syslog.h>
52 1.1 fvdl #include <unistd.h>
53 1.1 fvdl
54 1.1 fvdl #include "rpcbind.h"
55 1.1 fvdl
56 1.1 fvdl /*
57 1.1 fvdl * XXX this code is unsafe and is not used. It should be made safe.
58 1.1 fvdl */
59 1.3 christos #ifdef WARMSTART
60 1.1 fvdl
61 1.1 fvdl
62 1.1 fvdl /* These files keep the pmap_list and rpcb_list in XDR format */
63 1.4 ginsbach #define RPCBFILE _PATH_VARRUN "rpcbind.file"
64 1.1 fvdl #ifdef PORTMAP
65 1.4 ginsbach #define PMAPFILE _PATH_VARRUN "portmap.file"
66 1.1 fvdl #endif
67 1.1 fvdl
68 1.2 christos static bool_t write_struct(const char *, xdrproc_t, void *);
69 1.2 christos static bool_t read_struct(const char *, xdrproc_t, void *);
70 1.1 fvdl
71 1.1 fvdl static bool_t
72 1.2 christos write_struct(const char *filename, xdrproc_t structproc, void *list)
73 1.1 fvdl {
74 1.1 fvdl FILE *fp;
75 1.2 christos int fd;
76 1.1 fvdl XDR xdrs;
77 1.1 fvdl
78 1.3 christos (void)unlink(filename);
79 1.3 christos fd = open(filename, O_WRONLY|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR);
80 1.3 christos if (fd == -1 || (fp = fdopen(fd, "w")) == NULL) {
81 1.2 christos syslog(LOG_ERR, "Cannot open `%s' (%m)", filename);
82 1.2 christos syslog(LOG_ERR, "Cannot save any registration");
83 1.2 christos return FALSE;
84 1.2 christos }
85 1.1 fvdl
86 1.1 fvdl xdrstdio_create(&xdrs, fp, XDR_ENCODE);
87 1.1 fvdl
88 1.1 fvdl if (structproc(&xdrs, list) == FALSE) {
89 1.2 christos syslog(LOG_ERR, "xdr_%s: failed", filename);
90 1.2 christos (void)fclose(fp);
91 1.1 fvdl return (FALSE);
92 1.1 fvdl }
93 1.1 fvdl XDR_DESTROY(&xdrs);
94 1.2 christos (void)fclose(fp);
95 1.2 christos return TRUE;
96 1.1 fvdl }
97 1.1 fvdl
98 1.1 fvdl static bool_t
99 1.2 christos read_struct(const char *filename, xdrproc_t structproc, void *list)
100 1.1 fvdl {
101 1.1 fvdl FILE *fp;
102 1.1 fvdl XDR xdrs;
103 1.1 fvdl struct stat sbuf;
104 1.1 fvdl
105 1.1 fvdl if (stat(filename, &sbuf) != 0) {
106 1.2 christos warn("Cannot stat `%s'", filename);
107 1.1 fvdl goto error;
108 1.1 fvdl }
109 1.1 fvdl if ((sbuf.st_uid != 0) || (sbuf.st_mode & S_IRWXG) ||
110 1.1 fvdl (sbuf.st_mode & S_IRWXO)) {
111 1.2 christos warnx("Invalid permissions on `%s'", filename);
112 1.1 fvdl goto error;
113 1.1 fvdl }
114 1.1 fvdl fp = fopen(filename, "r");
115 1.1 fvdl if (fp == NULL) {
116 1.2 christos warn("cannot open `%s'", filename);
117 1.1 fvdl goto error;
118 1.1 fvdl }
119 1.1 fvdl xdrstdio_create(&xdrs, fp, XDR_DECODE);
120 1.1 fvdl
121 1.1 fvdl if (structproc(&xdrs, list) == FALSE) {
122 1.2 christos warnx("xdr_%s failed", filename);
123 1.2 christos (void)fclose(fp);
124 1.1 fvdl goto error;
125 1.1 fvdl }
126 1.1 fvdl XDR_DESTROY(&xdrs);
127 1.2 christos (void)fclose(fp);
128 1.2 christos return TRUE;
129 1.1 fvdl
130 1.2 christos error: warnx("Will start from scratch");
131 1.2 christos return FALSE;
132 1.1 fvdl }
133 1.1 fvdl
134 1.1 fvdl void
135 1.2 christos write_warmstart(void)
136 1.1 fvdl {
137 1.5 ginsbach (void)write_struct(RPCBFILE, (xdrproc_t) xdr_rpcblist_ptr, &list_rbl);
138 1.1 fvdl #ifdef PORTMAP
139 1.5 ginsbach (void)write_struct(PMAPFILE, (xdrproc_t) xdr_pmaplist_ptr, &list_pml);
140 1.1 fvdl #endif
141 1.1 fvdl
142 1.1 fvdl }
143 1.1 fvdl
144 1.1 fvdl void
145 1.2 christos read_warmstart(void)
146 1.1 fvdl {
147 1.1 fvdl rpcblist_ptr tmp_rpcbl = NULL;
148 1.1 fvdl #ifdef PORTMAP
149 1.1 fvdl struct pmaplist *tmp_pmapl = NULL;
150 1.1 fvdl #endif
151 1.1 fvdl int ok1, ok2 = TRUE;
152 1.1 fvdl
153 1.5 ginsbach ok1 = read_struct(RPCBFILE, (xdrproc_t) xdr_rpcblist_ptr, &tmp_rpcbl);
154 1.1 fvdl if (ok1 == FALSE)
155 1.1 fvdl return;
156 1.1 fvdl #ifdef PORTMAP
157 1.5 ginsbach ok2 = read_struct(PMAPFILE, (xdrproc_t) xdr_pmaplist_ptr, &tmp_pmapl);
158 1.1 fvdl #endif
159 1.1 fvdl if (ok2 == FALSE) {
160 1.1 fvdl xdr_free((xdrproc_t) xdr_rpcblist_ptr, (char *)&tmp_rpcbl);
161 1.1 fvdl return;
162 1.1 fvdl }
163 1.1 fvdl xdr_free((xdrproc_t) xdr_rpcblist_ptr, (char *)&list_rbl);
164 1.1 fvdl list_rbl = tmp_rpcbl;
165 1.1 fvdl #ifdef PORTMAP
166 1.1 fvdl xdr_free((xdrproc_t) xdr_pmaplist_ptr, (char *)&list_pml);
167 1.1 fvdl list_pml = tmp_pmapl;
168 1.1 fvdl #endif
169 1.1 fvdl }
170 1.3 christos #endif
171