xref: /netbsd-src/external/ibm-public/postfix/dist/src/util/stat_as.c (revision a30b880ed60a24c405edba78187a04247f4d9d33)
1 /*	$NetBSD: stat_as.c,v 1.1.1.2 2013/01/02 18:59:14 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 #include "warn_stat.h"
50 
51 /* stat_as - stat file as user */
52 
stat_as(const char * path,struct stat * st,uid_t euid,gid_t egid)53 int     stat_as(const char *path, struct stat * st, uid_t euid, gid_t egid)
54 {
55     uid_t   saved_euid = geteuid();
56     gid_t   saved_egid = getegid();
57     int     status;
58 
59     /*
60      * Switch to the target user privileges.
61      */
62     set_eugid(euid, egid);
63 
64     /*
65      * Stat that file.
66      */
67     status = stat(path, st);
68 
69     /*
70      * Restore saved privileges.
71      */
72     set_eugid(saved_euid, saved_egid);
73 
74     return (status);
75 }
76