1 /* $NetBSD: warmstart.c,v 1.8 2017/08/17 00:08:10 ginsbach Exp $ */ 2 /* $FreeBSD: head/usr.sbin/rpcbind/warmstart.c 258564 2013-11-25 16:44:02Z hrs $*/ 3 4 /*- 5 * Copyright (c) 2009, Sun Microsystems, Inc. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions are met: 10 * - Redistributions of source code must retain the above copyright notice, 11 * this list of conditions and the following disclaimer. 12 * - Redistributions in binary form must reproduce the above copyright notice, 13 * this list of conditions and the following disclaimer in the documentation 14 * and/or other materials provided with the distribution. 15 * - Neither the name of Sun Microsystems, Inc. nor the names of its 16 * contributors may be used to endorse or promote products derived 17 * from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 /* 32 * warmstart.c 33 * Allows for gathering of registrations from an earlier dumped file. 34 * 35 * Copyright (c) 1990 by Sun Microsystems, Inc. 36 */ 37 38 #ident "@(#)warmstart.c 1.7 93/07/05 SMI" 39 40 #include <sys/types.h> 41 #include <sys/stat.h> 42 #include <stdio.h> 43 #include <fcntl.h> 44 #include <err.h> 45 #include <paths.h> 46 #include <rpc/rpc.h> 47 #include <rpc/rpcb_prot.h> 48 #include <rpc/xdr.h> 49 #ifdef PORTMAP 50 #include <netinet/in.h> 51 #include <rpc/pmap_prot.h> 52 #endif 53 #include <syslog.h> 54 #include <unistd.h> 55 56 #include "rpcbind.h" 57 58 /* 59 * XXX this code is unsafe and is not used. It should be made safe. 60 */ 61 #ifdef WARMSTART 62 63 64 /* These files keep the pmap_list and rpcb_list in XDR format */ 65 #define RPCBFILE _PATH_VARRUN "rpcbind.file" 66 #ifdef PORTMAP 67 #define PMAPFILE _PATH_VARRUN "portmap.file" 68 #endif 69 70 static bool_t write_struct(const char *, xdrproc_t, void *); 71 static bool_t read_struct(const char *, xdrproc_t, void *); 72 73 static bool_t 74 write_struct(const char *filename, xdrproc_t structproc, void *list) 75 { 76 FILE *fp; 77 int fd; 78 XDR xdrs; 79 80 (void)unlink(filename); 81 fd = open(filename, O_WRONLY|O_CREAT|O_EXCL, S_IRUSR|S_IWUSR); 82 if (fd == -1 || (fp = fdopen(fd, "w")) == NULL) { 83 syslog(LOG_ERR, "Cannot open `%s' (%m)", filename); 84 syslog(LOG_ERR, "Cannot save any registration"); 85 return FALSE; 86 } 87 88 xdrstdio_create(&xdrs, fp, XDR_ENCODE); 89 90 if (structproc(&xdrs, list) == FALSE) { 91 syslog(LOG_ERR, "xdr_%s: failed", filename); 92 (void)fclose(fp); 93 return (FALSE); 94 } 95 XDR_DESTROY(&xdrs); 96 (void)fclose(fp); 97 return TRUE; 98 } 99 100 static bool_t 101 read_struct(const char *filename, xdrproc_t structproc, void *list) 102 { 103 FILE *fp; 104 XDR xdrs; 105 struct stat sbuf; 106 107 if (stat(filename, &sbuf) != 0) { 108 warn("Cannot stat `%s'", filename); 109 goto error; 110 } 111 if ((sbuf.st_uid != 0) || (sbuf.st_mode & S_IRWXG) || 112 (sbuf.st_mode & S_IRWXO)) { 113 warnx("Invalid permissions on `%s'", filename); 114 goto error; 115 } 116 fp = fopen(filename, "r"); 117 if (fp == NULL) { 118 warn("cannot open `%s'", filename); 119 goto error; 120 } 121 xdrstdio_create(&xdrs, fp, XDR_DECODE); 122 123 if (structproc(&xdrs, list) == FALSE) { 124 warnx("xdr_%s failed", filename); 125 (void)fclose(fp); 126 goto error; 127 } 128 XDR_DESTROY(&xdrs); 129 (void)fclose(fp); 130 return TRUE; 131 132 error: warnx("Will start from scratch"); 133 return FALSE; 134 } 135 136 void 137 write_warmstart(void) 138 { 139 (void)write_struct(RPCBFILE, (xdrproc_t) xdr_rpcblist_ptr, &list_rbl); 140 #ifdef PORTMAP 141 (void)write_struct(PMAPFILE, (xdrproc_t) xdr_pmaplist_ptr, &list_pml); 142 #endif 143 144 } 145 146 void 147 read_warmstart(void) 148 { 149 rpcblist_ptr tmp_rpcbl = NULL; 150 #ifdef PORTMAP 151 struct pmaplist *tmp_pmapl = NULL; 152 #endif 153 int ok1, ok2 = TRUE; 154 155 ok1 = read_struct(RPCBFILE, (xdrproc_t) xdr_rpcblist_ptr, &tmp_rpcbl); 156 if (ok1 == FALSE) 157 return; 158 #ifdef PORTMAP 159 ok2 = read_struct(PMAPFILE, (xdrproc_t) xdr_pmaplist_ptr, &tmp_pmapl); 160 #endif 161 if (ok2 == FALSE) { 162 xdr_free((xdrproc_t) xdr_rpcblist_ptr, (char *)&tmp_rpcbl); 163 return; 164 } 165 xdr_free((xdrproc_t) xdr_rpcblist_ptr, (char *)&list_rbl); 166 list_rbl = tmp_rpcbl; 167 #ifdef PORTMAP 168 xdr_free((xdrproc_t) xdr_pmaplist_ptr, (char *)&list_pml); 169 list_pml = tmp_pmapl; 170 #endif 171 } 172 #endif 173