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