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.
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <sys/stropts.h>
35 #include <sys/sockio.h>
38 #include <auth_attr.h>
39 #include <auth_list.h>
42 #include "libilb_impl.h"
46 * logs error messages, either to stderr or syslog, depending on
49 static boolean_t ilbd_debugging
= B_FALSE
;
51 /* Socket to issue ioctl() to the kernel */
52 static int ksock
= -1;
55 ilbd_enable_debug(void)
57 ilbd_debugging
= B_TRUE
;
63 return (ilbd_debugging
);
67 * All routines log to syslog, unless the daemon is running in
68 * the foreground, in which case the logging goes to stderr.
69 * The following logging functions are available:
72 * logdebug(): A printf-like function for outputting debug messages
73 * (messages at LOG_DEBUG) that are only of use to developers.
75 * logerr(): A printf-like function for outputting error messages
76 * (messages at LOG_ERR) from the daemon.
78 * logperror*(): A set of functions used to output error messages
79 * (messages at LOG_ERR); these automatically append strerror(errno)
80 * and a newline to the message passed to them.
82 * NOTE: since the logging functions write to syslog, the messages passed
83 * to them are not eligible for localization. Thus, gettext() must
89 ilbd_log(int pri
, const char *fmt
, ...)
94 if (ilbd_debugging
== B_TRUE
) {
95 (void) vfprintf(stderr
, fmt
, ap
);
96 (void) fprintf(stderr
, "\n");
98 vsyslog(pri
, fmt
, ap
);
106 logperror(const char *str
)
108 if (ilbd_debugging
== B_TRUE
)
109 (void) fprintf(stderr
, "%s: %s\n", str
, strerror(errno
));
111 syslog(LOG_ERR
, "%s: %m", str
);
116 ilbd_check_client_config_auth(const struct passwd
*pwd
)
118 if (chkauthattr(NET_ILB_CONFIG_AUTH
, pwd
->pw_name
) == 0) {
119 logdebug("user %s is not authorized for"
120 " configuration operation", pwd
->pw_name
);
121 return (ILB_STATUS_CFGAUTH
);
123 return (ILB_STATUS_OK
);
128 ilbd_check_client_enable_auth(const struct passwd
*pwd
)
130 if (chkauthattr(NET_ILB_ENABLE_AUTH
, pwd
->pw_name
) == 0) {
131 logdebug("user %s is not authorized for"
132 " enable/disable operation", pwd
->pw_name
);
133 return (ILB_STATUS_CFGAUTH
);
135 return (ILB_STATUS_OK
);
140 * input param. "err" should be one of the errnos defined in
141 * /usr/include/sys/errno.h
142 * this list is NOT complete.
145 ilb_map_errno2ilbstat(int err
)
147 ilb_status_t rc
= ILB_STATUS_INTERNAL
;
151 rc
= ILB_STATUS_OK
; /* for completeness' sake */
154 rc
= ILB_STATUS_EINVAL
;
157 rc
= ILB_STATUS_ENOENT
;
160 rc
= ILB_STATUS_ENOMEM
;
163 rc
= ILB_STATUS_INPROGRESS
;
166 rc
= ILB_STATUS_EEXIST
;
173 i_get_kcmd_sz(void *cmdp
)
177 switch (((ilb_rule_cmd_t
*)cmdp
)->cmd
) {
178 case ILB_DESTROY_RULE
:
179 case ILB_ENABLE_RULE
:
180 case ILB_DISABLE_RULE
:
181 sz
= sizeof (ilb_name_cmd_t
);
183 case ILB_CREATE_RULE
:
185 sz
= sizeof (ilb_rule_cmd_t
);
188 sz
= sizeof (ilb_num_rules_cmd_t
);
190 case ILB_NUM_SERVERS
:
191 sz
= sizeof (ilb_num_servers_cmd_t
);
193 case ILB_ADD_SERVERS
: {
194 ilb_servers_info_cmd_t
*kcmd
= (ilb_servers_info_cmd_t
*)cmdp
;
196 sz
= sizeof (*kcmd
) + ((kcmd
->num_servers
- 1) *
197 sizeof (kcmd
->servers
));
200 case ILB_RULE_NAMES
: {
201 ilb_rule_names_cmd_t
*kcmd
= (ilb_rule_names_cmd_t
*)cmdp
;
203 sz
= sizeof (*kcmd
) +
204 ((kcmd
->num_names
- 1) * sizeof (kcmd
->buf
));
207 case ILB_DEL_SERVERS
:
208 case ILB_ENABLE_SERVERS
:
209 case ILB_DISABLE_SERVERS
: {
210 ilb_servers_cmd_t
*kcmd
= (ilb_servers_cmd_t
*)cmdp
;
212 sz
= sizeof (*kcmd
) +
213 ((kcmd
->num_servers
- 1) * sizeof (kcmd
->servers
));
223 * parameter 'sz' is optional (indicated by == 0); if it's not set
224 * we try to derive it from cmdp->cmd
227 do_ioctl(void *cmdp
, ssize_t sz
)
233 ksock
= socket(AF_INET
, SOCK_DGRAM
, 0);
235 logperror("do_ioctl: AF_INET socket call"
237 return (ILB_STATUS_INTERNAL
);
241 (void) memset(&ioc
, 0, sizeof (ioc
));
242 ioc
.ic_cmd
= SIOCILB
;
247 sz
= i_get_kcmd_sz(cmdp
);
250 logdebug("do_ioctl: unknown command");
251 return (ILB_STATUS_INVAL_CMD
);
257 i_rc
= ioctl(ksock
, I_STR
, (caddr_t
)&ioc
);
259 logdebug("do_ioctl: SIOCILB ioctl (%d) failed: %s",
260 *(ilb_cmd_t
*)cmdp
, strerror(errno
));
261 return (ilb_map_errno2ilbstat(errno
));
264 return (ILB_STATUS_OK
);
268 * Create an OK reply to a client request. It is assumed that the passed
269 * in buffer is large enough to hold the reply.
272 ilbd_reply_ok(uint32_t *rbuf
, size_t *rbufsz
)
274 ilb_comm_t
*ic
= (ilb_comm_t
*)rbuf
;
276 ic
->ic_cmd
= ILBD_CMD_OK
;
277 /* Default is one exchange of request/response. */
278 ic
->ic_flags
= ILB_COMM_END
;
279 *rbufsz
= sizeof (ilb_comm_t
);
283 * Create an error reply to a client request. It is assumed that the passed
284 * in buffer is large enough to hold the reply.
287 ilbd_reply_err(uint32_t *rbuf
, size_t *rbufsz
, ilb_status_t status
)
289 ilb_comm_t
*ic
= (ilb_comm_t
*)rbuf
;
291 ic
->ic_cmd
= ILBD_CMD_ERROR
;
292 /* Default is one exchange of request/response. */
293 ic
->ic_flags
= ILB_COMM_END
;
294 *(ilb_status_t
*)&ic
->ic_data
= status
;
295 *rbufsz
= sizeof (ilb_comm_t
) + sizeof (ilb_status_t
);