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]
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
33 #include <sys/types.h>
34 #include <sys/socket.h>
40 static ilbadm_cmd_help_t create_sg_help
= {
41 "[-s server=hostspec[:portspec...]] groupname"
44 static ilbadm_cmd_help_t create_rule_help
= {
45 "[-e] [-p] -i vip=value,port=value[,protocol=value] \n" \
46 " -m lbalg=value,type=value[,proxy-src=ip-range][,pmask=mask] \n"\
47 " -h hc-name=value[,hc-port=value]] \n" \
48 " [-t [conn-drain=N][,nat-timeout=N][,persist-timeout=N]] \n" \
49 " -o servergroup=value name"
52 static ilbadm_cmd_help_t destroy_rule_help
= {
56 static ilbadm_cmd_help_t add_server_help
= {
57 "-s server=value[,value ...] servergroup"
60 static ilbadm_cmd_help_t remove_server_help
= {
61 "-s server=value[,value ...] servergroup"
65 static ilbadm_cmd_help_t disable_server_help
= {
69 static ilbadm_cmd_help_t enable_server_help
= {
73 static ilbadm_cmd_help_t enable_rule_help
= {
77 static ilbadm_cmd_help_t disable_rule_help
= {
81 static ilbadm_cmd_help_t show_server_help
= {
82 "[[-p] -o field[,field...]] [rulename ... ]"
85 static ilbadm_cmd_help_t showstats_help
= {
86 "[-p] -o field[,...]] [-tdAvi]\n" \
87 " [-r rulename|-s servername] [interval [count]]"
90 static ilbadm_cmd_help_t show_nat_help
= {
94 static ilbadm_cmd_help_t show_persist_help
= {
98 static ilbadm_cmd_help_t show_hc_help
= {
102 static ilbadm_cmd_help_t create_hc_help
= {
103 "[-n] -h hc-test=value[,hc-timeout=value]\n" \
104 " [,hc-count=value][,hc-interval=value] hcname"
107 static ilbadm_cmd_help_t destroy_hc_help
= {
111 static ilbadm_cmd_help_t show_hc_result_help
= {
115 static ilbadm_cmd_help_t show_rule_help
= {
116 "[-e|-d] [-f |[-p] -o key[,key ...]] [name ...]"
119 static ilbadm_cmd_help_t destroy_servergroup_help
= {
123 static ilbadm_cmd_help_t show_servergroup_help
= {
124 "[[-p] -o field[,field]] [name]"
127 static ilbadm_cmd_help_t export_config_help
= {
131 static ilbadm_cmd_help_t import_config_help
= {
134 static ilbadm_cmd_desc_t ilbadm_cmds
[] = {
135 {"create-rule", "create-rl", ilbadm_create_rule
, &create_rule_help
},
136 {"delete-rule", "delete-rl", ilbadm_destroy_rule
, &destroy_rule_help
},
137 {"enable-rule", "enable-rl", ilbadm_enable_rule
, &enable_rule_help
},
138 {"disable-rule", "disable-rl", ilbadm_disable_rule
,
140 {"show-rule", "show-rl", ilbadm_show_rules
, &show_rule_help
},
142 {"create-servergroup", "create-sg", ilbadm_create_servergroup
,
144 {"delete-servergroup", "delete-sg", ilbadm_destroy_servergroup
,
145 &destroy_servergroup_help
},
146 {"show-servergroup", "show-sg", ilbadm_show_servergroups
,
147 &show_servergroup_help
},
149 {"add-server", "add-srv", ilbadm_add_server_to_group
,
151 {"remove-server", "remove-srv", ilbadm_rem_server_from_group
,
152 &remove_server_help
},
153 {"disable-server", "disable-srv", ilbadm_disable_server
,
154 &disable_server_help
},
155 {"enable-server", "enable-srv", ilbadm_enable_server
,
156 &enable_server_help
},
157 {"show-server", "show-srv", ilbadm_show_server
,
160 {"show-healthcheck", "show-hc", ilbadm_show_hc
, &show_hc_help
},
161 {"create-healthcheck", "create-hc", ilbadm_create_hc
, &create_hc_help
},
162 {"delete-healthcheck", "delete-hc", ilbadm_destroy_hc
,
164 {"show-hc-result", "show-hc-res", ilbadm_show_hc_result
,
165 &show_hc_result_help
},
167 {"export-config", "export-cf", ilbadm_export
, &export_config_help
},
168 {"import-config", "import-cf", ilbadm_noimport
, &import_config_help
},
170 {"show-statistics", "show-stats", ilbadm_show_stats
, &showstats_help
},
171 {"show-nat", "show-nat", ilbadm_show_nat
, &show_nat_help
},
172 {"show-persist", "show-pt", ilbadm_show_persist
,
180 ilbadm_noimport(int argc
, char *argv
[])
182 ilbadm_err(ilbadm_errstr(ILBADM_NORECURSIVE
));
183 return (ILBADM_LIBERR
);
187 print_cmd_short(char *name
, FILE *fp
, ilbadm_cmd_desc_t
*cmd
)
191 while (cmd
->c_name
[0] != '\0') {
192 if (cmd
->c_help
!= NULL
&&
193 (h
= cmd
->c_help
->h_help
) != NULL
)
194 (void) fprintf(fp
, "%s %s|%s %s\n", name
,
195 cmd
->c_name
, cmd
->c_alias
, h
);
197 (void) fprintf(fp
, "%s %s|%s\n", name
, cmd
->c_name
,
204 print_cmdlist_short(char *name
, FILE *fp
)
206 print_cmd_short(name
, fp
, ilbadm_cmds
);
209 #define IMPORT_FILE 0x1
212 match_cmd(char *name
, ilbadm_cmd_desc_t
*cmds
, cmdfunc_t
*action
, int flags
)
214 ilbadm_cmd_desc_t
*cmd
;
216 if ((flags
& IMPORT_FILE
) == IMPORT_FILE
) {
217 if (strcasecmp(name
, "export-config") == 0 ||
218 strcasecmp(name
, "export-cf") == 0) {
219 ilbadm_err(gettext("export from import file"
225 for (cmd
= &cmds
[0]; cmd
->c_name
[0] != '\0'; cmd
++) {
226 if (strncasecmp(cmd
->c_name
, name
, sizeof (cmd
->c_name
)) == 0 ||
227 strncasecmp(cmd
->c_alias
, name
, sizeof (cmd
->c_alias
)) == 0)
230 *action
= cmd
->c_action
;
234 * read and parse commandline
236 static ilbadm_status_t
237 ilb_import_cmdline(int argc
, char *argv
[], int flags
)
239 ilbadm_status_t rc
= ILBADM_OK
;
242 match_cmd(argv
[0], ilbadm_cmds
, &cmd
, flags
);
245 rc
= cmd(argc
, argv
);
247 rc
= ILBADM_INVAL_COMMAND
;
248 ilbadm_err(ilbadm_errstr(rc
));
263 i_getln_to_argv(FILE *fp
, arg_t
**ap
)
265 static char *linebuf
= NULL
;
266 char *stringp
, *currp
;
267 char delim
[] = " \t\n";
271 #define STR_DIFF(s1, s2) (int)((char *)s2 - (char *)s1)
272 #define STR_ADJ_SZ(sz, buf, s) (sz - STR_DIFF(buf, s))
275 if ((linebuf
= (char *)malloc(LINESZ
)) == NULL
)
278 stringp
= currp
= linebuf
;
281 if (fgets(currp
, STR_ADJ_SZ(LINESZ
, linebuf
, currp
), fp
) == NULL
)
284 /* ignore lines starting with a # character */
288 for (; stringp
!= NULL
&& currp
!= NULL
; i
++) {
289 currp
= strsep(&stringp
, delim
);
291 * if there's more than one adjacent delimiters ...
293 if (*currp
== '\0') {
298 * if we find a '\' at the end of a line, treat
299 * it as a continuation character.
301 if (*currp
== '\\' && stringp
== NULL
) {
306 a
= (arg_t
*)malloc(sizeof (*a
));
307 bzero(a
, sizeof (*a
));
309 if (a
->listsz
<= i
) {
314 ((a
->listsz
- 1) * sizeof (a
->arglist
));
315 a
= (arg_t
*)realloc(a
, sz
);
318 a
->arglist
[i
] = currp
;
323 static ilbadm_status_t
324 ilb_import_file(int fd
, int flags
)
329 ilbadm_status_t rc
= ILBADM_OK
;
331 if ((fp
= fdopen(fd
, "r")) == NULL
) {
332 ilbadm_err(gettext("cannot import file for reading"));
336 if ((flags
& ILBADM_IMPORT_PRESERVE
) == 0) {
337 ilb_handle_t h
= ILB_INVALID_HANDLE
;
340 rclib
= ilb_open(&h
);
341 if (rclib
== ILB_STATUS_OK
)
342 (void) ilb_reset_config(h
);
343 if (h
!= ILB_INVALID_HANDLE
)
347 while ((argcount
= i_getln_to_argv(fp
, &a
)) > 0) {
349 rc
= ilb_import_cmdline(argcount
, a
->arglist
, IMPORT_FILE
);
358 * this is the wrapper around everything to do with importing and
359 * parsing either commandline or persistent storage.
360 * if (fd == -1), parse commandline, otherwise use the given fd as input.
364 ilbadm_import(int fd
, int argc
, char *argv
[], int flags
)
369 rc
= ilb_import_cmdline(argc
, argv
, 0);
371 rc
= ilb_import_file(fd
, flags
);
377 ilbadm_export(int argc
, char *argv
[])
379 ilb_handle_t h
= ILB_INVALID_HANDLE
;
380 ilbadm_status_t rc
= ILBADM_OK
;
381 ilb_status_t rclib
= ILB_STATUS_OK
;
385 char tmpfile
[MAXPATHLEN
];
392 (void) snprintf(tmpfile
, sizeof (tmpfile
), "%sXXXXXX", fname
);
393 fd
= mkstemp(tmpfile
);
396 ilbadm_err(gettext("cannot create working file"));
400 fp
= fdopen(fd
, "w");
402 ilbadm_err(gettext("cannot open file for writing"), fd
);
406 rclib
= ilb_open(&h
);
407 if (rclib
!= ILB_STATUS_OK
)
410 rc
= ilbadm_export_servergroups(h
, fp
);
414 rc
= ilbadm_export_hc(h
, fp
);
418 rc
= ilbadm_export_rules(h
, fp
);
423 if (rename(tmpfile
, fname
) == -1) {
424 ilbadm_err(gettext("cannot create %s: %s"), fname
,
432 if (h
!= ILB_INVALID_HANDLE
)
435 if ((rc
!= ILBADM_OK
) && (rc
!= ILBADM_LIBERR
))
436 ilbadm_err(ilbadm_errstr(rc
));
438 if (*tmpfile
!= '\0')
439 (void) unlink(tmpfile
);