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