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