#include "uucp.h"
#include "uudefs.h"
#include "sysdep.h"
#include "system.h"
#include <errno.h>
#if HAVE_FCNTL_H
#include <fcntl.h>
#else
#if HAVE_SYS_FILE_H
#include <sys/file.h>
#endif
#endif
#ifndef O_RDONLY
#define O_RDONLY 0
#define O_WRONLY 1
#define O_RDWR 2
#endif
#ifndef O_NOCTTY
#define O_NOCTTY 0
#endif
#ifndef FD_CLOEXEC
#define FD_CLOEXEC 1
#endif
openfile_t
esysdep_user_fopen (zfile, frd, fbinary)
const char *zfile;
boolean frd;
boolean fbinary ATTRIBUTE_UNUSED;
{
uid_t ieuid;
gid_t iegid;
openfile_t e;
const char *zerr;
int o = 0;
if (! fsuser_perms (&ieuid, &iegid))
return EFILECLOSED;
zerr = NULL;
#if USE_STDIO
e = fopen (zfile, frd ? "r" : "w");
if (e == NULL)
zerr = "fopen";
else
o = fileno (e);
#else
if (frd)
{
e = open ((char *) zfile, O_RDONLY | O_NOCTTY, 0);
zerr = "open";
}
else
{
e = creat ((char *) zfile, IPUBLIC_FILE_MODE);
zerr = "creat";
}
if (e >= 0)
{
o = e;
zerr = NULL;
}
#endif
if (! fsuucp_perms ((long) ieuid, (long) iegid))
{
if (ffileisopen (e))
(void) ffileclose (e);
return EFILECLOSED;
}
if (zerr != NULL)
{
ulog (LOG_ERROR, "%s (%s): %s", zerr, zfile, strerror (errno));
#if ! HAVE_SETREUID
#if HAVE_SAVED_SETUID
if (errno == EACCES && getuid () == 0)
ulog (LOG_ERROR,
"The superuser may only transfer files that are readable by %s",
OWNER);
#else
if (errno == EACCES)
ulog (LOG_ERROR,
"You may only transfer files that are readable by %s", OWNER);
#endif
#endif
return EFILECLOSED;
}
if (fcntl (o, F_SETFD, fcntl (o, F_GETFD, 0) | FD_CLOEXEC) < 0)
{
ulog (LOG_ERROR, "fcntl (FD_CLOEXEC): %s", strerror (errno));
(void) ffileclose (e);
return EFILECLOSED;
}
return e;
}