From a104c2eda38d2fbf81285e0223ed755783792169 Mon Sep 17 00:00:00 2001 From: Aris Adamantiadis Date: Fri, 19 Dec 2008 15:29:33 +0000 Subject: http://www.cynapses.org/tmp/patches/libssh/0001-Replace-getenv-USER-with-getpwuid-functions.patch from andreas (mostly userdirectory cleaning) git-svn-id: svn+ssh://svn.berlios.de/svnroot/repos/libssh/trunk@193 7dcaeef0-15fb-0310-b436-a5af3365683c --- libssh/misc.c | 54 +++++++++++------------------------------------------- libssh/options.c | 32 ++++++++++++++------------------ 2 files changed, 25 insertions(+), 61 deletions(-) diff --git a/libssh/misc.c b/libssh/misc.c index 963748fb..0ec37b2b 100644 --- a/libssh/misc.c +++ b/libssh/misc.c @@ -20,6 +20,8 @@ You should have received a copy of the GNU Lesser General Public License along with the SSH Library; see the file COPYING. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + +#include #include #include #include @@ -37,52 +39,18 @@ MA 02111-1307, USA. */ #include "libssh/libssh.h" #ifndef _WIN32 -/* if the program was executed suid root, don't trust the user ! */ -static int is_trusted(){ - if(geteuid()!=getuid()) - return 0; - return 1; -} +char *ssh_get_user_home_dir(){ + static char szPath[PATH_MAX] = {0}; + struct passwd *pwd = NULL; -static char *get_homedir_from_uid(int uid){ - struct passwd *pwd; - char *home; - while((pwd=getpwent())){ - if(pwd->pw_uid == uid){ - home=strdup(pwd->pw_dir); - endpwent(); - return home; - } + pwd = getpwuid(getuid()); + if (pwd == NULL) { + return NULL; } - endpwent(); - return NULL; -} -static char *get_homedir_from_login(char *user){ - struct passwd *pwd; - char *home; - while((pwd=getpwent())){ - if(!strcmp(pwd->pw_name,user)){ - home=strdup(pwd->pw_dir); - endpwent(); - return home; - } - } - endpwent(); - return NULL; -} - -char *ssh_get_user_home_dir(){ - char *home; - char *user; - int trusted=is_trusted(); - if(trusted){ - if((home=getenv("HOME"))) - return strdup(home); - if((user=getenv("USER"))) - return get_homedir_from_login(user); - } - return get_homedir_from_uid(getuid()); + snprintf(szPath, PATH_MAX - 1, "%s", pwd->pw_dir); + + return szPath; } #else /* _WIN32 */ diff --git a/libssh/options.c b/libssh/options.c index d95b3292..762980d2 100644 --- a/libssh/options.c +++ b/libssh/options.c @@ -294,32 +294,28 @@ int ssh_options_set_wanted_algos(SSH_OPTIONS *opt, int algo, const char *list){ } #ifndef _WIN32 -static char *get_username_from_uid(SSH_OPTIONS *opt, int uid){ - struct passwd *pwd; - char *user; - while((pwd=getpwent())){ - if(pwd->pw_uid == uid){ - user=strdup(pwd->pw_name); - endpwent(); - return user; - } +static char *get_username_from_uid(SSH_OPTIONS *opt, uid_t uid){ + struct passwd *pwd = NULL; + + pwd = getpwuid(uid); + + if (pwd == NULL) { + ssh_set_error(opt,SSH_FATAL,"uid %d doesn't exist !",uid); + return NULL; } - endpwent(); - ssh_set_error(opt,SSH_FATAL,"uid %d doesn't exist !",uid); - return NULL; + + return strdup(pwd->pw_name); } #endif /* this function must be called when no specific username has been asked. it has to guess it */ int ssh_options_default_username(SSH_OPTIONS *opt){ - char *user; - if(opt->username) - return 0; - user=getenv("USER"); - if(user){ - opt->username=strdup(user); + char *user = NULL; + + if (opt->username) { return 0; } + #ifndef _WIN32 user=get_username_from_uid(opt,getuid()); if(user){ -- cgit v1.2.3