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 | |
parent | f8df0413f0a057b6a3d3dd7bd8bc7c5d80911d3a (diff) |
upstream: move authorized_keys option parsing helpsers to misc.c
and make them public; ok markus@
OpenBSD-Commit-ID: c18bcb2a687227b3478377c981c2d56af2638ea2
-rw-r--r-- | auth-options.c | 71 | ||||
-rw-r--r-- | misc.c | 74 | ||||
-rw-r--r-- | misc.h | 7 |
3 files changed, 80 insertions, 72 deletions
diff --git a/auth-options.c b/auth-options.c index ca92f7a73..ac362e271 100644 --- a/auth-options.c +++ b/auth-options.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: auth-options.c,v 1.86 2019/07/09 04:15:00 djm Exp $ */ | 1 | /* $OpenBSD: auth-options.c,v 1.87 2019/09/03 08:32:11 djm Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2018 Damien Miller <djm@mindrot.org> | 3 | * Copyright (c) 2018 Damien Miller <djm@mindrot.org> |
4 | * | 4 | * |
@@ -40,75 +40,6 @@ | |||
40 | #include "ssh2.h" | 40 | #include "ssh2.h" |
41 | #include "auth-options.h" | 41 | #include "auth-options.h" |
42 | 42 | ||
43 | /* | ||
44 | * Match flag 'opt' in *optsp, and if allow_negate is set then also match | ||
45 | * 'no-opt'. Returns -1 if option not matched, 1 if option matches or 0 | ||
46 | * if negated option matches. | ||
47 | * If the option or negated option matches, then *optsp is updated to | ||
48 | * point to the first character after the option. | ||
49 | */ | ||
50 | static int | ||
51 | opt_flag(const char *opt, int allow_negate, const char **optsp) | ||
52 | { | ||
53 | size_t opt_len = strlen(opt); | ||
54 | const char *opts = *optsp; | ||
55 | int negate = 0; | ||
56 | |||
57 | if (allow_negate && strncasecmp(opts, "no-", 3) == 0) { | ||
58 | opts += 3; | ||
59 | negate = 1; | ||
60 | } | ||
61 | if (strncasecmp(opts, opt, opt_len) == 0) { | ||
62 | *optsp = opts + opt_len; | ||
63 | return negate ? 0 : 1; | ||
64 | } | ||
65 | return -1; | ||
66 | } | ||
67 | |||
68 | static char * | ||
69 | opt_dequote(const char **sp, const char **errstrp) | ||
70 | { | ||
71 | const char *s = *sp; | ||
72 | char *ret; | ||
73 | size_t i; | ||
74 | |||
75 | *errstrp = NULL; | ||
76 | if (*s != '"') { | ||
77 | *errstrp = "missing start quote"; | ||
78 | return NULL; | ||
79 | } | ||
80 | s++; | ||
81 | if ((ret = malloc(strlen((s)) + 1)) == NULL) { | ||
82 | *errstrp = "memory allocation failed"; | ||
83 | return NULL; | ||
84 | } | ||
85 | for (i = 0; *s != '\0' && *s != '"';) { | ||
86 | if (s[0] == '\\' && s[1] == '"') | ||
87 | s++; | ||
88 | ret[i++] = *s++; | ||
89 | } | ||
90 | if (*s == '\0') { | ||
91 | *errstrp = "missing end quote"; | ||
92 | free(ret); | ||
93 | return NULL; | ||
94 | } | ||
95 | ret[i] = '\0'; | ||
96 | s++; | ||
97 | *sp = s; | ||
98 | return ret; | ||
99 | } | ||
100 | |||
101 | static int | ||
102 | opt_match(const char **opts, const char *term) | ||
103 | { | ||
104 | if (strncasecmp((*opts), term, strlen(term)) == 0 && | ||
105 | (*opts)[strlen(term)] == '=') { | ||
106 | *opts += strlen(term) + 1; | ||
107 | return 1; | ||
108 | } | ||
109 | return 0; | ||
110 | } | ||
111 | |||
112 | static int | 43 | static int |
113 | dup_strings(char ***dstp, size_t *ndstp, char **src, size_t nsrc) | 44 | dup_strings(char ***dstp, size_t *ndstp, char **src, size_t nsrc) |
114 | { | 45 | { |
@@ -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 | */ | ||
2150 | int | ||
2151 | opt_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 | |||
2168 | char * | ||
2169 | opt_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 | |||
2201 | int | ||
2202 | opt_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 | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: misc.h,v 1.80 2019/09/03 08:29:58 djm Exp $ */ | 1 | /* $OpenBSD: misc.h,v 1.81 2019/09/03 08:32:11 djm Exp $ */ |
2 | 2 | ||
3 | /* | 3 | /* |
4 | * Author: Tatu Ylonen <ylo@cs.hut.fi> | 4 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
@@ -166,6 +166,11 @@ int safe_path(const char *, struct stat *, const char *, uid_t, | |||
166 | int safe_path_fd(int, const char *, struct passwd *, | 166 | int safe_path_fd(int, const char *, struct passwd *, |
167 | char *err, size_t errlen); | 167 | char *err, size_t errlen); |
168 | 168 | ||
169 | /* authorized_key-style options parsing helpers */ | ||
170 | int opt_flag(const char *opt, int allow_negate, const char **optsp); | ||
171 | char *opt_dequote(const char **sp, const char **errstrp); | ||
172 | int opt_match(const char **opts, const char *term); | ||
173 | |||
169 | /* readpass.c */ | 174 | /* readpass.c */ |
170 | 175 | ||
171 | #define RP_ECHO 0x0001 | 176 | #define RP_ECHO 0x0001 |