Blame netlabelctl/mgmt.c

Packit 51d0f7
/*
Packit 51d0f7
 * Management Functions
Packit 51d0f7
 *
Packit 51d0f7
 * Author: Paul Moore <paul@paul-moore.com>
Packit 51d0f7
 *
Packit 51d0f7
 */
Packit 51d0f7
Packit 51d0f7
/*
Packit 51d0f7
 * (c) Copyright Hewlett-Packard Development Company, L.P., 2006
Packit 51d0f7
 *
Packit 51d0f7
 * This program is free software: you can redistribute it and/or modify
Packit 51d0f7
 * it under the terms of version 2 of the GNU General Public License as
Packit 51d0f7
 * published by the Free Software Foundation.
Packit 51d0f7
 *
Packit 51d0f7
 * This program is distributed in the hope that it will be useful,
Packit 51d0f7
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 51d0f7
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 51d0f7
 * GNU General Public License for more details.
Packit 51d0f7
 *
Packit 51d0f7
 * You should have received a copy of the GNU General Public License
Packit 51d0f7
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Packit 51d0f7
 *
Packit 51d0f7
 */
Packit 51d0f7
Packit 51d0f7
#include <stdlib.h>
Packit 51d0f7
#include <stdio.h>
Packit 51d0f7
#include <string.h>
Packit 51d0f7
#include <errno.h>
Packit 51d0f7
Packit 51d0f7
#include <libnetlabel.h>
Packit 51d0f7
Packit 51d0f7
#include "netlabelctl.h"
Packit 51d0f7
Packit 51d0f7
/**
Packit 51d0f7
 * Display a list of the kernel's NetLabel protocols
Packit 51d0f7
 *
Packit 51d0f7
 * Request the kernel's supported NetLabel protocols and display the list to
Packit 51d0f7
 * the user.  Returns zero on success, negative values on failure.
Packit 51d0f7
 *
Packit 51d0f7
 */
Packit 51d0f7
static int mgmt_protocols(void)
Packit 51d0f7
{
Packit 51d0f7
	int rc;
Packit 51d0f7
	nlbl_proto *list = NULL;
Packit 51d0f7
	size_t count;
Packit 51d0f7
	uint32_t iter;
Packit 51d0f7
Packit 51d0f7
	rc = nlbl_mgmt_protocols(NULL, &list);
Packit 51d0f7
	if (rc < 0)
Packit 51d0f7
		return rc;
Packit 51d0f7
	count = rc;
Packit 51d0f7
Packit 51d0f7
	printf(MSG("NetLabel protocols : "));
Packit 51d0f7
	for (iter = 0; iter < count; iter++) {
Packit 51d0f7
		switch (list[iter]) {
Packit 51d0f7
		case NETLBL_NLTYPE_UNLABELED:
Packit 51d0f7
			printf("UNLABELED");
Packit 51d0f7
			break;
Packit 51d0f7
		case NETLBL_NLTYPE_RIPSO:
Packit 51d0f7
			printf("RIPSO");
Packit 51d0f7
			break;
Packit 51d0f7
		case NETLBL_NLTYPE_CIPSOV4:
Packit 51d0f7
			/* preserve "CIPSOv4" for any scripts */
Packit 51d0f7
			if (opt_pretty)
Packit 51d0f7
				printf("CIPSO");
Packit 51d0f7
			else
Packit 51d0f7
				printf("CIPSOv4");
Packit 51d0f7
			break;
Packit 51d0f7
		case NETLBL_NLTYPE_CIPSOV6:
Packit 51d0f7
			printf("CIPSOv6");
Packit 51d0f7
			break;
Packit 51d0f7
		case NETLBL_NLTYPE_CALIPSO:
Packit 51d0f7
			printf("CALIPSO");
Packit 51d0f7
			break;
Packit 51d0f7
		default:
Packit 51d0f7
			printf("UNKNOWN(%u)", list[iter]);
Packit 51d0f7
			break;
Packit 51d0f7
		}
Packit 51d0f7
		if (iter + 1 < count)
Packit 51d0f7
			printf("%s", (opt_pretty ? " " : ","));
Packit 51d0f7
	}
Packit 51d0f7
	printf("\n");
Packit 51d0f7
Packit 51d0f7
	if (list != NULL)
Packit 51d0f7
		free(list);
Packit 51d0f7
	return 0;
Packit 51d0f7
}
Packit 51d0f7
Packit 51d0f7
/**
Packit 51d0f7
 * Display the kernel's NetLabel version
Packit 51d0f7
 *
Packit 51d0f7
 * Request the kernel's NetLabel version string and display it to the user.
Packit 51d0f7
 * Returns zero on success, negative values on failure.
Packit 51d0f7
 *
Packit 51d0f7
 */
Packit 51d0f7
static int mgmt_version(void)
Packit 51d0f7
{
Packit 51d0f7
	int rc;
Packit 51d0f7
	uint32_t kernel_ver;
Packit 51d0f7
Packit 51d0f7
	rc = nlbl_mgmt_version(NULL, &kernel_ver);
Packit 51d0f7
	if (rc < 0)
Packit 51d0f7
		return rc;
Packit 51d0f7
Packit 51d0f7
	if (opt_pretty != 0) {
Packit 51d0f7
		printf("Supported NetLabel protocol versions\n"
Packit 51d0f7
		       "  kernel : %u\n"
Packit 51d0f7
		       "  %s : %u\n",
Packit 51d0f7
		       kernel_ver, nlctl_name, NETLBL_PROTO_VERSION);
Packit 51d0f7
	} else
Packit 51d0f7
		printf("%u\n", kernel_ver);
Packit 51d0f7
Packit 51d0f7
	return 0;
Packit 51d0f7
}
Packit 51d0f7
Packit 51d0f7
/**
Packit 51d0f7
 * Entry point for the NetLabel management functions
Packit 51d0f7
 * @param argc the number of arguments
Packit 51d0f7
 * @param argv the argument list
Packit 51d0f7
 *
Packit 51d0f7
 * Description:
Packit 51d0f7
 * Parses the argument list and performs the requested operation.  Returns zero
Packit 51d0f7
 * on success, negative values on failure.
Packit 51d0f7
 *
Packit 51d0f7
 */
Packit 51d0f7
int mgmt_main(int argc, char *argv[])
Packit 51d0f7
{
Packit 51d0f7
	int rc;
Packit 51d0f7
Packit 51d0f7
	/* sanity checks */
Packit 51d0f7
	if (argc <= 0 || argv == NULL || argv[0] == NULL)
Packit 51d0f7
		return -EINVAL;
Packit 51d0f7
Packit 51d0f7
	/* handle the request */
Packit 51d0f7
	if (strcmp(argv[0], "version") == 0) {
Packit 51d0f7
		/* kernel version */
Packit 51d0f7
		rc = mgmt_version();
Packit 51d0f7
	} else if (strcmp(argv[0], "protocols") == 0) {
Packit 51d0f7
		/* module list */
Packit 51d0f7
		rc = mgmt_protocols();
Packit 51d0f7
	} else {
Packit 51d0f7
		/* unknown request */
Packit 51d0f7
		rc = -EINVAL;
Packit 51d0f7
	}
Packit 51d0f7
Packit 51d0f7
	return rc;
Packit 51d0f7
}