spppcontrol.c revision 330449
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 1997, 2001 Joerg Wunsch
5 *
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: stable/11/sbin/spppcontrol/spppcontrol.c 330449 2018-03-05 07:26:05Z eadler $");
31
32#include <sys/types.h>
33#include <sys/ioctl.h>
34#include <sys/socket.h>
35
36#include <net/if.h>
37#include <net/if_sppp.h>
38
39#include <err.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43#include <sysexits.h>
44#include <unistd.h>
45
46static void usage(void);
47void	print_vals(const char *ifname, struct spppreq *sp);
48const char *phase_name(enum ppp_phase phase);
49const char *proto_name(u_short proto);
50const char *authflags(u_short flags);
51
52#define PPP_PAP		0xc023
53#define PPP_CHAP	0xc223
54
55int
56main(int argc, char **argv)
57{
58	int s, c;
59	int errs = 0, verbose = 0;
60	size_t off;
61	long to;
62	char *endp;
63	const char *ifname, *cp;
64	struct ifreq ifr;
65	struct spppreq spr;
66
67	while ((c = getopt(argc, argv, "v")) != -1)
68		switch (c) {
69		case 'v':
70			verbose++;
71			break;
72
73		default:
74			errs++;
75			break;
76		}
77	argv += optind;
78	argc -= optind;
79
80	if (errs || argc < 1)
81		usage();
82
83	ifname = argv[0];
84	strncpy(ifr.ifr_name, ifname, sizeof ifr.ifr_name);
85
86	/* use a random AF to create the socket */
87	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
88		err(EX_UNAVAILABLE, "ifconfig: socket");
89
90	argc--;
91	argv++;
92
93	spr.cmd = (uintptr_t) SPPPIOGDEFS;
94	ifr.ifr_data = (caddr_t)&spr;
95
96	if (ioctl(s, SIOCGIFGENERIC, &ifr) == -1)
97		err(EX_OSERR, "SIOCGIFGENERIC(SPPPIOGDEFS)");
98
99	if (argc == 0) {
100		/* list only mode */
101		print_vals(ifname, &spr);
102		return 0;
103	}
104
105#define startswith(s) strncmp(argv[0], s, (off = strlen(s))) == 0
106
107	while (argc > 0) {
108		if (startswith("authproto=")) {
109			cp = argv[0] + off;
110			if (strcmp(cp, "pap") == 0)
111				spr.defs.myauth.proto =
112					spr.defs.hisauth.proto = PPP_PAP;
113			else if (strcmp(cp, "chap") == 0)
114				spr.defs.myauth.proto =
115					spr.defs.hisauth.proto = PPP_CHAP;
116			else if (strcmp(cp, "none") == 0)
117				spr.defs.myauth.proto =
118					spr.defs.hisauth.proto = 0;
119			else
120				errx(EX_DATAERR, "bad auth proto: %s", cp);
121		} else if (startswith("myauthproto=")) {
122			cp = argv[0] + off;
123			if (strcmp(cp, "pap") == 0)
124				spr.defs.myauth.proto = PPP_PAP;
125			else if (strcmp(cp, "chap") == 0)
126				spr.defs.myauth.proto = PPP_CHAP;
127			else if (strcmp(cp, "none") == 0)
128				spr.defs.myauth.proto = 0;
129			else
130				errx(EX_DATAERR, "bad auth proto: %s", cp);
131		} else if (startswith("myauthname="))
132			strncpy(spr.defs.myauth.name, argv[0] + off,
133				AUTHNAMELEN);
134		else if (startswith("myauthsecret=") ||
135			 startswith("myauthkey="))
136			strncpy(spr.defs.myauth.secret, argv[0] + off,
137				AUTHKEYLEN);
138		else if (startswith("hisauthproto=")) {
139			cp = argv[0] + off;
140			if (strcmp(cp, "pap") == 0)
141				spr.defs.hisauth.proto = PPP_PAP;
142			else if (strcmp(cp, "chap") == 0)
143				spr.defs.hisauth.proto = PPP_CHAP;
144			else if (strcmp(cp, "none") == 0)
145				spr.defs.hisauth.proto = 0;
146			else
147				errx(EX_DATAERR, "bad auth proto: %s", cp);
148		} else if (startswith("hisauthname="))
149			strncpy(spr.defs.hisauth.name, argv[0] + off,
150				AUTHNAMELEN);
151		else if (startswith("hisauthsecret=") ||
152			 startswith("hisauthkey="))
153			strncpy(spr.defs.hisauth.secret, argv[0] + off,
154				AUTHKEYLEN);
155		else if (strcmp(argv[0], "callin") == 0)
156			spr.defs.hisauth.flags |= AUTHFLAG_NOCALLOUT;
157		else if (strcmp(argv[0], "always") == 0)
158			spr.defs.hisauth.flags &= ~AUTHFLAG_NOCALLOUT;
159		else if (strcmp(argv[0], "norechallenge") == 0)
160			spr.defs.hisauth.flags |= AUTHFLAG_NORECHALLENGE;
161		else if (strcmp(argv[0], "rechallenge") == 0)
162			spr.defs.hisauth.flags &= ~AUTHFLAG_NORECHALLENGE;
163		else if (startswith("lcp-timeout=")) {
164			cp = argv[0] + off;
165			to = strtol(cp, &endp, 10);
166			if (*cp == '\0' || *endp != '\0' ||
167			    /*
168			     * NB: 10 ms is the minimal possible value for
169			     * hz=100.  We assume no kernel has less clock
170			     * frequency than that...
171			     */
172			    to < 10 || to > 20000)
173				errx(EX_DATAERR, "bad lcp timeout value: %s",
174				     cp);
175			spr.defs.lcp.timeout = to;
176		} else if (strcmp(argv[0], "enable-vj") == 0)
177			spr.defs.enable_vj = 1;
178		else if (strcmp(argv[0], "disable-vj") == 0)
179			spr.defs.enable_vj = 0;
180		else if (strcmp(argv[0], "enable-ipv6") == 0)
181			spr.defs.enable_ipv6 = 1;
182		else if (strcmp(argv[0], "disable-ipv6") == 0)
183			spr.defs.enable_ipv6 = 0;
184		else
185			errx(EX_DATAERR, "bad parameter: \"%s\"", argv[0]);
186
187		argv++;
188		argc--;
189	}
190
191	spr.cmd = (uintptr_t)SPPPIOSDEFS;
192
193	if (ioctl(s, SIOCSIFGENERIC, &ifr) == -1)
194		err(EX_OSERR, "SIOCSIFGENERIC(SPPPIOSDEFS)");
195
196	if (verbose)
197		print_vals(ifname, &spr);
198
199	return 0;
200}
201
202static void
203usage(void)
204{
205	fprintf(stderr, "%s\n%s\n",
206	"usage: spppcontrol [-v] ifname [{my|his}auth{proto|name|secret}=...]",
207	"       spppcontrol [-v] ifname callin|always");
208	exit(EX_USAGE);
209}
210
211void
212print_vals(const char *ifname, struct spppreq *sp)
213{
214	printf("%s:\tphase=%s\n", ifname, phase_name(sp->defs.pp_phase));
215	if (sp->defs.myauth.proto) {
216		printf("\tmyauthproto=%s myauthname=\"%.*s\"\n",
217		       proto_name(sp->defs.myauth.proto),
218		       AUTHNAMELEN, sp->defs.myauth.name);
219	}
220	if (sp->defs.hisauth.proto) {
221		printf("\thisauthproto=%s hisauthname=\"%.*s\"%s\n",
222		       proto_name(sp->defs.hisauth.proto),
223		       AUTHNAMELEN, sp->defs.hisauth.name,
224		       authflags(sp->defs.hisauth.flags));
225	}
226	printf("\tlcp-timeout=%d ms\n", sp->defs.lcp.timeout);
227	printf("\t%sable-vj\n", sp->defs.enable_vj? "en": "dis");
228	printf("\t%sable-ipv6\n", sp->defs.enable_ipv6? "en": "dis");
229}
230
231const char *
232phase_name(enum ppp_phase phase)
233{
234	switch (phase) {
235	case PHASE_DEAD:	return "dead";
236	case PHASE_ESTABLISH:	return "establish";
237	case PHASE_TERMINATE:	return "terminate";
238	case PHASE_AUTHENTICATE: return "authenticate";
239	case PHASE_NETWORK:	return "network";
240	}
241	return "illegal";
242}
243
244const char *
245proto_name(u_short proto)
246{
247	static char buf[12];
248	switch (proto) {
249	case PPP_PAP:	return "pap";
250	case PPP_CHAP:	return "chap";
251	}
252	sprintf(buf, "0x%x", (unsigned)proto);
253	return buf;
254}
255
256const char *
257authflags(u_short flags)
258{
259	static char buf[30];
260	buf[0] = '\0';
261	if (flags & AUTHFLAG_NOCALLOUT)
262		strcat(buf, " callin");
263	if (flags & AUTHFLAG_NORECHALLENGE)
264		strcat(buf, " norechallenge");
265	return buf;
266}
267