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