diff options
author | Brian Somers <brian@FreeBSD.org> | 2000-03-14 01:47:31 +0000 |
---|---|---|
committer | Brian Somers <brian@FreeBSD.org> | 2000-03-14 01:47:31 +0000 |
commit | 2a30e2ac0b09cb67cd405a077241278929a8ca65 (patch) | |
tree | 70e68e479e5b27fc33e6978e974a7c7ec9bfe734 /usr.sbin | |
parent | d568d6c405600fff668454f930567249c69279d0 (diff) | |
download | src-2a30e2ac0b09cb67cd405a077241278929a8ca65.tar.gz src-2a30e2ac0b09cb67cd405a077241278929a8ca65.zip |
Understand environment variables in commands
Submitted by: Mark Knight <markk@knigma.org>
Notes
Notes:
svn path=/head/; revision=58045
Diffstat (limited to 'usr.sbin')
-rw-r--r-- | usr.sbin/ppp/README.changes | 1 | ||||
-rw-r--r-- | usr.sbin/ppp/command.c | 14 | ||||
-rw-r--r-- | usr.sbin/ppp/command.h | 2 | ||||
-rw-r--r-- | usr.sbin/ppp/systems.c | 146 | ||||
-rw-r--r-- | usr.sbin/ppp/systems.h | 1 |
5 files changed, 102 insertions, 62 deletions
diff --git a/usr.sbin/ppp/README.changes b/usr.sbin/ppp/README.changes index a0a0694068e2..ac4de49937f4 100644 --- a/usr.sbin/ppp/README.changes +++ b/usr.sbin/ppp/README.changes @@ -100,3 +100,4 @@ o Unbalanced quotes in commands are now warned about and the entire command is ignored. o It is now only necessary to escape the `-' character in chat scripts twice. See the example files for details. +o Environment variables and ~ are expanded on in commands diff --git a/usr.sbin/ppp/command.c b/usr.sbin/ppp/command.c index 06ce82d4a049..99753c3cfe0e 100644 --- a/usr.sbin/ppp/command.c +++ b/usr.sbin/ppp/command.c @@ -935,6 +935,18 @@ FindExec(struct bundle *bundle, struct cmdtab const *cmds, int argc, int argn, } int +command_Expand_Interpret(char *buff, int nb, char *argv[MAXARGS], int offset) +{ + char buff2[LINE_LEN-offset]; + + InterpretArg(buff, buff2); + strncpy(buff, buff2, LINE_LEN - offset - 1); + buff[LINE_LEN - offset - 1] = '\0'; + + return command_Interpret(buff, nb, argv); +} + +int command_Interpret(char *buff, int nb, char *argv[MAXARGS]) { char *cp; @@ -1013,7 +1025,7 @@ command_Decode(struct bundle *bundle, char *buff, int nb, struct prompt *prompt, int argc; char *argv[MAXARGS]; - if ((argc = command_Interpret(buff, nb, argv)) < 0) + if ((argc = command_Expand_Interpret(buff, nb, argv, 0)) < 0) return 0; command_Run(bundle, argc, (char const *const *)argv, prompt, label, NULL); diff --git a/usr.sbin/ppp/command.h b/usr.sbin/ppp/command.h index d08f67db7012..afb41dde3fba 100644 --- a/usr.sbin/ppp/command.h +++ b/usr.sbin/ppp/command.h @@ -55,6 +55,7 @@ extern const char Version[]; extern void command_Expand(char **, int, char const *const *, struct bundle *, int, pid_t); +extern int command_Expand_Interpret(char *, int, char *vector[MAXARGS], int); extern int command_Interpret(char *, int, char *vector[MAXARGS]); extern void command_Run(struct bundle *, int, char const *const *, struct prompt *, const char *, struct datalink *); @@ -62,3 +63,4 @@ extern int command_Decode(struct bundle *, char *, int, struct prompt *, const char *); extern struct link *command_ChooseLink(struct cmdargs const *); extern const char *command_ShowNegval(unsigned); + diff --git a/usr.sbin/ppp/systems.c b/usr.sbin/ppp/systems.c index 18a9df07ee34..e00009772d50 100644 --- a/usr.sbin/ppp/systems.c +++ b/usr.sbin/ppp/systems.c @@ -59,79 +59,106 @@ CloseSecret(FILE *fp) } /* Move string from ``from'' to ``to'', interpreting ``~'' and $.... */ -static const char * +const char * InterpretArg(const char *from, char *to) { - const char *env; char *ptr, *startto, *endto; - int len; + struct passwd *pwd; + int len, instring; + const char *env; + instring = 0; startto = to; endto = to + LINE_LEN - 1; while(issep(*from)) from++; - if (*from == '~') { - struct passwd *pwd; - - ptr = strchr(++from, '/'); - len = ptr ? ptr - from : strlen(from); - if (len == 0) { - pwd = getpwuid(ID0realuid()); - } else { - strncpy(to, from, len); - to[len] = '\0'; - pwd = getpwnam(to); - } - strncpy(to, pwd ? pwd->pw_dir : _PATH_PPP, endto - to); - endpwent(); - - *endto = '\0'; - to += strlen(to); - from += len; - } - - while (to < endto && !issep(*from) && *from != '#' && *from != '\0') { - if (*from == '$') { - if (from[1] == '$') { - *to = '\0'; /* For an empty var name below */ - from += 2; - } else if (from[1] == '{') { - ptr = strchr(from+2, '}'); - if (ptr) { - len = ptr - from - 2; - if (endto - to < len ) - len = endto - to; - if (len) { - strncpy(to, from+2, len); - to[len] = '\0'; - from = ptr+1; + while (*from != '\0') { + switch (*from) { + case '"': + instring = !instring; + *to++ = *from++; + break; + case '\\': + switch (*++from) { + case '$': + case '~': + break; /* Swallow the escapes */ + + default: + *to++ = '\\'; /* Pass the escapes on, maybe skipping \# */ + break; + } + *to++ = *from++; + break; + case '$': + if (from[1] == '$') { + *to = '\0'; /* For an empty var name below */ + from += 2; + } else if (from[1] == '{') { + ptr = strchr(from+2, '}'); + if (ptr) { + len = ptr - from - 2; + if (endto - to < len ) + len = endto - to; + if (len) { + strncpy(to, from+2, len); + to[len] = '\0'; + from = ptr+1; + } else { + *to++ = *from++; + continue; + } } else { *to++ = *from++; continue; } } else { - *to++ = *from++; - continue; + ptr = to; + for (from++; (isalnum(*from) || *from == '_') && ptr < endto; from++) + *ptr++ = *from; + *ptr = '\0'; } - } else { - ptr = to; - for (from++; (isalnum(*from) || *from == '_') && ptr < endto; from++) - *ptr++ = *from; - *ptr = '\0'; - } - if (*to == '\0') - *to++ = '$'; - else if ((env = getenv(to)) != NULL) { - strncpy(to, env, endto - to); - *endto = '\0'; - to += strlen(to); - } - } else { - if (*from == '\\') - from++; - *to++ = *from++; + if (*to == '\0') + *to++ = '$'; + else if ((env = getenv(to)) != NULL) { + strncpy(to, env, endto - to); + *endto = '\0'; + to += strlen(to); + } + break; + + case '~': + ptr = strchr(++from, '/'); + len = ptr ? ptr - from : strlen(from); + if (len == 0) + pwd = getpwuid(ID0realuid()); + else { + strncpy(to, from, len); + to[len] = '\0'; + pwd = getpwnam(to); + } + if (pwd == NULL) + *to++ = '~'; + else { + strncpy(to, pwd->pw_dir, endto - to); + *endto = '\0'; + to += strlen(to); + from += len; + } + endpwent(); + break; + + case '#': + if (!instring) + while (*from != '\0') + *to++ = *from++; + break; + + default: + *to++ = *from++; + break; } } @@ -144,9 +171,6 @@ InterpretArg(const char *from, char *to) } *to = '\0'; - while (issep(*from)) - from++; - return from; } @@ -363,7 +387,7 @@ ReadSystem(struct bundle *bundle, const char *name, const char *file, } len = strlen(cp); - if ((argc = command_Interpret(cp, len, argv)) < 0) + if ((argc = command_Expand_Interpret(cp, len, argv, cp - line)) < 0) log_Printf(LogWARN, "%s: %d: Syntax error\n", filename, linenum); else { allowcmd = argc > 0 && !strcasecmp(argv[0], "allow"); diff --git a/usr.sbin/ppp/systems.h b/usr.sbin/ppp/systems.h index 39130d642cb1..969e4ab59933 100644 --- a/usr.sbin/ppp/systems.h +++ b/usr.sbin/ppp/systems.h @@ -35,3 +35,4 @@ extern int AllowUsers(struct cmdargs const *); extern int AllowModes(struct cmdargs const *); extern int LoadCommand(struct cmdargs const *); extern int SaveCommand(struct cmdargs const *); +extern const char *InterpretArg(const char *, char *); |