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 #include <stdio.h> 24 #include <unistd.h> 25 #include <string.h> 26 #include <stdlib.h> 27 #include <pwd.h> 28 #include <sys/types.h> 29 #include <netdb.h> 30 #include "libssh/libssh.h" 31 32 /* if the program was executed suid root, don't trust the user ! */ 33 static int is_trusted(){ 34 if(geteuid()!=getuid()) 35 return 0; 36 return 1; 37 } 38 39 static char *get_homedir_from_uid(int uid){ 40 struct passwd *pwd; 41 char *home; 42 while((pwd=getpwent())){ 43 if(pwd->pw_uid == uid){ 44 home=strdup(pwd->pw_dir); 45 endpwent(); 46 return home; 47 } 48 } 49 endpwent(); 50 return NULL; 51 } 52 53 static char *get_homedir_from_login(char *user){ 54 struct passwd *pwd; 55 char *home; 56 while((pwd=getpwent())){ 57 if(!strcmp(pwd->pw_name,user)){ 58 home=strdup(pwd->pw_dir); 59 endpwent(); 60 return home; 61 } 62 } 63 endpwent(); 64 return NULL; 65 } 66 67 char *ssh_get_user_home_dir(){ 68 char *home; 69 char *user; 70 int trusted=is_trusted(); 71 if(trusted){ 72 if((home=getenv("HOME"))) 73 return strdup(home); 74 if((user=getenv("USER"))) 75 return get_homedir_from_login(user); 76 } 77 return get_homedir_from_uid(getuid()); 78 } 79 80 /* we have read access on file */ 81 int ssh_file_readaccess_ok(char *file){ 82 if(!access(file,R_OK)) 83 return 1; 84 return 0; 85 } 86 87 88 u64 ntohll(u64 a){ 89 #ifdef WORDS_BIGENDIAN 90 return a; 91 #else 92 u32 low=a & 0xffffffff; 93 u32 high = a >> 32 ; 94 low=ntohl(low); 95 high=ntohl(high); 96 return (( ((u64)low) << 32) | ( high)); 97 #endif 98 } |