2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2006 Max Laier. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 static const char rcsid[] =
33 #include <sys/param.h>
34 #include <sys/ioctl.h>
35 #include <sys/socket.h>
50 setifgroup(const char *group_name, int d, int s, const struct afswtch *rafp)
52 struct ifgroupreq ifgr;
54 memset(&ifgr, 0, sizeof(ifgr));
55 strlcpy(ifgr.ifgr_name, name, IFNAMSIZ);
57 if (group_name[0] && isdigit(group_name[strlen(group_name) - 1]))
58 errx(1, "setifgroup: group names may not end in a digit");
60 if (strlcpy(ifgr.ifgr_group, group_name, IFNAMSIZ) >= IFNAMSIZ)
61 errx(1, "setifgroup: group name too long");
62 if (ioctl(s, SIOCAIFGROUP, (caddr_t)&ifgr) == -1 && errno != EEXIST)
63 err(1," SIOCAIFGROUP");
68 unsetifgroup(const char *group_name, int d, int s, const struct afswtch *rafp)
70 struct ifgroupreq ifgr;
72 memset(&ifgr, 0, sizeof(ifgr));
73 strlcpy(ifgr.ifgr_name, name, IFNAMSIZ);
75 if (group_name[0] && isdigit(group_name[strlen(group_name) - 1]))
76 errx(1, "unsetifgroup: group names may not end in a digit");
78 if (strlcpy(ifgr.ifgr_group, group_name, IFNAMSIZ) >= IFNAMSIZ)
79 errx(1, "unsetifgroup: group name too long");
80 if (ioctl(s, SIOCDIFGROUP, (caddr_t)&ifgr) == -1 && errno != ENOENT)
81 err(1, "SIOCDIFGROUP");
88 struct ifgroupreq ifgr;
91 memset(&ifgr, 0, sizeof(ifgr));
92 strlcpy(ifgr.ifgr_name, name, IFNAMSIZ);
94 if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1) {
95 if (errno == EINVAL || errno == ENOTTY)
98 err(1, "SIOCGIFGROUP");
103 (struct ifg_req *)calloc(len / sizeof(struct ifg_req),
104 sizeof(struct ifg_req));
105 if (ifgr.ifgr_groups == NULL)
106 err(1, "getifgroups");
107 if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1)
108 err(1, "SIOCGIFGROUP");
111 ifg = ifgr.ifgr_groups;
112 for (; ifg && len >= sizeof(struct ifg_req); ifg++) {
113 len -= sizeof(struct ifg_req);
114 if (strcmp(ifg->ifgrq_group, "all")) {
118 printf(" %s", ifg->ifgrq_group);
124 free(ifgr.ifgr_groups);
128 printgroup(const char *groupname)
130 struct ifgroupreq ifgr;
135 s = socket(AF_LOCAL, SOCK_DGRAM, 0);
137 err(1, "socket(AF_LOCAL,SOCK_DGRAM)");
138 bzero(&ifgr, sizeof(ifgr));
139 strlcpy(ifgr.ifgr_name, groupname, sizeof(ifgr.ifgr_name));
140 if (ioctl(s, SIOCGIFGMEMB, (caddr_t)&ifgr) == -1) {
141 if (errno == EINVAL || errno == ENOTTY ||
145 err(1, "SIOCGIFGMEMB");
149 if ((ifgr.ifgr_groups = calloc(1, len)) == NULL)
150 err(1, "printgroup");
151 if (ioctl(s, SIOCGIFGMEMB, (caddr_t)&ifgr) == -1)
152 err(1, "SIOCGIFGMEMB");
154 for (ifg = ifgr.ifgr_groups; ifg && len >= sizeof(struct ifg_req);
156 len -= sizeof(struct ifg_req);
157 printf("%s\n", ifg->ifgrq_member);
160 free(ifgr.ifgr_groups);
165 static struct cmd group_cmds[] = {
166 DEF_CMD_ARG("group", setifgroup),
167 DEF_CMD_ARG("-group", unsetifgroup),
169 static struct afswtch af_group = {
170 .af_name = "af_group",
172 .af_other_status = getifgroups,
174 static struct option group_gopt = { "g:", "[-g groupname]", printgroup };
176 static __constructor void
181 for (i = 0; i < nitems(group_cmds); i++)
182 cmd_register(&group_cmds[i]);
183 af_register(&af_group);
184 opt_register(&group_gopt);