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
28 static const char rcsid[] =
29   "$FreeBSD: /c/ncvs/src/sbin/spppcontrol/spppcontrol.c,v 1.4.2.2 1999/08/29 15:15:47 peter Exp $";
30 #endif /* not lint */
31 
32 #include <sys/param.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 
46 static void usage(void);
47 void	print_vals(const char *ifname, struct spppreq *sp);
48 const char *phase_name(enum ppp_phase phase);
49 const char *proto_name(u_short proto);
50 const char *authflags(u_short flags);
51 
52 #define PPP_PAP		0xc023
53 #define PPP_CHAP	0xc223
54 
55 int
main(int argc,char ** argv)56 main(int argc, char **argv)
57 {
58 	int s, c;
59 	int errs = 0, verbose = 0;
60 	size_t off;
61 	const char *ifname, *cp;
62 	struct ifreq ifr;
63 	struct spppreq spr;
64 
65 	while ((c = getopt(argc, argv, "v")) != -1)
66 		switch (c) {
67 		case 'v':
68 			verbose++;
69 			break;
70 
71 		default:
72 			errs++;
73 			break;
74 		}
75 	argv += optind;
76 	argc -= optind;
77 
78 	if (errs || argc < 1)
79 		usage();
80 
81 	ifname = argv[0];
82 	strncpy(ifr.ifr_name, ifname, sizeof ifr.ifr_name);
83 
84 	/* use a random AF to create the socket */
85 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
86 		err(EX_UNAVAILABLE, "socket");
87 
88 	argc--;
89 	argv++;
90 
91 	spr.cmd = (int)SPPPIOGDEFS;
92 	ifr.ifr_data = (caddr_t)&spr;
93 
94 	if (ioctl(s, SIOCGIFGENERIC, &ifr) == -1)
95 		err(EX_OSERR, "SIOCGIFGENERIC(SPPPIOGDEFS)");
96 
97 	if (argc == 0) {
98 		/* list only mode */
99 		print_vals(ifname, &spr);
100 		return 0;
101 	}
102 
103 #define startswith(s) strncmp(argv[0], s, (off = strlen(s))) == 0
104 
105 	while (argc > 0) {
106 		if (startswith("authproto=")) {
107 			cp = argv[0] + off;
108 			if (strcmp(cp, "pap") == 0)
109 				spr.defs.myauth.proto =
110 					spr.defs.hisauth.proto = PPP_PAP;
111 			else if (strcmp(cp, "chap") == 0)
112 				spr.defs.myauth.proto =
113 					spr.defs.hisauth.proto = PPP_CHAP;
114 			else if (strcmp(cp, "none") == 0)
115 				spr.defs.myauth.proto =
116 					spr.defs.hisauth.proto = 0;
117 			else
118 				errx(EX_DATAERR, "bad auth proto: %s", cp);
119 		} else if (startswith("myauthproto=")) {
120 			cp = argv[0] + off;
121 			if (strcmp(cp, "pap") == 0)
122 				spr.defs.myauth.proto = PPP_PAP;
123 			else if (strcmp(cp, "chap") == 0)
124 				spr.defs.myauth.proto = PPP_CHAP;
125 			else if (strcmp(cp, "none") == 0)
126 				spr.defs.myauth.proto = 0;
127 			else
128 				errx(EX_DATAERR, "bad auth proto: %s", cp);
129 		} else if (startswith("myauthname="))
130 			strncpy(spr.defs.myauth.name, argv[0] + off,
131 				AUTHNAMELEN);
132 		else if (startswith("myauthsecret=") ||
133 			 startswith("myauthkey="))
134 			strncpy(spr.defs.myauth.secret, argv[0] + off,
135 				AUTHKEYLEN);
136 		else if (startswith("hisauthproto=")) {
137 			cp = argv[0] + off;
138 			if (strcmp(cp, "pap") == 0)
139 				spr.defs.hisauth.proto = PPP_PAP;
140 			else if (strcmp(cp, "chap") == 0)
141 				spr.defs.hisauth.proto = PPP_CHAP;
142 			else if (strcmp(cp, "none") == 0)
143 				spr.defs.hisauth.proto = 0;
144 			else
145 				errx(EX_DATAERR, "bad auth proto: %s", cp);
146 		} else if (startswith("hisauthname="))
147 			strncpy(spr.defs.hisauth.name, argv[0] + off,
148 				AUTHNAMELEN);
149 		else if (startswith("hisauthsecret=") ||
150 			 startswith("hisauthkey="))
151 			strncpy(spr.defs.hisauth.secret, argv[0] + off,
152 				AUTHKEYLEN);
153 		else if (strcmp(argv[0], "callin") == 0)
154 			spr.defs.hisauth.flags |= AUTHFLAG_NOCALLOUT;
155 		else if (strcmp(argv[0], "always") == 0)
156 			spr.defs.hisauth.flags &= ~AUTHFLAG_NOCALLOUT;
157 		else if (strcmp(argv[0], "norechallenge") == 0)
158 			spr.defs.hisauth.flags |= AUTHFLAG_NORECHALLENGE;
159 		else if (strcmp(argv[0], "rechallenge") == 0)
160 			spr.defs.hisauth.flags &= ~AUTHFLAG_NORECHALLENGE;
161 		else
162 			errx(EX_DATAERR, "bad parameter: \"%s\"", argv[0]);
163 
164 		argv++;
165 		argc--;
166 	}
167 
168 	spr.cmd = (int)SPPPIOSDEFS;
169 
170 	if (ioctl(s, SIOCSIFGENERIC, &ifr) == -1)
171 		err(EX_OSERR, "SIOCSIFGENERIC(SPPPIOSDEFS)");
172 
173 	if (verbose)
174 		print_vals(ifname, &spr);
175 
176 	return 0;
177 }
178 
179 static void
usage(void)180 usage(void)
181 {
182 	fprintf(stderr, "%s\n%s\n",
183 	"usage: spppcontrol [-v] ifname [{my|his}auth{proto|name|secret}=...]",
184 	"       spppcontrol [-v] ifname callin|always");
185 	exit(EX_USAGE);
186 }
187 
188 void
print_vals(const char * ifname,struct spppreq * sp)189 print_vals(const char *ifname, struct spppreq *sp)
190 {
191 	printf("%s:\tphase=%s\n", ifname, phase_name(sp->defs.pp_phase));
192 	if (sp->defs.myauth.proto) {
193 		printf("\tmyauthproto=%s myauthname=\"%.*s\"\n",
194 		       proto_name(sp->defs.myauth.proto),
195 		       AUTHNAMELEN, sp->defs.myauth.name);
196 	}
197 	if (sp->defs.hisauth.proto) {
198 		printf("\thisauthproto=%s hisauthname=\"%.*s\"%s\n",
199 		       proto_name(sp->defs.hisauth.proto),
200 		       AUTHNAMELEN, sp->defs.hisauth.name,
201 		       authflags(sp->defs.hisauth.flags));
202 	}
203 }
204 
205 const char *
phase_name(enum ppp_phase phase)206 phase_name(enum ppp_phase phase)
207 {
208 	switch (phase) {
209 	case PHASE_DEAD:	return "dead";
210 	case PHASE_ESTABLISH:	return "establish";
211 	case PHASE_TERMINATE:	return "terminate";
212 	case PHASE_AUTHENTICATE: return "authenticate";
213 	case PHASE_NETWORK:	return "network";
214 	}
215 	return "illegal";
216 }
217 
218 const char *
proto_name(u_short proto)219 proto_name(u_short proto)
220 {
221 	static char buf[12];
222 	switch (proto) {
223 	case PPP_PAP:	return "pap";
224 	case PPP_CHAP:	return "chap";
225 	}
226 	snprintf(buf, sizeof buf, "0x%x", (unsigned)proto);
227 	return buf;
228 }
229 
230 const char *
authflags(u_short flags)231 authflags(u_short flags)
232 {
233 	static char buf[30];
234 	buf[0] = '\0';
235 	if (flags & AUTHFLAG_NOCALLOUT)
236 		strlcat(buf, " callin", sizeof buf);
237 	if (flags & AUTHFLAG_NORECHALLENGE)
238 		strlcat(buf, " norechallenge", sizeof buf);
239 	return buf;
240 }
241