1 /*- 2 * Copyright (c) 2002, 2004 Networks Associates Technology, Inc. 3 * All rights reserved. 4 * 5 * This software was developed for the FreeBSD Project by NAI Labs, the 6 * Security Research Division of Network Associates, Inc. under 7 * DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA 8 * CHATS research program. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include <sys/param.h> 36 #include <sys/errno.h> 37 #include <sys/time.h> 38 #include <sys/sysctl.h> 39 40 #include <security/mac_bsdextended/mac_bsdextended.h> 41 42 #include <err.h> 43 #include <stdio.h> 44 #include <stdlib.h> 45 #include <string.h> 46 #include <ugidfw.h> 47 48 void add_rule(int argc, char *argv[]); 49 void list_rules(void); 50 void remove_rule(int argc, char *argv[]); 51 void set_rule(int argc, char *argv[]); 52 void usage(void); 53 54 void 55 usage(void) 56 { 57 58 fprintf(stderr, "usage: ugidfw add [subject [not] [uid uid] [gid gid]]" 59 " [object [not] [uid uid] \\\n"); 60 fprintf(stderr, " [gid gid]] mode arswxn\n"); 61 fprintf(stderr, " ugidfw list\n"); 62 fprintf(stderr, " ugidfw set rulenum [subject [not] [uid uid] [gid gid]]" 63 " [object [not] \\\n"); 64 fprintf(stderr, " [uid uid] [gid gid]] mode arswxn\n"); 65 fprintf(stderr, " ugidfw remove rulenum\n"); 66 67 exit(1); 68 } 69 70 void 71 add_rule(int argc, char *argv[]) 72 { 73 char errstr[BUFSIZ]; 74 struct mac_bsdextended_rule rule; 75 int error, rulenum; 76 77 error = bsde_parse_rule(argc, argv, &rule, BUFSIZ, errstr); 78 if (error) { 79 warnx("%s", errstr); 80 return; 81 } 82 83 error = bsde_add_rule(&rulenum, &rule, BUFSIZ, errstr); 84 if (error) { 85 warnx("%s", errstr); 86 return; 87 } 88 printf("Added rule %d\n", rulenum); 89 } 90 91 void 92 list_rules(void) 93 { 94 char errstr[BUFSIZ], charstr[BUFSIZ]; 95 struct mac_bsdextended_rule rule; 96 int error, i, rule_count, rule_slots; 97 98 rule_slots = bsde_get_rule_slots(BUFSIZ, errstr); 99 if (rule_slots == -1) { 100 warnx("unable to get rule slots; mac_bsdextended.ko " 101 "may not be loaded"); 102 errx(1, "bsde_get_rule_slots: %s", errstr); 103 } 104 105 rule_count = bsde_get_rule_count(BUFSIZ, errstr); 106 if (rule_count == -1) 107 errx(1, "bsde_get_rule_count: %s", errstr); 108 109 printf("%d slots, %d rules\n", rule_slots, rule_count); 110 111 for (i = 0; i <= rule_slots; i++) { 112 error = bsde_get_rule(i, &rule, BUFSIZ, errstr); 113 switch (error) { 114 case -2: 115 continue; 116 case -1: 117 warnx("rule %d: %s", i, errstr); 118 continue; 119 case 0: 120 break; 121 } 122 123 if (bsde_rule_to_string(&rule, charstr, BUFSIZ) == -1) 124 warnx("unable to translate rule %d to string", i); 125 else 126 printf("%d %s\n", i, charstr); 127 } 128 } 129 130 void 131 set_rule(int argc, char *argv[]) 132 { 133 char errstr[BUFSIZ]; 134 struct mac_bsdextended_rule rule; 135 long value; 136 int error, rulenum; 137 char *endp; 138 139 if (argc < 1) 140 usage(); 141 142 value = strtol(argv[0], &endp, 10); 143 if (*endp != '\0') 144 usage(); 145 146 if ((long) value != (int) value || value < 0) 147 usage(); 148 149 rulenum = value; 150 151 error = bsde_parse_rule(argc - 1, argv + 1, &rule, BUFSIZ, errstr); 152 if (error) { 153 warnx("%s", errstr); 154 return; 155 } 156 157 error = bsde_set_rule(rulenum, &rule, BUFSIZ, errstr); 158 if (error) { 159 warnx("%s", errstr); 160 return; 161 } 162 } 163 164 void 165 remove_rule(int argc, char *argv[]) 166 { 167 char errstr[BUFSIZ]; 168 long value; 169 int error, rulenum; 170 char *endp; 171 172 if (argc != 1) 173 usage(); 174 175 value = strtol(argv[0], &endp, 10); 176 if (*endp != '\0') 177 usage(); 178 179 if ((long) value != (int) value || value < 0) 180 usage(); 181 182 rulenum = value; 183 184 error = bsde_delete_rule(rulenum, BUFSIZ, errstr); 185 if (error) 186 warnx("%s", errstr); 187 } 188 189 int 190 main(int argc, char *argv[]) 191 { 192 193 if (argc < 2) 194 usage(); 195 196 if (strcmp("add", argv[1]) == 0) { 197 add_rule(argc-2, argv+2); 198 } else if (strcmp("list", argv[1]) == 0) { 199 if (argc != 2) 200 usage(); 201 list_rules(); 202 } else if (strcmp("set", argv[1]) == 0) { 203 set_rule(argc-2, argv+2); 204 } else if (strcmp("remove", argv[1]) == 0) { 205 remove_rule(argc-2, argv+2); 206 } else 207 usage(); 208 209 return (0); 210 } 211