Home | History | Annotate | Line # | Download | only in kern
sysv_ipc.c revision 1.13
      1  1.13  mycroft /*	$NetBSD: sysv_ipc.c,v 1.13 1998/08/15 05:19:50 mycroft Exp $	*/
      2   1.7      cgd 
      3  1.13  mycroft /*-
      4  1.13  mycroft  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  1.13  mycroft  * All rights reserved.
      6  1.13  mycroft  *
      7  1.13  mycroft  * This code is derived from software contributed to The NetBSD Foundation
      8  1.13  mycroft  * by Charles M. Hannum.
      9   1.1      cgd  *
     10   1.1      cgd  * Redistribution and use in source and binary forms, with or without
     11   1.1      cgd  * modification, are permitted provided that the following conditions
     12   1.1      cgd  * are met:
     13   1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     14   1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     15   1.6  hpeyerl  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.6  hpeyerl  *    notice, this list of conditions and the following disclaimer in the
     17   1.6  hpeyerl  *    documentation and/or other materials provided with the distribution.
     18   1.6  hpeyerl  * 3. All advertising materials mentioning features or use of this software
     19   1.6  hpeyerl  *    must display the following acknowledgement:
     20  1.13  mycroft  *        This product includes software developed by the NetBSD
     21  1.13  mycroft  *        Foundation, Inc. and its contributors.
     22  1.13  mycroft  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.13  mycroft  *    contributors may be used to endorse or promote products derived
     24  1.13  mycroft  *    from this software without specific prior written permission.
     25   1.1      cgd  *
     26  1.13  mycroft  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.13  mycroft  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.13  mycroft  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.13  mycroft  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.13  mycroft  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.13  mycroft  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.13  mycroft  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.13  mycroft  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.13  mycroft  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.13  mycroft  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.13  mycroft  * POSSIBILITY OF SUCH DAMAGE.
     37   1.1      cgd  */
     38   1.1      cgd 
     39   1.2  mycroft #include <sys/param.h>
     40   1.2  mycroft #include <sys/kernel.h>
     41   1.2  mycroft #include <sys/proc.h>
     42   1.2  mycroft #include <sys/ipc.h>
     43   1.4  hpeyerl #include <sys/systm.h>
     44  1.10  mycroft #include <sys/mount.h>
     45  1.10  mycroft #include <sys/vnode.h>
     46  1.12  mycroft #include <sys/stat.h>
     47   1.1      cgd 
     48   1.1      cgd /*
     49   1.4  hpeyerl  * Check for ipc permission
     50   1.1      cgd  */
     51   1.1      cgd 
     52   1.4  hpeyerl int
     53   1.5  mycroft ipcperm(cred, perm, mode)
     54   1.5  mycroft 	struct ucred *cred;
     55   1.4  hpeyerl 	struct ipc_perm *perm;
     56   1.1      cgd 	int mode;
     57   1.1      cgd {
     58  1.12  mycroft 	mode_t mask;
     59  1.12  mycroft 
     60  1.12  mycroft 	if (cred->cr_uid == 0)
     61  1.12  mycroft 		return (0);
     62   1.1      cgd 
     63   1.9  mycroft 	if (mode == IPC_M) {
     64  1.12  mycroft 		if (cred->cr_uid == perm->uid ||
     65   1.9  mycroft 		    cred->cr_uid == perm->cuid)
     66   1.9  mycroft 			return (0);
     67   1.9  mycroft 		return (EPERM);
     68   1.1      cgd 	}
     69   1.4  hpeyerl 
     70  1.12  mycroft 	mask = 0;
     71  1.12  mycroft 
     72  1.12  mycroft 	if (cred->cr_uid == perm->uid ||
     73  1.12  mycroft 	    cred->cr_uid == perm->cuid) {
     74  1.12  mycroft 		if (mode & IPC_R)
     75  1.12  mycroft 			mask |= S_IRUSR;
     76  1.12  mycroft 		if (mode & IPC_W)
     77  1.12  mycroft 			mask |= S_IWUSR;
     78  1.12  mycroft 		return ((perm->mode & mask) == mask ? 0 : EACCES);
     79  1.12  mycroft 	}
     80  1.12  mycroft 
     81  1.12  mycroft 	if (cred->cr_gid == perm->gid || groupmember(perm->gid, cred) ||
     82  1.12  mycroft 	    cred->cr_gid == perm->cgid || groupmember(perm->cgid, cred)) {
     83  1.12  mycroft 		if (mode & IPC_R)
     84  1.12  mycroft 			mask |= S_IRGRP;
     85  1.12  mycroft 		if (mode & IPC_W)
     86  1.12  mycroft 			mask |= S_IWGRP;
     87  1.12  mycroft 		return ((perm->mode & mask) == mask ? 0 : EACCES);
     88  1.12  mycroft 	}
     89  1.12  mycroft 
     90  1.12  mycroft 	if (mode & IPC_R)
     91  1.12  mycroft 		mask |= S_IROTH;
     92  1.12  mycroft 	if (mode & IPC_W)
     93  1.12  mycroft 		mask |= S_IWOTH;
     94  1.12  mycroft 	return ((perm->mode & mask) == mask ? 0 : EACCES);
     95   1.1      cgd }
     96