]> git.ipfire.org Git - thirdparty/openssh-portable.git/commitdiff
upstream: move authorized_keys option parsing helpsers to misc.c
authordjm@openbsd.org <djm@openbsd.org>
Tue, 3 Sep 2019 08:32:11 +0000 (08:32 +0000)
committerDamien Miller <djm@mindrot.org>
Tue, 3 Sep 2019 08:39:31 +0000 (18:39 +1000)
and make them public; ok markus@

OpenBSD-Commit-ID: c18bcb2a687227b3478377c981c2d56af2638ea2

auth-options.c
misc.c
misc.h

index ca92f7a7314e85be055c1b02a26d34bc0856fff7..ac362e2710752937eea7d6584328196e1bc08838 100644 (file)
@@ -1,4 +1,4 @@
-/* $OpenBSD: auth-options.c,v 1.86 2019/07/09 04:15:00 djm Exp $ */
+/* $OpenBSD: auth-options.c,v 1.87 2019/09/03 08:32:11 djm Exp $ */
 /*
  * Copyright (c) 2018 Damien Miller <djm@mindrot.org>
  *
 #include "ssh2.h"
 #include "auth-options.h"
 
-/*
- * 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.
- */
-static 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;
-}
-
-static 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;
-}
-
-static 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;
-}
-
 static int
 dup_strings(char ***dstp, size_t *ndstp, char **src, size_t nsrc)
 {
diff --git a/misc.c b/misc.c
index d5e44da775c6841d0f67578eb54d10e027f1cef6..88833d7ff24680b661343bc93f84b279253c7bca 100644 (file)
--- a/misc.c
+++ b/misc.c
@@ -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;
+}
+
diff --git a/misc.h b/misc.h
index 8deea5ea64a26965ace3b353d46c123159a3ee6b..bcc34f980424d9ae4cd78f3b5c43d49416e35f0e 100644 (file)
--- a/misc.h
+++ b/misc.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: misc.h,v 1.80 2019/09/03 08:29:58 djm Exp $ */
+/* $OpenBSD: misc.h,v 1.81 2019/09/03 08:32:11 djm Exp $ */
 
 /*
  * Author: Tatu Ylonen <ylo@cs.hut.fi>
@@ -166,6 +166,11 @@ int         safe_path(const char *, struct stat *, const char *, uid_t,
 int     safe_path_fd(int, const char *, struct passwd *,
             char *err, size_t errlen);
 
+/* authorized_key-style options parsing helpers */
+int    opt_flag(const char *opt, int allow_negate, const char **optsp);
+char   *opt_dequote(const char **sp, const char **errstrp);
+int    opt_match(const char **opts, const char *term);
+
 /* readpass.c */
 
 #define RP_ECHO                        0x0001