Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / ibm-public / postfix / dist / src / util / stat_as.c
blob0380d350b14829f739c2d2fe71ce667a5f05a0fa
1 /* $NetBSD$ */
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>
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 /*--*/
38 /* System library. */
40 #include <sys_defs.h>
41 #include <sys/stat.h>
42 #include <unistd.h>
44 /* Utility library. */
46 #include "msg.h"
47 #include "set_eugid.h"
48 #include "stat_as.h"
50 /* stat_as - stat file as user */
52 int stat_as(const char *path, struct stat * st, uid_t euid, gid_t egid)
54 uid_t saved_euid = geteuid();
55 gid_t saved_egid = getegid();
56 int status;
59 * Switch to the target user privileges.
61 set_eugid(euid, egid);
64 * Stat that file.
66 status = stat(path, st);
69 * Restore saved privileges.
71 set_eugid(saved_euid, saved_egid);
73 return (status);