From nobody Tue Jun 13 21:29:23 2023 X-Original-To: dev-commits-src-branches@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4QghX36DL0z4cb7G; Tue, 13 Jun 2023 21:29:23 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4QghX35Ghcz421x; Tue, 13 Jun 2023 21:29:23 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1686691763; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=moRFRcD3+WMqA7PEKREHO7zf9D/rU+DFm+E5oPpqmpg=; b=VTcTnWcEAousyVC0C8L8dOTkPQmNTOksdCunhM0Q6eS0Cyhq4vzOhd4N8QqgJd5drJy/qM bWrh1IcDEM2C+rPlObs8gamA2M16FJlMdMMCuwNY1EW424ax4OnM5XM/24JeU3yR2hws4t NWV4UUNMXAlf3cXNoaQQiRq2MjbFJHNhbi9suEDi+WpqeYoAmTUuB/1k9YsR3PJ3F7g5VI piKbG7XBiYL6am2z8JL8scTdo182sWGigIONy/xRScy6nquQAcbZvDDjbnKOim1Jn+6alB uDhPoU/5L0ATtV4ppTTKTAIQpE8nCW6DTWHUIyFBwsdpUqXdkIJUgfnWxK5asg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1686691763; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=moRFRcD3+WMqA7PEKREHO7zf9D/rU+DFm+E5oPpqmpg=; b=XJi1XJmHrEzrBUm17baljJ1dQnR4PK3/FU2UkIcaPIq1ITe1RQTLXWGQNwCT1krZdchZ91 JeZcqfFimSKq3z80ZWNh+stk77zrUa/WuN0XZ7wU3I+RmWDAQ2OYuKliYKURgGhiLSKSqk dlPyw/hEiZC6+mdYy9LWnSq9veHmVDehEHfoooLpItmdGgaxSvi8N9V5ozRtTrYu7MiBax W63YGotcwK4TgC2MIl+Oo7fDOnjX6PeTNN07ATk8wCwN9tN8JoGoAaaheKcASiapK9Vv6j DW+/Z8hXXYWZ2Wc9CLFJAyNTq5zkWjSle8B4dCg4YxiUuRgLi7fLPC7AVMXxzQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1686691763; a=rsa-sha256; cv=none; b=N8SsM11z1p9MLy0fVkmrLqlMZgOuinoE07cpmcvmX7Mhc3Vfa4TJkzA2H2cKbeninwbQAn /OOV/9nTUzChNgOwrTMfQsKxu9q1C4rJNcDN+ooUAvqNx88HpvJyf/JoF3MCn1nOZ1TENy kLYmh28VAKMM3LIqiCpfocTIreNaL+joi6ly8g9UuxUCrWd00REFNp58GF2f5Z7FiiowAA 8Eeqr/hE14/cSuuQgvimdZ8KBid+JLB4JYKhddo4UWFb1joeNuWL7M6vX3N7zIulGAe8Gf 1nObspy3ep/c3EREvmp2zQmIh4U107qbg7kutvtyyGRmJMd0jncrPxYiLQOknA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4QghX32wvpzdVT; Tue, 13 Jun 2023 21:29:23 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 35DLTNaO018620; Tue, 13 Jun 2023 21:29:23 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 35DLTNuw018619; Tue, 13 Jun 2023 21:29:23 GMT (envelope-from git) Date: Tue, 13 Jun 2023 21:29:23 GMT Message-Id: <202306132129.35DLTNuw018619@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Ed Maste Subject: git: f122e552354e - stable/13 - Allow a comma-separated list in login class capabilities, by adding a version of strcspn that allows quoting. List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: emaste X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: f122e552354e84f18b82d11e40c6f8f214ed8abc Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by emaste: URL: https://cgit.FreeBSD.org/src/commit/?id=f122e552354e84f18b82d11e40c6f8f214ed8abc commit f122e552354e84f18b82d11e40c6f8f214ed8abc Author: Sean Eric Fagan AuthorDate: 2023-01-14 18:37:31 +0000 Commit: Ed Maste CommitDate: 2023-06-13 21:28:54 +0000 Allow a comma-separated list in login class capabilities, by adding a version of strcspn that allows quoting. PR: 236204 Differential Revision: https://reviews.freebsd.org/D25368 (cherry picked from commit f32db406504ece1b28f43dc816736e081fe22826) --- lib/libutil/login_cap.c | 109 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 107 insertions(+), 2 deletions(-) diff --git a/lib/libutil/login_cap.c b/lib/libutil/login_cap.c index cea7630698af..ffefb865ed5e 100644 --- a/lib/libutil/login_cap.c +++ b/lib/libutil/login_cap.c @@ -94,6 +94,101 @@ allocarray(size_t sz) } +/* + * This is a variant of strcspn, which checks for quoted + * strings. That is,: + * strcspn_quote("how 'now, brown' cow", ",", NULL); + * will return the index for the nul, rather than the comma, because + * the string is quoted. It does not handle escaped characters + * at this time. + */ +static size_t +strcspn_quote(const char *str, const char *exclude, int *is_quoted) +{ + size_t indx = 0; + char quote = 0; + + if (str == NULL) + return 0; + + if (is_quoted) + *is_quoted = 0; + + for (indx = 0; str[indx] != 0; indx++) { + if (quote && str[indx] == quote) { + if (is_quoted) + *is_quoted = 1; + quote = 0; + continue; + } + if (quote == 0 && + (str[indx] == '\'' || str[indx] == '"')) { + quote = str[indx]; + continue; + } + if (quote == 0 && + strchr(exclude, str[indx]) != NULL) + return indx; + } + return indx; +} + +/* + * Remove quotes from the given string. + * It's a very simplistic approach: the first + * single or double quote it finds, it looks for + * the next one, and if it finds it, moves the + * entire string backwards in two chunks + * (first quote + 1 to first quote, length + * rest of string, and then second quote + 1 + * to second quote, length rest of the string). + */ +static void +remove_quotes(char *str) +{ + static const char *quote_chars = "'\""; + char qc = 0; + int found = 0; + + do { + char *loc = NULL; + + found = 0; + /* + * If qc is 0, then we haven't found + * a quote yet, so do a strcspn search. + */ + if (qc == 0) { + size_t indx; + indx = strcspn(str, quote_chars); + if (str[indx] == '\0') + return; /* We're done */ + loc = str + indx; + qc = str[indx]; + } else { + /* + * We've found a quote character, + * so use strchr to find the next one. + */ + loc = strchr(str, qc); + if (loc == NULL) + return; + qc = 0; + } + if (loc) { + /* + * This gives us the location of the + * quoted character. We need to move + * the entire string down, from loc+1 + * to loc. + */ + size_t len = strlen(loc + 1) + 1; + memmove(loc, loc + 1, len); + found = 1; + } + } while (found != 0); +} + /* * arrayize() * Turn a simple string separated by any of @@ -112,7 +207,7 @@ arrayize(const char *str, const char *chars, int *size) /* count the sub-strings */ for (i = 0, cptr = str; *cptr; i++) { - int count = strcspn(cptr, chars); + int count = strcspn_quote(cptr, chars, NULL); cptr += count; if (*cptr) ++cptr; @@ -126,11 +221,21 @@ arrayize(const char *str, const char *chars, int *size) /* now split the string */ i = 0; while (*ptr) { - int count = strcspn(ptr, chars); + int quoted = 0; + int count = strcspn_quote(ptr, chars, "ed); + char *base = ptr; res[i++] = ptr; ptr += count; if (*ptr) *ptr++ = '\0'; + /* + * If the string contains a quoted element, we + * need to remove the quotes. + */ + if (quoted) { + remove_quotes(base); + } + } res[i] = NULL; }