summaryrefslogtreecommitdiff
path: root/misc.c
diff options
context:
space:
mode:
authordjm@openbsd.org <djm@openbsd.org>2019-09-03 08:32:11 +0000
committerDamien Miller <djm@mindrot.org>2019-09-03 18:39:31 +1000
commit5485f8d50a5bc46aeed829075ebf5d9c617027ea (patch)
treefaaa341e91f2e3006af62927fe484ab09fbc5b79 /misc.c
parentf8df0413f0a057b6a3d3dd7bd8bc7c5d80911d3a (diff)
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.c74
1 files changed, 73 insertions, 1 deletions
diff --git a/misc.c b/misc.c
index d5e44da77..88833d7ff 100644
--- a/misc.c
+++ b/misc.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: misc.c,v 1.141 2019/09/03 08:29:58 djm Exp $ */ 1/* $OpenBSD: misc.c,v 1.142 2019/09/03 08:32:11 djm Exp $ */
2/* 2/*
3 * Copyright (c) 2000 Markus Friedl. All rights reserved. 3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 * Copyright (c) 2005,2006 Damien Miller. All rights reserved. 4 * Copyright (c) 2005,2006 Damien Miller. All rights reserved.
@@ -2137,3 +2137,75 @@ skip_space(char **cpp)
2137 ; 2137 ;
2138 *cpp = cp; 2138 *cpp = cp;
2139} 2139}
2140
2141/* authorized_key-style options parsing helpers */
2142
2143/*
2144 * Match flag 'opt' in *optsp, and if allow_negate is set then also match
2145 * 'no-opt'. Returns -1 if option not matched, 1 if option matches or 0
2146 * if negated option matches.
2147 * If the option or negated option matches, then *optsp is updated to
2148 * point to the first character after the option.
2149 */
2150int
2151opt_flag(const char *opt, int allow_negate, const char **optsp)
2152{
2153 size_t opt_len = strlen(opt);
2154 const char *opts = *optsp;
2155 int negate = 0;
2156
2157 if (allow_negate && strncasecmp(opts, "no-", 3) == 0) {
2158 opts += 3;
2159 negate = 1;
2160 }
2161 if (strncasecmp(opts, opt, opt_len) == 0) {
2162 *optsp = opts + opt_len;
2163 return negate ? 0 : 1;
2164 }
2165 return -1;
2166}
2167
2168char *
2169opt_dequote(const char **sp, const char **errstrp)
2170{
2171 const char *s = *sp;
2172 char *ret;
2173 size_t i;
2174
2175 *errstrp = NULL;
2176 if (*s != '"') {
2177 *errstrp = "missing start quote";
2178 return NULL;
2179 }
2180 s++;
2181 if ((ret = malloc(strlen((s)) + 1)) == NULL) {
2182 *errstrp = "memory allocation failed";
2183 return NULL;
2184 }
2185 for (i = 0; *s != '\0' && *s != '"';) {
2186 if (s[0] == '\\' && s[1] == '"')
2187 s++;
2188 ret[i++] = *s++;
2189 }
2190 if (*s == '\0') {
2191 *errstrp = "missing end quote";
2192 free(ret);
2193 return NULL;
2194 }
2195 ret[i] = '\0';
2196 s++;
2197 *sp = s;
2198 return ret;
2199}
2200
2201int
2202opt_match(const char **opts, const char *term)
2203{
2204 if (strncasecmp((*opts), term, strlen(term)) == 0 &&
2205 (*opts)[strlen(term)] == '=') {
2206 *opts += strlen(term) + 1;
2207 return 1;
2208 }
2209 return 0;
2210}
2211