2 * Copyright (c) 2006 Max Laier. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 static const char rcsid[] =
31 #include <sys/param.h>
32 #include <sys/ioctl.h>
33 #include <sys/socket.h>
48 setifgroup(const char *group_name, int d, int s, const struct afswtch *rafp)
50 struct ifgroupreq ifgr;
52 memset(&ifgr, 0, sizeof(ifgr));
53 strlcpy(ifgr.ifgr_name, name, IFNAMSIZ);
55 if (group_name[0] && isdigit(group_name[strlen(group_name) - 1]))
56 errx(1, "setifgroup: group names may not end in a digit");
58 if (strlcpy(ifgr.ifgr_group, group_name, IFNAMSIZ) >= IFNAMSIZ)
59 errx(1, "setifgroup: group name too long");
60 if (ioctl(s, SIOCAIFGROUP, (caddr_t)&ifgr) == -1 && errno != EEXIST)
61 err(1," SIOCAIFGROUP");
66 unsetifgroup(const char *group_name, int d, int s, const struct afswtch *rafp)
68 struct ifgroupreq ifgr;
70 memset(&ifgr, 0, sizeof(ifgr));
71 strlcpy(ifgr.ifgr_name, name, IFNAMSIZ);
73 if (group_name[0] && isdigit(group_name[strlen(group_name) - 1]))
74 errx(1, "unsetifgroup: group names may not end in a digit");
76 if (strlcpy(ifgr.ifgr_group, group_name, IFNAMSIZ) >= IFNAMSIZ)
77 errx(1, "unsetifgroup: group name too long");
78 if (ioctl(s, SIOCDIFGROUP, (caddr_t)&ifgr) == -1 && errno != ENOENT)
79 err(1, "SIOCDIFGROUP");
86 struct ifgroupreq ifgr;
92 memset(&ifgr, 0, sizeof(ifgr));
93 strlcpy(ifgr.ifgr_name, name, IFNAMSIZ);
95 if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1) {
96 if (errno == EINVAL || errno == ENOTTY)
99 err(1, "SIOCGIFGROUP");
104 (struct ifg_req *)calloc(len / sizeof(struct ifg_req),
105 sizeof(struct ifg_req));
106 if (ifgr.ifgr_groups == NULL)
107 err(1, "getifgroups");
108 if (ioctl(s, SIOCGIFGROUP, (caddr_t)&ifgr) == -1)
109 err(1, "SIOCGIFGROUP");
112 ifg = ifgr.ifgr_groups;
113 for (; ifg && len >= sizeof(struct ifg_req); ifg++) {
114 len -= sizeof(struct ifg_req);
115 if (strcmp(ifg->ifgrq_group, "all")) {
117 printf("\tgroups: ");
119 printf("%s ", ifg->ifgrq_group);
127 printgroup(const char *groupname)
129 struct ifgroupreq ifgr;
134 s = socket(AF_LOCAL, SOCK_DGRAM, 0);
136 err(1, "socket(AF_LOCAL,SOCK_DGRAM)");
137 bzero(&ifgr, sizeof(ifgr));
138 strlcpy(ifgr.ifgr_name, groupname, sizeof(ifgr.ifgr_name));
139 if (ioctl(s, SIOCGIFGMEMB, (caddr_t)&ifgr) == -1) {
140 if (errno == EINVAL || errno == ENOTTY ||
144 err(1, "SIOCGIFGMEMB");
148 if ((ifgr.ifgr_groups = calloc(1, len)) == NULL)
149 err(1, "printgroup");
150 if (ioctl(s, SIOCGIFGMEMB, (caddr_t)&ifgr) == -1)
151 err(1, "SIOCGIFGMEMB");
153 for (ifg = ifgr.ifgr_groups; ifg && len >= sizeof(struct ifg_req);
155 len -= sizeof(struct ifg_req);
156 printf("%s\n", ifg->ifgrq_member);
159 free(ifgr.ifgr_groups);
164 static struct cmd group_cmds[] = {
165 DEF_CMD_ARG("group", setifgroup),
166 DEF_CMD_ARG("-group", unsetifgroup),
168 static struct afswtch af_group = {
169 .af_name = "af_group",
171 .af_other_status = getifgroups,
173 static struct option group_gopt = { "g:", "[-g groupname]", printgroup };
175 static __constructor void
180 for (i = 0; i < nitems(group_cmds); i++)
181 cmd_register(&group_cmds[i]);
182 af_register(&af_group);
183 opt_register(&group_gopt);