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