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