From: Todd C. Miller Date: Thu, 14 Sep 2000 20:48:58 +0000 (+0000) Subject: sudo_setenv() now exits on memory alloc failure instead of returning -1. X-Git-Tag: SUDO_1_6_4~255 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9745a31948eb6206b50dad3317342c97e8286831;p=sudo sudo_setenv() now exits on memory alloc failure instead of returning -1. --- diff --git a/sudo.c b/sudo.c index 70204d276..163b679b9 100644 --- a/sudo.c +++ b/sudo.c @@ -374,11 +374,7 @@ main(argc, argv) /* Replace the PATH envariable with a secure one. */ if (def_str(I_SECURE_PATH) && !user_is_exempt()) - if (sudo_setenv("PATH", def_str(I_SECURE_PATH))) { - (void) fprintf(stderr, "%s: cannot allocate memory!\n", - Argv[0]); - exit(1); - } + sudo_setenv("PATH", def_str(I_SECURE_PATH)); /* Restore coredumpsize resource limit. */ #if defined(RLIMIT_CORE) && !defined(SUDO_DEVEL) @@ -390,7 +386,7 @@ main(argc, argv) /* Set $HOME for `sudo -H'. Only valid at PERM_RUNAS. */ if ((sudo_mode & MODE_RESET_HOME) && runas_homedir) - (void) sudo_setenv("HOME", runas_homedir); + sudo_setenv("HOME", runas_homedir); #ifndef PROFILING if ((sudo_mode & MODE_BACKGROUND) && fork() > 0) @@ -739,10 +735,7 @@ add_env(contiguous) } else { buf = user_cmnd; } - if (sudo_setenv("SUDO_COMMAND", buf)) { - (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); - exit(1); - } + sudo_setenv("SUDO_COMMAND", buf); if (NewArgc > 1) free(buf); @@ -754,32 +747,16 @@ add_env(contiguous) user_args = NULL; } - /* Add the SUDO_USER environment variable. */ - if (sudo_setenv("SUDO_USER", user_name)) { - (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); - exit(1); - } - - /* Add the SUDO_UID environment variable. */ + /* Add the SUDO_USER, SUDO_UID, SUDO_GID environment variables. */ + sudo_setenv("SUDO_USER", user_name); (void) sprintf(idstr, "%ld", (long) user_uid); - if (sudo_setenv("SUDO_UID", idstr)) { - (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); - exit(1); - } - - /* Add the SUDO_GID environment variable. */ + sudo_setenv("SUDO_UID", idstr); (void) sprintf(idstr, "%ld", (long) user_gid); - if (sudo_setenv("SUDO_GID", idstr)) { - (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); - exit(1); - } + sudo_setenv("SUDO_GID", idstr); /* Set PS1 if SUDO_PS1 is set. */ if ((buf = getenv("SUDO_PS1"))) - if (sudo_setenv("PS1", buf)) { - (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); - exit(1); - } + sudo_setenv("PS1", buf); } /* @@ -949,18 +926,8 @@ set_perms(perm, sudo_mode) /* Set $USER and $LOGNAME to target user */ if (def_flag(I_LOGNAME)) { - if (sudo_setenv("USER", pw->pw_name)) { - (void) fprintf(stderr, - "%s: cannot allocate memory!\n", - Argv[0]); - exit(1); - } - if (sudo_setenv("LOGNAME", pw->pw_name)) { - (void) fprintf(stderr, - "%s: cannot allocate memory!\n", - Argv[0]); - exit(1); - } + sudo_setenv("USER", pw->pw_name); + sudo_setenv("LOGNAME", pw->pw_name); } if (def_flag(I_LOGINCLASS)) { diff --git a/sudo.h b/sudo.h index b80b9bc86..d8b34146b 100644 --- a/sudo.h +++ b/sudo.h @@ -192,7 +192,7 @@ int vasprintf __P((char **, const char *, va_list)); int strcasecmp __P((const char *, const char *)); #endif char *sudo_goodpath __P((const char *)); -int sudo_setenv __P((char *, char *)); +void sudo_setenv __P((char *, char *)); char *tgetpass __P((const char *, int, int)); int find_path __P((char *, char **)); void check_user __P((void)); diff --git a/sudo_setenv.c b/sudo_setenv.c index 55344413b..8162fc128 100644 --- a/sudo_setenv.c +++ b/sudo_setenv.c @@ -64,23 +64,28 @@ static const char rcsid[] = "$Sudo$"; /* - * Add a string of the form "var=val" to the environment. If we are unable - * to expand the current environent, return -1, else return 0. + * Add a string of the form "var=val" to the environment. Exits if it is + * unable to expand the current environent. */ -int +void sudo_setenv(var, val) char *var; char *val; { #ifdef HAVE_SETENV - return(setenv(var, val, 1)); + if (setenv(var, val, 1) == -1) { + (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); + exit(1); + } #else char *envstring, *tmp; envstring = tmp = (char *) malloc(strlen(var) + strlen(val) + 2); - if (envstring == NULL) - return(-1); + if (envstring == NULL) { + (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]); + exit(1); + } while ((*tmp++ = *var++)) ; @@ -90,6 +95,6 @@ sudo_setenv(var, val) while ((*tmp++ = *val++)) ; - return(putenv(envstring)); + putenv(envstring); #endif /* HAVE_SETENV */ }