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