spppcontrol.c revision 30303
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 * $Id$
27 */
28
29#include <sys/param.h>
30#include <sys/callout.h>
31#include <sys/ioctl.h>
32#include <sys/socket.h>
33#include <sys/time.h>
34
35#include <net/if.h>
36#include <net/if_var.h>
37#include <net/if_sppp.h>
38
39#include <err.h>
40#include <errno.h>
41#include <fcntl.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
45#include <sysexits.h>
46#include <unistd.h>
47
48void	usage(void);
49void	print_vals(const char *ifname, struct spppreq *sp);
50const char *phase_name(enum ppp_phase phase);
51const char *proto_name(u_short proto);
52const char *authflags(u_short flags);
53
54#define PPP_PAP		0xc023
55#define PPP_CHAP	0xc223
56
57int
58main(int argc, char **argv)
59{
60	int s, c;
61	int errs = 0, verbose = 0;
62	size_t off;
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 = (int)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
164			errx(EX_DATAERR, "bad parameter: \"%s\"", argv[0]);
165
166		argv++;
167		argc--;
168	}
169
170	spr.cmd = (int)SPPPIOSDEFS;
171
172	if (ioctl(s, SIOCSIFGENERIC, &ifr) == -1)
173		err(EX_OSERR, "SIOCSIFGENERIC(SPPPIOSDEFS)");
174
175	if (verbose)
176		print_vals(ifname, &spr);
177
178	return 0;
179}
180
181void
182usage(void)
183{
184	errx(EX_USAGE,
185	     "usage: [-v] ifname [{my|his}auth{proto|name|secret}=..."
186	     "|callin|always]");
187}
188
189void
190print_vals(const char *ifname, struct spppreq *sp)
191{
192	printf("%s:\tphase=%s\n", ifname, phase_name(sp->defs.pp_phase));
193	if (sp->defs.myauth.proto) {
194		printf("\tmyauthproto=%s myauthname=\"%.*s\"\n",
195		       proto_name(sp->defs.myauth.proto),
196		       AUTHNAMELEN, sp->defs.myauth.name);
197	}
198	if (sp->defs.hisauth.proto) {
199		printf("\thisauthproto=%s hisauthname=\"%.*s\"%s\n",
200		       proto_name(sp->defs.hisauth.proto),
201		       AUTHNAMELEN, sp->defs.hisauth.name,
202		       authflags(sp->defs.hisauth.flags));
203	}
204}
205
206const char *
207phase_name(enum ppp_phase phase)
208{
209	switch (phase) {
210	case PHASE_DEAD:	return "dead";
211	case PHASE_ESTABLISH:	return "establish";
212	case PHASE_TERMINATE:	return "terminate";
213	case PHASE_AUTHENTICATE: return "authenticate";
214	case PHASE_NETWORK:	return "network";
215	}
216	return "illegal";
217}
218
219const char *
220proto_name(u_short proto)
221{
222	static char buf[12];
223	switch (proto) {
224	case PPP_PAP:	return "pap";
225	case PPP_CHAP:	return "chap";
226	}
227	sprintf(buf, "0x%x", (unsigned)proto);
228	return buf;
229}
230
231const char *
232authflags(u_short flags)
233{
234	static char buf[10];
235	buf[0] = '\0';
236	if (flags & AUTHFLAG_NOCALLOUT)
237		strcat(buf, " callin");
238	if (flags & AUTHFLAG_NORECHALLENGE)
239		strcat(buf, " norechallenge");
240	return buf;
241}
242