xref: /dragonfly/sys/sys/caps.h (revision 9bb2a92d)
1 /*
2  * SYS/CAPS.H
3  *
4  *	Implements an architecture independant Capability Service API
5  *
6  * $DragonFly: src/sys/sys/caps.h,v 1.5 2004/03/06 22:14:16 dillon Exp $
7  */
8 
9 #ifndef _SYS_CAPS_H_
10 #define _SYS_CAPS_H_
11 
12 #ifndef _SYS_TYPES_H_
13 #include <sys/types.h>
14 #endif
15 #ifndef _SYS_MSGPORT_H_
16 #include <sys/msgport.h>
17 #endif
18 
19 typedef enum caps_msg_state {
20 	CAPMS_REQUEST,
21 	CAPMS_REQUEST_RETRY, 	/* internal / FUTURE */
22 	CAPMS_REPLY,
23 	CAPMS_REPLY_RETRY,	/* internal / FUGURE */
24 	CAPMS_DISPOSE
25 } caps_msg_state_t;
26 
27 typedef struct caps_msgid {
28 	off_t			c_id;
29 	caps_msg_state_t	c_state;
30 	int			c_reserved01;
31 } *caps_msgid_t;
32 
33 typedef enum caps_type {
34 	CAPT_UNKNOWN, CAPT_CLIENT, CAPT_SERVICE, CAPT_REMOTE, CAPT_FORKED
35 } caps_type_t;
36 
37 typedef int64_t	caps_gen_t;
38 
39 /*
40  * Note: upper 16 bits reserved for kernel use
41  */
42 #define CAPF_UFLAGS	0xFFFF
43 #define CAPF_USER	0x0001
44 #define CAPF_GROUP	0x0002
45 #define CAPF_WORLD	0x0004
46 #define CAPF_EXCL	0x0008
47 #define CAPF_ANYCLIENT	(CAPF_USER|CAPF_GROUP|CAPF_WORLD)
48 #define CAPF_WCRED	0x0010	/* waiting for cred */
49 #define CAPF_NOFORK	0x0020	/* do not create a dummy entry on fork */
50 #define CAPF_WAITSVC	0x0040	/* block if service not available */
51 /* FUTURE: CAPF_ASYNC - support async services */
52 /* FUTURE: CAPF_NOGROUPS - don't bother filling in the groups[] array */
53 /* FUTURE: CAPF_TERM - send termination request to existing service */
54 /* FUTURE: CAPF_TAKE - take over existing service's connections */
55 /* FUTURE: CAPF_DISPOSE_IMM - need immediate dispose wakeups */
56 
57 /*
58  * Abort codes
59  */
60 #define CAPS_ABORT_NOTIMPL	0	/* abort not implemented, no action */
61 #define CAPS_ABORT_RETURNED	1	/* already returned, no action */
62 #define CAPS_ABORT_BEFORESERVER	2	/* caught before the server got it */
63 #define CAPS_ABORT_ATSERVER	3	/* server had retrieved message */
64 
65 #define CAPF_ABORT_HARD		0x0001	/* rip out from under server (3) */
66 
67 #define CAPS_MAXGROUPS	16
68 #define CAPS_MAXNAMELEN	64
69 #define CAPS_MAXINPROG	128
70 
71 struct thread;
72 
73 typedef struct caps_port {
74 	struct lwkt_port	cp_lport;
75 	int			cp_portid;	/* caps port id */
76 	int			cp_upcallid;	/* upcall id */
77 } *caps_port_t;
78 
79 typedef struct caps_cred {
80 	pid_t			pid;
81 	uid_t			uid;
82 	uid_t			euid;
83 	gid_t			gid;
84 	int			ngroups;
85 	int			cacheid;
86 	gid_t			groups[CAPS_MAXGROUPS];
87 } *caps_cred_t;
88 
89 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
90 
91 struct caps_kmsg;
92 
93 TAILQ_HEAD(caps_kmsg_queue, caps_kmsg);
94 
95 /*
96  * caps_kinfo -	Holds a client or service registration
97  *
98  * ci_msgpendq: holds the kernel copy of the message after it has been
99  * 		sent to the local port.  The message is matched up against
100  *		replies and automatically replied if the owner closes its
101  *		connection.
102  */
103 typedef struct caps_kinfo {
104 	struct lwkt_port	ci_lport;	/* embedded local port */
105 	struct caps_kinfo	*ci_tdnext;	/* per-process list */
106 	struct caps_kinfo	*ci_hnext;	/* registration hash table */
107 	struct thread		*ci_td;		/* owner */
108 	struct caps_kmsg_queue	ci_msgpendq;	/* pending reply (just rcvd) */
109 	struct caps_kmsg_queue	ci_msguserq;	/* pending reply (user holds) */
110 	struct caps_kinfo	*ci_rcaps;	/* connected to remote */
111 	int			ci_cmsgcount;	/* client in-progress msgs */
112 	int			ci_id;
113 	int			ci_flags;
114 	int			ci_refs;
115 	int			ci_mrefs;	/* message (vmspace) refs */
116 	caps_type_t		ci_type;
117 	caps_gen_t		ci_gen;
118 	uid_t			ci_uid;
119 	gid_t			ci_gid;
120 	int			ci_namelen;
121 	char			ci_name[4];	/* variable length */
122 	/* ci_name must be last element */
123 } *caps_kinfo_t;
124 
125 /* note: user flags are held in the low 16 bits */
126 #define CAPKF_TDLIST	0x00010000
127 #define CAPKF_HLIST	0x00020000
128 #define CAPKF_FLUSH	0x00040000
129 #define CAPKF_RCAPS	0x00080000
130 #define CAPKF_CLOSED	0x00100000
131 #define CAPKF_MWAIT	0x00200000
132 
133 /*
134  * Kernel caps message.  The kernel keepps track of messagse received,
135  * undergoing processing by the service, and returned.  User-supplied data
136  * is copied on reception rather then transmission.
137  */
138 typedef struct caps_kmsg {
139 	TAILQ_ENTRY(caps_kmsg)	km_node;
140 	caps_kinfo_t		km_mcaps;	/* message sender */
141 	void			*km_umsg;	/* mcaps vmspace */
142 	int			km_umsg_size;	/* mcaps vmspace */
143 	struct caps_cred	km_ccr;		/* caps cred for msg */
144 	struct caps_msgid	km_msgid;
145 	int			km_flags;
146 } *caps_kmsg_t;
147 
148 #define km_state	km_msgid.c_state
149 
150 #define CAPKMF_ONUSERQ		0x0001
151 #define CAPKMF_ONPENDQ		0x0002
152 #define CAPKMF_REPLY		0x0004
153 #define CAPKMF_CDONE		0x0008
154 #define CAPKMF_PEEKED		0x0010
155 #define CAPKMF_ABORTED		0x0020
156 
157 #endif
158 
159 #ifdef _KERNEL
160 
161 /*
162  * kernel support
163  */
164 void caps_exit(struct thread *td);
165 void caps_fork(struct proc *p1, struct proc *p2, int flags);
166 
167 #else
168 
169 /*
170  * Userland API (libcaps)
171  */
172 caps_port_t caps_service(const char *, uid_t, gid_t, mode_t, int);
173 caps_port_t caps_client(const char *, uid_t, gid_t, int);
174 
175 /*
176  * Syscall API
177  */
178 int caps_sys_service(const char *, uid_t, gid_t, int, int);
179 int caps_sys_client(const char *, uid_t, gid_t, int, int);
180 off_t caps_sys_put(int, void *, int);
181 int caps_sys_reply(int, void *, int, off_t);
182 int caps_sys_get(int, void *, int, caps_msgid_t, caps_cred_t);
183 int caps_sys_wait(int, void *, int, caps_msgid_t, caps_cred_t);
184 int caps_sys_abort(int, off_t, int);
185 int caps_sys_setgen(int, caps_gen_t);
186 caps_gen_t caps_sys_getgen(int);
187 
188 #endif
189 
190 #endif
191 
192