summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--auth-options.c71
-rw-r--r--misc.c74
-rw-r--r--misc.h7
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 */
50static int
51opt_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
68static char *
69opt_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
101static int
102opt_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
112static int 43static int
113dup_strings(char ***dstp, size_t *ndstp, char **src, size_t nsrc) 44dup_strings(char ***dstp, size_t *ndstp, char **src, size_t nsrc)
114{ 45{
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
diff --git a/misc.h b/misc.h
index 8deea5ea6..bcc34f980 100644
--- a/misc.h
+++ b/misc.h
@@ -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,
166int safe_path_fd(int, const char *, struct passwd *, 166int 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 */
170int opt_flag(const char *opt, int allow_negate, const char **optsp);
171char *opt_dequote(const char **sp, const char **errstrp);
172int 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