warmstart.c revision 104589
174462Salfred/* 274462Salfred * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 374462Salfred * unrestricted use provided that this legend is included on all tape 474462Salfred * media and as a part of the software program in whole or part. Users 574462Salfred * may copy or modify Sun RPC without charge, but are not authorized 674462Salfred * to license or distribute it to anyone else except as part of a product or 774462Salfred * program developed by the user. 874462Salfred * 974462Salfred * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 1074462Salfred * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 1174462Salfred * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 1274462Salfred * 1374462Salfred * Sun RPC is provided with no support and without any obligation on the 1474462Salfred * part of Sun Microsystems, Inc. to assist in its use, correction, 1574462Salfred * modification or enhancement. 1674462Salfred * 1774462Salfred * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 1874462Salfred * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 1974462Salfred * OR ANY PART THEREOF. 2074462Salfred * 2174462Salfred * In no event will Sun Microsystems, Inc. be liable for any lost revenue 2274462Salfred * or profits or other special, indirect and consequential damages, even if 2374462Salfred * Sun has been advised of the possibility of such damages. 2474462Salfred * 2574462Salfred * Sun Microsystems, Inc. 2674462Salfred * 2550 Garcia Avenue 2774462Salfred * Mountain View, California 94043 2874462Salfred */ 2974462Salfred/* 3074462Salfred * warmstart.c 3174462Salfred * Allows for gathering of registrations from a earlier dumped file. 3274462Salfred * 3374462Salfred * Copyright (c) 1990 by Sun Microsystems, Inc. 3474462Salfred */ 3574462Salfred 3674462Salfred/* 3774462Salfred * #ident "@(#)warmstart.c 1.7 93/07/05 SMI" 3874462Salfred * $FreeBSD: head/usr.sbin/rpcbind/warmstart.c 104589 2002-10-07 01:16:00Z alfred $/ 3974462Salfred */ 4074462Salfred#include <sys/types.h> 4174462Salfred#include <sys/stat.h> 4274462Salfred#include <stdio.h> 4374462Salfred#include <rpc/rpc.h> 4474462Salfred#include <rpc/rpcb_prot.h> 4574462Salfred#include <rpc/xdr.h> 4674462Salfred#ifdef PORTMAP 4774462Salfred#include <netinet/in.h> 4874462Salfred#include <rpc/pmap_prot.h> 4974462Salfred#endif 5074462Salfred#include <syslog.h> 5174462Salfred#include <unistd.h> 5274462Salfred 5374462Salfred#include "rpcbind.h" 5474462Salfred 5574462Salfred/* 5674462Salfred * XXX this code is unsafe and is not used. It should be made safe. 5774462Salfred */ 5874462Salfred 5974462Salfred 6074462Salfred/* These files keep the pmap_list and rpcb_list in XDR format */ 6174462Salfred#define RPCBFILE "/tmp/rpcbind.file" 6274462Salfred#ifdef PORTMAP 6374462Salfred#define PMAPFILE "/tmp/portmap.file" 6474462Salfred#endif 6574462Salfred 6674462Salfredstatic bool_t write_struct __P((char *, xdrproc_t, void *)); 6774462Salfredstatic bool_t read_struct __P((char *, xdrproc_t, void *)); 6874462Salfred 6974462Salfredstatic bool_t 7074462Salfredwrite_struct(char *filename, xdrproc_t structproc, void *list) 7174462Salfred{ 7274462Salfred FILE *fp; 7374462Salfred XDR xdrs; 7474462Salfred mode_t omask; 7574462Salfred 7674462Salfred omask = umask(077); 7774462Salfred fp = fopen(filename, "w"); 7874462Salfred if (fp == NULL) { 7974462Salfred int i; 8074462Salfred 8174462Salfred for (i = 0; i < 10; i++) 8274462Salfred close(i); 8374462Salfred fp = fopen(filename, "w"); 8474462Salfred if (fp == NULL) { 8574462Salfred syslog(LOG_ERR, 8674462Salfred "cannot open file = %s for writing", filename); 8774462Salfred syslog(LOG_ERR, "cannot save any registration"); 8874462Salfred return (FALSE); 8974462Salfred } 9074462Salfred } 9174462Salfred (void) umask(omask); 9274462Salfred xdrstdio_create(&xdrs, fp, XDR_ENCODE); 9374462Salfred 9474462Salfred if (structproc(&xdrs, list) == FALSE) { 9574462Salfred syslog(LOG_ERR, "rpcbind: xdr_%s: failed", filename); 9674462Salfred fclose(fp); 9774462Salfred return (FALSE); 9874462Salfred } 9974462Salfred XDR_DESTROY(&xdrs); 10074462Salfred fclose(fp); 10174462Salfred return (TRUE); 10274462Salfred} 10374462Salfred 10474462Salfredstatic bool_t 10574462Salfredread_struct(char *filename, xdrproc_t structproc, void *list) 10674462Salfred{ 10774462Salfred FILE *fp; 10874462Salfred XDR xdrs; 10974462Salfred struct stat sbuf; 11074462Salfred 11174462Salfred if (stat(filename, &sbuf) != 0) { 11274462Salfred fprintf(stderr, 11374462Salfred "rpcbind: cannot stat file = %s for reading\n", filename); 11474462Salfred goto error; 11574462Salfred } 11674462Salfred if ((sbuf.st_uid != 0) || (sbuf.st_mode & S_IRWXG) || 11774462Salfred (sbuf.st_mode & S_IRWXO)) { 11874462Salfred fprintf(stderr, 11974462Salfred "rpcbind: invalid permissions on file = %s for reading\n", 12074462Salfred filename); 12174462Salfred goto error; 12274462Salfred } 12374462Salfred fp = fopen(filename, "r"); 12474462Salfred if (fp == NULL) { 12574462Salfred fprintf(stderr, 12674462Salfred "rpcbind: cannot open file = %s for reading\n", filename); 12774462Salfred goto error; 12874462Salfred } 12974462Salfred xdrstdio_create(&xdrs, fp, XDR_DECODE); 13074462Salfred 13174462Salfred if (structproc(&xdrs, list) == FALSE) { 13274462Salfred fprintf(stderr, "rpcbind: xdr_%s: failed\n", filename); 13374462Salfred fclose(fp); 13474462Salfred goto error; 13574462Salfred } 13674462Salfred XDR_DESTROY(&xdrs); 13774462Salfred fclose(fp); 13874462Salfred return (TRUE); 13974462Salfred 14074462Salfrederror: fprintf(stderr, "rpcbind: will start from scratch\n"); 14174462Salfred return (FALSE); 14274462Salfred} 14374462Salfred 14474462Salfredvoid 14574462Salfredwrite_warmstart() 14674462Salfred{ 147104589Salfred (void) write_struct(RPCBFILE, (xdrproc_t)xdr_rpcblist_ptr, &list_rbl); 14874462Salfred#ifdef PORTMAP 149104589Salfred (void) write_struct(PMAPFILE, (xdrproc_t)xdr_pmaplist_ptr, &list_pml); 15074462Salfred#endif 15174462Salfred 15274462Salfred} 15374462Salfred 15474462Salfredvoid 15574462Salfredread_warmstart() 15674462Salfred{ 15774462Salfred rpcblist_ptr tmp_rpcbl = NULL; 15874462Salfred#ifdef PORTMAP 15974462Salfred struct pmaplist *tmp_pmapl = NULL; 16074462Salfred#endif 16174462Salfred int ok1, ok2 = TRUE; 16274462Salfred 163104589Salfred ok1 = read_struct(RPCBFILE, (xdrproc_t)xdr_rpcblist_ptr, &tmp_rpcbl); 16474462Salfred if (ok1 == FALSE) 16574462Salfred return; 16674462Salfred#ifdef PORTMAP 167104589Salfred ok2 = read_struct(PMAPFILE, (xdrproc_t)xdr_pmaplist_ptr, &tmp_pmapl); 16874462Salfred#endif 16974462Salfred if (ok2 == FALSE) { 17074462Salfred xdr_free((xdrproc_t) xdr_rpcblist_ptr, (char *)&tmp_rpcbl); 17174462Salfred return; 17274462Salfred } 17374462Salfred xdr_free((xdrproc_t) xdr_rpcblist_ptr, (char *)&list_rbl); 17474462Salfred list_rbl = tmp_rpcbl; 17574462Salfred#ifdef PORTMAP 17674462Salfred xdr_free((xdrproc_t) xdr_pmaplist_ptr, (char *)&list_pml); 17774462Salfred list_pml = tmp_pmapl; 17874462Salfred#endif 17974462Salfred} 180