setfmac.c revision 105756
1270031Spfg/*-
254820Speter * Copyright (c) 2002 Networks Associates Technology, Inc.
354820Speter * All rights reserved.
454820Speter *
554820Speter * This software was developed for the FreeBSD Project by NAI Labs, the
654820Speter * Security Research Division of Network Associates, Inc. under
754820Speter * DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA
854820Speter * CHATS research program.
954820Speter *
1054820Speter * Redistribution and use in source and binary forms, with or without
1154820Speter * modification, are permitted provided that the following conditions
1254820Speter * are met:
1354820Speter * 1. Redistributions of source code must retain the above copyright
1454820Speter *    notice, this list of conditions and the following disclaimer.
1554820Speter * 2. Redistributions in binary form must reproduce the above copyright
1654820Speter *    notice, this list of conditions and the following disclaimer in the
1754820Speter *    documentation and/or other materials provided with the distribution.
1854820Speter * 3. The names of the authors may not be used to endorse or promote
1954820Speter *    products derived from this software without specific prior written
2054820Speter *    permission.
2154820Speter *
2254820Speter * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2354820Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2454820Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2554820Speter * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2654820Speter * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2754820Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2854820Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2954820Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3054820Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3154820Speter * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3254820Speter * SUCH DAMAGE.
3384225Sdillon *
3454820Speter * $FreeBSD: head/usr.sbin/setfmac/setfmac.c 105756 2002-10-23 03:15:24Z rwatson $
3555227Speter */
3654820Speter#include <sys/types.h>
3754820Speter#include <sys/mac.h>
3854820Speter
3954820Speter#include <err.h>
4054820Speter#include <paths.h>
4155227Speter#include <stdio.h>
4254820Speter#include <stdlib.h>
4392917Sobrien#include <string.h>
4454820Speter#include <sysexits.h>
4554820Speter#include <unistd.h>
4654820Speter
4754820Speter#define	MAXELEMENTS	32
4854820Speter
4954820Spetervoid
50121193Smarkmusage(void)
5154820Speter{
5254820Speter
5354820Speter	fprintf(stderr, "setfmac [-h] [label] [file1] [file2 ...]\n");
5454820Speter	exit (EX_USAGE);
5555227Speter}
5654820Speter
5754820Speterint
5855227Spetermain(int argc, char *argv[])
5954820Speter{
6054820Speter	char ch;
6154820Speter	mac_t label;
62270031Spfg	int hflag;
6354820Speter	int error, i;
6454820Speter
6554820Speter	hflag = 0;
6654820Speter	while ((ch = getopt(argc, argv, "h")) != -1) {
6754820Speter		switch (ch) {
6854820Speter		case 'h':
6954820Speter			hflag = 1;
7054820Speter			break;
7154820Speter		default:
7254820Speter			usage();
7354820Speter		}
7454820Speter	}
7554820Speter
7654820Speter	argv += optind;
7754820Speter	argc -= optind;
7854820Speter
79121193Smarkm	if (argc < 2)
8054820Speter		usage();
8154820Speter
8254820Speter	error = mac_from_text(&label, argv[0]);
8354820Speter	if (error != 0) {
8454820Speter		perror("mac_from_text");
8554820Speter		return (-1);
8654820Speter	}
8754820Speter
8854820Speter	for (i = 1; i < argc; i++) {
8955227Speter		if (hflag)
9054820Speter			error = mac_set_link(argv[i], label);
9155227Speter		else
9254820Speter			error = mac_set_file(argv[i], label);
9354820Speter		if (error != 0) {
9454820Speter			perror(argv[i]);
9554820Speter			return (-1);
9654820Speter		}
9754820Speter
9854820Speter	}
9954820Speter
10054820Speter	mac_free(label);
10154820Speter
10254820Speter	exit(EX_OK);
10354820Speter}
10454820Speter