|
Packit |
51d0f7 |
/*
|
|
Packit |
51d0f7 |
* CALIPSO Functions
|
|
Packit |
51d0f7 |
*
|
|
Packit |
51d0f7 |
* Author: Paul Moore <paul@paul-moore.com>
|
|
Packit |
51d0f7 |
* Author: Huw Davies <huw@codeweavers.com>
|
|
Packit |
51d0f7 |
*
|
|
Packit |
51d0f7 |
*/
|
|
Packit |
51d0f7 |
|
|
Packit |
51d0f7 |
/*
|
|
Packit |
51d0f7 |
* (c) Copyright Hewlett-Packard Development Company, L.P., 2006
|
|
Packit |
51d0f7 |
* (c) Copyright Huw Davies <huw@codeweavers.com>, 2015
|
|
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 |
|
|
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 |
* Add a CALIPSO label mapping
|
|
Packit |
51d0f7 |
* @param argc the number of arguments
|
|
Packit |
51d0f7 |
* @param argv the argument list
|
|
Packit |
51d0f7 |
*
|
|
Packit |
51d0f7 |
* Add a CALIPSO label mapping to the NetLabel system. Returns zero on
|
|
Packit |
51d0f7 |
* success, negative values on failure.
|
|
Packit |
51d0f7 |
*
|
|
Packit |
51d0f7 |
*/
|
|
Packit |
51d0f7 |
static int calipso_add(int argc, char *argv[])
|
|
Packit |
51d0f7 |
{
|
|
Packit |
51d0f7 |
int rc;
|
|
Packit |
51d0f7 |
uint32_t calipso_type = CALIPSO_MAP_UNKNOWN;
|
|
Packit |
51d0f7 |
uint32_t iter;
|
|
Packit |
51d0f7 |
nlbl_clp_doi doi = 0;
|
|
Packit |
51d0f7 |
|
|
Packit |
51d0f7 |
/* sanity checks */
|
|
Packit |
51d0f7 |
if (argc <= 0 || argv == NULL || argv[0] == NULL)
|
|
Packit |
51d0f7 |
return -EINVAL;
|
|
Packit |
51d0f7 |
|
|
Packit |
51d0f7 |
/* parse the arguments */
|
|
Packit |
51d0f7 |
for (iter = 0; iter < argc && argv[iter] != NULL; iter++) {
|
|
Packit |
51d0f7 |
if (strcmp(argv[iter], "pass") == 0) {
|
|
Packit |
51d0f7 |
calipso_type = CALIPSO_MAP_PASS;
|
|
Packit |
51d0f7 |
} else if (strncmp(argv[iter], "doi:", 4) == 0) {
|
|
Packit |
51d0f7 |
/* doi */
|
|
Packit |
51d0f7 |
doi = atoi(argv[iter] + 4);
|
|
Packit |
51d0f7 |
} else
|
|
Packit |
51d0f7 |
return -EINVAL;
|
|
Packit |
51d0f7 |
}
|
|
Packit |
51d0f7 |
|
|
Packit |
51d0f7 |
/* add the calipso mapping */
|
|
Packit |
51d0f7 |
switch (calipso_type) {
|
|
Packit |
51d0f7 |
case CALIPSO_MAP_PASS:
|
|
Packit |
51d0f7 |
/* pass through mapping */
|
|
Packit |
51d0f7 |
rc = nlbl_calipso_add_pass(NULL, doi);
|
|
Packit |
51d0f7 |
break;
|
|
Packit |
51d0f7 |
default:
|
|
Packit |
51d0f7 |
rc = -EINVAL;
|
|
Packit |
51d0f7 |
}
|
|
Packit |
51d0f7 |
|
|
Packit |
51d0f7 |
return rc;
|
|
Packit |
51d0f7 |
}
|
|
Packit |
51d0f7 |
|
|
Packit |
51d0f7 |
/**
|
|
Packit |
51d0f7 |
* Remove a CALIPSO label mapping
|
|
Packit |
51d0f7 |
* @param argc the number of arguments
|
|
Packit |
51d0f7 |
* @param argv the argument list
|
|
Packit |
51d0f7 |
*
|
|
Packit |
51d0f7 |
* Remove a CALIPSO label mapping from the NetLabel system. Returns zero on
|
|
Packit |
51d0f7 |
* success, negative values on failure.
|
|
Packit |
51d0f7 |
*
|
|
Packit |
51d0f7 |
*/
|
|
Packit |
51d0f7 |
static int calipso_del(int argc, char *argv[])
|
|
Packit |
51d0f7 |
{
|
|
Packit |
51d0f7 |
uint32_t iter;
|
|
Packit |
51d0f7 |
nlbl_clp_doi doi = 0;
|
|
Packit |
51d0f7 |
|
|
Packit |
51d0f7 |
/* sanity checks */
|
|
Packit |
51d0f7 |
if (argc <= 0 || argv == NULL || argv[0] == NULL)
|
|
Packit |
51d0f7 |
return -EINVAL;
|
|
Packit |
51d0f7 |
|
|
Packit |
51d0f7 |
/* parse the arguments */
|
|
Packit |
51d0f7 |
for (iter = 0; iter < argc && argv[iter] != NULL; iter++) {
|
|
Packit |
51d0f7 |
if (strncmp(argv[iter], "doi:", 4) == 0) {
|
|
Packit |
51d0f7 |
/* doi */
|
|
Packit |
51d0f7 |
doi = atoi(argv[iter] + 4);
|
|
Packit |
51d0f7 |
} else
|
|
Packit |
51d0f7 |
return -EINVAL;
|
|
Packit |
51d0f7 |
}
|
|
Packit |
51d0f7 |
|
|
Packit |
51d0f7 |
/* delete the mapping */
|
|
Packit |
51d0f7 |
return nlbl_calipso_del(NULL, doi);
|
|
Packit |
51d0f7 |
}
|
|
Packit |
51d0f7 |
|
|
Packit |
51d0f7 |
/**
|
|
Packit |
51d0f7 |
* List all of the CALIPSO label mappings
|
|
Packit |
51d0f7 |
*
|
|
Packit |
51d0f7 |
* List the configured CALIPSO label mappings. Returns zero on success,
|
|
Packit |
51d0f7 |
* negative values on failure.
|
|
Packit |
51d0f7 |
*
|
|
Packit |
51d0f7 |
*/
|
|
Packit |
51d0f7 |
static int calipso_list_all(void)
|
|
Packit |
51d0f7 |
{
|
|
Packit |
51d0f7 |
int rc;
|
|
Packit |
51d0f7 |
uint32_t iter;
|
|
Packit |
51d0f7 |
nlbl_clp_doi *doi_list = NULL;
|
|
Packit |
51d0f7 |
nlbl_clp_mtype *mtype_list = NULL;
|
|
Packit |
51d0f7 |
size_t count;
|
|
Packit |
51d0f7 |
|
|
Packit |
51d0f7 |
rc = nlbl_calipso_listall(NULL, &doi_list, &mtype_list);
|
|
Packit |
51d0f7 |
if (rc < 0)
|
|
Packit |
51d0f7 |
goto list_all_return;
|
|
Packit |
51d0f7 |
count = rc;
|
|
Packit |
51d0f7 |
|
|
Packit |
51d0f7 |
if (opt_pretty != 0) {
|
|
Packit |
51d0f7 |
printf("Configured CALIPSO mappings (%zu)\n", count);
|
|
Packit |
51d0f7 |
for (iter = 0; iter < count; iter++) {
|
|
Packit |
51d0f7 |
/* doi value */
|
|
Packit |
51d0f7 |
printf(" DOI value : %u\n", doi_list[iter]);
|
|
Packit |
51d0f7 |
/* map type */
|
|
Packit |
51d0f7 |
printf(" mapping type : ");
|
|
Packit |
51d0f7 |
switch (mtype_list[iter]) {
|
|
Packit |
51d0f7 |
case CALIPSO_MAP_PASS:
|
|
Packit |
51d0f7 |
printf("PASS_THROUGH\n");
|
|
Packit |
51d0f7 |
break;
|
|
Packit |
51d0f7 |
default:
|
|
Packit |
51d0f7 |
printf("UNKNOWN(%u)\n", mtype_list[iter]);
|
|
Packit |
51d0f7 |
break;
|
|
Packit |
51d0f7 |
}
|
|
Packit |
51d0f7 |
}
|
|
Packit |
51d0f7 |
} else {
|
|
Packit |
51d0f7 |
for (iter = 0; iter < count; iter++) {
|
|
Packit |
51d0f7 |
/* doi value */
|
|
Packit |
51d0f7 |
printf("%u,", doi_list[iter]);
|
|
Packit |
51d0f7 |
/* map type */
|
|
Packit |
51d0f7 |
switch (mtype_list[iter]) {
|
|
Packit |
51d0f7 |
case CALIPSO_MAP_PASS:
|
|
Packit |
51d0f7 |
printf("PASS_THROUGH");
|
|
Packit |
51d0f7 |
break;
|
|
Packit |
51d0f7 |
default:
|
|
Packit |
51d0f7 |
printf("UNKNOWN(%u)", mtype_list[iter]);
|
|
Packit |
51d0f7 |
break;
|
|
Packit |
51d0f7 |
}
|
|
Packit |
51d0f7 |
if (iter + 1 < count)
|
|
Packit |
51d0f7 |
printf(" ");
|
|
Packit |
51d0f7 |
}
|
|
Packit |
51d0f7 |
printf("\n");
|
|
Packit |
51d0f7 |
}
|
|
Packit |
51d0f7 |
|
|
Packit |
51d0f7 |
rc = 0;
|
|
Packit |
51d0f7 |
|
|
Packit |
51d0f7 |
list_all_return:
|
|
Packit |
51d0f7 |
if (doi_list != NULL)
|
|
Packit |
51d0f7 |
free(doi_list);
|
|
Packit |
51d0f7 |
if (mtype_list != NULL)
|
|
Packit |
51d0f7 |
free(mtype_list);
|
|
Packit |
51d0f7 |
return rc;
|
|
Packit |
51d0f7 |
}
|
|
Packit |
51d0f7 |
|
|
Packit |
51d0f7 |
/**
|
|
Packit |
51d0f7 |
* List a specific CALIPSO DOI label mapping
|
|
Packit |
51d0f7 |
* @param doi the DOI value
|
|
Packit |
51d0f7 |
*
|
|
Packit |
51d0f7 |
* List the configured CALIPSO label mapping. Returns zero on success,
|
|
Packit |
51d0f7 |
* negative values on failure.
|
|
Packit |
51d0f7 |
*
|
|
Packit |
51d0f7 |
*/
|
|
Packit |
51d0f7 |
static int calipso_list_doi(uint32_t doi)
|
|
Packit |
51d0f7 |
{
|
|
Packit |
51d0f7 |
int rc;
|
|
Packit |
51d0f7 |
nlbl_clp_mtype maptype;
|
|
Packit |
51d0f7 |
|
|
Packit |
51d0f7 |
rc = nlbl_calipso_list(NULL, doi, &maptype);
|
|
Packit |
51d0f7 |
if (rc < 0)
|
|
Packit |
51d0f7 |
return rc;
|
|
Packit |
51d0f7 |
|
|
Packit |
51d0f7 |
if (opt_pretty != 0) {
|
|
Packit |
51d0f7 |
printf("Configured CALIPSO mapping (DOI = %u)\n", doi);
|
|
Packit |
51d0f7 |
switch (maptype) {
|
|
Packit |
51d0f7 |
case CALIPSO_MAP_PASS:
|
|
Packit |
51d0f7 |
printf(" type: PASS_THROUGH\n");
|
|
Packit |
51d0f7 |
break;
|
|
Packit |
51d0f7 |
}
|
|
Packit |
51d0f7 |
} else {
|
|
Packit |
51d0f7 |
switch (maptype) {
|
|
Packit |
51d0f7 |
case CALIPSO_MAP_PASS:
|
|
Packit |
51d0f7 |
printf("type:PASS_THROUGH");
|
|
Packit |
51d0f7 |
break;
|
|
Packit |
51d0f7 |
}
|
|
Packit |
51d0f7 |
printf("\n");
|
|
Packit |
51d0f7 |
}
|
|
Packit |
51d0f7 |
|
|
Packit |
51d0f7 |
return 0;
|
|
Packit |
51d0f7 |
}
|
|
Packit |
51d0f7 |
|
|
Packit |
51d0f7 |
/**
|
|
Packit |
51d0f7 |
* List the CALIPSO label mappings
|
|
Packit |
51d0f7 |
* @param argc the number of arguments
|
|
Packit |
51d0f7 |
* @param argv the argument list
|
|
Packit |
51d0f7 |
*
|
|
Packit |
51d0f7 |
* List the configured CALIPSO label mappings. Returns zero on success,
|
|
Packit |
51d0f7 |
* negative values on failure.
|
|
Packit |
51d0f7 |
*
|
|
Packit |
51d0f7 |
*/
|
|
Packit |
51d0f7 |
static int calipso_list(int argc, char *argv[])
|
|
Packit |
51d0f7 |
{
|
|
Packit |
51d0f7 |
uint32_t iter;
|
|
Packit |
51d0f7 |
uint32_t doi_flag = 0;
|
|
Packit |
51d0f7 |
nlbl_clp_doi doi = 0;
|
|
Packit |
51d0f7 |
|
|
Packit |
51d0f7 |
/* parse the arguments */
|
|
Packit |
51d0f7 |
for (iter = 0; iter < argc && argv[iter] != NULL; iter++) {
|
|
Packit |
51d0f7 |
if (strncmp(argv[iter], "doi:", 4) == 0) {
|
|
Packit |
51d0f7 |
/* doi */
|
|
Packit |
51d0f7 |
doi = atoi(argv[iter] + 4);
|
|
Packit |
51d0f7 |
doi_flag = 1;
|
|
Packit |
51d0f7 |
} else
|
|
Packit |
51d0f7 |
return -EINVAL;
|
|
Packit |
51d0f7 |
}
|
|
Packit |
51d0f7 |
|
|
Packit |
51d0f7 |
if (doi_flag != 0)
|
|
Packit |
51d0f7 |
return calipso_list_doi(doi);
|
|
Packit |
51d0f7 |
else
|
|
Packit |
51d0f7 |
return calipso_list_all();
|
|
Packit |
51d0f7 |
}
|
|
Packit |
51d0f7 |
|
|
Packit |
51d0f7 |
/**
|
|
Packit |
51d0f7 |
* Entry point for the NetLabel CALIPSO functions
|
|
Packit |
51d0f7 |
* @param argc the number of arguments
|
|
Packit |
51d0f7 |
* @param argv the argument list
|
|
Packit |
51d0f7 |
*
|
|
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 calipso_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], "add") == 0) {
|
|
Packit |
51d0f7 |
/* add */
|
|
Packit |
51d0f7 |
rc = calipso_add(argc - 1, argv + 1);
|
|
Packit |
51d0f7 |
} else if (strcmp(argv[0], "del") == 0) {
|
|
Packit |
51d0f7 |
/* delete */
|
|
Packit |
51d0f7 |
rc = calipso_del(argc - 1, argv + 1);
|
|
Packit |
51d0f7 |
} else if (strcmp(argv[0], "list") == 0) {
|
|
Packit |
51d0f7 |
/* list */
|
|
Packit |
51d0f7 |
rc = calipso_list(argc - 1, argv + 1);
|
|
Packit |
51d0f7 |
} else {
|
|
Packit |
51d0f7 |
/* unknown request */
|
|
Packit |
51d0f7 |
rc = -EINVAL;
|
|
Packit |
51d0f7 |
}
|
|
Packit |
51d0f7 |
|
|
Packit |
51d0f7 |
return rc;
|
|
Packit |
51d0f7 |
}
|