]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/ppp/command.c
merge fix for boot-time hang on centos' xen
[FreeBSD/FreeBSD.git] / usr.sbin / ppp / command.c
1 /*-
2  * Copyright (c) 1996 - 2001 Brian Somers <brian@Awfulhak.org>
3  *          based on work by Toshiharu OHNO <tony-o@iij.ad.jp>
4  *                           Internet Initiative Japan, Inc (IIJ)
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30
31 #include <sys/param.h>
32 #include <netinet/in_systm.h>
33 #include <netinet/in.h>
34 #include <netinet/ip.h>
35 #include <arpa/inet.h>
36 #include <sys/socket.h>
37 #include <net/route.h>
38 #include <netdb.h>
39 #include <sys/un.h>
40
41 #include <ctype.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <paths.h>
45 #include <stdarg.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <sys/wait.h>
50 #include <termios.h>
51 #include <unistd.h>
52
53 #ifndef NONAT
54 #ifdef LOCALNAT
55 #include "alias.h"
56 #else
57 #include <alias.h>
58 #endif
59 #endif
60
61 #include "layer.h"
62 #include "defs.h"
63 #include "command.h"
64 #include "mbuf.h"
65 #include "log.h"
66 #include "timer.h"
67 #include "fsm.h"
68 #include "iplist.h"
69 #include "throughput.h"
70 #include "slcompress.h"
71 #include "lqr.h"
72 #include "hdlc.h"
73 #include "lcp.h"
74 #include "ncpaddr.h"
75 #include "ipcp.h"
76 #ifndef NONAT
77 #include "nat_cmd.h"
78 #endif
79 #include "systems.h"
80 #include "filter.h"
81 #include "descriptor.h"
82 #include "main.h"
83 #include "route.h"
84 #include "ccp.h"
85 #include "auth.h"
86 #include "async.h"
87 #include "link.h"
88 #include "physical.h"
89 #include "mp.h"
90 #ifndef NORADIUS
91 #include "radius.h"
92 #endif
93 #include "ipv6cp.h"
94 #include "ncp.h"
95 #include "bundle.h"
96 #include "server.h"
97 #include "prompt.h"
98 #include "chat.h"
99 #include "chap.h"
100 #include "cbcp.h"
101 #include "datalink.h"
102 #include "iface.h"
103 #include "id.h"
104 #include "probe.h"
105
106 /* ``set'' values */
107 #define VAR_AUTHKEY     0
108 #define VAR_DIAL        1
109 #define VAR_LOGIN       2
110 #define VAR_AUTHNAME    3
111 #define VAR_AUTOLOAD    4
112 #define VAR_WINSIZE     5
113 #define VAR_DEVICE      6
114 #define VAR_ACCMAP      7
115 #define VAR_MRRU        8
116 #define VAR_MRU         9
117 #define VAR_MTU         10
118 #define VAR_OPENMODE    11
119 #define VAR_PHONE       12
120 #define VAR_HANGUP      13
121 #define VAR_IDLETIMEOUT 14
122 #define VAR_LQRPERIOD   15
123 #define VAR_LCPRETRY    16
124 #define VAR_CHAPRETRY   17
125 #define VAR_PAPRETRY    18
126 #define VAR_CCPRETRY    19
127 #define VAR_IPCPRETRY   20
128 #define VAR_DNS         21
129 #define VAR_NBNS        22
130 #define VAR_MODE        23
131 #define VAR_CALLBACK    24
132 #define VAR_CBCP        25
133 #define VAR_CHOKED      26
134 #define VAR_SENDPIPE    27
135 #define VAR_RECVPIPE    28
136 #define VAR_RADIUS      29
137 #define VAR_CD          30
138 #define VAR_PARITY      31
139 #define VAR_CRTSCTS     32
140 #define VAR_URGENTPORTS 33
141 #define VAR_LOGOUT      34
142 #define VAR_IFQUEUE     35
143 #define VAR_MPPE        36
144 #define VAR_IPV6CPRETRY 37
145 #define VAR_RAD_ALIVE   38
146 #define VAR_PPPOE       39
147 #define VAR_PORT_ID     40
148
149 /* ``accept|deny|disable|enable'' masks */
150 #define NEG_HISMASK (1)
151 #define NEG_MYMASK (2)
152
153 /* ``accept|deny|disable|enable'' values */
154 #define NEG_ACFCOMP     40
155 #define NEG_CHAP05      41
156 #define NEG_CHAP80      42
157 #define NEG_CHAP80LM    43
158 #define NEG_DEFLATE     44
159 #define NEG_DNS         45
160 #define NEG_ECHO        46
161 #define NEG_ENDDISC     47
162 #define NEG_LQR         48
163 #define NEG_PAP         49
164 #define NEG_PPPDDEFLATE 50
165 #define NEG_PRED1       51
166 #define NEG_PROTOCOMP   52
167 #define NEG_SHORTSEQ    53
168 #define NEG_VJCOMP      54
169 #define NEG_MPPE        55
170 #define NEG_CHAP81      56
171
172 const char Version[] = "3.4.2";
173
174 static int ShowCommand(struct cmdargs const *);
175 static int TerminalCommand(struct cmdargs const *);
176 static int QuitCommand(struct cmdargs const *);
177 static int OpenCommand(struct cmdargs const *);
178 static int CloseCommand(struct cmdargs const *);
179 static int DownCommand(struct cmdargs const *);
180 static int SetCommand(struct cmdargs const *);
181 static int LinkCommand(struct cmdargs const *);
182 static int AddCommand(struct cmdargs const *);
183 static int DeleteCommand(struct cmdargs const *);
184 static int NegotiateCommand(struct cmdargs const *);
185 static int ClearCommand(struct cmdargs const *);
186 static int RunListCommand(struct cmdargs const *);
187 static int IfaceAddCommand(struct cmdargs const *);
188 static int IfaceDeleteCommand(struct cmdargs const *);
189 static int IfaceClearCommand(struct cmdargs const *);
190 static int SetProcTitle(struct cmdargs const *);
191 #ifndef NONAT
192 static int NatEnable(struct cmdargs const *);
193 static int NatOption(struct cmdargs const *);
194 #endif
195
196 static const char *
197 showcx(struct cmdtab const *cmd)
198 {
199   if (cmd->lauth & LOCAL_CX)
200     return "(c)";
201   else if (cmd->lauth & LOCAL_CX_OPT)
202     return "(o)";
203
204   return "";
205 }
206
207 static int
208 HelpCommand(struct cmdargs const *arg)
209 {
210   struct cmdtab const *cmd;
211   int n, cmax, dmax, cols, cxlen;
212   const char *cx;
213
214   if (!arg->prompt) {
215     log_Printf(LogWARN, "help: Cannot help without a prompt\n");
216     return 0;
217   }
218
219   if (arg->argc > arg->argn) {
220     for (cmd = arg->cmdtab; cmd->name || cmd->alias; cmd++)
221       if ((cmd->lauth & arg->prompt->auth) &&
222           ((cmd->name && !strcasecmp(cmd->name, arg->argv[arg->argn])) ||
223            (cmd->alias && !strcasecmp(cmd->alias, arg->argv[arg->argn])))) {
224         prompt_Printf(arg->prompt, "%s %s\n", cmd->syntax, showcx(cmd));
225         return 0;
226       }
227     return -1;
228   }
229
230   cmax = dmax = 0;
231   for (cmd = arg->cmdtab; cmd->func; cmd++)
232     if (cmd->name && (cmd->lauth & arg->prompt->auth)) {
233       if ((n = strlen(cmd->name) + strlen(showcx(cmd))) > cmax)
234         cmax = n;
235       if ((n = strlen(cmd->helpmes)) > dmax)
236         dmax = n;
237     }
238
239   cols = 80 / (dmax + cmax + 3);
240   n = 0;
241   prompt_Printf(arg->prompt, "(o) = Optional context,"
242                 " (c) = Context required\n");
243   for (cmd = arg->cmdtab; cmd->func; cmd++)
244     if (cmd->name && (cmd->lauth & arg->prompt->auth)) {
245       cx = showcx(cmd);
246       cxlen = cmax - strlen(cmd->name);
247       if (n % cols != 0)
248         prompt_Printf(arg->prompt, " ");
249       prompt_Printf(arg->prompt, "%s%-*.*s: %-*.*s",
250               cmd->name, cxlen, cxlen, cx, dmax, dmax, cmd->helpmes);
251       if (++n % cols == 0)
252         prompt_Printf(arg->prompt, "\n");
253     }
254   if (n % cols != 0)
255     prompt_Printf(arg->prompt, "\n");
256
257   return 0;
258 }
259
260 static int
261 IdentCommand(struct cmdargs const *arg)
262 {
263   Concatinate(arg->cx->physical->link.lcp.cfg.ident,
264               sizeof arg->cx->physical->link.lcp.cfg.ident,
265               arg->argc - arg->argn, arg->argv + arg->argn);
266   return 0;
267 }
268
269 static int
270 SendIdentification(struct cmdargs const *arg)
271 {
272   if (arg->cx->state < DATALINK_LCP) {
273     log_Printf(LogWARN, "sendident: link has not reached LCP\n");
274     return 2;
275   }
276   return lcp_SendIdentification(&arg->cx->physical->link.lcp) ? 0 : 1;
277 }
278
279 static int
280 CloneCommand(struct cmdargs const *arg)
281 {
282   char namelist[LINE_LEN];
283   char *name;
284   int f;
285
286   if (arg->argc == arg->argn)
287     return -1;
288
289   namelist[sizeof namelist - 1] = '\0';
290   for (f = arg->argn; f < arg->argc; f++) {
291     strncpy(namelist, arg->argv[f], sizeof namelist - 1);
292     for(name = strtok(namelist, ", "); name; name = strtok(NULL,", "))
293       bundle_DatalinkClone(arg->bundle, arg->cx, name);
294   }
295
296   return 0;
297 }
298
299 static int
300 RemoveCommand(struct cmdargs const *arg)
301 {
302   if (arg->argc != arg->argn)
303     return -1;
304
305   if (arg->cx->state != DATALINK_CLOSED) {
306     log_Printf(LogWARN, "remove: Cannot delete links that aren't closed\n");
307     return 2;
308   }
309
310   bundle_DatalinkRemove(arg->bundle, arg->cx);
311   return 0;
312 }
313
314 static int
315 RenameCommand(struct cmdargs const *arg)
316 {
317   if (arg->argc != arg->argn + 1)
318     return -1;
319
320   if (bundle_RenameDatalink(arg->bundle, arg->cx, arg->argv[arg->argn]))
321     return 0;
322
323   log_Printf(LogWARN, "%s -> %s: target name already exists\n",
324              arg->cx->name, arg->argv[arg->argn]);
325   return 1;
326 }
327
328 static int
329 LoadCommand(struct cmdargs const *arg)
330 {
331   const char *err;
332   int n, mode;
333
334   mode = arg->bundle->phys_type.all;
335
336   if (arg->argn < arg->argc) {
337     for (n = arg->argn; n < arg->argc; n++)
338       if ((err = system_IsValid(arg->argv[n], arg->prompt, mode)) != NULL) {
339         log_Printf(LogWARN, "%s: %s\n", arg->argv[n], err);
340         return 1;
341       }
342
343     for (n = arg->argn; n < arg->argc; n++) {
344       bundle_SetLabel(arg->bundle, arg->argv[arg->argc - 1]);
345       system_Select(arg->bundle, arg->argv[n], CONFFILE, arg->prompt, arg->cx);
346     }
347     bundle_SetLabel(arg->bundle, arg->argv[arg->argc - 1]);
348   } else if ((err = system_IsValid("default", arg->prompt, mode)) != NULL) {
349     log_Printf(LogWARN, "default: %s\n", err);
350     return 1;
351   } else {
352     bundle_SetLabel(arg->bundle, "default");
353     system_Select(arg->bundle, "default", CONFFILE, arg->prompt, arg->cx);
354     bundle_SetLabel(arg->bundle, "default");
355   }
356
357   return 0;
358 }
359
360 static int
361 LogCommand(struct cmdargs const *arg)
362 {
363   char buf[LINE_LEN];
364
365   if (arg->argn < arg->argc) {
366     char *argv[MAXARGS];
367     int argc = arg->argc - arg->argn;
368
369     if (argc >= (int)(sizeof argv / sizeof argv[0])) {
370       argc = sizeof argv / sizeof argv[0] - 1;
371       log_Printf(LogWARN, "Truncating log command to %d args\n", argc);
372     }
373     command_Expand(argv, argc, arg->argv + arg->argn, arg->bundle, 1, getpid());
374     Concatinate(buf, sizeof buf, argc, (const char *const *)argv);
375     log_Printf(LogLOG, "%s\n", buf);
376     command_Free(argc, argv);
377     return 0;
378   }
379
380   return -1;
381 }
382
383 static int
384 SaveCommand(struct cmdargs const *arg __unused)
385 {
386   log_Printf(LogWARN, "save command is not yet implemented.\n");
387   return 1;
388 }
389
390 static int
391 DialCommand(struct cmdargs const *arg)
392 {
393   int res;
394
395   if ((arg->cx && !(arg->cx->physical->type & (PHYS_INTERACTIVE|PHYS_AUTO)))
396       || (!arg->cx &&
397           (arg->bundle->phys_type.all & ~(PHYS_INTERACTIVE|PHYS_AUTO)))) {
398     log_Printf(LogWARN, "Manual dial is only available for auto and"
399               " interactive links\n");
400     return 1;
401   }
402
403   if (arg->argc > arg->argn && (res = LoadCommand(arg)) != 0)
404     return res;
405
406   bundle_Open(arg->bundle, arg->cx ? arg->cx->name : NULL, PHYS_ALL, 1);
407
408   return 0;
409 }
410
411 #define isinword(ch) (isalnum(ch) || (ch) == '_')
412
413 static char *
414 strstrword(char *big, const char *little)
415 {
416   /* Get the first occurance of the word ``little'' in ``big'' */
417   char *pos;
418   int len;
419
420   pos = big;
421   len = strlen(little);
422
423   while ((pos = strstr(pos, little)) != NULL)
424     if ((pos != big && isinword(pos[-1])) || isinword(pos[len]))
425       pos++;
426     else if (pos != big && pos[-1] == '\\')
427       memmove(pos - 1, pos, strlen(pos) + 1);
428     else
429       break;
430
431   return pos;
432 }
433
434 static char *
435 subst(char *tgt, const char *oldstr, const char *newstr)
436 {
437   /* tgt is a malloc()d area... realloc() as necessary */
438   char *word, *ntgt;
439   int ltgt, loldstr, lnewstr, pos;
440
441   if ((word = strstrword(tgt, oldstr)) == NULL)
442     return tgt;
443
444   ltgt = strlen(tgt) + 1;
445   loldstr = strlen(oldstr);
446   lnewstr = strlen(newstr);
447   do {
448     pos = word - tgt;
449     if (loldstr > lnewstr)
450       bcopy(word + loldstr, word + lnewstr, ltgt - pos - loldstr);
451     if (loldstr != lnewstr) {
452       ntgt = realloc(tgt, ltgt += lnewstr - loldstr);
453       if (ntgt == NULL)
454         break;                  /* Oh wonderful ! */
455       word = ntgt + pos;
456       tgt = ntgt;
457     }
458     if (lnewstr > loldstr)
459       bcopy(word + loldstr, word + lnewstr, ltgt - pos - lnewstr);
460     bcopy(newstr, word, lnewstr);
461   } while ((word = strstrword(word, oldstr)));
462
463   return tgt;
464 }
465
466 static char *
467 substip(char *tgt, const char *oldstr, struct in_addr ip)
468 {
469   return subst(tgt, oldstr, inet_ntoa(ip));
470 }
471
472 static char *
473 substlong(char *tgt, const char *oldstr, long l)
474 {
475   char buf[23];
476
477   snprintf(buf, sizeof buf, "%ld", l);
478
479   return subst(tgt, oldstr, buf);
480 }
481
482 static char *
483 substull(char *tgt, const char *oldstr, unsigned long long ull)
484 {
485   char buf[21];
486
487   snprintf(buf, sizeof buf, "%llu", ull);
488
489   return subst(tgt, oldstr, buf);
490 }
491
492
493 #ifndef NOINET6
494 static char *
495 substipv6(char *tgt, const char *oldstr, const struct ncpaddr *ip)
496 {
497     return subst(tgt, oldstr, ncpaddr_ntoa(ip));
498 }
499
500 #ifndef NORADIUS
501 static char *
502 substipv6prefix(char *tgt, const char *oldstr, const uint8_t *ipv6prefix)
503 {
504   uint8_t ipv6addr[INET6_ADDRSTRLEN];
505   uint8_t prefix[INET6_ADDRSTRLEN + sizeof("/128") - 1];
506
507   if (ipv6prefix) {
508     inet_ntop(AF_INET6, &ipv6prefix[2], ipv6addr, sizeof(ipv6addr));
509     snprintf(prefix, sizeof(prefix), "%s/%d", ipv6addr, ipv6prefix[1]);
510   } else
511     prefix[0] = '\0';
512   return subst(tgt, oldstr, prefix);
513 }
514 #endif
515 #endif
516
517 void
518 command_Expand(char **nargv, int argc, char const *const *oargv,
519                struct bundle *bundle, int inc0, pid_t pid)
520 {
521   int arg, secs;
522   char uptime[20];
523   unsigned long long oin, oout, pin, pout;
524
525   if (inc0)
526     arg = 0;            /* Start at arg 0 */
527   else {
528     nargv[0] = strdup(oargv[0]);
529     arg = 1;
530   }
531
532   secs = bundle_Uptime(bundle);
533   snprintf(uptime, sizeof uptime, "%d:%02d:%02d",
534            secs / 3600, (secs / 60) % 60, secs % 60);
535   oin = bundle->ncp.ipcp.throughput.OctetsIn;
536   oout = bundle->ncp.ipcp.throughput.OctetsOut;
537   pin = bundle->ncp.ipcp.throughput.PacketsIn;
538   pout = bundle->ncp.ipcp.throughput.PacketsOut;
539 #ifndef NOINET6
540   oin += bundle->ncp.ipv6cp.throughput.OctetsIn;
541   oout += bundle->ncp.ipv6cp.throughput.OctetsOut;
542   pin += bundle->ncp.ipv6cp.throughput.PacketsIn;
543   pout += bundle->ncp.ipv6cp.throughput.PacketsOut;
544 #endif
545
546   for (; arg < argc; arg++) {
547     nargv[arg] = strdup(oargv[arg]);
548     nargv[arg] = subst(nargv[arg], "AUTHNAME", bundle->cfg.auth.name);
549     nargv[arg] = subst(nargv[arg], "COMPILATIONDATE", __DATE__);
550     nargv[arg] = substip(nargv[arg], "DNS0", bundle->ncp.ipcp.ns.dns[0]);
551     nargv[arg] = substip(nargv[arg], "DNS1", bundle->ncp.ipcp.ns.dns[1]);
552     nargv[arg] = subst(nargv[arg], "ENDDISC",
553                        mp_Enddisc(bundle->ncp.mp.cfg.enddisc.class,
554                                   bundle->ncp.mp.cfg.enddisc.address,
555                                   bundle->ncp.mp.cfg.enddisc.len));
556     nargv[arg] = substip(nargv[arg], "HISADDR", bundle->ncp.ipcp.peer_ip);
557 #ifndef NOINET6
558     nargv[arg] = substipv6(nargv[arg], "HISADDR6", &bundle->ncp.ipv6cp.hisaddr);
559 #endif
560     nargv[arg] = subst(nargv[arg], "INTERFACE", bundle->iface->name);
561     nargv[arg] = substull(nargv[arg], "IPOCTETSIN",
562                           bundle->ncp.ipcp.throughput.OctetsIn);
563     nargv[arg] = substull(nargv[arg], "IPOCTETSOUT",
564                           bundle->ncp.ipcp.throughput.OctetsOut);
565     nargv[arg] = substull(nargv[arg], "IPPACKETSIN",
566                           bundle->ncp.ipcp.throughput.PacketsIn);
567     nargv[arg] = substull(nargv[arg], "IPPACKETSOUT",
568                           bundle->ncp.ipcp.throughput.PacketsOut);
569 #ifndef NOINET6
570     nargv[arg] = substull(nargv[arg], "IPV6OCTETSIN",
571                           bundle->ncp.ipv6cp.throughput.OctetsIn);
572     nargv[arg] = substull(nargv[arg], "IPV6OCTETSOUT",
573                           bundle->ncp.ipv6cp.throughput.OctetsOut);
574     nargv[arg] = substull(nargv[arg], "IPV6PACKETSIN",
575                           bundle->ncp.ipv6cp.throughput.PacketsIn);
576     nargv[arg] = substull(nargv[arg], "IPV6PACKETSOUT",
577                           bundle->ncp.ipv6cp.throughput.PacketsOut);
578 #endif
579     nargv[arg] = subst(nargv[arg], "LABEL", bundle_GetLabel(bundle));
580     nargv[arg] = substip(nargv[arg], "MYADDR", bundle->ncp.ipcp.my_ip);
581 #ifndef NOINET6
582     nargv[arg] = substipv6(nargv[arg], "MYADDR6", &bundle->ncp.ipv6cp.myaddr);
583 #ifndef NORADIUS
584     nargv[arg] = substipv6prefix(nargv[arg], "IPV6PREFIX",
585                                  bundle->radius.ipv6prefix);
586 #endif
587 #endif
588     nargv[arg] = substull(nargv[arg], "OCTETSIN", oin);
589     nargv[arg] = substull(nargv[arg], "OCTETSOUT", oout);
590     nargv[arg] = substull(nargv[arg], "PACKETSIN", pin);
591     nargv[arg] = substull(nargv[arg], "PACKETSOUT", pout);
592     nargv[arg] = subst(nargv[arg], "PEER_ENDDISC",
593                        mp_Enddisc(bundle->ncp.mp.peer.enddisc.class,
594                                   bundle->ncp.mp.peer.enddisc.address,
595                                   bundle->ncp.mp.peer.enddisc.len));
596     nargv[arg] = substlong(nargv[arg], "PROCESSID", pid);
597     if (server.cfg.port)
598       nargv[arg] = substlong(nargv[arg], "SOCKNAME", server.cfg.port);
599     else
600       nargv[arg] = subst(nargv[arg], "SOCKNAME", server.cfg.sockname);
601     nargv[arg] = subst(nargv[arg], "UPTIME", uptime);
602     nargv[arg] = subst(nargv[arg], "USER", bundle->ncp.mp.peer.authname);
603     nargv[arg] = subst(nargv[arg], "VERSION", Version);
604   }
605   nargv[arg] = NULL;
606 }
607
608 void
609 command_Free(int argc, char **argv)
610 {
611   while (argc) {
612     free(*argv);
613     argc--;
614     argv++;
615   }
616 }
617
618 static int
619 ShellCommand(struct cmdargs const *arg, int bg)
620 {
621   const char *shell;
622   pid_t shpid, pid;
623
624 #ifdef SHELL_ONLY_INTERACTIVELY
625   /* we're only allowed to shell when we run ppp interactively */
626   if (arg->prompt && arg->prompt->owner) {
627     log_Printf(LogWARN, "Can't start a shell from a network connection\n");
628     return 1;
629   }
630 #endif
631
632   if (arg->argc == arg->argn) {
633     if (!arg->prompt) {
634       log_Printf(LogWARN, "Can't start an interactive shell from"
635                 " a config file\n");
636       return 1;
637     } else if (arg->prompt->owner) {
638       log_Printf(LogWARN, "Can't start an interactive shell from"
639                 " a socket connection\n");
640       return 1;
641     } else if (bg) {
642       log_Printf(LogWARN, "Can only start an interactive shell in"
643                 " the foreground mode\n");
644       return 1;
645     }
646   }
647
648   pid = getpid();
649   if ((shpid = fork()) == 0) {
650     int i, fd;
651
652     if ((shell = getenv("SHELL")) == 0)
653       shell = _PATH_BSHELL;
654
655     timer_TermService();
656
657     if (arg->prompt)
658       fd = arg->prompt->fd_out;
659     else if ((fd = open(_PATH_DEVNULL, O_RDWR)) == -1) {
660       log_Printf(LogALERT, "Failed to open %s: %s\n",
661                 _PATH_DEVNULL, strerror(errno));
662       exit(1);
663     }
664     dup2(fd, STDIN_FILENO);
665     dup2(fd, STDOUT_FILENO);
666     dup2(fd, STDERR_FILENO);
667     for (i = getdtablesize(); i > STDERR_FILENO; i--)
668       fcntl(i, F_SETFD, 1);
669
670 #ifndef NOSUID
671     setuid(ID0realuid());
672 #endif
673     if (arg->argc > arg->argn) {
674       /* substitute pseudo args */
675       char *argv[MAXARGS];
676       int argc = arg->argc - arg->argn;
677
678       if (argc >= (int)(sizeof argv / sizeof argv[0])) {
679         argc = sizeof argv / sizeof argv[0] - 1;
680         log_Printf(LogWARN, "Truncating shell command to %d args\n", argc);
681       }
682       command_Expand(argv, argc, arg->argv + arg->argn, arg->bundle, 0, pid);
683       if (bg) {
684         pid_t p;
685
686         p = getpid();
687         if (daemon(1, 1) == -1) {
688           log_Printf(LogERROR, "%ld: daemon: %s\n", (long)p, strerror(errno));
689           exit(1);
690         }
691       } else if (arg->prompt)
692         printf("ppp: Pausing until %s finishes\n", arg->argv[arg->argn]);
693       execvp(argv[0], argv);
694     } else {
695       if (arg->prompt)
696         printf("ppp: Pausing until %s finishes\n", shell);
697       prompt_TtyOldMode(arg->prompt);
698       execl(shell, shell, (char *)NULL);
699     }
700
701     log_Printf(LogWARN, "exec() of %s failed: %s\n",
702               arg->argc > arg->argn ? arg->argv[arg->argn] : shell,
703               strerror(errno));
704     _exit(255);
705   }
706
707   if (shpid == (pid_t)-1)
708     log_Printf(LogERROR, "Fork failed: %s\n", strerror(errno));
709   else {
710     int status;
711     waitpid(shpid, &status, 0);
712   }
713
714   if (arg->prompt && !arg->prompt->owner)
715     prompt_TtyCommandMode(arg->prompt);
716
717   return 0;
718 }
719
720 static int
721 BgShellCommand(struct cmdargs const *arg)
722 {
723   if (arg->argc == arg->argn)
724     return -1;
725   return ShellCommand(arg, 1);
726 }
727
728 static int
729 FgShellCommand(struct cmdargs const *arg)
730 {
731   return ShellCommand(arg, 0);
732 }
733
734 static int
735 ResolvCommand(struct cmdargs const *arg)
736 {
737   if (arg->argc == arg->argn + 1) {
738     if (!strcasecmp(arg->argv[arg->argn], "reload"))
739       ipcp_LoadDNS(&arg->bundle->ncp.ipcp);
740     else if (!strcasecmp(arg->argv[arg->argn], "restore"))
741       ipcp_RestoreDNS(&arg->bundle->ncp.ipcp);
742     else if (!strcasecmp(arg->argv[arg->argn], "rewrite"))
743       ipcp_WriteDNS(&arg->bundle->ncp.ipcp);
744     else if (!strcasecmp(arg->argv[arg->argn], "readonly"))
745       arg->bundle->ncp.ipcp.ns.writable = 0;
746     else if (!strcasecmp(arg->argv[arg->argn], "writable"))
747       arg->bundle->ncp.ipcp.ns.writable = 1;
748     else
749       return -1;
750
751     return 0;
752   }
753
754   return -1;
755 }
756
757 #ifndef NONAT
758 static struct cmdtab const NatCommands[] =
759 {
760   {"addr", NULL, nat_RedirectAddr, LOCAL_AUTH,
761    "static address translation", "nat addr [addr_local addr_alias]", NULL},
762   {"deny_incoming", NULL, NatOption, LOCAL_AUTH,
763    "stop incoming connections", "nat deny_incoming yes|no",
764    (const void *) PKT_ALIAS_DENY_INCOMING},
765   {"enable", NULL, NatEnable, LOCAL_AUTH,
766    "enable NAT", "nat enable yes|no", NULL},
767   {"log", NULL, NatOption, LOCAL_AUTH,
768    "log NAT link creation", "nat log yes|no",
769    (const void *) PKT_ALIAS_LOG},
770   {"port", NULL, nat_RedirectPort, LOCAL_AUTH, "port redirection",
771    "nat port proto localaddr:port[-port] aliasport[-aliasport]", NULL},
772   {"proto", NULL, nat_RedirectProto, LOCAL_AUTH, "protocol redirection",
773    "nat proto proto localIP [publicIP [remoteIP]]", NULL},
774   {"proxy", NULL, nat_ProxyRule, LOCAL_AUTH,
775    "proxy control", "nat proxy server host[:port] ...", NULL},
776 #ifndef NO_FW_PUNCH
777   {"punch_fw", NULL, nat_PunchFW, LOCAL_AUTH,
778    "firewall control", "nat punch_fw [base count]", NULL},
779 #endif
780   {"skinny_port", NULL, nat_SkinnyPort, LOCAL_AUTH,
781    "TCP port used by Skinny Station protocol", "nat skinny_port [port]", NULL},
782   {"same_ports", NULL, NatOption, LOCAL_AUTH,
783    "try to leave port numbers unchanged", "nat same_ports yes|no",
784    (const void *) PKT_ALIAS_SAME_PORTS},
785   {"target", NULL, nat_SetTarget, LOCAL_AUTH,
786    "Default address for incoming connections", "nat target addr", NULL},
787   {"unregistered_only", NULL, NatOption, LOCAL_AUTH,
788    "translate unregistered (private) IP address space only",
789    "nat unregistered_only yes|no",
790    (const void *) PKT_ALIAS_UNREGISTERED_ONLY},
791   {"use_sockets", NULL, NatOption, LOCAL_AUTH,
792    "allocate host sockets", "nat use_sockets yes|no",
793    (const void *) PKT_ALIAS_USE_SOCKETS},
794   {"help", "?", HelpCommand, LOCAL_AUTH | LOCAL_NO_AUTH,
795    "Display this message", "nat help|? [command]", NatCommands},
796   {NULL, NULL, NULL, 0, NULL, NULL, NULL},
797 };
798 #endif
799
800 static struct cmdtab const AllowCommands[] = {
801   {"modes", "mode", AllowModes, LOCAL_AUTH,
802   "Only allow certain ppp modes", "allow modes mode...", NULL},
803   {"users", "user", AllowUsers, LOCAL_AUTH,
804   "Only allow ppp access to certain users", "allow users logname...", NULL},
805   {"help", "?", HelpCommand, LOCAL_AUTH | LOCAL_NO_AUTH,
806   "Display this message", "allow help|? [command]", AllowCommands},
807   {NULL, NULL, NULL, 0, NULL, NULL, NULL},
808 };
809
810 static struct cmdtab const IfaceCommands[] =
811 {
812   {"add", NULL, IfaceAddCommand, LOCAL_AUTH,
813    "Add iface address", "iface add addr[/bits| mask] peer", NULL},
814   {NULL, "add!", IfaceAddCommand, LOCAL_AUTH,
815    "Add or change an iface address", "iface add! addr[/bits| mask] peer",
816    (void *)1},
817   {"clear", NULL, IfaceClearCommand, LOCAL_AUTH,
818    "Clear iface address(es)", "iface clear [INET | INET6]", NULL},
819   {"delete", "rm", IfaceDeleteCommand, LOCAL_AUTH,
820    "Delete iface address", "iface delete addr", NULL},
821   {NULL, "rm!", IfaceDeleteCommand, LOCAL_AUTH,
822    "Delete iface address", "iface delete addr", (void *)1},
823   {NULL, "delete!", IfaceDeleteCommand, LOCAL_AUTH,
824    "Delete iface address", "iface delete addr", (void *)1},
825   {"show", NULL, iface_Show, LOCAL_AUTH,
826    "Show iface address(es)", "iface show", NULL},
827   {"help", "?", HelpCommand, LOCAL_AUTH | LOCAL_NO_AUTH,
828    "Display this message", "nat help|? [command]", IfaceCommands},
829   {NULL, NULL, NULL, 0, NULL, NULL, NULL},
830 };
831
832 static struct cmdtab const Commands[] = {
833   {"accept", NULL, NegotiateCommand, LOCAL_AUTH | LOCAL_CX_OPT,
834   "accept option request", "accept option ..", NULL},
835   {"add", NULL, AddCommand, LOCAL_AUTH,
836   "add route", "add dest mask gateway", NULL},
837   {NULL, "add!", AddCommand, LOCAL_AUTH,
838   "add or change route", "add! dest mask gateway", (void *)1},
839   {"allow", "auth", RunListCommand, LOCAL_AUTH,
840   "Allow ppp access", "allow users|modes ....", AllowCommands},
841   {"bg", "!bg", BgShellCommand, LOCAL_AUTH,
842   "Run a background command", "[!]bg command", NULL},
843   {"clear", NULL, ClearCommand, LOCAL_AUTH | LOCAL_CX_OPT,
844   "Clear throughput statistics",
845   "clear ipcp|ipv6cp|physical [current|overall|peak]...", NULL},
846   {"clone", NULL, CloneCommand, LOCAL_AUTH | LOCAL_CX,
847   "Clone a link", "clone newname...", NULL},
848   {"close", NULL, CloseCommand, LOCAL_AUTH | LOCAL_CX_OPT,
849   "Close an FSM", "close [lcp|ccp]", NULL},
850   {"delete", NULL, DeleteCommand, LOCAL_AUTH,
851   "delete route", "delete dest", NULL},
852   {NULL, "delete!", DeleteCommand, LOCAL_AUTH,
853   "delete a route if it exists", "delete! dest", (void *)1},
854   {"deny", NULL, NegotiateCommand, LOCAL_AUTH | LOCAL_CX_OPT,
855   "Deny option request", "deny option ..", NULL},
856   {"dial", "call", DialCommand, LOCAL_AUTH | LOCAL_CX_OPT,
857   "Dial and login", "dial|call [system ...]", NULL},
858   {"disable", NULL, NegotiateCommand, LOCAL_AUTH | LOCAL_CX_OPT,
859   "Disable option", "disable option ..", NULL},
860   {"down", NULL, DownCommand, LOCAL_AUTH | LOCAL_CX_OPT,
861   "Generate a down event", "down [ccp|lcp]", NULL},
862   {"enable", NULL, NegotiateCommand, LOCAL_AUTH | LOCAL_CX_OPT,
863   "Enable option", "enable option ..", NULL},
864   {"ident", NULL, IdentCommand, LOCAL_AUTH | LOCAL_CX,
865   "Set the link identity", "ident text...", NULL},
866   {"iface", "interface", RunListCommand, LOCAL_AUTH,
867   "interface control", "iface option ...", IfaceCommands},
868   {"link", "datalink", LinkCommand, LOCAL_AUTH,
869   "Link specific commands", "link name command ...", NULL},
870   {"load", NULL, LoadCommand, LOCAL_AUTH | LOCAL_CX_OPT,
871   "Load settings", "load [system ...]", NULL},
872   {"log", NULL, LogCommand, LOCAL_AUTH | LOCAL_CX_OPT,
873   "log information", "log word ...", NULL},
874 #ifndef NONAT
875   {"nat", "alias", RunListCommand, LOCAL_AUTH,
876   "NAT control", "nat option yes|no", NatCommands},
877 #endif
878   {"open", NULL, OpenCommand, LOCAL_AUTH | LOCAL_CX_OPT,
879   "Open an FSM", "open! [lcp|ccp|ipcp]", (void *)1},
880   {"passwd", NULL, PasswdCommand, LOCAL_NO_AUTH,
881   "Password for manipulation", "passwd LocalPassword", NULL},
882   {"quit", "bye", QuitCommand, LOCAL_AUTH | LOCAL_NO_AUTH,
883   "Quit PPP program", "quit|bye [all]", NULL},
884   {"remove", "rm", RemoveCommand, LOCAL_AUTH | LOCAL_CX,
885   "Remove a link", "remove", NULL},
886   {"rename", "mv", RenameCommand, LOCAL_AUTH | LOCAL_CX,
887   "Rename a link", "rename name", NULL},
888   {"resolv", NULL, ResolvCommand, LOCAL_AUTH,
889   "Manipulate resolv.conf", "resolv readonly|reload|restore|rewrite|writable",
890   NULL},
891   {"save", NULL, SaveCommand, LOCAL_AUTH,
892   "Save settings", "save", NULL},
893   {"sendident", NULL, SendIdentification, LOCAL_AUTH | LOCAL_CX,
894   "Transmit the link identity", "sendident", NULL},
895   {"set", "setup", SetCommand, LOCAL_AUTH | LOCAL_CX_OPT,
896   "Set parameters", "set[up] var value", NULL},
897   {"shell", "!", FgShellCommand, LOCAL_AUTH,
898   "Run a subshell", "shell|! [sh command]", NULL},
899   {"show", NULL, ShowCommand, LOCAL_AUTH | LOCAL_CX_OPT,
900   "Show status and stats", "show var", NULL},
901   {"term", NULL, TerminalCommand, LOCAL_AUTH | LOCAL_CX,
902   "Enter terminal mode", "term", NULL},
903   {"help", "?", HelpCommand, LOCAL_AUTH | LOCAL_NO_AUTH,
904   "Display this message", "help|? [command]", Commands},
905   {NULL, NULL, NULL, 0, NULL, NULL, NULL},
906 };
907
908 static int
909 ShowEscape(struct cmdargs const *arg)
910 {
911   if (arg->cx->physical->async.cfg.EscMap[32]) {
912     int code, bit;
913     const char *sep = "";
914
915     for (code = 0; code < 32; code++)
916       if (arg->cx->physical->async.cfg.EscMap[code])
917         for (bit = 0; bit < 8; bit++)
918           if (arg->cx->physical->async.cfg.EscMap[code] & (1 << bit)) {
919             prompt_Printf(arg->prompt, "%s0x%02x", sep, (code << 3) + bit);
920             sep = ", ";
921           }
922     prompt_Printf(arg->prompt, "\n");
923   }
924   return 0;
925 }
926
927 static int
928 ShowTimerList(struct cmdargs const *arg)
929 {
930   timer_Show(0, arg->prompt);
931   return 0;
932 }
933
934 static int
935 ShowStopped(struct cmdargs const *arg)
936 {
937   prompt_Printf(arg->prompt, " Stopped Timer:  LCP: ");
938   if (!arg->cx->physical->link.lcp.fsm.StoppedTimer.load)
939     prompt_Printf(arg->prompt, "Disabled");
940   else
941     prompt_Printf(arg->prompt, "%ld secs",
942                   arg->cx->physical->link.lcp.fsm.StoppedTimer.load / SECTICKS);
943
944   prompt_Printf(arg->prompt, ", CCP: ");
945   if (!arg->cx->physical->link.ccp.fsm.StoppedTimer.load)
946     prompt_Printf(arg->prompt, "Disabled");
947   else
948     prompt_Printf(arg->prompt, "%ld secs",
949                   arg->cx->physical->link.ccp.fsm.StoppedTimer.load / SECTICKS);
950
951   prompt_Printf(arg->prompt, "\n");
952
953   return 0;
954 }
955
956 static int
957 ShowVersion(struct cmdargs const *arg)
958 {
959   prompt_Printf(arg->prompt, "PPP Version %s - %s\n", Version, __DATE__);
960   return 0;
961 }
962
963 static int
964 ShowProtocolStats(struct cmdargs const *arg)
965 {
966   struct link *l = command_ChooseLink(arg);
967
968   prompt_Printf(arg->prompt, "%s:\n", l->name);
969   link_ReportProtocolStatus(l, arg->prompt);
970   return 0;
971 }
972
973 static struct cmdtab const ShowCommands[] = {
974   {"bundle", NULL, bundle_ShowStatus, LOCAL_AUTH,
975   "bundle details", "show bundle", NULL},
976   {"ccp", NULL, ccp_ReportStatus, LOCAL_AUTH | LOCAL_CX_OPT,
977   "CCP status", "show cpp", NULL},
978   {"compress", NULL, sl_Show, LOCAL_AUTH,
979   "VJ compression stats", "show compress", NULL},
980   {"escape", NULL, ShowEscape, LOCAL_AUTH | LOCAL_CX,
981   "escape characters", "show escape", NULL},
982   {"filter", NULL, filter_Show, LOCAL_AUTH,
983   "packet filters", "show filter [in|out|dial|alive]", NULL},
984   {"hdlc", NULL, hdlc_ReportStatus, LOCAL_AUTH | LOCAL_CX,
985   "HDLC errors", "show hdlc", NULL},
986   {"iface", "interface", iface_Show, LOCAL_AUTH,
987   "Interface status", "show iface", NULL},
988   {"ipcp", NULL, ipcp_Show, LOCAL_AUTH,
989   "IPCP status", "show ipcp", NULL},
990 #ifndef NOINET6
991   {"ipv6cp", NULL, ipv6cp_Show, LOCAL_AUTH,
992   "IPV6CP status", "show ipv6cp", NULL},
993 #endif
994   {"layers", NULL, link_ShowLayers, LOCAL_AUTH | LOCAL_CX_OPT,
995   "Protocol layers", "show layers", NULL},
996   {"lcp", NULL, lcp_ReportStatus, LOCAL_AUTH | LOCAL_CX,
997   "LCP status", "show lcp", NULL},
998   {"link", "datalink", datalink_Show, LOCAL_AUTH | LOCAL_CX,
999   "(high-level) link info", "show link", NULL},
1000   {"links", NULL, bundle_ShowLinks, LOCAL_AUTH,
1001   "available link names", "show links", NULL},
1002   {"log", NULL, log_ShowLevel, LOCAL_AUTH,
1003   "log levels", "show log", NULL},
1004   {"mem", NULL, mbuf_Show, LOCAL_AUTH,
1005   "mbuf allocations", "show mem", NULL},
1006   {"ncp", NULL, ncp_Show, LOCAL_AUTH,
1007   "NCP status", "show ncp", NULL},
1008   {"physical", NULL, physical_ShowStatus, LOCAL_AUTH | LOCAL_CX,
1009   "(low-level) link info", "show physical", NULL},
1010   {"mp", "multilink", mp_ShowStatus, LOCAL_AUTH,
1011   "multilink setup", "show mp", NULL},
1012   {"proto", NULL, ShowProtocolStats, LOCAL_AUTH | LOCAL_CX_OPT,
1013   "protocol summary", "show proto", NULL},
1014   {"route", NULL, route_Show, LOCAL_AUTH,
1015   "routing table", "show route", NULL},
1016   {"stopped", NULL, ShowStopped, LOCAL_AUTH | LOCAL_CX,
1017   "STOPPED timeout", "show stopped", NULL},
1018   {"timers", NULL, ShowTimerList, LOCAL_AUTH,
1019   "alarm timers", "show timers", NULL},
1020   {"version", NULL, ShowVersion, LOCAL_NO_AUTH | LOCAL_AUTH,
1021   "version string", "show version", NULL},
1022   {"who", NULL, log_ShowWho, LOCAL_AUTH,
1023   "client list", "show who", NULL},
1024   {"help", "?", HelpCommand, LOCAL_NO_AUTH | LOCAL_AUTH,
1025   "Display this message", "show help|? [command]", ShowCommands},
1026   {NULL, NULL, NULL, 0, NULL, NULL, NULL},
1027 };
1028
1029 static struct cmdtab const *
1030 FindCommand(struct cmdtab const *cmds, const char *str, int *pmatch)
1031 {
1032   int nmatch;
1033   int len;
1034   struct cmdtab const *found;
1035
1036   found = NULL;
1037   len = strlen(str);
1038   nmatch = 0;
1039   while (cmds->func) {
1040     if (cmds->name && strncasecmp(str, cmds->name, len) == 0) {
1041       if (cmds->name[len] == '\0') {
1042         *pmatch = 1;
1043         return cmds;
1044       }
1045       nmatch++;
1046       found = cmds;
1047     } else if (cmds->alias && strncasecmp(str, cmds->alias, len) == 0) {
1048       if (cmds->alias[len] == '\0') {
1049         *pmatch = 1;
1050         return cmds;
1051       }
1052       nmatch++;
1053       found = cmds;
1054     }
1055     cmds++;
1056   }
1057   *pmatch = nmatch;
1058   return found;
1059 }
1060
1061 static const char *
1062 mkPrefix(int argc, char const *const *argv, char *tgt, int sz)
1063 {
1064   int f, tlen, len;
1065
1066   tlen = 0;
1067   for (f = 0; f < argc && tlen < sz - 2; f++) {
1068     if (f)
1069       tgt[tlen++] = ' ';
1070     len = strlen(argv[f]);
1071     if (len > sz - tlen - 1)
1072       len = sz - tlen - 1;
1073     strncpy(tgt+tlen, argv[f], len);
1074     tlen += len;
1075   }
1076   tgt[tlen] = '\0';
1077   return tgt;
1078 }
1079
1080 static int
1081 FindExec(struct bundle *bundle, struct cmdtab const *cmds, int argc, int argn,
1082          char const *const *argv, struct prompt *prompt, struct datalink *cx)
1083 {
1084   struct cmdtab const *cmd;
1085   int val = 1;
1086   int nmatch;
1087   struct cmdargs arg;
1088   char prefix[100];
1089
1090   cmd = FindCommand(cmds, argv[argn], &nmatch);
1091   if (nmatch > 1)
1092     log_Printf(LogWARN, "%s: Ambiguous command\n",
1093               mkPrefix(argn+1, argv, prefix, sizeof prefix));
1094   else if (cmd && (!prompt || (cmd->lauth & prompt->auth))) {
1095     if ((cmd->lauth & LOCAL_CX) && !cx)
1096       /* We've got no context, but we require it */
1097       cx = bundle2datalink(bundle, NULL);
1098
1099     if ((cmd->lauth & LOCAL_CX) && !cx)
1100       log_Printf(LogWARN, "%s: No context (use the `link' command)\n",
1101                 mkPrefix(argn+1, argv, prefix, sizeof prefix));
1102     else {
1103       if (cx && !(cmd->lauth & (LOCAL_CX|LOCAL_CX_OPT))) {
1104         log_Printf(LogWARN, "%s: Redundant context (%s) ignored\n",
1105                   mkPrefix(argn+1, argv, prefix, sizeof prefix), cx->name);
1106         cx = NULL;
1107       }
1108       arg.cmdtab = cmds;
1109       arg.cmd = cmd;
1110       arg.argc = argc;
1111       arg.argn = argn+1;
1112       arg.argv = argv;
1113       arg.bundle = bundle;
1114       arg.cx = cx;
1115       arg.prompt = prompt;
1116       val = (*cmd->func) (&arg);
1117     }
1118   } else
1119     log_Printf(LogWARN, "%s: Invalid command\n",
1120               mkPrefix(argn+1, argv, prefix, sizeof prefix));
1121
1122   if (val == -1)
1123     log_Printf(LogWARN, "usage: %s\n", cmd->syntax);
1124   else if (val)
1125     log_Printf(LogWARN, "%s: Failed %d\n",
1126               mkPrefix(argn+1, argv, prefix, sizeof prefix), val);
1127
1128   return val;
1129 }
1130
1131 int
1132 command_Expand_Interpret(char *buff, int nb, char *argv[MAXARGS], int offset)
1133 {
1134   char buff2[LINE_LEN-offset];
1135
1136   InterpretArg(buff, buff2);
1137   strncpy(buff, buff2, LINE_LEN - offset - 1);
1138   buff[LINE_LEN - offset - 1] = '\0';
1139
1140   return command_Interpret(buff, nb, argv);
1141 }
1142
1143 int
1144 command_Interpret(char *buff, int nb, char *argv[MAXARGS])
1145 {
1146   char *cp;
1147
1148   if (nb > 0) {
1149     cp = buff + strcspn(buff, "\r\n");
1150     if (cp)
1151       *cp = '\0';
1152     return MakeArgs(buff, argv, MAXARGS, PARSE_REDUCE);
1153   }
1154   return 0;
1155 }
1156
1157 static int
1158 arghidden(char const *const *argv, int n)
1159 {
1160   /* Is arg n of the given command to be hidden from the log ? */
1161
1162   /* set authkey xxxxx */
1163   /* set key xxxxx */
1164   if (n == 2 && !strncasecmp(argv[0], "se", 2) &&
1165       (!strncasecmp(argv[1], "authk", 5) || !strncasecmp(argv[1], "ke", 2)))
1166     return 1;
1167
1168   /* passwd xxxxx */
1169   if (n == 1 && !strncasecmp(argv[0], "p", 1))
1170     return 1;
1171
1172   /* set server port xxxxx .... */
1173   if (n == 3 && !strncasecmp(argv[0], "se", 2) &&
1174       !strncasecmp(argv[1], "se", 2))
1175     return 1;
1176
1177   return 0;
1178 }
1179
1180 void
1181 command_Run(struct bundle *bundle, int argc, char const *const *argv,
1182            struct prompt *prompt, const char *label, struct datalink *cx)
1183 {
1184   if (argc > 0) {
1185     if (log_IsKept(LogCOMMAND)) {
1186       char buf[LINE_LEN];
1187       int f;
1188       size_t n;
1189
1190       if (label) {
1191         strncpy(buf, label, sizeof buf - 3);
1192         buf[sizeof buf - 3] = '\0';
1193         strcat(buf, ": ");
1194         n = strlen(buf);
1195       } else {
1196         *buf = '\0';
1197         n = 0;
1198       }
1199       buf[sizeof buf - 1] = '\0';       /* In case we run out of room in buf */
1200
1201       for (f = 0; f < argc; f++) {
1202         if (n < sizeof buf - 1 && f)
1203           buf[n++] = ' ';
1204         if (arghidden(argv, f))
1205           strncpy(buf+n, "********", sizeof buf - n - 1);
1206         else
1207           strncpy(buf+n, argv[f], sizeof buf - n - 1);
1208         n += strlen(buf+n);
1209       }
1210       log_Printf(LogCOMMAND, "%s\n", buf);
1211     }
1212     FindExec(bundle, Commands, argc, 0, argv, prompt, cx);
1213   }
1214 }
1215
1216 int
1217 command_Decode(struct bundle *bundle, char *buff, int nb, struct prompt *prompt,
1218               const char *label)
1219 {
1220   int argc;
1221   char *argv[MAXARGS];
1222
1223   if ((argc = command_Expand_Interpret(buff, nb, argv, 0)) < 0)
1224     return 0;
1225
1226   command_Run(bundle, argc, (char const *const *)argv, prompt, label, NULL);
1227   return 1;
1228 }
1229
1230 static int
1231 ShowCommand(struct cmdargs const *arg)
1232 {
1233   if (!arg->prompt)
1234     log_Printf(LogWARN, "show: Cannot show without a prompt\n");
1235   else if (arg->argc > arg->argn)
1236     FindExec(arg->bundle, ShowCommands, arg->argc, arg->argn, arg->argv,
1237              arg->prompt, arg->cx);
1238   else
1239     prompt_Printf(arg->prompt, "Use ``show ?'' to get a list.\n");
1240
1241   return 0;
1242 }
1243
1244 static int
1245 TerminalCommand(struct cmdargs const *arg)
1246 {
1247   if (!arg->prompt) {
1248     log_Printf(LogWARN, "term: Need a prompt\n");
1249     return 1;
1250   }
1251
1252   if (arg->cx->physical->link.lcp.fsm.state > ST_CLOSED) {
1253     prompt_Printf(arg->prompt, "LCP state is [%s]\n",
1254                   State2Nam(arg->cx->physical->link.lcp.fsm.state));
1255     return 1;
1256   }
1257
1258   datalink_Up(arg->cx, 0, 0);
1259   prompt_TtyTermMode(arg->prompt, arg->cx);
1260   return 0;
1261 }
1262
1263 static int
1264 QuitCommand(struct cmdargs const *arg)
1265 {
1266   if (!arg->prompt || prompt_IsController(arg->prompt) ||
1267       (arg->argc > arg->argn && !strcasecmp(arg->argv[arg->argn], "all") &&
1268        (arg->prompt->auth & LOCAL_AUTH)))
1269     Cleanup();
1270   if (arg->prompt)
1271     prompt_Destroy(arg->prompt, 1);
1272
1273   return 0;
1274 }
1275
1276 static int
1277 OpenCommand(struct cmdargs const *arg)
1278 {
1279   if (arg->argc == arg->argn)
1280     bundle_Open(arg->bundle, arg->cx ? arg->cx->name : NULL, PHYS_ALL, 1);
1281   else if (arg->argc == arg->argn + 1) {
1282     if (!strcasecmp(arg->argv[arg->argn], "lcp")) {
1283       struct datalink *cx = arg->cx ?
1284         arg->cx : bundle2datalink(arg->bundle, NULL);
1285       if (cx) {
1286         if (cx->physical->link.lcp.fsm.state == ST_OPENED)
1287           fsm_Reopen(&cx->physical->link.lcp.fsm);
1288         else
1289           bundle_Open(arg->bundle, cx->name, PHYS_ALL, 1);
1290       } else
1291         log_Printf(LogWARN, "open lcp: You must specify a link\n");
1292     } else if (!strcasecmp(arg->argv[arg->argn], "ccp")) {
1293       struct fsm *fp;
1294
1295       fp = &command_ChooseLink(arg)->ccp.fsm;
1296       if (fp->link->lcp.fsm.state != ST_OPENED)
1297         log_Printf(LogWARN, "open: LCP must be open before opening CCP\n");
1298       else if (fp->state == ST_OPENED)
1299         fsm_Reopen(fp);
1300       else {
1301         fp->open_mode = 0;      /* Not passive any more */
1302         if (fp->state == ST_STOPPED) {
1303           fsm_Down(fp);
1304           fsm_Up(fp);
1305         } else {
1306           fsm_Up(fp);
1307           fsm_Open(fp);
1308         }
1309       }
1310     } else if (!strcasecmp(arg->argv[arg->argn], "ipcp")) {
1311       if (arg->cx)
1312         log_Printf(LogWARN, "open ipcp: You need not specify a link\n");
1313       if (arg->bundle->ncp.ipcp.fsm.state == ST_OPENED)
1314         fsm_Reopen(&arg->bundle->ncp.ipcp.fsm);
1315       else
1316         bundle_Open(arg->bundle, NULL, PHYS_ALL, 1);
1317     } else
1318       return -1;
1319   } else
1320     return -1;
1321
1322   return 0;
1323 }
1324
1325 static int
1326 CloseCommand(struct cmdargs const *arg)
1327 {
1328   if (arg->argc == arg->argn)
1329     bundle_Close(arg->bundle, arg->cx ? arg->cx->name : NULL, CLOSE_STAYDOWN);
1330   else if (arg->argc == arg->argn + 1) {
1331     if (!strcasecmp(arg->argv[arg->argn], "lcp"))
1332       bundle_Close(arg->bundle, arg->cx ? arg->cx->name : NULL, CLOSE_LCP);
1333     else if (!strcasecmp(arg->argv[arg->argn], "ccp") ||
1334              !strcasecmp(arg->argv[arg->argn], "ccp!")) {
1335       struct fsm *fp;
1336
1337       fp = &command_ChooseLink(arg)->ccp.fsm;
1338       if (fp->state == ST_OPENED) {
1339         fsm_Close(fp);
1340         if (arg->argv[arg->argn][3] == '!')
1341           fp->open_mode = 0;            /* Stay ST_CLOSED */
1342         else
1343           fp->open_mode = OPEN_PASSIVE; /* Wait for the peer to start */
1344       }
1345     } else
1346       return -1;
1347   } else
1348     return -1;
1349
1350   return 0;
1351 }
1352
1353 static int
1354 DownCommand(struct cmdargs const *arg)
1355 {
1356   if (arg->argc == arg->argn) {
1357       if (arg->cx)
1358         datalink_Down(arg->cx, CLOSE_STAYDOWN);
1359       else
1360         bundle_Down(arg->bundle, CLOSE_STAYDOWN);
1361   } else if (arg->argc == arg->argn + 1) {
1362     if (!strcasecmp(arg->argv[arg->argn], "lcp")) {
1363       if (arg->cx)
1364         datalink_Down(arg->cx, CLOSE_LCP);
1365       else
1366         bundle_Down(arg->bundle, CLOSE_LCP);
1367     } else if (!strcasecmp(arg->argv[arg->argn], "ccp")) {
1368       struct fsm *fp = arg->cx ? &arg->cx->physical->link.ccp.fsm :
1369                                  &arg->bundle->ncp.mp.link.ccp.fsm;
1370       fsm2initial(fp);
1371     } else
1372       return -1;
1373   } else
1374     return -1;
1375
1376   return 0;
1377 }
1378
1379 static int
1380 SetModemSpeed(struct cmdargs const *arg)
1381 {
1382   long speed;
1383   char *end;
1384
1385   if (arg->argc > arg->argn && *arg->argv[arg->argn]) {
1386     if (arg->argc > arg->argn+1) {
1387       log_Printf(LogWARN, "SetModemSpeed: Too many arguments\n");
1388       return -1;
1389     }
1390     if (strcasecmp(arg->argv[arg->argn], "sync") == 0) {
1391       physical_SetSync(arg->cx->physical);
1392       return 0;
1393     }
1394     end = NULL;
1395     speed = strtol(arg->argv[arg->argn], &end, 10);
1396     if (*end || speed < 0) {
1397       log_Printf(LogWARN, "SetModemSpeed: Bad argument \"%s\"",
1398                 arg->argv[arg->argn]);
1399       return -1;
1400     }
1401     if (physical_SetSpeed(arg->cx->physical, speed))
1402       return 0;
1403     log_Printf(LogWARN, "%s: Invalid speed\n", arg->argv[arg->argn]);
1404   } else
1405     log_Printf(LogWARN, "SetModemSpeed: No speed specified\n");
1406
1407   return -1;
1408 }
1409
1410 static int
1411 SetStoppedTimeout(struct cmdargs const *arg)
1412 {
1413   struct link *l = &arg->cx->physical->link;
1414
1415   l->lcp.fsm.StoppedTimer.load = 0;
1416   l->ccp.fsm.StoppedTimer.load = 0;
1417   if (arg->argc <= arg->argn+2) {
1418     if (arg->argc > arg->argn) {
1419       l->lcp.fsm.StoppedTimer.load = atoi(arg->argv[arg->argn]) * SECTICKS;
1420       if (arg->argc > arg->argn+1)
1421         l->ccp.fsm.StoppedTimer.load = atoi(arg->argv[arg->argn+1]) * SECTICKS;
1422     }
1423     return 0;
1424   }
1425   return -1;
1426 }
1427
1428 static int
1429 SetServer(struct cmdargs const *arg)
1430 {
1431   int res = -1;
1432
1433   if (arg->argc > arg->argn && arg->argc < arg->argn+4) {
1434     const char *port, *passwd, *mask;
1435     size_t mlen;
1436
1437     /* What's what ? */
1438     port = arg->argv[arg->argn];
1439     if (arg->argc == arg->argn + 2) {
1440       passwd = arg->argv[arg->argn+1];
1441       mask = NULL;
1442     } else if (arg->argc == arg->argn + 3) {
1443       passwd = arg->argv[arg->argn+1];
1444       mask = arg->argv[arg->argn+2];
1445       mlen = strlen(mask);
1446       if (mlen == 0 || mlen > 4 || strspn(mask, "01234567") != mlen ||
1447           (mlen == 4 && *mask != '0')) {
1448         log_Printf(LogWARN, "%s %s: %s: Invalid mask\n",
1449                    arg->argv[arg->argn - 2], arg->argv[arg->argn - 1], mask);
1450         return -1;
1451       }
1452     } else if (arg->argc != arg->argn + 1)
1453       return -1;
1454     else if (strcasecmp(port, "none") == 0) {
1455       if (server_Clear(arg->bundle))
1456         log_Printf(LogPHASE, "Disabled server socket\n");
1457       return 0;
1458     } else if (strcasecmp(port, "open") == 0) {
1459       switch (server_Reopen(arg->bundle)) {
1460         case SERVER_OK:
1461           return 0;
1462         case SERVER_FAILED:
1463           log_Printf(LogWARN, "Failed to reopen server port\n");
1464           return 1;
1465         case SERVER_UNSET:
1466           log_Printf(LogWARN, "Cannot reopen unset server socket\n");
1467           return 1;
1468         default:
1469           break;
1470       }
1471       return -1;
1472     } else if (strcasecmp(port, "closed") == 0) {
1473       if (server_Close(arg->bundle))
1474         log_Printf(LogPHASE, "Closed server socket\n");
1475       else
1476         log_Printf(LogWARN, "Server socket not open\n");
1477
1478       return 0;
1479     } else
1480       return -1;
1481
1482     strncpy(server.cfg.passwd, passwd, sizeof server.cfg.passwd - 1);
1483     server.cfg.passwd[sizeof server.cfg.passwd - 1] = '\0';
1484
1485     if (*port == '/') {
1486       mode_t imask;
1487       char *ptr, name[LINE_LEN + 12];
1488
1489       if (mask == NULL)
1490         imask = (mode_t)-1;
1491       else for (imask = mlen = 0; mask[mlen]; mlen++)
1492         imask = (imask * 8) + mask[mlen] - '0';
1493
1494       ptr = strstr(port, "%d");
1495       if (ptr) {
1496         snprintf(name, sizeof name, "%.*s%d%s",
1497                  (int)(ptr - port), port, arg->bundle->unit, ptr + 2);
1498         port = name;
1499       }
1500       res = server_LocalOpen(arg->bundle, port, imask);
1501     } else {
1502       int iport, add = 0;
1503
1504       if (mask != NULL)
1505         return -1;
1506
1507       if (*port == '+') {
1508         port++;
1509         add = 1;
1510       }
1511       if (strspn(port, "0123456789") != strlen(port)) {
1512         struct servent *s;
1513
1514         if ((s = getservbyname(port, "tcp")) == NULL) {
1515           iport = 0;
1516           log_Printf(LogWARN, "%s: Invalid port or service\n", port);
1517         } else
1518           iport = ntohs(s->s_port);
1519       } else
1520         iport = atoi(port);
1521
1522       if (iport) {
1523         if (add)
1524           iport += arg->bundle->unit;
1525         res = server_TcpOpen(arg->bundle, iport);
1526       } else
1527         res = -1;
1528     }
1529   }
1530
1531   return res;
1532 }
1533
1534 static int
1535 SetEscape(struct cmdargs const *arg)
1536 {
1537   int code;
1538   int argc = arg->argc - arg->argn;
1539   char const *const *argv = arg->argv + arg->argn;
1540
1541   for (code = 0; code < 33; code++)
1542     arg->cx->physical->async.cfg.EscMap[code] = 0;
1543
1544   while (argc-- > 0) {
1545     sscanf(*argv++, "%x", &code);
1546     code &= 0xff;
1547     arg->cx->physical->async.cfg.EscMap[code >> 3] |= (1 << (code & 7));
1548     arg->cx->physical->async.cfg.EscMap[32] = 1;
1549   }
1550   return 0;
1551 }
1552
1553 static int
1554 SetInterfaceAddr(struct cmdargs const *arg)
1555 {
1556   struct ncp *ncp = &arg->bundle->ncp;
1557   struct ncpaddr ncpaddr;
1558   const char *hisaddr;
1559
1560   if (arg->argc > arg->argn + 4)
1561     return -1;
1562
1563   hisaddr = NULL;
1564   memset(&ncp->ipcp.cfg.my_range, '\0', sizeof ncp->ipcp.cfg.my_range);
1565   memset(&ncp->ipcp.cfg.peer_range, '\0', sizeof ncp->ipcp.cfg.peer_range);
1566   ncp->ipcp.cfg.HaveTriggerAddress = 0;
1567   ncp->ipcp.cfg.netmask.s_addr = INADDR_ANY;
1568   iplist_reset(&ncp->ipcp.cfg.peer_list);
1569
1570   if (arg->argc > arg->argn) {
1571     if (!ncprange_aton(&ncp->ipcp.cfg.my_range, ncp, arg->argv[arg->argn]))
1572       return 1;
1573     if (arg->argc > arg->argn+1) {
1574       hisaddr = arg->argv[arg->argn+1];
1575       if (arg->argc > arg->argn+2) {
1576         ncp->ipcp.ifmask = ncp->ipcp.cfg.netmask =
1577           GetIpAddr(arg->argv[arg->argn+2]);
1578         if (arg->argc > arg->argn+3) {
1579           ncp->ipcp.cfg.TriggerAddress = GetIpAddr(arg->argv[arg->argn+3]);
1580           ncp->ipcp.cfg.HaveTriggerAddress = 1;
1581         }
1582       }
1583     }
1584   }
1585
1586   /* 0.0.0.0 means any address (0 bits) */
1587   ncprange_getaddr(&ncp->ipcp.cfg.my_range, &ncpaddr);
1588   ncpaddr_getip4(&ncpaddr, &ncp->ipcp.my_ip);
1589   if (ncp->ipcp.my_ip.s_addr == INADDR_ANY)
1590     ncprange_setwidth(&ncp->ipcp.cfg.my_range, 0);
1591   bundle_AdjustFilters(arg->bundle, &ncpaddr, NULL);
1592
1593   if (hisaddr && !ipcp_UseHisaddr(arg->bundle, hisaddr,
1594                                   arg->bundle->phys_type.all & PHYS_AUTO))
1595     return 4;
1596
1597   return 0;
1598 }
1599
1600 static int
1601 SetRetry(int argc, char const *const *argv, u_int *timeout, u_int *maxreq,
1602           u_int *maxtrm, int def)
1603 {
1604   if (argc == 0) {
1605     *timeout = DEF_FSMRETRY;
1606     *maxreq = def;
1607     if (maxtrm != NULL)
1608       *maxtrm = def;
1609   } else {
1610     long l = atol(argv[0]);
1611
1612     if (l < MIN_FSMRETRY) {
1613       log_Printf(LogWARN, "%ld: Invalid FSM retry period - min %d\n",
1614                  l, MIN_FSMRETRY);
1615       return 1;
1616     } else
1617       *timeout = l;
1618
1619     if (argc > 1) {
1620       l = atol(argv[1]);
1621       if (l < 1) {
1622         log_Printf(LogWARN, "%ld: Invalid FSM REQ tries - changed to 1\n", l);
1623         l = 1;
1624       }
1625       *maxreq = l;
1626
1627       if (argc > 2 && maxtrm != NULL) {
1628         l = atol(argv[2]);
1629         if (l < 1) {
1630           log_Printf(LogWARN, "%ld: Invalid FSM TRM tries - changed to 1\n", l);
1631           l = 1;
1632         }
1633         *maxtrm = l;
1634       }
1635     }
1636   }
1637
1638   return 0;
1639 }
1640
1641 static int
1642 SetVariable(struct cmdargs const *arg)
1643 {
1644   long long_val, param = (long)arg->cmd->args;
1645   int mode, dummyint, f, first, res;
1646   u_short *change;
1647   const char *argp;
1648   struct datalink *cx = arg->cx;        /* LOCAL_CX uses this */
1649   struct link *l = command_ChooseLink(arg);     /* LOCAL_CX_OPT uses this */
1650   struct in_addr *ipaddr;
1651   struct ncpaddr ncpaddr[2];
1652
1653   if (arg->argc > arg->argn)
1654     argp = arg->argv[arg->argn];
1655   else
1656     argp = "";
1657
1658   res = 0;
1659
1660   if ((arg->cmd->lauth & LOCAL_CX) && !cx) {
1661     log_Printf(LogWARN, "set %s: No context (use the `link' command)\n",
1662               arg->cmd->name);
1663     return 1;
1664   } else if (cx && !(arg->cmd->lauth & (LOCAL_CX|LOCAL_CX_OPT))) {
1665     log_Printf(LogWARN, "set %s: Redundant context (%s) ignored\n",
1666               arg->cmd->name, cx->name);
1667     cx = NULL;
1668   }
1669
1670   switch (param) {
1671   case VAR_AUTHKEY:
1672     strncpy(arg->bundle->cfg.auth.key, argp,
1673             sizeof arg->bundle->cfg.auth.key - 1);
1674     arg->bundle->cfg.auth.key[sizeof arg->bundle->cfg.auth.key - 1] = '\0';
1675     break;
1676
1677   case VAR_AUTHNAME:
1678     switch (bundle_Phase(arg->bundle)) {
1679       default:
1680         log_Printf(LogWARN, "Altering authname while at phase %s\n",
1681                    bundle_PhaseName(arg->bundle));
1682         /* drop through */
1683       case PHASE_DEAD:
1684       case PHASE_ESTABLISH:
1685         strncpy(arg->bundle->cfg.auth.name, argp,
1686                 sizeof arg->bundle->cfg.auth.name - 1);
1687         arg->bundle->cfg.auth.name[sizeof arg->bundle->cfg.auth.name-1] = '\0';
1688         break;
1689     }
1690     break;
1691
1692   case VAR_AUTOLOAD:
1693     if (arg->argc == arg->argn + 3) {
1694       int v1, v2, v3;
1695       char *end;
1696
1697       v1 = strtol(arg->argv[arg->argn], &end, 0);
1698       if (v1 < 0 || *end) {
1699         log_Printf(LogWARN, "autoload: %s: Invalid min percentage\n",
1700                    arg->argv[arg->argn]);
1701         res = 1;
1702         break;
1703       }
1704
1705       v2 = strtol(arg->argv[arg->argn + 1], &end, 0);
1706       if (v2 < 0 || *end) {
1707         log_Printf(LogWARN, "autoload: %s: Invalid max percentage\n",
1708                    arg->argv[arg->argn + 1]);
1709         res = 1;
1710         break;
1711       }
1712       if (v2 < v1) {
1713         v3 = v1;
1714         v1 = v2;
1715         v2 = v3;
1716       }
1717
1718       v3 = strtol(arg->argv[arg->argn + 2], &end, 0);
1719       if (v3 <= 0 || *end) {
1720         log_Printf(LogWARN, "autoload: %s: Invalid throughput period\n",
1721                    arg->argv[arg->argn + 2]);
1722         res = 1;
1723         break;
1724       }
1725
1726       arg->bundle->ncp.mp.cfg.autoload.min = v1;
1727       arg->bundle->ncp.mp.cfg.autoload.max = v2;
1728       arg->bundle->ncp.mp.cfg.autoload.period = v3;
1729       mp_RestartAutoloadTimer(&arg->bundle->ncp.mp);
1730     } else {
1731       log_Printf(LogWARN, "Set autoload requires three arguments\n");
1732       res = 1;
1733     }
1734     break;
1735
1736   case VAR_DIAL:
1737     strncpy(cx->cfg.script.dial, argp, sizeof cx->cfg.script.dial - 1);
1738     cx->cfg.script.dial[sizeof cx->cfg.script.dial - 1] = '\0';
1739     break;
1740
1741   case VAR_LOGIN:
1742     strncpy(cx->cfg.script.login, argp, sizeof cx->cfg.script.login - 1);
1743     cx->cfg.script.login[sizeof cx->cfg.script.login - 1] = '\0';
1744     break;
1745
1746   case VAR_WINSIZE:
1747     if (arg->argc > arg->argn) {
1748       l->ccp.cfg.deflate.out.winsize = atoi(arg->argv[arg->argn]);
1749       if (l->ccp.cfg.deflate.out.winsize < 8 ||
1750           l->ccp.cfg.deflate.out.winsize > 15) {
1751           log_Printf(LogWARN, "%d: Invalid outgoing window size\n",
1752                     l->ccp.cfg.deflate.out.winsize);
1753           l->ccp.cfg.deflate.out.winsize = 15;
1754       }
1755       if (arg->argc > arg->argn+1) {
1756         l->ccp.cfg.deflate.in.winsize = atoi(arg->argv[arg->argn+1]);
1757         if (l->ccp.cfg.deflate.in.winsize < 8 ||
1758             l->ccp.cfg.deflate.in.winsize > 15) {
1759             log_Printf(LogWARN, "%d: Invalid incoming window size\n",
1760                       l->ccp.cfg.deflate.in.winsize);
1761             l->ccp.cfg.deflate.in.winsize = 15;
1762         }
1763       } else
1764         l->ccp.cfg.deflate.in.winsize = 0;
1765     } else {
1766       log_Printf(LogWARN, "No window size specified\n");
1767       res = 1;
1768     }
1769     break;
1770
1771 #ifndef NODES
1772   case VAR_MPPE:
1773     if (arg->argc > arg->argn + 2) {
1774       res = -1;
1775       break;
1776     }
1777
1778     if (arg->argc == arg->argn) {
1779       l->ccp.cfg.mppe.keybits = 0;
1780       l->ccp.cfg.mppe.state = MPPE_ANYSTATE;
1781       l->ccp.cfg.mppe.required = 0;
1782       break;
1783     }
1784
1785     if (!strcmp(argp, "*"))
1786       long_val = 0;
1787     else {
1788       long_val = atol(argp);
1789       if (long_val != 40 && long_val != 56 && long_val != 128) {
1790         log_Printf(LogWARN, "%s: Invalid bits value\n", argp);
1791         res = -1;
1792         break;
1793       }
1794     }
1795
1796     if (arg->argc == arg->argn + 2) {
1797       if (!strcmp(arg->argv[arg->argn + 1], "*"))
1798         l->ccp.cfg.mppe.state = MPPE_ANYSTATE;
1799       else if (!strcasecmp(arg->argv[arg->argn + 1], "stateless"))
1800         l->ccp.cfg.mppe.state = MPPE_STATELESS;
1801       else if (!strcasecmp(arg->argv[arg->argn + 1], "stateful"))
1802         l->ccp.cfg.mppe.state = MPPE_STATEFUL;
1803       else {
1804         log_Printf(LogWARN, "%s: Invalid state value\n",
1805                    arg->argv[arg->argn + 1]);
1806         res = -1;
1807         break;
1808       }
1809     } else
1810       l->ccp.cfg.mppe.state = MPPE_ANYSTATE;
1811     l->ccp.cfg.mppe.keybits = long_val;
1812     l->ccp.cfg.mppe.required = 1;
1813     break;
1814 #endif
1815
1816   case VAR_DEVICE:
1817     physical_SetDeviceList(cx->physical, arg->argc - arg->argn,
1818                            arg->argv + arg->argn);
1819     break;
1820
1821   case VAR_ACCMAP:
1822     if (arg->argc > arg->argn) {
1823       u_long ulong_val;
1824       sscanf(argp, "%lx", &ulong_val);
1825       cx->physical->link.lcp.cfg.accmap = (u_int32_t)ulong_val;
1826     } else {
1827       log_Printf(LogWARN, "No accmap specified\n");
1828       res = 1;
1829     }
1830     break;
1831
1832   case VAR_MODE:
1833     mode = Nam2mode(argp);
1834     if (mode == PHYS_NONE || mode == PHYS_ALL) {
1835       log_Printf(LogWARN, "%s: Invalid mode\n", argp);
1836       res = -1;
1837       break;
1838     }
1839     bundle_SetMode(arg->bundle, cx, mode);
1840     break;
1841
1842   case VAR_MRRU:
1843     switch (bundle_Phase(arg->bundle)) {
1844       case PHASE_DEAD:
1845         break;
1846       case PHASE_ESTABLISH:
1847         /* Make sure none of our links are DATALINK_LCP or greater */
1848         if (bundle_HighestState(arg->bundle) >= DATALINK_LCP) {
1849           log_Printf(LogWARN, "mrru: Only changable before LCP negotiations\n");
1850           res = 1;
1851           break;
1852         }
1853         break;
1854       default:
1855         log_Printf(LogWARN, "mrru: Only changable at phase DEAD/ESTABLISH\n");
1856         res = 1;
1857         break;
1858     }
1859     if (res != 0)
1860       break;
1861     long_val = atol(argp);
1862     if (long_val && long_val < MIN_MRU) {
1863       log_Printf(LogWARN, "MRRU %ld: too small - min %d\n", long_val, MIN_MRU);
1864       res = 1;
1865       break;
1866     } else if (long_val > MAX_MRU) {
1867       log_Printf(LogWARN, "MRRU %ld: too big - max %d\n", long_val, MAX_MRU);
1868       res = 1;
1869       break;
1870     } else
1871       arg->bundle->ncp.mp.cfg.mrru = long_val;
1872     break;
1873
1874   case VAR_MRU:
1875     long_val = 0;       /* silence gcc */
1876     change = NULL;      /* silence gcc */
1877     switch(arg->argc - arg->argn) {
1878     case 1:
1879       if (argp[strspn(argp, "0123456789")] != '\0') {
1880         res = -1;
1881         break;
1882       }
1883       /*FALLTHRU*/
1884     case 0:
1885       long_val = atol(argp);
1886       change = &l->lcp.cfg.mru;
1887       if (long_val > l->lcp.cfg.max_mru) {
1888         log_Printf(LogWARN, "MRU %ld: too large - max set to %d\n", long_val,
1889                    l->lcp.cfg.max_mru);
1890         res = 1;
1891         break;
1892       }
1893       break;
1894     case 2:
1895       if (strcasecmp(argp, "max") && strcasecmp(argp, "maximum")) {
1896         res = -1;
1897         break;
1898       }
1899       long_val = atol(arg->argv[arg->argn + 1]);
1900       change = &l->lcp.cfg.max_mru;
1901       if (long_val > MAX_MRU) {
1902         log_Printf(LogWARN, "MRU %ld: too large - maximum is %d\n", long_val,
1903                    MAX_MRU);
1904         res = 1;
1905         break;
1906       }
1907       break;
1908     default:
1909       res = -1;
1910       break;
1911     }
1912     if (res != 0)
1913       break;
1914
1915     if (long_val == 0)
1916       *change = 0;
1917     else if (long_val < MIN_MRU) {
1918       log_Printf(LogWARN, "MRU %ld: too small - min %d\n", long_val, MIN_MRU);
1919       res = 1;
1920       break;
1921     } else if (long_val > MAX_MRU) {
1922       log_Printf(LogWARN, "MRU %ld: too big - max %d\n", long_val, MAX_MRU);
1923       res = 1;
1924       break;
1925     } else
1926       *change = long_val;
1927     if (l->lcp.cfg.mru > *change)
1928       l->lcp.cfg.mru = *change;
1929     break;
1930
1931   case VAR_MTU:
1932     long_val = 0;       /* silence gcc */
1933     change = NULL;      /* silence gcc */
1934     switch(arg->argc - arg->argn) {
1935     case 1:
1936       if (argp[strspn(argp, "0123456789")] != '\0') {
1937         res = -1;
1938         break;
1939       }
1940       /*FALLTHRU*/
1941     case 0:
1942       long_val = atol(argp);
1943       change = &l->lcp.cfg.mtu;
1944       if (long_val > l->lcp.cfg.max_mtu) {
1945         log_Printf(LogWARN, "MTU %ld: too large - max set to %d\n", long_val,
1946                    l->lcp.cfg.max_mtu);
1947         res = 1;
1948         break;
1949       }
1950       break;
1951     case 2:
1952       if (strcasecmp(argp, "max") && strcasecmp(argp, "maximum")) {
1953         res = -1;
1954         break;
1955       }
1956       long_val = atol(arg->argv[arg->argn + 1]);
1957       change = &l->lcp.cfg.max_mtu;
1958       if (long_val > MAX_MTU) {
1959         log_Printf(LogWARN, "MTU %ld: too large - maximum is %d\n", long_val,
1960                    MAX_MTU);
1961         res = 1;
1962         break;
1963       }
1964       break;
1965     default:
1966       res = -1;
1967       break;
1968     }
1969
1970     if (res != 0)
1971       break;
1972
1973     if (long_val && long_val < MIN_MTU) {
1974       log_Printf(LogWARN, "MTU %ld: too small - min %d\n", long_val, MIN_MTU);
1975       res = 1;
1976       break;
1977     } else if (long_val > MAX_MTU) {
1978       log_Printf(LogWARN, "MTU %ld: too big - max %d\n", long_val, MAX_MTU);
1979       res = 1;
1980       break;
1981     } else
1982       *change = long_val;
1983     if (l->lcp.cfg.mtu > *change)
1984       l->lcp.cfg.mtu = *change;
1985     break;
1986
1987   case VAR_OPENMODE:
1988     if (strcasecmp(argp, "active") == 0)
1989       cx->physical->link.lcp.cfg.openmode = arg->argc > arg->argn+1 ?
1990         atoi(arg->argv[arg->argn+1]) : 1;
1991     else if (strcasecmp(argp, "passive") == 0)
1992       cx->physical->link.lcp.cfg.openmode = OPEN_PASSIVE;
1993     else {
1994       log_Printf(LogWARN, "%s: Invalid openmode\n", argp);
1995       res = 1;
1996     }
1997     break;
1998
1999   case VAR_PHONE:
2000     strncpy(cx->cfg.phone.list, argp, sizeof cx->cfg.phone.list - 1);
2001     cx->cfg.phone.list[sizeof cx->cfg.phone.list - 1] = '\0';
2002     cx->phone.alt = cx->phone.next = NULL;
2003     break;
2004
2005   case VAR_HANGUP:
2006     strncpy(cx->cfg.script.hangup, argp, sizeof cx->cfg.script.hangup - 1);
2007     cx->cfg.script.hangup[sizeof cx->cfg.script.hangup - 1] = '\0';
2008     break;
2009
2010   case VAR_IFQUEUE:
2011     long_val = atol(argp);
2012     arg->bundle->cfg.ifqueue = long_val < 0 ? 0 : long_val;
2013     break;
2014
2015   case VAR_LOGOUT:
2016     strncpy(cx->cfg.script.logout, argp, sizeof cx->cfg.script.logout - 1);
2017     cx->cfg.script.logout[sizeof cx->cfg.script.logout - 1] = '\0';
2018     break;
2019
2020   case VAR_IDLETIMEOUT:
2021     if (arg->argc > arg->argn+2) {
2022       log_Printf(LogWARN, "Too many idle timeout values\n");
2023       res = 1;
2024     } else if (arg->argc == arg->argn) {
2025       log_Printf(LogWARN, "Too few idle timeout values\n");
2026       res = 1;
2027     } else {
2028       unsigned long timeout, min;
2029
2030       timeout = strtoul(argp, NULL, 10);
2031       min = arg->bundle->cfg.idle.min_timeout;
2032       if (arg->argc == arg->argn + 2)
2033         min = strtoul(arg->argv[arg->argn + 1], NULL, 10);
2034       bundle_SetIdleTimer(arg->bundle, timeout, min);
2035     }
2036     break;
2037     
2038 #ifndef NORADIUS
2039   case VAR_RAD_ALIVE:
2040     if (arg->argc > arg->argn + 2) {
2041       log_Printf(LogWARN, "Too many RADIUS alive interval values\n");
2042       res = 1;
2043     } else if (arg->argc == arg->argn) {
2044       log_Printf(LogWARN, "Too few RADIUS alive interval values\n");
2045       res = 1;
2046     } else {
2047       arg->bundle->radius.alive.interval = atoi(argp);
2048       if (arg->bundle->radius.alive.interval && !arg->bundle->radius.cfg.file) {
2049         log_Printf(LogWARN, "rad_alive requires radius to be configured\n");
2050         res = 1;
2051       } else if (arg->bundle->ncp.ipcp.fsm.state == ST_OPENED) {
2052         if (arg->bundle->radius.alive.interval)
2053           radius_StartTimer(arg->bundle);
2054         else
2055           radius_StopTimer(&arg->bundle->radius);
2056       }
2057     }
2058     break;
2059 #endif
2060    
2061   case VAR_LQRPERIOD:
2062     long_val = atol(argp);
2063     if (long_val < MIN_LQRPERIOD) {
2064       log_Printf(LogWARN, "%ld: Invalid lqr period - min %d\n",
2065                  long_val, MIN_LQRPERIOD);
2066       res = 1;
2067     } else
2068       l->lcp.cfg.lqrperiod = long_val;
2069     break;
2070
2071   case VAR_LCPRETRY:
2072     res = SetRetry(arg->argc - arg->argn, arg->argv + arg->argn,
2073                    &cx->physical->link.lcp.cfg.fsm.timeout,
2074                    &cx->physical->link.lcp.cfg.fsm.maxreq,
2075                    &cx->physical->link.lcp.cfg.fsm.maxtrm, DEF_FSMTRIES);
2076     break;
2077
2078   case VAR_CHAPRETRY:
2079     res = SetRetry(arg->argc - arg->argn, arg->argv + arg->argn,
2080                    &cx->chap.auth.cfg.fsm.timeout,
2081                    &cx->chap.auth.cfg.fsm.maxreq, NULL, DEF_FSMAUTHTRIES);
2082     break;
2083
2084   case VAR_PAPRETRY:
2085     res = SetRetry(arg->argc - arg->argn, arg->argv + arg->argn,
2086                    &cx->pap.cfg.fsm.timeout, &cx->pap.cfg.fsm.maxreq,
2087                    NULL, DEF_FSMAUTHTRIES);
2088     break;
2089
2090   case VAR_CCPRETRY:
2091     res = SetRetry(arg->argc - arg->argn, arg->argv + arg->argn,
2092                    &l->ccp.cfg.fsm.timeout, &l->ccp.cfg.fsm.maxreq,
2093                    &l->ccp.cfg.fsm.maxtrm, DEF_FSMTRIES);
2094     break;
2095
2096   case VAR_IPCPRETRY:
2097     res = SetRetry(arg->argc - arg->argn, arg->argv + arg->argn,
2098                    &arg->bundle->ncp.ipcp.cfg.fsm.timeout,
2099                    &arg->bundle->ncp.ipcp.cfg.fsm.maxreq,
2100                    &arg->bundle->ncp.ipcp.cfg.fsm.maxtrm, DEF_FSMTRIES);
2101     break;
2102
2103 #ifndef NOINET6
2104   case VAR_IPV6CPRETRY:
2105     res = SetRetry(arg->argc - arg->argn, arg->argv + arg->argn,
2106                    &arg->bundle->ncp.ipv6cp.cfg.fsm.timeout,
2107                    &arg->bundle->ncp.ipv6cp.cfg.fsm.maxreq,
2108                    &arg->bundle->ncp.ipv6cp.cfg.fsm.maxtrm, DEF_FSMTRIES);
2109     break;
2110 #endif
2111
2112   case VAR_NBNS:
2113   case VAR_DNS:
2114     if (param == VAR_DNS) {
2115       ipaddr = arg->bundle->ncp.ipcp.cfg.ns.dns;
2116       ipaddr[0].s_addr = ipaddr[1].s_addr = INADDR_NONE;
2117     } else {
2118       ipaddr = arg->bundle->ncp.ipcp.cfg.ns.nbns;
2119       ipaddr[0].s_addr = ipaddr[1].s_addr = INADDR_ANY;
2120     }
2121
2122     if (arg->argc > arg->argn) {
2123       ncpaddr_aton(ncpaddr, &arg->bundle->ncp, arg->argv[arg->argn]);
2124       if (!ncpaddr_getip4(ncpaddr, ipaddr))
2125         return -1;
2126       if (arg->argc > arg->argn+1) {
2127         ncpaddr_aton(ncpaddr + 1, &arg->bundle->ncp, arg->argv[arg->argn + 1]);
2128         if (!ncpaddr_getip4(ncpaddr + 1, ipaddr + 1))
2129           return -1;
2130       }
2131
2132       if (ipaddr[0].s_addr == INADDR_ANY) {
2133         ipaddr[0] = ipaddr[1];
2134         ipaddr[1].s_addr = INADDR_ANY;
2135       }
2136       if (ipaddr[0].s_addr == INADDR_NONE) {
2137         ipaddr[0] = ipaddr[1];
2138         ipaddr[1].s_addr = INADDR_NONE;
2139       }
2140     }
2141     break;
2142
2143   case VAR_CALLBACK:
2144     cx->cfg.callback.opmask = 0;
2145     for (dummyint = arg->argn; dummyint < arg->argc; dummyint++) {
2146       if (!strcasecmp(arg->argv[dummyint], "auth"))
2147         cx->cfg.callback.opmask |= CALLBACK_BIT(CALLBACK_AUTH);
2148       else if (!strcasecmp(arg->argv[dummyint], "cbcp"))
2149         cx->cfg.callback.opmask |= CALLBACK_BIT(CALLBACK_CBCP);
2150       else if (!strcasecmp(arg->argv[dummyint], "e.164")) {
2151         if (dummyint == arg->argc - 1)
2152           log_Printf(LogWARN, "No E.164 arg (E.164 ignored) !\n");
2153         else {
2154           cx->cfg.callback.opmask |= CALLBACK_BIT(CALLBACK_E164);
2155           strncpy(cx->cfg.callback.msg, arg->argv[++dummyint],
2156                   sizeof cx->cfg.callback.msg - 1);
2157           cx->cfg.callback.msg[sizeof cx->cfg.callback.msg - 1] = '\0';
2158         }
2159       } else if (!strcasecmp(arg->argv[dummyint], "none"))
2160         cx->cfg.callback.opmask |= CALLBACK_BIT(CALLBACK_NONE);
2161       else {
2162         res = -1;
2163         break;
2164       }
2165     }
2166     if (cx->cfg.callback.opmask == CALLBACK_BIT(CALLBACK_NONE))
2167       cx->cfg.callback.opmask = 0;
2168     break;
2169
2170   case VAR_CBCP:
2171     cx->cfg.cbcp.delay = 0;
2172     *cx->cfg.cbcp.phone = '\0';
2173     cx->cfg.cbcp.fsmretry = DEF_FSMRETRY;
2174     if (arg->argc > arg->argn) {
2175       strncpy(cx->cfg.cbcp.phone, arg->argv[arg->argn],
2176               sizeof cx->cfg.cbcp.phone - 1);
2177       cx->cfg.cbcp.phone[sizeof cx->cfg.cbcp.phone - 1] = '\0';
2178       if (arg->argc > arg->argn + 1) {
2179         cx->cfg.cbcp.delay = atoi(arg->argv[arg->argn + 1]);
2180         if (arg->argc > arg->argn + 2) {
2181           long_val = atol(arg->argv[arg->argn + 2]);
2182           if (long_val < MIN_FSMRETRY)
2183             log_Printf(LogWARN, "%ld: Invalid CBCP FSM retry period - min %d\n",
2184                        long_val, MIN_FSMRETRY);
2185           else
2186             cx->cfg.cbcp.fsmretry = long_val;
2187         }
2188       }
2189     }
2190     break;
2191
2192   case VAR_CHOKED:
2193     arg->bundle->cfg.choked.timeout = atoi(argp);
2194     if (arg->bundle->cfg.choked.timeout <= 0)
2195       arg->bundle->cfg.choked.timeout = CHOKED_TIMEOUT;
2196     break;
2197
2198   case VAR_SENDPIPE:
2199     long_val = atol(argp);
2200     arg->bundle->ncp.cfg.sendpipe = long_val;
2201     break;
2202
2203   case VAR_RECVPIPE:
2204     long_val = atol(argp);
2205     arg->bundle->ncp.cfg.recvpipe = long_val;
2206     break;
2207
2208 #ifndef NORADIUS
2209   case VAR_RADIUS:
2210     if (!*argp)
2211       *arg->bundle->radius.cfg.file = '\0';
2212     else if (access(argp, R_OK)) {
2213       log_Printf(LogWARN, "%s: %s\n", argp, strerror(errno));
2214       res = 1;
2215       break;
2216     } else {
2217       strncpy(arg->bundle->radius.cfg.file, argp,
2218               sizeof arg->bundle->radius.cfg.file - 1);
2219       arg->bundle->radius.cfg.file
2220         [sizeof arg->bundle->radius.cfg.file - 1] = '\0';
2221     }
2222     break;
2223 #endif
2224
2225   case VAR_CD:
2226     if (*argp) {
2227       if (strcasecmp(argp, "off")) {
2228         long_val = atol(argp);
2229         if (long_val < 0)
2230           long_val = 0;
2231         cx->physical->cfg.cd.delay = long_val;
2232         cx->physical->cfg.cd.necessity = argp[strlen(argp)-1] == '!' ?
2233           CD_REQUIRED : CD_VARIABLE;
2234       } else
2235         cx->physical->cfg.cd.necessity = CD_NOTREQUIRED;
2236     } else {
2237       cx->physical->cfg.cd.delay = 0;
2238       cx->physical->cfg.cd.necessity = CD_DEFAULT;
2239     }
2240     break;
2241
2242   case VAR_PARITY:
2243     if (arg->argc == arg->argn + 1)
2244       res = physical_SetParity(arg->cx->physical, argp);
2245     else {
2246       log_Printf(LogWARN, "Parity value must be odd, even or none\n");
2247       res = 1;
2248     }
2249     break;
2250
2251   case VAR_CRTSCTS:
2252     if (strcasecmp(argp, "on") == 0)
2253       physical_SetRtsCts(arg->cx->physical, 1);
2254     else if (strcasecmp(argp, "off") == 0)
2255       physical_SetRtsCts(arg->cx->physical, 0);
2256     else {
2257       log_Printf(LogWARN, "RTS/CTS value must be on or off\n");
2258       res = 1;
2259     }
2260     break;
2261
2262   case VAR_URGENTPORTS:
2263     if (arg->argn == arg->argc) {
2264       ncp_SetUrgentTOS(&arg->bundle->ncp);
2265       ncp_ClearUrgentTcpPorts(&arg->bundle->ncp);
2266       ncp_ClearUrgentUdpPorts(&arg->bundle->ncp);
2267     } else if (!strcasecmp(arg->argv[arg->argn], "udp")) {
2268       ncp_SetUrgentTOS(&arg->bundle->ncp);
2269       if (arg->argn == arg->argc - 1)
2270         ncp_ClearUrgentUdpPorts(&arg->bundle->ncp);
2271       else for (f = arg->argn + 1; f < arg->argc; f++)
2272         if (*arg->argv[f] == '+')
2273           ncp_AddUrgentUdpPort(&arg->bundle->ncp, atoi(arg->argv[f] + 1));
2274         else if (*arg->argv[f] == '-')
2275           ncp_RemoveUrgentUdpPort(&arg->bundle->ncp, atoi(arg->argv[f] + 1));
2276         else {
2277           if (f == arg->argn)
2278             ncp_ClearUrgentUdpPorts(&arg->bundle->ncp);
2279           ncp_AddUrgentUdpPort(&arg->bundle->ncp, atoi(arg->argv[f]));
2280         }
2281     } else if (arg->argn == arg->argc - 1 &&
2282                !strcasecmp(arg->argv[arg->argn], "none")) {
2283       ncp_ClearUrgentTcpPorts(&arg->bundle->ncp);
2284       ncp_ClearUrgentUdpPorts(&arg->bundle->ncp);
2285       ncp_ClearUrgentTOS(&arg->bundle->ncp);
2286     } else {
2287       ncp_SetUrgentTOS(&arg->bundle->ncp);
2288       first = arg->argn;
2289       if (!strcasecmp(arg->argv[first], "tcp") && ++first == arg->argc)
2290         ncp_ClearUrgentTcpPorts(&arg->bundle->ncp);
2291
2292       for (f = first; f < arg->argc; f++)
2293         if (*arg->argv[f] == '+')
2294           ncp_AddUrgentTcpPort(&arg->bundle->ncp, atoi(arg->argv[f] + 1));
2295         else if (*arg->argv[f] == '-')
2296           ncp_RemoveUrgentTcpPort(&arg->bundle->ncp, atoi(arg->argv[f] + 1));
2297         else {
2298           if (f == first)
2299             ncp_ClearUrgentTcpPorts(&arg->bundle->ncp);
2300           ncp_AddUrgentTcpPort(&arg->bundle->ncp, atoi(arg->argv[f]));
2301         }
2302     }
2303     break;
2304
2305   case VAR_PPPOE:
2306     if (strcasecmp(argp, "3Com") == 0)
2307       physical_SetPPPoEnonstandard(arg->cx->physical, 1);
2308     else if (strcasecmp(argp, "standard") == 0)
2309       physical_SetPPPoEnonstandard(arg->cx->physical, 0);
2310     else {
2311       log_Printf(LogWARN, "PPPoE standard value must be \"standard\" or \"3Com\"\n");
2312       res = 1;
2313     }
2314     break;
2315
2316 #ifndef NORADIUS
2317   case VAR_PORT_ID:
2318     if (strcasecmp(argp, "default") == 0)
2319             arg->bundle->radius.port_id_type = RPI_DEFAULT;
2320     else if (strcasecmp(argp, "pid") == 0)
2321             arg->bundle->radius.port_id_type = RPI_PID;
2322     else if (strcasecmp(argp, "ifnum") == 0)
2323             arg->bundle->radius.port_id_type = RPI_IFNUM; 
2324     else if (strcasecmp(argp, "tunnum") == 0)
2325             arg->bundle->radius.port_id_type = RPI_TUNNUM;
2326     else {
2327            log_Printf(LogWARN,
2328                 "RADIUS port id must be one of \"default\", \"pid\", \"ifnum\" or \"tunnum\"\n");
2329            res = 1;
2330     }
2331
2332     if (arg->bundle->radius.port_id_type && !arg->bundle->radius.cfg.file) {
2333             log_Printf(LogWARN, "rad_port_id requires radius to be configured\n");
2334             res = 1;
2335     }
2336
2337     break;
2338 #endif
2339   }
2340
2341   return res;
2342 }
2343
2344 static struct cmdtab const SetCommands[] = {
2345   {"accmap", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX,
2346   "accmap value", "set accmap hex-value", (const void *)VAR_ACCMAP},
2347   {"authkey", "key", SetVariable, LOCAL_AUTH,
2348   "authentication key", "set authkey|key key", (const void *)VAR_AUTHKEY},
2349   {"authname", NULL, SetVariable, LOCAL_AUTH,
2350   "authentication name", "set authname name", (const void *)VAR_AUTHNAME},
2351   {"autoload", NULL, SetVariable, LOCAL_AUTH,
2352   "auto link [de]activation", "set autoload maxtime maxload mintime minload",
2353   (const void *)VAR_AUTOLOAD},
2354   {"bandwidth", NULL, mp_SetDatalinkBandwidth, LOCAL_AUTH | LOCAL_CX,
2355   "datalink bandwidth", "set bandwidth value", NULL},
2356   {"callback", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX,
2357   "callback control", "set callback [none|auth|cbcp|"
2358   "E.164 *|number[,number]...]...", (const void *)VAR_CALLBACK},
2359   {"cbcp", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX,
2360   "CBCP control", "set cbcp [*|phone[,phone...] [delay [timeout]]]",
2361   (const void *)VAR_CBCP},
2362   {"ccpretry", "ccpretries", SetVariable, LOCAL_AUTH | LOCAL_CX_OPT,
2363    "CCP retries", "set ccpretry value [attempts]", (const void *)VAR_CCPRETRY},
2364   {"cd", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX, "Carrier delay requirement",
2365    "set cd value[!]", (const void *)VAR_CD},
2366   {"chapretry", "chapretries", SetVariable, LOCAL_AUTH | LOCAL_CX,
2367    "CHAP retries", "set chapretry value [attempts]",
2368    (const void *)VAR_CHAPRETRY},
2369   {"choked", NULL, SetVariable, LOCAL_AUTH,
2370   "choked timeout", "set choked [secs]", (const void *)VAR_CHOKED},
2371   {"ctsrts", "crtscts", SetVariable, LOCAL_AUTH | LOCAL_CX,
2372    "Use hardware flow control", "set ctsrts [on|off]",
2373    (const char *)VAR_CRTSCTS},
2374   {"deflate", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX_OPT,
2375   "deflate window sizes", "set deflate out-winsize in-winsize",
2376   (const void *) VAR_WINSIZE},
2377 #ifndef NODES
2378   {"mppe", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX_OPT,
2379   "MPPE key size and state", "set mppe [40|56|128|* [stateful|stateless|*]]",
2380   (const void *) VAR_MPPE},
2381 #endif
2382   {"device", "line", SetVariable, LOCAL_AUTH | LOCAL_CX,
2383   "physical device name", "set device|line device-name[,device-name]",
2384   (const void *) VAR_DEVICE},
2385   {"dial", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX,
2386   "dialing script", "set dial chat-script", (const void *) VAR_DIAL},
2387   {"dns", NULL, SetVariable, LOCAL_AUTH, "Domain Name Server",
2388   "set dns pri-addr [sec-addr]", (const void *)VAR_DNS},
2389   {"enddisc", NULL, mp_SetEnddisc, LOCAL_AUTH,
2390   "Endpoint Discriminator", "set enddisc [IP|magic|label|psn value]", NULL},
2391   {"escape", NULL, SetEscape, LOCAL_AUTH | LOCAL_CX,
2392   "escape characters", "set escape hex-digit ...", NULL},
2393   {"filter", NULL, filter_Set, LOCAL_AUTH,
2394   "packet filters", "set filter alive|dial|in|out rule-no permit|deny "
2395   "[src_addr[/width]] [dst_addr[/width]] [proto "
2396   "[src [lt|eq|gt port]] [dst [lt|eq|gt port]] [estab] [syn] [finrst]]", NULL},
2397   {"hangup", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX,
2398   "hangup script", "set hangup chat-script", (const void *) VAR_HANGUP},
2399   {"ifaddr", NULL, SetInterfaceAddr, LOCAL_AUTH, "destination address",
2400   "set ifaddr [src-addr [dst-addr [netmask [trg-addr]]]]", NULL},
2401   {"ifqueue", NULL, SetVariable, LOCAL_AUTH, "interface queue",
2402   "set ifqueue packets", (const void *)VAR_IFQUEUE},
2403   {"ipcpretry", "ipcpretries", SetVariable, LOCAL_AUTH, "IPCP retries",
2404    "set ipcpretry value [attempts]", (const void *)VAR_IPCPRETRY},
2405   {"ipv6cpretry", "ipv6cpretries", SetVariable, LOCAL_AUTH, "IPV6CP retries",
2406    "set ipv6cpretry value [attempts]", (const void *)VAR_IPV6CPRETRY},
2407   {"lcpretry", "lcpretries", SetVariable, LOCAL_AUTH | LOCAL_CX, "LCP retries",
2408    "set lcpretry value [attempts]", (const void *)VAR_LCPRETRY},
2409   {"log", NULL, log_SetLevel, LOCAL_AUTH, "log level",
2410   "set log [local] [+|-]all|async|cbcp|ccp|chat|command|connect|debug|dns|hdlc|"
2411   "id0|ipcp|lcp|lqm|phase|physical|radius|sync|tcp/ip|timer|tun...", NULL},
2412   {"login", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX,
2413   "login script", "set login chat-script", (const void *) VAR_LOGIN},
2414   {"logout", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX,
2415   "logout script", "set logout chat-script", (const void *) VAR_LOGOUT},
2416   {"lqrperiod", "echoperiod", SetVariable, LOCAL_AUTH | LOCAL_CX_OPT,
2417   "LQR period", "set lqr/echo period value", (const void *)VAR_LQRPERIOD},
2418   {"mode", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX, "mode value",
2419   "set mode interactive|auto|ddial|background", (const void *)VAR_MODE},
2420   {"mrru", NULL, SetVariable, LOCAL_AUTH, "MRRU value",
2421   "set mrru value", (const void *)VAR_MRRU},
2422   {"mru", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX,
2423   "MRU value", "set mru [max[imum]] [value]", (const void *)VAR_MRU},
2424   {"mtu", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX,
2425   "interface MTU value", "set mtu [max[imum]] [value]", (const void *)VAR_MTU},
2426   {"nbns", NULL, SetVariable, LOCAL_AUTH, "NetBIOS Name Server",
2427   "set nbns pri-addr [sec-addr]", (const void *)VAR_NBNS},
2428   {"openmode", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX, "open mode",
2429   "set openmode active|passive [secs]", (const void *)VAR_OPENMODE},
2430   {"papretry", "papretries", SetVariable, LOCAL_AUTH | LOCAL_CX, "PAP retries",
2431    "set papretry value [attempts]", (const void *)VAR_PAPRETRY},
2432   {"parity", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX, "serial parity",
2433    "set parity [odd|even|none]", (const void *)VAR_PARITY},
2434   {"phone", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX, "telephone number(s)",
2435   "set phone phone1[:phone2[...]]", (const void *)VAR_PHONE},
2436   {"proctitle", "title", SetProcTitle, LOCAL_AUTH,
2437   "Process title", "set proctitle [value]", NULL},
2438 #ifndef NORADIUS
2439   {"radius", NULL, SetVariable, LOCAL_AUTH,
2440   "RADIUS Config", "set radius cfgfile", (const void *)VAR_RADIUS},
2441   {"rad_alive", NULL, SetVariable, LOCAL_AUTH,
2442   "Raduis alive interval", "set rad_alive value",
2443   (const void *)VAR_RAD_ALIVE},
2444   {"rad_port_id", NULL, SetVariable, LOCAL_AUTH,
2445   "NAS-Port-Id", "set rad_port_id [default|pid|ifnum|tunnum]", (const void *)VAR_PORT_ID},
2446 #endif
2447   {"reconnect", NULL, datalink_SetReconnect, LOCAL_AUTH | LOCAL_CX,
2448   "Reconnect timeout", "set reconnect value ntries", NULL},
2449   {"recvpipe", NULL, SetVariable, LOCAL_AUTH,
2450   "RECVPIPE value", "set recvpipe value", (const void *)VAR_RECVPIPE},
2451   {"redial", NULL, datalink_SetRedial, LOCAL_AUTH | LOCAL_CX,
2452   "Redial timeout", "set redial secs[+inc[-incmax]][.next] [attempts]", NULL},
2453   {"sendpipe", NULL, SetVariable, LOCAL_AUTH,
2454   "SENDPIPE value", "set sendpipe value", (const void *)VAR_SENDPIPE},
2455   {"server", "socket", SetServer, LOCAL_AUTH, "diagnostic port",
2456   "set server|socket TcpPort|LocalName|none|open|closed [password [mask]]",
2457   NULL},
2458   {"speed", NULL, SetModemSpeed, LOCAL_AUTH | LOCAL_CX,
2459   "physical speed", "set speed value|sync", NULL},
2460   {"stopped", NULL, SetStoppedTimeout, LOCAL_AUTH | LOCAL_CX,
2461   "STOPPED timeouts", "set stopped [LCPseconds [CCPseconds]]", NULL},
2462   {"timeout", NULL, SetVariable, LOCAL_AUTH, "Idle timeout",
2463   "set timeout idletime", (const void *)VAR_IDLETIMEOUT},
2464   {"urgent", NULL, SetVariable, LOCAL_AUTH, "urgent ports",
2465   "set urgent [tcp|udp] [+|-]port...", (const void *)VAR_URGENTPORTS},
2466   {"vj", NULL, ipcp_vjset, LOCAL_AUTH,
2467   "vj values", "set vj slots|slotcomp [value]", NULL},
2468   {"help", "?", HelpCommand, LOCAL_AUTH | LOCAL_NO_AUTH,
2469   "Display this message", "set help|? [command]", SetCommands},
2470   {"pppoe", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX,
2471    "Connect using standard/3Com mode", "set pppoe [standard|3Com]",
2472    (const char *)VAR_PPPOE},
2473   {NULL, NULL, NULL, 0, NULL, NULL, NULL},
2474 };
2475
2476 static int
2477 SetCommand(struct cmdargs const *arg)
2478 {
2479   if (arg->argc > arg->argn)
2480     FindExec(arg->bundle, SetCommands, arg->argc, arg->argn, arg->argv,
2481              arg->prompt, arg->cx);
2482   else if (arg->prompt)
2483     prompt_Printf(arg->prompt, "Use `set ?' to get a list or `set ? <var>' for"
2484                   " syntax help.\n");
2485   else
2486     log_Printf(LogWARN, "set command must have arguments\n");
2487
2488   return 0;
2489 }
2490
2491 static int
2492 AddCommand(struct cmdargs const *arg)
2493 {
2494   struct ncpaddr gw;
2495   struct ncprange dest;
2496   struct in_addr host;
2497 #ifndef NOINET6
2498   struct in6_addr host6;
2499 #endif
2500   int dest_default, gw_arg, addrs;
2501
2502   if (arg->argc != arg->argn+3 && arg->argc != arg->argn+2)
2503     return -1;
2504
2505   addrs = 0;
2506   dest_default = 0;
2507   if (arg->argc == arg->argn + 2) {
2508     if (!strcasecmp(arg->argv[arg->argn], "default"))
2509       dest_default = 1;
2510     else {
2511       if (!ncprange_aton(&dest, &arg->bundle->ncp, arg->argv[arg->argn]))
2512         return -1;
2513       if (!strncasecmp(arg->argv[arg->argn], "MYADDR", 6))
2514         addrs = ROUTE_DSTMYADDR;
2515       else if (!strncasecmp(arg->argv[arg->argn], "MYADDR6", 7))
2516         addrs = ROUTE_DSTMYADDR6;
2517       else if (!strncasecmp(arg->argv[arg->argn], "HISADDR", 7))
2518         addrs = ROUTE_DSTHISADDR;
2519       else if (!strncasecmp(arg->argv[arg->argn], "HISADDR6", 8))
2520         addrs = ROUTE_DSTHISADDR6;
2521       else if (!strncasecmp(arg->argv[arg->argn], "DNS0", 4))
2522         addrs = ROUTE_DSTDNS0;
2523       else if (!strncasecmp(arg->argv[arg->argn], "DNS1", 4))
2524         addrs = ROUTE_DSTDNS1;
2525     }
2526     gw_arg = 1;
2527   } else {
2528     if (strcasecmp(arg->argv[arg->argn], "MYADDR") == 0) {
2529       addrs = ROUTE_DSTMYADDR;
2530       host = arg->bundle->ncp.ipcp.my_ip;
2531     } else if (strcasecmp(arg->argv[arg->argn], "HISADDR") == 0) {
2532       addrs = ROUTE_DSTHISADDR;
2533       host = arg->bundle->ncp.ipcp.peer_ip;
2534     } else if (strcasecmp(arg->argv[arg->argn], "DNS0") == 0) {
2535       addrs = ROUTE_DSTDNS0;
2536       host = arg->bundle->ncp.ipcp.ns.dns[0];
2537     } else if (strcasecmp(arg->argv[arg->argn], "DNS1") == 0) {
2538       addrs = ROUTE_DSTDNS1;
2539       host = arg->bundle->ncp.ipcp.ns.dns[1];
2540     } else {
2541       host = GetIpAddr(arg->argv[arg->argn]);
2542       if (host.s_addr == INADDR_NONE) {
2543         log_Printf(LogWARN, "%s: Invalid destination address\n",
2544                    arg->argv[arg->argn]);
2545         return -1;
2546       }
2547     }
2548     ncprange_setip4(&dest, host, GetIpAddr(arg->argv[arg->argn + 1]));
2549     gw_arg = 2;
2550   }
2551
2552   if (strcasecmp(arg->argv[arg->argn + gw_arg], "HISADDR") == 0) {
2553     ncpaddr_setip4(&gw, arg->bundle->ncp.ipcp.peer_ip);
2554     addrs |= ROUTE_GWHISADDR;
2555 #ifndef NOINET6
2556   } else if (strcasecmp(arg->argv[arg->argn + gw_arg], "HISADDR6") == 0) {
2557     if (!ncpaddr_getip6(&arg->bundle->ncp.ipv6cp.hisaddr, &host6))
2558       memset(&host6, '\0', sizeof host6);
2559     ncpaddr_setip6(&gw, &host6);
2560     addrs |= ROUTE_GWHISADDR6;
2561 #endif
2562   } else {
2563     if (!ncpaddr_aton(&gw, &arg->bundle->ncp, arg->argv[arg->argn + gw_arg])) {
2564       log_Printf(LogWARN, "%s: Invalid gateway address\n",
2565                  arg->argv[arg->argn + gw_arg]);
2566       return -1;
2567     }
2568   }
2569
2570   if (dest_default)
2571     ncprange_setdefault(&dest, ncpaddr_family(&gw));
2572
2573   if (rt_Set(arg->bundle, RTM_ADD, &dest, &gw, arg->cmd->args ? 1 : 0,
2574              ((addrs & ROUTE_GWHISADDR) || (addrs & ROUTE_GWHISADDR6)) ? 1 : 0)
2575       && addrs != ROUTE_STATIC)
2576     route_Add(&arg->bundle->ncp.route, addrs, &dest, &gw);
2577
2578   return 0;
2579 }
2580
2581 static int
2582 DeleteCommand(struct cmdargs const *arg)
2583 {
2584   struct ncprange dest;
2585   int addrs;
2586
2587   if (arg->argc == arg->argn+1) {
2588     if(strcasecmp(arg->argv[arg->argn], "all") == 0) {
2589       route_IfDelete(arg->bundle, 0);
2590       route_DeleteAll(&arg->bundle->ncp.route);
2591     } else {
2592       addrs = 0;
2593       if (strcasecmp(arg->argv[arg->argn], "MYADDR") == 0) {
2594         ncprange_setip4host(&dest, arg->bundle->ncp.ipcp.my_ip);
2595         addrs = ROUTE_DSTMYADDR;
2596 #ifndef NOINET6
2597       } else if (strcasecmp(arg->argv[arg->argn], "MYADDR6") == 0) {
2598         ncprange_sethost(&dest, &arg->bundle->ncp.ipv6cp.myaddr);
2599         addrs = ROUTE_DSTMYADDR6;
2600 #endif
2601       } else if (strcasecmp(arg->argv[arg->argn], "HISADDR") == 0) {
2602         ncprange_setip4host(&dest, arg->bundle->ncp.ipcp.peer_ip);
2603         addrs = ROUTE_DSTHISADDR;
2604 #ifndef NOINET6
2605       } else if (strcasecmp(arg->argv[arg->argn], "HISADDR6") == 0) {
2606         ncprange_sethost(&dest, &arg->bundle->ncp.ipv6cp.hisaddr);
2607         addrs = ROUTE_DSTHISADDR6;
2608 #endif
2609       } else if (strcasecmp(arg->argv[arg->argn], "DNS0") == 0) {
2610         ncprange_setip4host(&dest, arg->bundle->ncp.ipcp.ns.dns[0]);
2611         addrs = ROUTE_DSTDNS0;
2612       } else if (strcasecmp(arg->argv[arg->argn], "DNS1") == 0) {
2613         ncprange_setip4host(&dest, arg->bundle->ncp.ipcp.ns.dns[1]);
2614         addrs = ROUTE_DSTDNS1;
2615       } else {
2616         ncprange_aton(&dest, &arg->bundle->ncp, arg->argv[arg->argn]);
2617         addrs = ROUTE_STATIC;
2618       }
2619       rt_Set(arg->bundle, RTM_DELETE, &dest, NULL, arg->cmd->args ? 1 : 0, 0);
2620       route_Delete(&arg->bundle->ncp.route, addrs, &dest);
2621     }
2622   } else
2623     return -1;
2624
2625   return 0;
2626 }
2627
2628 #ifndef NONAT
2629 static int
2630 NatEnable(struct cmdargs const *arg)
2631 {
2632   if (arg->argc == arg->argn+1) {
2633     if (strcasecmp(arg->argv[arg->argn], "yes") == 0) {
2634       if (!arg->bundle->NatEnabled) {
2635         if (arg->bundle->ncp.ipcp.fsm.state == ST_OPENED)
2636           PacketAliasSetAddress(arg->bundle->ncp.ipcp.my_ip);
2637         arg->bundle->NatEnabled = 1;
2638       }
2639       return 0;
2640     } else if (strcasecmp(arg->argv[arg->argn], "no") == 0) {
2641       arg->bundle->NatEnabled = 0;
2642       opt_disable(arg->bundle, OPT_IFACEALIAS);
2643       /* Don't iface_Clear() - there may be manually configured addresses */
2644       return 0;
2645     }
2646   }
2647
2648   return -1;
2649 }
2650
2651
2652 static int
2653 NatOption(struct cmdargs const *arg)
2654 {
2655   long param = (long)arg->cmd->args;
2656
2657   if (arg->argc == arg->argn+1) {
2658     if (strcasecmp(arg->argv[arg->argn], "yes") == 0) {
2659       if (arg->bundle->NatEnabled) {
2660         PacketAliasSetMode(param, param);
2661         return 0;
2662       }
2663       log_Printf(LogWARN, "nat not enabled\n");
2664     } else if (strcmp(arg->argv[arg->argn], "no") == 0) {
2665       if (arg->bundle->NatEnabled) {
2666         PacketAliasSetMode(0, param);
2667         return 0;
2668       }
2669       log_Printf(LogWARN, "nat not enabled\n");
2670     }
2671   }
2672   return -1;
2673 }
2674 #endif /* #ifndef NONAT */
2675
2676 static int
2677 LinkCommand(struct cmdargs const *arg)
2678 {
2679   if (arg->argc > arg->argn+1) {
2680     char namelist[LINE_LEN];
2681     struct datalink *cx;
2682     char *name;
2683     int result = 0;
2684
2685     if (!strcmp(arg->argv[arg->argn], "*")) {
2686       struct datalink *dl;
2687
2688       cx = arg->bundle->links;
2689       while (cx) {
2690         /* Watch it, the command could be a ``remove'' */
2691         dl = cx->next;
2692         FindExec(arg->bundle, Commands, arg->argc, arg->argn+1, arg->argv,
2693                  arg->prompt, cx);
2694         for (cx = arg->bundle->links; cx; cx = cx->next)
2695           if (cx == dl)
2696             break;              /* Pointer's still valid ! */
2697       }
2698     } else {
2699       strncpy(namelist, arg->argv[arg->argn], sizeof namelist - 1);
2700       namelist[sizeof namelist - 1] = '\0';
2701       for(name = strtok(namelist, ", "); name; name = strtok(NULL,", "))
2702         if (!bundle2datalink(arg->bundle, name)) {
2703           log_Printf(LogWARN, "link: %s: Invalid link name\n", name);
2704           return 1;
2705         }
2706
2707       strncpy(namelist, arg->argv[arg->argn], sizeof namelist - 1);
2708       namelist[sizeof namelist - 1] = '\0';
2709       for(name = strtok(namelist, ", "); name; name = strtok(NULL,", ")) {
2710         cx = bundle2datalink(arg->bundle, name);
2711         if (cx)
2712           FindExec(arg->bundle, Commands, arg->argc, arg->argn+1, arg->argv,
2713                    arg->prompt, cx);
2714         else {
2715           log_Printf(LogWARN, "link: %s: Invalidated link name !\n", name);
2716           result++;
2717         }
2718       }
2719     }
2720     return result;
2721   }
2722
2723   log_Printf(LogWARN, "usage: %s\n", arg->cmd->syntax);
2724   return 2;
2725 }
2726
2727 struct link *
2728 command_ChooseLink(struct cmdargs const *arg)
2729 {
2730   if (arg->cx)
2731     return &arg->cx->physical->link;
2732   else if (!arg->bundle->ncp.mp.cfg.mrru) {
2733     struct datalink *dl = bundle2datalink(arg->bundle, NULL);
2734     if (dl)
2735       return &dl->physical->link;
2736   }
2737   return &arg->bundle->ncp.mp.link;
2738 }
2739
2740 static const char *
2741 ident_cmd(const char *cmd, unsigned *keep, unsigned *add)
2742 {
2743   const char *result;
2744
2745   switch (*cmd) {
2746     case 'A':
2747     case 'a':
2748       result = "accept";
2749       *keep = NEG_MYMASK;
2750       *add = NEG_ACCEPTED;
2751       break;
2752     case 'D':
2753     case 'd':
2754       switch (cmd[1]) {
2755         case 'E':
2756         case 'e':
2757           result = "deny";
2758           *keep = NEG_MYMASK;
2759           *add = 0;
2760           break;
2761         case 'I':
2762         case 'i':
2763           result = "disable";
2764           *keep = NEG_HISMASK;
2765           *add = 0;
2766           break;
2767         default:
2768           return NULL;
2769       }
2770       break;
2771     case 'E':
2772     case 'e':
2773       result = "enable";
2774       *keep = NEG_HISMASK;
2775       *add = NEG_ENABLED;
2776       break;
2777     default:
2778       return NULL;
2779   }
2780
2781   return result;
2782 }
2783
2784 static int
2785 OptSet(struct cmdargs const *arg)
2786 {
2787   int opt = (int)(long)arg->cmd->args;
2788   unsigned keep;                        /* Keep this opt */
2789   unsigned add;                         /* Add this opt */
2790
2791   if (ident_cmd(arg->argv[arg->argn - 2], &keep, &add) == NULL)
2792     return 1;
2793
2794 #ifndef NOINET6
2795   if (add == NEG_ENABLED && opt == OPT_IPV6CP && !probe.ipv6_available) {
2796     log_Printf(LogWARN, "IPv6 is not available on this machine\n");
2797     return 1;
2798   }
2799 #endif
2800   if (!add && ((opt == OPT_NAS_IP_ADDRESS &&
2801                 !Enabled(arg->bundle, OPT_NAS_IDENTIFIER)) ||
2802                (opt == OPT_NAS_IDENTIFIER &&
2803                 !Enabled(arg->bundle, OPT_NAS_IP_ADDRESS)))) {
2804     log_Printf(LogWARN,
2805                "Cannot disable both NAS-IP-Address and NAS-Identifier\n");
2806     return 1;
2807   }
2808
2809   if (add)
2810     opt_enable(arg->bundle, opt);
2811   else
2812     opt_disable(arg->bundle, opt);
2813
2814   return 0;
2815 }
2816
2817 static int
2818 IfaceAliasOptSet(struct cmdargs const *arg)
2819 {
2820   unsigned long long save = arg->bundle->cfg.optmask;
2821   int result = OptSet(arg);
2822
2823   if (result == 0)
2824     if (Enabled(arg->bundle, OPT_IFACEALIAS) && !arg->bundle->NatEnabled) {
2825       arg->bundle->cfg.optmask = save;
2826       log_Printf(LogWARN, "Cannot enable iface-alias without NAT\n");
2827       result = 2;
2828     }
2829
2830   return result;
2831 }
2832
2833 static int
2834 NegotiateSet(struct cmdargs const *arg)
2835 {
2836   long param = (long)arg->cmd->args;
2837   struct link *l = command_ChooseLink(arg);     /* LOCAL_CX_OPT uses this */
2838   struct datalink *cx = arg->cx;        /* LOCAL_CX uses this */
2839   const char *cmd;
2840   unsigned keep;                        /* Keep these bits */
2841   unsigned add;                         /* Add these bits */
2842
2843   if ((cmd = ident_cmd(arg->argv[arg->argn-2], &keep, &add)) == NULL)
2844     return 1;
2845
2846   if ((arg->cmd->lauth & LOCAL_CX) && !cx) {
2847     log_Printf(LogWARN, "%s %s: No context (use the `link' command)\n",
2848               cmd, arg->cmd->name);
2849     return 2;
2850   } else if (cx && !(arg->cmd->lauth & (LOCAL_CX|LOCAL_CX_OPT))) {
2851     log_Printf(LogWARN, "%s %s: Redundant context (%s) ignored\n",
2852               cmd, arg->cmd->name, cx->name);
2853     cx = NULL;
2854   }
2855
2856   switch (param) {
2857     case NEG_ACFCOMP:
2858       cx->physical->link.lcp.cfg.acfcomp &= keep;
2859       cx->physical->link.lcp.cfg.acfcomp |= add;
2860       break;
2861     case NEG_CHAP05:
2862       cx->physical->link.lcp.cfg.chap05 &= keep;
2863       cx->physical->link.lcp.cfg.chap05 |= add;
2864       break;
2865 #ifndef NODES
2866     case NEG_CHAP80:
2867       cx->physical->link.lcp.cfg.chap80nt &= keep;
2868       cx->physical->link.lcp.cfg.chap80nt |= add;
2869       break;
2870     case NEG_CHAP80LM:
2871       cx->physical->link.lcp.cfg.chap80lm &= keep;
2872       cx->physical->link.lcp.cfg.chap80lm |= add;
2873       break;
2874     case NEG_CHAP81:
2875       cx->physical->link.lcp.cfg.chap81 &= keep;
2876       cx->physical->link.lcp.cfg.chap81 |= add;
2877       break;
2878     case NEG_MPPE:
2879       l->ccp.cfg.neg[CCP_NEG_MPPE] &= keep;
2880       l->ccp.cfg.neg[CCP_NEG_MPPE] |= add;
2881       break;
2882 #endif
2883     case NEG_DEFLATE:
2884       l->ccp.cfg.neg[CCP_NEG_DEFLATE] &= keep;
2885       l->ccp.cfg.neg[CCP_NEG_DEFLATE] |= add;
2886       break;
2887     case NEG_DNS:
2888       arg->bundle->ncp.ipcp.cfg.ns.dns_neg &= keep;
2889       arg->bundle->ncp.ipcp.cfg.ns.dns_neg |= add;
2890       break;
2891     case NEG_ECHO:      /* probably misplaced in this function ! */
2892       if (cx->physical->link.lcp.cfg.echo && !add) {
2893         cx->physical->link.lcp.cfg.echo = 0;
2894         cx->physical->hdlc.lqm.method &= ~LQM_ECHO;
2895         if (cx->physical->hdlc.lqm.method & LQM_ECHO &&
2896             !cx->physical->link.lcp.want_lqrperiod && 
2897             cx->physical->hdlc.lqm.timer.load) {
2898           cx->physical->hdlc.lqm.timer.load = 0;
2899           lqr_StopTimer(cx->physical);
2900         }
2901       } else if (!cx->physical->link.lcp.cfg.echo && add) {
2902         cx->physical->link.lcp.cfg.echo = 1;
2903         cx->physical->hdlc.lqm.method |= LQM_ECHO;
2904         cx->physical->hdlc.lqm.timer.load =
2905             cx->physical->link.lcp.cfg.lqrperiod * SECTICKS;
2906         if (cx->physical->link.lcp.fsm.state == ST_OPENED)
2907           (*cx->physical->hdlc.lqm.timer.func)(&cx->physical->link.lcp);
2908       }
2909       break;
2910     case NEG_ENDDISC:
2911       arg->bundle->ncp.mp.cfg.negenddisc &= keep;
2912       arg->bundle->ncp.mp.cfg.negenddisc |= add;
2913       break;
2914     case NEG_LQR:
2915       cx->physical->link.lcp.cfg.lqr &= keep;
2916       cx->physical->link.lcp.cfg.lqr |= add;
2917       break;
2918     case NEG_PAP:
2919       cx->physical->link.lcp.cfg.pap &= keep;
2920       cx->physical->link.lcp.cfg.pap |= add;
2921       break;
2922     case NEG_PPPDDEFLATE:
2923       l->ccp.cfg.neg[CCP_NEG_DEFLATE24] &= keep;
2924       l->ccp.cfg.neg[CCP_NEG_DEFLATE24] |= add;
2925       break;
2926     case NEG_PRED1:
2927       l->ccp.cfg.neg[CCP_NEG_PRED1] &= keep;
2928       l->ccp.cfg.neg[CCP_NEG_PRED1] |= add;
2929       break;
2930     case NEG_PROTOCOMP:
2931       cx->physical->link.lcp.cfg.protocomp &= keep;
2932       cx->physical->link.lcp.cfg.protocomp |= add;
2933       break;
2934     case NEG_SHORTSEQ:
2935       switch (bundle_Phase(arg->bundle)) {
2936         case PHASE_DEAD:
2937           break;
2938         case PHASE_ESTABLISH:
2939           /* Make sure none of our links are DATALINK_LCP or greater */
2940           if (bundle_HighestState(arg->bundle) >= DATALINK_LCP) {
2941             log_Printf(LogWARN, "shortseq: Only changable before"
2942                        " LCP negotiations\n");
2943             return 1;
2944           }
2945           break;
2946         default:
2947           log_Printf(LogWARN, "shortseq: Only changable at phase"
2948                      " DEAD/ESTABLISH\n");
2949           return 1;
2950       }
2951       arg->bundle->ncp.mp.cfg.shortseq &= keep;
2952       arg->bundle->ncp.mp.cfg.shortseq |= add;
2953       break;
2954     case NEG_VJCOMP:
2955       arg->bundle->ncp.ipcp.cfg.vj.neg &= keep;
2956       arg->bundle->ncp.ipcp.cfg.vj.neg |= add;
2957       break;
2958   }
2959
2960   return 0;
2961 }
2962
2963 static struct cmdtab const NegotiateCommands[] = {
2964   {"echo", NULL, NegotiateSet, LOCAL_AUTH | LOCAL_CX, "Send echo requests",
2965   "disable|enable", (const void *)NEG_ECHO},
2966   {"filter-decapsulation", NULL, OptSet, LOCAL_AUTH,
2967   "filter on PPPoUDP payloads", "disable|enable",
2968   (const void *)OPT_FILTERDECAP},
2969   {"force-scripts", NULL, OptSet, LOCAL_AUTH,
2970    "Force execution of the configured chat scripts", "disable|enable",
2971    (const void *)OPT_FORCE_SCRIPTS},
2972   {"idcheck", NULL, OptSet, LOCAL_AUTH, "Check FSM reply ids",
2973   "disable|enable", (const void *)OPT_IDCHECK},
2974   {"iface-alias", NULL, IfaceAliasOptSet, LOCAL_AUTH,
2975   "retain interface addresses", "disable|enable",
2976   (const void *)OPT_IFACEALIAS},
2977 #ifndef NOINET6
2978   {"ipcp", NULL, OptSet, LOCAL_AUTH, "IP Network Control Protocol",
2979   "disable|enable", (const void *)OPT_IPCP},
2980   {"ipv6cp", NULL, OptSet, LOCAL_AUTH, "IPv6 Network Control Protocol",
2981   "disable|enable", (const void *)OPT_IPV6CP},
2982 #endif
2983   {"keep-session", NULL, OptSet, LOCAL_AUTH, "Retain device session leader",
2984   "disable|enable", (const void *)OPT_KEEPSESSION},
2985   {"loopback", NULL, OptSet, LOCAL_AUTH, "Loop packets for local iface",
2986   "disable|enable", (const void *)OPT_LOOPBACK},
2987   {"nas-ip-address", NULL, OptSet, LOCAL_AUTH, "Send NAS-IP-Address to RADIUS",
2988   "disable|enable", (const void *)OPT_NAS_IP_ADDRESS},
2989   {"nas-identifier", NULL, OptSet, LOCAL_AUTH, "Send NAS-Identifier to RADIUS",
2990   "disable|enable", (const void *)OPT_NAS_IDENTIFIER},
2991   {"passwdauth", NULL, OptSet, LOCAL_AUTH, "Use passwd file",
2992   "disable|enable", (const void *)OPT_PASSWDAUTH},
2993   {"proxy", NULL, OptSet, LOCAL_AUTH, "Create a proxy ARP entry",
2994   "disable|enable", (const void *)OPT_PROXY},
2995   {"proxyall", NULL, OptSet, LOCAL_AUTH, "Proxy ARP for all remote hosts",
2996   "disable|enable", (const void *)OPT_PROXYALL},
2997   {"sroutes", NULL, OptSet, LOCAL_AUTH, "Use sticky routes",
2998   "disable|enable", (const void *)OPT_SROUTES},
2999   {"tcpmssfixup", "mssfixup", OptSet, LOCAL_AUTH, "Modify MSS options",
3000   "disable|enable", (const void *)OPT_TCPMSSFIXUP},
3001   {"throughput", NULL, OptSet, LOCAL_AUTH, "Rolling throughput",
3002   "disable|enable", (const void *)OPT_THROUGHPUT},
3003   {"utmp", NULL, OptSet, LOCAL_AUTH, "Log connections in utmp",
3004   "disable|enable", (const void *)OPT_UTMP},
3005
3006 #ifndef NOINET6
3007 #define NEG_OPT_MAX 17  /* accept/deny allowed below and not above */
3008 #else
3009 #define NEG_OPT_MAX 15
3010 #endif
3011
3012   {"acfcomp", NULL, NegotiateSet, LOCAL_AUTH | LOCAL_CX,
3013   "Address & Control field compression", "accept|deny|disable|enable",
3014   (const void *)NEG_ACFCOMP},
3015   {"chap", "chap05", NegotiateSet, LOCAL_AUTH | LOCAL_CX,
3016   "Challenge Handshake Authentication Protocol", "accept|deny|disable|enable",
3017   (const void *)NEG_CHAP05},
3018 #ifndef NODES
3019   {"mschap", "chap80nt", NegotiateSet, LOCAL_AUTH | LOCAL_CX,
3020   "Microsoft (NT) CHAP", "accept|deny|disable|enable",
3021   (const void *)NEG_CHAP80},
3022   {"LANMan", "chap80lm", NegotiateSet, LOCAL_AUTH | LOCAL_CX,
3023   "Microsoft (NT) CHAP", "accept|deny|disable|enable",
3024   (const void *)NEG_CHAP80LM},
3025   {"mschapv2", "chap81", NegotiateSet, LOCAL_AUTH | LOCAL_CX,
3026   "Microsoft CHAP v2", "accept|deny|disable|enable",
3027   (const void *)NEG_CHAP81},
3028   {"mppe", NULL, NegotiateSet, LOCAL_AUTH | LOCAL_CX_OPT,
3029   "MPPE encryption", "accept|deny|disable|enable",
3030   (const void *)NEG_MPPE},
3031 #endif
3032   {"deflate", NULL, NegotiateSet, LOCAL_AUTH | LOCAL_CX_OPT,
3033   "Deflate compression", "accept|deny|disable|enable",
3034   (const void *)NEG_DEFLATE},
3035   {"deflate24", NULL, NegotiateSet, LOCAL_AUTH | LOCAL_CX_OPT,
3036   "Deflate (type 24) compression", "accept|deny|disable|enable",
3037   (const void *)NEG_PPPDDEFLATE},
3038   {"dns", NULL, NegotiateSet, LOCAL_AUTH,
3039   "DNS specification", "accept|deny|disable|enable", (const void *)NEG_DNS},
3040   {"enddisc", NULL, NegotiateSet, LOCAL_AUTH, "ENDDISC negotiation",
3041   "accept|deny|disable|enable", (const void *)NEG_ENDDISC},
3042   {"lqr", NULL, NegotiateSet, LOCAL_AUTH | LOCAL_CX,
3043   "Link Quality Reports", "accept|deny|disable|enable",
3044   (const void *)NEG_LQR},
3045   {"pap", NULL, NegotiateSet, LOCAL_AUTH | LOCAL_CX,
3046   "Password Authentication protocol", "accept|deny|disable|enable",
3047   (const void *)NEG_PAP},
3048   {"pred1", "predictor1", NegotiateSet, LOCAL_AUTH | LOCAL_CX_OPT,
3049   "Predictor 1 compression", "accept|deny|disable|enable",
3050   (const void *)NEG_PRED1},
3051   {"protocomp", NULL, NegotiateSet, LOCAL_AUTH | LOCAL_CX,
3052   "Protocol field compression", "accept|deny|disable|enable",
3053   (const void *)NEG_PROTOCOMP},
3054   {"shortseq", NULL, NegotiateSet, LOCAL_AUTH,
3055   "MP Short Sequence Numbers", "accept|deny|disable|enable",
3056   (const void *)NEG_SHORTSEQ},
3057   {"vjcomp", NULL, NegotiateSet, LOCAL_AUTH,
3058   "Van Jacobson header compression", "accept|deny|disable|enable",
3059   (const void *)NEG_VJCOMP},
3060   {"help", "?", HelpCommand, LOCAL_AUTH | LOCAL_NO_AUTH,
3061   "Display this message", "accept|deny|disable|enable help|? [value]",
3062   NegotiateCommands},
3063   {NULL, NULL, NULL, 0, NULL, NULL, NULL},
3064 };
3065
3066 static int
3067 NegotiateCommand(struct cmdargs const *arg)
3068 {
3069   if (arg->argc > arg->argn) {
3070     char const *argv[3];
3071     unsigned keep, add;
3072     int n;
3073
3074     if ((argv[0] = ident_cmd(arg->argv[arg->argn-1], &keep, &add)) == NULL)
3075       return -1;
3076     argv[2] = NULL;
3077
3078     for (n = arg->argn; n < arg->argc; n++) {
3079       argv[1] = arg->argv[n];
3080       FindExec(arg->bundle, NegotiateCommands + (keep == NEG_HISMASK ?
3081                0 : NEG_OPT_MAX), 2, 1, argv, arg->prompt, arg->cx);
3082     }
3083   } else if (arg->prompt)
3084     prompt_Printf(arg->prompt, "Use `%s ?' to get a list.\n",
3085             arg->argv[arg->argn-1]);
3086   else
3087     log_Printf(LogWARN, "%s command must have arguments\n",
3088               arg->argv[arg->argn] );
3089
3090   return 0;
3091 }
3092
3093 const char *
3094 command_ShowNegval(unsigned val)
3095 {
3096   switch (val&3) {
3097     case 1: return "disabled & accepted";
3098     case 2: return "enabled & denied";
3099     case 3: return "enabled & accepted";
3100   }
3101   return "disabled & denied";
3102 }
3103
3104 static int
3105 ClearCommand(struct cmdargs const *arg)
3106 {
3107   struct pppThroughput *t;
3108   struct datalink *cx;
3109   int i, clear_type;
3110
3111   if (arg->argc < arg->argn + 1)
3112     return -1;
3113
3114   if (strcasecmp(arg->argv[arg->argn], "physical") == 0) {
3115     cx = arg->cx;
3116     if (!cx)
3117       cx = bundle2datalink(arg->bundle, NULL);
3118     if (!cx) {
3119       log_Printf(LogWARN, "A link must be specified for ``clear physical''\n");
3120       return 1;
3121     }
3122     t = &cx->physical->link.stats.total;
3123   } else if (strcasecmp(arg->argv[arg->argn], "ipcp") == 0)
3124     t = &arg->bundle->ncp.ipcp.throughput;
3125 #ifndef NOINET6
3126   else if (strcasecmp(arg->argv[arg->argn], "ipv6cp") == 0)
3127     t = &arg->bundle->ncp.ipv6cp.throughput;
3128 #endif
3129   else
3130     return -1;
3131
3132   if (arg->argc > arg->argn + 1) {
3133     clear_type = 0;
3134     for (i = arg->argn + 1; i < arg->argc; i++)
3135       if (strcasecmp(arg->argv[i], "overall") == 0)
3136         clear_type |= THROUGHPUT_OVERALL;
3137       else if (strcasecmp(arg->argv[i], "current") == 0)
3138         clear_type |= THROUGHPUT_CURRENT;
3139       else if (strcasecmp(arg->argv[i], "peak") == 0)
3140         clear_type |= THROUGHPUT_PEAK;
3141       else
3142         return -1;
3143   } else
3144     clear_type = THROUGHPUT_ALL;
3145
3146   throughput_clear(t, clear_type, arg->prompt);
3147   return 0;
3148 }
3149
3150 static int
3151 RunListCommand(struct cmdargs const *arg)
3152 {
3153   const char *cmd = arg->argc ? arg->argv[arg->argc - 1] : "???";
3154
3155 #ifndef NONAT
3156   if (arg->cmd->args == NatCommands &&
3157       tolower(*arg->argv[arg->argn - 1]) == 'a') {
3158     if (arg->prompt)
3159       prompt_Printf(arg->prompt, "The alias command is deprecated\n");
3160     else
3161       log_Printf(LogWARN, "The alias command is deprecated\n");
3162   }
3163 #endif
3164
3165   if (arg->argc > arg->argn)
3166     FindExec(arg->bundle, arg->cmd->args, arg->argc, arg->argn, arg->argv,
3167              arg->prompt, arg->cx);
3168   else if (arg->prompt)
3169     prompt_Printf(arg->prompt, "Use `%s help' to get a list or `%s help"
3170                   " <option>' for syntax help.\n", cmd, cmd);
3171   else
3172     log_Printf(LogWARN, "%s command must have arguments\n", cmd);
3173
3174   return 0;
3175 }
3176
3177 static int
3178 IfaceAddCommand(struct cmdargs const *arg)
3179 {
3180   struct ncpaddr peer, addr;
3181   struct ncprange ifa;
3182   struct in_addr mask;
3183   int n, how;
3184
3185   if (arg->argc == arg->argn + 1) {
3186     if (!ncprange_aton(&ifa, NULL, arg->argv[arg->argn]))
3187       return -1;
3188     ncpaddr_init(&peer);
3189   } else {
3190     if (arg->argc == arg->argn + 2) {
3191       if (!ncprange_aton(&ifa, NULL, arg->argv[arg->argn]))
3192         return -1;
3193       n = 1;
3194     } else if (arg->argc == arg->argn + 3) {
3195       if (!ncpaddr_aton(&addr, NULL, arg->argv[arg->argn]))
3196         return -1;
3197       if (ncpaddr_family(&addr) != AF_INET)
3198         return -1;
3199       ncprange_sethost(&ifa, &addr);
3200       if (!ncpaddr_aton(&addr, NULL, arg->argv[arg->argn + 1]))
3201         return -1;
3202       if (!ncpaddr_getip4(&addr, &mask))
3203         return -1;
3204       if (!ncprange_setip4mask(&ifa, mask))
3205         return -1;
3206       n = 2;
3207     } else
3208       return -1;
3209
3210     if (!ncpaddr_aton(&peer, NULL, arg->argv[arg->argn + n]))
3211       return -1;
3212
3213     if (ncprange_family(&ifa) != ncpaddr_family(&peer)) {
3214       log_Printf(LogWARN, "IfaceAddCommand: src and dst address families"
3215                  " differ\n");
3216       return -1;
3217     }
3218   }
3219
3220   how = IFACE_ADD_LAST;
3221   if (arg->cmd->args)
3222     how |= IFACE_FORCE_ADD;
3223
3224   return !iface_Add(arg->bundle->iface, &arg->bundle->ncp, &ifa, &peer, how);
3225 }
3226
3227 static int
3228 IfaceDeleteCommand(struct cmdargs const *arg)
3229 {
3230   struct ncpaddr ifa;
3231   struct in_addr ifa4;
3232   int ok;
3233
3234   if (arg->argc != arg->argn + 1)
3235     return -1;
3236
3237   if (!ncpaddr_aton(&ifa, NULL, arg->argv[arg->argn]))
3238     return -1;
3239
3240   if (arg->bundle->ncp.ipcp.fsm.state == ST_OPENED &&
3241       ncpaddr_getip4(&ifa, &ifa4) &&
3242       arg->bundle->ncp.ipcp.my_ip.s_addr == ifa4.s_addr) {
3243     log_Printf(LogWARN, "%s: Cannot remove active interface address\n",
3244                ncpaddr_ntoa(&ifa));
3245     return 1;
3246   }
3247
3248   ok = iface_Delete(arg->bundle->iface, &arg->bundle->ncp, &ifa);
3249   if (!ok) {
3250     if (arg->cmd->args)
3251       ok = 1;
3252     else if (arg->prompt)
3253       prompt_Printf(arg->prompt, "%s: No such interface address\n",
3254                     ncpaddr_ntoa(&ifa));
3255     else
3256       log_Printf(LogWARN, "%s: No such interface address\n",
3257                  ncpaddr_ntoa(&ifa));
3258   }
3259
3260   return !ok;
3261 }
3262
3263 static int
3264 IfaceClearCommand(struct cmdargs const *arg)
3265 {
3266   int family, how;
3267
3268   family = 0;
3269   if (arg->argc == arg->argn + 1) {
3270     if (strcasecmp(arg->argv[arg->argn], "inet") == 0)
3271       family = AF_INET;
3272 #ifndef NOINET6
3273     else if (strcasecmp(arg->argv[arg->argn], "inet6") == 0)
3274       family = AF_INET6;
3275 #endif
3276     else
3277       return -1;
3278   } else if (arg->argc != arg->argn)
3279     return -1;
3280
3281   how = arg->bundle->ncp.ipcp.fsm.state == ST_OPENED ||
3282         arg->bundle->phys_type.all & PHYS_AUTO ?
3283         IFACE_CLEAR_ALIASES : IFACE_CLEAR_ALL;
3284   iface_Clear(arg->bundle->iface, &arg->bundle->ncp, family, how);
3285
3286   return 0;
3287 }
3288
3289 static int
3290 SetProcTitle(struct cmdargs const *arg)
3291 {
3292   static char title[LINE_LEN];
3293   char *argv[MAXARGS];
3294   int argc = arg->argc - arg->argn;
3295
3296   if (arg->argc <= arg->argn) {
3297     SetTitle(NULL);
3298     return 0;
3299   }
3300
3301   if ((unsigned)argc >= sizeof argv / sizeof argv[0]) {
3302     argc = sizeof argv / sizeof argv[0] - 1;
3303     log_Printf(LogWARN, "Truncating proc title to %d args\n", argc);
3304   }
3305   command_Expand(argv, argc, arg->argv + arg->argn, arg->bundle, 1, getpid());
3306   Concatinate(title, sizeof title, argc, (const char *const *)argv);
3307   SetTitle(title);
3308   command_Free(argc, argv);
3309
3310   return 0;
3311 }