From 4690d3ecf69d50910d2480b4df38249a88ed2f1a Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Mon, 23 Dec 2019 13:15:34 -0700 Subject: [PATCH] Add cfmakeraw() for systems without it. --- MANIFEST | 1 + config.h.in | 3 +++ configure | 26 +++++++++++++++++++ configure.ac | 4 +++ include/sudo_compat.h | 6 +++++ lib/util/cfmakeraw.c | 59 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 99 insertions(+) create mode 100644 lib/util/cfmakeraw.c diff --git a/MANIFEST b/MANIFEST index 930a1987e..688c6f212 100644 --- a/MANIFEST +++ b/MANIFEST @@ -109,6 +109,7 @@ lib/util/aix.c lib/util/arc4random.c lib/util/arc4random.h lib/util/arc4random_uniform.c +lib/util/cfmakeraw.c lib/util/chacha_private.h lib/util/closefrom.c lib/util/digest.c diff --git a/config.h.in b/config.h.in index c80e2a538..7966d5559 100644 --- a/config.h.in +++ b/config.h.in @@ -82,6 +82,9 @@ /* Define to 1 to enable BSM audit support. */ #undef HAVE_BSM_AUDIT +/* Define to 1 if you have the `cfmakeraw' function. */ +#undef HAVE_CFMAKERAW + /* Define to 1 if you have the `clock_gettime' function. */ #undef HAVE_CLOCK_GETTIME diff --git a/configure b/configure index 69391022b..d6a8454a0 100755 --- a/configure +++ b/configure @@ -19638,6 +19638,32 @@ done fi ;; esac +for ac_func in cfmakeraw +do : + ac_fn_c_check_func "$LINENO" "cfmakeraw" "ac_cv_func_cfmakeraw" +if test "x$ac_cv_func_cfmakeraw" = xyes; then : + cat >>confdefs.h <<_ACEOF +#define HAVE_CFMAKERAW 1 +_ACEOF + +else + + case " $LIBOBJS " in + *" cfmakeraw.$ac_objext "* ) ;; + *) LIBOBJS="$LIBOBJS cfmakeraw.$ac_objext" + ;; +esac + + + for _sym in sudo_cfmakeraw; do + COMPAT_EXP="${COMPAT_EXP}${_sym} +" + done + + +fi +done + for ac_func in getgrouplist do : ac_fn_c_check_func "$LINENO" "getgrouplist" "ac_cv_func_getgrouplist" diff --git a/configure.ac b/configure.ac index 494f505b9..510de90d2 100644 --- a/configure.ac +++ b/configure.ac @@ -2574,6 +2574,10 @@ case "$host_os" in fi ;; esac +AC_CHECK_FUNCS([cfmakeraw], [], [ + AC_LIBOBJ(cfmakeraw) + SUDO_APPEND_COMPAT_EXP(sudo_cfmakeraw) +]) AC_CHECK_FUNCS([getgrouplist], [], [ case "$host_os" in aix*) diff --git a/include/sudo_compat.h b/include/sudo_compat.h index a8c932185..cbc7aca10 100644 --- a/include/sudo_compat.h +++ b/include/sudo_compat.h @@ -392,7 +392,13 @@ int getdomainname(char *, size_t); struct passwd; struct stat; struct timespec; +struct termios; +#ifndef HAVE_CFMAKERAW +__dso_public void sudo_cfmakeraw(struct termios *term); +# undef cfmakeraw +# define cfmakeraw(_a) sudo_cfmakeraw((_a)) +#endif /* HAVE_CFMAKERAW */ #ifndef HAVE_CLOSEFROM __dso_public void sudo_closefrom(int); # undef closefrom diff --git a/lib/util/cfmakeraw.c b/lib/util/cfmakeraw.c new file mode 100644 index 000000000..1985131f2 --- /dev/null +++ b/lib/util/cfmakeraw.c @@ -0,0 +1,59 @@ +/* + * SPDX-License-Identifier: ISC + * + * Copyright (c) 2019 Todd C. Miller + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +/* + * This is an open source non-commercial project. Dear PVS-Studio, please check it. + * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com + */ + +#include + +#include +#include + +#include "sudo_compat.h" + +/* Non-standard termios input flags */ +#ifndef IUCLC +# define IUCLC 0 +#endif +#ifndef IMAXBEL +# define IMAXBEL 0 +#endif + +/* Non-standard termios local flags */ +#ifndef IEXTEN +# define IEXTEN 0 +#endif + +/* + * Set termios to raw mode (BSD extension). + */ +void +sudo_cfmakeraw(struct termios *term) +{ + /* Set terminal to raw mode */ + CLR(term->c_iflag, + IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON|IMAXBEL|IUCLC); + CLR(term->c_oflag, OPOST); + CLR(term->c_lflag, ECHO|ECHONL|ICANON|ISIG|IEXTEN); + CLR(term->c_cflag, CSIZE|PARENB); + SET(term->c_cflag, CS8); + term->c_cc[VMIN] = 1; + term->c_cc[VTIME] = 0; +}