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