xref: /freebsd/lib/libcasper/services/cap_fileargs/cap_fileargs.h (revision f6a3b357e9be4c6423c85eff9a847163a0d307c8)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2018 Mariusz Zaborski <oshogbo@FreeBSD.org>
5  * 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  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #ifndef _FILEARGS_H_
32 #define	_FILEARGS_H_
33 
34 #include <sys/dnv.h>
35 #include <sys/nv.h>
36 
37 #include <stdbool.h>
38 
39 #define	FA_OPEN		1
40 #define	FA_LSTAT	2
41 
42 #ifdef WITH_CASPER
43 struct fileargs;
44 typedef struct fileargs fileargs_t;
45 struct stat;
46 
47 fileargs_t *fileargs_init(int argc, char *argv[], int flags, mode_t mode,
48     cap_rights_t *rightsp, int operations);
49 fileargs_t *fileargs_cinit(cap_channel_t *cas, int argc, char *argv[],
50     int flags, mode_t mode, cap_rights_t *rightsp, int operations);
51 fileargs_t *fileargs_initnv(nvlist_t *limits);
52 fileargs_t *fileargs_cinitnv(cap_channel_t *cas, nvlist_t *limits);
53 int fileargs_lstat(fileargs_t *fa, const char *name, struct stat *sb);
54 int fileargs_open(fileargs_t *fa, const char *name);
55 void fileargs_free(fileargs_t *fa);
56 FILE *fileargs_fopen(fileargs_t *fa, const char *name, const char *mode);
57 
58 fileargs_t *fileargs_wrap(cap_channel_t *chan, int fdflags);
59 cap_channel_t *fileargs_unwrap(fileargs_t *fa, int *fdflags);
60 #else
61 typedef struct fileargs {
62 	int	fa_flags;
63 	mode_t	fa_mode;
64 } fileargs_t;
65 
66 static inline fileargs_t *
67 fileargs_init(int argc __unused, char *argv[] __unused, int flags, mode_t mode,
68     cap_rights_t *rightsp __unused, int operations __unused) {
69 	fileargs_t *fa;
70 
71 	fa = malloc(sizeof(*fa));
72 	if (fa != NULL) {
73 		fa->fa_flags = flags;
74 		fa->fa_mode = mode;
75 	}
76 
77 	return (fa);
78 }
79 
80 static inline fileargs_t *
81 fileargs_cinit(cap_channel_t *cas __unused, int argc, char *argv[], int flags,
82     mode_t mode, cap_rights_t *rightsp, int operations)
83 {
84 
85 	return (fileargs_init(argc, argv, flags, mode, rightsp, operations));
86 }
87 
88 static inline fileargs_t *
89 fileargs_initnv(nvlist_t *limits)
90 {
91 	fileargs_t *fa;
92 
93 	fa = fileargs_init(0, NULL,
94 	    nvlist_get_number(limits, "flags"),
95 	    dnvlist_get_number(limits, "mode", 0),
96 	    NULL,
97 	    nvlist_get_number(limits, "operations"));
98 	nvlist_destroy(limits);
99 
100 	return (fa);
101 }
102 
103 static inline fileargs_t *
104 fileargs_cinitnv(cap_channel_t *cas __unused, nvlist_t *limits)
105 {
106 
107 	return (fileargs_initnv(limits));
108 }
109 
110 #define fileargs_lstat(fa, name, sb)						\
111 	lstat(name, sb)
112 #define	fileargs_open(fa, name)							\
113 	open(name, fa->fa_flags, fa->fa_mode)
114 static inline
115 FILE *fileargs_fopen(fileargs_t *fa, const char *name, const char *mode)
116 {
117 	(void) fa;
118 	return (fopen(name, mode));
119 }
120 #define	fileargs_free(fa)		(free(fa))
121 
122 static inline fileargs_t *
123 fileargs_wrap(cap_channel_t *chan, int fdflags)
124 {
125 
126 	cap_close(chan);
127 	return (fileargs_init(0, NULL, fdflags, 0, NULL, 0));
128 }
129 
130 static inline cap_channel_t *
131 fileargs_unwrap(fileargs_t *fa, int *fdflags)
132 {
133 
134 	if (fdflags != NULL) {
135 		*fdflags = fa->fa_flags;
136 	}
137 	fileargs_free(fa);
138 	return (cap_init());
139 }
140 
141 #endif
142 
143 #endif	/* !_FILEARGS_H_ */
144