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