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