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