#include "uucp.h"
#include "uudefs.h"
#include "uuconf.h"
#include "system.h"
#include "sysdep.h"
#include <errno.h>
#include <pwd.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
#if ! HAVE_GETHOSTNAME && HAVE_UNAME
#include <sys/utsname.h>
#endif
#if HAVE_GETCWD
#undef HAVE_GETWD
#define HAVE_GETWD 0
#else
#if ! HAVE_GETWD
#undef HAVE_GETCWD
#define HAVE_GETCWD 1
#endif
#endif
#if HAVE_GETWD
#if HAVE_SYS_PARAMS_H
#include <sys/params.h>
#endif
#if HAVE_LIMITS_H
#include <limits.h>
#endif
#ifndef MAXPATHLEN
#ifdef PATH_MAX
#define MAXPATHLEN PATH_MAX
#else
#define MAXPATHLEN 1024
#endif
#endif
#endif
#ifndef getlogin
extern char *getlogin ();
#endif
#if GETPWNAM_DECLARATION_OK
#ifndef getpwnam
extern struct passwd *getpwnam ();
#endif
#endif
#if GETPWUID_DECLARATION_OK
#ifndef getpwuid
extern struct passwd *getpwuid ();
#endif
#endif
#if HAVE_GETCWD
#ifndef getcwd
extern char *getcwd ();
#endif
#endif
#if HAVE_GETWD
#ifndef getwd
extern char *getwd ();
#endif
#endif
#if HAVE_SYSCONF
#ifndef sysconf
extern long sysconf ();
#endif
#endif
static char *zSlogin;
const char *zSspooldir;
const char *zSlockdir;
const char *zSlocalname;
char *zScwd;
#if SPOOLDIR_V2 || SPOOLDIR_BSD42 || SPOOLDIR_BSD43 || SPOOLDIR_ULTRIX
size_t cSysdep_max_name_len = 7;
#endif
#if SPOOLDIR_HDB || SPOOLDIR_SVR4
size_t cSysdep_max_name_len = 14;
#endif
#if SPOOLDIR_TAYLOR
#if HAVE_LONG_FILE_NAMES
size_t cSysdep_max_name_len = 255;
#else
size_t cSysdep_max_name_len = 14;
#endif
#endif
void
usysdep_initialize (puuconf,iflags)
pointer puuconf;
int iflags;
{
int iuuconf;
char *z;
struct passwd *q;
ulog_id (getpid ());
if ((iflags & INIT_NOCLOSE) == 0)
{
int cdescs;
int o;
#if HAVE_GETDTABLESIZE
cdescs = getdtablesize ();
#else
#if HAVE_SYSCONF
cdescs = sysconf (_SC_OPEN_MAX);
#else
#ifdef OPEN_MAX
cdescs = OPEN_MAX;
#else
#ifdef NOFILE
cdescs = NOFILE;
#else
cdescs = 20;
#endif
#endif
#endif
#endif
for (o = 3; o < cdescs; o++)
(void) close (o);
}
if (fcntl (0, F_GETFD, 0) < 0
&& open ((char *) "/dev/null", O_RDONLY, 0) != 0)
exit (EXIT_FAILURE);
if (fcntl (1, F_GETFD, 0) < 0
&& open ((char *) "/dev/null", O_WRONLY, 0) != 1)
exit (EXIT_FAILURE);
if (fcntl (2, F_GETFD, 0) < 0
&& open ((char *) "/dev/null", O_WRONLY, 0) != 2)
exit (EXIT_FAILURE);
iuuconf = uuconf_spooldir (puuconf, &zSspooldir);
if (iuuconf != UUCONF_SUCCESS)
ulog_uuconf (LOG_FATAL, puuconf, iuuconf);
iuuconf = uuconf_lockdir (puuconf, &zSlockdir);
if (iuuconf != UUCONF_SUCCESS)
ulog_uuconf (LOG_FATAL, puuconf, iuuconf);
iuuconf = uuconf_localname (puuconf, &zSlocalname);
if (iuuconf == UUCONF_NOT_FOUND)
{
#if HAVE_GETHOSTNAME
char ab[256];
if (gethostname (ab, sizeof ab - 1) < 0)
ulog (LOG_FATAL, "gethostname: %s", strerror (errno));
ab[sizeof ab - 1] = '\0';
ab[strcspn (ab, ".")] = '\0';
zSlocalname = zbufcpy (ab);
#else
#if HAVE_UNAME
struct utsname s;
if (uname (&s) < 0)
ulog (LOG_FATAL, "uname: %s", strerror (errno));
zSlocalname = zbufcpy (s.nodename);
#else
ulog (LOG_FATAL, "Don't know how to get local node name");
#endif
#endif
}
else if (iuuconf != UUCONF_SUCCESS)
ulog_uuconf (LOG_FATAL, puuconf, iuuconf);
umask (0);
z = getenv ("LOGNAME");
if (z == NULL)
z = getenv ("USER");
if (z == NULL)
z = getlogin ();
if (z == NULL)
q = NULL;
else
{
q = getpwnam (z);
if (q != NULL)
z = q->pw_name;
}
if (q == NULL || q->pw_uid != getuid ())
{
q = getpwuid (getuid ());
if (q == NULL)
z = NULL;
else
z = q->pw_name;
}
if (z != NULL)
zSlogin = zbufcpy (z);
{
char *zlf;
if (getuid() == 4 && UUCONF_SUCCESS == uuconf_logfile (puuconf, &zlf)) {
chmod(zlf, 0777);
}
}
if ((iflags & INIT_SUID) != 0
&& geteuid () == 0)
{
q = getpwnam (OWNER);
if (q != NULL)
setuid (q->pw_uid);
}
if ((iflags & INIT_GETCWD) != 0)
{
const char *zenv;
struct stat senv, sdot;
zenv = getenv ("PWD");
if (zenv != NULL
&& stat ((char *) zenv, &senv) == 0
&& stat ((char *) ".", &sdot) == 0
&& senv.st_ino == sdot.st_ino
&& senv.st_dev == sdot.st_dev)
zScwd = zbufcpy (zenv);
else
{
#if HAVE_GETCWD
{
size_t c;
c = 128;
while (TRUE)
{
zScwd = (char *) xmalloc (c);
if (getcwd (zScwd, c) != NULL)
break;
xfree ((pointer) zScwd);
zScwd = NULL;
if (errno != ERANGE)
break;
c <<= 1;
}
}
#endif
#if HAVE_GETWD
zScwd = (char *) xmalloc (MAXPATHLEN);
if (getwd (zScwd) == NULL)
{
xfree ((pointer) zScwd);
zScwd = NULL;
}
#endif
if (zScwd != NULL)
zScwd = (char *) xrealloc ((pointer) zScwd,
strlen (zScwd) + 1);
}
}
if ((iflags & INIT_NOCHDIR) == 0)
{
if (chdir (zSspooldir) < 0)
{
if (errno == ENOENT
&& mkdir ((char *) zSspooldir, IDIRECTORY_MODE) < 0)
ulog (LOG_FATAL, "mkdir (%s): %s", zSspooldir,
strerror (errno));
if (chdir (zSspooldir) < 0)
ulog (LOG_FATAL, "chdir (%s): %s", zSspooldir,
strerror (errno));
}
}
}
void
usysdep_exit (fsuccess)
boolean fsuccess;
{
exit (fsuccess ? EXIT_SUCCESS : EXIT_FAILURE);
}
boolean fsysdep_other_config (z)
const char *z ATTRIBUTE_UNUSED;
{
(void) setuid (getuid ());
(void) setgid (getgid ());
return TRUE;
}
const char *
zsysdep_localname ()
{
return zSlocalname;
}
const char *
zsysdep_login_name ()
{
if (zSlogin == NULL)
ulog (LOG_FATAL, "Can't get login name");
return zSlogin;
}