diff options
author | djm@openbsd.org <djm@openbsd.org> | 2019-09-03 08:32:11 +0000 |
---|---|---|
committer | Damien Miller <djm@mindrot.org> | 2019-09-03 18:39:31 +1000 |
commit | 5485f8d50a5bc46aeed829075ebf5d9c617027ea (patch) | |
tree | faaa341e91f2e3006af62927fe484ab09fbc5b79 /misc.c | |
parent | f8df0413f0a057b6a3d3dd7bd8bc7c5d80911d3a (diff) | |
download | openssh-git-5485f8d50a5bc46aeed829075ebf5d9c617027ea.tar.gz |
upstream: move authorized_keys option parsing helpsers to misc.c
and make them public; ok markus@
OpenBSD-Commit-ID: c18bcb2a687227b3478377c981c2d56af2638ea2
Diffstat (limited to 'misc.c')
-rw-r--r-- | misc.c | 74 |
1 files changed, 73 insertions, 1 deletions
@@ -1,4 +1,4 @@ -/* $OpenBSD: misc.c,v 1.141 2019/09/03 08:29:58 djm Exp $ */ +/* $OpenBSD: misc.c,v 1.142 2019/09/03 08:32:11 djm Exp $ */ /* * Copyright (c) 2000 Markus Friedl. All rights reserved. * Copyright (c) 2005,2006 Damien Miller. All rights reserved. @@ -2137,3 +2137,75 @@ skip_space(char **cpp) ; *cpp = cp; } + +/* authorized_key-style options parsing helpers */ + +/* + * Match flag 'opt' in *optsp, and if allow_negate is set then also match + * 'no-opt'. Returns -1 if option not matched, 1 if option matches or 0 + * if negated option matches. + * If the option or negated option matches, then *optsp is updated to + * point to the first character after the option. + */ +int +opt_flag(const char *opt, int allow_negate, const char **optsp) +{ + size_t opt_len = strlen(opt); + const char *opts = *optsp; + int negate = 0; + + if (allow_negate && strncasecmp(opts, "no-", 3) == 0) { + opts += 3; + negate = 1; + } + if (strncasecmp(opts, opt, opt_len) == 0) { + *optsp = opts + opt_len; + return negate ? 0 : 1; + } + return -1; +} + +char * +opt_dequote(const char **sp, const char **errstrp) +{ + const char *s = *sp; + char *ret; + size_t i; + + *errstrp = NULL; + if (*s != '"') { + *errstrp = "missing start quote"; + return NULL; + } + s++; + if ((ret = malloc(strlen((s)) + 1)) == NULL) { + *errstrp = "memory allocation failed"; + return NULL; + } + for (i = 0; *s != '\0' && *s != '"';) { + if (s[0] == '\\' && s[1] == '"') + s++; + ret[i++] = *s++; + } + if (*s == '\0') { + *errstrp = "missing end quote"; + free(ret); + return NULL; + } + ret[i] = '\0'; + s++; + *sp = s; + return ret; +} + +int +opt_match(const char **opts, const char *term) +{ + if (strncasecmp((*opts), term, strlen(term)) == 0 && + (*opts)[strlen(term)] == '=') { + *opts += strlen(term) + 1; + return 1; + } + return 0; +} + |