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