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