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 2007 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 /* 30 * atohexlabel - Convert a human readable label to its internal 31 * equivalent. 32 */ 33 34 #include <errno.h> 35 #include <libintl.h> 36 #include <locale.h> 37 #include <stdio.h> 38 #include <stdlib.h> 39 #include <string.h> 40 #include <unistd.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 *ascii, const int err) 52 { 53 if (errno == EINVAL) { 54 switch (err) { 55 case M_BAD_STRING: 56 (void) fprintf(stderr, 57 gettext("atohexlabel: bad string %s\n"), ascii); 58 break; 59 case M_BAD_LABEL: 60 (void) fprintf(stderr, 61 gettext("atohexlabel: bad previous label\n")); 62 break; 63 default: 64 (void) fprintf(stderr, 65 gettext("atohexlabel: parsing error found in " 66 "\"%s\" at position %d\n"), ascii, err); 67 break; 68 } 69 } else { 70 perror("atohexlabel"); 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; /* binary labels */ 82 char ascii[PIPE_BUF]; /* human readable label */ 83 char *hex = NULL; /* internal 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 98 default: 99 errflg++; 100 break; 101 } 102 } 103 104 argc -= optind - 1; 105 if (errflg || argc > 2) { 106 107 (void) fprintf(stderr, 108 gettext("usage: %s [-c] [human readable label]\n"), 109 argv[0]); 110 exit(1); 111 /*NOTREACHED*/ 112 } 113 114 if (argc == 2) { 115 /* use label on command line */ 116 117 (void) strlcpy(ascii, argv[optind], sizeof (ascii)); 118 } else { 119 /* read label from standard input */ 120 if ((c = read(STDIN_FILENO, ascii, sizeof (ascii))) < 0) { 121 perror(gettext("reading ASCII coded label")); 122 exit(1); 123 /*NOTREACHED*/ 124 } 125 if (ascii[c-1] == '\n') { 126 /* replace '\n' with end of string */ 127 ascii[c-1] = '\0'; 128 } else { 129 /* ensure end of string */ 130 ascii[c] = '\0'; 131 } 132 } 133 134 if (cflg) { 135 if (str_to_label(ascii, &label, USER_CLEAR, L_NO_CORRECTION, 136 &err) == -1) { 137 label_error(ascii, err); 138 } 139 if (label_to_str(label, &hex, M_INTERNAL, DEF_NAMES) != 0) { 140 perror("label_to_str"); 141 exit(1); 142 } 143 (void) printf("%s\n", hex); 144 m_label_free(label); 145 free(hex); 146 } else { 147 if (str_to_label(ascii, &label, MAC_LABEL, L_NO_CORRECTION, 148 &err) == -1) { 149 label_error(ascii, err); 150 } 151 if (label_to_str(label, &hex, M_INTERNAL, DEF_NAMES) != 0) { 152 perror("label_to_str"); 153 exit(1); 154 } 155 (void) printf("%s\n", hex); 156 m_label_free(label); 157 free(hex); 158 } 159 160 return (0); /* really exit(0); */ 161 } 162