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