diff options
author | Andrew Gregory <andrew.gregory.8@gmail.com> | 2015-11-11 19:19:59 -0500 |
---|---|---|
committer | Allan McRae <allan@archlinux.org> | 2015-11-28 16:26:51 +1000 |
commit | b42d0852f31ce7cef75da5ce51e6c09545ef8f2d (patch) | |
tree | 7f509a32250117dc51bc10ca8e7bab8fdb72acc7 /lib/libalpm | |
parent | e0607f6ae28e85ab905cd10536055ff5b6cc4652 (diff) | |
download | pacman-b42d0852f31ce7cef75da5ce51e6c09545ef8f2d.tar.xz |
allow arguments in hook Exec fields
Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com>
Signed-off-by: Allan McRae <allan@archlinux.org>
Diffstat (limited to 'lib/libalpm')
-rw-r--r-- | lib/libalpm/hook.c | 129 |
1 files changed, 124 insertions, 5 deletions
diff --git a/lib/libalpm/hook.c b/lib/libalpm/hook.c index 0191a21e..bff072d6 100644 --- a/lib/libalpm/hook.c +++ b/lib/libalpm/hook.c @@ -17,6 +17,7 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. */ +#include <ctype.h> #include <dirent.h> #include <errno.h> #include <string.h> @@ -49,7 +50,7 @@ struct _alpm_hook_t { char *name; alpm_list_t *triggers; alpm_list_t *depends; - char *cmd; + char **cmd; enum _alpm_hook_when_t when; int abort_on_fail; }; @@ -67,11 +68,22 @@ static void _alpm_trigger_free(struct _alpm_trigger_t *trigger) } } +static void _alpm_wordsplit_free(char **ws) +{ + if(ws) { + char **c; + for(c = ws; *c; c++) { + free(*c); + } + free(ws); + } +} + static void _alpm_hook_free(struct _alpm_hook_t *hook) { if(hook) { free(hook->name); - free(hook->cmd); + _alpm_wordsplit_free(hook->cmd); alpm_list_free_inner(hook->triggers, (alpm_list_fn_free) _alpm_trigger_free); alpm_list_free(hook->triggers); FREELIST(hook->depends); @@ -141,6 +153,107 @@ static int _alpm_hook_validate(alpm_handle_t *handle, return ret; } +static char **_alpm_wordsplit(char *str) +{ + char *c = str, *end; + char **out = NULL, **outsave; + size_t count = 0; + + if(str == NULL) { + errno = EINVAL; + return NULL; + } + + for(c = str; isspace(*c); c++); + while(*c) { + size_t wordlen = 0; + + /* extend our array */ + outsave = out; + if((out = realloc(out, (count + 1) * sizeof(char*))) == NULL) { + out = outsave; + goto error; + } + + /* calculate word length and check for unbalanced quotes */ + for(end = c; *end && !isspace(*end); end++) { + if(*end == '\'' || *end == '"') { + char quote = *end; + while(*(++end) && *end != quote) { + if(*end == '\\' && *(end + 1) == quote) { + end++; + } + wordlen++; + } + if(*end != quote) { + errno = EINVAL; + goto error; + } + } else { + if(*end == '\\' && (end[1] == '\'' || end[1] == '"')) { + end++; /* skip the '\\' */ + } + wordlen++; + } + } + + if(wordlen == (size_t) (end - c)) { + /* no internal quotes or escapes, copy it the easy way */ + if((out[count++] = strndup(c, wordlen)) == NULL) { + goto error; + } + } else { + /* manually copy to remove quotes and escapes */ + char *dest = out[count++] = malloc(wordlen + 1); + if(dest == NULL) { goto error; } + while(c < end) { + if(*c == '\'' || *c == '"') { + char quote = *c; + /* we know there must be a matching end quote, + * no need to check for '\0' */ + for(c++; *c != quote; c++) { + if(*c == '\\' && *(c + 1) == quote) { + c++; + } + *(dest++) = *c; + } + c++; + } else { + if(*c == '\\' && (c[1] == '\'' || c[1] == '"')) { + c++; /* skip the '\\' */ + } + *(dest++) = *(c++); + } + } + *dest = '\0'; + } + + if(*end == '\0') { + break; + } else { + for(c = end + 1; isspace(*c); c++); + } + } + + outsave = out; + if((out = realloc(out, (count + 1) * sizeof(char*))) == NULL) { + out = outsave; + goto error; + } + + out[count++] = NULL; + + return out; + +error: + /* can't use wordsplit_free here because NULL has not been appended */ + while(count) { + free(out[--count]); + } + free(out); + return NULL; +} + static int _alpm_hook_parse_cb(const char *file, int line, const char *section, char *key, char *value, void *data) { @@ -208,7 +321,14 @@ static int _alpm_hook_parse_cb(const char *file, int line, } else if(strcmp(key, "AbortOnFail") == 0) { hook->abort_on_fail = 1; } else if(strcmp(key, "Exec") == 0) { - STRDUP(hook->cmd, value, return 1); + if((hook->cmd = _alpm_wordsplit(value)) == NULL) { + if(errno == EINVAL) { + error(_("hook %s line %d: invalid value %s\n"), file, line, value); + } else { + error(_("hook %s line %d: unable to set option (%s)\n"), + file, line, strerror(errno)); + } + } } else { error(_("hook %s line %d: invalid option %s\n"), file, line, key); } @@ -383,7 +503,6 @@ static alpm_list_t *find_hook(alpm_list_t *haystack, const void *needle) static int _alpm_hook_run_hook(alpm_handle_t *handle, struct _alpm_hook_t *hook) { alpm_list_t *i, *pkgs = _alpm_db_get_pkgcache(handle->db_local); - char *const argv[] = { hook->cmd, NULL }; for(i = hook->depends; i; i = i->next) { if(!alpm_find_satisfier(pkgs, i->data)) { @@ -393,7 +512,7 @@ static int _alpm_hook_run_hook(alpm_handle_t *handle, struct _alpm_hook_t *hook) } } - return _alpm_run_chroot(handle, hook->cmd, argv); + return _alpm_run_chroot(handle, hook->cmd[0], hook->cmd); } int _alpm_hook_run(alpm_handle_t *handle, enum _alpm_hook_when_t when) |