xref: /onnv-gate/usr/src/cmd/tsol/tninfo/tninfo.c (revision 11561:e0d5740d4722)
14746Srica /*
24746Srica  * CDDL HEADER START
34746Srica  *
44746Srica  * The contents of this file are subject to the terms of the
54746Srica  * Common Development and Distribution License (the "License").
64746Srica  * You may not use this file except in compliance with the License.
74746Srica  *
84746Srica  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
94746Srica  * or http://www.opensolaris.org/os/licensing.
104746Srica  * See the License for the specific language governing permissions
114746Srica  * and limitations under the License.
124746Srica  *
134746Srica  * When distributing Covered Code, include this CDDL HEADER in each
144746Srica  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
154746Srica  * If applicable, add the following below this CDDL HEADER, with the
164746Srica  * fields enclosed by brackets "[]" replaced with your own identifying
174746Srica  * information: Portions Copyright [yyyy] [name of copyright owner]
184746Srica  *
194746Srica  * CDDL HEADER END
204746Srica  */
214746Srica 
224746Srica /*
23*11561SRic.Aleshire@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
244746Srica  * Use is subject to license terms.
254746Srica  */
264746Srica 
274746Srica /*
284746Srica  * tninfo.c - Trusted network reporting utility
294746Srica  */
304746Srica #include <sys/types.h>
314746Srica #include <errno.h>
324746Srica #include <stdio.h>
334746Srica #include <locale.h>
344746Srica #include <string.h>
354746Srica #include <stdlib.h>
364746Srica #include <libtsnet.h>
374746Srica #include <netinet/in.h>
384746Srica #include <arpa/inet.h>
394746Srica #include <netdb.h>
404746Srica #include <tsol/label.h>
414746Srica #include <zone.h>
424746Srica 
434746Srica static void usage(void);
444746Srica static int print_rhtp(const char *);
454746Srica static int print_rh(const char *);
464746Srica static int print_mlp(const char *);
474746Srica 
484746Srica int
main(int argc,char * argv[])494746Srica main(int argc, char *argv[])
504746Srica {
514746Srica 	int chr;
524746Srica 	int ret = 0; /* return code */
534746Srica 
544746Srica 	/* set the locale for only the messages system (all else is clean) */
554746Srica 	(void) setlocale(LC_ALL, "");
564746Srica #ifndef TEXT_DOMAIN		/* Should be defined by cc -D */
574746Srica #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it weren't */
584746Srica #endif
594746Srica 
604746Srica 	(void) textdomain(TEXT_DOMAIN);
614746Srica 
624746Srica 	if (argc <= 1)
634746Srica 		usage();
644746Srica 
654746Srica 	while ((chr = getopt(argc, argv, "h:m:t:")) != EOF) {
664746Srica 		switch (chr) {
674746Srica 		case 'h':
684746Srica 			ret |= print_rh(optarg);
694746Srica 			break;
704746Srica 		case 'm':
714746Srica 			ret |= print_mlp(optarg);
724746Srica 			break;
734746Srica 		case 't':
744746Srica 			ret |= print_rhtp(optarg);
754746Srica 			break;
764746Srica 		default:
774746Srica 			usage();
784746Srica 		}
794746Srica 	}
804746Srica 
814746Srica 	return (ret);
824746Srica }
834746Srica 
844746Srica static void
usage(void)854746Srica usage(void)
864746Srica {
874746Srica 	(void) fprintf(stderr, gettext("usage: tninfo [-h host_name] "
884746Srica 	    "[-m zone_name] [-t template_name]\n"));
894746Srica 	exit(1);
904746Srica }
914746Srica 
92*11561SRic.Aleshire@Sun.COM static void
l_to_str(const m_label_t * l,char ** str,int ltype)93*11561SRic.Aleshire@Sun.COM l_to_str(const m_label_t *l, char **str, int ltype)
94*11561SRic.Aleshire@Sun.COM {
95*11561SRic.Aleshire@Sun.COM 	if (label_to_str(l, str, ltype, DEF_NAMES) != 0)
96*11561SRic.Aleshire@Sun.COM 		*str = strdup(gettext("translation failed"));
97*11561SRic.Aleshire@Sun.COM }
98*11561SRic.Aleshire@Sun.COM 
994746Srica static int
print_rhtp(const char * rhtp_name)1004746Srica print_rhtp(const char *rhtp_name)
1014746Srica {
1024746Srica 	tsol_tpent_t tp;
103*11561SRic.Aleshire@Sun.COM 	char *str, *str2;
104*11561SRic.Aleshire@Sun.COM 	const m_label_t *l1, *l2;
1054746Srica 	int i;
1064746Srica 
1074746Srica 	(void) strlcpy(tp.name, rhtp_name, sizeof (tp.name));
1084746Srica 
1094746Srica 	if (tnrhtp(TNDB_GET, &tp) != 0) {
1104746Srica 		if (errno == ENOENT)
1114746Srica 			(void) fprintf(stderr, gettext("tninfo: tnrhtp entry "
1124746Srica 			    "%1$s does not exist\n"), tp.name);
1134746Srica 		else
1144746Srica 			(void) fprintf(stderr,
1154746Srica 			    gettext("tninfo: tnrhtp TNDB_GET(%1$s) failed: "
1164746Srica 			    "%2$s\n"), tp.name, strerror(errno));
1174746Srica 		return (1);
1184746Srica 	}
1194746Srica 
1204746Srica 	(void) printf("=====================================\n");
1214746Srica 	(void) printf(gettext("Remote Host Template Table Entries:\n"));
1224746Srica 
1234746Srica 	(void) printf("__________________________\n");
1244746Srica 	(void) printf(gettext("template: %s\n"), tp.name);
1254746Srica 
1264746Srica 	switch (tp.host_type) {
1274746Srica 	case UNLABELED:
1284746Srica 		(void) printf(gettext("host_type: UNLABELED\n"));
1294746Srica 		(void) printf(gettext("doi: %d\n"), tp.tp_doi);
1304746Srica 
1314746Srica 		if (tp.tp_mask_unl & TSOL_MSK_DEF_LABEL) {
132*11561SRic.Aleshire@Sun.COM 			l_to_str(&tp.tp_def_label, &str, M_LABEL);
133*11561SRic.Aleshire@Sun.COM 			l_to_str(&tp.tp_def_label, &str2, M_INTERNAL);
1344746Srica 			(void) printf(gettext("def_label: %s\nhex: %s\n"),
1354746Srica 			    str, str2);
136*11561SRic.Aleshire@Sun.COM 			free(str);
137*11561SRic.Aleshire@Sun.COM 			free(str2);
1384746Srica 		}
1394746Srica 
1404746Srica 		if (tp.tp_mask_unl & TSOL_MSK_SL_RANGE_TSOL) {
1414746Srica 			(void) printf(gettext("For routing only:\n"));
142*11561SRic.Aleshire@Sun.COM 			l_to_str(&tp.tp_gw_sl_range.lower_bound,
143*11561SRic.Aleshire@Sun.COM 			    &str, M_LABEL);
144*11561SRic.Aleshire@Sun.COM 			l_to_str(&tp.tp_gw_sl_range.lower_bound,
145*11561SRic.Aleshire@Sun.COM 			    &str2, M_INTERNAL);
1464746Srica 			(void) printf(gettext("min_sl: %s\nhex: %s\n"),
1474746Srica 			    str, str2);
148*11561SRic.Aleshire@Sun.COM 			free(str);
149*11561SRic.Aleshire@Sun.COM 			free(str2);
1504746Srica 
151*11561SRic.Aleshire@Sun.COM 			l_to_str(&tp.tp_gw_sl_range.upper_bound,
152*11561SRic.Aleshire@Sun.COM 			    &str, M_LABEL);
153*11561SRic.Aleshire@Sun.COM 			l_to_str(&tp.tp_gw_sl_range.upper_bound,
154*11561SRic.Aleshire@Sun.COM 			    &str2, M_INTERNAL);
1554746Srica 			(void) printf(gettext("max_sl: %s\nhex: %s\n"),
1564746Srica 			    str, str2);
157*11561SRic.Aleshire@Sun.COM 			free(str);
158*11561SRic.Aleshire@Sun.COM 			free(str2);
1594746Srica 
160*11561SRic.Aleshire@Sun.COM 			l1 = (const m_label_t *)&tp.tp_gw_sl_set[0];
161*11561SRic.Aleshire@Sun.COM 			l2 = (const m_label_t *)&tp.tp_gw_sl_set[NSLS_MAX];
1624746Srica 			for (i = 0; l1 < l2; l1++, i++) {
163*11561SRic.Aleshire@Sun.COM 				if (label_to_str(l1, &str2, M_INTERNAL,
164*11561SRic.Aleshire@Sun.COM 				    DEF_NAMES) != 0)
1654746Srica 					break;
166*11561SRic.Aleshire@Sun.COM 				l_to_str(l1, &str, M_LABEL);
1674746Srica 				(void) printf(gettext("sl_set[%1$d]: %2$s\n"
1684746Srica 				    "hex: %3$s\n"), i, str, str2);
169*11561SRic.Aleshire@Sun.COM 				free(str);
170*11561SRic.Aleshire@Sun.COM 				free(str2);
1714746Srica 			}
1724746Srica 		}
1734746Srica 		break;
1744746Srica 
1754746Srica 	case SUN_CIPSO:
1764746Srica 		(void) printf(gettext("host_type: CIPSO\n"));
1774746Srica 		(void) printf(gettext("doi: %d\n"), tp.tp_doi);
1784746Srica 		if (tp.tp_mask_cipso & TSOL_MSK_SL_RANGE_TSOL) {
179*11561SRic.Aleshire@Sun.COM 			l_to_str(&tp.tp_sl_range_cipso.lower_bound,
180*11561SRic.Aleshire@Sun.COM 			    &str, M_LABEL);
181*11561SRic.Aleshire@Sun.COM 			l_to_str(&tp.tp_sl_range_cipso.lower_bound,
182*11561SRic.Aleshire@Sun.COM 			    &str2, M_INTERNAL);
183*11561SRic.Aleshire@Sun.COM 
1844746Srica 			(void) printf(gettext("min_sl: %s\nhex: %s\n"),
1854746Srica 			    str, str2);
186*11561SRic.Aleshire@Sun.COM 			free(str);
187*11561SRic.Aleshire@Sun.COM 			free(str2);
188*11561SRic.Aleshire@Sun.COM 
189*11561SRic.Aleshire@Sun.COM 			l_to_str(&tp.tp_sl_range_cipso.upper_bound,
190*11561SRic.Aleshire@Sun.COM 			    &str, M_LABEL);
191*11561SRic.Aleshire@Sun.COM 			l_to_str(&tp.tp_sl_range_cipso.upper_bound,
192*11561SRic.Aleshire@Sun.COM 			    &str2, M_INTERNAL);
193*11561SRic.Aleshire@Sun.COM 
1944746Srica 			(void) printf(gettext("max_sl: %s\nhex: %s\n"),
1954746Srica 			    str, str2);
196*11561SRic.Aleshire@Sun.COM 			free(str);
197*11561SRic.Aleshire@Sun.COM 			free(str2);
1984746Srica 
199*11561SRic.Aleshire@Sun.COM 			l1 = (const m_label_t *)&tp.tp_sl_set_cipso[0];
200*11561SRic.Aleshire@Sun.COM 			l2 = (const m_label_t *)&tp.tp_sl_set_cipso[NSLS_MAX];
2014746Srica 			for (i = 0; l1 < l2; l1++, i++) {
202*11561SRic.Aleshire@Sun.COM 				if (label_to_str(l1, &str2, M_INTERNAL,
203*11561SRic.Aleshire@Sun.COM 				    DEF_NAMES) != 0)
2044746Srica 					break;
205*11561SRic.Aleshire@Sun.COM 				l_to_str(l1, &str, M_LABEL);
206*11561SRic.Aleshire@Sun.COM 
2074746Srica 				(void) printf(gettext("sl_set[%1$d]: %2$s\n"
2084746Srica 				    "hex: %3$s\n"), i, str, str2);
209*11561SRic.Aleshire@Sun.COM 				free(str);
210*11561SRic.Aleshire@Sun.COM 				free(str2);
2114746Srica 			}
2124746Srica 		}
2134746Srica 		break;
2144746Srica 
2154746Srica 	default:
2164746Srica 		(void) printf(gettext("unsupported host type: %ld\n"),
2174746Srica 		    tp.host_type);
2184746Srica 	}
2194746Srica 	return (0);
2204746Srica }
2214746Srica 
2224746Srica static int
print_rh(const char * rh_name)2234746Srica print_rh(const char *rh_name)
2244746Srica {
2254746Srica 	int herr;
2264746Srica 	struct hostent *hp;
2274746Srica 	in6_addr_t in6;
2284746Srica 	char abuf[INET6_ADDRSTRLEN];
2294746Srica 	tsol_rhent_t rhent;
2304746Srica 
2314746Srica 	if ((hp = getipnodebyname(rh_name, AF_INET6,
2324746Srica 	    AI_ALL | AI_ADDRCONFIG | AI_V4MAPPED, &herr)) == NULL) {
2334746Srica 		(void) fprintf(stderr, gettext("tninfo: unknown host or "
2344746Srica 		    "invalid literal address: %s\n"), rh_name);
2354746Srica 		if (herr == TRY_AGAIN)
2364746Srica 			(void) fprintf(stderr,
2374746Srica 			    gettext("\t(try again later)\n"));
2384746Srica 		return (1);
2394746Srica 	}
2404746Srica 
2414746Srica 	(void) memset(&rhent, 0, sizeof (rhent));
2424746Srica 	(void) memcpy(&in6, hp->h_addr, hp->h_length);
2434746Srica 
2444746Srica 	if (IN6_IS_ADDR_V4MAPPED(&in6)) {
2454746Srica 		rhent.rh_address.ta_family = AF_INET;
2464746Srica 		IN6_V4MAPPED_TO_INADDR(&in6, &rhent.rh_address.ta_addr_v4);
2474746Srica 		(void) inet_ntop(AF_INET, &rhent.rh_address.ta_addr_v4, abuf,
2484746Srica 		    sizeof (abuf));
2494746Srica 	} else {
2504746Srica 		rhent.rh_address.ta_family = AF_INET6;
2514746Srica 		rhent.rh_address.ta_addr_v6 = in6;
2524746Srica 		(void) inet_ntop(AF_INET6, &in6, abuf, sizeof (abuf));
2534746Srica 	}
2544746Srica 
2554746Srica 	(void) printf(gettext("IP address= %s\n"), abuf);
2564746Srica 
2574746Srica 	if (tnrh(TNDB_GET, &rhent) != 0) {
2584746Srica 		if (errno == ENOENT)
2594746Srica 			(void) fprintf(stderr, gettext("tninfo: tnrhdb entry "
2604746Srica 			    "%1$s does not exist\n"), abuf);
2614746Srica 		else
2624746Srica 			(void) fprintf(stderr, gettext("tninfo: TNDB_GET(%1$s) "
2634746Srica 			    "failed: %2$s\n"), abuf, strerror(errno));
2644746Srica 		return (1);
2654746Srica 	}
2664746Srica 
2674746Srica 	if (rhent.rh_template[0] != '\0')
2684746Srica 		(void) printf(gettext("Template = %.*s\n"), TNTNAMSIZ,
2694746Srica 		    rhent.rh_template);
2704746Srica 	else
2714746Srica 		(void) printf(gettext("No template exists.\n"));
2724746Srica 
2734746Srica 	return (0);
2744746Srica }
2754746Srica 
2764746Srica static int
iterate_mlps(tsol_mlpent_t * tsme,const char * type)2774746Srica iterate_mlps(tsol_mlpent_t *tsme, const char *type)
2784746Srica {
2794746Srica 	struct protoent *pe;
2804746Srica 
2814746Srica 	/* get the first entry */
2824746Srica 	tsme->tsme_mlp.mlp_ipp = 0;
2834746Srica 	tsme->tsme_mlp.mlp_port = 0;
2844746Srica 	tsme->tsme_mlp.mlp_port_upper = 0;
2854746Srica 	if (tnmlp(TNDB_GET, tsme) == -1) {
2864746Srica 		if (errno == ENOENT) {
2874746Srica 			(void) printf(gettext("%s: no entries\n"), type);
2884746Srica 			return (0);
2894746Srica 		} else {
2904746Srica 			perror("tnmlp TNDB_GET");
2914746Srica 			return (-1);
2924746Srica 		}
2934746Srica 	}
2944746Srica 	(void) printf("%s: ", type);
2954746Srica 	for (;;) {
2964746Srica 		(void) printf("%u", tsme->tsme_mlp.mlp_port);
2974746Srica 		if (tsme->tsme_mlp.mlp_port != tsme->tsme_mlp.mlp_port_upper)
2984746Srica 			(void) printf("-%u", tsme->tsme_mlp.mlp_port_upper);
2994746Srica 		if ((pe = getprotobynumber(tsme->tsme_mlp.mlp_ipp)) == NULL)
3004746Srica 			(void) printf("/%u", tsme->tsme_mlp.mlp_ipp);
3014746Srica 		else
3024746Srica 			(void) printf("/%s", pe->p_name);
3034746Srica 		if (tsme->tsme_mlp.mlp_ipp == 255) {
3044746Srica 			tsme->tsme_mlp.mlp_port++;
3054746Srica 			tsme->tsme_mlp.mlp_ipp = 0;
3064746Srica 		} else {
3074746Srica 			tsme->tsme_mlp.mlp_ipp++;
3084746Srica 		}
3094746Srica 		if (tnmlp(TNDB_GET, tsme) == -1)
3104746Srica 			break;
3114746Srica 		(void) putchar(';');
3124746Srica 	}
3134746Srica 	(void) putchar('\n');
3144746Srica 	return (0);
3154746Srica }
3164746Srica 
3174746Srica /*
3184746Srica  * Print all of the MLPs for the given zone.
3194746Srica  */
3204746Srica static int
print_mlp(const char * zonename)3214746Srica print_mlp(const char *zonename)
3224746Srica {
3234746Srica 	tsol_mlpent_t tsme;
3244746Srica 
3254746Srica 	if ((tsme.tsme_zoneid = getzoneidbyname(zonename)) == -1) {
3264746Srica 		(void) fprintf(stderr, gettext("tninfo: zone '%s' unknown\n"),
3274746Srica 		    zonename);
3284746Srica 		return (1);
3294746Srica 	}
3304746Srica 	tsme.tsme_flags = 0;
3314746Srica 	if (iterate_mlps(&tsme, gettext("private")) == -1)
3324746Srica 		return (1);
3334746Srica 	tsme.tsme_flags = TSOL_MEF_SHARED;
3344746Srica 	if (iterate_mlps(&tsme, gettext("shared")) == -1)
3354746Srica 		return (1);
3364746Srica 	return (0);
3374746Srica }
338