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