From 80287c981cbb8fc38583c48eac56554bd25c3f3c Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Mon, 17 May 1999 01:36:30 +0000 Subject: [PATCH] Move interface-related defines to interfaces.h so we don't have to include everywhere. --- Makefile.in | 8 +++--- check.c | 1 - check_sia.c | 1 - compat.h | 31 +++++++++------------ find_path.c | 1 - getspwuid.c | 1 - goodpath.c | 1 - interfaces.c | 10 +------ interfaces.h | 48 ++++++++++++++++++++++++++++++++ lex.yy.c | 1 - logging.c | 1 - parse.c | 1 + parse.lex | 1 - parse.yacc | 1 - secureware.c | 1 - sudo.c | 7 +++-- sudo.h | 21 ++++++-------- sudo.tab.c | 1 - sudo_setenv.c | 1 - testsudoers.c | 1 + tgetpass.c | 77 +++++++++++++++++++-------------------------------- visudo.c | 1 - 22 files changed, 109 insertions(+), 108 deletions(-) create mode 100644 interfaces.h diff --git a/Makefile.in b/Makefile.in index 761ab3ab4..ceacef970 100644 --- a/Makefile.in +++ b/Makefile.in @@ -110,8 +110,8 @@ TESTOBJS = interfaces.o testsudoers.o LIBOBJS = @LIBOBJS@ @ALLOCA@ -HDRS = sudo.h compat.h version.h insults.h \ - ins_2001.h ins_classic.h ins_goons.h ins_csops.h sudo.tab.h +HDRS = sudo.h compat.h version.h insults.h ins_2001.h ins_classic.h \ + ins_goons.h ins_csops.h interfaces.h sudo.tab.h VERSION = 1.6 @@ -129,9 +129,9 @@ DISTFILES = $(SRCS) $(HDRS) BUGS CHANGES COPYING HISTORY INSTALL \ VERSIONFILES = emul/utime.h check.c compat.h config.h.in dce_pwent.c \ find_path.c getspwuid.c getcwd.c goodpath.c ins_2001.h \ - ins_classic.h ins_csops.h ins_goons.h insults.h interfaces.c \ + ins_classic.h ins_csops.h ins_goons.h insults.h interfaces.h \ logging.c parse.c parse.lex parse.yacc pathnames.h.in \ - putenv.c sudo.c sudo.h sudo_setenv.c testsudoers.c \ + putenv.c sudo.c sudo.h sudo_setenv.c testsudoers.c interfaces.c \ tgetpass.c utime.c visudo.c secureware.c check_sia.c alloc.c all: $(PROGS) diff --git a/check.c b/check.c index b29b2d241..fe9d57010 100644 --- a/check.c +++ b/check.c @@ -50,7 +50,6 @@ #include #include #include -#include #include #include #ifdef HAVE_KERB4 diff --git a/check_sia.c b/check_sia.c index 45f8c2681..681eb6fc2 100644 --- a/check_sia.c +++ b/check_sia.c @@ -45,7 +45,6 @@ #endif /* HAVE_STRINGS_H */ #include #include -#include #include #include diff --git a/compat.h b/compat.h index 28c90aa25..20dbca6e3 100644 --- a/compat.h +++ b/compat.h @@ -86,29 +86,24 @@ #endif /* S_IRWXU */ /* - * Some OS's may not have this. + * In case this is not defined in or */ #ifndef howmany #define howmany(x, y) (((x) + ((y) - 1)) / (y)) #endif /* - * We used to use the system definition of PASS_MAX or _PASSWD_LEN, - * but that caused problems with various alternate authentication - * methods. So, we just define our own and assume that it is >= the - * system max. - */ -#define SUDO_PASS_MAX 256 - -/* - * Some OS's lack these + * These should be defined in but not everyone has them. */ -#ifndef UID_NO_CHANGE -# define UID_NO_CHANGE ((uid_t) -1) -#endif /* UID_NO_CHANGE */ -#ifndef GID_NO_CHANGE -# define GID_NO_CHANGE ((gid_t) -1) -#endif /* GID_NO_CHANGE */ +#ifndef STDIN_FILENO +# define STDIN_FILENO 0 +#endif +#ifndef STDOUT_FILENO +# define STDOUT_FILENO 1 +#endif +#ifndef STDERR_FILENO +# define STDERR_FILENO 2 +#endif /* * Emulate seteuid() for AIX via setuidx() -- needed for some versions of AIX @@ -125,9 +120,9 @@ */ #ifndef HAVE_SETEUID # ifdef __hpux -# define seteuid(_EUID) (setresuid(UID_NO_CHANGE, _EUID, UID_NO_CHANGE)) +# define seteuid(_EUID) (setresuid((uid_t) -1, _EUID, (uid_t) -1)) # else -# define seteuid(_EUID) (setreuid(UID_NO_CHANGE, _EUID)) +# define seteuid(_EUID) (setreuid((uid_t) -1, _EUID)) # endif /* __hpux */ #endif /* HAVE_SETEUID */ diff --git a/find_path.c b/find_path.c index 84e81f221..859706dd0 100644 --- a/find_path.c +++ b/find_path.c @@ -48,7 +48,6 @@ #include #include #include -#include #include "sudo.h" #ifndef STDC_HEADERS diff --git a/getspwuid.c b/getspwuid.c index 8ca5f6a96..748eee30a 100644 --- a/getspwuid.c +++ b/getspwuid.c @@ -43,7 +43,6 @@ #include #include #include -#include #include #ifdef HAVE_GETSPNAM # include diff --git a/goodpath.c b/goodpath.c index 93d334c4a..6f174e055 100644 --- a/goodpath.c +++ b/goodpath.c @@ -45,7 +45,6 @@ #include #include #include -#include #include "sudo.h" diff --git a/interfaces.c b/interfaces.c index a6899cce7..e26ecb3d1 100644 --- a/interfaces.c +++ b/interfaces.c @@ -70,20 +70,12 @@ #include #include "sudo.h" -#include "version.h" +#include "interfaces.h" #ifndef lint static const char rcsid[] = "$Sudo$"; #endif /* lint */ -/* - * Globals - */ -struct interface *interfaces; -int num_interfaces = 0; -extern int Argc; -extern char **Argv; - #if defined(SIOCGIFCONF) && !defined(STUB_LOAD_INTERFACES) /********************************************************************** diff --git a/interfaces.h b/interfaces.h new file mode 100644 index 000000000..74a0c67f8 --- /dev/null +++ b/interfaces.h @@ -0,0 +1,48 @@ +/* + * CU sudo version 1.6 + * Copyright (c) 1996, 1998, 1999 Todd C. Miller + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 1, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * Please send bugs, changes, problems to sudo-bugs@courtesan.com + * + * $Sudo$ + */ + +#ifndef _SUDO_INTERFACES_H +#define _SUDO_INTERFACES_H + +/* + * IP address and netmask pairs for checking against local interfaces. + */ +struct interface { + struct in_addr addr; + struct in_addr netmask; +}; + +/* + * Prototypes for external functions. + */ +void load_interfaces __P((void)); + +/* + * Definitions for external variables. + */ +#ifndef MAIN +extern struct interface *interfaces; +extern int num_interfaces; +#endif + +#endif /* _SUDO_INTERFACES_H */ diff --git a/lex.yy.c b/lex.yy.c index 1667083d3..b9cadc8da 100644 --- a/lex.yy.c +++ b/lex.yy.c @@ -571,7 +571,6 @@ char *yytext; #include #include #include -#include #include "sudo.h" #include "sudo.tab.h" diff --git a/logging.c b/logging.c index 4892069af..830936547 100644 --- a/logging.c +++ b/logging.c @@ -53,7 +53,6 @@ #include #include #include -#include #include "sudo.h" diff --git a/parse.c b/parse.c index f3b64e7bc..f18a8f867 100644 --- a/parse.c +++ b/parse.c @@ -77,6 +77,7 @@ #endif #include "sudo.h" +#include "interfaces.h" #ifndef lint static const char rcsid[] = "$Sudo$"; diff --git a/parse.lex b/parse.lex index e92402b53..383a34941 100644 --- a/parse.lex +++ b/parse.lex @@ -43,7 +43,6 @@ #include #include #include -#include #include "sudo.h" #include "sudo.tab.h" diff --git a/parse.yacc b/parse.yacc index 043dfd144..28a377758 100644 --- a/parse.yacc +++ b/parse.yacc @@ -38,7 +38,6 @@ #include #include #include -#include #ifdef HAVE_STRING_H #include #endif /* HAVE_STRING_H */ diff --git a/secureware.c b/secureware.c index f85ad38bb..af6f72c85 100644 --- a/secureware.c +++ b/secureware.c @@ -44,7 +44,6 @@ #endif /* HAVE_STRINGS_H */ #include #include -#include #include #ifdef __hpux # include diff --git a/sudo.c b/sudo.c index b8699d663..3a2c9cc52 100644 --- a/sudo.c +++ b/sudo.c @@ -77,6 +77,7 @@ #endif /* HAVE_KERB5 */ #include "sudo.h" +#include "interfaces.h" #include "version.h" #ifndef STDC_HEADERS @@ -131,8 +132,8 @@ char *shost; char cwd[MAXPATHLEN]; FILE *sudoers_fp = NULL; static char *runas_homedir = NULL; -extern struct interface *interfaces; -extern int num_interfaces; +struct interface *interfaces; +int num_interfaces; extern int printmatches; int arg_prompt = 0; /* was -p used? */ #ifdef HAVE_KERB5 @@ -869,7 +870,7 @@ check_sudoers() (void) fprintf(stderr, "%s: fixed mode on %s\n", Argv[0], _PATH_SUDO_SUDOERS); if (statbuf.st_gid != SUDOERS_GID) { - if (!chown(_PATH_SUDO_SUDOERS,GID_NO_CHANGE,SUDOERS_GID)) { + if (!chown(_PATH_SUDO_SUDOERS,(uid_t) -1,SUDOERS_GID)) { (void) fprintf(stderr, "%s: set group on %s\n", Argv[0], _PATH_SUDO_SUDOERS); statbuf.st_gid = SUDOERS_GID; diff --git a/sudo.h b/sudo.h index 0ac1b6903..a414997db 100644 --- a/sudo.h +++ b/sudo.h @@ -28,14 +28,6 @@ #include #include "compat.h" -/* - * IP address and netmask pairs for checking against local interfaces. - */ -struct interface { - struct in_addr addr; - struct in_addr netmask; -}; - /* * Data structure used in parsing sudoers; * top of stack values are the ones that @@ -222,6 +214,14 @@ struct generic_alias { #define GETPASS(p, t) tgetpass(p, t) #endif +/* + * We used to use the system definition of PASS_MAX or _PASSWD_LEN, + * but that caused problems with various alternate authentication + * methods. So, we just define our own and assume that it is >= the + * system max. + */ +#define SUDO_PASS_MAX 256 + /* * Function prototypes */ @@ -235,7 +235,7 @@ int putenv __P((const char *)); #endif char *sudo_goodpath __P((const char *)); int sudo_setenv __P((char *, char *)); -char *tgetpass __P((char *, int)); +char *tgetpass __P((const char *, int)); int find_path __P((char *, char **)); void log_error __P((int)); void inform_user __P((int)); @@ -243,7 +243,6 @@ void check_user __P((void)); int validate __P((int)); void set_perms __P((int, int)); void remove_timestamp __P((void)); -void load_interfaces __P((void)); int check_secureware __P((char *)); void sia_attempt_auth __P((void)); int yyparse __P((void)); @@ -262,8 +261,6 @@ YY_DECL; extern char host[]; extern char *shost; extern char cwd[]; -extern struct interface *interfaces; -extern int num_interfaces; extern struct passwd *user_pw_ent; extern char *runas_user; extern char *tty; diff --git a/sudo.tab.c b/sudo.tab.c index e63b94566..83076666b 100644 --- a/sudo.tab.c +++ b/sudo.tab.c @@ -56,7 +56,6 @@ static char yyrcsid[] #include #include #include -#include #ifdef HAVE_STRING_H #include #endif /* HAVE_STRING_H */ diff --git a/sudo_setenv.c b/sudo_setenv.c index 963cad4ad..5e6af077b 100644 --- a/sudo_setenv.c +++ b/sudo_setenv.c @@ -40,7 +40,6 @@ #endif /* HAVE_UNISTD_H */ #include #include -#include #include "sudo.h" diff --git a/testsudoers.c b/testsudoers.c index 9d58ffbee..13a44d173 100644 --- a/testsudoers.c +++ b/testsudoers.c @@ -63,6 +63,7 @@ #include #include "sudo.h" +#include "interfaces.h" #ifndef lint static const char rcsid[] = "$Sudo$"; diff --git a/tgetpass.c b/tgetpass.c index 1a4d6b530..4f4f1b994 100644 --- a/tgetpass.c +++ b/tgetpass.c @@ -67,8 +67,7 @@ #endif /* HAVE_TERMIO_H */ #endif /* HAVE_TERMIOS_H */ -#include -#include "compat.h" +#include "sudo.h" #ifndef TCSASOFT #define TCSASOFT 0 @@ -108,8 +107,7 @@ tgetpass(prompt, timeout) #else int oldmask; #endif /* POSIX_SIGNALS */ - int n, echo; - FILE *input, *output; + int n, echo, input, output; static char buf[SUDO_PASS_MAX + 1]; fd_set *readfds; struct timeval tv; @@ -130,45 +128,36 @@ tgetpass(prompt, timeout) * open /dev/tty for reading/writing if possible or use * stdin and stderr instead. */ - if ((input = fopen(_PATH_TTY, "r+")) == NULL) { - input = stdin; - output = stderr; - } else { - output = input; - setbuf(output, NULL); + if ((input = output = open(_PATH_TTY, O_RDWR)) == NULL) { + input = STDIN_FILENO; + output = STDERR_FILENO; } /* print the prompt */ if (prompt) - fputs(prompt, output); - - /* rewind if necessary */ - if (input == output) { - (void) fflush(output); - (void) rewind(output); - } + (void) write(output, prompt, strlen(prompt) + 1); /* * turn off echo */ #ifdef HAVE_TERMIOS_H - (void) tcgetattr(fileno(input), &term); + (void) tcgetattr(input, &term); if ((echo = (term.c_lflag & ECHO))) { term.c_lflag &= ~ECHO; - (void) tcsetattr(fileno(input), TCSAFLUSH|TCSASOFT, &term); + (void) tcsetattr(input, TCSAFLUSH|TCSASOFT, &term); } #else #ifdef HAVE_TERMIO_H - (void) ioctl(fileno(input), TCGETA, &term); + (void) ioctl(input, TCGETA, &term); if ((echo = (term.c_lflag & ECHO))) { term.c_lflag &= ~ECHO; - (void) ioctl(fileno(input), TCSETA, &term); + (void) ioctl(input, TCSETA, &term); } #else - (void) ioctl(fileno(input), TIOCGETP, &ttyb); + (void) ioctl(input, TIOCGETP, &ttyb); if ((echo = (ttyb.sg_flags & ECHO))) { ttyb.sg_flags &= ~ECHO; - (void) ioctl(fileno(input), TIOCSETP, &ttyb); + (void) ioctl(input, TIOCSETP, &ttyb); } #endif /* HAVE_TERMIO_H */ #endif /* HAVE_TERMIOS_H */ @@ -178,14 +167,10 @@ tgetpass(prompt, timeout) */ if (timeout > 0) { /* setup for select(2) */ - n = howmany(fileno(input) + 1, NFDBITS) * sizeof(fd_mask); - if ((readfds = (fd_set *) malloc(n)) == NULL) { - (void) fprintf(stderr, "Cannot allocate memory: "); - perror(""); - return(NULL); - } + n = howmany(input + 1, NFDBITS) * sizeof(fd_mask); + readfds = (fd_set *) emalloc(n); (void) memset((VOID *)readfds, 0, n); - FD_SET(fileno(input), readfds); + FD_SET(input, readfds); /* set timeout for select */ tv.tv_sec = timeout; @@ -195,21 +180,21 @@ tgetpass(prompt, timeout) * get password or return empty string if nothing to read by timeout */ buf[0] = '\0'; - while ((n = select(fileno(input) + 1, readfds, 0, 0, &tv)) == -1 && + while ((n = select(input + 1, readfds, 0, 0, &tv)) == -1 && errno == EINTR) ; - if (n != 0 && fgets(buf, sizeof(buf), input)) { - n = strlen(buf); + if (n != 0 && (n = read(input, buf, sizeof(buf) - 1)) > 0) { if (buf[n - 1] == '\n') - buf[n - 1] = '\0'; + n--; + buf[n] = '\0'; } free(readfds); } else { buf[0] = '\0'; - if (fgets(buf, sizeof(buf), input)) { - n = strlen(buf); + if ((n = read(input, buf, sizeof(buf) - 1)) > 0) { if (buf[n - 1] == '\n') - buf[n - 1] = '\0'; + n--; + buf[n] = '\0'; } } @@ -217,30 +202,24 @@ tgetpass(prompt, timeout) #ifdef HAVE_TERMIOS_H if (echo) { term.c_lflag |= ECHO; - (void) tcsetattr(fileno(input), TCSAFLUSH|TCSASOFT, &term); + (void) tcsetattr(input, TCSAFLUSH|TCSASOFT, &term); } #else #ifdef HAVE_TERMIO_H if (echo) { term.c_lflag |= ECHO; - (void) ioctl(fileno(input), TCSETA, &term); + (void) ioctl(input, TCSETA, &term); } #else if (echo) { ttyb.sg_flags |= ECHO; - (void) ioctl(fileno(input), TIOCSETP, &ttyb); + (void) ioctl(input, TIOCSETP, &ttyb); } #endif /* HAVE_TERMIO_H */ #endif /* HAVE_TERMIOS_H */ - /* rewind if necessary */ - if (input == output) { - (void) fflush(output); - (void) rewind(output); - } - /* print a newline since echo is turned off */ - (void) fputc('\n', output); + (void) write(output, "\n", 1); /* restore old signal mask */ #ifdef POSIX_SIGNALS @@ -250,8 +229,8 @@ tgetpass(prompt, timeout) #endif /* close /dev/tty if that's what we opened */ - if (input != stdin) - (void) fclose(input); + if (input != STDIN_FILENO) + (void) close(input); return(buf); } diff --git a/visudo.c b/visudo.c index b7d309aa0..813a8228d 100644 --- a/visudo.c +++ b/visudo.c @@ -53,7 +53,6 @@ #include #include #include -#include #include "sudo.h" #include "version.h" -- 2.50.1