Home | History | Annotate | Line # | Download | only in libpuffs
creds.c revision 1.1
      1  1.1  pooka /*	$NetBSD: creds.c,v 1.1 2006/10/22 22:52:21 pooka Exp $	*/
      2  1.1  pooka 
      3  1.1  pooka /*
      4  1.1  pooka  * Copyright (c) 2006  Antti Kantee.  All Rights Reserved.
      5  1.1  pooka  *
      6  1.1  pooka  * Development of this software was supported by the Ulla Tuominen Foundation.
      7  1.1  pooka  *
      8  1.1  pooka  * Redistribution and use in source and binary forms, with or without
      9  1.1  pooka  * modification, are permitted provided that the following conditions
     10  1.1  pooka  * are met:
     11  1.1  pooka  * 1. Redistributions of source code must retain the above copyright
     12  1.1  pooka  *    notice, this list of conditions and the following disclaimer.
     13  1.1  pooka  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  pooka  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  pooka  *    documentation and/or other materials provided with the distribution.
     16  1.1  pooka  * 3. The name of the company nor the name of the author may be used to
     17  1.1  pooka  *    endorse or promote products derived from this software without specific
     18  1.1  pooka  *    prior written permission.
     19  1.1  pooka  *
     20  1.1  pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     21  1.1  pooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     22  1.1  pooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     23  1.1  pooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     24  1.1  pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25  1.1  pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     26  1.1  pooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27  1.1  pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28  1.1  pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29  1.1  pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30  1.1  pooka  * SUCH DAMAGE.
     31  1.1  pooka  */
     32  1.1  pooka 
     33  1.1  pooka #include <sys/cdefs.h>
     34  1.1  pooka #if !defined(lint)
     35  1.1  pooka __RCSID("$NetBSD: creds.c,v 1.1 2006/10/22 22:52:21 pooka Exp $");
     36  1.1  pooka #endif /* !lint */
     37  1.1  pooka 
     38  1.1  pooka /*
     39  1.1  pooka  * Interface for dealing with credits.
     40  1.1  pooka  */
     41  1.1  pooka 
     42  1.1  pooka #include <sys/types.h>
     43  1.1  pooka #include <sys/param.h>
     44  1.1  pooka 
     45  1.1  pooka #include <puffs.h>
     46  1.1  pooka #include <errno.h>
     47  1.1  pooka #include <string.h>
     48  1.1  pooka 
     49  1.1  pooka #define UUCCRED(a) (a->pcr_type == PUFFCRED_TYPE_UUC)
     50  1.1  pooka #define INTCRED(a) (a->pcr_type == PUFFCRED_TYPE_INTERNAL)
     51  1.1  pooka 
     52  1.1  pooka int
     53  1.1  pooka puffs_cred_getuid(const struct puffs_cred *pcr, uid_t *ruid)
     54  1.1  pooka {
     55  1.1  pooka 
     56  1.1  pooka 	if (!UUCCRED(pcr))
     57  1.1  pooka 		return EINVAL;
     58  1.1  pooka 	*ruid = pcr->pcr_uuc.cr_uid;
     59  1.1  pooka 
     60  1.1  pooka 	return 0;
     61  1.1  pooka }
     62  1.1  pooka 
     63  1.1  pooka int
     64  1.1  pooka puffs_cred_getgid(const struct puffs_cred *pcr, gid_t *rgid)
     65  1.1  pooka {
     66  1.1  pooka 
     67  1.1  pooka 	if (!UUCCRED(pcr))
     68  1.1  pooka 		return EINVAL;
     69  1.1  pooka 	*rgid = pcr->pcr_uuc.cr_gid;
     70  1.1  pooka 
     71  1.1  pooka 	return 0;
     72  1.1  pooka }
     73  1.1  pooka 
     74  1.1  pooka int
     75  1.1  pooka puffs_cred_getgroups(const struct puffs_cred *pcr, gid_t *rgids, short *ngids)
     76  1.1  pooka {
     77  1.1  pooka 	short ncopy;
     78  1.1  pooka 
     79  1.1  pooka 	if (!UUCCRED(pcr))
     80  1.1  pooka 		return EINVAL;
     81  1.1  pooka 
     82  1.1  pooka 	ncopy = MIN(*ngids, NGROUPS);
     83  1.1  pooka 	memcpy(rgids, pcr->pcr_uuc.cr_groups, ncopy);
     84  1.1  pooka 	*ngids = ncopy;
     85  1.1  pooka 
     86  1.1  pooka 	return 0;
     87  1.1  pooka }
     88  1.1  pooka 
     89  1.1  pooka int
     90  1.1  pooka puffs_cred_isuid(const struct puffs_cred *pcr, uid_t uid)
     91  1.1  pooka {
     92  1.1  pooka 
     93  1.1  pooka 	return UUCCRED(pcr) && pcr->pcr_uuc.cr_uid == uid;
     94  1.1  pooka }
     95  1.1  pooka 
     96  1.1  pooka int
     97  1.1  pooka puffs_cred_hasgroup(const struct puffs_cred *pcr, gid_t gid)
     98  1.1  pooka {
     99  1.1  pooka 	short i;
    100  1.1  pooka 
    101  1.1  pooka 	if (!UUCCRED(pcr))
    102  1.1  pooka 		return 0;
    103  1.1  pooka 
    104  1.1  pooka 	if (pcr->pcr_uuc.cr_gid == gid)
    105  1.1  pooka 		return 1;
    106  1.1  pooka 	for (i = 0; i < pcr->pcr_uuc.cr_ngroups; i++)
    107  1.1  pooka 		if (pcr->pcr_uuc.cr_groups[i] == gid)
    108  1.1  pooka 			return 1;
    109  1.1  pooka 
    110  1.1  pooka 	return 0;
    111  1.1  pooka }
    112  1.1  pooka 
    113  1.1  pooka int
    114  1.1  pooka puffs_cred_iskernel(const struct puffs_cred *pcr)
    115  1.1  pooka {
    116  1.1  pooka 
    117  1.1  pooka 	return INTCRED(pcr) && pcr->pcr_internal == PUFFCRED_CRED_NOCRED;
    118  1.1  pooka }
    119  1.1  pooka 
    120  1.1  pooka int
    121  1.1  pooka puffs_cred_isfs(const struct puffs_cred *pcr)
    122  1.1  pooka {
    123  1.1  pooka 
    124  1.1  pooka 	return INTCRED(pcr) && pcr->pcr_internal == PUFFCRED_CRED_FSCRED;
    125  1.1  pooka }
    126  1.1  pooka 
    127  1.1  pooka int
    128  1.1  pooka puffs_cred_isjuggernaut(const struct puffs_cred *pcr)
    129  1.1  pooka {
    130  1.1  pooka 
    131  1.1  pooka 	return puffs_cred_isuid(pcr, 0) || puffs_cred_iskernel(pcr)
    132  1.1  pooka 	    || puffs_cred_isfs(pcr);
    133  1.1  pooka }
    134