1 /* misc.c */ 2 /* some misc routines than aren't really part of the ssh protocols but can be useful to the client */ 3 4 /* 5 Copyright 2003 Aris Adamantiadis 6 7 This file is part of the SSH Library 8 9 The SSH Library is free software; you can redistribute it and/or modify 10 it under the terms of the GNU Lesser General Public License as published by 11 the Free Software Foundation; either version 2.1 of the License, or (at your 12 option) any later version. 13 14 The SSH Library is distributed in the hope that it will be useful, but 15 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 16 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 17 License for more details. 18 19 You should have received a copy of the GNU Lesser General Public License 20 along with the SSH Library; see the file COPYING. If not, write to 21 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, 22 MA 02111-1307, USA. */ 23 24 #include "libssh/priv.h" 25 26 #include <stdio.h> 27 #include <unistd.h> 28 #include <string.h> 29 #include <stdlib.h> 30 #ifdef HAVE_PWD_H 31 #include <pwd.h> 32 #endif 33 #include <sys/types.h> 34 #ifdef HAVE_NETDB_H 35 #include <netdb.h> 36 #endif 37 #include "libssh/libssh.h" 38 39 /* if the program was executed suid root, don't trust the user ! */ 40 static int is_trusted(){ 41 #ifdef HAVE_GETUID 42 # ifdef HAVE_GETEUID 43 if(geteuid()!=getuid()) 44 return 0; 45 # endif 46 #endif 47 return 1; 48 } 49 50 static char *get_homedir_from_uid(int uid){ 51 char *home; 52 #ifdef HAVE_GETPWENT 53 struct passwd *pwd; 54 while((pwd=getpwent())){ 55 if(pwd->pw_uid == uid){ 56 home=strdup(pwd->pw_dir); 57 endpwent(); 58 return home; 59 } 60 } 61 endpwent(); 62 #else 63 #ifdef __WIN32__ 64 home = strdup("c:/"); 65 return(home); 66 #endif 67 #endif 68 return NULL; 69 } 70 71 static char *get_homedir_from_login(char *user){ 72 #ifdef HAVE_GETPWENT 73 struct passwd *pwd; 74 char *home; 75 while((pwd=getpwent())){ 76 if(!strcmp(pwd->pw_name,user)){ 77 home=strdup(pwd->pw_dir); 78 endpwent(); 79 return home; 80 } 81 } 82 endpwent(); 83 #endif 84 return NULL; 85 } 86 87 char *ssh_get_user_home_dir(){ 88 char *home; 89 char *user; 90 int trusted=is_trusted(); 91 if(trusted){ 92 if((home=getenv("HOME"))) 93 return strdup(home); 94 if((user=getenv("USER"))) 95 return get_homedir_from_login(user); 96 } 97 #ifdef HAVE_GETUID 98 return get_homedir_from_uid(getuid()); 99 #else 100 return get_homedir_from_uid(0); 101 #endif 102 } 103 104 /* we have read access on file */ 105 int ssh_file_readaccess_ok(char *file){ 106 if(!access(file,R_OK)) 107 return 1; 108 return 0; 109 } 110 111 112 u64 ntohll(u64 a){ 113 #ifdef WORDS_BIGENDIAN 114 return a; 115 #else 116 u32 low=a & 0xffffffff; 117 u32 high = a >> 32 ; 118 low=ntohl(low); 119 high=ntohl(high); 120 return (( ((u64)low) << 32) | ( high)); 121 #endif 122 } |