1 /* $NetBSD: stat_as.c,v 1.1.1.1 2009/06/23 10:09:01 tron Exp $ */ 2 3 /*++ 4 /* NAME 5 /* stat_as 3 6 /* SUMMARY 7 /* stat file as user 8 /* SYNOPSIS 9 /* #include <sys/stat.h> 10 /* #include <stat_as.h> 11 /* 12 /* int stat_as(path, st, euid, egid) 13 /* const char *path; 14 /* struct stat *st; 15 /* uid_t euid; 16 /* gid_t egid; 17 /* DESCRIPTION 18 /* stat_as() looks up the file status of the named \fIpath\fR, 19 /* using the effective rights specified by \fIeuid\fR 20 /* and \fIegid\fR, and stores the result into the structure pointed 21 /* to by \fIst\fR. A -1 result means the lookup failed. 22 /* This call follows symbolic links. 23 /* DIAGNOSTICS 24 /* Fatal error: no permission to change privilege level. 25 /* SEE ALSO 26 /* set_eugid(3) switch effective rights 27 /* LICENSE 28 /* .ad 29 /* .fi 30 /* The Secure Mailer license must be distributed with this software. 31 /* AUTHOR(S) 32 /* Wietse Venema 33 /* IBM T.J. Watson Research 34 /* P.O. Box 704 35 /* Yorktown Heights, NY 10598, USA 36 /*--*/ 37 38 /* System library. */ 39 40 #include <sys_defs.h> 41 #include <sys/stat.h> 42 #include <unistd.h> 43 44 /* Utility library. */ 45 46 #include "msg.h" 47 #include "set_eugid.h" 48 #include "stat_as.h" 49 50 /* stat_as - stat file as user */ 51 52 int stat_as(const char *path, struct stat * st, uid_t euid, gid_t egid) 53 { 54 uid_t saved_euid = geteuid(); 55 gid_t saved_egid = getegid(); 56 int status; 57 58 /* 59 * Switch to the target user privileges. 60 */ 61 set_eugid(euid, egid); 62 63 /* 64 * Stat that file. 65 */ 66 status = stat(path, st); 67 68 /* 69 * Restore saved privileges. 70 */ 71 set_eugid(saved_euid, saved_egid); 72 73 return (status); 74 } 75