summaryrefslogtreecommitdiff
path: root/usr/src/cmd/cmd-inet/usr.sbin/ilbadm/ilbadm.c
blob: 949fcc064cf0e51702126729af4ca4e695576bcb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <errno.h>
#include <libgen.h>
#include <libilb.h>
#include "ilbadm.h"

/*
 * Error strings for error values returned by ilbadm functions
 */
const char *
ilbadm_errstr(ilbadm_status_t rc)
{
	switch (rc) {
	case ILBADM_OK:
		return (gettext("no error"));
	case ILBADM_FAIL:
		return (gettext("processing of command failed"));
	case ILBADM_ENOMEM:
		return (gettext("memory allocation failure"));
	case ILBADM_EINVAL:
		return (gettext("invalid value  - refer to ilbadm(1M)"));
	case ILBADM_HCPRINT:
		return (gettext("failed to print healthcheck values"));
	case ILBADM_INVAL_AF:
		return (gettext("address family is invalid"));
	case ILBADM_INVAL_PORT:
		return (gettext("port value is invalid"));
	case ILBADM_INVAL_SRVID:
		return (gettext("server ID is invalid"));
	case ILBADM_INVAL_ADDR:
		return (gettext("address is invalid"));
	case ILBADM_INVAL_ARGS:
		return (gettext("invalid/incompatible keywords - refer to"
		    " ilbadm(1M)"));
	case ILBADM_ENOSGNAME:
		return (gettext("servergroup name missing"));
	case ILBADM_ENORULE:
		return (gettext("rule name missing or specified"
		    " rule not found"));
	case ILBADM_ENOSERVER:
		return (gettext("server name missing or specified"
		    " server not found"));
	case ILBADM_INVAL_ALG:
		return (gettext("LB algorithm is invalid"));
	case ILBADM_ENOPROTO:
		return (gettext("protocol does not exist in"
		    " protocol database"));
	case ILBADM_ENOSERVICE:
		return (gettext("servicename does not exist in nameservices"));
	case ILBADM_INVAL_OPER:
		return (gettext("operation type is invalid"));
	case ILBADM_INVAL_KEYWORD:
		return (gettext("keyword is invalid - please refer"
		    " to ilbadm(1M)"));
	case ILBADM_ASSIGNREQ:
		return (gettext("assignment '=' missing"));
	case ILBADM_NORECURSIVE:
		return (gettext("recursive import not allowed"));
	case ILBADM_INVAL_COMMAND:
		return (gettext("subcommand is invalid - please refer"
		    " to ilbadm(1M)"));
	case ILBADM_ENOPROXY:
		return (gettext("proxy-src is missing"));
	case ILBADM_INVAL_PROXY:
		return (gettext("proxy-src not allowed"));
	case ILBADM_ENOOPTION:
		return (gettext("mandatory argument(s) missing - refer"
		    " to ilbadm(1M)"));
	case ILBADM_TOOMANYIPADDR:
		return (gettext("address range contains more than 255"
		    " IP addresses"));
	case ILBADM_EXPORTFAIL:
		return (gettext("could not export servergroup because"
		    " of lack of space"));
	case ILBADM_INVAL_SYNTAX:
		return (gettext("syntax failure - refer to ilbadm(1M)"));
	case ILBADM_NOKEYWORD_VAL:
		return (gettext("missing value"));
	case ILBADM_LIBERR:
		return (gettext("library error"));
	default:
		return (gettext("unknown error"));


	}
}

/* PRINTFLIKE1 */
void
ilbadm_err(const char *format, ...)
{
	/* similar to warn() of dladm.c */
	va_list alist;

	(void) fprintf(stderr, "ilbadm: ");

	va_start(alist, format);
	(void) vfprintf(stderr, format, alist);
	va_end(alist);

	(void) fprintf(stderr, "\n");
}

void
Usage(char *name)
{
	(void) fprintf(stderr, gettext("Usage:\n"));
	print_cmdlist_short(basename(name), stderr);
	exit(1);
}

static void
print_version(char *name)
{
	(void) printf("%s %s\n", basename(name), ILBADM_VERSION);
	(void) printf(gettext(ILBADM_COPYRIGHT));
	exit(0);
}

void
unknown_opt(char **argv, int optind)
{
	ilbadm_err(gettext("bad or misplaced option %s"), argv[optind]);
	exit(1);
}

void
incomplete_cmdline(char *name)
{
	ilbadm_err(gettext("the command line is incomplete "
	    "(more arguments expected)"));
	Usage(name);
}

static void
bad_importfile(char *name, char *filename)
{
	ilbadm_err(gettext("file %s cannot be opened for reading"), filename);
	Usage(name);
}

int
main(int argc, char *argv[])
{
	ilbadm_status_t	rc;
	int		c;
	int		fd = -1;
	int		flags = 0;

	(void) setlocale(LC_ALL, "");

#if !defined(TEXT_DOMAIN)
#define	TEXT_DOMAIN "SYS_TEST"
#endif
	(void) textdomain(TEXT_DOMAIN);

	/* handle global options (-?, -V) first */
	while ((c = getopt(argc, argv, ":V:?")) != -1) {
		switch ((char)c) {
		case 'V': print_version(argv[0]);
			/* not reached */
			break;
		case '?':
			Usage(argv[0]);
			/* not reached */
			break;
		default: unknown_opt(argv, optind - 1);
			/* not reached */
			break;
		}
	}

	if (optind >= argc)
		incomplete_cmdline(argv[0]);

	/*
	 * we can import from a given file (argv[2]) or from
	 * stdin (if no file given)
	 */
	if (strcasecmp(argv[1], "import-config") == 0 ||
	    strcasecmp(argv[1], "import-cf") == 0) {
		int shift = 0;

		if (argc > 2 && strcmp(argv[2], "-p") == 0) {
			shift++;
			flags |= ILBADM_IMPORT_PRESERVE;
		}

		if (argc - shift < 3)
			fd = 0;
		else
			if ((fd = open(argv[2+shift], O_RDONLY)) == -1)
				bad_importfile(argv[0], argv[2+shift]);
	}

	argv++;
	argc--;

	/*
	 * re-set optind for next callers of getopt() - they all believe they're
	 * the first.
	 */
	optind = 1;
	optopt = 0;

	rc = ilbadm_import(fd, argc, argv, flags);

	/*
	 * The error messages have been printed out, using
	 * ilbadm_errstr() and ilb_errstr(), before we get here.
	 * So just set the exit value
	 */
	if (rc != ILBADM_OK)
		return (1);
	/* success */
	return (0);
}