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
27 * $FreeBSD: head/sbin/ifconfig/ifgroup.c 326276 2017-11-27 15:37:16Z pfg $
30 #include <sys/param.h>
31 #include <sys/ioctl.h>
32 #include <sys/socket.h>
46 setifgroup(const char *group_name, int d __unused, int s,
47 const struct afswtch *rafp __unused)
49 struct ifgroupreq ifgr;
51 memset(&ifgr, 0, sizeof(ifgr));
52 strlcpy(ifgr.ifgr_name, name, IFNAMSIZ);
54 if (group_name[0] && isdigit(group_name[strlen(group_name) - 1]))
55 errx(1, "setifgroup: group names may not end in a digit");
57 if (strlcpy(ifgr.ifgr_group, group_name, IFNAMSIZ) >= IFNAMSIZ)
58 errx(1, "setifgroup: group name too long");
59 if (ioctl(s, SIOCAIFGROUP, (caddr_t)&ifgr) == -1 && errno != EEXIST)
60 err(1," SIOCAIFGROUP");
64 unsetifgroup(const char *group_name, int d __unused, int s,
65 const struct afswtch *rafp __unused)
67 struct ifgroupreq ifgr;
69 memset(&ifgr, 0, sizeof(ifgr));
70 strlcpy(ifgr.ifgr_name, name, IFNAMSIZ);
72 if (group_name[0] && isdigit(group_name[strlen(group_name) - 1]))
73 errx(1, "unsetifgroup: group names may not end in a digit");
75 if (strlcpy(ifgr.ifgr_group, group_name, IFNAMSIZ) >= IFNAMSIZ)
76 errx(1, "unsetifgroup: group name too long");
77 if (ioctl(s, SIOCDIFGROUP, (caddr_t)&ifgr) == -1 && errno != ENOENT)
78 err(1, "SIOCDIFGROUP");
84 struct ifgroupreq ifgr;
88 memset(&ifgr, 0, sizeof(ifgr));
89 strlcpy(ifgr.ifgr_name, name, IFNAMSIZ);
91 if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1) {
92 if (errno == EINVAL || errno == ENOTTY)
95 err(1, "SIOCGIFGROUP");
99 ifgr.ifgr_groups = calloc(len / sizeof(struct ifg_req),
100 sizeof(struct ifg_req));
101 if (ifgr.ifgr_groups == NULL)
102 err(1, "getifgroups");
103 if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1)
104 err(1, "SIOCGIFGROUP");
107 ifg = ifgr.ifgr_groups;
108 for (; ifg && len >= sizeof(struct ifg_req); ifg++) {
109 len -= sizeof(struct ifg_req);
110 if (strcmp(ifg->ifgrq_group, "all") != 0) {
112 printf("\tgroups: ");
114 printf("%s ", ifg->ifgrq_group);
120 free(ifgr.ifgr_groups);
124 printgroup(const char *groupname)
126 struct ifgroupreq ifgr;
131 s = socket(AF_LOCAL, SOCK_DGRAM, 0);
133 err(1, "socket(AF_LOCAL,SOCK_DGRAM)");
135 memset(&ifgr, 0, sizeof(ifgr));
136 strlcpy(ifgr.ifgr_name, groupname, sizeof(ifgr.ifgr_name));
137 if (ioctl(s, SIOCGIFGMEMB, (caddr_t)&ifgr) == -1) {
138 if (errno == EINVAL || errno == ENOTTY ||
142 err(1, "SIOCGIFGMEMB");
146 if ((ifgr.ifgr_groups = calloc(1, len)) == NULL)
147 err(1, "printgroup");
148 if (ioctl(s, SIOCGIFGMEMB, (caddr_t)&ifgr) == -1)
149 err(1, "SIOCGIFGMEMB");
151 for (ifg = ifgr.ifgr_groups;
152 ifg && len >= sizeof(struct ifg_req);
154 len -= sizeof(struct ifg_req);
155 printf("%s\n", ifg->ifgrq_member);
158 free(ifgr.ifgr_groups);
163 static struct cmd group_cmds[] = {
164 DEF_CMD_ARG("group", setifgroup),
165 DEF_CMD_ARG("-group", unsetifgroup),
167 static struct afswtch af_group = {
168 .af_name = "af_group",
170 .af_other_status = getifgroups,
172 static struct option group_gopt = { "g:", "[-g groupname]", printgroup, NULL };
174 static __constructor(101) void
179 for (i = 0; i < nitems(group_cmds); i++)
180 cmd_register(&group_cmds[i]);
181 af_register(&af_group);
182 opt_register(&group_gopt);