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/cdefs.h> 35 #include <sys/dnv.h> 36 #include <sys/nv.h> 37 38 #include <stdbool.h> 39 40 #define FA_OPEN 1 41 #define FA_LSTAT 2 42 43 #ifdef WITH_CASPER 44 struct fileargs; 45 typedef struct fileargs fileargs_t; 46 struct stat; 47 48 __BEGIN_DECLS 49 50 fileargs_t *fileargs_init(int argc, char *argv[], int flags, mode_t mode, 51 cap_rights_t *rightsp, int operations); 52 fileargs_t *fileargs_cinit(cap_channel_t *cas, int argc, char *argv[], 53 int flags, mode_t mode, cap_rights_t *rightsp, int operations); 54 fileargs_t *fileargs_initnv(nvlist_t *limits); 55 fileargs_t *fileargs_cinitnv(cap_channel_t *cas, nvlist_t *limits); 56 int fileargs_lstat(fileargs_t *fa, const char *name, struct stat *sb); 57 int fileargs_open(fileargs_t *fa, const char *name); 58 void fileargs_free(fileargs_t *fa); 59 FILE *fileargs_fopen(fileargs_t *fa, const char *name, const char *mode); 60 61 fileargs_t *fileargs_wrap(cap_channel_t *chan, int fdflags); 62 cap_channel_t *fileargs_unwrap(fileargs_t *fa, int *fdflags); 63 64 __END_DECLS 65 66 #else 67 typedef struct fileargs { 68 int fa_flags; 69 mode_t fa_mode; 70 } fileargs_t; 71 72 static inline fileargs_t * 73 fileargs_init(int argc __unused, char *argv[] __unused, int flags, mode_t mode, 74 cap_rights_t *rightsp __unused, int operations __unused) { 75 fileargs_t *fa; 76 77 fa = malloc(sizeof(*fa)); 78 if (fa != NULL) { 79 fa->fa_flags = flags; 80 fa->fa_mode = mode; 81 } 82 83 return (fa); 84 } 85 86 static inline fileargs_t * 87 fileargs_cinit(cap_channel_t *cas __unused, int argc, char *argv[], int flags, 88 mode_t mode, cap_rights_t *rightsp, int operations) 89 { 90 91 return (fileargs_init(argc, argv, flags, mode, rightsp, operations)); 92 } 93 94 static inline fileargs_t * 95 fileargs_initnv(nvlist_t *limits) 96 { 97 fileargs_t *fa; 98 99 fa = fileargs_init(0, NULL, 100 nvlist_get_number(limits, "flags"), 101 dnvlist_get_number(limits, "mode", 0), 102 NULL, 103 nvlist_get_number(limits, "operations")); 104 nvlist_destroy(limits); 105 106 return (fa); 107 } 108 109 static inline fileargs_t * 110 fileargs_cinitnv(cap_channel_t *cas __unused, nvlist_t *limits) 111 { 112 113 return (fileargs_initnv(limits)); 114 } 115 116 #define fileargs_lstat(fa, name, sb) \ 117 lstat(name, sb) 118 #define fileargs_open(fa, name) \ 119 open(name, fa->fa_flags, fa->fa_mode) 120 static inline 121 FILE *fileargs_fopen(fileargs_t *fa, const char *name, const char *mode) 122 { 123 (void) fa; 124 return (fopen(name, mode)); 125 } 126 #define fileargs_free(fa) (free(fa)) 127 128 static inline fileargs_t * 129 fileargs_wrap(cap_channel_t *chan, int fdflags) 130 { 131 132 cap_close(chan); 133 return (fileargs_init(0, NULL, fdflags, 0, NULL, 0)); 134 } 135 136 static inline cap_channel_t * 137 fileargs_unwrap(fileargs_t *fa, int *fdflags) 138 { 139 140 if (fdflags != NULL) { 141 *fdflags = fa->fa_flags; 142 } 143 fileargs_free(fa); 144 return (cap_init()); 145 } 146 147 #endif 148 149 #endif /* !_FILEARGS_H_ */ 150