1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 28 /* 29 * hextoalabel - Convert an internal label to its human readable 30 * equivalent. 31 */ 32 33 #include <errno.h> 34 #include <libintl.h> 35 #include <locale.h> 36 #include <stdio.h> 37 #include <stdlib.h> 38 #include <string.h> 39 #include <unistd.h> 40 #include <stropts.h> 41 42 #include <sys/param.h> 43 44 #include <tsol/label.h> 45 46 #if !defined(TEXT_DOMAIN) 47 #define TEXT_DOMAIN "SYS_TEST" 48 #endif /* !defined(TEXT_DOMAIN) */ 49 50 static void 51 label_error(const char *hex, const int err) 52 { 53 if (errno == EINVAL) { 54 switch (err) { 55 case M_BAD_STRING: 56 (void) fprintf(stderr, 57 gettext("hextoalabel: bad string %s\n"), hex); 58 break; 59 case M_BAD_LABEL: 60 (void) fprintf(stderr, 61 gettext("hextoalabel: bad previous label\n")); 62 break; 63 default: 64 (void) fprintf(stderr, 65 gettext("hextoalabel: parsing error found in " 66 "\"%s\" at position %d\n"), hex, err); 67 break; 68 } 69 } else { 70 perror("hextoalabel"); 71 } 72 exit(1); 73 /*NOTREACHED*/ 74 } 75 76 int 77 main(int argc, char **argv) 78 { 79 int cflg = 0; /* true if Clearance only */ 80 int errflg = 0; /* true if arg error */ 81 m_label_t *label = NULL; 82 char hex[PIPE_BUF]; /* internal label */ 83 char *ascii = NULL; /* human readable label to print */ 84 int err = 0; /* label error */ 85 int c; 86 87 (void) setlocale(LC_ALL, ""); 88 (void) textdomain(TEXT_DOMAIN); 89 90 opterr = 0; 91 while ((c = getopt(argc, argv, "c")) != EOF) { 92 93 switch (c) { 94 case 'c': 95 cflg++; 96 break; 97 default: 98 errflg++; 99 break; 100 } 101 } 102 103 argc -= optind - 1; 104 if (errflg || argc > 2) { 105 106 (void) fprintf(stderr, 107 gettext("usage: %s [-c] [hexadecimal label]\n"), argv[0]); 108 exit(1); 109 /*NOTREACHED*/ 110 } 111 112 if (argc == 2) { 113 /* use label on command line */ 114 115 (void) strlcpy(hex, argv[optind], sizeof (hex)); 116 } else { 117 /* read label from standard input */ 118 119 if ((c = read(STDIN_FILENO, hex, sizeof (hex))) <= 0) { 120 121 perror(gettext("reading hexadecimal label")); 122 exit(1); 123 /*NOTREACHED*/ 124 } 125 126 /* 127 * replace '\n' or (end of buffer) with end of string. 128 */ 129 hex[c-1] = '\0'; 130 131 /* 132 * flush any remaining input past the size of the buffer. 133 */ 134 (void) ioctl(STDIN_FILENO, I_FLUSH, FLUSHR); 135 } 136 137 if (cflg) { 138 if (str_to_label(hex, &label, USER_CLEAR, L_NO_CORRECTION, 139 &err) == -1) { 140 label_error(hex, err); 141 } 142 if (label_to_str(label, &ascii, M_LABEL, DEF_NAMES) != 0) { 143 perror("label_to_str"); 144 exit(1); 145 } 146 (void) printf("%s\n", ascii); 147 m_label_free(label); 148 free(ascii); 149 } else { 150 if (str_to_label(hex, &label, MAC_LABEL, L_NO_CORRECTION, 151 &err) == -1) { 152 label_error(hex, err); 153 } 154 if (label_to_str(label, &ascii, M_LABEL, DEF_NAMES) != 0) { 155 perror("label_to_str"); 156 exit(1); 157 } 158 (void) printf("%s\n", ascii); 159 m_label_free(label); 160 free(ascii); 161 } 162 163 return (0); /* really exit(0); */ 164 } 165