spppcontrol.c revision 32313
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.2 1998/01/05 07:23:02 charnier 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#include <sys/time.h> 37 38#include <net/if.h> 39#include <net/if_var.h> 40#include <net/if_sppp.h> 41 42#include <err.h> 43#include <stdio.h> 44#include <string.h> 45#include <sysexits.h> 46#include <unistd.h> 47 48static void 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 181static void 182usage(void) 183{ 184 fprintf(stderr, "%s\n%s\n", 185 "usage: spppcontrol [-v] ifname [{my|his}auth{proto|name|secret}=...]", 186 " spppcontrol [-v] ifname callin|always"); 187 exit(EX_USAGE); 188} 189 190void 191print_vals(const char *ifname, struct spppreq *sp) 192{ 193 printf("%s:\tphase=%s\n", ifname, phase_name(sp->defs.pp_phase)); 194 if (sp->defs.myauth.proto) { 195 printf("\tmyauthproto=%s myauthname=\"%.*s\"\n", 196 proto_name(sp->defs.myauth.proto), 197 AUTHNAMELEN, sp->defs.myauth.name); 198 } 199 if (sp->defs.hisauth.proto) { 200 printf("\thisauthproto=%s hisauthname=\"%.*s\"%s\n", 201 proto_name(sp->defs.hisauth.proto), 202 AUTHNAMELEN, sp->defs.hisauth.name, 203 authflags(sp->defs.hisauth.flags)); 204 } 205} 206 207const char * 208phase_name(enum ppp_phase phase) 209{ 210 switch (phase) { 211 case PHASE_DEAD: return "dead"; 212 case PHASE_ESTABLISH: return "establish"; 213 case PHASE_TERMINATE: return "terminate"; 214 case PHASE_AUTHENTICATE: return "authenticate"; 215 case PHASE_NETWORK: return "network"; 216 } 217 return "illegal"; 218} 219 220const char * 221proto_name(u_short proto) 222{ 223 static char buf[12]; 224 switch (proto) { 225 case PPP_PAP: return "pap"; 226 case PPP_CHAP: return "chap"; 227 } 228 sprintf(buf, "0x%x", (unsigned)proto); 229 return buf; 230} 231 232const char * 233authflags(u_short flags) 234{ 235 static char buf[10]; 236 buf[0] = '\0'; 237 if (flags & AUTHFLAG_NOCALLOUT) 238 strcat(buf, " callin"); 239 if (flags & AUTHFLAG_NORECHALLENGE) 240 strcat(buf, " norechallenge"); 241 return buf; 242} 243