Deleted Added
full compact
spppcontrol.c (88724) spppcontrol.c (114589)
1/*
2 * Copyright (c) 1997, 2001 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
1/*
2 * Copyright (c) 1997, 2001 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 88724 2001-12-30 20:43:09Z joerg $";
30#endif /* not lint */
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sbin/spppcontrol/spppcontrol.c 114589 2003-05-03 18:41:59Z obrien $");
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 = (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 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 = (int)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}
29
30#include <sys/types.h>
31#include <sys/ioctl.h>
32#include <sys/socket.h>
33
34#include <net/if.h>
35#include <net/if_sppp.h>
36
37#include <err.h>
38#include <stdio.h>
39#include <stdlib.h>
40#include <string.h>
41#include <sysexits.h>
42#include <unistd.h>
43
44static void usage(void);
45void print_vals(const char *ifname, struct spppreq *sp);
46const char *phase_name(enum ppp_phase phase);
47const char *proto_name(u_short proto);
48const char *authflags(u_short flags);
49
50#define PPP_PAP 0xc023
51#define PPP_CHAP 0xc223
52
53int
54main(int argc, char **argv)
55{
56 int s, c;
57 int errs = 0, verbose = 0;
58 size_t off;
59 long to;
60 char *endp;
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, "ifconfig: 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 if (startswith("lcp-timeout=")) {
162 cp = argv[0] + off;
163 to = strtol(cp, &endp, 10);
164 if (*cp == '\0' || *endp != '\0' ||
165 /*
166 * NB: 10 ms is the minimal possible value for
167 * hz=100. We assume no kernel has less clock
168 * frequency than that...
169 */
170 to < 10 || to > 20000)
171 errx(EX_DATAERR, "bad lcp timeout value: %s",
172 cp);
173 spr.defs.lcp.timeout = to;
174 } else if (strcmp(argv[0], "enable-vj") == 0)
175 spr.defs.enable_vj = 1;
176 else if (strcmp(argv[0], "disable-vj") == 0)
177 spr.defs.enable_vj = 0;
178 else if (strcmp(argv[0], "enable-ipv6") == 0)
179 spr.defs.enable_ipv6 = 1;
180 else if (strcmp(argv[0], "disable-ipv6") == 0)
181 spr.defs.enable_ipv6 = 0;
182 else
183 errx(EX_DATAERR, "bad parameter: \"%s\"", argv[0]);
184
185 argv++;
186 argc--;
187 }
188
189 spr.cmd = (int)SPPPIOSDEFS;
190
191 if (ioctl(s, SIOCSIFGENERIC, &ifr) == -1)
192 err(EX_OSERR, "SIOCSIFGENERIC(SPPPIOSDEFS)");
193
194 if (verbose)
195 print_vals(ifname, &spr);
196
197 return 0;
198}
199
200static void
201usage(void)
202{
203 fprintf(stderr, "%s\n%s\n",
204 "usage: spppcontrol [-v] ifname [{my|his}auth{proto|name|secret}=...]",
205 " spppcontrol [-v] ifname callin|always");
206 exit(EX_USAGE);
207}
208
209void
210print_vals(const char *ifname, struct spppreq *sp)
211{
212 printf("%s:\tphase=%s\n", ifname, phase_name(sp->defs.pp_phase));
213 if (sp->defs.myauth.proto) {
214 printf("\tmyauthproto=%s myauthname=\"%.*s\"\n",
215 proto_name(sp->defs.myauth.proto),
216 AUTHNAMELEN, sp->defs.myauth.name);
217 }
218 if (sp->defs.hisauth.proto) {
219 printf("\thisauthproto=%s hisauthname=\"%.*s\"%s\n",
220 proto_name(sp->defs.hisauth.proto),
221 AUTHNAMELEN, sp->defs.hisauth.name,
222 authflags(sp->defs.hisauth.flags));
223 }
224 printf("\tlcp-timeout=%d ms\n", sp->defs.lcp.timeout);
225 printf("\t%sable-vj\n", sp->defs.enable_vj? "en": "dis");
226 printf("\t%sable-ipv6\n", sp->defs.enable_ipv6? "en": "dis");
227}
228
229const char *
230phase_name(enum ppp_phase phase)
231{
232 switch (phase) {
233 case PHASE_DEAD: return "dead";
234 case PHASE_ESTABLISH: return "establish";
235 case PHASE_TERMINATE: return "terminate";
236 case PHASE_AUTHENTICATE: return "authenticate";
237 case PHASE_NETWORK: return "network";
238 }
239 return "illegal";
240}
241
242const char *
243proto_name(u_short proto)
244{
245 static char buf[12];
246 switch (proto) {
247 case PPP_PAP: return "pap";
248 case PPP_CHAP: return "chap";
249 }
250 sprintf(buf, "0x%x", (unsigned)proto);
251 return buf;
252}
253
254const char *
255authflags(u_short flags)
256{
257 static char buf[30];
258 buf[0] = '\0';
259 if (flags & AUTHFLAG_NOCALLOUT)
260 strcat(buf, " callin");
261 if (flags & AUTHFLAG_NORECHALLENGE)
262 strcat(buf, " norechallenge");
263 return buf;
264}