15b38a427SRobert Watson /*- 25b38a427SRobert Watson * Copyright (c) 2002 Networks Associates Technology, Inc. 35b38a427SRobert Watson * All rights reserved. 45b38a427SRobert Watson * 55b38a427SRobert Watson * This software was developed for the FreeBSD Project by Network 65b38a427SRobert Watson * Associates Laboratories, the Security Research Division of Network 75b38a427SRobert Watson * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 85b38a427SRobert Watson * ("CBOSS"), as part of the DARPA CHATS research program. 95b38a427SRobert Watson * 105b38a427SRobert Watson * Redistribution and use in source and binary forms, with or without 115b38a427SRobert Watson * modification, are permitted provided that the following conditions 125b38a427SRobert Watson * are met: 135b38a427SRobert Watson * 1. Redistributions of source code must retain the above copyright 145b38a427SRobert Watson * notice, this list of conditions and the following disclaimer. 155b38a427SRobert Watson * 2. Redistributions in binary form must reproduce the above copyright 165b38a427SRobert Watson * notice, this list of conditions and the following disclaimer in the 175b38a427SRobert Watson * documentation and/or other materials provided with the distribution. 185b38a427SRobert Watson * 3. The names of the authors may not be used to endorse or promote 195b38a427SRobert Watson * products derived from this software without specific prior written 205b38a427SRobert Watson * permission. 215b38a427SRobert Watson * 225b38a427SRobert Watson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 235b38a427SRobert Watson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 245b38a427SRobert Watson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 255b38a427SRobert Watson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 265b38a427SRobert Watson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 275b38a427SRobert Watson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 285b38a427SRobert Watson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 295b38a427SRobert Watson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 305b38a427SRobert Watson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 315b38a427SRobert Watson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 325b38a427SRobert Watson * SUCH DAMAGE. 335b38a427SRobert Watson * 345b38a427SRobert Watson * $FreeBSD$ 355b38a427SRobert Watson */ 365b38a427SRobert Watson #include <sys/types.h> 375b38a427SRobert Watson #include <sys/mac.h> 385b38a427SRobert Watson 395b38a427SRobert Watson #include <err.h> 405b38a427SRobert Watson #include <paths.h> 415b38a427SRobert Watson #include <stdio.h> 425b38a427SRobert Watson #include <stdlib.h> 435b38a427SRobert Watson #include <string.h> 445b38a427SRobert Watson #include <sysexits.h> 455b38a427SRobert Watson #include <unistd.h> 465b38a427SRobert Watson 475b38a427SRobert Watson #define MAXELEMENTS 32 485b38a427SRobert Watson 493e7cb94cSXin LI static void 505b38a427SRobert Watson usage(void) 515b38a427SRobert Watson { 525b38a427SRobert Watson 535b38a427SRobert Watson fprintf(stderr, "getpmac [-l list,of,labels] [-p pid]\n"); 545b38a427SRobert Watson exit (EX_USAGE); 555b38a427SRobert Watson } 565b38a427SRobert Watson 575b38a427SRobert Watson int 585b38a427SRobert Watson main(int argc, char *argv[]) 595b38a427SRobert Watson { 605cfe0423SPeter Grehan char *labellist, *string; 615b38a427SRobert Watson mac_t label; 625b38a427SRobert Watson pid_t pid; 635cfe0423SPeter Grehan int ch, error, pid_set; 645b38a427SRobert Watson 655b38a427SRobert Watson pid_set = 0; 665b38a427SRobert Watson pid = 0; 675b38a427SRobert Watson labellist = NULL; 685b38a427SRobert Watson while ((ch = getopt(argc, argv, "l:p:")) != -1) { 695b38a427SRobert Watson switch (ch) { 705b38a427SRobert Watson case 'l': 715b38a427SRobert Watson if (labellist != NULL) 725b38a427SRobert Watson usage(); 735b38a427SRobert Watson labellist = argv[optind - 1]; 745b38a427SRobert Watson break; 755b38a427SRobert Watson case 'p': 765b38a427SRobert Watson if (pid_set) 775b38a427SRobert Watson usage(); 785b38a427SRobert Watson pid = atoi(argv[optind - 1]); 795b38a427SRobert Watson pid_set = 1; 805b38a427SRobert Watson break; 815b38a427SRobert Watson default: 825b38a427SRobert Watson usage(); 835b38a427SRobert Watson } 845b38a427SRobert Watson 855b38a427SRobert Watson } 865b38a427SRobert Watson 875b38a427SRobert Watson argc -= optind; 885b38a427SRobert Watson argv += optind; 895b38a427SRobert Watson 905b38a427SRobert Watson if (argc != 0) 915b38a427SRobert Watson usage(); 925b38a427SRobert Watson 935b38a427SRobert Watson if (labellist != NULL) 945b38a427SRobert Watson error = mac_prepare(&label, labellist); 955b38a427SRobert Watson else 965b38a427SRobert Watson error = mac_prepare_process_label(&label); 975b38a427SRobert Watson if (error != 0) { 985b38a427SRobert Watson perror("mac_prepare"); 995b38a427SRobert Watson return (-1); 1005b38a427SRobert Watson } 1015b38a427SRobert Watson 1025b38a427SRobert Watson if (pid_set) { 1035b38a427SRobert Watson error = mac_get_pid(pid, label); 1045b38a427SRobert Watson if (error) 1055b38a427SRobert Watson perror("mac_get_pid"); 106b2beb67fSRobert Watson } else { 1075b38a427SRobert Watson error = mac_get_proc(label); 1085b38a427SRobert Watson if (error) 1095b38a427SRobert Watson perror("mac_get_proc"); 1105b38a427SRobert Watson } 1115b38a427SRobert Watson if (error) { 1125b38a427SRobert Watson mac_free(label); 1135b38a427SRobert Watson exit (-1); 1145b38a427SRobert Watson } 1155b38a427SRobert Watson error = mac_to_text(label, &string); 1165b38a427SRobert Watson if (error != 0) { 1175b38a427SRobert Watson perror("mac_to_text"); 1185b38a427SRobert Watson exit(EX_DATAERR); 1195b38a427SRobert Watson } 1205b38a427SRobert Watson 1215e1f7b80SRobert Watson if (strlen(string) > 0) 1225b38a427SRobert Watson printf("%s\n", string); 1235b38a427SRobert Watson 1245b38a427SRobert Watson mac_free(label); 1255b38a427SRobert Watson free(string); 1265b38a427SRobert Watson exit(EX_OK); 1275b38a427SRobert Watson } 128