1ca0716f5SRobert Watson /*- 2bb97b418SRobert Watson * Copyright (c) 2005-2006 Robert N. M. Watson 3ca0716f5SRobert Watson * All rights reserved. 4ca0716f5SRobert Watson * 5ca0716f5SRobert Watson * Redistribution and use in source and binary forms, with or without 6ca0716f5SRobert Watson * modification, are permitted provided that the following conditions 7ca0716f5SRobert Watson * are met: 8ca0716f5SRobert Watson * 1. Redistributions of source code must retain the above copyright 9ca0716f5SRobert Watson * notice, this list of conditions and the following disclaimer. 10ca0716f5SRobert Watson * 2. Redistributions in binary form must reproduce the above copyright 11ca0716f5SRobert Watson * notice, this list of conditions and the following disclaimer in the 12ca0716f5SRobert Watson * documentation and/or other materials provided with the distribution. 13ca0716f5SRobert Watson * 14ca0716f5SRobert Watson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15ca0716f5SRobert Watson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16ca0716f5SRobert Watson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17ca0716f5SRobert Watson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18ca0716f5SRobert Watson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19ca0716f5SRobert Watson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20ca0716f5SRobert Watson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21ca0716f5SRobert Watson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22ca0716f5SRobert Watson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23ca0716f5SRobert Watson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24ca0716f5SRobert Watson * SUCH DAMAGE. 25ca0716f5SRobert Watson * 26bb97b418SRobert Watson * $P4: //depot/projects/trustedbsd/openbsm/tools/audump.c#6 $ 27ca0716f5SRobert Watson */ 28ca0716f5SRobert Watson 29ca0716f5SRobert Watson #include <bsm/libbsm.h> 30ca0716f5SRobert Watson #include <string.h> 31ca0716f5SRobert Watson #include <err.h> 32ca0716f5SRobert Watson #include <limits.h> 33ca0716f5SRobert Watson #include <stdio.h> 34ca0716f5SRobert Watson #include <stdlib.h> 35ca0716f5SRobert Watson 36ca0716f5SRobert Watson /* 37ca0716f5SRobert Watson * Simple tool to dump various /etc/security databases using the defined APIs. 38ca0716f5SRobert Watson */ 39ca0716f5SRobert Watson 40ca0716f5SRobert Watson static void 41ca0716f5SRobert Watson usage(void) 42ca0716f5SRobert Watson { 43ca0716f5SRobert Watson 44f4e380b0SRobert Watson fprintf(stderr, "usage: audump [class|class_r|control|event|event_r|" 45ca0716f5SRobert Watson "user|user_r]\n"); 46ca0716f5SRobert Watson exit(-1); 47ca0716f5SRobert Watson } 48ca0716f5SRobert Watson 49ca0716f5SRobert Watson static void 50ca0716f5SRobert Watson audump_class(void) 51ca0716f5SRobert Watson { 52ca0716f5SRobert Watson au_class_ent_t *cp; 53ca0716f5SRobert Watson 54ca0716f5SRobert Watson while ((cp = getauclassent()) != NULL) 55ca0716f5SRobert Watson printf("0x%08x:%s:%s\n", cp->ac_class, cp->ac_name, 56ca0716f5SRobert Watson cp->ac_desc); 57ca0716f5SRobert Watson } 58ca0716f5SRobert Watson 59ca0716f5SRobert Watson static void 60ca0716f5SRobert Watson audump_class_r(void) 61ca0716f5SRobert Watson { 62ca0716f5SRobert Watson char class_ent_name[AU_CLASS_NAME_MAX]; 63ca0716f5SRobert Watson char class_ent_desc[AU_CLASS_DESC_MAX]; 64ca0716f5SRobert Watson au_class_ent_t c, *cp; 65ca0716f5SRobert Watson 66ca0716f5SRobert Watson bzero(&c, sizeof(c)); 67ca0716f5SRobert Watson bzero(class_ent_name, sizeof(class_ent_name)); 68ca0716f5SRobert Watson bzero(class_ent_desc, sizeof(class_ent_desc)); 69ca0716f5SRobert Watson c.ac_name = class_ent_name; 70ca0716f5SRobert Watson c.ac_desc = class_ent_desc; 71ca0716f5SRobert Watson 72ca0716f5SRobert Watson while ((cp = getauclassent_r(&c)) != NULL) 73ca0716f5SRobert Watson printf("0x%08x:%s:%s\n", cp->ac_class, cp->ac_name, 74ca0716f5SRobert Watson cp->ac_desc); 75ca0716f5SRobert Watson } 76ca0716f5SRobert Watson 77ca0716f5SRobert Watson static void 78ca0716f5SRobert Watson audump_control(void) 79ca0716f5SRobert Watson { 80bb97b418SRobert Watson char string[PATH_MAX], string2[PATH_MAX]; 81ca0716f5SRobert Watson int ret, val; 82bb97b418SRobert Watson long policy; 83ca0716f5SRobert Watson 84ca0716f5SRobert Watson ret = getacflg(string, PATH_MAX); 85ca0716f5SRobert Watson if (ret == -2) 86ca0716f5SRobert Watson err(-1, "getacflg"); 87ca0716f5SRobert Watson if (ret != 0) 88ca0716f5SRobert Watson errx(-1, "getacflg: %d", ret); 89ca0716f5SRobert Watson 90ca0716f5SRobert Watson printf("flags:%s\n", string); 91ca0716f5SRobert Watson 92ca0716f5SRobert Watson ret = getacmin(&val); 93ca0716f5SRobert Watson if (ret == -2) 94ca0716f5SRobert Watson err(-1, "getacmin"); 95ca0716f5SRobert Watson if (ret != 0) 96ca0716f5SRobert Watson errx(-1, "getacmin: %d", ret); 97ca0716f5SRobert Watson 98ca0716f5SRobert Watson printf("min:%d\n", val); 99ca0716f5SRobert Watson 100ca0716f5SRobert Watson ret = getacna(string, PATH_MAX); 101ca0716f5SRobert Watson if (ret == -2) 102ca0716f5SRobert Watson err(-1, "getacna"); 103ca0716f5SRobert Watson if (ret != 0) 104ca0716f5SRobert Watson errx(-1, "getacna: %d", ret); 105ca0716f5SRobert Watson 106ca0716f5SRobert Watson printf("naflags:%s\n", string); 107ca0716f5SRobert Watson 108ca0716f5SRobert Watson setac(); 109ca0716f5SRobert Watson do { 110ca0716f5SRobert Watson ret = getacdir(string, PATH_MAX); 111ca0716f5SRobert Watson if (ret == -1) 112ca0716f5SRobert Watson break; 113ca0716f5SRobert Watson if (ret == -2) 114ca0716f5SRobert Watson err(-1, "getacdir"); 115ca0716f5SRobert Watson if (ret != 0) 116ca0716f5SRobert Watson errx(-1, "getacdir: %d", ret); 117ca0716f5SRobert Watson printf("dir:%s\n", string); 118ca0716f5SRobert Watson 119ca0716f5SRobert Watson } while (ret == 0); 120bb97b418SRobert Watson 121bb97b418SRobert Watson ret = getacpol(string, PATH_MAX); 122bb97b418SRobert Watson if (ret != 0) 123bb97b418SRobert Watson err(-1, "getacpol"); 124bb97b418SRobert Watson if (au_strtopol(string, &policy) < 0) 125bb97b418SRobert Watson err(-1, "au_strtopol"); 126bb97b418SRobert Watson if (au_poltostr(policy, string2, PATH_MAX) < 0) 127bb97b418SRobert Watson err(-1, "au_poltostr"); 128bb97b418SRobert Watson printf("policy:%s\n", string2); 129ca0716f5SRobert Watson } 130ca0716f5SRobert Watson 131ca0716f5SRobert Watson static void 132ca0716f5SRobert Watson printf_classmask(au_class_t classmask) 133ca0716f5SRobert Watson { 134ca0716f5SRobert Watson au_class_ent_t *c; 135ca0716f5SRobert Watson u_int32_t i; 136ca0716f5SRobert Watson int first; 137ca0716f5SRobert Watson 138ca0716f5SRobert Watson first = 1; 139ca0716f5SRobert Watson for (i = 0; i < 32; i++) { 140ca0716f5SRobert Watson if (classmask & (2 << i)) { 141ca0716f5SRobert Watson if (first) 142ca0716f5SRobert Watson first = 0; 143ca0716f5SRobert Watson else 144ca0716f5SRobert Watson printf(","); 145ca0716f5SRobert Watson c = getauclassnum(2 << i); 146ca0716f5SRobert Watson if (c != NULL) 147ca0716f5SRobert Watson printf("%s", c->ac_name); 148ca0716f5SRobert Watson else 149ca0716f5SRobert Watson printf("0x%x", 2 << i); 150ca0716f5SRobert Watson } 151ca0716f5SRobert Watson } 152ca0716f5SRobert Watson } 153ca0716f5SRobert Watson 154ca0716f5SRobert Watson static void 155ca0716f5SRobert Watson audump_event(void) 156ca0716f5SRobert Watson { 157ca0716f5SRobert Watson au_event_ent_t *ep; 158ca0716f5SRobert Watson 159ca0716f5SRobert Watson while ((ep = getauevent()) != NULL) { 160ca0716f5SRobert Watson printf("%d:%s:%s:", ep->ae_number, ep->ae_name, ep->ae_desc); 161ca0716f5SRobert Watson printf_classmask(ep->ae_class); 162ca0716f5SRobert Watson printf("\n"); 163ca0716f5SRobert Watson } 164ca0716f5SRobert Watson } 165ca0716f5SRobert Watson 166ca0716f5SRobert Watson static void 167ca0716f5SRobert Watson audump_event_r(void) 168ca0716f5SRobert Watson { 169ca0716f5SRobert Watson char event_ent_name[AU_EVENT_NAME_MAX]; 170ca0716f5SRobert Watson char event_ent_desc[AU_EVENT_DESC_MAX]; 171ca0716f5SRobert Watson au_event_ent_t e, *ep; 172ca0716f5SRobert Watson 173ca0716f5SRobert Watson bzero(&e, sizeof(e)); 174ca0716f5SRobert Watson bzero(event_ent_name, sizeof(event_ent_name)); 175ca0716f5SRobert Watson bzero(event_ent_desc, sizeof(event_ent_desc)); 176ca0716f5SRobert Watson e.ae_name = event_ent_name; 177ca0716f5SRobert Watson e.ae_desc = event_ent_desc; 178ca0716f5SRobert Watson 179ca0716f5SRobert Watson while ((ep = getauevent_r(&e)) != NULL) { 180ca0716f5SRobert Watson printf("%d:%s:%s:", ep->ae_number, ep->ae_name, ep->ae_desc); 181ca0716f5SRobert Watson printf_classmask(ep->ae_class); 182ca0716f5SRobert Watson printf("\n"); 183ca0716f5SRobert Watson } 184ca0716f5SRobert Watson } 185ca0716f5SRobert Watson 186ca0716f5SRobert Watson static void 187ca0716f5SRobert Watson audump_user(void) 188ca0716f5SRobert Watson { 189ca0716f5SRobert Watson au_user_ent_t *up; 190ca0716f5SRobert Watson 191ca0716f5SRobert Watson while ((up = getauuserent()) != NULL) { 192ca0716f5SRobert Watson printf("%s:", up->au_name); 193ca0716f5SRobert Watson // printf_classmask(up->au_always); 194ca0716f5SRobert Watson printf(":"); 195ca0716f5SRobert Watson // printf_classmask(up->au_never); 196ca0716f5SRobert Watson printf("\n"); 197ca0716f5SRobert Watson } 198ca0716f5SRobert Watson } 199ca0716f5SRobert Watson 200ca0716f5SRobert Watson static void 201ca0716f5SRobert Watson audump_user_r(void) 202ca0716f5SRobert Watson { 203ca0716f5SRobert Watson char user_ent_name[AU_USER_NAME_MAX]; 204ca0716f5SRobert Watson au_user_ent_t u, *up; 205ca0716f5SRobert Watson 206ca0716f5SRobert Watson bzero(&u, sizeof(u)); 207ca0716f5SRobert Watson bzero(user_ent_name, sizeof(user_ent_name)); 208ca0716f5SRobert Watson u.au_name = user_ent_name; 209ca0716f5SRobert Watson 210ca0716f5SRobert Watson while ((up = getauuserent_r(&u)) != NULL) { 211ca0716f5SRobert Watson printf("%s:", up->au_name); 212ca0716f5SRobert Watson // printf_classmask(up->au_always); 213ca0716f5SRobert Watson printf(":"); 214ca0716f5SRobert Watson // printf_classmask(up->au_never); 215ca0716f5SRobert Watson printf("\n"); 216ca0716f5SRobert Watson } 217ca0716f5SRobert Watson } 218ca0716f5SRobert Watson 219ca0716f5SRobert Watson int 220ca0716f5SRobert Watson main(int argc, char *argv[]) 221ca0716f5SRobert Watson { 222ca0716f5SRobert Watson 223ca0716f5SRobert Watson if (argc != 2) 224ca0716f5SRobert Watson usage(); 225ca0716f5SRobert Watson 226ca0716f5SRobert Watson if (strcmp(argv[1], "class") == 0) 227ca0716f5SRobert Watson audump_class(); 228ca0716f5SRobert Watson else if (strcmp(argv[1], "class_r") == 0) 229ca0716f5SRobert Watson audump_class_r(); 230ca0716f5SRobert Watson else if (strcmp(argv[1], "control") == 0) 231ca0716f5SRobert Watson audump_control(); 232ca0716f5SRobert Watson else if (strcmp(argv[1], "event") == 0) 233ca0716f5SRobert Watson audump_event(); 234ca0716f5SRobert Watson else if (strcmp(argv[1], "event_r") == 0) 235ca0716f5SRobert Watson audump_event_r(); 236ca0716f5SRobert Watson else if (strcmp(argv[1], "user") == 0) 237ca0716f5SRobert Watson audump_user(); 238ca0716f5SRobert Watson else if (strcmp(argv[1], "user_r") == 0) 239ca0716f5SRobert Watson audump_user_r(); 240ca0716f5SRobert Watson else 241ca0716f5SRobert Watson usage(); 242ca0716f5SRobert Watson 243ca0716f5SRobert Watson return (0); 244ca0716f5SRobert Watson } 245