xref: /freebsd/sbin/ifconfig/ifgroup.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 #include <sys/param.h>
29 #include <sys/ioctl.h>
30 #include <sys/socket.h>
31 #include <net/if.h>
32 
33 #include <ctype.h>
34 #include <err.h>
35 #include <errno.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 
41 #include <libifconfig.h>
42 
43 #include "ifconfig.h"
44 
45 static void
46 setifgroup(if_ctx *ctx, const char *group_name, int dummy __unused)
47 {
48 	struct ifgroupreq ifgr = {};
49 
50 	strlcpy(ifgr.ifgr_name, ctx->ifname, IFNAMSIZ);
51 
52 	if (group_name[0] && isdigit(group_name[strlen(group_name) - 1]))
53 		errx(1, "setifgroup: group names may not end in a digit");
54 
55 	if (strlcpy(ifgr.ifgr_group, group_name, IFNAMSIZ) >= IFNAMSIZ)
56 		errx(1, "setifgroup: group name too long");
57 	if (ioctl_ctx(ctx, SIOCAIFGROUP, (caddr_t)&ifgr) == -1 && errno != EEXIST)
58 		err(1," SIOCAIFGROUP");
59 }
60 
61 static void
62 unsetifgroup(if_ctx *ctx, const char *group_name, int dummy __unused)
63 {
64 	struct ifgroupreq ifgr = {};
65 
66 	strlcpy(ifgr.ifgr_name, ctx->ifname, IFNAMSIZ);
67 
68 	if (group_name[0] && isdigit(group_name[strlen(group_name) - 1]))
69 		errx(1, "unsetifgroup: group names may not end in a digit");
70 
71 	if (strlcpy(ifgr.ifgr_group, group_name, IFNAMSIZ) >= IFNAMSIZ)
72 		errx(1, "unsetifgroup: group name too long");
73 	if (ioctl_ctx(ctx, SIOCDIFGROUP, (caddr_t)&ifgr) == -1 && errno != ENOENT)
74 		err(1, "SIOCDIFGROUP");
75 }
76 
77 static void
78 getifgroups(if_ctx *ctx)
79 {
80 	struct ifgroupreq ifgr;
81 	size_t cnt;
82 
83 	if (ifconfig_get_groups(lifh, ctx->ifname, &ifgr) == -1)
84 		return;
85 
86 	cnt = 0;
87 	for (size_t i = 0; i < ifgr.ifgr_len / sizeof(struct ifg_req); ++i) {
88 		struct ifg_req *ifg = &ifgr.ifgr_groups[i];
89 
90 		if (strcmp(ifg->ifgrq_group, "all")) {
91 			if (cnt == 0)
92 				printf("\tgroups:");
93 			cnt++;
94 			printf(" %s", ifg->ifgrq_group);
95 		}
96 	}
97 	if (cnt)
98 		printf("\n");
99 
100 	free(ifgr.ifgr_groups);
101 }
102 
103 static void
104 printgroup(const char *groupname)
105 {
106 	struct ifgroupreq	 ifgr;
107 	struct ifg_req		*ifg;
108 	unsigned int		 len;
109 	int			 s;
110 
111 	s = socket(AF_LOCAL, SOCK_DGRAM, 0);
112 	if (s == -1)
113 		err(1, "socket(AF_LOCAL,SOCK_DGRAM)");
114 	bzero(&ifgr, sizeof(ifgr));
115 	strlcpy(ifgr.ifgr_name, groupname, sizeof(ifgr.ifgr_name));
116 	if (ioctl(s, SIOCGIFGMEMB, (caddr_t)&ifgr) == -1) {
117 		if (errno == EINVAL || errno == ENOTTY ||
118 		    errno == ENOENT)
119 			exit(exit_code);
120 		else
121 			err(1, "SIOCGIFGMEMB");
122 	}
123 
124 	len = ifgr.ifgr_len;
125 	if ((ifgr.ifgr_groups = calloc(1, len)) == NULL)
126 		err(1, "printgroup");
127 	if (ioctl(s, SIOCGIFGMEMB, (caddr_t)&ifgr) == -1)
128 		err(1, "SIOCGIFGMEMB");
129 
130 	for (ifg = ifgr.ifgr_groups; ifg && len >= sizeof(struct ifg_req);
131 	    ifg++) {
132 		len -= sizeof(struct ifg_req);
133 		printf("%s\n", ifg->ifgrq_member);
134 	}
135 	free(ifgr.ifgr_groups);
136 
137 	exit(exit_code);
138 }
139 
140 static struct cmd group_cmds[] = {
141 	DEF_CMD_ARG("group",	setifgroup),
142 	DEF_CMD_ARG("-group",	unsetifgroup),
143 };
144 
145 static struct afswtch af_group = {
146 	.af_name	= "af_group",
147 	.af_af		= AF_UNSPEC,
148 	.af_other_status = getifgroups,
149 };
150 
151 static struct option group_gopt = {
152 	.opt		= "g:",
153 	.opt_usage	= "[-g groupname]",
154 	.cb		= printgroup,
155 };
156 
157 static __constructor void
158 group_ctor(void)
159 {
160 	for (size_t i = 0; i < nitems(group_cmds);  i++)
161 		cmd_register(&group_cmds[i]);
162 	af_register(&af_group);
163 	opt_register(&group_gopt);
164 }
165