Deleted Added
full compact
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 "$Id: spppcontrol.c,v 1.4 1998/08/03 06:24:59 charnier Exp $";
29 "$Id: spppcontrol.c,v 1.5 1999/02/23 21:47:05 gj Exp $";
30#endif /* not lint */
31
32#include <sys/param.h>
33#include <sys/callout.h>
34#include <sys/ioctl.h>
35#include <sys/socket.h>
36
37#include <net/if.h>
38#include <net/if_var.h>
39#include <net/if_sppp.h>
40
41#include <err.h>
42#include <stdio.h>
43#include <string.h>
44#include <sysexits.h>
45#include <unistd.h>
46
47static void usage(void);
48void print_vals(const char *ifname, struct spppreq *sp);
49const char *phase_name(enum ppp_phase phase);
50const char *proto_name(u_short proto);
51const char *authflags(u_short flags);
52
53#define PPP_PAP 0xc023
54#define PPP_CHAP 0xc223
55
56int
57main(int argc, char **argv)
58{
59 int s, c;
60 int errs = 0, verbose = 0;
61 size_t off;
62 const char *ifname, *cp;
63 struct ifreq ifr;
64 struct spppreq spr;
65
66 while ((c = getopt(argc, argv, "v")) != -1)
67 switch (c) {
68 case 'v':
69 verbose++;
70 break;
71
72 default:
73 errs++;
74 break;
75 }
76 argv += optind;
77 argc -= optind;
78
79 if (errs || argc < 1)
80 usage();
81
82 ifname = argv[0];
83 strncpy(ifr.ifr_name, ifname, sizeof ifr.ifr_name);
84
85 /* use a random AF to create the socket */
86 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
87 err(EX_UNAVAILABLE, "ifconfig: socket");
88
89 argc--;
90 argv++;
91
92 spr.cmd = (int)SPPPIOGDEFS;
93 ifr.ifr_data = (caddr_t)&spr;
94
95 if (ioctl(s, SIOCGIFGENERIC, &ifr) == -1)
96 err(EX_OSERR, "SIOCGIFGENERIC(SPPPIOGDEFS)");
97
98 if (argc == 0) {
99 /* list only mode */
100 print_vals(ifname, &spr);
101 return 0;
102 }
103
104#define startswith(s) strncmp(argv[0], s, (off = strlen(s))) == 0
105
106 while (argc > 0) {
107 if (startswith("authproto=")) {
108 cp = argv[0] + off;
109 if (strcmp(cp, "pap") == 0)
110 spr.defs.myauth.proto =
111 spr.defs.hisauth.proto = PPP_PAP;
112 else if (strcmp(cp, "chap") == 0)
113 spr.defs.myauth.proto =
114 spr.defs.hisauth.proto = PPP_CHAP;
115 else if (strcmp(cp, "none") == 0)
116 spr.defs.myauth.proto =
117 spr.defs.hisauth.proto = 0;
118 else
119 errx(EX_DATAERR, "bad auth proto: %s", cp);
120 } else if (startswith("myauthproto=")) {
121 cp = argv[0] + off;
122 if (strcmp(cp, "pap") == 0)
123 spr.defs.myauth.proto = PPP_PAP;
124 else if (strcmp(cp, "chap") == 0)
125 spr.defs.myauth.proto = PPP_CHAP;
126 else if (strcmp(cp, "none") == 0)
127 spr.defs.myauth.proto = 0;
128 else
129 errx(EX_DATAERR, "bad auth proto: %s", cp);
130 } else if (startswith("myauthname="))
131 strncpy(spr.defs.myauth.name, argv[0] + off,
132 AUTHNAMELEN);
133 else if (startswith("myauthsecret=") ||
134 startswith("myauthkey="))
135 strncpy(spr.defs.myauth.secret, argv[0] + off,
136 AUTHKEYLEN);
137 else if (startswith("hisauthproto=")) {
138 cp = argv[0] + off;
139 if (strcmp(cp, "pap") == 0)
140 spr.defs.hisauth.proto = PPP_PAP;
141 else if (strcmp(cp, "chap") == 0)
142 spr.defs.hisauth.proto = PPP_CHAP;
143 else if (strcmp(cp, "none") == 0)
144 spr.defs.hisauth.proto = 0;
145 else
146 errx(EX_DATAERR, "bad auth proto: %s", cp);
147 } else if (startswith("hisauthname="))
148 strncpy(spr.defs.hisauth.name, argv[0] + off,
149 AUTHNAMELEN);
150 else if (startswith("hisauthsecret=") ||
151 startswith("hisauthkey="))
152 strncpy(spr.defs.hisauth.secret, argv[0] + off,
153 AUTHKEYLEN);
154 else if (strcmp(argv[0], "callin") == 0)
155 spr.defs.hisauth.flags |= AUTHFLAG_NOCALLOUT;
156 else if (strcmp(argv[0], "always") == 0)
157 spr.defs.hisauth.flags &= ~AUTHFLAG_NOCALLOUT;
158 else if (strcmp(argv[0], "norechallenge") == 0)
159 spr.defs.hisauth.flags |= AUTHFLAG_NORECHALLENGE;
160 else if (strcmp(argv[0], "rechallenge") == 0)
161 spr.defs.hisauth.flags &= ~AUTHFLAG_NORECHALLENGE;
162 else
163 errx(EX_DATAERR, "bad parameter: \"%s\"", argv[0]);
164
165 argv++;
166 argc--;
167 }
168
169 spr.cmd = (int)SPPPIOSDEFS;
170
171 if (ioctl(s, SIOCSIFGENERIC, &ifr) == -1)
172 err(EX_OSERR, "SIOCSIFGENERIC(SPPPIOSDEFS)");
173
174 if (verbose)
175 print_vals(ifname, &spr);
176
177 return 0;
178}
179
180static void
181usage(void)
182{
183 fprintf(stderr, "%s\n%s\n",
184 "usage: spppcontrol [-v] ifname [{my|his}auth{proto|name|secret}=...]",
185 " spppcontrol [-v] ifname callin|always");
186 exit(EX_USAGE);
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[20];
234 static char buf[30];
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}