1105756Srwatson/*-
2330449Seadler * SPDX-License-Identifier: BSD-3-Clause
3330449Seadler *
4105756Srwatson * Copyright (c) 2002 Networks Associates Technology, Inc.
5105756Srwatson * All rights reserved.
6105756Srwatson *
7105756Srwatson * This software was developed for the FreeBSD Project by Network
8105756Srwatson * Associates Laboratories, the Security Research Division of Network
9105756Srwatson * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
10105756Srwatson * ("CBOSS"), as part of the DARPA CHATS research program.
11105756Srwatson *
12105756Srwatson * Redistribution and use in source and binary forms, with or without
13105756Srwatson * modification, are permitted provided that the following conditions
14105756Srwatson * are met:
15105756Srwatson * 1. Redistributions of source code must retain the above copyright
16105756Srwatson *    notice, this list of conditions and the following disclaimer.
17105756Srwatson * 2. Redistributions in binary form must reproduce the above copyright
18105756Srwatson *    notice, this list of conditions and the following disclaimer in the
19105756Srwatson *    documentation and/or other materials provided with the distribution.
20105756Srwatson * 3. The names of the authors may not be used to endorse or promote
21105756Srwatson *    products derived from this software without specific prior written
22105756Srwatson *    permission.
23105756Srwatson *
24105756Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25105756Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26105756Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27105756Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28105756Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29105756Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30105756Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31105756Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32105756Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33105756Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34105756Srwatson * SUCH DAMAGE.
35105756Srwatson *
36105756Srwatson * $FreeBSD: stable/11/usr.sbin/getpmac/getpmac.c 330449 2018-03-05 07:26:05Z eadler $
37105756Srwatson */
38105756Srwatson#include <sys/types.h>
39105756Srwatson#include <sys/mac.h>
40105756Srwatson
41105756Srwatson#include <err.h>
42105756Srwatson#include <paths.h>
43105756Srwatson#include <stdio.h>
44105756Srwatson#include <stdlib.h>
45105756Srwatson#include <string.h>
46105756Srwatson#include <sysexits.h>
47105756Srwatson#include <unistd.h>
48105756Srwatson
49105756Srwatson#define	MAXELEMENTS	32
50105756Srwatson
51140907Sdelphijstatic void
52105756Srwatsonusage(void)
53105756Srwatson{
54105756Srwatson
55105756Srwatson	fprintf(stderr, "getpmac [-l list,of,labels] [-p pid]\n");
56105756Srwatson	exit (EX_USAGE);
57105756Srwatson}
58105756Srwatson
59105756Srwatsonint
60105756Srwatsonmain(int argc, char *argv[])
61105756Srwatson{
62124830Sgrehan	char *labellist, *string;
63105756Srwatson	mac_t label;
64105756Srwatson	pid_t pid;
65124830Sgrehan	int ch, error, pid_set;
66105756Srwatson
67105756Srwatson	pid_set = 0;
68105756Srwatson	pid = 0;
69105756Srwatson	labellist = NULL;
70105756Srwatson	while ((ch = getopt(argc, argv, "l:p:")) != -1) {
71105756Srwatson		switch (ch) {
72105756Srwatson		case 'l':
73105756Srwatson			if (labellist != NULL)
74105756Srwatson				usage();
75105756Srwatson			labellist = argv[optind - 1];
76105756Srwatson			break;
77105756Srwatson		case 'p':
78105756Srwatson			if (pid_set)
79105756Srwatson				usage();
80105756Srwatson			pid = atoi(argv[optind - 1]);
81105756Srwatson			pid_set = 1;
82105756Srwatson			break;
83105756Srwatson		default:
84105756Srwatson			usage();
85105756Srwatson		}
86105756Srwatson
87105756Srwatson	}
88105756Srwatson
89105756Srwatson	argc -= optind;
90105756Srwatson	argv += optind;
91105756Srwatson
92105756Srwatson	if (argc != 0)
93105756Srwatson		usage();
94105756Srwatson
95105756Srwatson	if (labellist != NULL)
96105756Srwatson		error = mac_prepare(&label, labellist);
97105756Srwatson	else
98105756Srwatson		error = mac_prepare_process_label(&label);
99105756Srwatson	if (error != 0) {
100105756Srwatson		perror("mac_prepare");
101105756Srwatson		return (-1);
102105756Srwatson	}
103105756Srwatson
104105756Srwatson	if (pid_set) {
105105756Srwatson		error = mac_get_pid(pid, label);
106105756Srwatson		if (error)
107105756Srwatson			perror("mac_get_pid");
108195971Srwatson	} else {
109105756Srwatson		error = mac_get_proc(label);
110105756Srwatson		if (error)
111105756Srwatson			perror("mac_get_proc");
112105756Srwatson	}
113105756Srwatson	if (error) {
114105756Srwatson		mac_free(label);
115105756Srwatson		exit (-1);
116105756Srwatson	}
117105756Srwatson	error = mac_to_text(label, &string);
118105756Srwatson	if (error != 0) {
119105756Srwatson		perror("mac_to_text");
120105756Srwatson		exit(EX_DATAERR);
121105756Srwatson	}
122105756Srwatson
123194555Srwatson	if (strlen(string) > 0)
124194555Srwatson		printf("%s\n", string);
125105756Srwatson
126105756Srwatson	mac_free(label);
127105756Srwatson	free(string);
128105756Srwatson	exit(EX_OK);
129105756Srwatson}
130