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>
36 #include <sys/param.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
= {
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
= {
70 static ilbadm_cmd_help_t enable_server_help
= {
74 static ilbadm_cmd_help_t enable_rule_help
= {
78 static ilbadm_cmd_help_t disable_rule_help
= {
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
= {
95 static ilbadm_cmd_help_t show_persist_help
= {
99 static ilbadm_cmd_help_t show_hc_help
= {
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
= {
112 static ilbadm_cmd_help_t show_hc_result_help
= {
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
= {
124 static ilbadm_cmd_help_t show_servergroup_help
= {
125 "[[-p] -o field[,field]] [name]"
128 static ilbadm_cmd_help_t export_config_help
= {
132 static ilbadm_cmd_help_t import_config_help
= {
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
,
141 {"show-rule", "show-rl", ilbadm_show_rules
, &show_rule_help
},
143 {"create-servergroup", "create-sg", ilbadm_create_servergroup
,
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
,
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
,
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
,
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
,
181 ilbadm_noimport(int argc
, char *argv
[])
183 ilbadm_err(ilbadm_errstr(ILBADM_NORECURSIVE
));
184 return (ILBADM_LIBERR
);
188 print_cmd_short(char *name
, FILE *fp
, ilbadm_cmd_desc_t
*cmd
)
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
);
198 (void) fprintf(fp
, "%s %s|%s\n", name
, cmd
->c_name
,
205 print_cmdlist_short(char *name
, FILE *fp
)
207 print_cmd_short(name
, fp
, ilbadm_cmds
);
210 #define IMPORT_FILE 0x1
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"
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)
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
;
243 match_cmd(argv
[0], ilbadm_cmds
, &cmd
, flags
);
246 rc
= cmd(argc
, argv
);
248 rc
= ILBADM_INVAL_COMMAND
;
249 ilbadm_err(ilbadm_errstr(rc
));
264 i_getln_to_argv(FILE *fp
, arg_t
**ap
)
266 static char *linebuf
= NULL
;
267 char *stringp
, *currp
;
268 char delim
[] = " \t\n";
272 #define STR_DIFF(s1, s2) (int)((char *)s2 - (char *)s1)
273 #define STR_ADJ_SZ(sz, buf, s) (sz - STR_DIFF(buf, s))
276 if ((linebuf
= (char *)malloc(LINESZ
)) == NULL
)
279 stringp
= currp
= linebuf
;
282 if (fgets(currp
, STR_ADJ_SZ(LINESZ
, linebuf
, currp
), fp
) == NULL
)
285 /* ignore lines starting with a # character */
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') {
299 * if we find a '\' at the end of a line, treat
300 * it as a continuation character.
302 if (*currp
== '\\' && stringp
== NULL
) {
307 a
= (arg_t
*)malloc(sizeof (*a
));
308 bzero(a
, sizeof (*a
));
310 if (a
->listsz
<= i
) {
315 ((a
->listsz
- 1) * sizeof (a
->arglist
));
316 a
= (arg_t
*)realloc(a
, sz
);
319 a
->arglist
[i
] = currp
;
324 static ilbadm_status_t
325 ilb_import_file(int fd
, int flags
)
330 ilbadm_status_t rc
= ILBADM_OK
;
332 if ((fp
= fdopen(fd
, "r")) == NULL
) {
333 ilbadm_err(gettext("cannot import file for reading"));
337 if ((flags
& ILBADM_IMPORT_PRESERVE
) == 0) {
338 ilb_handle_t h
= ILB_INVALID_HANDLE
;
341 rclib
= ilb_open(&h
);
342 if (rclib
== ILB_STATUS_OK
)
343 (void) ilb_reset_config(h
);
344 if (h
!= ILB_INVALID_HANDLE
)
348 while ((argcount
= i_getln_to_argv(fp
, &a
)) > 0) {
350 rc
= ilb_import_cmdline(argcount
, a
->arglist
, IMPORT_FILE
);
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.
365 ilbadm_import(int fd
, int argc
, char *argv
[], int flags
)
370 rc
= ilb_import_cmdline(argc
, argv
, 0);
372 rc
= ilb_import_file(fd
, flags
);
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
;
386 char tmpfile
[MAXPATHLEN
];
393 (void) snprintf(tmpfile
, sizeof (tmpfile
), "%sXXXXXX", fname
);
394 fd
= mkstemp(tmpfile
);
397 ilbadm_err(gettext("cannot create working file"));
401 fp
= fdopen(fd
, "w");
403 ilbadm_err(gettext("cannot open file for writing"), fd
);
407 rclib
= ilb_open(&h
);
408 if (rclib
!= ILB_STATUS_OK
)
411 rc
= ilbadm_export_servergroups(h
, fp
);
415 rc
= ilbadm_export_hc(h
, fp
);
419 rc
= ilbadm_export_rules(h
, fp
);
424 if (rename(tmpfile
, fname
) == -1) {
425 ilbadm_err(gettext("cannot create %s: %s"), fname
,
433 if (h
!= ILB_INVALID_HANDLE
)
436 if ((rc
!= ILBADM_OK
) && (rc
!= ILBADM_LIBERR
))
437 ilbadm_err(ilbadm_errstr(rc
));
439 if (*tmpfile
!= '\0')
440 (void) unlink(tmpfile
);