2006-06-20 00:20:45 +02:00
|
|
|
/*-
|
2023-05-10 17:40:58 +02:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2017-11-27 16:37:16 +01:00
|
|
|
*
|
2006-06-20 00:20:45 +02:00
|
|
|
* Copyright (c) 2006 Max Laier. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
|
|
|
static const char rcsid[] =
|
|
|
|
"$FreeBSD$";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
2015-09-27 09:51:18 +02:00
|
|
|
#include <sys/param.h>
|
2006-06-20 00:20:45 +02:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2021-02-27 09:17:04 +01:00
|
|
|
#include <libifconfig.h>
|
|
|
|
|
2006-06-20 00:20:45 +02:00
|
|
|
#include "ifconfig.h"
|
|
|
|
|
|
|
|
static void
|
2023-05-23 19:02:12 +02:00
|
|
|
setifgroup(if_ctx *ctx, const char *group_name, int dummy __unused)
|
2006-06-20 00:20:45 +02:00
|
|
|
{
|
|
|
|
struct ifgroupreq ifgr;
|
|
|
|
|
|
|
|
memset(&ifgr, 0, sizeof(ifgr));
|
|
|
|
strlcpy(ifgr.ifgr_name, name, IFNAMSIZ);
|
|
|
|
|
|
|
|
if (group_name[0] && isdigit(group_name[strlen(group_name) - 1]))
|
|
|
|
errx(1, "setifgroup: group names may not end in a digit");
|
|
|
|
|
|
|
|
if (strlcpy(ifgr.ifgr_group, group_name, IFNAMSIZ) >= IFNAMSIZ)
|
|
|
|
errx(1, "setifgroup: group name too long");
|
2023-05-23 19:02:12 +02:00
|
|
|
if (ioctl_ctx(ctx, SIOCAIFGROUP, (caddr_t)&ifgr) == -1 && errno != EEXIST)
|
2006-06-20 00:20:45 +02:00
|
|
|
err(1," SIOCAIFGROUP");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2023-05-23 19:02:12 +02:00
|
|
|
unsetifgroup(if_ctx *ctx, const char *group_name, int dummy __unused)
|
2006-06-20 00:20:45 +02:00
|
|
|
{
|
|
|
|
struct ifgroupreq ifgr;
|
|
|
|
|
|
|
|
memset(&ifgr, 0, sizeof(ifgr));
|
|
|
|
strlcpy(ifgr.ifgr_name, name, IFNAMSIZ);
|
|
|
|
|
|
|
|
if (group_name[0] && isdigit(group_name[strlen(group_name) - 1]))
|
|
|
|
errx(1, "unsetifgroup: group names may not end in a digit");
|
|
|
|
|
|
|
|
if (strlcpy(ifgr.ifgr_group, group_name, IFNAMSIZ) >= IFNAMSIZ)
|
|
|
|
errx(1, "unsetifgroup: group name too long");
|
2023-05-23 19:02:12 +02:00
|
|
|
if (ioctl_ctx(ctx, SIOCDIFGROUP, (caddr_t)&ifgr) == -1 && errno != ENOENT)
|
2006-06-20 00:20:45 +02:00
|
|
|
err(1, "SIOCDIFGROUP");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2023-05-23 19:02:12 +02:00
|
|
|
getifgroups(if_ctx *ctx __unused)
|
2006-06-20 00:20:45 +02:00
|
|
|
{
|
2021-02-27 09:17:04 +01:00
|
|
|
struct ifgroupreq ifgr;
|
|
|
|
size_t cnt;
|
2006-06-20 00:20:45 +02:00
|
|
|
|
2021-02-27 09:17:04 +01:00
|
|
|
if (ifconfig_get_groups(lifh, name, &ifgr) == -1)
|
2021-02-28 11:30:09 +01:00
|
|
|
return;
|
2006-06-20 00:20:45 +02:00
|
|
|
|
|
|
|
cnt = 0;
|
2021-02-27 09:17:04 +01:00
|
|
|
for (size_t i = 0; i < ifgr.ifgr_len / sizeof(struct ifg_req); ++i) {
|
|
|
|
struct ifg_req *ifg = &ifgr.ifgr_groups[i];
|
|
|
|
|
2006-06-20 00:20:45 +02:00
|
|
|
if (strcmp(ifg->ifgrq_group, "all")) {
|
|
|
|
if (cnt == 0)
|
2019-02-26 04:34:47 +01:00
|
|
|
printf("\tgroups:");
|
2006-06-20 00:20:45 +02:00
|
|
|
cnt++;
|
2019-02-26 04:34:47 +01:00
|
|
|
printf(" %s", ifg->ifgrq_group);
|
2006-06-20 00:20:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (cnt)
|
|
|
|
printf("\n");
|
2014-11-19 14:57:39 +01:00
|
|
|
|
|
|
|
free(ifgr.ifgr_groups);
|
2006-06-20 00:20:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-02-17 19:22:20 +01:00
|
|
|
printgroup(const char *groupname)
|
2006-06-20 00:20:45 +02:00
|
|
|
{
|
|
|
|
struct ifgroupreq ifgr;
|
|
|
|
struct ifg_req *ifg;
|
2023-05-23 13:04:09 +02:00
|
|
|
unsigned int len;
|
2006-06-20 00:20:45 +02:00
|
|
|
int s;
|
|
|
|
|
2009-03-15 23:33:18 +01:00
|
|
|
s = socket(AF_LOCAL, SOCK_DGRAM, 0);
|
2006-06-20 00:20:45 +02:00
|
|
|
if (s == -1)
|
2009-03-15 23:33:18 +01:00
|
|
|
err(1, "socket(AF_LOCAL,SOCK_DGRAM)");
|
2006-06-20 00:20:45 +02:00
|
|
|
bzero(&ifgr, sizeof(ifgr));
|
|
|
|
strlcpy(ifgr.ifgr_name, groupname, sizeof(ifgr.ifgr_name));
|
|
|
|
if (ioctl(s, SIOCGIFGMEMB, (caddr_t)&ifgr) == -1) {
|
|
|
|
if (errno == EINVAL || errno == ENOTTY ||
|
|
|
|
errno == ENOENT)
|
2018-04-17 14:54:58 +02:00
|
|
|
exit(exit_code);
|
2006-06-20 00:20:45 +02:00
|
|
|
else
|
|
|
|
err(1, "SIOCGIFGMEMB");
|
|
|
|
}
|
|
|
|
|
|
|
|
len = ifgr.ifgr_len;
|
|
|
|
if ((ifgr.ifgr_groups = calloc(1, len)) == NULL)
|
|
|
|
err(1, "printgroup");
|
|
|
|
if (ioctl(s, SIOCGIFGMEMB, (caddr_t)&ifgr) == -1)
|
|
|
|
err(1, "SIOCGIFGMEMB");
|
|
|
|
|
|
|
|
for (ifg = ifgr.ifgr_groups; ifg && len >= sizeof(struct ifg_req);
|
|
|
|
ifg++) {
|
|
|
|
len -= sizeof(struct ifg_req);
|
|
|
|
printf("%s\n", ifg->ifgrq_member);
|
|
|
|
}
|
|
|
|
free(ifgr.ifgr_groups);
|
|
|
|
|
2018-04-17 14:54:58 +02:00
|
|
|
exit(exit_code);
|
2006-06-20 00:20:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct cmd group_cmds[] = {
|
|
|
|
DEF_CMD_ARG("group", setifgroup),
|
|
|
|
DEF_CMD_ARG("-group", unsetifgroup),
|
|
|
|
};
|
2023-05-23 13:04:09 +02:00
|
|
|
|
2006-06-20 00:20:45 +02:00
|
|
|
static struct afswtch af_group = {
|
|
|
|
.af_name = "af_group",
|
|
|
|
.af_af = AF_UNSPEC,
|
|
|
|
.af_other_status = getifgroups,
|
|
|
|
};
|
2023-05-23 13:04:09 +02:00
|
|
|
|
|
|
|
static struct option group_gopt = {
|
|
|
|
.opt = "g:",
|
|
|
|
.opt_usage = "[-g groupname]",
|
|
|
|
.cb = printgroup,
|
|
|
|
};
|
2006-06-20 00:20:45 +02:00
|
|
|
|
|
|
|
static __constructor void
|
|
|
|
group_ctor(void)
|
|
|
|
{
|
2023-05-23 13:04:09 +02:00
|
|
|
for (size_t i = 0; i < nitems(group_cmds); i++)
|
2006-06-20 00:20:45 +02:00
|
|
|
cmd_register(&group_cmds[i]);
|
|
|
|
af_register(&af_group);
|
|
|
|
opt_register(&group_gopt);
|
|
|
|
}
|