xref: /illumos-gate/usr/src/cmd/rpcbind/warmstart.c (revision 8f6d9dae)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * warmstart.c
247c478bd9Sstevel@tonic-gate  * Allows for gathering of registrations from a earlier dumped file.
257c478bd9Sstevel@tonic-gate  *
267c478bd9Sstevel@tonic-gate  * Copyright 1990,2002-2003 Sun Microsystems, Inc.  All rights reserved.
277c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
287c478bd9Sstevel@tonic-gate  */
29*8f6d9daeSMarcel Telka /*
30*8f6d9daeSMarcel Telka  * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
31*8f6d9daeSMarcel Telka  */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <errno.h>
357c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
367c478bd9Sstevel@tonic-gate #include <rpc/rpcb_prot.h>
377c478bd9Sstevel@tonic-gate #include <sys/types.h>
387c478bd9Sstevel@tonic-gate #include <sys/stat.h>
397c478bd9Sstevel@tonic-gate #ifdef PORTMAP
407c478bd9Sstevel@tonic-gate #include <netinet/in.h>
417c478bd9Sstevel@tonic-gate #include <rpc/pmap_prot.h>
427c478bd9Sstevel@tonic-gate #endif
437c478bd9Sstevel@tonic-gate #include "rpcbind.h"
447c478bd9Sstevel@tonic-gate #include <syslog.h>
457c478bd9Sstevel@tonic-gate #include <unistd.h>
467c478bd9Sstevel@tonic-gate #include <rpcsvc/daemon_utils.h>
47*8f6d9daeSMarcel Telka #include <assert.h>
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /* These files keep the pmap_list and rpcb_list in XDR format */
507c478bd9Sstevel@tonic-gate static const char rpcbfile[] = DAEMON_DIR "/rpcbind.file";
517c478bd9Sstevel@tonic-gate #ifdef PORTMAP
527c478bd9Sstevel@tonic-gate static const char pmapfile[] = DAEMON_DIR "/portmap.file";
537c478bd9Sstevel@tonic-gate #endif
547c478bd9Sstevel@tonic-gate 
55*8f6d9daeSMarcel Telka static FILE *
open_tmp_file(const char * filename)56*8f6d9daeSMarcel Telka open_tmp_file(const char *filename)
577c478bd9Sstevel@tonic-gate {
587c478bd9Sstevel@tonic-gate 	int fd;
597c478bd9Sstevel@tonic-gate 	FILE *fp;
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 	/*
627c478bd9Sstevel@tonic-gate 	 * Remove any existing files, and create a new one.
637c478bd9Sstevel@tonic-gate 	 * Ensure that rpcbind is not forced to overwrite
647c478bd9Sstevel@tonic-gate 	 * a file pointed to by a symbolic link created
657c478bd9Sstevel@tonic-gate 	 * by an attacker.
667c478bd9Sstevel@tonic-gate 	 * Use open O_CREAT|O_EXCL so file is not created
677c478bd9Sstevel@tonic-gate 	 * between unlink() and open() operation.
687c478bd9Sstevel@tonic-gate 	 */
697c478bd9Sstevel@tonic-gate 	if (unlink(filename) == -1) {
707c478bd9Sstevel@tonic-gate 		if (errno != ENOENT)
717c478bd9Sstevel@tonic-gate 			return (NULL);
727c478bd9Sstevel@tonic-gate 	}
737c478bd9Sstevel@tonic-gate 	fd = open(filename, O_CREAT|O_EXCL|O_WRONLY, 0600);
747c478bd9Sstevel@tonic-gate 	if (fd == -1)
757c478bd9Sstevel@tonic-gate 		return (NULL);
767c478bd9Sstevel@tonic-gate 	fp = fdopen(fd, "w");
777c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
787c478bd9Sstevel@tonic-gate 		close(fd);
797c478bd9Sstevel@tonic-gate 		return (NULL);
807c478bd9Sstevel@tonic-gate 	}
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	return (fp);
837c478bd9Sstevel@tonic-gate }
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate static bool_t
write_struct(const char * filename,xdrproc_t structproc,void * list)86*8f6d9daeSMarcel Telka write_struct(const char *filename, xdrproc_t structproc, void *list)
877c478bd9Sstevel@tonic-gate {
887c478bd9Sstevel@tonic-gate 	FILE *fp;
897c478bd9Sstevel@tonic-gate 	XDR xdrs;
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	fp = open_tmp_file(filename);
927c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
937c478bd9Sstevel@tonic-gate 		int i;
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 		for (i = 0; i < 10; i++)
967c478bd9Sstevel@tonic-gate 			close(i);
977c478bd9Sstevel@tonic-gate 		fp = open_tmp_file(filename);
987c478bd9Sstevel@tonic-gate 		if (fp == NULL) {
997c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR,
1007c478bd9Sstevel@tonic-gate 			    "cannot open file = %s for writing", filename);
1017c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "cannot save any registration");
1027c478bd9Sstevel@tonic-gate 			return (FALSE);
1037c478bd9Sstevel@tonic-gate 		}
1047c478bd9Sstevel@tonic-gate 	}
1057c478bd9Sstevel@tonic-gate 	xdrstdio_create(&xdrs, fp, XDR_ENCODE);
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	if (structproc(&xdrs, list) == FALSE) {
108*8f6d9daeSMarcel Telka 		XDR_DESTROY(&xdrs);
1097c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "rpcbind: xdr_%s: failed", filename);
1107c478bd9Sstevel@tonic-gate 		fclose(fp);
1117c478bd9Sstevel@tonic-gate 		return (FALSE);
1127c478bd9Sstevel@tonic-gate 	}
1137c478bd9Sstevel@tonic-gate 	XDR_DESTROY(&xdrs);
1147c478bd9Sstevel@tonic-gate 	fclose(fp);
1157c478bd9Sstevel@tonic-gate 	return (TRUE);
1167c478bd9Sstevel@tonic-gate }
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate static bool_t
read_struct(const char * filename,xdrproc_t structproc,void * list)119*8f6d9daeSMarcel Telka read_struct(const char *filename, xdrproc_t structproc, void *list)
1207c478bd9Sstevel@tonic-gate {
1217c478bd9Sstevel@tonic-gate 	int fd;
1227c478bd9Sstevel@tonic-gate 	FILE *fp = NULL;
1237c478bd9Sstevel@tonic-gate 	XDR xdrs;
1247c478bd9Sstevel@tonic-gate 	struct stat sbuf_fstat, sbuf_lstat;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	fd = open(filename, O_RDONLY, 0600);
1277c478bd9Sstevel@tonic-gate 	if (fd == -1) {
1287c478bd9Sstevel@tonic-gate 		fprintf(stderr,
1297c478bd9Sstevel@tonic-gate 		    "rpcbind: cannot open file = %s for reading\n", filename);
1307c478bd9Sstevel@tonic-gate 		goto error;
1317c478bd9Sstevel@tonic-gate 	}
1327c478bd9Sstevel@tonic-gate 	fp = fdopen(fd, "r");
1337c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
1347c478bd9Sstevel@tonic-gate 		close(fd);
1357c478bd9Sstevel@tonic-gate 		fprintf(stderr,
1367c478bd9Sstevel@tonic-gate 		    "rpcbind: cannot open file = %s for reading\n", filename);
1377c478bd9Sstevel@tonic-gate 		goto error;
1387c478bd9Sstevel@tonic-gate 	}
1397c478bd9Sstevel@tonic-gate 	if (fstat(fd, &sbuf_fstat) != 0) {
1407c478bd9Sstevel@tonic-gate 		fprintf(stderr,
1417c478bd9Sstevel@tonic-gate 		    "rpcbind: cannot stat file = %s for reading\n", filename);
1427c478bd9Sstevel@tonic-gate 		goto error;
1437c478bd9Sstevel@tonic-gate 	}
1447c478bd9Sstevel@tonic-gate 	if (sbuf_fstat.st_uid != DAEMON_UID ||
1457c478bd9Sstevel@tonic-gate 	    (!S_ISREG(sbuf_fstat.st_mode)) ||
1467c478bd9Sstevel@tonic-gate 	    (sbuf_fstat.st_mode & S_IRWXG) ||
1477c478bd9Sstevel@tonic-gate 	    (sbuf_fstat.st_mode & S_IRWXO) ||
1487c478bd9Sstevel@tonic-gate 	    (sbuf_fstat.st_nlink != 1)) {
149*8f6d9daeSMarcel Telka 		fprintf(stderr, "rpcbind: invalid permissions on file = %s for "
150*8f6d9daeSMarcel Telka 		    "reading\n", filename);
1517c478bd9Sstevel@tonic-gate 		goto error;
1527c478bd9Sstevel@tonic-gate 	}
1537c478bd9Sstevel@tonic-gate 	/*
1547c478bd9Sstevel@tonic-gate 	 * Make sure that the pathname for fstat and lstat is the same and
1557c478bd9Sstevel@tonic-gate 	 * that it's not a link.  An attacker can create symbolic or
1567c478bd9Sstevel@tonic-gate 	 * hard links and use them to gain unauthorised access to the
1577c478bd9Sstevel@tonic-gate 	 * system when rpcbind aborts or terminates on SIGINT or SIGTERM.
1587c478bd9Sstevel@tonic-gate 	 */
1597c478bd9Sstevel@tonic-gate 	if (lstat(filename, &sbuf_lstat) != 0) {
1607c478bd9Sstevel@tonic-gate 		fprintf(stderr,
1617c478bd9Sstevel@tonic-gate 		    "rpcbind: cannot lstat file = %s for reading\n", filename);
1627c478bd9Sstevel@tonic-gate 		goto error;
1637c478bd9Sstevel@tonic-gate 	}
1647c478bd9Sstevel@tonic-gate 	if (sbuf_lstat.st_uid != DAEMON_UID ||
1657c478bd9Sstevel@tonic-gate 	    (!S_ISREG(sbuf_lstat.st_mode)) ||
1667c478bd9Sstevel@tonic-gate 	    (sbuf_lstat.st_mode & S_IRWXG) ||
1677c478bd9Sstevel@tonic-gate 	    (sbuf_lstat.st_mode & S_IRWXO) ||
1687c478bd9Sstevel@tonic-gate 	    (sbuf_lstat.st_nlink != 1) ||
1697c478bd9Sstevel@tonic-gate 	    (sbuf_fstat.st_dev != sbuf_lstat.st_dev) ||
1707c478bd9Sstevel@tonic-gate 	    (sbuf_fstat.st_ino != sbuf_lstat.st_ino)) {
171*8f6d9daeSMarcel Telka 		fprintf(stderr, "rpcbind: invalid lstat permissions on file = "
172*8f6d9daeSMarcel Telka 		    "%s for reading\n", filename);
1737c478bd9Sstevel@tonic-gate 		goto error;
1747c478bd9Sstevel@tonic-gate 	}
1757c478bd9Sstevel@tonic-gate 	xdrstdio_create(&xdrs, fp, XDR_DECODE);
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	if (structproc(&xdrs, list) == FALSE) {
178*8f6d9daeSMarcel Telka 		XDR_DESTROY(&xdrs);
1797c478bd9Sstevel@tonic-gate 		fprintf(stderr, "rpcbind: xdr_%s: failed\n", filename);
1807c478bd9Sstevel@tonic-gate 		goto error;
1817c478bd9Sstevel@tonic-gate 	}
1827c478bd9Sstevel@tonic-gate 	XDR_DESTROY(&xdrs);
1837c478bd9Sstevel@tonic-gate 	fclose(fp);
1847c478bd9Sstevel@tonic-gate 	return (TRUE);
1857c478bd9Sstevel@tonic-gate 
186*8f6d9daeSMarcel Telka error:
187*8f6d9daeSMarcel Telka 	fprintf(stderr, "rpcbind: will start from scratch\n");
1887c478bd9Sstevel@tonic-gate 	if (fp != NULL)
1897c478bd9Sstevel@tonic-gate 		fclose(fp);
1907c478bd9Sstevel@tonic-gate 	return (FALSE);
1917c478bd9Sstevel@tonic-gate }
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate void
write_warmstart(void)1947c478bd9Sstevel@tonic-gate write_warmstart(void)
1957c478bd9Sstevel@tonic-gate {
196*8f6d9daeSMarcel Telka 	assert(RW_WRITE_HELD(&list_rbl_lock));
1977c478bd9Sstevel@tonic-gate 	(void) write_struct(rpcbfile, xdr_rpcblist_ptr, &list_rbl);
1987c478bd9Sstevel@tonic-gate #ifdef PORTMAP
199*8f6d9daeSMarcel Telka 	assert(RW_WRITE_HELD(&list_pml_lock));
2007c478bd9Sstevel@tonic-gate 	(void) write_struct(pmapfile, xdr_pmaplist_ptr, &list_pml);
2017c478bd9Sstevel@tonic-gate #endif
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate }
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate void
read_warmstart(void)2067c478bd9Sstevel@tonic-gate read_warmstart(void)
2077c478bd9Sstevel@tonic-gate {
2087c478bd9Sstevel@tonic-gate 	rpcblist_ptr tmp_rpcbl = NULL;
2097c478bd9Sstevel@tonic-gate #ifdef PORTMAP
2107c478bd9Sstevel@tonic-gate 	pmaplist_ptr tmp_pmapl = NULL;
2117c478bd9Sstevel@tonic-gate #endif
2127c478bd9Sstevel@tonic-gate 
213*8f6d9daeSMarcel Telka 	if (read_struct(rpcbfile, xdr_rpcblist_ptr, &tmp_rpcbl) == FALSE)
2147c478bd9Sstevel@tonic-gate 		return;
215*8f6d9daeSMarcel Telka 
2167c478bd9Sstevel@tonic-gate #ifdef PORTMAP
217*8f6d9daeSMarcel Telka 	if (read_struct(pmapfile, xdr_pmaplist_ptr, &tmp_pmapl) == FALSE) {
2187c478bd9Sstevel@tonic-gate 		xdr_free((xdrproc_t)xdr_rpcblist_ptr, (char *)&tmp_rpcbl);
2197c478bd9Sstevel@tonic-gate 		return;
2207c478bd9Sstevel@tonic-gate 	}
221*8f6d9daeSMarcel Telka #endif
222*8f6d9daeSMarcel Telka 
2237c478bd9Sstevel@tonic-gate 	xdr_free((xdrproc_t)xdr_rpcblist_ptr, (char *)&list_rbl);
2247c478bd9Sstevel@tonic-gate 	list_rbl = tmp_rpcbl;
2257c478bd9Sstevel@tonic-gate #ifdef PORTMAP
2267c478bd9Sstevel@tonic-gate 	xdr_free((xdrproc_t)xdr_pmaplist_ptr, (char *)&list_pml);
2277c478bd9Sstevel@tonic-gate 	list_pml = (pmaplist *)tmp_pmapl;
2287c478bd9Sstevel@tonic-gate #endif
2297c478bd9Sstevel@tonic-gate }
230