from andreas (mostly userdirectory cleaning)


git-svn-id: svn+ssh://svn.berlios.de/svnroot/repos/libssh/trunk@193 7dcaeef0-15fb-0310-b436-a5af3365683c
This commit is contained in:
Aris Adamantiadis
2008-12-19 15:29:33 +00:00
parent 16a3379a61
commit a104c2eda3
2 changed files with 26 additions and 62 deletions

View File

@@ -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 along with the SSH Library; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA. */ MA 02111-1307, USA. */
#include <limits.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
@@ -37,52 +39,18 @@ MA 02111-1307, USA. */
#include "libssh/libssh.h" #include "libssh/libssh.h"
#ifndef _WIN32 #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;
}
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;
}
}
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 *ssh_get_user_home_dir(){
char *home; static char szPath[PATH_MAX] = {0};
char *user; struct passwd *pwd = NULL;
int trusted=is_trusted();
if(trusted){ pwd = getpwuid(getuid());
if((home=getenv("HOME"))) if (pwd == NULL) {
return strdup(home); return NULL;
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 */ #else /* _WIN32 */

View File

@@ -294,32 +294,28 @@ int ssh_options_set_wanted_algos(SSH_OPTIONS *opt, int algo, const char *list){
} }
#ifndef _WIN32 #ifndef _WIN32
static char *get_username_from_uid(SSH_OPTIONS *opt, int uid){ static char *get_username_from_uid(SSH_OPTIONS *opt, uid_t uid){
struct passwd *pwd; struct passwd *pwd = NULL;
char *user;
while((pwd=getpwent())){ pwd = getpwuid(uid);
if(pwd->pw_uid == uid){
user=strdup(pwd->pw_name); if (pwd == NULL) {
endpwent();
return user;
}
}
endpwent();
ssh_set_error(opt,SSH_FATAL,"uid %d doesn't exist !",uid); ssh_set_error(opt,SSH_FATAL,"uid %d doesn't exist !",uid);
return NULL; return NULL;
}
return strdup(pwd->pw_name);
} }
#endif #endif
/* this function must be called when no specific username has been asked. it has to guess it */ /* 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){ int ssh_options_default_username(SSH_OPTIONS *opt){
char *user; char *user = NULL;
if(opt->username)
return 0; if (opt->username) {
user=getenv("USER");
if(user){
opt->username=strdup(user);
return 0; return 0;
} }
#ifndef _WIN32 #ifndef _WIN32
user=get_username_from_uid(opt,getuid()); user=get_username_from_uid(opt,getuid());
if(user){ if(user){