ugidfw.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause
  3. *
  4. * Copyright (c) 2002, 2004 Networks Associates Technology, Inc.
  5. * All rights reserved.
  6. *
  7. * This software was developed for the FreeBSD Project by NAI Labs, the
  8. * Security Research Division of Network Associates, Inc. under
  9. * DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA
  10. * CHATS research program.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. */
  33. #include <sys/param.h>
  34. #include <sys/errno.h>
  35. #include <sys/mount.h>
  36. #include <sys/time.h>
  37. #include <sys/sysctl.h>
  38. #include <security/mac_bsdextended/mac_bsdextended.h>
  39. #include <err.h>
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <ugidfw.h>
  44. void add_rule(int argc, char *argv[]);
  45. void list_rules(void);
  46. void remove_rule(int argc, char *argv[]);
  47. void set_rule(int argc, char *argv[]);
  48. void usage(void);
  49. void
  50. usage(void)
  51. {
  52. fprintf(stderr, "usage: ugidfw add [subject [not] [uid uid] [gid gid]]"
  53. " [object [not] [uid uid] \\\n");
  54. fprintf(stderr, " [gid gid]] mode arswxn\n");
  55. fprintf(stderr, " ugidfw list\n");
  56. fprintf(stderr, " ugidfw set rulenum [subject [not] [uid uid] [gid gid]]"
  57. " [object [not] \\\n");
  58. fprintf(stderr, " [uid uid] [gid gid]] mode arswxn\n");
  59. fprintf(stderr, " ugidfw remove rulenum\n");
  60. exit(1);
  61. }
  62. void
  63. add_rule(int argc, char *argv[])
  64. {
  65. char errstr[BUFSIZ], charstr[BUFSIZ];
  66. struct mac_bsdextended_rule rule;
  67. int error, rulenum;
  68. error = bsde_parse_rule(argc, argv, &rule, BUFSIZ, errstr);
  69. if (error) {
  70. warnx("%s", errstr);
  71. return;
  72. }
  73. error = bsde_add_rule(&rulenum, &rule, BUFSIZ, errstr);
  74. if (error) {
  75. warnx("%s", errstr);
  76. return;
  77. }
  78. if (bsde_rule_to_string(&rule, charstr, BUFSIZ) == -1)
  79. warnx("Added rule, but unable to print string.");
  80. else
  81. printf("%d %s\n", rulenum, charstr);
  82. }
  83. void
  84. list_rules(void)
  85. {
  86. char errstr[BUFSIZ], charstr[BUFSIZ];
  87. struct mac_bsdextended_rule rule;
  88. int error, i, rule_count, rule_slots;
  89. rule_slots = bsde_get_rule_slots(BUFSIZ, errstr);
  90. if (rule_slots == -1) {
  91. warnx("unable to get rule slots; mac_bsdextended.ko "
  92. "may not be loaded");
  93. errx(1, "bsde_get_rule_slots: %s", errstr);
  94. }
  95. rule_count = bsde_get_rule_count(BUFSIZ, errstr);
  96. if (rule_count == -1)
  97. errx(1, "bsde_get_rule_count: %s", errstr);
  98. printf("%d slots, %d rules\n", rule_slots, rule_count);
  99. for (i = 0; i < rule_slots; i++) {
  100. error = bsde_get_rule(i, &rule, BUFSIZ, errstr);
  101. switch (error) {
  102. case -2:
  103. continue;
  104. case -1:
  105. warnx("rule %d: %s", i, errstr);
  106. continue;
  107. case 0:
  108. break;
  109. }
  110. if (bsde_rule_to_string(&rule, charstr, BUFSIZ) == -1)
  111. warnx("unable to translate rule %d to string", i);
  112. else
  113. printf("%d %s\n", i, charstr);
  114. }
  115. }
  116. void
  117. set_rule(int argc, char *argv[])
  118. {
  119. char errstr[BUFSIZ];
  120. struct mac_bsdextended_rule rule;
  121. long value;
  122. int error, rulenum;
  123. char *endp;
  124. if (argc < 1)
  125. usage();
  126. value = strtol(argv[0], &endp, 10);
  127. if (*endp != '\0')
  128. usage();
  129. if ((long) value != (int) value || value < 0)
  130. usage();
  131. rulenum = value;
  132. error = bsde_parse_rule(argc - 1, argv + 1, &rule, BUFSIZ, errstr);
  133. if (error) {
  134. warnx("%s", errstr);
  135. return;
  136. }
  137. error = bsde_set_rule(rulenum, &rule, BUFSIZ, errstr);
  138. if (error) {
  139. warnx("%s", errstr);
  140. return;
  141. }
  142. }
  143. void
  144. remove_rule(int argc, char *argv[])
  145. {
  146. char errstr[BUFSIZ];
  147. long value;
  148. int error, rulenum;
  149. char *endp;
  150. if (argc != 1)
  151. usage();
  152. value = strtol(argv[0], &endp, 10);
  153. if (*endp != '\0')
  154. usage();
  155. if ((long) value != (int) value || value < 0)
  156. usage();
  157. rulenum = value;
  158. error = bsde_delete_rule(rulenum, BUFSIZ, errstr);
  159. if (error)
  160. warnx("%s", errstr);
  161. }
  162. int
  163. main(int argc, char *argv[])
  164. {
  165. if (argc < 2)
  166. usage();
  167. if (strcmp("add", argv[1]) == 0) {
  168. add_rule(argc-2, argv+2);
  169. } else if (strcmp("list", argv[1]) == 0) {
  170. if (argc != 2)
  171. usage();
  172. list_rules();
  173. } else if (strcmp("set", argv[1]) == 0) {
  174. set_rule(argc-2, argv+2);
  175. } else if (strcmp("remove", argv[1]) == 0) {
  176. remove_rule(argc-2, argv+2);
  177. } else
  178. usage();
  179. return (0);
  180. }