xref: /freebsd/usr.sbin/ppp/command.c (revision daf1cffce2e07931f27c6c6998652e90df6ba87e)
1 /*
2  *		PPP User command processing module
3  *
4  *	    Written by Toshiharu OHNO (tony-o@iij.ad.jp)
5  *
6  *   Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd.
7  *
8  * Redistribution and use in source and binary forms are permitted
9  * provided that the above copyright notice and this paragraph are
10  * duplicated in all such forms and that any documentation,
11  * advertising materials, and other materials related to such
12  * distribution and use acknowledge that the software was developed
13  * by the Internet Initiative Japan, Inc.  The name of the
14  * IIJ may not be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19  *
20  * $FreeBSD$
21  *
22  */
23 #include <sys/param.h>
24 #include <netinet/in_systm.h>
25 #include <netinet/in.h>
26 #include <netinet/ip.h>
27 #include <arpa/inet.h>
28 #include <sys/socket.h>
29 #include <net/route.h>
30 #include <netdb.h>
31 #include <sys/un.h>
32 
33 #include <ctype.h>
34 #include <errno.h>
35 #include <fcntl.h>
36 #ifdef __OpenBSD__
37 #include <util.h>
38 #else
39 #include <libutil.h>
40 #endif
41 #include <paths.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <sys/wait.h>
46 #include <termios.h>
47 #include <unistd.h>
48 
49 #ifndef NONAT
50 #ifdef __FreeBSD__
51 #include <alias.h>
52 #else
53 #include "alias.h"
54 #endif
55 #endif
56 #include "layer.h"
57 #include "defs.h"
58 #include "command.h"
59 #include "mbuf.h"
60 #include "log.h"
61 #include "timer.h"
62 #include "fsm.h"
63 #include "lcp.h"
64 #include "iplist.h"
65 #include "throughput.h"
66 #include "slcompress.h"
67 #include "lqr.h"
68 #include "hdlc.h"
69 #include "ipcp.h"
70 #ifndef NONAT
71 #include "nat_cmd.h"
72 #endif
73 #include "systems.h"
74 #include "filter.h"
75 #include "descriptor.h"
76 #include "main.h"
77 #include "route.h"
78 #include "ccp.h"
79 #include "auth.h"
80 #include "async.h"
81 #include "link.h"
82 #include "physical.h"
83 #include "mp.h"
84 #ifndef NORADIUS
85 #include "radius.h"
86 #endif
87 #include "bundle.h"
88 #include "server.h"
89 #include "prompt.h"
90 #include "chat.h"
91 #include "chap.h"
92 #include "cbcp.h"
93 #include "datalink.h"
94 #include "iface.h"
95 #include "id.h"
96 
97 /* ``set'' values */
98 #define	VAR_AUTHKEY	0
99 #define	VAR_DIAL	1
100 #define	VAR_LOGIN	2
101 #define	VAR_AUTHNAME	3
102 #define	VAR_AUTOLOAD	4
103 #define	VAR_WINSIZE	5
104 #define	VAR_DEVICE	6
105 #define	VAR_ACCMAP	7
106 #define	VAR_MRRU	8
107 #define	VAR_MRU		9
108 #define	VAR_MTU		10
109 #define	VAR_OPENMODE	11
110 #define	VAR_PHONE	12
111 #define	VAR_HANGUP	13
112 #define	VAR_IDLETIMEOUT	14
113 #define	VAR_LQRPERIOD	15
114 #define	VAR_LCPRETRY	16
115 #define	VAR_CHAPRETRY	17
116 #define	VAR_PAPRETRY	18
117 #define	VAR_CCPRETRY	19
118 #define	VAR_IPCPRETRY	20
119 #define	VAR_DNS		21
120 #define	VAR_NBNS	22
121 #define	VAR_MODE	23
122 #define	VAR_CALLBACK	24
123 #define	VAR_CBCP	25
124 #define	VAR_CHOKED	26
125 #define	VAR_SENDPIPE	27
126 #define	VAR_RECVPIPE	28
127 #define	VAR_RADIUS	29
128 #define	VAR_CD		30
129 #define	VAR_PARITY	31
130 #define VAR_CRTSCTS	32
131 #define VAR_URGENTPORTS	33
132 #define	VAR_LOGOUT	34
133 
134 /* ``accept|deny|disable|enable'' masks */
135 #define NEG_HISMASK (1)
136 #define NEG_MYMASK (2)
137 
138 /* ``accept|deny|disable|enable'' values */
139 #define NEG_ACFCOMP	40
140 #define NEG_CHAP05	41
141 #define NEG_CHAP80	42
142 #define NEG_CHAP80LM	43
143 #define NEG_DEFLATE	44
144 #define NEG_DNS		45
145 #define NEG_ENDDISC	46
146 #define NEG_LQR		47
147 #define NEG_PAP		48
148 #define NEG_PPPDDEFLATE	49
149 #define NEG_PRED1	50
150 #define NEG_PROTOCOMP	51
151 #define NEG_SHORTSEQ	52
152 #define NEG_VJCOMP	53
153 
154 const char Version[] = "2.26";
155 
156 static int ShowCommand(struct cmdargs const *);
157 static int TerminalCommand(struct cmdargs const *);
158 static int QuitCommand(struct cmdargs const *);
159 static int OpenCommand(struct cmdargs const *);
160 static int CloseCommand(struct cmdargs const *);
161 static int DownCommand(struct cmdargs const *);
162 static int SetCommand(struct cmdargs const *);
163 static int LinkCommand(struct cmdargs const *);
164 static int AddCommand(struct cmdargs const *);
165 static int DeleteCommand(struct cmdargs const *);
166 static int NegotiateCommand(struct cmdargs const *);
167 static int ClearCommand(struct cmdargs const *);
168 static int RunListCommand(struct cmdargs const *);
169 static int IfaceAddCommand(struct cmdargs const *);
170 static int IfaceDeleteCommand(struct cmdargs const *);
171 static int IfaceClearCommand(struct cmdargs const *);
172 static int SetProcTitle(struct cmdargs const *);
173 #ifndef NONAT
174 static int AliasEnable(struct cmdargs const *);
175 static int AliasOption(struct cmdargs const *);
176 #endif
177 
178 static const char *
179 showcx(struct cmdtab const *cmd)
180 {
181   if (cmd->lauth & LOCAL_CX)
182     return "(c)";
183   else if (cmd->lauth & LOCAL_CX_OPT)
184     return "(o)";
185 
186   return "";
187 }
188 
189 static int
190 HelpCommand(struct cmdargs const *arg)
191 {
192   struct cmdtab const *cmd;
193   int n, cmax, dmax, cols, cxlen;
194   const char *cx;
195 
196   if (!arg->prompt) {
197     log_Printf(LogWARN, "help: Cannot help without a prompt\n");
198     return 0;
199   }
200 
201   if (arg->argc > arg->argn) {
202     for (cmd = arg->cmdtab; cmd->name || cmd->alias; cmd++)
203       if ((cmd->lauth & arg->prompt->auth) &&
204           ((cmd->name && !strcasecmp(cmd->name, arg->argv[arg->argn])) ||
205            (cmd->alias && !strcasecmp(cmd->alias, arg->argv[arg->argn])))) {
206 	prompt_Printf(arg->prompt, "%s %s\n", cmd->syntax, showcx(cmd));
207 	return 0;
208       }
209     return -1;
210   }
211 
212   cmax = dmax = 0;
213   for (cmd = arg->cmdtab; cmd->func; cmd++)
214     if (cmd->name && (cmd->lauth & arg->prompt->auth)) {
215       if ((n = strlen(cmd->name) + strlen(showcx(cmd))) > cmax)
216         cmax = n;
217       if ((n = strlen(cmd->helpmes)) > dmax)
218         dmax = n;
219     }
220 
221   cols = 80 / (dmax + cmax + 3);
222   n = 0;
223   prompt_Printf(arg->prompt, "(o) = Optional context,"
224                 " (c) = Context required\n");
225   for (cmd = arg->cmdtab; cmd->func; cmd++)
226     if (cmd->name && (cmd->lauth & arg->prompt->auth)) {
227       cx = showcx(cmd);
228       cxlen = cmax - strlen(cmd->name);
229       if (n % cols != 0)
230         prompt_Printf(arg->prompt, " ");
231       prompt_Printf(arg->prompt, "%s%-*.*s: %-*.*s",
232               cmd->name, cxlen, cxlen, cx, dmax, dmax, cmd->helpmes);
233       if (++n % cols == 0)
234         prompt_Printf(arg->prompt, "\n");
235     }
236   if (n % cols != 0)
237     prompt_Printf(arg->prompt, "\n");
238 
239   return 0;
240 }
241 
242 static int
243 CloneCommand(struct cmdargs const *arg)
244 {
245   char namelist[LINE_LEN];
246   char *name;
247   int f;
248 
249   if (arg->argc == arg->argn)
250     return -1;
251 
252   namelist[sizeof namelist - 1] = '\0';
253   for (f = arg->argn; f < arg->argc; f++) {
254     strncpy(namelist, arg->argv[f], sizeof namelist - 1);
255     for(name = strtok(namelist, ", "); name; name = strtok(NULL,", "))
256       bundle_DatalinkClone(arg->bundle, arg->cx, name);
257   }
258 
259   return 0;
260 }
261 
262 static int
263 RemoveCommand(struct cmdargs const *arg)
264 {
265   if (arg->argc != arg->argn)
266     return -1;
267 
268   if (arg->cx->state != DATALINK_CLOSED) {
269     log_Printf(LogWARN, "remove: Cannot delete links that aren't closed\n");
270     return 2;
271   }
272 
273   bundle_DatalinkRemove(arg->bundle, arg->cx);
274   return 0;
275 }
276 
277 static int
278 RenameCommand(struct cmdargs const *arg)
279 {
280   if (arg->argc != arg->argn + 1)
281     return -1;
282 
283   if (bundle_RenameDatalink(arg->bundle, arg->cx, arg->argv[arg->argn]))
284     return 0;
285 
286   log_Printf(LogWARN, "%s -> %s: target name already exists\n",
287              arg->cx->name, arg->argv[arg->argn]);
288   return 1;
289 }
290 
291 int
292 LoadCommand(struct cmdargs const *arg)
293 {
294   const char *err;
295   int n, mode;
296 
297   mode = arg->bundle->phys_type.all;
298 
299   if (arg->argn < arg->argc) {
300     for (n = arg->argn; n < arg->argc; n++)
301       if ((err = system_IsValid(arg->argv[n], arg->prompt, mode)) != NULL) {
302         log_Printf(LogWARN, "%s: %s\n", arg->argv[n], err);
303         return 1;
304       }
305 
306     for (n = arg->argn; n < arg->argc; n++) {
307       bundle_SetLabel(arg->bundle, arg->argv[arg->argc - 1]);
308       system_Select(arg->bundle, arg->argv[n], CONFFILE, arg->prompt, arg->cx);
309     }
310     bundle_SetLabel(arg->bundle, arg->argv[arg->argc - 1]);
311   } else if ((err = system_IsValid("default", arg->prompt, mode)) != NULL) {
312     log_Printf(LogWARN, "default: %s\n", err);
313     return 1;
314   } else {
315     bundle_SetLabel(arg->bundle, "default");
316     system_Select(arg->bundle, "default", CONFFILE, arg->prompt, arg->cx);
317     bundle_SetLabel(arg->bundle, "default");
318   }
319 
320   return 0;
321 }
322 
323 int
324 SaveCommand(struct cmdargs const *arg)
325 {
326   log_Printf(LogWARN, "save command is not implemented (yet).\n");
327   return 1;
328 }
329 
330 static int
331 DialCommand(struct cmdargs const *arg)
332 {
333   int res;
334 
335   if ((arg->cx && !(arg->cx->physical->type & (PHYS_INTERACTIVE|PHYS_AUTO)))
336       || (!arg->cx &&
337           (arg->bundle->phys_type.all & ~(PHYS_INTERACTIVE|PHYS_AUTO)))) {
338     log_Printf(LogWARN, "Manual dial is only available for auto and"
339               " interactive links\n");
340     return 1;
341   }
342 
343   if (arg->argc > arg->argn && (res = LoadCommand(arg)) != 0)
344     return res;
345 
346   bundle_Open(arg->bundle, arg->cx ? arg->cx->name : NULL, PHYS_ALL, 1);
347 
348   return 0;
349 }
350 
351 #define isinword(ch) (isalnum(ch) || (ch) == '_')
352 
353 static char *
354 strstrword(char *big, const char *little)
355 {
356   /* Get the first occurance of the word ``little'' in ``big'' */
357   char *pos;
358   int len;
359 
360   pos = big;
361   len = strlen(little);
362 
363   while ((pos = strstr(pos, little)) != NULL)
364     if ((pos != big && isinword(pos[-1])) || isinword(pos[len]))
365       pos++;
366     else if (pos != big && pos[-1] == '\\')
367       memmove(pos - 1, pos, strlen(pos) + 1);
368     else
369       break;
370 
371   return pos;
372 }
373 
374 static char *
375 subst(char *tgt, const char *oldstr, const char *newstr)
376 {
377   /* tgt is a malloc()d area... realloc() as necessary */
378   char *word, *ntgt;
379   int ltgt, loldstr, lnewstr, pos;
380 
381   if ((word = strstrword(tgt, oldstr)) == NULL)
382     return tgt;
383 
384   ltgt = strlen(tgt) + 1;
385   loldstr = strlen(oldstr);
386   lnewstr = strlen(newstr);
387   do {
388     pos = word - tgt;
389     if (loldstr > lnewstr)
390       bcopy(word + loldstr, word + lnewstr, ltgt - pos - loldstr);
391     if (loldstr != lnewstr) {
392       ntgt = realloc(tgt, ltgt += lnewstr - loldstr);
393       if (ntgt == NULL)
394         break;			/* Oh wonderful ! */
395       word = ntgt + pos;
396       tgt = ntgt;
397     }
398     if (lnewstr > loldstr)
399       bcopy(word + loldstr, word + lnewstr, ltgt - pos - loldstr);
400     bcopy(newstr, word, lnewstr);
401   } while ((word = strstrword(word, oldstr)));
402 
403   return tgt;
404 }
405 
406 void
407 command_Expand(char **nargv, int argc, char const *const *oargv,
408                struct bundle *bundle, int inc0, pid_t pid)
409 {
410   int arg;
411   char pidstr[12];
412 
413   if (inc0)
414     arg = 0;		/* Start at arg 0 */
415   else {
416     nargv[0] = strdup(oargv[0]);
417     arg = 1;
418   }
419   snprintf(pidstr, sizeof pidstr, "%d", (int)pid);
420   for (; arg < argc; arg++) {
421     nargv[arg] = strdup(oargv[arg]);
422     nargv[arg] = subst(nargv[arg], "HISADDR",
423                        inet_ntoa(bundle->ncp.ipcp.peer_ip));
424     nargv[arg] = subst(nargv[arg], "AUTHNAME", bundle->cfg.auth.name);
425     nargv[arg] = subst(nargv[arg], "INTERFACE", bundle->iface->name);
426     nargv[arg] = subst(nargv[arg], "MYADDR", inet_ntoa(bundle->ncp.ipcp.my_ip));
427     nargv[arg] = subst(nargv[arg], "USER", bundle->ncp.mp.peer.authname);
428     nargv[arg] = subst(nargv[arg], "PEER_ENDDISC",
429                        mp_Enddisc(bundle->ncp.mp.peer.enddisc.class,
430                                   bundle->ncp.mp.peer.enddisc.address,
431                                   bundle->ncp.mp.peer.enddisc.len));
432     nargv[arg] = subst(nargv[arg], "ENDDISC",
433                        mp_Enddisc(bundle->ncp.mp.cfg.enddisc.class,
434                                   bundle->ncp.mp.cfg.enddisc.address,
435                                   bundle->ncp.mp.cfg.enddisc.len));
436     nargv[arg] = subst(nargv[arg], "PROCESSID", pidstr);
437     nargv[arg] = subst(nargv[arg], "LABEL", bundle_GetLabel(bundle));
438   }
439   nargv[arg] = NULL;
440 }
441 
442 static int
443 ShellCommand(struct cmdargs const *arg, int bg)
444 {
445   const char *shell;
446   pid_t shpid, pid;
447 
448 #ifdef SHELL_ONLY_INTERACTIVELY
449   /* we're only allowed to shell when we run ppp interactively */
450   if (arg->prompt && arg->prompt->owner) {
451     log_Printf(LogWARN, "Can't start a shell from a network connection\n");
452     return 1;
453   }
454 #endif
455 
456   if (arg->argc == arg->argn) {
457     if (!arg->prompt) {
458       log_Printf(LogWARN, "Can't start an interactive shell from"
459                 " a config file\n");
460       return 1;
461     } else if (arg->prompt->owner) {
462       log_Printf(LogWARN, "Can't start an interactive shell from"
463                 " a socket connection\n");
464       return 1;
465     } else if (bg) {
466       log_Printf(LogWARN, "Can only start an interactive shell in"
467 		" the foreground mode\n");
468       return 1;
469     }
470   }
471 
472   pid = getpid();
473   if ((shpid = fork()) == 0) {
474     int i, fd;
475 
476     if ((shell = getenv("SHELL")) == 0)
477       shell = _PATH_BSHELL;
478 
479     timer_TermService();
480 
481     if (arg->prompt)
482       fd = arg->prompt->fd_out;
483     else if ((fd = open(_PATH_DEVNULL, O_RDWR)) == -1) {
484       log_Printf(LogALERT, "Failed to open %s: %s\n",
485                 _PATH_DEVNULL, strerror(errno));
486       exit(1);
487     }
488     dup2(fd, STDIN_FILENO);
489     dup2(fd, STDOUT_FILENO);
490     dup2(fd, STDERR_FILENO);
491     for (i = getdtablesize(); i > STDERR_FILENO; i--)
492       fcntl(i, F_SETFD, 1);
493 
494     setuid(ID0realuid());
495     if (arg->argc > arg->argn) {
496       /* substitute pseudo args */
497       char *argv[MAXARGS];
498       int argc = arg->argc - arg->argn;
499 
500       if (argc >= sizeof argv / sizeof argv[0]) {
501         argc = sizeof argv / sizeof argv[0] - 1;
502         log_Printf(LogWARN, "Truncating shell command to %d args\n", argc);
503       }
504       command_Expand(argv, argc, arg->argv + arg->argn, arg->bundle, 0, pid);
505       if (bg) {
506 	pid_t p;
507 
508 	p = getpid();
509 	if (daemon(1, 1) == -1) {
510 	  log_Printf(LogERROR, "%d: daemon: %s\n", (int)p, strerror(errno));
511 	  exit(1);
512 	}
513       } else if (arg->prompt)
514         printf("ppp: Pausing until %s finishes\n", arg->argv[arg->argn]);
515       execvp(argv[0], argv);
516     } else {
517       if (arg->prompt)
518         printf("ppp: Pausing until %s finishes\n", shell);
519       prompt_TtyOldMode(arg->prompt);
520       execl(shell, shell, NULL);
521     }
522 
523     log_Printf(LogWARN, "exec() of %s failed: %s\n",
524               arg->argc > arg->argn ? arg->argv[arg->argn] : shell,
525               strerror(errno));
526     _exit(255);
527   }
528 
529   if (shpid == (pid_t) - 1)
530     log_Printf(LogERROR, "Fork failed: %s\n", strerror(errno));
531   else {
532     int status;
533     waitpid(shpid, &status, 0);
534   }
535 
536   if (arg->prompt && !arg->prompt->owner)
537     prompt_TtyCommandMode(arg->prompt);
538 
539   return 0;
540 }
541 
542 static int
543 BgShellCommand(struct cmdargs const *arg)
544 {
545   if (arg->argc == arg->argn)
546     return -1;
547   return ShellCommand(arg, 1);
548 }
549 
550 static int
551 FgShellCommand(struct cmdargs const *arg)
552 {
553   return ShellCommand(arg, 0);
554 }
555 
556 #ifndef NONAT
557 static struct cmdtab const AliasCommands[] =
558 {
559   {"addr", NULL, nat_RedirectAddr, LOCAL_AUTH,
560    "static address translation", "nat addr [addr_local addr_alias]"},
561   {"deny_incoming", NULL, AliasOption, LOCAL_AUTH,
562    "stop incoming connections", "nat deny_incoming yes|no",
563    (const void *) PKT_ALIAS_DENY_INCOMING},
564   {"enable", NULL, AliasEnable, LOCAL_AUTH,
565    "enable NAT", "nat enable yes|no"},
566   {"log", NULL, AliasOption, LOCAL_AUTH,
567    "log NAT link creation", "nat log yes|no",
568    (const void *) PKT_ALIAS_LOG},
569   {"port", NULL, nat_RedirectPort, LOCAL_AUTH, "port redirection",
570    "nat port proto localaddr:port[-port] aliasport[-aliasport]"},
571   {"pptp", NULL, nat_Pptp, LOCAL_AUTH,
572    "Set the PPTP address", "nat pptp IP"},
573   {"proxy", NULL, nat_ProxyRule, LOCAL_AUTH,
574    "proxy control", "nat proxy server host[:port] ..."},
575   {"same_ports", NULL, AliasOption, LOCAL_AUTH,
576    "try to leave port numbers unchanged", "nat same_ports yes|no",
577    (const void *) PKT_ALIAS_SAME_PORTS},
578   {"unregistered_only", NULL, AliasOption, LOCAL_AUTH,
579    "translate unregistered (private) IP address space only",
580    "nat unregistered_only yes|no",
581    (const void *) PKT_ALIAS_UNREGISTERED_ONLY},
582   {"use_sockets", NULL, AliasOption, LOCAL_AUTH,
583    "allocate host sockets", "nat use_sockets yes|no",
584    (const void *) PKT_ALIAS_USE_SOCKETS},
585   {"help", "?", HelpCommand, LOCAL_AUTH | LOCAL_NO_AUTH,
586    "Display this message", "nat help|? [command]", AliasCommands},
587   {NULL, NULL, NULL},
588 };
589 #endif
590 
591 static struct cmdtab const AllowCommands[] = {
592   {"modes", "mode", AllowModes, LOCAL_AUTH,
593   "Only allow certain ppp modes", "allow modes mode..."},
594   {"users", "user", AllowUsers, LOCAL_AUTH,
595   "Only allow ppp access to certain users", "allow users logname..."},
596   {"help", "?", HelpCommand, LOCAL_AUTH | LOCAL_NO_AUTH,
597   "Display this message", "allow help|? [command]", AllowCommands},
598   {NULL, NULL, NULL},
599 };
600 
601 static struct cmdtab const IfaceCommands[] =
602 {
603   {"add", NULL, IfaceAddCommand, LOCAL_AUTH,
604    "Add iface address", "iface add addr[/bits| mask] peer", NULL},
605   {NULL, "add!", IfaceAddCommand, LOCAL_AUTH,
606    "Add or change an iface address", "iface add! addr[/bits| mask] peer",
607    (void *)1},
608   {"clear", NULL, IfaceClearCommand, LOCAL_AUTH,
609    "Clear iface address(es)", "iface clear"},
610   {"delete", "rm", IfaceDeleteCommand, LOCAL_AUTH,
611    "Delete iface address", "iface delete addr", NULL},
612   {NULL, "rm!", IfaceDeleteCommand, LOCAL_AUTH,
613    "Delete iface address", "iface delete addr", (void *)1},
614   {NULL, "delete!", IfaceDeleteCommand, LOCAL_AUTH,
615    "Delete iface address", "iface delete addr", (void *)1},
616   {"show", NULL, iface_Show, LOCAL_AUTH,
617    "Show iface address(es)", "iface show"},
618   {"help", "?", HelpCommand, LOCAL_AUTH | LOCAL_NO_AUTH,
619    "Display this message", "nat help|? [command]", IfaceCommands},
620   {NULL, NULL, NULL},
621 };
622 
623 static struct cmdtab const Commands[] = {
624   {"accept", NULL, NegotiateCommand, LOCAL_AUTH | LOCAL_CX_OPT,
625   "accept option request", "accept option .."},
626   {"add", NULL, AddCommand, LOCAL_AUTH,
627   "add route", "add dest mask gateway", NULL},
628   {NULL, "add!", AddCommand, LOCAL_AUTH,
629   "add or change route", "add! dest mask gateway", (void *)1},
630   {"allow", "auth", RunListCommand, LOCAL_AUTH,
631   "Allow ppp access", "allow users|modes ....", AllowCommands},
632   {"bg", "!bg", BgShellCommand, LOCAL_AUTH,
633   "Run a background command", "[!]bg command"},
634   {"clear", NULL, ClearCommand, LOCAL_AUTH | LOCAL_CX_OPT,
635   "Clear throughput statistics",
636   "clear ipcp|physical [current|overall|peak]..."},
637   {"clone", NULL, CloneCommand, LOCAL_AUTH | LOCAL_CX,
638   "Clone a link", "clone newname..."},
639   {"close", NULL, CloseCommand, LOCAL_AUTH | LOCAL_CX_OPT,
640   "Close an FSM", "close [lcp|ccp]"},
641   {"delete", NULL, DeleteCommand, LOCAL_AUTH,
642   "delete route", "delete dest", NULL},
643   {NULL, "delete!", DeleteCommand, LOCAL_AUTH,
644   "delete a route if it exists", "delete! dest", (void *)1},
645   {"deny", NULL, NegotiateCommand, LOCAL_AUTH | LOCAL_CX_OPT,
646   "Deny option request", "deny option .."},
647   {"dial", "call", DialCommand, LOCAL_AUTH | LOCAL_CX_OPT,
648   "Dial and login", "dial|call [system ...]", NULL},
649   {"disable", NULL, NegotiateCommand, LOCAL_AUTH | LOCAL_CX_OPT,
650   "Disable option", "disable option .."},
651   {"down", NULL, DownCommand, LOCAL_AUTH | LOCAL_CX_OPT,
652   "Generate a down event", "down [ccp|lcp]"},
653   {"enable", NULL, NegotiateCommand, LOCAL_AUTH | LOCAL_CX_OPT,
654   "Enable option", "enable option .."},
655   {"iface", "interface", RunListCommand, LOCAL_AUTH,
656   "interface control", "iface option ...", IfaceCommands},
657   {"link", "datalink", LinkCommand, LOCAL_AUTH,
658   "Link specific commands", "link name command ..."},
659   {"load", NULL, LoadCommand, LOCAL_AUTH | LOCAL_CX_OPT,
660   "Load settings", "load [system ...]"},
661 #ifndef NONAT
662   {"nat", "alias", RunListCommand, LOCAL_AUTH,
663   "NAT control", "nat option yes|no", AliasCommands},
664 #endif
665   {"open", NULL, OpenCommand, LOCAL_AUTH | LOCAL_CX_OPT,
666   "Open an FSM", "open! [lcp|ccp|ipcp]", (void *)1},
667   {"passwd", NULL, PasswdCommand, LOCAL_NO_AUTH,
668   "Password for manipulation", "passwd LocalPassword"},
669   {"quit", "bye", QuitCommand, LOCAL_AUTH | LOCAL_NO_AUTH,
670   "Quit PPP program", "quit|bye [all]"},
671   {"remove", "rm", RemoveCommand, LOCAL_AUTH | LOCAL_CX,
672   "Remove a link", "remove"},
673   {"rename", "mv", RenameCommand, LOCAL_AUTH | LOCAL_CX,
674   "Rename a link", "rename name"},
675   {"save", NULL, SaveCommand, LOCAL_AUTH,
676   "Save settings", "save"},
677   {"set", "setup", SetCommand, LOCAL_AUTH | LOCAL_CX_OPT,
678   "Set parameters", "set[up] var value"},
679   {"shell", "!", FgShellCommand, LOCAL_AUTH,
680   "Run a subshell", "shell|! [sh command]"},
681   {"show", NULL, ShowCommand, LOCAL_AUTH | LOCAL_CX_OPT,
682   "Show status and stats", "show var"},
683   {"term", NULL, TerminalCommand, LOCAL_AUTH | LOCAL_CX,
684   "Enter terminal mode", "term"},
685   {"help", "?", HelpCommand, LOCAL_AUTH | LOCAL_NO_AUTH,
686   "Display this message", "help|? [command]", Commands},
687   {NULL, NULL, NULL},
688 };
689 
690 static int
691 ShowEscape(struct cmdargs const *arg)
692 {
693   if (arg->cx->physical->async.cfg.EscMap[32]) {
694     int code, bit;
695     const char *sep = "";
696 
697     for (code = 0; code < 32; code++)
698       if (arg->cx->physical->async.cfg.EscMap[code])
699 	for (bit = 0; bit < 8; bit++)
700 	  if (arg->cx->physical->async.cfg.EscMap[code] & (1 << bit)) {
701 	    prompt_Printf(arg->prompt, "%s0x%02x", sep, (code << 3) + bit);
702             sep = ", ";
703           }
704     prompt_Printf(arg->prompt, "\n");
705   }
706   return 0;
707 }
708 
709 static int
710 ShowTimerList(struct cmdargs const *arg)
711 {
712   timer_Show(0, arg->prompt);
713   return 0;
714 }
715 
716 static int
717 ShowStopped(struct cmdargs const *arg)
718 {
719   prompt_Printf(arg->prompt, " Stopped Timer:  LCP: ");
720   if (!arg->cx->physical->link.lcp.fsm.StoppedTimer.load)
721     prompt_Printf(arg->prompt, "Disabled");
722   else
723     prompt_Printf(arg->prompt, "%ld secs",
724                   arg->cx->physical->link.lcp.fsm.StoppedTimer.load / SECTICKS);
725 
726   prompt_Printf(arg->prompt, ", CCP: ");
727   if (!arg->cx->physical->link.ccp.fsm.StoppedTimer.load)
728     prompt_Printf(arg->prompt, "Disabled");
729   else
730     prompt_Printf(arg->prompt, "%ld secs",
731                   arg->cx->physical->link.ccp.fsm.StoppedTimer.load / SECTICKS);
732 
733   prompt_Printf(arg->prompt, "\n");
734 
735   return 0;
736 }
737 
738 static int
739 ShowVersion(struct cmdargs const *arg)
740 {
741   prompt_Printf(arg->prompt, "PPP Version %s - %s\n", Version, __DATE__);
742   return 0;
743 }
744 
745 static int
746 ShowProtocolStats(struct cmdargs const *arg)
747 {
748   struct link *l = command_ChooseLink(arg);
749 
750   prompt_Printf(arg->prompt, "%s:\n", l->name);
751   link_ReportProtocolStatus(l, arg->prompt);
752   return 0;
753 }
754 
755 static struct cmdtab const ShowCommands[] = {
756   {"bundle", NULL, bundle_ShowStatus, LOCAL_AUTH,
757   "bundle details", "show bundle"},
758   {"ccp", NULL, ccp_ReportStatus, LOCAL_AUTH | LOCAL_CX_OPT,
759   "CCP status", "show cpp"},
760   {"compress", NULL, sl_Show, LOCAL_AUTH,
761   "VJ compression stats", "show compress"},
762   {"escape", NULL, ShowEscape, LOCAL_AUTH | LOCAL_CX,
763   "escape characters", "show escape"},
764   {"filter", NULL, filter_Show, LOCAL_AUTH,
765   "packet filters", "show filter [in|out|dial|alive]"},
766   {"hdlc", NULL, hdlc_ReportStatus, LOCAL_AUTH | LOCAL_CX,
767   "HDLC errors", "show hdlc"},
768   {"iface", "interface", iface_Show, LOCAL_AUTH,
769   "Interface status", "show iface"},
770   {"ipcp", NULL, ipcp_Show, LOCAL_AUTH,
771   "IPCP status", "show ipcp"},
772   {"layers", NULL, link_ShowLayers, LOCAL_AUTH | LOCAL_CX_OPT,
773   "Protocol layers", "show layers"},
774   {"lcp", NULL, lcp_ReportStatus, LOCAL_AUTH | LOCAL_CX,
775   "LCP status", "show lcp"},
776   {"link", "datalink", datalink_Show, LOCAL_AUTH | LOCAL_CX,
777   "(high-level) link info", "show link"},
778   {"links", NULL, bundle_ShowLinks, LOCAL_AUTH,
779   "available link names", "show links"},
780   {"log", NULL, log_ShowLevel, LOCAL_AUTH,
781   "log levels", "show log"},
782   {"mem", NULL, mbuf_Show, LOCAL_AUTH,
783   "mbuf allocations", "show mem"},
784   {"physical", NULL, physical_ShowStatus, LOCAL_AUTH | LOCAL_CX,
785   "(low-level) link info", "show physical"},
786   {"mp", "multilink", mp_ShowStatus, LOCAL_AUTH,
787   "multilink setup", "show mp"},
788   {"proto", NULL, ShowProtocolStats, LOCAL_AUTH | LOCAL_CX_OPT,
789   "protocol summary", "show proto"},
790   {"route", NULL, route_Show, LOCAL_AUTH,
791   "routing table", "show route"},
792   {"stopped", NULL, ShowStopped, LOCAL_AUTH | LOCAL_CX,
793   "STOPPED timeout", "show stopped"},
794   {"timers", NULL, ShowTimerList, LOCAL_AUTH,
795   "alarm timers", "show timers"},
796   {"version", NULL, ShowVersion, LOCAL_NO_AUTH | LOCAL_AUTH,
797   "version string", "show version"},
798   {"who", NULL, log_ShowWho, LOCAL_AUTH,
799   "client list", "show who"},
800   {"help", "?", HelpCommand, LOCAL_NO_AUTH | LOCAL_AUTH,
801   "Display this message", "show help|? [command]", ShowCommands},
802   {NULL, NULL, NULL},
803 };
804 
805 static struct cmdtab const *
806 FindCommand(struct cmdtab const *cmds, const char *str, int *pmatch)
807 {
808   int nmatch;
809   int len;
810   struct cmdtab const *found;
811 
812   found = NULL;
813   len = strlen(str);
814   nmatch = 0;
815   while (cmds->func) {
816     if (cmds->name && strncasecmp(str, cmds->name, len) == 0) {
817       if (cmds->name[len] == '\0') {
818 	*pmatch = 1;
819 	return cmds;
820       }
821       nmatch++;
822       found = cmds;
823     } else if (cmds->alias && strncasecmp(str, cmds->alias, len) == 0) {
824       if (cmds->alias[len] == '\0') {
825 	*pmatch = 1;
826 	return cmds;
827       }
828       nmatch++;
829       found = cmds;
830     }
831     cmds++;
832   }
833   *pmatch = nmatch;
834   return found;
835 }
836 
837 static const char *
838 mkPrefix(int argc, char const *const *argv, char *tgt, int sz)
839 {
840   int f, tlen, len;
841 
842   tlen = 0;
843   for (f = 0; f < argc && tlen < sz - 2; f++) {
844     if (f)
845       tgt[tlen++] = ' ';
846     len = strlen(argv[f]);
847     if (len > sz - tlen - 1)
848       len = sz - tlen - 1;
849     strncpy(tgt+tlen, argv[f], len);
850     tlen += len;
851   }
852   tgt[tlen] = '\0';
853   return tgt;
854 }
855 
856 static int
857 FindExec(struct bundle *bundle, struct cmdtab const *cmds, int argc, int argn,
858          char const *const *argv, struct prompt *prompt, struct datalink *cx)
859 {
860   struct cmdtab const *cmd;
861   int val = 1;
862   int nmatch;
863   struct cmdargs arg;
864   char prefix[100];
865 
866   cmd = FindCommand(cmds, argv[argn], &nmatch);
867   if (nmatch > 1)
868     log_Printf(LogWARN, "%s: Ambiguous command\n",
869               mkPrefix(argn+1, argv, prefix, sizeof prefix));
870   else if (cmd && (!prompt || (cmd->lauth & prompt->auth))) {
871     if ((cmd->lauth & LOCAL_CX) && !cx)
872       /* We've got no context, but we require it */
873       cx = bundle2datalink(bundle, NULL);
874 
875     if ((cmd->lauth & LOCAL_CX) && !cx)
876       log_Printf(LogWARN, "%s: No context (use the `link' command)\n",
877                 mkPrefix(argn+1, argv, prefix, sizeof prefix));
878     else {
879       if (cx && !(cmd->lauth & (LOCAL_CX|LOCAL_CX_OPT))) {
880         log_Printf(LogWARN, "%s: Redundant context (%s) ignored\n",
881                   mkPrefix(argn+1, argv, prefix, sizeof prefix), cx->name);
882         cx = NULL;
883       }
884       arg.cmdtab = cmds;
885       arg.cmd = cmd;
886       arg.argc = argc;
887       arg.argn = argn+1;
888       arg.argv = argv;
889       arg.bundle = bundle;
890       arg.cx = cx;
891       arg.prompt = prompt;
892       val = (*cmd->func) (&arg);
893     }
894   } else
895     log_Printf(LogWARN, "%s: Invalid command\n",
896               mkPrefix(argn+1, argv, prefix, sizeof prefix));
897 
898   if (val == -1)
899     log_Printf(LogWARN, "Usage: %s\n", cmd->syntax);
900   else if (val)
901     log_Printf(LogWARN, "%s: Failed %d\n",
902               mkPrefix(argn+1, argv, prefix, sizeof prefix), val);
903 
904   return val;
905 }
906 
907 int
908 command_Interpret(char *buff, int nb, char *argv[MAXARGS])
909 {
910   char *cp;
911 
912   if (nb > 0) {
913     cp = buff + strcspn(buff, "\r\n");
914     if (cp)
915       *cp = '\0';
916     return MakeArgs(buff, argv, MAXARGS, PARSE_REDUCE);
917   }
918   return 0;
919 }
920 
921 static int
922 arghidden(int argc, char const *const *argv, int n)
923 {
924   /* Is arg n of the given command to be hidden from the log ? */
925 
926   /* set authkey xxxxx */
927   /* set key xxxxx */
928   if (n == 2 && !strncasecmp(argv[0], "se", 2) &&
929       (!strncasecmp(argv[1], "authk", 5) || !strncasecmp(argv[1], "ke", 2)))
930     return 1;
931 
932   /* passwd xxxxx */
933   if (n == 1 && !strncasecmp(argv[0], "p", 1))
934     return 1;
935 
936   /* set server port xxxxx .... */
937   if (n == 3 && !strncasecmp(argv[0], "se", 2) &&
938       !strncasecmp(argv[1], "se", 2))
939     return 1;
940 
941   return 0;
942 }
943 
944 void
945 command_Run(struct bundle *bundle, int argc, char const *const *argv,
946            struct prompt *prompt, const char *label, struct datalink *cx)
947 {
948   if (argc > 0) {
949     if (log_IsKept(LogCOMMAND)) {
950       char buf[LINE_LEN];
951       int f, n;
952 
953       if (label) {
954         strncpy(buf, label, sizeof buf - 3);
955         buf[sizeof buf - 3] = '\0';
956         strcat(buf, ": ");
957         n = strlen(buf);
958       } else {
959         *buf = '\0';
960         n = 0;
961       }
962       buf[sizeof buf - 1] = '\0';	/* In case we run out of room in buf */
963 
964       for (f = 0; f < argc; f++) {
965         if (n < sizeof buf - 1 && f)
966           buf[n++] = ' ';
967         if (arghidden(argc, argv, f))
968           strncpy(buf+n, "********", sizeof buf - n - 1);
969         else
970           strncpy(buf+n, argv[f], sizeof buf - n - 1);
971         n += strlen(buf+n);
972       }
973       log_Printf(LogCOMMAND, "%s\n", buf);
974     }
975     FindExec(bundle, Commands, argc, 0, argv, prompt, cx);
976   }
977 }
978 
979 int
980 command_Decode(struct bundle *bundle, char *buff, int nb, struct prompt *prompt,
981               const char *label)
982 {
983   int argc;
984   char *argv[MAXARGS];
985 
986   if ((argc = command_Interpret(buff, nb, argv)) < 0)
987     return 0;
988 
989   command_Run(bundle, argc, (char const *const *)argv, prompt, label, NULL);
990   return 1;
991 }
992 
993 static int
994 ShowCommand(struct cmdargs const *arg)
995 {
996   if (!arg->prompt)
997     log_Printf(LogWARN, "show: Cannot show without a prompt\n");
998   else if (arg->argc > arg->argn)
999     FindExec(arg->bundle, ShowCommands, arg->argc, arg->argn, arg->argv,
1000              arg->prompt, arg->cx);
1001   else
1002     prompt_Printf(arg->prompt, "Use ``show ?'' to get a list.\n");
1003 
1004   return 0;
1005 }
1006 
1007 static int
1008 TerminalCommand(struct cmdargs const *arg)
1009 {
1010   if (!arg->prompt) {
1011     log_Printf(LogWARN, "term: Need a prompt\n");
1012     return 1;
1013   }
1014 
1015   if (arg->cx->physical->link.lcp.fsm.state > ST_CLOSED) {
1016     prompt_Printf(arg->prompt, "LCP state is [%s]\n",
1017                   State2Nam(arg->cx->physical->link.lcp.fsm.state));
1018     return 1;
1019   }
1020 
1021   datalink_Up(arg->cx, 0, 0);
1022   prompt_TtyTermMode(arg->prompt, arg->cx);
1023   return 0;
1024 }
1025 
1026 static int
1027 QuitCommand(struct cmdargs const *arg)
1028 {
1029   if (!arg->prompt || prompt_IsController(arg->prompt) ||
1030       (arg->argc > arg->argn && !strcasecmp(arg->argv[arg->argn], "all") &&
1031        (arg->prompt->auth & LOCAL_AUTH)))
1032     Cleanup(EX_NORMAL);
1033   if (arg->prompt)
1034     prompt_Destroy(arg->prompt, 1);
1035 
1036   return 0;
1037 }
1038 
1039 static int
1040 OpenCommand(struct cmdargs const *arg)
1041 {
1042   if (arg->argc == arg->argn)
1043     bundle_Open(arg->bundle, arg->cx ? arg->cx->name : NULL, PHYS_ALL, 1);
1044   else if (arg->argc == arg->argn + 1) {
1045     if (!strcasecmp(arg->argv[arg->argn], "lcp")) {
1046       struct datalink *cx = arg->cx ?
1047         arg->cx : bundle2datalink(arg->bundle, NULL);
1048       if (cx) {
1049         if (cx->physical->link.lcp.fsm.state == ST_OPENED)
1050           fsm_Reopen(&cx->physical->link.lcp.fsm);
1051         else
1052           bundle_Open(arg->bundle, cx->name, PHYS_ALL, 1);
1053       } else
1054         log_Printf(LogWARN, "open lcp: You must specify a link\n");
1055     } else if (!strcasecmp(arg->argv[arg->argn], "ccp")) {
1056       struct fsm *fp;
1057 
1058       fp = &command_ChooseLink(arg)->ccp.fsm;
1059       if (fp->link->lcp.fsm.state != ST_OPENED)
1060         log_Printf(LogWARN, "open: LCP must be open before opening CCP\n");
1061       else if (fp->state == ST_OPENED)
1062         fsm_Reopen(fp);
1063       else {
1064         fp->open_mode = 0;	/* Not passive any more */
1065         if (fp->state == ST_STOPPED) {
1066           fsm_Down(fp);
1067           fsm_Up(fp);
1068         } else {
1069           fsm_Up(fp);
1070           fsm_Open(fp);
1071         }
1072       }
1073     } else if (!strcasecmp(arg->argv[arg->argn], "ipcp")) {
1074       if (arg->cx)
1075         log_Printf(LogWARN, "open ipcp: You need not specify a link\n");
1076       if (arg->bundle->ncp.ipcp.fsm.state == ST_OPENED)
1077         fsm_Reopen(&arg->bundle->ncp.ipcp.fsm);
1078       else
1079         bundle_Open(arg->bundle, NULL, PHYS_ALL, 1);
1080     } else
1081       return -1;
1082   } else
1083     return -1;
1084 
1085   return 0;
1086 }
1087 
1088 static int
1089 CloseCommand(struct cmdargs const *arg)
1090 {
1091   if (arg->argc == arg->argn)
1092     bundle_Close(arg->bundle, arg->cx ? arg->cx->name : NULL, CLOSE_STAYDOWN);
1093   else if (arg->argc == arg->argn + 1) {
1094     if (!strcasecmp(arg->argv[arg->argn], "lcp"))
1095       bundle_Close(arg->bundle, arg->cx ? arg->cx->name : NULL, CLOSE_LCP);
1096     else if (!strcasecmp(arg->argv[arg->argn], "ccp") ||
1097              !strcasecmp(arg->argv[arg->argn], "ccp!")) {
1098       struct fsm *fp;
1099 
1100       fp = &command_ChooseLink(arg)->ccp.fsm;
1101       if (fp->state == ST_OPENED) {
1102         fsm_Close(fp);
1103         if (arg->argv[arg->argn][3] == '!')
1104           fp->open_mode = 0;		/* Stay ST_CLOSED */
1105         else
1106           fp->open_mode = OPEN_PASSIVE;	/* Wait for the peer to start */
1107       }
1108     } else
1109       return -1;
1110   } else
1111     return -1;
1112 
1113   return 0;
1114 }
1115 
1116 static int
1117 DownCommand(struct cmdargs const *arg)
1118 {
1119   if (arg->argc == arg->argn) {
1120       if (arg->cx)
1121         datalink_Down(arg->cx, CLOSE_STAYDOWN);
1122       else
1123         bundle_Down(arg->bundle, CLOSE_STAYDOWN);
1124   } else if (arg->argc == arg->argn + 1) {
1125     if (!strcasecmp(arg->argv[arg->argn], "lcp")) {
1126       if (arg->cx)
1127         datalink_Down(arg->cx, CLOSE_LCP);
1128       else
1129         bundle_Down(arg->bundle, CLOSE_LCP);
1130     } else if (!strcasecmp(arg->argv[arg->argn], "ccp")) {
1131       struct fsm *fp = arg->cx ? &arg->cx->physical->link.ccp.fsm :
1132                                  &arg->bundle->ncp.mp.link.ccp.fsm;
1133       fsm2initial(fp);
1134     } else
1135       return -1;
1136   } else
1137     return -1;
1138 
1139   return 0;
1140 }
1141 
1142 static int
1143 SetModemSpeed(struct cmdargs const *arg)
1144 {
1145   long speed;
1146   char *end;
1147 
1148   if (arg->argc > arg->argn && *arg->argv[arg->argn]) {
1149     if (arg->argc > arg->argn+1) {
1150       log_Printf(LogWARN, "SetModemSpeed: Too many arguments\n");
1151       return -1;
1152     }
1153     if (strcasecmp(arg->argv[arg->argn], "sync") == 0) {
1154       physical_SetSync(arg->cx->physical);
1155       return 0;
1156     }
1157     end = NULL;
1158     speed = strtol(arg->argv[arg->argn], &end, 10);
1159     if (*end) {
1160       log_Printf(LogWARN, "SetModemSpeed: Bad argument \"%s\"",
1161                 arg->argv[arg->argn]);
1162       return -1;
1163     }
1164     if (physical_SetSpeed(arg->cx->physical, speed))
1165       return 0;
1166     log_Printf(LogWARN, "%s: Invalid speed\n", arg->argv[arg->argn]);
1167   } else
1168     log_Printf(LogWARN, "SetModemSpeed: No speed specified\n");
1169 
1170   return -1;
1171 }
1172 
1173 static int
1174 SetStoppedTimeout(struct cmdargs const *arg)
1175 {
1176   struct link *l = &arg->cx->physical->link;
1177 
1178   l->lcp.fsm.StoppedTimer.load = 0;
1179   l->ccp.fsm.StoppedTimer.load = 0;
1180   if (arg->argc <= arg->argn+2) {
1181     if (arg->argc > arg->argn) {
1182       l->lcp.fsm.StoppedTimer.load = atoi(arg->argv[arg->argn]) * SECTICKS;
1183       if (arg->argc > arg->argn+1)
1184         l->ccp.fsm.StoppedTimer.load = atoi(arg->argv[arg->argn+1]) * SECTICKS;
1185     }
1186     return 0;
1187   }
1188   return -1;
1189 }
1190 
1191 static int
1192 SetServer(struct cmdargs const *arg)
1193 {
1194   int res = -1;
1195 
1196   if (arg->argc > arg->argn && arg->argc < arg->argn+4) {
1197     const char *port, *passwd, *mask;
1198     int mlen;
1199 
1200     /* What's what ? */
1201     port = arg->argv[arg->argn];
1202     if (arg->argc == arg->argn + 2) {
1203       passwd = arg->argv[arg->argn+1];
1204       mask = NULL;
1205     } else if (arg->argc == arg->argn + 3) {
1206       passwd = arg->argv[arg->argn+1];
1207       mask = arg->argv[arg->argn+2];
1208       mlen = strlen(mask);
1209       if (mlen == 0 || mlen > 4 || strspn(mask, "01234567") != mlen ||
1210           (mlen == 4 && *mask != '0')) {
1211         log_Printf(LogWARN, "%s %s: %s: Invalid mask\n",
1212                    arg->argv[arg->argn - 2], arg->argv[arg->argn - 1], mask);
1213         return -1;
1214       }
1215     } else if (strcasecmp(port, "none") == 0) {
1216       if (server_Close(arg->bundle))
1217         log_Printf(LogPHASE, "Disabled server port.\n");
1218       return 0;
1219     } else
1220       return -1;
1221 
1222     strncpy(server.passwd, passwd, sizeof server.passwd - 1);
1223     server.passwd[sizeof server.passwd - 1] = '\0';
1224 
1225     if (*port == '/') {
1226       mode_t imask;
1227       char *ptr, name[LINE_LEN + 12];
1228 
1229       if (mask == NULL)
1230         imask = (mode_t)-1;
1231       else for (imask = mlen = 0; mask[mlen]; mlen++)
1232         imask = (imask * 8) + mask[mlen] - '0';
1233 
1234       ptr = strstr(port, "%d");
1235       if (ptr) {
1236         snprintf(name, sizeof name, "%.*s%d%s",
1237                  (int)(ptr - port), port, arg->bundle->unit, ptr + 2);
1238         port = name;
1239       }
1240       res = server_LocalOpen(arg->bundle, port, imask);
1241     } else {
1242       int iport, add = 0;
1243 
1244       if (mask != NULL)
1245         return -1;
1246 
1247       if (*port == '+') {
1248         port++;
1249         add = 1;
1250       }
1251       if (strspn(port, "0123456789") != strlen(port)) {
1252         struct servent *s;
1253 
1254         if ((s = getservbyname(port, "tcp")) == NULL) {
1255 	  iport = 0;
1256 	  log_Printf(LogWARN, "%s: Invalid port or service\n", port);
1257 	} else
1258 	  iport = ntohs(s->s_port);
1259       } else
1260         iport = atoi(port);
1261 
1262       if (iport) {
1263         if (add)
1264           iport += arg->bundle->unit;
1265         res = server_TcpOpen(arg->bundle, iport);
1266       } else
1267         res = -1;
1268     }
1269   }
1270 
1271   return res;
1272 }
1273 
1274 static int
1275 SetEscape(struct cmdargs const *arg)
1276 {
1277   int code;
1278   int argc = arg->argc - arg->argn;
1279   char const *const *argv = arg->argv + arg->argn;
1280 
1281   for (code = 0; code < 33; code++)
1282     arg->cx->physical->async.cfg.EscMap[code] = 0;
1283 
1284   while (argc-- > 0) {
1285     sscanf(*argv++, "%x", &code);
1286     code &= 0xff;
1287     arg->cx->physical->async.cfg.EscMap[code >> 3] |= (1 << (code & 7));
1288     arg->cx->physical->async.cfg.EscMap[32] = 1;
1289   }
1290   return 0;
1291 }
1292 
1293 static int
1294 SetInterfaceAddr(struct cmdargs const *arg)
1295 {
1296   struct ipcp *ipcp = &arg->bundle->ncp.ipcp;
1297   const char *hisaddr;
1298 
1299   if (arg->argc > arg->argn + 4)
1300     return -1;
1301 
1302   hisaddr = NULL;
1303   memset(&ipcp->cfg.my_range, '\0', sizeof ipcp->cfg.my_range);
1304   memset(&ipcp->cfg.peer_range, '\0', sizeof ipcp->cfg.peer_range);
1305   ipcp->cfg.HaveTriggerAddress = 0;
1306   ipcp->cfg.netmask.s_addr = INADDR_ANY;
1307   iplist_reset(&ipcp->cfg.peer_list);
1308 
1309   if (arg->argc > arg->argn) {
1310     if (!ParseAddr(ipcp, arg->argv[arg->argn],
1311                    &ipcp->cfg.my_range.ipaddr, &ipcp->cfg.my_range.mask,
1312                    &ipcp->cfg.my_range.width))
1313       return 1;
1314     if (arg->argc > arg->argn+1) {
1315       hisaddr = arg->argv[arg->argn+1];
1316       if (arg->argc > arg->argn+2) {
1317         ipcp->ifmask = ipcp->cfg.netmask = GetIpAddr(arg->argv[arg->argn+2]);
1318 	if (arg->argc > arg->argn+3) {
1319 	  ipcp->cfg.TriggerAddress = GetIpAddr(arg->argv[arg->argn+3]);
1320 	  ipcp->cfg.HaveTriggerAddress = 1;
1321 	}
1322       }
1323     }
1324   }
1325 
1326   /* 0.0.0.0 means any address (0 bits) */
1327   if (ipcp->cfg.my_range.ipaddr.s_addr == INADDR_ANY) {
1328     ipcp->cfg.my_range.mask.s_addr = INADDR_ANY;
1329     ipcp->cfg.my_range.width = 0;
1330   }
1331   ipcp->my_ip.s_addr = ipcp->cfg.my_range.ipaddr.s_addr;
1332   bundle_AdjustFilters(arg->bundle, &ipcp->my_ip, NULL);
1333 
1334   if (hisaddr && !ipcp_UseHisaddr(arg->bundle, hisaddr,
1335                                   arg->bundle->phys_type.all & PHYS_AUTO))
1336     return 4;
1337 
1338   return 0;
1339 }
1340 
1341 static int
1342 SetRetry(int argc, char const *const *argv, u_int *timeout, u_int *maxreq,
1343           u_int *maxtrm, int def)
1344 {
1345   if (argc == 0) {
1346     *timeout = DEF_FSMRETRY;
1347     *maxreq = def;
1348     if (maxtrm != NULL)
1349       *maxtrm = def;
1350   } else {
1351     long l = atol(argv[0]);
1352 
1353     if (l < MIN_FSMRETRY) {
1354       log_Printf(LogWARN, "%ld: Invalid FSM retry period - min %d\n",
1355                  l, MIN_FSMRETRY);
1356       return 1;
1357     } else
1358       *timeout = l;
1359 
1360     if (argc > 1) {
1361       l = atol(argv[1]);
1362       if (l < 1) {
1363         log_Printf(LogWARN, "%ld: Invalid FSM REQ tries - changed to 1\n", l);
1364         l = 1;
1365       }
1366       *maxreq = l;
1367 
1368       if (argc > 2 && maxtrm != NULL) {
1369         l = atol(argv[2]);
1370         if (l < 1) {
1371           log_Printf(LogWARN, "%ld: Invalid FSM TRM tries - changed to 1\n", l);
1372           l = 1;
1373         }
1374         *maxtrm = l;
1375       }
1376     }
1377   }
1378 
1379   return 0;
1380 }
1381 
1382 static int
1383 SetVariable(struct cmdargs const *arg)
1384 {
1385   long long_val, param = (long)arg->cmd->args;
1386   int mode, dummyint, f, first;
1387   const char *argp;
1388   struct datalink *cx = arg->cx;	/* LOCAL_CX uses this */
1389   const char *err = NULL;
1390   struct link *l = command_ChooseLink(arg);	/* LOCAL_CX_OPT uses this */
1391   struct in_addr dummyaddr, *addr;
1392 
1393   if (arg->argc > arg->argn)
1394     argp = arg->argv[arg->argn];
1395   else
1396     argp = "";
1397 
1398   if ((arg->cmd->lauth & LOCAL_CX) && !cx) {
1399     log_Printf(LogWARN, "set %s: No context (use the `link' command)\n",
1400               arg->cmd->name);
1401     return 1;
1402   } else if (cx && !(arg->cmd->lauth & (LOCAL_CX|LOCAL_CX_OPT))) {
1403     log_Printf(LogWARN, "set %s: Redundant context (%s) ignored\n",
1404               arg->cmd->name, cx->name);
1405     cx = NULL;
1406   }
1407 
1408   switch (param) {
1409   case VAR_AUTHKEY:
1410     strncpy(arg->bundle->cfg.auth.key, argp,
1411             sizeof arg->bundle->cfg.auth.key - 1);
1412     arg->bundle->cfg.auth.key[sizeof arg->bundle->cfg.auth.key - 1] = '\0';
1413     break;
1414 
1415   case VAR_AUTHNAME:
1416     switch (bundle_Phase(arg->bundle)) {
1417       case PHASE_DEAD:
1418       case PHASE_ESTABLISH:
1419         strncpy(arg->bundle->cfg.auth.name, argp,
1420                 sizeof arg->bundle->cfg.auth.name - 1);
1421         arg->bundle->cfg.auth.name[sizeof arg->bundle->cfg.auth.name-1] = '\0';
1422         break;
1423       default:
1424         err = "set authname: Only available at phase DEAD/ESTABLISH\n";
1425         log_Printf(LogWARN, err);
1426         break;
1427     }
1428     break;
1429 
1430   case VAR_AUTOLOAD:
1431     if (arg->argc == arg->argn + 3) {
1432       int v1, v2, v3;
1433       char *end;
1434 
1435       v1 = strtol(arg->argv[arg->argn], &end, 0);
1436       if (v1 < 0 || *end) {
1437         log_Printf(LogWARN, "autoload: %s: Invalid min percentage\n",
1438                    arg->argv[arg->argn]);
1439         return 1;
1440       }
1441 
1442       v2 = strtol(arg->argv[arg->argn + 1], &end, 0);
1443       if (v2 < 0 || *end) {
1444         log_Printf(LogWARN, "autoload: %s: Invalid max percentage\n",
1445                    arg->argv[arg->argn + 1]);
1446         return 1;
1447       }
1448       if (v2 < v1) {
1449         v3 = v1;
1450         v1 = v2;
1451         v2 = v3;
1452       }
1453 
1454       v3 = strtol(arg->argv[arg->argn + 2], &end, 0);
1455       if (v3 <= 0 || *end) {
1456         log_Printf(LogWARN, "autoload: %s: Invalid throughput period\n",
1457                    arg->argv[arg->argn + 2]);
1458         return 1;
1459       }
1460 
1461       arg->bundle->ncp.mp.cfg.autoload.min = v1;
1462       arg->bundle->ncp.mp.cfg.autoload.max = v2;
1463       arg->bundle->ncp.mp.cfg.autoload.period = v3;
1464       mp_RestartAutoloadTimer(&arg->bundle->ncp.mp);
1465     } else {
1466       err = "Set autoload requires three arguments\n";
1467       log_Printf(LogWARN, err);
1468     }
1469     break;
1470 
1471   case VAR_DIAL:
1472     strncpy(cx->cfg.script.dial, argp, sizeof cx->cfg.script.dial - 1);
1473     cx->cfg.script.dial[sizeof cx->cfg.script.dial - 1] = '\0';
1474     break;
1475 
1476   case VAR_LOGIN:
1477     strncpy(cx->cfg.script.login, argp, sizeof cx->cfg.script.login - 1);
1478     cx->cfg.script.login[sizeof cx->cfg.script.login - 1] = '\0';
1479     break;
1480 
1481   case VAR_WINSIZE:
1482     if (arg->argc > arg->argn) {
1483       l->ccp.cfg.deflate.out.winsize = atoi(arg->argv[arg->argn]);
1484       if (l->ccp.cfg.deflate.out.winsize < 8 ||
1485           l->ccp.cfg.deflate.out.winsize > 15) {
1486           log_Printf(LogWARN, "%d: Invalid outgoing window size\n",
1487                     l->ccp.cfg.deflate.out.winsize);
1488           l->ccp.cfg.deflate.out.winsize = 15;
1489       }
1490       if (arg->argc > arg->argn+1) {
1491         l->ccp.cfg.deflate.in.winsize = atoi(arg->argv[arg->argn+1]);
1492         if (l->ccp.cfg.deflate.in.winsize < 8 ||
1493             l->ccp.cfg.deflate.in.winsize > 15) {
1494             log_Printf(LogWARN, "%d: Invalid incoming window size\n",
1495                       l->ccp.cfg.deflate.in.winsize);
1496             l->ccp.cfg.deflate.in.winsize = 15;
1497         }
1498       } else
1499         l->ccp.cfg.deflate.in.winsize = 0;
1500     } else {
1501       err = "No window size specified\n";
1502       log_Printf(LogWARN, err);
1503     }
1504     break;
1505 
1506   case VAR_DEVICE:
1507     physical_SetDeviceList(cx->physical, arg->argc - arg->argn,
1508                            arg->argv + arg->argn);
1509     break;
1510 
1511   case VAR_ACCMAP:
1512     if (arg->argc > arg->argn) {
1513       u_long ulong_val;
1514       sscanf(argp, "%lx", &ulong_val);
1515       cx->physical->link.lcp.cfg.accmap = (u_int32_t)ulong_val;
1516     } else {
1517       err = "No accmap specified\n";
1518       log_Printf(LogWARN, err);
1519     }
1520     break;
1521 
1522   case VAR_MODE:
1523     mode = Nam2mode(argp);
1524     if (mode == PHYS_NONE || mode == PHYS_ALL) {
1525       log_Printf(LogWARN, "%s: Invalid mode\n", argp);
1526       return -1;
1527     }
1528     bundle_SetMode(arg->bundle, cx, mode);
1529     break;
1530 
1531   case VAR_MRRU:
1532     switch (bundle_Phase(arg->bundle)) {
1533       case PHASE_DEAD:
1534         break;
1535       case PHASE_ESTABLISH:
1536         /* Make sure none of our links are DATALINK_LCP or greater */
1537         if (bundle_HighestState(arg->bundle) >= DATALINK_LCP) {
1538           log_Printf(LogWARN, "mrru: Only changable before LCP negotiations\n");
1539           return 1;
1540         }
1541         break;
1542       default:
1543         log_Printf(LogWARN, "mrru: Only changable at phase DEAD/ESTABLISH\n");
1544         return 1;
1545     }
1546     long_val = atol(argp);
1547     if (long_val && long_val < MIN_MRU) {
1548       log_Printf(LogWARN, "MRRU %ld: too small - min %d\n", long_val, MIN_MRU);
1549       return 1;
1550     } else if (long_val > MAX_MRU) {
1551       log_Printf(LogWARN, "MRRU %ld: too big - max %d\n", long_val, MAX_MRU);
1552       return 1;
1553     } else
1554       arg->bundle->ncp.mp.cfg.mrru = long_val;
1555     break;
1556 
1557   case VAR_MRU:
1558     long_val = atol(argp);
1559     if (long_val == 0)
1560       l->lcp.cfg.mru = DEF_MRU;
1561     else if (long_val < MIN_MRU) {
1562       log_Printf(LogWARN, "MRU %ld: too small - min %d\n", long_val, MIN_MRU);
1563       return 1;
1564     } else if (long_val > MAX_MRU) {
1565       log_Printf(LogWARN, "MRU %ld: too big - max %d\n", long_val, MAX_MRU);
1566       return 1;
1567     } else
1568       l->lcp.cfg.mru = long_val;
1569     break;
1570 
1571   case VAR_MTU:
1572     long_val = atol(argp);
1573     if (long_val && long_val < MIN_MTU) {
1574       log_Printf(LogWARN, "MTU %ld: too small - min %d\n", long_val, MIN_MTU);
1575       return 1;
1576     } else if (long_val > MAX_MTU) {
1577       log_Printf(LogWARN, "MTU %ld: too big - max %d\n", long_val, MAX_MTU);
1578       return 1;
1579     } else
1580       arg->bundle->cfg.mtu = long_val;
1581     break;
1582 
1583   case VAR_OPENMODE:
1584     if (strcasecmp(argp, "active") == 0)
1585       cx->physical->link.lcp.cfg.openmode = arg->argc > arg->argn+1 ?
1586         atoi(arg->argv[arg->argn+1]) : 1;
1587     else if (strcasecmp(argp, "passive") == 0)
1588       cx->physical->link.lcp.cfg.openmode = OPEN_PASSIVE;
1589     else {
1590       err = "%s: Invalid openmode\n";
1591       log_Printf(LogWARN, err, argp);
1592     }
1593     break;
1594 
1595   case VAR_PHONE:
1596     strncpy(cx->cfg.phone.list, argp, sizeof cx->cfg.phone.list - 1);
1597     cx->cfg.phone.list[sizeof cx->cfg.phone.list - 1] = '\0';
1598     cx->phone.alt = cx->phone.next = NULL;
1599     break;
1600 
1601   case VAR_HANGUP:
1602     strncpy(cx->cfg.script.hangup, argp, sizeof cx->cfg.script.hangup - 1);
1603     cx->cfg.script.hangup[sizeof cx->cfg.script.hangup - 1] = '\0';
1604     break;
1605 
1606   case VAR_LOGOUT:
1607     strncpy(cx->cfg.script.logout, argp, sizeof cx->cfg.script.logout - 1);
1608     cx->cfg.script.logout[sizeof cx->cfg.script.logout - 1] = '\0';
1609     break;
1610 
1611   case VAR_IDLETIMEOUT:
1612     if (arg->argc > arg->argn+2)
1613       err = "Too many idle timeout values\n";
1614     else if (arg->argc == arg->argn)
1615       err = "Too few idle timeout values\n";
1616     else {
1617       int timeout, min;
1618 
1619       timeout = atoi(argp);
1620       min = arg->argc == arg->argn + 2 ? atoi(arg->argv[arg->argn + 1]) : -1;
1621       bundle_SetIdleTimer(arg->bundle, timeout, min);
1622     }
1623     if (err)
1624       log_Printf(LogWARN, err);
1625     break;
1626 
1627   case VAR_LQRPERIOD:
1628     long_val = atol(argp);
1629     if (long_val < MIN_LQRPERIOD) {
1630       log_Printf(LogWARN, "%ld: Invalid lqr period - min %d\n",
1631                  long_val, MIN_LQRPERIOD);
1632       return 1;
1633     } else
1634       l->lcp.cfg.lqrperiod = long_val;
1635     break;
1636 
1637   case VAR_LCPRETRY:
1638     return SetRetry(arg->argc - arg->argn, arg->argv + arg->argn,
1639                     &cx->physical->link.lcp.cfg.fsm.timeout,
1640                     &cx->physical->link.lcp.cfg.fsm.maxreq,
1641                     &cx->physical->link.lcp.cfg.fsm.maxtrm, DEF_FSMTRIES);
1642     break;
1643 
1644   case VAR_CHAPRETRY:
1645     return SetRetry(arg->argc - arg->argn, arg->argv + arg->argn,
1646                     &cx->chap.auth.cfg.fsm.timeout,
1647                     &cx->chap.auth.cfg.fsm.maxreq, NULL, DEF_FSMAUTHTRIES);
1648     break;
1649 
1650   case VAR_PAPRETRY:
1651     return SetRetry(arg->argc - arg->argn, arg->argv + arg->argn,
1652                     &cx->pap.cfg.fsm.timeout, &cx->pap.cfg.fsm.maxreq,
1653                     NULL, DEF_FSMAUTHTRIES);
1654     break;
1655 
1656   case VAR_CCPRETRY:
1657     return SetRetry(arg->argc - arg->argn, arg->argv + arg->argn,
1658                     &l->ccp.cfg.fsm.timeout, &l->ccp.cfg.fsm.maxreq,
1659                     &l->ccp.cfg.fsm.maxtrm, DEF_FSMTRIES);
1660     break;
1661 
1662   case VAR_IPCPRETRY:
1663     return SetRetry(arg->argc - arg->argn, arg->argv + arg->argn,
1664                     &arg->bundle->ncp.ipcp.cfg.fsm.timeout,
1665                     &arg->bundle->ncp.ipcp.cfg.fsm.maxreq,
1666                     &arg->bundle->ncp.ipcp.cfg.fsm.maxtrm, DEF_FSMTRIES);
1667     break;
1668 
1669   case VAR_NBNS:
1670   case VAR_DNS:
1671     if (param == VAR_DNS)
1672       addr = arg->bundle->ncp.ipcp.cfg.ns.dns;
1673     else
1674       addr = arg->bundle->ncp.ipcp.cfg.ns.nbns;
1675 
1676     addr[0].s_addr = addr[1].s_addr = INADDR_ANY;
1677 
1678     if (arg->argc > arg->argn) {
1679       ParseAddr(&arg->bundle->ncp.ipcp, arg->argv[arg->argn],
1680                 addr, &dummyaddr, &dummyint);
1681       if (arg->argc > arg->argn+1)
1682         ParseAddr(&arg->bundle->ncp.ipcp, arg->argv[arg->argn + 1],
1683                   addr + 1, &dummyaddr, &dummyint);
1684 
1685       if (addr[1].s_addr == INADDR_ANY)
1686         addr[1].s_addr = addr[0].s_addr;
1687       if (addr[0].s_addr == INADDR_ANY)
1688         addr[0].s_addr = addr[1].s_addr;
1689     }
1690     break;
1691 
1692   case VAR_CALLBACK:
1693     cx->cfg.callback.opmask = 0;
1694     for (dummyint = arg->argn; dummyint < arg->argc; dummyint++) {
1695       if (!strcasecmp(arg->argv[dummyint], "auth"))
1696         cx->cfg.callback.opmask |= CALLBACK_BIT(CALLBACK_AUTH);
1697       else if (!strcasecmp(arg->argv[dummyint], "cbcp"))
1698         cx->cfg.callback.opmask |= CALLBACK_BIT(CALLBACK_CBCP);
1699       else if (!strcasecmp(arg->argv[dummyint], "e.164")) {
1700         if (dummyint == arg->argc - 1)
1701           log_Printf(LogWARN, "No E.164 arg (E.164 ignored) !\n");
1702         else {
1703           cx->cfg.callback.opmask |= CALLBACK_BIT(CALLBACK_E164);
1704           strncpy(cx->cfg.callback.msg, arg->argv[++dummyint],
1705                   sizeof cx->cfg.callback.msg - 1);
1706           cx->cfg.callback.msg[sizeof cx->cfg.callback.msg - 1] = '\0';
1707         }
1708       } else if (!strcasecmp(arg->argv[dummyint], "none"))
1709         cx->cfg.callback.opmask |= CALLBACK_BIT(CALLBACK_NONE);
1710       else
1711         return -1;
1712     }
1713     if (cx->cfg.callback.opmask == CALLBACK_BIT(CALLBACK_NONE))
1714       cx->cfg.callback.opmask = 0;
1715     break;
1716 
1717   case VAR_CBCP:
1718     cx->cfg.cbcp.delay = 0;
1719     *cx->cfg.cbcp.phone = '\0';
1720     cx->cfg.cbcp.fsmretry = DEF_FSMRETRY;
1721     if (arg->argc > arg->argn) {
1722       strncpy(cx->cfg.cbcp.phone, arg->argv[arg->argn],
1723               sizeof cx->cfg.cbcp.phone - 1);
1724       cx->cfg.cbcp.phone[sizeof cx->cfg.cbcp.phone - 1] = '\0';
1725       if (arg->argc > arg->argn + 1) {
1726         cx->cfg.cbcp.delay = atoi(arg->argv[arg->argn + 1]);
1727         if (arg->argc > arg->argn + 2) {
1728           long_val = atol(arg->argv[arg->argn + 2]);
1729           if (long_val < MIN_FSMRETRY)
1730             log_Printf(LogWARN, "%ld: Invalid CBCP FSM retry period - min %d\n",
1731                        long_val, MIN_FSMRETRY);
1732           else
1733             cx->cfg.cbcp.fsmretry = long_val;
1734         }
1735       }
1736     }
1737     break;
1738 
1739   case VAR_CHOKED:
1740     arg->bundle->cfg.choked.timeout = atoi(argp);
1741     if (arg->bundle->cfg.choked.timeout <= 0)
1742       arg->bundle->cfg.choked.timeout = CHOKED_TIMEOUT;
1743     break;
1744 
1745   case VAR_SENDPIPE:
1746     long_val = atol(argp);
1747     arg->bundle->ncp.ipcp.cfg.sendpipe = long_val;
1748     break;
1749 
1750   case VAR_RECVPIPE:
1751     long_val = atol(argp);
1752     arg->bundle->ncp.ipcp.cfg.recvpipe = long_val;
1753     break;
1754 
1755 #ifndef NORADIUS
1756   case VAR_RADIUS:
1757     if (!*argp)
1758       *arg->bundle->radius.cfg.file = '\0';
1759     else if (access(argp, R_OK)) {
1760       log_Printf(LogWARN, "%s: %s\n", argp, strerror(errno));
1761       return 1;
1762     } else {
1763       strncpy(arg->bundle->radius.cfg.file, argp,
1764               sizeof arg->bundle->radius.cfg.file - 1);
1765       arg->bundle->radius.cfg.file
1766         [sizeof arg->bundle->radius.cfg.file - 1] = '\0';
1767     }
1768     break;
1769 #endif
1770 
1771   case VAR_CD:
1772     if (*argp) {
1773       if (strcasecmp(argp, "off")) {
1774         long_val = atol(argp);
1775         if (long_val < 0)
1776           long_val = 0;
1777         cx->physical->cfg.cd.delay = long_val;
1778         cx->physical->cfg.cd.necessity = argp[strlen(argp)-1] == '!' ?
1779           CD_REQUIRED : CD_VARIABLE;
1780       } else
1781         cx->physical->cfg.cd.necessity = CD_NOTREQUIRED;
1782     } else {
1783       cx->physical->cfg.cd.delay = 0;
1784       cx->physical->cfg.cd.necessity = CD_DEFAULT;
1785     }
1786     break;
1787 
1788   case VAR_PARITY:
1789     if (arg->argc == arg->argn + 1)
1790       return physical_SetParity(arg->cx->physical, argp);
1791     else {
1792       err = "Parity value must be odd, even or none\n";
1793       log_Printf(LogWARN, err);
1794     }
1795     break;
1796 
1797   case VAR_CRTSCTS:
1798     if (strcasecmp(argp, "on") == 0)
1799       physical_SetRtsCts(arg->cx->physical, 1);
1800     else if (strcasecmp(argp, "off") == 0)
1801       physical_SetRtsCts(arg->cx->physical, 0);
1802     else {
1803       err = "RTS/CTS value must be on or off\n";
1804       log_Printf(LogWARN, err);
1805     }
1806     break;
1807 
1808   case VAR_URGENTPORTS:
1809     if (arg->argn == arg->argc) {
1810       ipcp_ClearUrgentTcpPorts(&arg->bundle->ncp.ipcp);
1811       ipcp_ClearUrgentUdpPorts(&arg->bundle->ncp.ipcp);
1812     } else if (!strcasecmp(arg->argv[arg->argn], "udp")) {
1813       if (arg->argn == arg->argc - 1)
1814         ipcp_ClearUrgentUdpPorts(&arg->bundle->ncp.ipcp);
1815       else for (f = arg->argn + 1; f < arg->argc; f++)
1816         if (*arg->argv[f] == '+')
1817           ipcp_AddUrgentUdpPort(&arg->bundle->ncp.ipcp, atoi(arg->argv[f] + 1));
1818         else if (*arg->argv[f] == '-')
1819           ipcp_RemoveUrgentUdpPort(&arg->bundle->ncp.ipcp,
1820                                    atoi(arg->argv[f] + 1));
1821         else {
1822           if (f == arg->argn)
1823             ipcp_ClearUrgentUdpPorts(&arg->bundle->ncp.ipcp);
1824           ipcp_AddUrgentUdpPort(&arg->bundle->ncp.ipcp, atoi(arg->argv[f]));
1825         }
1826     } else {
1827       first = arg->argn;
1828       if (!strcasecmp(arg->argv[first], "tcp") && ++first == arg->argc)
1829         ipcp_ClearUrgentTcpPorts(&arg->bundle->ncp.ipcp);
1830 
1831       for (f = first; f < arg->argc; f++)
1832         if (*arg->argv[f] == '+')
1833           ipcp_AddUrgentTcpPort(&arg->bundle->ncp.ipcp, atoi(arg->argv[f] + 1));
1834         else if (*arg->argv[f] == '-')
1835           ipcp_RemoveUrgentTcpPort(&arg->bundle->ncp.ipcp,
1836                                    atoi(arg->argv[f] + 1));
1837         else {
1838           if (f == first)
1839             ipcp_ClearUrgentTcpPorts(&arg->bundle->ncp.ipcp);
1840           ipcp_AddUrgentTcpPort(&arg->bundle->ncp.ipcp, atoi(arg->argv[f]));
1841         }
1842     }
1843     break;
1844   }
1845 
1846   return err ? 1 : 0;
1847 }
1848 
1849 static struct cmdtab const SetCommands[] = {
1850   {"accmap", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX,
1851   "accmap value", "set accmap hex-value", (const void *)VAR_ACCMAP},
1852   {"authkey", "key", SetVariable, LOCAL_AUTH,
1853   "authentication key", "set authkey|key key", (const void *)VAR_AUTHKEY},
1854   {"authname", NULL, SetVariable, LOCAL_AUTH,
1855   "authentication name", "set authname name", (const void *)VAR_AUTHNAME},
1856   {"autoload", NULL, SetVariable, LOCAL_AUTH,
1857   "auto link [de]activation", "set autoload maxtime maxload mintime minload",
1858   (const void *)VAR_AUTOLOAD},
1859   {"bandwidth", NULL, mp_SetDatalinkBandwidth, LOCAL_AUTH | LOCAL_CX,
1860   "datalink bandwidth", "set bandwidth value"},
1861   {"callback", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX,
1862   "callback control", "set callback [none|auth|cbcp|"
1863   "E.164 *|number[,number]...]...", (const void *)VAR_CALLBACK},
1864   {"cbcp", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX,
1865   "CBCP control", "set cbcp [*|phone[,phone...] [delay [timeout]]]",
1866   (const void *)VAR_CBCP},
1867   {"ccpretry", "ccpretries", SetVariable, LOCAL_AUTH | LOCAL_CX_OPT,
1868    "CCP retries", "set ccpretry value [attempts]", (const void *)VAR_CCPRETRY},
1869   {"cd", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX, "Carrier delay requirement",
1870    "set cd value[!]", (const void *)VAR_CD},
1871   {"chapretry", "chapretries", SetVariable, LOCAL_AUTH | LOCAL_CX,
1872    "CHAP retries", "set chapretry value [attempts]",
1873    (const void *)VAR_CHAPRETRY},
1874   {"choked", NULL, SetVariable, LOCAL_AUTH,
1875   "choked timeout", "set choked [secs]", (const void *)VAR_CHOKED},
1876   {"ctsrts", "crtscts", SetVariable, LOCAL_AUTH | LOCAL_CX,
1877    "Use hardware flow control", "set ctsrts [on|off]",
1878    (const char *)VAR_CRTSCTS},
1879   {"deflate", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX_OPT,
1880   "deflate window sizes", "set deflate out-winsize in-winsize",
1881   (const void *) VAR_WINSIZE},
1882   {"device", "line", SetVariable, LOCAL_AUTH | LOCAL_CX,
1883   "physical device name", "set device|line device-name[,device-name]",
1884   (const void *) VAR_DEVICE},
1885   {"dial", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX,
1886   "dialing script", "set dial chat-script", (const void *) VAR_DIAL},
1887   {"dns", NULL, SetVariable, LOCAL_AUTH, "Domain Name Server",
1888   "set dns pri-addr [sec-addr]", (const void *)VAR_DNS},
1889   {"enddisc", NULL, mp_SetEnddisc, LOCAL_AUTH,
1890   "Endpoint Discriminator", "set enddisc [IP|magic|label|psn value]"},
1891   {"escape", NULL, SetEscape, LOCAL_AUTH | LOCAL_CX,
1892   "escape characters", "set escape hex-digit ..."},
1893   {"filter", NULL, filter_Set, LOCAL_AUTH,
1894   "packet filters", "set filter alive|dial|in|out rule-no permit|deny "
1895   "[src_addr[/width]] [dst_addr[/width]] [tcp|udp|icmp|ospf|igmp "
1896   "[src [lt|eq|gt port]] [dst [lt|eq|gt port]] [estab] [syn] [finrst]]"},
1897   {"hangup", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX,
1898   "hangup script", "set hangup chat-script", (const void *) VAR_HANGUP},
1899   {"ifaddr", NULL, SetInterfaceAddr, LOCAL_AUTH, "destination address",
1900   "set ifaddr [src-addr [dst-addr [netmask [trg-addr]]]]"},
1901   {"ipcpretry", "ipcpretries", SetVariable, LOCAL_AUTH, "IPCP retries",
1902    "set ipcpretry value [attempts]", (const void *)VAR_IPCPRETRY},
1903   {"lcpretry", "lcpretries", SetVariable, LOCAL_AUTH | LOCAL_CX, "LCP retries",
1904    "set lcpretry value [attempts]", (const void *)VAR_LCPRETRY},
1905   {"log", NULL, log_SetLevel, LOCAL_AUTH, "log level",
1906   "set log [local] [+|-]async|cbcp|ccp|chat|command|connect|debug|hdlc|id0|"
1907   "ipcp|lcp|lqm|phase|physical|sync|tcp/ip|timer|tun..."},
1908   {"login", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX,
1909   "login script", "set login chat-script", (const void *) VAR_LOGIN},
1910   {"logout", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX,
1911   "logout script", "set logout chat-script", (const void *) VAR_LOGOUT},
1912   {"lqrperiod", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX_OPT,
1913   "LQR period", "set lqrperiod value", (const void *)VAR_LQRPERIOD},
1914   {"mode", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX, "mode value",
1915   "set mode interactive|auto|ddial|background", (const void *)VAR_MODE},
1916   {"mrru", NULL, SetVariable, LOCAL_AUTH, "MRRU value",
1917   "set mrru value", (const void *)VAR_MRRU},
1918   {"mru", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX_OPT,
1919   "MRU value", "set mru value", (const void *)VAR_MRU},
1920   {"mtu", NULL, SetVariable, LOCAL_AUTH,
1921   "interface MTU value", "set mtu value", (const void *)VAR_MTU},
1922   {"nbns", NULL, SetVariable, LOCAL_AUTH, "NetBIOS Name Server",
1923   "set nbns pri-addr [sec-addr]", (const void *)VAR_NBNS},
1924   {"openmode", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX, "open mode",
1925   "set openmode active|passive [secs]", (const void *)VAR_OPENMODE},
1926   {"papretry", "papretries", SetVariable, LOCAL_AUTH | LOCAL_CX, "PAP retries",
1927    "set papretry value [attempts]", (const void *)VAR_PAPRETRY},
1928   {"parity", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX, "serial parity",
1929    "set parity [odd|even|none]", (const void *)VAR_PARITY},
1930   {"phone", NULL, SetVariable, LOCAL_AUTH | LOCAL_CX, "telephone number(s)",
1931   "set phone phone1[:phone2[...]]", (const void *)VAR_PHONE},
1932   {"proctitle", "title", SetProcTitle, LOCAL_AUTH,
1933   "Process title", "set proctitle [value]"},
1934 #ifndef NORADIUS
1935   {"radius", NULL, SetVariable, LOCAL_AUTH,
1936   "RADIUS Config", "set radius cfgfile", (const void *)VAR_RADIUS},
1937 #endif
1938   {"reconnect", NULL, datalink_SetReconnect, LOCAL_AUTH | LOCAL_CX,
1939   "Reconnect timeout", "set reconnect value ntries"},
1940   {"recvpipe", NULL, SetVariable, LOCAL_AUTH,
1941   "RECVPIPE value", "set recvpipe value", (const void *)VAR_RECVPIPE},
1942   {"redial", NULL, datalink_SetRedial, LOCAL_AUTH | LOCAL_CX,
1943   "Redial timeout", "set redial secs[+inc[-incmax]][.next] [attempts]"},
1944   {"sendpipe", NULL, SetVariable, LOCAL_AUTH,
1945   "SENDPIPE value", "set sendpipe value", (const void *)VAR_SENDPIPE},
1946   {"server", "socket", SetServer, LOCAL_AUTH,
1947   "server port", "set server|socket TcpPort|LocalName|none password [mask]"},
1948   {"speed", NULL, SetModemSpeed, LOCAL_AUTH | LOCAL_CX,
1949   "physical speed", "set speed value|sync"},
1950   {"stopped", NULL, SetStoppedTimeout, LOCAL_AUTH | LOCAL_CX,
1951   "STOPPED timeouts", "set stopped [LCPseconds [CCPseconds]]"},
1952   {"timeout", NULL, SetVariable, LOCAL_AUTH, "Idle timeout",
1953   "set timeout idletime", (const void *)VAR_IDLETIMEOUT},
1954   {"urgent", NULL, SetVariable, LOCAL_AUTH, "urgent ports",
1955   "set urgent [tcp|udp] [+|-]port...", (const void *)VAR_URGENTPORTS},
1956   {"vj", NULL, ipcp_vjset, LOCAL_AUTH,
1957   "vj values", "set vj slots|slotcomp [value]"},
1958   {"help", "?", HelpCommand, LOCAL_AUTH | LOCAL_NO_AUTH,
1959   "Display this message", "set help|? [command]", SetCommands},
1960   {NULL, NULL, NULL},
1961 };
1962 
1963 static int
1964 SetCommand(struct cmdargs const *arg)
1965 {
1966   if (arg->argc > arg->argn)
1967     FindExec(arg->bundle, SetCommands, arg->argc, arg->argn, arg->argv,
1968              arg->prompt, arg->cx);
1969   else if (arg->prompt)
1970     prompt_Printf(arg->prompt, "Use `set ?' to get a list or `set ? <var>' for"
1971 	    " syntax help.\n");
1972   else
1973     log_Printf(LogWARN, "set command must have arguments\n");
1974 
1975   return 0;
1976 }
1977 
1978 static int
1979 AddCommand(struct cmdargs const *arg)
1980 {
1981   struct in_addr dest, gateway, netmask;
1982   int gw, addrs;
1983 
1984   if (arg->argc != arg->argn+3 && arg->argc != arg->argn+2)
1985     return -1;
1986 
1987   addrs = 0;
1988   if (arg->argc == arg->argn+2) {
1989     if (!strcasecmp(arg->argv[arg->argn], "default"))
1990       dest.s_addr = netmask.s_addr = INADDR_ANY;
1991     else {
1992       int width;
1993 
1994       if (!ParseAddr(&arg->bundle->ncp.ipcp, arg->argv[arg->argn],
1995 	             &dest, &netmask, &width))
1996         return -1;
1997       if (!strncasecmp(arg->argv[arg->argn], "MYADDR", 6))
1998         addrs = ROUTE_DSTMYADDR;
1999       else if (!strncasecmp(arg->argv[arg->argn], "HISADDR", 7))
2000         addrs = ROUTE_DSTHISADDR;
2001     }
2002     gw = 1;
2003   } else {
2004     if (strcasecmp(arg->argv[arg->argn], "MYADDR") == 0) {
2005       addrs = ROUTE_DSTMYADDR;
2006       dest = arg->bundle->ncp.ipcp.my_ip;
2007     } else if (strcasecmp(arg->argv[arg->argn], "HISADDR") == 0) {
2008       addrs = ROUTE_DSTHISADDR;
2009       dest = arg->bundle->ncp.ipcp.peer_ip;
2010     } else
2011       dest = GetIpAddr(arg->argv[arg->argn]);
2012     netmask = GetIpAddr(arg->argv[arg->argn+1]);
2013     gw = 2;
2014   }
2015 
2016   if (strcasecmp(arg->argv[arg->argn+gw], "HISADDR") == 0) {
2017     gateway = arg->bundle->ncp.ipcp.peer_ip;
2018     addrs |= ROUTE_GWHISADDR;
2019   } else
2020     gateway = GetIpAddr(arg->argv[arg->argn+gw]);
2021 
2022   if (bundle_SetRoute(arg->bundle, RTM_ADD, dest, gateway, netmask,
2023                   arg->cmd->args ? 1 : 0, (addrs & ROUTE_GWHISADDR) ? 1 : 0)
2024       && addrs != ROUTE_STATIC)
2025     route_Add(&arg->bundle->ncp.ipcp.route, addrs, dest, netmask, gateway);
2026 
2027   return 0;
2028 }
2029 
2030 static int
2031 DeleteCommand(struct cmdargs const *arg)
2032 {
2033   struct in_addr dest, none;
2034   int addrs;
2035 
2036   if (arg->argc == arg->argn+1) {
2037     if(strcasecmp(arg->argv[arg->argn], "all") == 0) {
2038       route_IfDelete(arg->bundle, 0);
2039       route_DeleteAll(&arg->bundle->ncp.ipcp.route);
2040     } else {
2041       addrs = 0;
2042       if (strcasecmp(arg->argv[arg->argn], "MYADDR") == 0) {
2043         dest = arg->bundle->ncp.ipcp.my_ip;
2044         addrs = ROUTE_DSTMYADDR;
2045       } else if (strcasecmp(arg->argv[arg->argn], "HISADDR") == 0) {
2046         dest = arg->bundle->ncp.ipcp.peer_ip;
2047         addrs = ROUTE_DSTHISADDR;
2048       } else {
2049         dest = GetIpAddr(arg->argv[arg->argn]);
2050         if (dest.s_addr == INADDR_NONE) {
2051           log_Printf(LogWARN, "%s: Invalid IP address\n", arg->argv[arg->argn]);
2052           return -1;
2053         }
2054         addrs = ROUTE_STATIC;
2055       }
2056       none.s_addr = INADDR_ANY;
2057       bundle_SetRoute(arg->bundle, RTM_DELETE, dest, none, none,
2058                       arg->cmd->args ? 1 : 0, 0);
2059       route_Delete(&arg->bundle->ncp.ipcp.route, addrs, dest);
2060     }
2061   } else
2062     return -1;
2063 
2064   return 0;
2065 }
2066 
2067 #ifndef NONAT
2068 static int
2069 AliasEnable(struct cmdargs const *arg)
2070 {
2071   if (arg->argc == arg->argn+1) {
2072     if (strcasecmp(arg->argv[arg->argn], "yes") == 0) {
2073       if (!arg->bundle->NatEnabled) {
2074         if (arg->bundle->ncp.ipcp.fsm.state == ST_OPENED)
2075           PacketAliasSetAddress(arg->bundle->ncp.ipcp.my_ip);
2076         arg->bundle->NatEnabled = 1;
2077       }
2078       return 0;
2079     } else if (strcasecmp(arg->argv[arg->argn], "no") == 0) {
2080       arg->bundle->NatEnabled = 0;
2081       arg->bundle->cfg.opt &= ~OPT_IFACEALIAS;
2082       /* Don't iface_Clear() - there may be manually configured addresses */
2083       return 0;
2084     }
2085   }
2086 
2087   return -1;
2088 }
2089 
2090 
2091 static int
2092 AliasOption(struct cmdargs const *arg)
2093 {
2094   long param = (long)arg->cmd->args;
2095 
2096   if (arg->argc == arg->argn+1) {
2097     if (strcasecmp(arg->argv[arg->argn], "yes") == 0) {
2098       if (arg->bundle->NatEnabled) {
2099 	PacketAliasSetMode(param, param);
2100 	return 0;
2101       }
2102       log_Printf(LogWARN, "nat not enabled\n");
2103     } else if (strcmp(arg->argv[arg->argn], "no") == 0) {
2104       if (arg->bundle->NatEnabled) {
2105 	PacketAliasSetMode(0, param);
2106 	return 0;
2107       }
2108       log_Printf(LogWARN, "nat not enabled\n");
2109     }
2110   }
2111   return -1;
2112 }
2113 #endif /* #ifndef NONAT */
2114 
2115 static int
2116 LinkCommand(struct cmdargs const *arg)
2117 {
2118   if (arg->argc > arg->argn+1) {
2119     char namelist[LINE_LEN];
2120     struct datalink *cx;
2121     char *name;
2122     int result = 0;
2123 
2124     if (!strcmp(arg->argv[arg->argn], "*")) {
2125       struct datalink *dl;
2126 
2127       cx = arg->bundle->links;
2128       while (cx) {
2129         /* Watch it, the command could be a ``remove'' */
2130         dl = cx->next;
2131         FindExec(arg->bundle, Commands, arg->argc, arg->argn+1, arg->argv,
2132                  arg->prompt, cx);
2133         for (cx = arg->bundle->links; cx; cx = cx->next)
2134           if (cx == dl)
2135             break;		/* Pointer's still valid ! */
2136       }
2137     } else {
2138       strncpy(namelist, arg->argv[arg->argn], sizeof namelist - 1);
2139       namelist[sizeof namelist - 1] = '\0';
2140       for(name = strtok(namelist, ", "); name; name = strtok(NULL,", "))
2141         if (!bundle2datalink(arg->bundle, name)) {
2142           log_Printf(LogWARN, "link: %s: Invalid link name\n", name);
2143           return 1;
2144         }
2145 
2146       strncpy(namelist, arg->argv[arg->argn], sizeof namelist - 1);
2147       namelist[sizeof namelist - 1] = '\0';
2148       for(name = strtok(namelist, ", "); name; name = strtok(NULL,", ")) {
2149         cx = bundle2datalink(arg->bundle, name);
2150         if (cx)
2151           FindExec(arg->bundle, Commands, arg->argc, arg->argn+1, arg->argv,
2152                    arg->prompt, cx);
2153         else {
2154           log_Printf(LogWARN, "link: %s: Invalidated link name !\n", name);
2155           result++;
2156         }
2157       }
2158     }
2159     return result;
2160   }
2161 
2162   log_Printf(LogWARN, "Usage: %s\n", arg->cmd->syntax);
2163   return 2;
2164 }
2165 
2166 struct link *
2167 command_ChooseLink(struct cmdargs const *arg)
2168 {
2169   if (arg->cx)
2170     return &arg->cx->physical->link;
2171   else if (!arg->bundle->ncp.mp.cfg.mrru) {
2172     struct datalink *dl = bundle2datalink(arg->bundle, NULL);
2173     if (dl)
2174       return &dl->physical->link;
2175   }
2176   return &arg->bundle->ncp.mp.link;
2177 }
2178 
2179 static const char *
2180 ident_cmd(const char *cmd, unsigned *keep, unsigned *add)
2181 {
2182   const char *result;
2183 
2184   switch (*cmd) {
2185     case 'A':
2186     case 'a':
2187       result = "accept";
2188       *keep = NEG_MYMASK;
2189       *add = NEG_ACCEPTED;
2190       break;
2191     case 'D':
2192     case 'd':
2193       switch (cmd[1]) {
2194         case 'E':
2195         case 'e':
2196           result = "deny";
2197           *keep = NEG_MYMASK;
2198           *add = 0;
2199           break;
2200         case 'I':
2201         case 'i':
2202           result = "disable";
2203           *keep = NEG_HISMASK;
2204           *add = 0;
2205           break;
2206         default:
2207           return NULL;
2208       }
2209       break;
2210     case 'E':
2211     case 'e':
2212       result = "enable";
2213       *keep = NEG_HISMASK;
2214       *add = NEG_ENABLED;
2215       break;
2216     default:
2217       return NULL;
2218   }
2219 
2220   return result;
2221 }
2222 
2223 static int
2224 OptSet(struct cmdargs const *arg)
2225 {
2226   int bit = (int)(long)arg->cmd->args;
2227   const char *cmd;
2228   unsigned keep;			/* Keep these bits */
2229   unsigned add;				/* Add these bits */
2230 
2231   if ((cmd = ident_cmd(arg->argv[arg->argn-2], &keep, &add)) == NULL)
2232     return 1;
2233 
2234   if (add)
2235     arg->bundle->cfg.opt |= bit;
2236   else
2237     arg->bundle->cfg.opt &= ~bit;
2238   return 0;
2239 }
2240 
2241 static int
2242 IfaceAliasOptSet(struct cmdargs const *arg)
2243 {
2244   unsigned save = arg->bundle->cfg.opt;
2245   int result = OptSet(arg);
2246 
2247   if (result == 0)
2248     if (Enabled(arg->bundle, OPT_IFACEALIAS) && !arg->bundle->NatEnabled) {
2249       arg->bundle->cfg.opt = save;
2250       log_Printf(LogWARN, "Cannot enable iface-alias without NAT\n");
2251       result = 2;
2252     }
2253 
2254   return result;
2255 }
2256 
2257 static int
2258 NegotiateSet(struct cmdargs const *arg)
2259 {
2260   long param = (long)arg->cmd->args;
2261   struct link *l = command_ChooseLink(arg);	/* LOCAL_CX_OPT uses this */
2262   struct datalink *cx = arg->cx;	/* LOCAL_CX uses this */
2263   const char *cmd;
2264   unsigned keep;			/* Keep these bits */
2265   unsigned add;				/* Add these bits */
2266 
2267   if ((cmd = ident_cmd(arg->argv[arg->argn-2], &keep, &add)) == NULL)
2268     return 1;
2269 
2270   if ((arg->cmd->lauth & LOCAL_CX) && !cx) {
2271     log_Printf(LogWARN, "%s %s: No context (use the `link' command)\n",
2272               cmd, arg->cmd->name);
2273     return 2;
2274   } else if (cx && !(arg->cmd->lauth & (LOCAL_CX|LOCAL_CX_OPT))) {
2275     log_Printf(LogWARN, "%s %s: Redundant context (%s) ignored\n",
2276               cmd, arg->cmd->name, cx->name);
2277     cx = NULL;
2278   }
2279 
2280   switch (param) {
2281     case NEG_ACFCOMP:
2282       cx->physical->link.lcp.cfg.acfcomp &= keep;
2283       cx->physical->link.lcp.cfg.acfcomp |= add;
2284       break;
2285     case NEG_CHAP05:
2286       cx->physical->link.lcp.cfg.chap05 &= keep;
2287       cx->physical->link.lcp.cfg.chap05 |= add;
2288       break;
2289 #ifdef HAVE_DES
2290     case NEG_CHAP80:
2291       cx->physical->link.lcp.cfg.chap80nt &= keep;
2292       cx->physical->link.lcp.cfg.chap80nt |= add;
2293       break;
2294     case NEG_CHAP80LM:
2295       cx->physical->link.lcp.cfg.chap80lm &= keep;
2296       cx->physical->link.lcp.cfg.chap80lm |= add;
2297       break;
2298 #endif
2299     case NEG_DEFLATE:
2300       l->ccp.cfg.neg[CCP_NEG_DEFLATE] &= keep;
2301       l->ccp.cfg.neg[CCP_NEG_DEFLATE] |= add;
2302       break;
2303     case NEG_DNS:
2304       arg->bundle->ncp.ipcp.cfg.ns.dns_neg &= keep;
2305       arg->bundle->ncp.ipcp.cfg.ns.dns_neg |= add;
2306       break;
2307     case NEG_ENDDISC:
2308       arg->bundle->ncp.mp.cfg.negenddisc &= keep;
2309       arg->bundle->ncp.mp.cfg.negenddisc |= add;
2310       break;
2311     case NEG_LQR:
2312       cx->physical->link.lcp.cfg.lqr &= keep;
2313       cx->physical->link.lcp.cfg.lqr |= add;
2314       break;
2315     case NEG_PAP:
2316       cx->physical->link.lcp.cfg.pap &= keep;
2317       cx->physical->link.lcp.cfg.pap |= add;
2318       break;
2319     case NEG_PPPDDEFLATE:
2320       l->ccp.cfg.neg[CCP_NEG_DEFLATE24] &= keep;
2321       l->ccp.cfg.neg[CCP_NEG_DEFLATE24] |= add;
2322       break;
2323     case NEG_PRED1:
2324       l->ccp.cfg.neg[CCP_NEG_PRED1] &= keep;
2325       l->ccp.cfg.neg[CCP_NEG_PRED1] |= add;
2326       break;
2327     case NEG_PROTOCOMP:
2328       cx->physical->link.lcp.cfg.protocomp &= keep;
2329       cx->physical->link.lcp.cfg.protocomp |= add;
2330       break;
2331     case NEG_SHORTSEQ:
2332       switch (bundle_Phase(arg->bundle)) {
2333         case PHASE_DEAD:
2334           break;
2335         case PHASE_ESTABLISH:
2336           /* Make sure none of our links are DATALINK_LCP or greater */
2337           if (bundle_HighestState(arg->bundle) >= DATALINK_LCP) {
2338             log_Printf(LogWARN, "shortseq: Only changable before"
2339                        " LCP negotiations\n");
2340             return 1;
2341           }
2342           break;
2343         default:
2344           log_Printf(LogWARN, "shortseq: Only changable at phase"
2345                      " DEAD/ESTABLISH\n");
2346           return 1;
2347       }
2348       arg->bundle->ncp.mp.cfg.shortseq &= keep;
2349       arg->bundle->ncp.mp.cfg.shortseq |= add;
2350       break;
2351     case NEG_VJCOMP:
2352       arg->bundle->ncp.ipcp.cfg.vj.neg &= keep;
2353       arg->bundle->ncp.ipcp.cfg.vj.neg |= add;
2354       break;
2355   }
2356 
2357   return 0;
2358 }
2359 
2360 static struct cmdtab const NegotiateCommands[] = {
2361   {"idcheck", NULL, OptSet, LOCAL_AUTH, "Check FSM reply ids",
2362   "disable|enable", (const void *)OPT_IDCHECK},
2363   {"iface-alias", NULL, IfaceAliasOptSet, LOCAL_AUTH,
2364    "retain interface addresses", "disable|enable",
2365    (const void *)OPT_IFACEALIAS},
2366   {"keep-session", NULL, OptSet, LOCAL_AUTH, "Retain device session leader",
2367   "disable|enable", (const void *)OPT_KEEPSESSION},
2368   {"loopback", NULL, OptSet, LOCAL_AUTH, "Loop packets for local iface",
2369   "disable|enable", (const void *)OPT_LOOPBACK},
2370   {"passwdauth", NULL, OptSet, LOCAL_AUTH, "Use passwd file",
2371   "disable|enable", (const void *)OPT_PASSWDAUTH},
2372   {"proxy", NULL, OptSet, LOCAL_AUTH, "Create a proxy ARP entry",
2373   "disable|enable", (const void *)OPT_PROXY},
2374   {"proxyall", NULL, OptSet, LOCAL_AUTH, "Proxy ARP for all remote hosts",
2375   "disable|enable", (const void *)OPT_PROXYALL},
2376   {"sroutes", NULL, OptSet, LOCAL_AUTH, "Use sticky routes",
2377   "disable|enable", (const void *)OPT_SROUTES},
2378   {"throughput", NULL, OptSet, LOCAL_AUTH, "Rolling throughput",
2379   "disable|enable", (const void *)OPT_THROUGHPUT},
2380   {"utmp", NULL, OptSet, LOCAL_AUTH, "Log connections in utmp",
2381   "disable|enable", (const void *)OPT_UTMP},
2382 
2383 #define OPT_MAX 10	/* accept/deny allowed below and not above */
2384 
2385   {"acfcomp", NULL, NegotiateSet, LOCAL_AUTH | LOCAL_CX,
2386   "Address & Control field compression", "accept|deny|disable|enable",
2387   (const void *)NEG_ACFCOMP},
2388   {"chap", "chap05", NegotiateSet, LOCAL_AUTH | LOCAL_CX,
2389   "Challenge Handshake Authentication Protocol", "accept|deny|disable|enable",
2390   (const void *)NEG_CHAP05},
2391 #ifdef HAVE_DES
2392   {"mschap", "chap80nt", NegotiateSet, LOCAL_AUTH | LOCAL_CX,
2393   "Microsoft (NT) CHAP", "accept|deny|disable|enable",
2394   (const void *)NEG_CHAP80},
2395   {"LANMan", "chap80lm", NegotiateSet, LOCAL_AUTH | LOCAL_CX,
2396   "Microsoft (NT) CHAP", "accept|deny|disable|enable",
2397   (const void *)NEG_CHAP80LM},
2398 #endif
2399   {"deflate", NULL, NegotiateSet, LOCAL_AUTH | LOCAL_CX_OPT,
2400   "Deflate compression", "accept|deny|disable|enable",
2401   (const void *)NEG_DEFLATE},
2402   {"deflate24", NULL, NegotiateSet, LOCAL_AUTH | LOCAL_CX_OPT,
2403   "Deflate (type 24) compression", "accept|deny|disable|enable",
2404   (const void *)NEG_PPPDDEFLATE},
2405   {"dns", NULL, NegotiateSet, LOCAL_AUTH,
2406   "DNS specification", "accept|deny|disable|enable", (const void *)NEG_DNS},
2407   {"enddisc", NULL, NegotiateSet, LOCAL_AUTH, "ENDDISC negotiation",
2408   "accept|deny|disable|enable", (const void *)NEG_ENDDISC},
2409   {"lqr", NULL, NegotiateSet, LOCAL_AUTH | LOCAL_CX,
2410   "Link Quality Reports", "accept|deny|disable|enable",
2411   (const void *)NEG_LQR},
2412   {"pap", NULL, NegotiateSet, LOCAL_AUTH | LOCAL_CX,
2413   "Password Authentication protocol", "accept|deny|disable|enable",
2414   (const void *)NEG_PAP},
2415   {"pred1", "predictor1", NegotiateSet, LOCAL_AUTH | LOCAL_CX_OPT,
2416   "Predictor 1 compression", "accept|deny|disable|enable",
2417   (const void *)NEG_PRED1},
2418   {"protocomp", NULL, NegotiateSet, LOCAL_AUTH | LOCAL_CX,
2419   "Protocol field compression", "accept|deny|disable|enable",
2420   (const void *)NEG_PROTOCOMP},
2421   {"shortseq", NULL, NegotiateSet, LOCAL_AUTH,
2422   "MP Short Sequence Numbers", "accept|deny|disable|enable",
2423   (const void *)NEG_SHORTSEQ},
2424   {"vjcomp", NULL, NegotiateSet, LOCAL_AUTH,
2425   "Van Jacobson header compression", "accept|deny|disable|enable",
2426   (const void *)NEG_VJCOMP},
2427   {"help", "?", HelpCommand, LOCAL_AUTH | LOCAL_NO_AUTH,
2428   "Display this message", "accept|deny|disable|enable help|? [value]",
2429   NegotiateCommands},
2430   {NULL, NULL, NULL},
2431 };
2432 
2433 static int
2434 NegotiateCommand(struct cmdargs const *arg)
2435 {
2436   if (arg->argc > arg->argn) {
2437     char const *argv[3];
2438     unsigned keep, add;
2439     int n;
2440 
2441     if ((argv[0] = ident_cmd(arg->argv[arg->argn-1], &keep, &add)) == NULL)
2442       return -1;
2443     argv[2] = NULL;
2444 
2445     for (n = arg->argn; n < arg->argc; n++) {
2446       argv[1] = arg->argv[n];
2447       FindExec(arg->bundle, NegotiateCommands + (keep == NEG_HISMASK ?
2448                0 : OPT_MAX), 2, 1, argv, arg->prompt, arg->cx);
2449     }
2450   } else if (arg->prompt)
2451     prompt_Printf(arg->prompt, "Use `%s ?' to get a list.\n",
2452 	    arg->argv[arg->argn-1]);
2453   else
2454     log_Printf(LogWARN, "%s command must have arguments\n",
2455               arg->argv[arg->argn] );
2456 
2457   return 0;
2458 }
2459 
2460 const char *
2461 command_ShowNegval(unsigned val)
2462 {
2463   switch (val&3) {
2464     case 1: return "disabled & accepted";
2465     case 2: return "enabled & denied";
2466     case 3: return "enabled & accepted";
2467   }
2468   return "disabled & denied";
2469 }
2470 
2471 static int
2472 ClearCommand(struct cmdargs const *arg)
2473 {
2474   struct pppThroughput *t;
2475   struct datalink *cx;
2476   int i, clear_type;
2477 
2478   if (arg->argc < arg->argn + 1)
2479     return -1;
2480 
2481   if (strcasecmp(arg->argv[arg->argn], "physical") == 0) {
2482     cx = arg->cx;
2483     if (!cx)
2484       cx = bundle2datalink(arg->bundle, NULL);
2485     if (!cx) {
2486       log_Printf(LogWARN, "A link must be specified for ``clear physical''\n");
2487       return 1;
2488     }
2489     t = &cx->physical->link.throughput;
2490   } else if (strcasecmp(arg->argv[arg->argn], "ipcp") == 0)
2491     t = &arg->bundle->ncp.ipcp.throughput;
2492   else
2493     return -1;
2494 
2495   if (arg->argc > arg->argn + 1) {
2496     clear_type = 0;
2497     for (i = arg->argn + 1; i < arg->argc; i++)
2498       if (strcasecmp(arg->argv[i], "overall") == 0)
2499         clear_type |= THROUGHPUT_OVERALL;
2500       else if (strcasecmp(arg->argv[i], "current") == 0)
2501         clear_type |= THROUGHPUT_CURRENT;
2502       else if (strcasecmp(arg->argv[i], "peak") == 0)
2503         clear_type |= THROUGHPUT_PEAK;
2504       else
2505         return -1;
2506   } else
2507     clear_type = THROUGHPUT_ALL;
2508 
2509   throughput_clear(t, clear_type, arg->prompt);
2510   return 0;
2511 }
2512 
2513 static int
2514 RunListCommand(struct cmdargs const *arg)
2515 {
2516   const char *cmd = arg->argc ? arg->argv[arg->argc - 1] : "???";
2517 
2518   if (arg->argc > arg->argn)
2519     FindExec(arg->bundle, arg->cmd->args, arg->argc, arg->argn, arg->argv,
2520              arg->prompt, arg->cx);
2521   else if (arg->prompt)
2522     prompt_Printf(arg->prompt, "Use `%s help' to get a list or `%s help"
2523                   " <option>' for syntax help.\n", cmd, cmd);
2524   else
2525     log_Printf(LogWARN, "%s command must have arguments\n", cmd);
2526 
2527   return 0;
2528 }
2529 
2530 static int
2531 IfaceAddCommand(struct cmdargs const *arg)
2532 {
2533   int bits, n, how;
2534   struct in_addr ifa, mask, brd;
2535 
2536   if (arg->argc == arg->argn + 1) {
2537     if (!ParseAddr(NULL, arg->argv[arg->argn], &ifa, NULL, NULL))
2538       return -1;
2539     mask.s_addr = brd.s_addr = INADDR_BROADCAST;
2540   } else {
2541     if (arg->argc == arg->argn + 2) {
2542       if (!ParseAddr(NULL, arg->argv[arg->argn], &ifa, &mask, &bits))
2543         return -1;
2544       n = 1;
2545     } else if (arg->argc == arg->argn + 3) {
2546       if (!ParseAddr(NULL, arg->argv[arg->argn], &ifa, NULL, NULL))
2547         return -1;
2548       if (!ParseAddr(NULL, arg->argv[arg->argn + 1], &mask, NULL, NULL))
2549         return -1;
2550       n = 2;
2551     } else
2552       return -1;
2553 
2554     if (!ParseAddr(NULL, arg->argv[arg->argn + n], &brd, NULL, NULL))
2555       return -1;
2556   }
2557 
2558   how = IFACE_ADD_LAST;
2559   if (arg->cmd->args)
2560     how |= IFACE_FORCE_ADD;
2561 
2562   return !iface_inAdd(arg->bundle->iface, ifa, mask, brd, how);
2563 }
2564 
2565 static int
2566 IfaceDeleteCommand(struct cmdargs const *arg)
2567 {
2568   struct in_addr ifa;
2569   int ok;
2570 
2571   if (arg->argc != arg->argn + 1)
2572     return -1;
2573 
2574   if (!ParseAddr(NULL, arg->argv[arg->argn], &ifa, NULL, NULL))
2575     return -1;
2576 
2577   if (arg->bundle->ncp.ipcp.fsm.state == ST_OPENED &&
2578       arg->bundle->ncp.ipcp.my_ip.s_addr == ifa.s_addr) {
2579     log_Printf(LogWARN, "%s: Cannot remove active interface address\n",
2580                inet_ntoa(ifa));
2581     return 1;
2582   }
2583 
2584   ok = iface_inDelete(arg->bundle->iface, ifa);
2585   if (!ok) {
2586     if (arg->cmd->args)
2587       ok = 1;
2588     else if (arg->prompt)
2589       prompt_Printf(arg->prompt, "%s: No such address\n", inet_ntoa(ifa));
2590     else
2591       log_Printf(LogWARN, "%s: No such address\n", inet_ntoa(ifa));
2592   }
2593 
2594   return !ok;
2595 }
2596 
2597 static int
2598 IfaceClearCommand(struct cmdargs const *arg)
2599 {
2600   int how;
2601 
2602   if (arg->argc != arg->argn)
2603     return -1;
2604 
2605   how = arg->bundle->ncp.ipcp.fsm.state == ST_OPENED ||
2606         arg->bundle->phys_type.all & PHYS_AUTO ?
2607         IFACE_CLEAR_ALIASES : IFACE_CLEAR_ALL;
2608   iface_Clear(arg->bundle->iface, how);
2609 
2610   return 0;
2611 }
2612 
2613 static int
2614 SetProcTitle(struct cmdargs const *arg)
2615 {
2616   static char title[LINE_LEN];
2617   char *argv[MAXARGS], *ptr;
2618   int len, remaining, f, argc = arg->argc - arg->argn;
2619 
2620   if (arg->argc == arg->argn) {
2621     ID0setproctitle(NULL);
2622     return 0;
2623   }
2624 
2625   if (argc >= sizeof argv / sizeof argv[0]) {
2626     argc = sizeof argv / sizeof argv[0] - 1;
2627     log_Printf(LogWARN, "Truncating proc title to %d args\n", argc);
2628   }
2629   command_Expand(argv, argc, arg->argv + arg->argn, arg->bundle, 1, getpid());
2630 
2631   ptr = title;
2632   remaining = sizeof title - 1;
2633   for (f = 0; f < argc && remaining; f++) {
2634     if (f) {
2635       *ptr++ = ' ';
2636       remaining--;
2637     }
2638     len = strlen(argv[f]);
2639     if (len > remaining)
2640       len = remaining;
2641     memcpy(ptr, argv[f], len);
2642     remaining -= len;
2643     ptr += len;
2644   }
2645   *ptr = '\0';
2646 
2647   ID0setproctitle(title);
2648 
2649   return 0;
2650 }
2651