dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / cmd-inet / usr.sbin / ilbadm / ilbadm_import.c
blob68b492fc30454b6281cead5065932a335c29a0c6
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <strings.h>
32 #include <errno.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <sys/stat.h>
36 #include <sys/param.h>
37 #include <fcntl.h>
38 #include <libilb.h>
39 #include "ilbadm.h"
41 static ilbadm_cmd_help_t create_sg_help = {
42 "[-s server=hostspec[:portspec...]] groupname"
45 static ilbadm_cmd_help_t create_rule_help = {
46 "[-e] [-p] -i vip=value,port=value[,protocol=value] \n" \
47 " -m lbalg=value,type=value[,proxy-src=ip-range][,pmask=mask] \n"\
48 " -h hc-name=value[,hc-port=value]] \n" \
49 " [-t [conn-drain=N][,nat-timeout=N][,persist-timeout=N]] \n" \
50 " -o servergroup=value name"
53 static ilbadm_cmd_help_t destroy_rule_help = {
54 "-a | name ..."
57 static ilbadm_cmd_help_t add_server_help = {
58 "-s server=value[,value ...] servergroup"
61 static ilbadm_cmd_help_t remove_server_help = {
62 "-s server=value[,value ...] servergroup"
66 static ilbadm_cmd_help_t disable_server_help = {
67 "server ... "
70 static ilbadm_cmd_help_t enable_server_help = {
71 "server ..."
74 static ilbadm_cmd_help_t enable_rule_help = {
75 "[name ... ]"
78 static ilbadm_cmd_help_t disable_rule_help = {
79 "[name ... ]"
82 static ilbadm_cmd_help_t show_server_help = {
83 "[[-p] -o field[,field...]] [rulename ... ]"
86 static ilbadm_cmd_help_t showstats_help = {
87 "[-p] -o field[,...]] [-tdAvi]\n" \
88 " [-r rulename|-s servername] [interval [count]]"
91 static ilbadm_cmd_help_t show_nat_help = {
92 "[count]"
95 static ilbadm_cmd_help_t show_persist_help = {
96 "[count]"
99 static ilbadm_cmd_help_t show_hc_help = {
100 "[hc-name]"
103 static ilbadm_cmd_help_t create_hc_help = {
104 "[-n] -h hc-test=value[,hc-timeout=value]\n" \
105 " [,hc-count=value][,hc-interval=value] hcname"
108 static ilbadm_cmd_help_t destroy_hc_help = {
109 "name ..."
112 static ilbadm_cmd_help_t show_hc_result_help = {
113 "[rule-name]"
116 static ilbadm_cmd_help_t show_rule_help = {
117 "[-e|-d] [-f |[-p] -o key[,key ...]] [name ...]"
120 static ilbadm_cmd_help_t destroy_servergroup_help = {
121 "groupname"
124 static ilbadm_cmd_help_t show_servergroup_help = {
125 "[[-p] -o field[,field]] [name]"
128 static ilbadm_cmd_help_t export_config_help = {
129 "[filename]"
132 static ilbadm_cmd_help_t import_config_help = {
133 "[-p] [filename]"
135 static ilbadm_cmd_desc_t ilbadm_cmds[] = {
136 {"create-rule", "create-rl", ilbadm_create_rule, &create_rule_help},
137 {"delete-rule", "delete-rl", ilbadm_destroy_rule, &destroy_rule_help},
138 {"enable-rule", "enable-rl", ilbadm_enable_rule, &enable_rule_help},
139 {"disable-rule", "disable-rl", ilbadm_disable_rule,
140 &disable_rule_help},
141 {"show-rule", "show-rl", ilbadm_show_rules, &show_rule_help},
143 {"create-servergroup", "create-sg", ilbadm_create_servergroup,
144 &create_sg_help},
145 {"delete-servergroup", "delete-sg", ilbadm_destroy_servergroup,
146 &destroy_servergroup_help},
147 {"show-servergroup", "show-sg", ilbadm_show_servergroups,
148 &show_servergroup_help},
150 {"add-server", "add-srv", ilbadm_add_server_to_group,
151 &add_server_help},
152 {"remove-server", "remove-srv", ilbadm_rem_server_from_group,
153 &remove_server_help},
154 {"disable-server", "disable-srv", ilbadm_disable_server,
155 &disable_server_help},
156 {"enable-server", "enable-srv", ilbadm_enable_server,
157 &enable_server_help},
158 {"show-server", "show-srv", ilbadm_show_server,
159 &show_server_help},
161 {"show-healthcheck", "show-hc", ilbadm_show_hc, &show_hc_help},
162 {"create-healthcheck", "create-hc", ilbadm_create_hc, &create_hc_help},
163 {"delete-healthcheck", "delete-hc", ilbadm_destroy_hc,
164 &destroy_hc_help},
165 {"show-hc-result", "show-hc-res", ilbadm_show_hc_result,
166 &show_hc_result_help},
168 {"export-config", "export-cf", ilbadm_export, &export_config_help},
169 {"import-config", "import-cf", ilbadm_noimport, &import_config_help},
171 {"show-statistics", "show-stats", ilbadm_show_stats, &showstats_help},
172 {"show-nat", "show-nat", ilbadm_show_nat, &show_nat_help},
173 {"show-persist", "show-pt", ilbadm_show_persist,
174 &show_persist_help},
175 {"", "", NULL, NULL}
179 /* ARGSUSED */
180 ilbadm_status_t
181 ilbadm_noimport(int argc, char *argv[])
183 ilbadm_err(ilbadm_errstr(ILBADM_NORECURSIVE));
184 return (ILBADM_LIBERR);
187 static void
188 print_cmd_short(char *name, FILE *fp, ilbadm_cmd_desc_t *cmd)
190 char *h;
192 while (cmd->c_name[0] != '\0') {
193 if (cmd->c_help != NULL &&
194 (h = cmd->c_help->h_help) != NULL)
195 (void) fprintf(fp, "%s %s|%s %s\n", name,
196 cmd->c_name, cmd->c_alias, h);
197 else
198 (void) fprintf(fp, "%s %s|%s\n", name, cmd->c_name,
199 cmd->c_alias);
200 cmd++;
204 void
205 print_cmdlist_short(char *name, FILE *fp)
207 print_cmd_short(name, fp, ilbadm_cmds);
210 #define IMPORT_FILE 0x1
212 static void
213 match_cmd(char *name, ilbadm_cmd_desc_t *cmds, cmdfunc_t *action, int flags)
215 ilbadm_cmd_desc_t *cmd;
217 if ((flags & IMPORT_FILE) == IMPORT_FILE) {
218 if (strcasecmp(name, "export-config") == 0 ||
219 strcasecmp(name, "export-cf") == 0) {
220 ilbadm_err(gettext("export from import file"
221 " not allowed"));
222 exit(1);
226 for (cmd = &cmds[0]; cmd->c_name[0] != '\0'; cmd++) {
227 if (strncasecmp(cmd->c_name, name, sizeof (cmd->c_name)) == 0 ||
228 strncasecmp(cmd->c_alias, name, sizeof (cmd->c_alias)) == 0)
229 break;
231 *action = cmd->c_action;
235 * read and parse commandline
237 static ilbadm_status_t
238 ilb_import_cmdline(int argc, char *argv[], int flags)
240 ilbadm_status_t rc = ILBADM_OK;
241 cmdfunc_t cmd;
243 match_cmd(argv[0], ilbadm_cmds, &cmd, flags);
245 if (*cmd != NULL) {
246 rc = cmd(argc, argv);
247 } else {
248 rc = ILBADM_INVAL_COMMAND;
249 ilbadm_err(ilbadm_errstr(rc));
252 return (rc);
255 #define CHUNK 10
256 #define LINESZ 1024
258 typedef struct {
259 int listsz;
260 char *arglist[1];
261 } arg_t;
263 static int
264 i_getln_to_argv(FILE *fp, arg_t **ap)
266 static char *linebuf = NULL;
267 char *stringp, *currp;
268 char delim[] = " \t\n";
269 int i;
270 arg_t *a = *ap;
272 #define STR_DIFF(s1, s2) (int)((char *)s2 - (char *)s1)
273 #define STR_ADJ_SZ(sz, buf, s) (sz - STR_DIFF(buf, s))
275 if (linebuf == NULL)
276 if ((linebuf = (char *)malloc(LINESZ)) == NULL)
277 return (0);
279 stringp = currp = linebuf;
280 i = 0;
281 read_next:
282 if (fgets(currp, STR_ADJ_SZ(LINESZ, linebuf, currp), fp) == NULL)
283 return (i);
285 /* ignore lines starting with a # character */
286 if (*currp == '#')
287 goto read_next;
289 for (; stringp != NULL && currp != NULL; i++) {
290 currp = strsep(&stringp, delim);
292 * if there's more than one adjacent delimiters ...
294 if (*currp == '\0') {
295 i--;
296 continue;
299 * if we find a '\' at the end of a line, treat
300 * it as a continuation character.
302 if (*currp == '\\' && stringp == NULL) {
303 stringp = currp;
304 goto read_next;
306 if (a == NULL) {
307 a = (arg_t *)malloc(sizeof (*a));
308 bzero(a, sizeof (*a));
310 if (a->listsz <= i) {
311 int sz;
313 a->listsz += CHUNK;
314 sz = sizeof (*a) +
315 ((a->listsz - 1) * sizeof (a->arglist));
316 a = (arg_t *)realloc(a, sz);
317 *ap = a;
319 a->arglist[i] = currp;
321 return (i);
324 static ilbadm_status_t
325 ilb_import_file(int fd, int flags)
327 FILE *fp;
328 arg_t *a = NULL;
329 int argcount;
330 ilbadm_status_t rc = ILBADM_OK;
332 if ((fp = fdopen(fd, "r")) == NULL) {
333 ilbadm_err(gettext("cannot import file for reading"));
334 exit(1);
337 if ((flags & ILBADM_IMPORT_PRESERVE) == 0) {
338 ilb_handle_t h = ILB_INVALID_HANDLE;
339 ilb_status_t rclib;
341 rclib = ilb_open(&h);
342 if (rclib == ILB_STATUS_OK)
343 (void) ilb_reset_config(h);
344 if (h != ILB_INVALID_HANDLE)
345 (void) ilb_close(h);
348 while ((argcount = i_getln_to_argv(fp, &a)) > 0) {
349 optind = 1;
350 rc = ilb_import_cmdline(argcount, a->arglist, IMPORT_FILE);
351 if (rc != ILBADM_OK)
352 break;
355 return (rc);
359 * this is the wrapper around everything to do with importing and
360 * parsing either commandline or persistent storage.
361 * if (fd == -1), parse commandline, otherwise use the given fd as input.
363 /* ARGSUSED */
364 ilbadm_status_t
365 ilbadm_import(int fd, int argc, char *argv[], int flags)
367 ilbadm_status_t rc;
369 if (fd == -1)
370 rc = ilb_import_cmdline(argc, argv, 0);
371 else
372 rc = ilb_import_file(fd, flags);
374 return (rc);
377 ilbadm_status_t
378 ilbadm_export(int argc, char *argv[])
380 ilb_handle_t h = ILB_INVALID_HANDLE;
381 ilbadm_status_t rc = ILBADM_OK;
382 ilb_status_t rclib = ILB_STATUS_OK;
383 int fd;
384 FILE *fp;
385 char *fname = NULL;
386 char tmpfile[MAXPATHLEN];
388 if (argc < 2) {
389 fd = 1; /* stdout */
390 *tmpfile = '\0';
391 } else {
392 fname = argv[1];
393 (void) snprintf(tmpfile, sizeof (tmpfile), "%sXXXXXX", fname);
394 fd = mkstemp(tmpfile);
396 if (fd == -1) {
397 ilbadm_err(gettext("cannot create working file"));
398 exit(1);
401 fp = fdopen(fd, "w");
402 if (fp == NULL) {
403 ilbadm_err(gettext("cannot open file for writing"), fd);
404 exit(1);
407 rclib = ilb_open(&h);
408 if (rclib != ILB_STATUS_OK)
409 goto out;
411 rc = ilbadm_export_servergroups(h, fp);
412 if (rc != ILBADM_OK)
413 goto out;
415 rc = ilbadm_export_hc(h, fp);
416 if (rc != ILBADM_OK)
417 goto out;
419 rc = ilbadm_export_rules(h, fp);
420 if (rc != ILBADM_OK)
421 goto out;
423 if (fname != NULL) {
424 if (rename(tmpfile, fname) == -1) {
425 ilbadm_err(gettext("cannot create %s: %s"), fname,
426 strerror(errno));
427 exit(1);
429 *tmpfile = '\0';
432 out:
433 if (h != ILB_INVALID_HANDLE)
434 (void) ilb_close(h);
436 if ((rc != ILBADM_OK) && (rc != ILBADM_LIBERR))
437 ilbadm_err(ilbadm_errstr(rc));
438 (void) fclose(fp);
439 if (*tmpfile != '\0')
440 (void) unlink(tmpfile);
441 return (rc);