1*dbed73cbSSangeeta Misra /* 2*dbed73cbSSangeeta Misra * CDDL HEADER START 3*dbed73cbSSangeeta Misra * 4*dbed73cbSSangeeta Misra * The contents of this file are subject to the terms of the 5*dbed73cbSSangeeta Misra * Common Development and Distribution License (the "License"). 6*dbed73cbSSangeeta Misra * You may not use this file except in compliance with the License. 7*dbed73cbSSangeeta Misra * 8*dbed73cbSSangeeta Misra * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9*dbed73cbSSangeeta Misra * or http://www.opensolaris.org/os/licensing. 10*dbed73cbSSangeeta Misra * See the License for the specific language governing permissions 11*dbed73cbSSangeeta Misra * and limitations under the License. 12*dbed73cbSSangeeta Misra * 13*dbed73cbSSangeeta Misra * When distributing Covered Code, include this CDDL HEADER in each 14*dbed73cbSSangeeta Misra * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15*dbed73cbSSangeeta Misra * If applicable, add the following below this CDDL HEADER, with the 16*dbed73cbSSangeeta Misra * fields enclosed by brackets "[]" replaced with your own identifying 17*dbed73cbSSangeeta Misra * information: Portions Copyright [yyyy] [name of copyright owner] 18*dbed73cbSSangeeta Misra * 19*dbed73cbSSangeeta Misra * CDDL HEADER END 20*dbed73cbSSangeeta Misra */ 21*dbed73cbSSangeeta Misra 22*dbed73cbSSangeeta Misra /* 23*dbed73cbSSangeeta Misra * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 24*dbed73cbSSangeeta Misra * Use is subject to license terms. 25*dbed73cbSSangeeta Misra */ 26*dbed73cbSSangeeta Misra 27*dbed73cbSSangeeta Misra #include <stdio.h> 28*dbed73cbSSangeeta Misra #include <unistd.h> 29*dbed73cbSSangeeta Misra #include <stdlib.h> 30*dbed73cbSSangeeta Misra #include <strings.h> 31*dbed73cbSSangeeta Misra #include <sys/types.h> 32*dbed73cbSSangeeta Misra #include <sys/socket.h> 33*dbed73cbSSangeeta Misra #include <fcntl.h> 34*dbed73cbSSangeeta Misra #include <errno.h> 35*dbed73cbSSangeeta Misra #include <libgen.h> 36*dbed73cbSSangeeta Misra #include <libilb.h> 37*dbed73cbSSangeeta Misra #include "ilbadm.h" 38*dbed73cbSSangeeta Misra 39*dbed73cbSSangeeta Misra /* 40*dbed73cbSSangeeta Misra * Error strings for error values returned by ilbadm functions 41*dbed73cbSSangeeta Misra */ 42*dbed73cbSSangeeta Misra const char * 43*dbed73cbSSangeeta Misra ilbadm_errstr(ilbadm_status_t rc) 44*dbed73cbSSangeeta Misra { 45*dbed73cbSSangeeta Misra switch (rc) { 46*dbed73cbSSangeeta Misra case ILBADM_OK: 47*dbed73cbSSangeeta Misra return (gettext("no error")); 48*dbed73cbSSangeeta Misra case ILBADM_FAIL: 49*dbed73cbSSangeeta Misra return (gettext("processing of command failed")); 50*dbed73cbSSangeeta Misra case ILBADM_ENOMEM: 51*dbed73cbSSangeeta Misra return (gettext("memory allocation failure")); 52*dbed73cbSSangeeta Misra case ILBADM_EINVAL: 53*dbed73cbSSangeeta Misra return (gettext("invalid value - refer to ilbadm(1M)")); 54*dbed73cbSSangeeta Misra case ILBADM_HCPRINT: 55*dbed73cbSSangeeta Misra return (gettext("failed to print healthcheck values")); 56*dbed73cbSSangeeta Misra case ILBADM_INVAL_AF: 57*dbed73cbSSangeeta Misra return (gettext("address family is invalid")); 58*dbed73cbSSangeeta Misra case ILBADM_INVAL_PORT: 59*dbed73cbSSangeeta Misra return (gettext("port value is invalid")); 60*dbed73cbSSangeeta Misra case ILBADM_INVAL_SRVID: 61*dbed73cbSSangeeta Misra return (gettext("server ID is invalid")); 62*dbed73cbSSangeeta Misra case ILBADM_INVAL_ADDR: 63*dbed73cbSSangeeta Misra return (gettext("address is invalid")); 64*dbed73cbSSangeeta Misra case ILBADM_INVAL_ARGS: 65*dbed73cbSSangeeta Misra return (gettext("invalid/incompatible keywords - refer to" 66*dbed73cbSSangeeta Misra " ilbadm(1M)")); 67*dbed73cbSSangeeta Misra case ILBADM_ENOSGNAME: 68*dbed73cbSSangeeta Misra return (gettext("servergroup name missing")); 69*dbed73cbSSangeeta Misra case ILBADM_ENORULE: 70*dbed73cbSSangeeta Misra return (gettext("rule name missing or specified" 71*dbed73cbSSangeeta Misra " rule not found")); 72*dbed73cbSSangeeta Misra case ILBADM_ENOSERVER: 73*dbed73cbSSangeeta Misra return (gettext("server name missing or specified" 74*dbed73cbSSangeeta Misra " server not found")); 75*dbed73cbSSangeeta Misra case ILBADM_INVAL_ALG: 76*dbed73cbSSangeeta Misra return (gettext("LB algorithm is invalid")); 77*dbed73cbSSangeeta Misra case ILBADM_ENOPROTO: 78*dbed73cbSSangeeta Misra return (gettext("protocol does not exist in" 79*dbed73cbSSangeeta Misra " protocol database")); 80*dbed73cbSSangeeta Misra case ILBADM_ENOSERVICE: 81*dbed73cbSSangeeta Misra return (gettext("servicename does not exist in nameservices")); 82*dbed73cbSSangeeta Misra case ILBADM_INVAL_OPER: 83*dbed73cbSSangeeta Misra return (gettext("operation type is invalid")); 84*dbed73cbSSangeeta Misra case ILBADM_INVAL_KEYWORD: 85*dbed73cbSSangeeta Misra return (gettext("keyword is invalid - please refer" 86*dbed73cbSSangeeta Misra " to ilbadm(1M)")); 87*dbed73cbSSangeeta Misra case ILBADM_ASSIGNREQ: 88*dbed73cbSSangeeta Misra return (gettext("assignment '=' missing")); 89*dbed73cbSSangeeta Misra case ILBADM_NORECURSIVE: 90*dbed73cbSSangeeta Misra return (gettext("recursive import not allowed")); 91*dbed73cbSSangeeta Misra case ILBADM_INVAL_COMMAND: 92*dbed73cbSSangeeta Misra return (gettext("subcommand is invalid - please refer" 93*dbed73cbSSangeeta Misra " to ilbadm(1M)")); 94*dbed73cbSSangeeta Misra case ILBADM_ENOPROXY: 95*dbed73cbSSangeeta Misra return (gettext("proxy-src is missing")); 96*dbed73cbSSangeeta Misra case ILBADM_INVAL_PROXY: 97*dbed73cbSSangeeta Misra return (gettext("proxy-src not allowed")); 98*dbed73cbSSangeeta Misra case ILBADM_ENOOPTION: 99*dbed73cbSSangeeta Misra return (gettext("mandatory argument(s) missing - refer" 100*dbed73cbSSangeeta Misra " to ilbadm(1M)")); 101*dbed73cbSSangeeta Misra case ILBADM_TOOMANYIPADDR: 102*dbed73cbSSangeeta Misra return (gettext("address range contains more than 255" 103*dbed73cbSSangeeta Misra " IP addresses")); 104*dbed73cbSSangeeta Misra case ILBADM_EXPORTFAIL: 105*dbed73cbSSangeeta Misra return (gettext("could not export servergroup because" 106*dbed73cbSSangeeta Misra " of lack of space")); 107*dbed73cbSSangeeta Misra case ILBADM_INVAL_SYNTAX: 108*dbed73cbSSangeeta Misra return (gettext("syntax failure - refer to ilbadm(1M)")); 109*dbed73cbSSangeeta Misra case ILBADM_NOKEYWORD_VAL: 110*dbed73cbSSangeeta Misra return (gettext("missing value")); 111*dbed73cbSSangeeta Misra case ILBADM_LIBERR: 112*dbed73cbSSangeeta Misra return (gettext("library error")); 113*dbed73cbSSangeeta Misra default: 114*dbed73cbSSangeeta Misra return (gettext("unknown error")); 115*dbed73cbSSangeeta Misra 116*dbed73cbSSangeeta Misra 117*dbed73cbSSangeeta Misra } 118*dbed73cbSSangeeta Misra } 119*dbed73cbSSangeeta Misra 120*dbed73cbSSangeeta Misra /* PRINTFLIKE1 */ 121*dbed73cbSSangeeta Misra void 122*dbed73cbSSangeeta Misra ilbadm_err(const char *format, ...) 123*dbed73cbSSangeeta Misra { 124*dbed73cbSSangeeta Misra /* similar to warn() of dladm.c */ 125*dbed73cbSSangeeta Misra va_list alist; 126*dbed73cbSSangeeta Misra 127*dbed73cbSSangeeta Misra (void) fprintf(stderr, "ilbadm: "); 128*dbed73cbSSangeeta Misra 129*dbed73cbSSangeeta Misra va_start(alist, format); 130*dbed73cbSSangeeta Misra (void) vfprintf(stderr, format, alist); 131*dbed73cbSSangeeta Misra va_end(alist); 132*dbed73cbSSangeeta Misra 133*dbed73cbSSangeeta Misra (void) fprintf(stderr, "\n"); 134*dbed73cbSSangeeta Misra } 135*dbed73cbSSangeeta Misra 136*dbed73cbSSangeeta Misra void 137*dbed73cbSSangeeta Misra Usage(char *name) 138*dbed73cbSSangeeta Misra { 139*dbed73cbSSangeeta Misra (void) fprintf(stderr, gettext("Usage:\n")); 140*dbed73cbSSangeeta Misra print_cmdlist_short(basename(name), stderr); 141*dbed73cbSSangeeta Misra exit(1); 142*dbed73cbSSangeeta Misra } 143*dbed73cbSSangeeta Misra 144*dbed73cbSSangeeta Misra static void 145*dbed73cbSSangeeta Misra print_version(char *name) 146*dbed73cbSSangeeta Misra { 147*dbed73cbSSangeeta Misra (void) printf("%s %s\n", basename(name), ILBADM_VERSION); 148*dbed73cbSSangeeta Misra (void) printf(gettext(ILBADM_COPYRIGHT)); 149*dbed73cbSSangeeta Misra exit(0); 150*dbed73cbSSangeeta Misra } 151*dbed73cbSSangeeta Misra 152*dbed73cbSSangeeta Misra void 153*dbed73cbSSangeeta Misra unknown_opt(char **argv, int optind) 154*dbed73cbSSangeeta Misra { 155*dbed73cbSSangeeta Misra ilbadm_err(gettext("bad or misplaced option %s"), argv[optind]); 156*dbed73cbSSangeeta Misra exit(1); 157*dbed73cbSSangeeta Misra } 158*dbed73cbSSangeeta Misra 159*dbed73cbSSangeeta Misra void 160*dbed73cbSSangeeta Misra incomplete_cmdline(char *name) 161*dbed73cbSSangeeta Misra { 162*dbed73cbSSangeeta Misra ilbadm_err(gettext("the command line is incomplete " 163*dbed73cbSSangeeta Misra "(more arguments expected)")); 164*dbed73cbSSangeeta Misra Usage(name); 165*dbed73cbSSangeeta Misra } 166*dbed73cbSSangeeta Misra 167*dbed73cbSSangeeta Misra static void 168*dbed73cbSSangeeta Misra bad_importfile(char *name, char *filename) 169*dbed73cbSSangeeta Misra { 170*dbed73cbSSangeeta Misra ilbadm_err(gettext("file %s cannot be opened for reading"), filename); 171*dbed73cbSSangeeta Misra Usage(name); 172*dbed73cbSSangeeta Misra } 173*dbed73cbSSangeeta Misra 174*dbed73cbSSangeeta Misra int 175*dbed73cbSSangeeta Misra main(int argc, char *argv[]) 176*dbed73cbSSangeeta Misra { 177*dbed73cbSSangeeta Misra ilbadm_status_t rc; 178*dbed73cbSSangeeta Misra int c; 179*dbed73cbSSangeeta Misra int fd = -1; 180*dbed73cbSSangeeta Misra int flags = 0; 181*dbed73cbSSangeeta Misra 182*dbed73cbSSangeeta Misra (void) setlocale(LC_ALL, ""); 183*dbed73cbSSangeeta Misra 184*dbed73cbSSangeeta Misra #if !defined(TEXT_DOMAIN) 185*dbed73cbSSangeeta Misra #define TEXT_DOMAIN "SYS_TEST" 186*dbed73cbSSangeeta Misra #endif 187*dbed73cbSSangeeta Misra (void) textdomain(TEXT_DOMAIN); 188*dbed73cbSSangeeta Misra 189*dbed73cbSSangeeta Misra /* handle global options (-?, -V) first */ 190*dbed73cbSSangeeta Misra while ((c = getopt(argc, argv, ":V:?")) != -1) { 191*dbed73cbSSangeeta Misra switch ((char)c) { 192*dbed73cbSSangeeta Misra case 'V': print_version(argv[0]); 193*dbed73cbSSangeeta Misra /* not reached */ 194*dbed73cbSSangeeta Misra break; 195*dbed73cbSSangeeta Misra case '?': 196*dbed73cbSSangeeta Misra Usage(argv[0]); 197*dbed73cbSSangeeta Misra /* not reached */ 198*dbed73cbSSangeeta Misra break; 199*dbed73cbSSangeeta Misra default: unknown_opt(argv, optind - 1); 200*dbed73cbSSangeeta Misra /* not reached */ 201*dbed73cbSSangeeta Misra break; 202*dbed73cbSSangeeta Misra } 203*dbed73cbSSangeeta Misra } 204*dbed73cbSSangeeta Misra 205*dbed73cbSSangeeta Misra if (optind >= argc) 206*dbed73cbSSangeeta Misra incomplete_cmdline(argv[0]); 207*dbed73cbSSangeeta Misra 208*dbed73cbSSangeeta Misra /* 209*dbed73cbSSangeeta Misra * we can import from a given file (argv[2]) or from 210*dbed73cbSSangeeta Misra * stdin (if no file given) 211*dbed73cbSSangeeta Misra */ 212*dbed73cbSSangeeta Misra if (strcasecmp(argv[1], "import-config") == 0 || 213*dbed73cbSSangeeta Misra strcasecmp(argv[1], "import-cf") == 0) { 214*dbed73cbSSangeeta Misra int shift = 0; 215*dbed73cbSSangeeta Misra 216*dbed73cbSSangeeta Misra if (argc > 2 && strcmp(argv[2], "-p") == 0) { 217*dbed73cbSSangeeta Misra shift++; 218*dbed73cbSSangeeta Misra flags |= ILBADM_IMPORT_PRESERVE; 219*dbed73cbSSangeeta Misra } 220*dbed73cbSSangeeta Misra 221*dbed73cbSSangeeta Misra if (argc - shift < 3) 222*dbed73cbSSangeeta Misra fd = 0; 223*dbed73cbSSangeeta Misra else 224*dbed73cbSSangeeta Misra if ((fd = open(argv[2+shift], O_RDONLY)) == -1) 225*dbed73cbSSangeeta Misra bad_importfile(argv[0], argv[2+shift]); 226*dbed73cbSSangeeta Misra } 227*dbed73cbSSangeeta Misra 228*dbed73cbSSangeeta Misra argv++; 229*dbed73cbSSangeeta Misra argc--; 230*dbed73cbSSangeeta Misra 231*dbed73cbSSangeeta Misra /* 232*dbed73cbSSangeeta Misra * re-set optind for next callers of getopt() - they all believe they're 233*dbed73cbSSangeeta Misra * the first. 234*dbed73cbSSangeeta Misra */ 235*dbed73cbSSangeeta Misra optind = 1; 236*dbed73cbSSangeeta Misra optopt = 0; 237*dbed73cbSSangeeta Misra 238*dbed73cbSSangeeta Misra rc = ilbadm_import(fd, argc, argv, flags); 239*dbed73cbSSangeeta Misra 240*dbed73cbSSangeeta Misra /* 241*dbed73cbSSangeeta Misra * The error messages have been printed out, using 242*dbed73cbSSangeeta Misra * ilbadm_errstr() and ilb_errstr(), before we get here. 243*dbed73cbSSangeeta Misra * So just set the exit value 244*dbed73cbSSangeeta Misra */ 245*dbed73cbSSangeeta Misra if (rc != ILBADM_OK) 246*dbed73cbSSangeeta Misra return (1); 247*dbed73cbSSangeeta Misra /* success */ 248*dbed73cbSSangeeta Misra return (0); 249*dbed73cbSSangeeta Misra } 250