xref: /freebsd/usr.sbin/ppp/command.c (revision 7d8f797b725e3efc0a4256554654780df83c456c)
1 /*-
2  * Copyright (c) 1996 - 2001 Brian Somers <brian@Awfulhak.org>
3  *          based on work by Toshiharu OHNO <tony-o@iij.ad.jp>
4  *                           Internet Initiative Japan, Inc (IIJ)
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #include <sys/param.h>
32 #include <netinet/in_systm.h>
33 #include <netinet/in.h>
34 #include <netinet/ip.h>
35 #include <arpa/inet.h>
36 #include <sys/socket.h>
37 #include <net/route.h>
38 #include <netdb.h>
39 #include <sys/un.h>
40 
41 #include <ctype.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <paths.h>
45 #include <stdarg.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <sys/wait.h>
50 #include <termios.h>
51 #include <unistd.h>
52 
53 #ifndef NONAT
54 #ifdef LOCALNAT
55 #include "alias.h"
56 #else
57 #include <alias.h>
58 #endif
59 #endif
60 
61 #include "layer.h"
62 #include "defs.h"
63 #include "command.h"
64 #include "mbuf.h"
65 #include "log.h"
66 #include "timer.h"
67 #include "fsm.h"
68 #include "iplist.h"
69 #include "throughput.h"
70 #include "slcompress.h"
71 #include "lqr.h"
72 #include "hdlc.h"
73 #include "lcp.h"
74 #include "ncpaddr.h"
75 #include "ipcp.h"
76 #ifndef NONAT
77 #include "nat_cmd.h"
78 #endif
79 #include "systems.h"
80 #include "filter.h"
81 #include "descriptor.h"
82 #include "main.h"
83 #include "route.h"
84 #include "ccp.h"
85 #include "auth.h"
86 #include "async.h"
87 #include "link.h"
88 #include "physical.h"
89 #include "mp.h"
90 #ifndef NORADIUS
91 #include "radius.h"
92 #endif
93 #include "ipv6cp.h"
94 #include "ncp.h"
95 #include "bundle.h"
96 #include "server.h"
97 #include "prompt.h"
98 #include "chat.h"
99 #include "chap.h"
100 #include "cbcp.h"
101 #include "datalink.h"
102 #include "iface.h"
103 #include "id.h"
104 #include "probe.h"
105 
106 /* ``set'' values */
107 #define	VAR_AUTHKEY	0
108 #define	VAR_DIAL	1
109 #define	VAR_LOGIN	2
110 #define	VAR_AUTHNAME	3
111 #define	VAR_AUTOLOAD	4
112 #define	VAR_WINSIZE	5
113 #define	VAR_DEVICE	6
114 #define	VAR_ACCMAP	7
115 #define	VAR_MRRU	8
116 #define	VAR_MRU		9
117 #define	VAR_MTU		10
118 #define	VAR_OPENMODE	11
119 #define	VAR_PHONE	12
120 #define	VAR_HANGUP	13
121 #define	VAR_IDLETIMEOUT	14
122 #define	VAR_LQRPERIOD	15
123 #define	VAR_LCPRETRY	16
124 #define	VAR_CHAPRETRY	17
125 #define	VAR_PAPRETRY	18
126 #define	VAR_CCPRETRY	19
127 #define	VAR_IPCPRETRY	20
128 #define	VAR_DNS		21
129 #define	VAR_NBNS	22
130 #define	VAR_MODE	23
131 #define	VAR_CALLBACK	24
132 #define	VAR_CBCP	25
133 #define	VAR_CHOKED	26
134 #define	VAR_SENDPIPE	27
135 #define	VAR_RECVPIPE	28
136 #define	VAR_RADIUS	29
137 #define	VAR_CD		30
138 #define	VAR_PARITY	31
139 #define VAR_CRTSCTS	32
140 #define VAR_URGENTPORTS	33
141 #define	VAR_LOGOUT	34
142 #define	VAR_IFQUEUE	35
143 #define	VAR_MPPE	36
144 #define	VAR_IPV6CPRETRY	37
145 #define	VAR_RAD_ALIVE	38
146 #define	VAR_PPPOE	39
147 #define	VAR_PORT_ID	40
148 
149 /* ``accept|deny|disable|enable'' masks */
150 #define NEG_HISMASK (1)
151 #define NEG_MYMASK (2)
152 
153 /* ``accept|deny|disable|enable'' values */
154 #define NEG_ACFCOMP	40
155 #define NEG_CHAP05	41
156 #define NEG_CHAP80	42
157 #define NEG_CHAP80LM	43
158 #define NEG_DEFLATE	44
159 #define NEG_DNS		45
160 #define NEG_ECHO	46
161 #define NEG_ENDDISC	47
162 #define NEG_LQR		48
163 #define NEG_PAP		49
164 #define NEG_PPPDDEFLATE	50
165 #define NEG_PRED1	51
166 #define NEG_PROTOCOMP	52
167 #define NEG_SHORTSEQ	53
168 #define NEG_VJCOMP	54
169 #define NEG_MPPE	55
170 #define NEG_CHAP81	56
171 
172 const char Version[] = "3.4.2";
173 
174 static int ShowCommand(struct cmdargs const *);
175 static int TerminalCommand(struct cmdargs const *);
176 static int QuitCommand(struct cmdargs const *);
177 static int OpenCommand(struct cmdargs const *);
178 static int CloseCommand(struct cmdargs const *);
179 static int DownCommand(struct cmdargs const *);
180 static int SetCommand(struct cmdargs const *);
181 static int LinkCommand(struct cmdargs const *);
182 static int AddCommand(struct cmdargs const *);
183 static int DeleteCommand(struct cmdargs const *);
184 static int NegotiateCommand(struct cmdargs const *);
185 static int ClearCommand(struct cmdargs const *);
186 static int RunListCommand(struct cmdargs const *);
187 static int 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")) == 0)
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_URGENTPORTS:
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 {
2293       ncp_SetUrgentTOS(&arg->bundle->ncp);
2294       first = arg->argn;
2295       if (!strcasecmp(arg->argv[first], "tcp") && ++first == arg->argc)
2296         ncp_ClearUrgentTcpPorts(&arg->bundle->ncp);
2297 
2298       for (f = first; f < arg->argc; f++)
2299         if (*arg->argv[f] == '+')
2300           ncp_AddUrgentTcpPort(&arg->bundle->ncp, atoi(arg->argv[f] + 1));
2301         else if (*arg->argv[f] == '-')
2302           ncp_RemoveUrgentTcpPort(&arg->bundle->ncp, atoi(arg->argv[f] + 1));
2303         else {
2304           if (f == first)
2305             ncp_ClearUrgentTcpPorts(&arg->bundle->ncp);
2306           ncp_AddUrgentTcpPort(&arg->bundle->ncp, atoi(arg->argv[f]));
2307         }
2308     }
2309     break;
2310 
2311   case VAR_PPPOE:
2312     if (strcasecmp(argp, "3Com") == 0)
2313       physical_SetPPPoEnonstandard(arg->cx->physical, 1);
2314     else if (strcasecmp(argp, "standard") == 0)
2315       physical_SetPPPoEnonstandard(arg->cx->physical, 0);
2316     else {
2317       log_Printf(LogWARN, "PPPoE standard value must be \"standard\" or \"3Com\"\n");
2318       res = 1;
2319     }
2320     break;
2321 
2322 #ifndef NORADIUS
2323   case VAR_PORT_ID:
2324     if (strcasecmp(argp, "default") == 0)
2325 	    arg->bundle->radius.port_id_type = RPI_DEFAULT;
2326     else if (strcasecmp(argp, "pid") == 0)
2327 	    arg->bundle->radius.port_id_type = RPI_PID;
2328     else if (strcasecmp(argp, "ifnum") == 0)
2329 	    arg->bundle->radius.port_id_type = RPI_IFNUM;
2330     else if (strcasecmp(argp, "tunnum") == 0)
2331 	    arg->bundle->radius.port_id_type = RPI_TUNNUM;
2332     else {
2333 	   log_Printf(LogWARN,
2334 		"RADIUS port id must be one of \"default\", \"pid\", \"ifnum\" or \"tunnum\"\n");
2335 	   res = 1;
2336     }
2337 
2338     if (arg->bundle->radius.port_id_type && !*arg->bundle->radius.cfg.file) {
2339 	    log_Printf(LogWARN, "rad_port_id requires radius to be configured\n");
2340 	    res = 1;
2341     }
2342 
2343     break;
2344 #endif
2345   }
2346 
2347   return res;
2348 }
2349 
2350 static struct cmdtab const SetCommands[] = {
2351   {"accmap", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX,
2352   "accmap value", "set accmap hex-value", (const void *)VAR_ACCMAP},
2353   {"authkey", "key", SetVariable, LOCAL_AUTH,
2354   "authentication key", "set authkey|key key", (const void *)VAR_AUTHKEY},
2355   {"authname", NULL, SetVariable, LOCAL_AUTH,
2356   "authentication name", "set authname name", (const void *)VAR_AUTHNAME},
2357   {"autoload", NULL, SetVariable, LOCAL_AUTH,
2358   "auto link [de]activation", "set autoload maxtime maxload mintime minload",
2359   (const void *)VAR_AUTOLOAD},
2360   {"bandwidth", NULL, mp_SetDatalinkBandwidth, LOCAL_AUTH | LOCAL_CX,
2361   "datalink bandwidth", "set bandwidth value", NULL},
2362   {"callback", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX,
2363   "callback control", "set callback [none|auth|cbcp|"
2364   "E.164 *|number[,number]...]...", (const void *)VAR_CALLBACK},
2365   {"cbcp", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX,
2366   "CBCP control", "set cbcp [*|phone[,phone...] [delay [timeout]]]",
2367   (const void *)VAR_CBCP},
2368   {"ccpretry", "ccpretries", SetVariable, LOCAL_AUTH | LOCAL_CX_OPT,
2369    "CCP retries", "set ccpretry value [attempts]", (const void *)VAR_CCPRETRY},
2370   {"cd", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX, "Carrier delay requirement",
2371    "set cd value[!]", (const void *)VAR_CD},
2372   {"chapretry", "chapretries", SetVariable, LOCAL_AUTH | LOCAL_CX,
2373    "CHAP retries", "set chapretry value [attempts]",
2374    (const void *)VAR_CHAPRETRY},
2375   {"choked", NULL, SetVariable, LOCAL_AUTH,
2376   "choked timeout", "set choked [secs]", (const void *)VAR_CHOKED},
2377   {"ctsrts", "crtscts", SetVariable, LOCAL_AUTH | LOCAL_CX,
2378    "Use hardware flow control", "set ctsrts [on|off]",
2379    (const char *)VAR_CRTSCTS},
2380   {"deflate", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX_OPT,
2381   "deflate window sizes", "set deflate out-winsize in-winsize",
2382   (const void *) VAR_WINSIZE},
2383 #ifndef NODES
2384   {"mppe", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX_OPT,
2385   "MPPE key size and state", "set mppe [40|56|128|* [stateful|stateless|*]]",
2386   (const void *) VAR_MPPE},
2387 #endif
2388   {"device", "line", SetVariable, LOCAL_AUTH | LOCAL_CX,
2389   "physical device name", "set device|line device-name[,device-name]",
2390   (const void *) VAR_DEVICE},
2391   {"dial", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX,
2392   "dialing script", "set dial chat-script", (const void *) VAR_DIAL},
2393   {"dns", NULL, SetVariable, LOCAL_AUTH, "Domain Name Server",
2394   "set dns pri-addr [sec-addr]", (const void *)VAR_DNS},
2395   {"enddisc", NULL, mp_SetEnddisc, LOCAL_AUTH,
2396   "Endpoint Discriminator", "set enddisc [IP|magic|label|psn value]", NULL},
2397   {"escape", NULL, SetEscape, LOCAL_AUTH | LOCAL_CX,
2398   "escape characters", "set escape hex-digit ...", NULL},
2399   {"filter", NULL, filter_Set, LOCAL_AUTH,
2400   "packet filters", "set filter alive|dial|in|out rule-no permit|deny "
2401   "[src_addr[/width]] [dst_addr[/width]] [proto "
2402   "[src [lt|eq|gt port]] [dst [lt|eq|gt port]] [estab] [syn] [finrst]]", NULL},
2403   {"hangup", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX,
2404   "hangup script", "set hangup chat-script", (const void *) VAR_HANGUP},
2405   {"ifaddr", NULL, SetInterfaceAddr, LOCAL_AUTH, "destination address",
2406   "set ifaddr [src-addr [dst-addr [netmask [trg-addr]]]]", NULL},
2407   {"ifqueue", NULL, SetVariable, LOCAL_AUTH, "interface queue",
2408   "set ifqueue packets", (const void *)VAR_IFQUEUE},
2409   {"ipcpretry", "ipcpretries", SetVariable, LOCAL_AUTH, "IPCP retries",
2410    "set ipcpretry value [attempts]", (const void *)VAR_IPCPRETRY},
2411   {"ipv6cpretry", "ipv6cpretries", SetVariable, LOCAL_AUTH, "IPV6CP retries",
2412    "set ipv6cpretry value [attempts]", (const void *)VAR_IPV6CPRETRY},
2413   {"lcpretry", "lcpretries", SetVariable, LOCAL_AUTH | LOCAL_CX, "LCP retries",
2414    "set lcpretry value [attempts]", (const void *)VAR_LCPRETRY},
2415   {"log", NULL, log_SetLevel, LOCAL_AUTH, "log level",
2416   "set log [local] [+|-]all|async|cbcp|ccp|chat|command|connect|debug|dns|hdlc|"
2417   "id0|ipcp|lcp|lqm|phase|physical|radius|sync|tcp/ip|timer|tun...", NULL},
2418   {"login", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX,
2419   "login script", "set login chat-script", (const void *) VAR_LOGIN},
2420   {"logout", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX,
2421   "logout script", "set logout chat-script", (const void *) VAR_LOGOUT},
2422   {"lqrperiod", "echoperiod", SetVariable, LOCAL_AUTH | LOCAL_CX_OPT,
2423   "LQR period", "set lqr/echo period value", (const void *)VAR_LQRPERIOD},
2424   {"mode", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX, "mode value",
2425   "set mode interactive|auto|ddial|background", (const void *)VAR_MODE},
2426   {"mrru", NULL, SetVariable, LOCAL_AUTH, "MRRU value",
2427   "set mrru value", (const void *)VAR_MRRU},
2428   {"mru", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX,
2429   "MRU value", "set mru [max[imum]] [value]", (const void *)VAR_MRU},
2430   {"mtu", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX,
2431   "interface MTU value", "set mtu [max[imum]] [value]", (const void *)VAR_MTU},
2432   {"nbns", NULL, SetVariable, LOCAL_AUTH, "NetBIOS Name Server",
2433   "set nbns pri-addr [sec-addr]", (const void *)VAR_NBNS},
2434   {"openmode", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX, "open mode",
2435   "set openmode active|passive [secs]", (const void *)VAR_OPENMODE},
2436   {"papretry", "papretries", SetVariable, LOCAL_AUTH | LOCAL_CX, "PAP retries",
2437    "set papretry value [attempts]", (const void *)VAR_PAPRETRY},
2438   {"parity", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX, "serial parity",
2439    "set parity [odd|even|none]", (const void *)VAR_PARITY},
2440   {"phone", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX, "telephone number(s)",
2441   "set phone phone1[:phone2[...]]", (const void *)VAR_PHONE},
2442   {"proctitle", "title", SetProcTitle, LOCAL_AUTH,
2443   "Process title", "set proctitle [value]", NULL},
2444 #ifndef NORADIUS
2445   {"radius", NULL, SetVariable, LOCAL_AUTH,
2446   "RADIUS Config", "set radius cfgfile", (const void *)VAR_RADIUS},
2447   {"rad_alive", NULL, SetVariable, LOCAL_AUTH,
2448   "Raduis alive interval", "set rad_alive value",
2449   (const void *)VAR_RAD_ALIVE},
2450   {"rad_port_id", NULL, SetVariable, LOCAL_AUTH,
2451   "NAS-Port-Id", "set rad_port_id [default|pid|ifnum|tunnum]", (const void *)VAR_PORT_ID},
2452 #endif
2453   {"reconnect", NULL, datalink_SetReconnect, LOCAL_AUTH | LOCAL_CX,
2454   "Reconnect timeout", "set reconnect value ntries", NULL},
2455   {"recvpipe", NULL, SetVariable, LOCAL_AUTH,
2456   "RECVPIPE value", "set recvpipe value", (const void *)VAR_RECVPIPE},
2457   {"redial", NULL, datalink_SetRedial, LOCAL_AUTH | LOCAL_CX,
2458   "Redial timeout", "set redial secs[+inc[-incmax]][.next] [attempts]", NULL},
2459   {"sendpipe", NULL, SetVariable, LOCAL_AUTH,
2460   "SENDPIPE value", "set sendpipe value", (const void *)VAR_SENDPIPE},
2461   {"server", "socket", SetServer, LOCAL_AUTH, "diagnostic port",
2462   "set server|socket TcpPort|LocalName|none|open|closed [password [mask]]",
2463   NULL},
2464   {"speed", NULL, SetModemSpeed, LOCAL_AUTH | LOCAL_CX,
2465   "physical speed", "set speed value|sync", NULL},
2466   {"stopped", NULL, SetStoppedTimeout, LOCAL_AUTH | LOCAL_CX,
2467   "STOPPED timeouts", "set stopped [LCPseconds [CCPseconds]]", NULL},
2468   {"timeout", NULL, SetVariable, LOCAL_AUTH, "Idle timeout",
2469   "set timeout idletime", (const void *)VAR_IDLETIMEOUT},
2470   {"urgent", NULL, SetVariable, LOCAL_AUTH, "urgent ports",
2471   "set urgent [tcp|udp] [+|-]port...", (const void *)VAR_URGENTPORTS},
2472   {"vj", NULL, ipcp_vjset, LOCAL_AUTH,
2473   "vj values", "set vj slots|slotcomp [value]", NULL},
2474   {"help", "?", HelpCommand, LOCAL_AUTH | LOCAL_NO_AUTH,
2475   "Display this message", "set help|? [command]", SetCommands},
2476   {"pppoe", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX,
2477    "Connect using standard/3Com mode", "set pppoe [standard|3Com]",
2478    (const char *)VAR_PPPOE},
2479   {NULL, NULL, NULL, 0, NULL, NULL, NULL},
2480 };
2481 
2482 static int
2483 SetCommand(struct cmdargs const *arg)
2484 {
2485   if (arg->argc > arg->argn)
2486     FindExec(arg->bundle, SetCommands, arg->argc, arg->argn, arg->argv,
2487              arg->prompt, arg->cx);
2488   else if (arg->prompt)
2489     prompt_Printf(arg->prompt, "Use `set ?' to get a list or `set ? <var>' for"
2490 	          " syntax help.\n");
2491   else
2492     log_Printf(LogWARN, "set command must have arguments\n");
2493 
2494   return 0;
2495 }
2496 
2497 static int
2498 AddCommand(struct cmdargs const *arg)
2499 {
2500   struct ncpaddr gw;
2501   struct ncprange dest;
2502   struct in_addr host;
2503 #ifndef NOINET6
2504   struct in6_addr host6;
2505 #endif
2506   int dest_default, gw_arg, addrs;
2507 
2508   if (arg->argc != arg->argn+3 && arg->argc != arg->argn+2)
2509     return -1;
2510 
2511   addrs = 0;
2512   dest_default = 0;
2513   if (arg->argc == arg->argn + 2) {
2514     if (!strcasecmp(arg->argv[arg->argn], "default"))
2515       dest_default = 1;
2516     else {
2517       if (!ncprange_aton(&dest, &arg->bundle->ncp, arg->argv[arg->argn]))
2518         return -1;
2519       if (!strncasecmp(arg->argv[arg->argn], "MYADDR", 6))
2520         addrs = ROUTE_DSTMYADDR;
2521       else if (!strncasecmp(arg->argv[arg->argn], "MYADDR6", 7))
2522         addrs = ROUTE_DSTMYADDR6;
2523       else if (!strncasecmp(arg->argv[arg->argn], "HISADDR", 7))
2524         addrs = ROUTE_DSTHISADDR;
2525       else if (!strncasecmp(arg->argv[arg->argn], "HISADDR6", 8))
2526         addrs = ROUTE_DSTHISADDR6;
2527       else if (!strncasecmp(arg->argv[arg->argn], "DNS0", 4))
2528         addrs = ROUTE_DSTDNS0;
2529       else if (!strncasecmp(arg->argv[arg->argn], "DNS1", 4))
2530         addrs = ROUTE_DSTDNS1;
2531     }
2532     gw_arg = 1;
2533   } else {
2534     if (strcasecmp(arg->argv[arg->argn], "MYADDR") == 0) {
2535       addrs = ROUTE_DSTMYADDR;
2536       host = arg->bundle->ncp.ipcp.my_ip;
2537     } else if (strcasecmp(arg->argv[arg->argn], "HISADDR") == 0) {
2538       addrs = ROUTE_DSTHISADDR;
2539       host = arg->bundle->ncp.ipcp.peer_ip;
2540     } else if (strcasecmp(arg->argv[arg->argn], "DNS0") == 0) {
2541       addrs = ROUTE_DSTDNS0;
2542       host = arg->bundle->ncp.ipcp.ns.dns[0];
2543     } else if (strcasecmp(arg->argv[arg->argn], "DNS1") == 0) {
2544       addrs = ROUTE_DSTDNS1;
2545       host = arg->bundle->ncp.ipcp.ns.dns[1];
2546     } else {
2547       host = GetIpAddr(arg->argv[arg->argn]);
2548       if (host.s_addr == INADDR_NONE) {
2549         log_Printf(LogWARN, "%s: Invalid destination address\n",
2550                    arg->argv[arg->argn]);
2551         return -1;
2552       }
2553     }
2554     ncprange_setip4(&dest, host, GetIpAddr(arg->argv[arg->argn + 1]));
2555     gw_arg = 2;
2556   }
2557 
2558   if (strcasecmp(arg->argv[arg->argn + gw_arg], "HISADDR") == 0) {
2559     ncpaddr_setip4(&gw, arg->bundle->ncp.ipcp.peer_ip);
2560     addrs |= ROUTE_GWHISADDR;
2561 #ifndef NOINET6
2562   } else if (strcasecmp(arg->argv[arg->argn + gw_arg], "HISADDR6") == 0) {
2563     if (!ncpaddr_getip6(&arg->bundle->ncp.ipv6cp.hisaddr, &host6))
2564       memset(&host6, '\0', sizeof host6);
2565     ncpaddr_setip6(&gw, &host6);
2566     addrs |= ROUTE_GWHISADDR6;
2567 #endif
2568   } else {
2569     if (!ncpaddr_aton(&gw, &arg->bundle->ncp, arg->argv[arg->argn + gw_arg])) {
2570       log_Printf(LogWARN, "%s: Invalid gateway address\n",
2571                  arg->argv[arg->argn + gw_arg]);
2572       return -1;
2573     }
2574   }
2575 
2576   if (dest_default)
2577     ncprange_setdefault(&dest, ncpaddr_family(&gw));
2578 
2579   if (rt_Set(arg->bundle, RTM_ADD, &dest, &gw, arg->cmd->args ? 1 : 0,
2580              ((addrs & ROUTE_GWHISADDR) || (addrs & ROUTE_GWHISADDR6)) ? 1 : 0)
2581       && addrs != ROUTE_STATIC)
2582     route_Add(&arg->bundle->ncp.route, addrs, &dest, &gw);
2583 
2584   return 0;
2585 }
2586 
2587 static int
2588 DeleteCommand(struct cmdargs const *arg)
2589 {
2590   struct ncprange dest;
2591   int addrs;
2592 
2593   if (arg->argc == arg->argn+1) {
2594     if(strcasecmp(arg->argv[arg->argn], "all") == 0) {
2595       route_IfDelete(arg->bundle, 0);
2596       route_DeleteAll(&arg->bundle->ncp.route);
2597     } else {
2598       addrs = 0;
2599       if (strcasecmp(arg->argv[arg->argn], "MYADDR") == 0) {
2600         ncprange_setip4host(&dest, arg->bundle->ncp.ipcp.my_ip);
2601         addrs = ROUTE_DSTMYADDR;
2602 #ifndef NOINET6
2603       } else if (strcasecmp(arg->argv[arg->argn], "MYADDR6") == 0) {
2604         ncprange_sethost(&dest, &arg->bundle->ncp.ipv6cp.myaddr);
2605         addrs = ROUTE_DSTMYADDR6;
2606 #endif
2607       } else if (strcasecmp(arg->argv[arg->argn], "HISADDR") == 0) {
2608         ncprange_setip4host(&dest, arg->bundle->ncp.ipcp.peer_ip);
2609         addrs = ROUTE_DSTHISADDR;
2610 #ifndef NOINET6
2611       } else if (strcasecmp(arg->argv[arg->argn], "HISADDR6") == 0) {
2612         ncprange_sethost(&dest, &arg->bundle->ncp.ipv6cp.hisaddr);
2613         addrs = ROUTE_DSTHISADDR6;
2614 #endif
2615       } else if (strcasecmp(arg->argv[arg->argn], "DNS0") == 0) {
2616         ncprange_setip4host(&dest, arg->bundle->ncp.ipcp.ns.dns[0]);
2617         addrs = ROUTE_DSTDNS0;
2618       } else if (strcasecmp(arg->argv[arg->argn], "DNS1") == 0) {
2619         ncprange_setip4host(&dest, arg->bundle->ncp.ipcp.ns.dns[1]);
2620         addrs = ROUTE_DSTDNS1;
2621       } else {
2622         ncprange_aton(&dest, &arg->bundle->ncp, arg->argv[arg->argn]);
2623         addrs = ROUTE_STATIC;
2624       }
2625       rt_Set(arg->bundle, RTM_DELETE, &dest, NULL, arg->cmd->args ? 1 : 0, 0);
2626       route_Delete(&arg->bundle->ncp.route, addrs, &dest);
2627     }
2628   } else
2629     return -1;
2630 
2631   return 0;
2632 }
2633 
2634 #ifndef NONAT
2635 static int
2636 NatEnable(struct cmdargs const *arg)
2637 {
2638   if (arg->argc == arg->argn+1) {
2639     if (strcasecmp(arg->argv[arg->argn], "yes") == 0) {
2640       if (!arg->bundle->NatEnabled) {
2641         if (arg->bundle->ncp.ipcp.fsm.state == ST_OPENED)
2642           LibAliasSetAddress(la, arg->bundle->ncp.ipcp.my_ip);
2643         arg->bundle->NatEnabled = 1;
2644       }
2645       return 0;
2646     } else if (strcasecmp(arg->argv[arg->argn], "no") == 0) {
2647       arg->bundle->NatEnabled = 0;
2648       opt_disable(arg->bundle, OPT_IFACEALIAS);
2649       /* Don't iface_Clear() - there may be manually configured addresses */
2650       return 0;
2651     }
2652   }
2653 
2654   return -1;
2655 }
2656 
2657 
2658 static int
2659 NatOption(struct cmdargs const *arg)
2660 {
2661   long param = (long)arg->cmd->args;
2662 
2663   if (arg->argc == arg->argn+1) {
2664     if (strcasecmp(arg->argv[arg->argn], "yes") == 0) {
2665       if (arg->bundle->NatEnabled) {
2666 	LibAliasSetMode(la, param, param);
2667 	return 0;
2668       }
2669       log_Printf(LogWARN, "nat not enabled\n");
2670     } else if (strcmp(arg->argv[arg->argn], "no") == 0) {
2671       if (arg->bundle->NatEnabled) {
2672 	LibAliasSetMode(la, 0, param);
2673 	return 0;
2674       }
2675       log_Printf(LogWARN, "nat not enabled\n");
2676     }
2677   }
2678   return -1;
2679 }
2680 #endif /* #ifndef NONAT */
2681 
2682 static int
2683 LinkCommand(struct cmdargs const *arg)
2684 {
2685   if (arg->argc > arg->argn+1) {
2686     char namelist[LINE_LEN];
2687     struct datalink *cx;
2688     char *name;
2689     int result = 0;
2690 
2691     if (!strcmp(arg->argv[arg->argn], "*")) {
2692       struct datalink *dl;
2693 
2694       cx = arg->bundle->links;
2695       while (cx) {
2696         /* Watch it, the command could be a ``remove'' */
2697         dl = cx->next;
2698         FindExec(arg->bundle, Commands, arg->argc, arg->argn+1, arg->argv,
2699                  arg->prompt, cx);
2700         for (cx = arg->bundle->links; cx; cx = cx->next)
2701           if (cx == dl)
2702             break;		/* Pointer's still valid ! */
2703       }
2704     } else {
2705       strncpy(namelist, arg->argv[arg->argn], sizeof namelist - 1);
2706       namelist[sizeof namelist - 1] = '\0';
2707       for(name = strtok(namelist, ", "); name; name = strtok(NULL,", "))
2708         if (!bundle2datalink(arg->bundle, name)) {
2709           log_Printf(LogWARN, "link: %s: Invalid link name\n", name);
2710           return 1;
2711         }
2712 
2713       strncpy(namelist, arg->argv[arg->argn], sizeof namelist - 1);
2714       namelist[sizeof namelist - 1] = '\0';
2715       for(name = strtok(namelist, ", "); name; name = strtok(NULL,", ")) {
2716         cx = bundle2datalink(arg->bundle, name);
2717         if (cx)
2718           FindExec(arg->bundle, Commands, arg->argc, arg->argn+1, arg->argv,
2719                    arg->prompt, cx);
2720         else {
2721           log_Printf(LogWARN, "link: %s: Invalidated link name !\n", name);
2722           result++;
2723         }
2724       }
2725     }
2726     return result;
2727   }
2728 
2729   log_Printf(LogWARN, "usage: %s\n", arg->cmd->syntax);
2730   return 2;
2731 }
2732 
2733 struct link *
2734 command_ChooseLink(struct cmdargs const *arg)
2735 {
2736   if (arg->cx)
2737     return &arg->cx->physical->link;
2738   else if (!arg->bundle->ncp.mp.cfg.mrru) {
2739     struct datalink *dl = bundle2datalink(arg->bundle, NULL);
2740     if (dl)
2741       return &dl->physical->link;
2742   }
2743   return &arg->bundle->ncp.mp.link;
2744 }
2745 
2746 static const char *
2747 ident_cmd(const char *cmd, unsigned *keep, unsigned *add)
2748 {
2749   const char *result;
2750 
2751   switch (*cmd) {
2752     case 'A':
2753     case 'a':
2754       result = "accept";
2755       *keep = NEG_MYMASK;
2756       *add = NEG_ACCEPTED;
2757       break;
2758     case 'D':
2759     case 'd':
2760       switch (cmd[1]) {
2761         case 'E':
2762         case 'e':
2763           result = "deny";
2764           *keep = NEG_MYMASK;
2765           *add = 0;
2766           break;
2767         case 'I':
2768         case 'i':
2769           result = "disable";
2770           *keep = NEG_HISMASK;
2771           *add = 0;
2772           break;
2773         default:
2774           return NULL;
2775       }
2776       break;
2777     case 'E':
2778     case 'e':
2779       result = "enable";
2780       *keep = NEG_HISMASK;
2781       *add = NEG_ENABLED;
2782       break;
2783     default:
2784       return NULL;
2785   }
2786 
2787   return result;
2788 }
2789 
2790 static int
2791 OptSet(struct cmdargs const *arg)
2792 {
2793   int opt = (int)(long)arg->cmd->args;
2794   unsigned keep;			/* Keep this opt */
2795   unsigned add;				/* Add this opt */
2796 
2797   if (ident_cmd(arg->argv[arg->argn - 2], &keep, &add) == NULL)
2798     return 1;
2799 
2800 #ifndef NOINET6
2801   if (add == NEG_ENABLED && opt == OPT_IPV6CP && !probe.ipv6_available) {
2802     log_Printf(LogWARN, "IPv6 is not available on this machine\n");
2803     return 1;
2804   }
2805 #endif
2806   if (!add && ((opt == OPT_NAS_IP_ADDRESS &&
2807                 !Enabled(arg->bundle, OPT_NAS_IDENTIFIER)) ||
2808                (opt == OPT_NAS_IDENTIFIER &&
2809                 !Enabled(arg->bundle, OPT_NAS_IP_ADDRESS)))) {
2810     log_Printf(LogWARN,
2811                "Cannot disable both NAS-IP-Address and NAS-Identifier\n");
2812     return 1;
2813   }
2814 
2815   if (add)
2816     opt_enable(arg->bundle, opt);
2817   else
2818     opt_disable(arg->bundle, opt);
2819 
2820   return 0;
2821 }
2822 
2823 static int
2824 IfaceAliasOptSet(struct cmdargs const *arg)
2825 {
2826   unsigned long long save = arg->bundle->cfg.optmask;
2827   int result = OptSet(arg);
2828 
2829   if (result == 0)
2830     if (Enabled(arg->bundle, OPT_IFACEALIAS) && !arg->bundle->NatEnabled) {
2831       arg->bundle->cfg.optmask = save;
2832       log_Printf(LogWARN, "Cannot enable iface-alias without NAT\n");
2833       result = 2;
2834     }
2835 
2836   return result;
2837 }
2838 
2839 static int
2840 NegotiateSet(struct cmdargs const *arg)
2841 {
2842   long param = (long)arg->cmd->args;
2843   struct link *l = command_ChooseLink(arg);	/* LOCAL_CX_OPT uses this */
2844   struct datalink *cx = arg->cx;	/* LOCAL_CX uses this */
2845   const char *cmd;
2846   unsigned keep;			/* Keep these bits */
2847   unsigned add;				/* Add these bits */
2848 
2849   if ((cmd = ident_cmd(arg->argv[arg->argn-2], &keep, &add)) == NULL)
2850     return 1;
2851 
2852   if ((arg->cmd->lauth & LOCAL_CX) && !cx) {
2853     log_Printf(LogWARN, "%s %s: No context (use the `link' command)\n",
2854               cmd, arg->cmd->name);
2855     return 2;
2856   } else if (cx && !(arg->cmd->lauth & (LOCAL_CX|LOCAL_CX_OPT))) {
2857     log_Printf(LogWARN, "%s %s: Redundant context (%s) ignored\n",
2858               cmd, arg->cmd->name, cx->name);
2859     cx = NULL;
2860   }
2861 
2862   switch (param) {
2863     case NEG_ACFCOMP:
2864       cx->physical->link.lcp.cfg.acfcomp &= keep;
2865       cx->physical->link.lcp.cfg.acfcomp |= add;
2866       break;
2867     case NEG_CHAP05:
2868       cx->physical->link.lcp.cfg.chap05 &= keep;
2869       cx->physical->link.lcp.cfg.chap05 |= add;
2870       break;
2871 #ifndef NODES
2872     case NEG_CHAP80:
2873       cx->physical->link.lcp.cfg.chap80nt &= keep;
2874       cx->physical->link.lcp.cfg.chap80nt |= add;
2875       break;
2876     case NEG_CHAP80LM:
2877       cx->physical->link.lcp.cfg.chap80lm &= keep;
2878       cx->physical->link.lcp.cfg.chap80lm |= add;
2879       break;
2880     case NEG_CHAP81:
2881       cx->physical->link.lcp.cfg.chap81 &= keep;
2882       cx->physical->link.lcp.cfg.chap81 |= add;
2883       break;
2884     case NEG_MPPE:
2885       l->ccp.cfg.neg[CCP_NEG_MPPE] &= keep;
2886       l->ccp.cfg.neg[CCP_NEG_MPPE] |= add;
2887       break;
2888 #endif
2889     case NEG_DEFLATE:
2890       l->ccp.cfg.neg[CCP_NEG_DEFLATE] &= keep;
2891       l->ccp.cfg.neg[CCP_NEG_DEFLATE] |= add;
2892       break;
2893     case NEG_DNS:
2894       arg->bundle->ncp.ipcp.cfg.ns.dns_neg &= keep;
2895       arg->bundle->ncp.ipcp.cfg.ns.dns_neg |= add;
2896       break;
2897     case NEG_ECHO:	/* probably misplaced in this function ! */
2898       if (cx->physical->link.lcp.cfg.echo && !add) {
2899         cx->physical->link.lcp.cfg.echo = 0;
2900         cx->physical->hdlc.lqm.method &= ~LQM_ECHO;
2901         if (cx->physical->hdlc.lqm.method & LQM_ECHO &&
2902             !cx->physical->link.lcp.want_lqrperiod &&
2903             cx->physical->hdlc.lqm.timer.load) {
2904           cx->physical->hdlc.lqm.timer.load = 0;
2905           lqr_StopTimer(cx->physical);
2906         }
2907       } else if (!cx->physical->link.lcp.cfg.echo && add) {
2908         cx->physical->link.lcp.cfg.echo = 1;
2909         cx->physical->hdlc.lqm.method |= LQM_ECHO;
2910         cx->physical->hdlc.lqm.timer.load =
2911 	    cx->physical->link.lcp.cfg.lqrperiod * SECTICKS;
2912         if (cx->physical->link.lcp.fsm.state == ST_OPENED)
2913           (*cx->physical->hdlc.lqm.timer.func)(&cx->physical->link.lcp);
2914       }
2915       break;
2916     case NEG_ENDDISC:
2917       arg->bundle->ncp.mp.cfg.negenddisc &= keep;
2918       arg->bundle->ncp.mp.cfg.negenddisc |= add;
2919       break;
2920     case NEG_LQR:
2921       cx->physical->link.lcp.cfg.lqr &= keep;
2922       cx->physical->link.lcp.cfg.lqr |= add;
2923       break;
2924     case NEG_PAP:
2925       cx->physical->link.lcp.cfg.pap &= keep;
2926       cx->physical->link.lcp.cfg.pap |= add;
2927       break;
2928     case NEG_PPPDDEFLATE:
2929       l->ccp.cfg.neg[CCP_NEG_DEFLATE24] &= keep;
2930       l->ccp.cfg.neg[CCP_NEG_DEFLATE24] |= add;
2931       break;
2932     case NEG_PRED1:
2933       l->ccp.cfg.neg[CCP_NEG_PRED1] &= keep;
2934       l->ccp.cfg.neg[CCP_NEG_PRED1] |= add;
2935       break;
2936     case NEG_PROTOCOMP:
2937       cx->physical->link.lcp.cfg.protocomp &= keep;
2938       cx->physical->link.lcp.cfg.protocomp |= add;
2939       break;
2940     case NEG_SHORTSEQ:
2941       switch (bundle_Phase(arg->bundle)) {
2942         case PHASE_DEAD:
2943           break;
2944         case PHASE_ESTABLISH:
2945           /* Make sure none of our links are DATALINK_LCP or greater */
2946           if (bundle_HighestState(arg->bundle) >= DATALINK_LCP) {
2947             log_Printf(LogWARN, "shortseq: Only changeable before"
2948                        " LCP negotiations\n");
2949             return 1;
2950           }
2951           break;
2952         default:
2953           log_Printf(LogWARN, "shortseq: Only changeable at phase"
2954                      " DEAD/ESTABLISH\n");
2955           return 1;
2956       }
2957       arg->bundle->ncp.mp.cfg.shortseq &= keep;
2958       arg->bundle->ncp.mp.cfg.shortseq |= add;
2959       break;
2960     case NEG_VJCOMP:
2961       arg->bundle->ncp.ipcp.cfg.vj.neg &= keep;
2962       arg->bundle->ncp.ipcp.cfg.vj.neg |= add;
2963       break;
2964   }
2965 
2966   return 0;
2967 }
2968 
2969 static struct cmdtab const NegotiateCommands[] = {
2970   {"echo", NULL, NegotiateSet, LOCAL_AUTH | LOCAL_CX, "Send echo requests",
2971   "disable|enable", (const void *)NEG_ECHO},
2972   {"filter-decapsulation", NULL, OptSet, LOCAL_AUTH,
2973   "filter on PPPoUDP payloads", "disable|enable",
2974   (const void *)OPT_FILTERDECAP},
2975   {"force-scripts", NULL, OptSet, LOCAL_AUTH,
2976    "Force execution of the configured chat scripts", "disable|enable",
2977    (const void *)OPT_FORCE_SCRIPTS},
2978   {"idcheck", NULL, OptSet, LOCAL_AUTH, "Check FSM reply ids",
2979   "disable|enable", (const void *)OPT_IDCHECK},
2980   {"iface-alias", NULL, IfaceAliasOptSet, LOCAL_AUTH,
2981   "retain interface addresses", "disable|enable",
2982   (const void *)OPT_IFACEALIAS},
2983 #ifndef NOINET6
2984   {"ipcp", NULL, OptSet, LOCAL_AUTH, "IP Network Control Protocol",
2985   "disable|enable", (const void *)OPT_IPCP},
2986   {"ipv6cp", NULL, OptSet, LOCAL_AUTH, "IPv6 Network Control Protocol",
2987   "disable|enable", (const void *)OPT_IPV6CP},
2988 #endif
2989   {"keep-session", NULL, OptSet, LOCAL_AUTH, "Retain device session leader",
2990   "disable|enable", (const void *)OPT_KEEPSESSION},
2991   {"loopback", NULL, OptSet, LOCAL_AUTH, "Loop packets for local iface",
2992   "disable|enable", (const void *)OPT_LOOPBACK},
2993   {"nas-ip-address", NULL, OptSet, LOCAL_AUTH, "Send NAS-IP-Address to RADIUS",
2994   "disable|enable", (const void *)OPT_NAS_IP_ADDRESS},
2995   {"nas-identifier", NULL, OptSet, LOCAL_AUTH, "Send NAS-Identifier to RADIUS",
2996   "disable|enable", (const void *)OPT_NAS_IDENTIFIER},
2997   {"passwdauth", NULL, OptSet, LOCAL_AUTH, "Use passwd file",
2998   "disable|enable", (const void *)OPT_PASSWDAUTH},
2999   {"proxy", NULL, OptSet, LOCAL_AUTH, "Create a proxy ARP entry",
3000   "disable|enable", (const void *)OPT_PROXY},
3001   {"proxyall", NULL, OptSet, LOCAL_AUTH, "Proxy ARP for all remote hosts",
3002   "disable|enable", (const void *)OPT_PROXYALL},
3003   {"sroutes", NULL, OptSet, LOCAL_AUTH, "Use sticky routes",
3004   "disable|enable", (const void *)OPT_SROUTES},
3005   {"tcpmssfixup", "mssfixup", OptSet, LOCAL_AUTH, "Modify MSS options",
3006   "disable|enable", (const void *)OPT_TCPMSSFIXUP},
3007   {"throughput", NULL, OptSet, LOCAL_AUTH, "Rolling throughput",
3008   "disable|enable", (const void *)OPT_THROUGHPUT},
3009   {"utmp", NULL, OptSet, LOCAL_AUTH, "Log connections in utmp",
3010   "disable|enable", (const void *)OPT_UTMP},
3011 
3012 #ifndef NOINET6
3013 #define NEG_OPT_MAX 17	/* accept/deny allowed below and not above */
3014 #else
3015 #define NEG_OPT_MAX 15
3016 #endif
3017 
3018   {"acfcomp", NULL, NegotiateSet, LOCAL_AUTH | LOCAL_CX,
3019   "Address & Control field compression", "accept|deny|disable|enable",
3020   (const void *)NEG_ACFCOMP},
3021   {"chap", "chap05", NegotiateSet, LOCAL_AUTH | LOCAL_CX,
3022   "Challenge Handshake Authentication Protocol", "accept|deny|disable|enable",
3023   (const void *)NEG_CHAP05},
3024 #ifndef NODES
3025   {"mschap", "chap80nt", NegotiateSet, LOCAL_AUTH | LOCAL_CX,
3026   "Microsoft (NT) CHAP", "accept|deny|disable|enable",
3027   (const void *)NEG_CHAP80},
3028   {"LANMan", "chap80lm", NegotiateSet, LOCAL_AUTH | LOCAL_CX,
3029   "Microsoft (NT) CHAP", "accept|deny|disable|enable",
3030   (const void *)NEG_CHAP80LM},
3031   {"mschapv2", "chap81", NegotiateSet, LOCAL_AUTH | LOCAL_CX,
3032   "Microsoft CHAP v2", "accept|deny|disable|enable",
3033   (const void *)NEG_CHAP81},
3034   {"mppe", NULL, NegotiateSet, LOCAL_AUTH | LOCAL_CX_OPT,
3035   "MPPE encryption", "accept|deny|disable|enable",
3036   (const void *)NEG_MPPE},
3037 #endif
3038   {"deflate", NULL, NegotiateSet, LOCAL_AUTH | LOCAL_CX_OPT,
3039   "Deflate compression", "accept|deny|disable|enable",
3040   (const void *)NEG_DEFLATE},
3041   {"deflate24", NULL, NegotiateSet, LOCAL_AUTH | LOCAL_CX_OPT,
3042   "Deflate (type 24) compression", "accept|deny|disable|enable",
3043   (const void *)NEG_PPPDDEFLATE},
3044   {"dns", NULL, NegotiateSet, LOCAL_AUTH,
3045   "DNS specification", "accept|deny|disable|enable", (const void *)NEG_DNS},
3046   {"enddisc", NULL, NegotiateSet, LOCAL_AUTH, "ENDDISC negotiation",
3047   "accept|deny|disable|enable", (const void *)NEG_ENDDISC},
3048   {"lqr", NULL, NegotiateSet, LOCAL_AUTH | LOCAL_CX,
3049   "Link Quality Reports", "accept|deny|disable|enable",
3050   (const void *)NEG_LQR},
3051   {"pap", NULL, NegotiateSet, LOCAL_AUTH | LOCAL_CX,
3052   "Password Authentication protocol", "accept|deny|disable|enable",
3053   (const void *)NEG_PAP},
3054   {"pred1", "predictor1", NegotiateSet, LOCAL_AUTH | LOCAL_CX_OPT,
3055   "Predictor 1 compression", "accept|deny|disable|enable",
3056   (const void *)NEG_PRED1},
3057   {"protocomp", NULL, NegotiateSet, LOCAL_AUTH | LOCAL_CX,
3058   "Protocol field compression", "accept|deny|disable|enable",
3059   (const void *)NEG_PROTOCOMP},
3060   {"shortseq", NULL, NegotiateSet, LOCAL_AUTH,
3061   "MP Short Sequence Numbers", "accept|deny|disable|enable",
3062   (const void *)NEG_SHORTSEQ},
3063   {"vjcomp", NULL, NegotiateSet, LOCAL_AUTH,
3064   "Van Jacobson header compression", "accept|deny|disable|enable",
3065   (const void *)NEG_VJCOMP},
3066   {"help", "?", HelpCommand, LOCAL_AUTH | LOCAL_NO_AUTH,
3067   "Display this message", "accept|deny|disable|enable help|? [value]",
3068   NegotiateCommands},
3069   {NULL, NULL, NULL, 0, NULL, NULL, NULL},
3070 };
3071 
3072 static int
3073 NegotiateCommand(struct cmdargs const *arg)
3074 {
3075   if (arg->argc > arg->argn) {
3076     char const *argv[3];
3077     unsigned keep, add;
3078     int n;
3079 
3080     if ((argv[0] = ident_cmd(arg->argv[arg->argn-1], &keep, &add)) == NULL)
3081       return -1;
3082     argv[2] = NULL;
3083 
3084     for (n = arg->argn; n < arg->argc; n++) {
3085       argv[1] = arg->argv[n];
3086       FindExec(arg->bundle, NegotiateCommands + (keep == NEG_HISMASK ?
3087                0 : NEG_OPT_MAX), 2, 1, argv, arg->prompt, arg->cx);
3088     }
3089   } else if (arg->prompt)
3090     prompt_Printf(arg->prompt, "Use `%s ?' to get a list.\n",
3091 	    arg->argv[arg->argn-1]);
3092   else
3093     log_Printf(LogWARN, "%s command must have arguments\n",
3094               arg->argv[arg->argn] );
3095 
3096   return 0;
3097 }
3098 
3099 const char *
3100 command_ShowNegval(unsigned val)
3101 {
3102   switch (val&3) {
3103     case 1: return "disabled & accepted";
3104     case 2: return "enabled & denied";
3105     case 3: return "enabled & accepted";
3106   }
3107   return "disabled & denied";
3108 }
3109 
3110 static int
3111 ClearCommand(struct cmdargs const *arg)
3112 {
3113   struct pppThroughput *t;
3114   struct datalink *cx;
3115   int i, clear_type;
3116 
3117   if (arg->argc < arg->argn + 1)
3118     return -1;
3119 
3120   if (strcasecmp(arg->argv[arg->argn], "physical") == 0) {
3121     cx = arg->cx;
3122     if (!cx)
3123       cx = bundle2datalink(arg->bundle, NULL);
3124     if (!cx) {
3125       log_Printf(LogWARN, "A link must be specified for ``clear physical''\n");
3126       return 1;
3127     }
3128     t = &cx->physical->link.stats.total;
3129   } else if (strcasecmp(arg->argv[arg->argn], "ipcp") == 0)
3130     t = &arg->bundle->ncp.ipcp.throughput;
3131 #ifndef NOINET6
3132   else if (strcasecmp(arg->argv[arg->argn], "ipv6cp") == 0)
3133     t = &arg->bundle->ncp.ipv6cp.throughput;
3134 #endif
3135   else
3136     return -1;
3137 
3138   if (arg->argc > arg->argn + 1) {
3139     clear_type = 0;
3140     for (i = arg->argn + 1; i < arg->argc; i++)
3141       if (strcasecmp(arg->argv[i], "overall") == 0)
3142         clear_type |= THROUGHPUT_OVERALL;
3143       else if (strcasecmp(arg->argv[i], "current") == 0)
3144         clear_type |= THROUGHPUT_CURRENT;
3145       else if (strcasecmp(arg->argv[i], "peak") == 0)
3146         clear_type |= THROUGHPUT_PEAK;
3147       else
3148         return -1;
3149   } else
3150     clear_type = THROUGHPUT_ALL;
3151 
3152   throughput_clear(t, clear_type, arg->prompt);
3153   return 0;
3154 }
3155 
3156 static int
3157 RunListCommand(struct cmdargs const *arg)
3158 {
3159   const char *cmd = arg->argc ? arg->argv[arg->argc - 1] : "???";
3160 
3161 #ifndef NONAT
3162   if (arg->cmd->args == NatCommands &&
3163       tolower(*arg->argv[arg->argn - 1]) == 'a') {
3164     if (arg->prompt)
3165       prompt_Printf(arg->prompt, "The alias command is deprecated\n");
3166     else
3167       log_Printf(LogWARN, "The alias command is deprecated\n");
3168   }
3169 #endif
3170 
3171   if (arg->argc > arg->argn)
3172     FindExec(arg->bundle, arg->cmd->args, arg->argc, arg->argn, arg->argv,
3173              arg->prompt, arg->cx);
3174   else if (arg->prompt)
3175     prompt_Printf(arg->prompt, "Use `%s help' to get a list or `%s help"
3176                   " <option>' for syntax help.\n", cmd, cmd);
3177   else
3178     log_Printf(LogWARN, "%s command must have arguments\n", cmd);
3179 
3180   return 0;
3181 }
3182 
3183 static int
3184 IfaceNameCommand(struct cmdargs const *arg)
3185 {
3186   int n = arg->argn;
3187 
3188   if (arg->argc != n + 1)
3189     return -1;
3190 
3191   if (!iface_Name(arg->bundle->iface, arg->argv[n]))
3192     return 1;
3193 
3194   log_SetTun(arg->bundle->unit, arg->bundle->iface->name);
3195   return 0;
3196 }
3197 
3198 static int
3199 IfaceAddCommand(struct cmdargs const *arg)
3200 {
3201   struct ncpaddr peer, addr;
3202   struct ncprange ifa;
3203   struct in_addr mask;
3204   int n, how;
3205 
3206   if (arg->argc == arg->argn + 1) {
3207     if (!ncprange_aton(&ifa, NULL, arg->argv[arg->argn]))
3208       return -1;
3209     ncpaddr_init(&peer);
3210   } else {
3211     if (arg->argc == arg->argn + 2) {
3212       if (!ncprange_aton(&ifa, NULL, arg->argv[arg->argn]))
3213         return -1;
3214       n = 1;
3215     } else if (arg->argc == arg->argn + 3) {
3216       if (!ncpaddr_aton(&addr, NULL, arg->argv[arg->argn]))
3217         return -1;
3218       if (ncpaddr_family(&addr) != AF_INET)
3219         return -1;
3220       ncprange_sethost(&ifa, &addr);
3221       if (!ncpaddr_aton(&addr, NULL, arg->argv[arg->argn + 1]))
3222         return -1;
3223       if (!ncpaddr_getip4(&addr, &mask))
3224         return -1;
3225       if (!ncprange_setip4mask(&ifa, mask))
3226         return -1;
3227       n = 2;
3228     } else
3229       return -1;
3230 
3231     if (!ncpaddr_aton(&peer, NULL, arg->argv[arg->argn + n]))
3232       return -1;
3233 
3234     if (ncprange_family(&ifa) != ncpaddr_family(&peer)) {
3235       log_Printf(LogWARN, "IfaceAddCommand: src and dst address families"
3236                  " differ\n");
3237       return -1;
3238     }
3239   }
3240 
3241   how = IFACE_ADD_LAST;
3242   if (arg->cmd->args)
3243     how |= IFACE_FORCE_ADD;
3244 
3245   return !iface_Add(arg->bundle->iface, &arg->bundle->ncp, &ifa, &peer, how);
3246 }
3247 
3248 static int
3249 IfaceDeleteCommand(struct cmdargs const *arg)
3250 {
3251   struct ncpaddr ifa;
3252   struct in_addr ifa4;
3253   int ok;
3254 
3255   if (arg->argc != arg->argn + 1)
3256     return -1;
3257 
3258   if (!ncpaddr_aton(&ifa, NULL, arg->argv[arg->argn]))
3259     return -1;
3260 
3261   if (arg->bundle->ncp.ipcp.fsm.state == ST_OPENED &&
3262       ncpaddr_getip4(&ifa, &ifa4) &&
3263       arg->bundle->ncp.ipcp.my_ip.s_addr == ifa4.s_addr) {
3264     log_Printf(LogWARN, "%s: Cannot remove active interface address\n",
3265                ncpaddr_ntoa(&ifa));
3266     return 1;
3267   }
3268 
3269   ok = iface_Delete(arg->bundle->iface, &arg->bundle->ncp, &ifa);
3270   if (!ok) {
3271     if (arg->cmd->args)
3272       ok = 1;
3273     else if (arg->prompt)
3274       prompt_Printf(arg->prompt, "%s: No such interface address\n",
3275                     ncpaddr_ntoa(&ifa));
3276     else
3277       log_Printf(LogWARN, "%s: No such interface address\n",
3278                  ncpaddr_ntoa(&ifa));
3279   }
3280 
3281   return !ok;
3282 }
3283 
3284 static int
3285 IfaceClearCommand(struct cmdargs const *arg)
3286 {
3287   int family, how;
3288 
3289   family = 0;
3290   if (arg->argc == arg->argn + 1) {
3291     if (strcasecmp(arg->argv[arg->argn], "inet") == 0)
3292       family = AF_INET;
3293 #ifndef NOINET6
3294     else if (strcasecmp(arg->argv[arg->argn], "inet6") == 0)
3295       family = AF_INET6;
3296 #endif
3297     else
3298       return -1;
3299   } else if (arg->argc != arg->argn)
3300     return -1;
3301 
3302   how = arg->bundle->ncp.ipcp.fsm.state == ST_OPENED ||
3303         arg->bundle->phys_type.all & PHYS_AUTO ?
3304         IFACE_CLEAR_ALIASES : IFACE_CLEAR_ALL;
3305   iface_Clear(arg->bundle->iface, &arg->bundle->ncp, family, how);
3306 
3307   return 0;
3308 }
3309 
3310 static int
3311 SetProcTitle(struct cmdargs const *arg)
3312 {
3313   static char title[LINE_LEN];
3314   char *argv[MAXARGS];
3315   int argc = arg->argc - arg->argn;
3316 
3317   if (arg->argc <= arg->argn) {
3318     SetTitle(NULL);
3319     return 0;
3320   }
3321 
3322   if ((unsigned)argc >= sizeof argv / sizeof argv[0]) {
3323     argc = sizeof argv / sizeof argv[0] - 1;
3324     log_Printf(LogWARN, "Truncating proc title to %d args\n", argc);
3325   }
3326   command_Expand(argv, argc, arg->argv + arg->argn, arg->bundle, 1, getpid());
3327   Concatinate(title, sizeof title, argc, (const char *const *)argv);
3328   SetTitle(title);
3329   command_Free(argc, argv);
3330 
3331   return 0;
3332 }
3333