xref: /freebsd/sbin/ifconfig/ifgroup.c (revision ee21ee1572d40a3b74f18638dae38c1a9ad1e9e3)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2006 Max Laier. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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
25  * SUCH DAMAGE.
26  */
27 
28 #ifndef lint
29 static const char rcsid[] =
30   "$FreeBSD$";
31 #endif /* not lint */
32 
33 #include <sys/param.h>
34 #include <sys/ioctl.h>
35 #include <sys/socket.h>
36 #include <net/if.h>
37 
38 #include <ctype.h>
39 #include <err.h>
40 #include <errno.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 
46 #include <libifconfig.h>
47 
48 #include "ifconfig.h"
49 
50 /* ARGSUSED */
51 static void
52 setifgroup(const char *group_name, int d, int s, const struct afswtch *rafp)
53 {
54 	struct ifgroupreq ifgr;
55 
56 	memset(&ifgr, 0, sizeof(ifgr));
57 	strlcpy(ifgr.ifgr_name, name, IFNAMSIZ);
58 
59 	if (group_name[0] && isdigit(group_name[strlen(group_name) - 1]))
60 		errx(1, "setifgroup: group names may not end in a digit");
61 
62 	if (strlcpy(ifgr.ifgr_group, group_name, IFNAMSIZ) >= IFNAMSIZ)
63 		errx(1, "setifgroup: group name too long");
64 	if (ioctl(s, SIOCAIFGROUP, (caddr_t)&ifgr) == -1 && errno != EEXIST)
65 		err(1," SIOCAIFGROUP");
66 }
67 
68 /* ARGSUSED */
69 static void
70 unsetifgroup(const char *group_name, int d, int s, const struct afswtch *rafp)
71 {
72 	struct ifgroupreq ifgr;
73 
74 	memset(&ifgr, 0, sizeof(ifgr));
75 	strlcpy(ifgr.ifgr_name, name, IFNAMSIZ);
76 
77 	if (group_name[0] && isdigit(group_name[strlen(group_name) - 1]))
78 		errx(1, "unsetifgroup: group names may not end in a digit");
79 
80 	if (strlcpy(ifgr.ifgr_group, group_name, IFNAMSIZ) >= IFNAMSIZ)
81 		errx(1, "unsetifgroup: group name too long");
82 	if (ioctl(s, SIOCDIFGROUP, (caddr_t)&ifgr) == -1 && errno != ENOENT)
83 		err(1, "SIOCDIFGROUP");
84 }
85 
86 static void
87 getifgroups(int s)
88 {
89 	ifconfig_handle_t *lifh;
90 	struct ifgroupreq ifgr;
91 	size_t cnt;
92 
93 	lifh = ifconfig_open();
94 	if (lifh == NULL)
95 		return;
96 
97 	if (ifconfig_get_groups(lifh, name, &ifgr) == -1)
98 		goto close;
99 
100 	cnt = 0;
101 	for (size_t i = 0; i < ifgr.ifgr_len / sizeof(struct ifg_req); ++i) {
102 		struct ifg_req *ifg = &ifgr.ifgr_groups[i];
103 
104 		if (strcmp(ifg->ifgrq_group, "all")) {
105 			if (cnt == 0)
106 				printf("\tgroups:");
107 			cnt++;
108 			printf(" %s", ifg->ifgrq_group);
109 		}
110 	}
111 	if (cnt)
112 		printf("\n");
113 
114 	free(ifgr.ifgr_groups);
115 close:
116 	ifconfig_close(lifh);
117 }
118 
119 static void
120 printgroup(const char *groupname)
121 {
122 	struct ifgroupreq	 ifgr;
123 	struct ifg_req		*ifg;
124 	int			 len, cnt = 0;
125 	int			 s;
126 
127 	s = socket(AF_LOCAL, SOCK_DGRAM, 0);
128 	if (s == -1)
129 		err(1, "socket(AF_LOCAL,SOCK_DGRAM)");
130 	bzero(&ifgr, sizeof(ifgr));
131 	strlcpy(ifgr.ifgr_name, groupname, sizeof(ifgr.ifgr_name));
132 	if (ioctl(s, SIOCGIFGMEMB, (caddr_t)&ifgr) == -1) {
133 		if (errno == EINVAL || errno == ENOTTY ||
134 		    errno == ENOENT)
135 			exit(exit_code);
136 		else
137 			err(1, "SIOCGIFGMEMB");
138 	}
139 
140 	len = ifgr.ifgr_len;
141 	if ((ifgr.ifgr_groups = calloc(1, len)) == NULL)
142 		err(1, "printgroup");
143 	if (ioctl(s, SIOCGIFGMEMB, (caddr_t)&ifgr) == -1)
144 		err(1, "SIOCGIFGMEMB");
145 
146 	for (ifg = ifgr.ifgr_groups; ifg && len >= sizeof(struct ifg_req);
147 	    ifg++) {
148 		len -= sizeof(struct ifg_req);
149 		printf("%s\n", ifg->ifgrq_member);
150 		cnt++;
151 	}
152 	free(ifgr.ifgr_groups);
153 
154 	exit(exit_code);
155 }
156 
157 static struct cmd group_cmds[] = {
158 	DEF_CMD_ARG("group",	setifgroup),
159 	DEF_CMD_ARG("-group",	unsetifgroup),
160 };
161 static struct afswtch af_group = {
162 	.af_name	= "af_group",
163 	.af_af		= AF_UNSPEC,
164 	.af_other_status = getifgroups,
165 };
166 static struct option group_gopt = { "g:", "[-g groupname]", printgroup };
167 
168 static __constructor void
169 group_ctor(void)
170 {
171 	int i;
172 
173 	for (i = 0; i < nitems(group_cmds);  i++)
174 		cmd_register(&group_cmds[i]);
175 	af_register(&af_group);
176 	opt_register(&group_gopt);
177 }
178