[BACK]Return to pt_file.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sbin / mount_portal

File: [cvs.NetBSD.org] / src / sbin / mount_portal / pt_file.c (download)

Revision 1.17, Mon Jul 2 18:07:45 2007 UTC (12 years, 11 months ago) by pooka
Branch: MAIN
CVS Tags: yamt-pf42-baseX, yamt-pf42-base4, yamt-pf42-base3, yamt-pf42-base2, yamt-pf42-base, yamt-pf42, yamt-pagecache-tag8, yamt-pagecache-base8, yamt-pagecache-base7, yamt-pagecache-base6, yamt-pagecache-base5, yamt-pagecache-base4, yamt-pagecache-base3, yamt-pagecache-base2, yamt-pagecache-base, wrstuden-revivesa-base-3, wrstuden-revivesa-base-2, wrstuden-revivesa-base-1, wrstuden-revivesa-base, wrstuden-revivesa, riastradh-drm2-base2, riastradh-drm2-base1, riastradh-drm2-base, riastradh-drm2, netbsd-6-base, netbsd-6-1-RELEASE, netbsd-6-1-RC4, netbsd-6-1-RC3, netbsd-6-1-RC2, netbsd-6-1-RC1, netbsd-6-1-5-RELEASE, netbsd-6-1-4-RELEASE, netbsd-6-1-3-RELEASE, netbsd-6-1-2-RELEASE, netbsd-6-1-1-RELEASE, netbsd-6-1, netbsd-6-0-RELEASE, netbsd-6-0-RC2, netbsd-6-0-RC1, netbsd-6-0-6-RELEASE, netbsd-6-0-5-RELEASE, netbsd-6-0-4-RELEASE, netbsd-6-0-3-RELEASE, netbsd-6-0-2-RELEASE, netbsd-6-0-1-RELEASE, netbsd-6-0, netbsd-6, netbsd-5-base, netbsd-5-2-RELEASE, netbsd-5-2-RC1, netbsd-5-2-3-RELEASE, netbsd-5-2-2-RELEASE, netbsd-5-2-1-RELEASE, netbsd-5-2, netbsd-5-1-RELEASE, netbsd-5-1-RC4, netbsd-5-1-RC3, netbsd-5-1-RC2, netbsd-5-1-RC1, netbsd-5-1-5-RELEASE, netbsd-5-1-4-RELEASE, netbsd-5-1-3-RELEASE, netbsd-5-1-2-RELEASE, netbsd-5-1-1-RELEASE, netbsd-5-1, netbsd-5-0-RELEASE, netbsd-5-0-RC4, netbsd-5-0-RC3, netbsd-5-0-RC2, netbsd-5-0-RC1, netbsd-5-0-2-RELEASE, netbsd-5-0-1-RELEASE, netbsd-5-0, netbsd-5, mjf-devfs2-base, mjf-devfs2, mjf-devfs-base, mjf-devfs, matt-premerge-20091211, matt-nb6-plus-nbase, matt-nb6-plus-base, matt-nb6-plus, matt-nb5-pq3-base, matt-nb5-pq3, matt-nb5-mips64-u2-k2-k4-k7-k8-k9, matt-nb5-mips64-u1-k1-k5, matt-nb5-mips64-premerge-20101231, matt-nb5-mips64-premerge-20091211, matt-nb5-mips64-k15, matt-nb5-mips64, matt-nb4-mips64-k7-u2a-k9b, matt-mips64-premerge-20101231, matt-mips64-base2, matt-mips64-base, matt-mips64, matt-armv6-prevmlocking, matt-armv6-nbase, matt-armv6-base, matt-armv6, keiichi-mipv6-base, keiichi-mipv6, jym-xensuspend-nbase, jym-xensuspend-base, jym-xensuspend, hpcarm-cleanup-nbase, hpcarm-cleanup-base, hpcarm-cleanup, cube-autoconf-base, cube-autoconf, cherry-xenmp-base, cherry-xenmp, bouyer-quota2-nbase, bouyer-quota2-base, bouyer-quota2, agc-symver-base, agc-symver
Branch point for: yamt-pagecache, tls-maxphys
Changes since 1.16: +3 -3 lines

The kernel socket isn't used by any of the providers, and it's a bit
hard to imagine how it could be used, so get rid of it.

/*	$NetBSD: pt_file.c,v 1.17 2007/07/02 18:07:45 pooka Exp $	*/

/*
 * Copyright (c) 1992, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * This code is derived from software donated to Berkeley by
 * Jan-Simon Pendry.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *	from: Id: pt_file.c,v 1.1 1992/05/25 21:43:09 jsp Exp
 *	@(#)pt_file.c	8.3 (Berkeley) 7/3/94
 */

#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: pt_file.c,v 1.17 2007/07/02 18:07:45 pooka Exp $");
#endif /* not lint */

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/syslog.h>

#include "portald.h"

#ifdef DEBUG
#define DEBUG_SYSLOG	syslog
#else
/*
 * The "if (0) ..." will be optimized away by the compiler if
 * DEBUG is not defined.
 */
#define DEBUG_SYSLOG	if (0) syslog
#endif

int
lose_credentials(struct portal_cred *pcr)
{
	/*
	 * If we are root, then switch into the caller's credentials.
	 * By the way, we _always_ log failures, to make
	 * sure questionable activity is noticed.
	 */
	if (getuid() == 0) {
		/* Set groups first ... */
		if (setgroups(pcr->pcr_ngroups, pcr->pcr_groups) < 0) {
			syslog(LOG_WARNING,
			    "lose_credentials: setgroups() failed (%m)");
			return (errno);
		}
		/* ... then gid ... */
		if (setgid(pcr->pcr_gid) < 0) {
			syslog(LOG_WARNING,
				"lose_credentials: setgid(%d) failed (%m)",
				pcr->pcr_gid);
		}
		/*
		 * ... and now do the setuid() where we lose all special
		 * powers (both real and effective userid).
		 */
		if (setuid(pcr->pcr_uid) < 0) {
			syslog(LOG_WARNING,
				"lose_credentials: setuid(%d) failed (%m)",
				pcr->pcr_uid);
		}
		/* The credential change was successful! */
		DEBUG_SYSLOG(LOG_DEBUG, "Root-owned mount process lowered credentials -- returning successfully!\n");
		return 0;
	}
	DEBUG_SYSLOG(LOG_DEBUG, "Actual/effective/caller's creds are:");
	DEBUG_SYSLOG(LOG_DEBUG, "%d/%d %d/%d %d/%d", getuid(),
	    getgid(), geteuid(), getegid(), pcr->pcr_uid, pcr->pcr_gid);
	/*
	 * Else, fail if the uid is neither actual or effective
	 * uid of mount process...
	 */
	if ((getuid() != pcr->pcr_uid) && (geteuid() != pcr->pcr_uid)) {
		syslog(LOG_WARNING,
		    "lose_credentials: uid %d != uid %d, or euid %d != uid %d",
		    getuid(), pcr->pcr_uid, geteuid(), pcr->pcr_uid);
		return EPERM;
	}
	/*
	 * ... or the gid is neither the actual or effective
	 * gid of the mount process.
	 */
	if ((getgid() != pcr->pcr_gid) && (getegid() != pcr->pcr_gid)) {
		syslog(LOG_WARNING,
		    "lose_credentials: gid %d != gid %d, or egid %d != gid %d",
		    getgid(), pcr->pcr_gid, getegid(), pcr->pcr_gid);
		return EPERM;
	}
	/*
	 * If we make it here, we have a uid _and_ gid match! Allow the
	 * access.
	 */
	DEBUG_SYSLOG(LOG_DEBUG, "Returning successfully!\n");
	return 0;
}

int
portal_file(struct portal_cred *pcr, char *key, char **v, int *fdp)
{
	int     fd;
	char    pbuf[MAXPATHLEN];
	int     error;
	int     origuid, origgid;

	origuid = getuid();
	origgid = getgid();
	pbuf[0] = '/';
	strlcpy(pbuf + 1, key + (v[1] ? strlen(v[1]) : 0), sizeof(pbuf) - 1);
	DEBUG_SYSLOG(LOG_DEBUG, "path = %s, uid = %d, gid = %d",
	    pbuf, pcr->pcr_uid, pcr->pcr_gid);

	if ((error = lose_credentials(pcr)) != 0) {
		DEBUG_SYSLOG(LOG_DEBUG, "portal_file: Credential err %d", error);
		return error;
	}
	error = 0;
	/*
	 * Be careful -- only set error to errno if there is an error.
	 * errno could hold an old, uncaught value, from a routine
	 * called long before now.
	 */
	fd = open(pbuf, O_RDWR | O_CREAT, 0666);
	if (fd < 0) {
		error = errno;
		if (error == EACCES) {
			DEBUG_SYSLOG(LOG_DEBUG, "Error:  could not open '%s' "
			    "read/write with create flag.  "
			    "Trying read-only open...", pbuf);
			/* Try opening read-only. */
			fd = open(pbuf, O_RDONLY, 0);
			if (fd < 0) {
				error = errno;
				DEBUG_SYSLOG(LOG_DEBUG, "That failed too!  %m");
			} else {
				/* Clear the error indicator. */
				error = 0;
			}
		} else {
			DEBUG_SYSLOG(LOG_DEBUG, "Error:  could not open '%s': %m", pbuf);
		}

	}
	if (seteuid((uid_t) origuid) < 0) {	/* XXX - should reset gidset
						 * too */
		error = errno;
		syslog(LOG_WARNING, "setcred: %m");
		if (fd >= 0) {
			(void) close(fd);
			fd = -1;
		}
	}
	if (error == 0)
		*fdp = fd;

	DEBUG_SYSLOG(LOG_DEBUG, "pt_file returns *fdp = %d, error = %d", *fdp,
	    error);
	return (error);
}