xref: /freebsd/sys/sys/ucred.h (revision 535af610)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	@(#)ucred.h	8.4 (Berkeley) 1/9/95
32  * $FreeBSD$
33  */
34 
35 #ifndef _SYS_UCRED_H_
36 #define	_SYS_UCRED_H_
37 
38 #if defined(_KERNEL) || defined(_WANT_UCRED)
39 #include <sys/_lock.h>
40 #include <sys/_mutex.h>
41 #endif
42 #include <bsm/audit.h>
43 
44 struct loginclass;
45 
46 #define	XU_NGROUPS	16
47 
48 /*
49  * Credentials.
50  *
51  * Please do not inspect cr_uid directly to determine superuserness.  The
52  * priv(9) interface should be used to check for privilege.
53  *
54  * Lock reference:
55  *      c - cr_mtx
56  *
57  * Unmarked fields are constant after creation.
58  *
59  * See "Credential management" comment in kern_prot.c for more information.
60  */
61 #if defined(_KERNEL) || defined(_WANT_UCRED)
62 struct ucred {
63 	struct mtx cr_mtx;
64 	long	cr_ref;			/* (c) reference count */
65 	u_int	cr_users;		/* (c) proc + thread using this cred */
66 	u_int	cr_flags;		/* credential flags */
67 	struct auditinfo_addr	cr_audit;	/* Audit properties. */
68 #define	cr_startcopy cr_uid
69 	uid_t	cr_uid;			/* effective user id */
70 	uid_t	cr_ruid;		/* real user id */
71 	uid_t	cr_svuid;		/* saved user id */
72 	int	cr_ngroups;		/* number of groups */
73 	gid_t	cr_rgid;		/* real group id */
74 	gid_t	cr_svgid;		/* saved group id */
75 	struct uidinfo	*cr_uidinfo;	/* per euid resource consumption */
76 	struct uidinfo	*cr_ruidinfo;	/* per ruid resource consumption */
77 	struct prison	*cr_prison;	/* jail(2) */
78 	struct loginclass	*cr_loginclass; /* login class */
79 	void 		*cr_pspare2[2];	/* general use 2 */
80 #define	cr_endcopy	cr_label
81 	struct label	*cr_label;	/* MAC label */
82 	gid_t	*cr_groups;		/* groups */
83 	int	cr_agroups;		/* Available groups */
84 	gid_t   cr_smallgroups[XU_NGROUPS];	/* storage for small groups */
85 };
86 #define	NOCRED	((struct ucred *)0)	/* no credential available */
87 #define	FSCRED	((struct ucred *)-1)	/* filesystem credential */
88 #endif /* _KERNEL || _WANT_UCRED */
89 
90 /*
91  * Flags for cr_flags.
92  */
93 #define	CRED_FLAG_CAPMODE	0x00000001	/* In capability mode. */
94 
95 /*
96  * This is the external representation of struct ucred.
97  */
98 struct xucred {
99 	u_int	cr_version;		/* structure layout version */
100 	uid_t	cr_uid;			/* effective user id */
101 	short	cr_ngroups;		/* number of groups */
102 	gid_t	cr_groups[XU_NGROUPS];	/* groups */
103 	union {
104 		void	*_cr_unused1;	/* compatibility with old ucred */
105 		pid_t	cr_pid;
106 	};
107 };
108 #define	XUCRED_VERSION	0
109 
110 /* This can be used for both ucred and xucred structures. */
111 #define	cr_gid cr_groups[0]
112 
113 #ifdef _KERNEL
114 struct proc;
115 struct thread;
116 
117 struct credbatch {
118 	struct ucred *cred;
119 	int users;
120 	int ref;
121 };
122 
123 static inline void
124 credbatch_prep(struct credbatch *crb)
125 {
126 	crb->cred = NULL;
127 	crb->users = 0;
128 	crb->ref = 0;
129 }
130 void	credbatch_add(struct credbatch *crb, struct thread *td);
131 
132 static inline void
133 credbatch_process(struct credbatch *crb __unused)
134 {
135 
136 }
137 
138 void	credbatch_final(struct credbatch *crb);
139 
140 void	change_egid(struct ucred *newcred, gid_t egid);
141 void	change_euid(struct ucred *newcred, struct uidinfo *euip);
142 void	change_rgid(struct ucred *newcred, gid_t rgid);
143 void	change_ruid(struct ucred *newcred, struct uidinfo *ruip);
144 void	change_svgid(struct ucred *newcred, gid_t svgid);
145 void	change_svuid(struct ucred *newcred, uid_t svuid);
146 void	crcopy(struct ucred *dest, struct ucred *src);
147 struct ucred	*crcopysafe(struct proc *p, struct ucred *cr);
148 struct ucred	*crdup(struct ucred *cr);
149 void	crextend(struct ucred *cr, int n);
150 void	proc_set_cred_init(struct proc *p, struct ucred *cr);
151 void	proc_set_cred(struct proc *p, struct ucred *cr);
152 void	proc_unset_cred(struct proc *p);
153 void	crfree(struct ucred *cr);
154 struct ucred	*crcowsync(void);
155 struct ucred	*crget(void);
156 struct ucred	*crhold(struct ucred *cr);
157 struct ucred	*crcowget(struct ucred *cr);
158 void	crcowfree(struct thread *td);
159 void	cru2x(struct ucred *cr, struct xucred *xcr);
160 void	cru2xt(struct thread *td, struct xucred *xcr);
161 void	crsetgroups(struct ucred *cr, int n, gid_t *groups);
162 int	groupmember(gid_t gid, struct ucred *cred);
163 #endif /* _KERNEL */
164 
165 #endif /* !_SYS_UCRED_H_ */
166