home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2J (Developer) / os42jdev.iso / NextDeveloper / Source / GNU / uucp / Uucp.framework / unix.subproj / init.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-09  |  9.6 KB  |  399 lines

  1. /* init.c
  2.    Initialize the system dependent routines.
  3.  
  4.    Copyright (C) 1991, 1992, 1993, 1994 Ian Lance Taylor
  5.  
  6.    This file is part of the Taylor UUCP package.
  7.  
  8.    This program is free software; you can redistribute it and/or
  9.    modify it under the terms of the GNU General Public License as
  10.    published by the Free Software Foundation; either version 2 of the
  11.    License, or (at your option) any later version.
  12.  
  13.    This program is distributed in the hope that it will be useful, but
  14.    WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.    General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with this program; if not, write to the Free Software
  20.    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  21.  
  22.    The author of the program may be contacted at ian@airs.com or
  23.    c/o Cygnus Support, 48 Grove Street, Somerville, MA 02144.
  24.    */
  25.  
  26. #include "uucp.h"
  27.  
  28. #include "uudefs.h"
  29. #include "uuconf.h"
  30. #include "system.h"
  31. #include "sysdep.h"
  32.  
  33. #include <errno.h>
  34. #include <pwd.h>
  35.  
  36. #if HAVE_FCNTL_H
  37. #include <fcntl.h>
  38. #else
  39. #if HAVE_SYS_FILE_H
  40. #include <sys/file.h>
  41. #endif
  42. #endif
  43.  
  44. #ifndef O_RDONLY
  45. #define O_RDONLY 0
  46. #define O_WRONLY 1
  47. #define O_RDWR 2
  48. #endif
  49.  
  50. #if ! HAVE_GETHOSTNAME && HAVE_UNAME
  51. #include <sys/utsname.h>
  52. #endif
  53.  
  54. /* Use getcwd in preference to getwd; if we have neither, we will be
  55.    using a getcwd replacement.  */
  56. #if HAVE_GETCWD
  57. #undef HAVE_GETWD
  58. #define HAVE_GETWD 0
  59. #else /* ! HAVE_GETCWD */
  60. #if ! HAVE_GETWD
  61. #undef HAVE_GETCWD
  62. #define HAVE_GETCWD 1
  63. #endif /* ! HAVE_GETWD */
  64. #endif /* ! HAVE_GETCWD */
  65.  
  66. #if HAVE_GETWD
  67. /* Get a value for MAXPATHLEN.  */
  68. #if HAVE_SYS_PARAMS_H
  69. #include <sys/params.h>
  70. #endif
  71.  
  72. #if HAVE_LIMITS_H
  73. #include <limits.h>
  74. #endif
  75.  
  76. #ifndef MAXPATHLEN
  77. #ifdef PATH_MAX
  78. #define MAXPATHLEN PATH_MAX
  79. #else /* ! defined (PATH_MAX) */
  80. #define MAXPATHLEN 1024
  81. #endif /* ! defined (PATH_MAX) */
  82. #endif /* ! defined (MAXPATHLEN) */
  83. #endif /* HAVE_GETWD */
  84.  
  85. /* External functions.  */
  86. #ifndef getlogin
  87. extern char *getlogin ();
  88. #endif
  89. #if GETPWNAM_DECLARATION_OK
  90. #ifndef getpwnam
  91. extern struct passwd *getpwnam ();
  92. #endif
  93. #endif
  94. #if GETPWUID_DECLARATION_OK
  95. #ifndef getpwuid
  96. extern struct passwd *getpwuid ();
  97. #endif
  98. #endif
  99. #if HAVE_GETCWD
  100. #ifndef getcwd
  101. extern char *getcwd ();
  102. #endif
  103. #endif
  104. #if HAVE_GETWD
  105. #ifndef getwd
  106. extern char *getwd ();
  107. #endif
  108. #endif
  109. #if HAVE_SYSCONF
  110. #ifndef sysconf
  111. extern long sysconf ();
  112. #endif
  113. #endif
  114.  
  115. /* Initialize the system dependent routines.  We will probably be running
  116.    suid to uucp, so we make sure that nothing is obviously wrong.  We
  117.    save the login name since we will be losing the real uid.  */
  118. static char *zSlogin;
  119.  
  120. /* The UUCP spool directory.  */
  121. const char *zSspooldir;
  122.  
  123. /* The UUCP lock directory.  */
  124. const char *zSlockdir;
  125.  
  126. /* The local UUCP name.  */
  127. const char *zSlocalname;
  128.  
  129. /* We save the current directory since we will do a chdir to the
  130.    spool directory.  */
  131. char *zScwd;
  132.  
  133. /* The maximum length of a system name is controlled by the type of spool
  134.    directory we use.  */
  135. #if SPOOLDIR_V2 || SPOOLDIR_BSD42 || SPOOLDIR_BSD43 || SPOOLDIR_ULTRIX
  136. size_t cSysdep_max_name_len = 7;
  137. #endif
  138. #if SPOOLDIR_HDB || SPOOLDIR_SVR4
  139. size_t cSysdep_max_name_len = 14;
  140. #endif
  141. #if SPOOLDIR_TAYLOR
  142. #if HAVE_LONG_FILE_NAMES
  143. size_t cSysdep_max_name_len = 255;
  144. #else /* ! HAVE_LONG_FILE_NAMES */
  145. size_t cSysdep_max_name_len = 14;
  146. #endif /* ! HAVE_LONG_FILE_NAMES */
  147. #endif /* SPOOLDIR_TAYLOR */
  148.  
  149. /* Initialize the system dependent routines.  */
  150.  
  151. void
  152. usysdep_initialize (puuconf,iflags)
  153.      pointer puuconf;
  154.      int iflags;
  155. {
  156.   int iuuconf;
  157.   char *z;
  158.   struct passwd *q;
  159.  
  160.   ulog_id (getpid ());
  161.  
  162.   if ((iflags & INIT_NOCLOSE) == 0)
  163.     {
  164.       int cdescs;
  165.       int o;
  166.  
  167.       /* Close everything but stdin, stdout and stderr.  */
  168. #if HAVE_GETDTABLESIZE
  169.       cdescs = getdtablesize ();
  170. #else
  171. #if HAVE_SYSCONF
  172.       cdescs = sysconf (_SC_OPEN_MAX);
  173. #else
  174. #ifdef OPEN_MAX
  175.       cdescs = OPEN_MAX;
  176. #else
  177. #ifdef NOFILE
  178.       cdescs = NOFILE;
  179. #else
  180.       cdescs = 20;
  181. #endif /* ! defined (NOFILE) */
  182. #endif /* ! defined (OPEN_MAX) */
  183. #endif /* ! HAVE_SYSCONF */
  184. #endif /* ! HAVE_GETDTABLESIZE */
  185.  
  186.       for (o = 3; o < cdescs; o++)
  187.     (void) close (o);
  188.     }
  189.  
  190.   /* Make sure stdin, stdout and stderr are open.  */
  191.   if (fcntl (0, F_GETFD, 0) < 0
  192.       && open ((char *) "/dev/null", O_RDONLY, 0) != 0)
  193.     exit (EXIT_FAILURE);
  194.   if (fcntl (1, F_GETFD, 0) < 0
  195.       && open ((char *) "/dev/null", O_WRONLY, 0) != 1)
  196.     exit (EXIT_FAILURE);
  197.   if (fcntl (2, F_GETFD, 0) < 0
  198.       && open ((char *) "/dev/null", O_WRONLY, 0) != 2)
  199.     exit (EXIT_FAILURE);
  200.  
  201.   iuuconf = uuconf_spooldir (puuconf, &zSspooldir);
  202.   if (iuuconf != UUCONF_SUCCESS)
  203.     ulog_uuconf (LOG_FATAL, puuconf, iuuconf);
  204.  
  205.   iuuconf = uuconf_lockdir (puuconf, &zSlockdir);
  206.   if (iuuconf != UUCONF_SUCCESS)
  207.     ulog_uuconf (LOG_FATAL, puuconf, iuuconf);
  208.  
  209.   iuuconf = uuconf_localname (puuconf, &zSlocalname);
  210.   if (iuuconf == UUCONF_NOT_FOUND)
  211.     {
  212. #if HAVE_GETHOSTNAME
  213.       char ab[256];
  214.  
  215.       if (gethostname (ab, sizeof ab - 1) < 0)
  216.     ulog (LOG_FATAL, "gethostname: %s", strerror (errno));
  217.       ab[sizeof ab - 1] = '\0';
  218.       ab[strcspn (ab, ".")] = '\0';
  219.       zSlocalname = zbufcpy (ab);
  220. #else /* ! HAVE_GETHOSTNAME */
  221. #if HAVE_UNAME
  222.       struct utsname s;
  223.  
  224.       if (uname (&s) < 0)
  225.     ulog (LOG_FATAL, "uname: %s", strerror (errno));
  226.       zSlocalname = zbufcpy (s.nodename);
  227. #else /* ! HAVE_UNAME */
  228.       ulog (LOG_FATAL, "Don't know how to get local node name");
  229. #endif /* ! HAVE_UNAME */
  230. #endif /* ! HAVE_GETHOSTNAME */
  231.     }
  232.   else if (iuuconf != UUCONF_SUCCESS)
  233.     ulog_uuconf (LOG_FATAL, puuconf, iuuconf);
  234.  
  235.   /* We always set our file modes to exactly what we want.  */
  236.   umask (0);
  237.  
  238.   /* Get the login name, making sure that it matches the uid.  Many
  239.      systems truncate the getlogin return value to 8 characters, but
  240.      keep the full name in the password file, so we prefer the name in
  241.      the password file.  */
  242.   z = getenv ("LOGNAME");
  243.   if (z == NULL)
  244.     z = getenv ("USER");
  245.   if (z == NULL)
  246.     z = getlogin ();
  247.   if (z == NULL)
  248.     q = NULL;
  249.   else
  250.     {
  251.       q = getpwnam (z);
  252.       if (q != NULL)
  253.     z = q->pw_name;
  254.     }
  255.   if (q == NULL || q->pw_uid != getuid ())
  256.     {
  257.       q = getpwuid (getuid ());
  258.       if (q == NULL)
  259.     z = NULL;
  260.       else
  261.     z = q->pw_name;
  262.     }
  263.   if (z != NULL)
  264.     zSlogin = zbufcpy (z);
  265.  
  266.   /* On some old systems, an suid program run by root is started with
  267.      an euid of 0.  If this happens, we look up the uid we should have
  268.      and set ourselves to it manually.  This means that on such a
  269.      system root will not be able to uucp or uux files that are not
  270.      readable by uucp.  */
  271.   if ((iflags & INIT_SUID) != 0
  272.       && geteuid () == 0)
  273.     {
  274.       q = getpwnam (OWNER);
  275.       if (q != NULL)
  276.     setuid (q->pw_uid);
  277.     }
  278.  
  279.   if ((iflags & INIT_GETCWD) != 0)
  280.     {
  281.       const char *zenv;
  282.       struct stat senv, sdot;
  283.  
  284.       /* Get the current working directory.  We have to get it now,
  285.      since we're about to do a chdir.  We use PWD if it's defined
  286.      and if it really names the working directory, since if it's
  287.      not the same as whatever getcwd returns it's probably more
  288.      appropriate.  */
  289.       zenv = getenv ("PWD");
  290.       if (zenv != NULL
  291.       && stat ((char *) zenv, &senv) == 0
  292.       && stat ((char *) ".", &sdot) == 0
  293.       && senv.st_ino == sdot.st_ino
  294.       && senv.st_dev == sdot.st_dev)
  295.     zScwd = zbufcpy (zenv);
  296.       else
  297.     {
  298.  
  299. #if HAVE_GETCWD
  300.       {
  301.         size_t c;
  302.  
  303.         c = 128;
  304.         while (TRUE)
  305.           {
  306.         zScwd = (char *) xmalloc (c);
  307.         if (getcwd (zScwd, c) != NULL)
  308.           break;
  309.         xfree ((pointer) zScwd);
  310.         zScwd = NULL;
  311.         if (errno != ERANGE)
  312.           break;
  313.         c <<= 1;
  314.           }
  315.       }
  316. #endif /* HAVE_GETCWD */
  317.  
  318. #if HAVE_GETWD
  319.       zScwd = (char *) xmalloc (MAXPATHLEN);
  320.       if (getwd (zScwd) == NULL)
  321.         {
  322.           xfree ((pointer) zScwd);
  323.           zScwd = NULL;
  324.         }
  325. #endif /* HAVE_GETWD */
  326.  
  327.       if (zScwd != NULL)
  328.         zScwd = (char *) xrealloc ((pointer) zScwd,
  329.                        strlen (zScwd) + 1);
  330.     }
  331.     }
  332.  
  333.   if ((iflags & INIT_NOCHDIR) == 0)
  334.     {
  335.       /* Connect to the spool directory, and create it if it doesn't
  336.      exist.  */
  337.       if (chdir (zSspooldir) < 0)
  338.     {
  339.       if (errno == ENOENT
  340.           && mkdir ((char *) zSspooldir, IDIRECTORY_MODE) < 0)
  341.         ulog (LOG_FATAL, "mkdir (%s): %s", zSspooldir,
  342.           strerror (errno));
  343.       if (chdir (zSspooldir) < 0)
  344.         ulog (LOG_FATAL, "chdir (%s): %s", zSspooldir,
  345.           strerror (errno));
  346.     }
  347.     }
  348. }
  349.  
  350. /* Exit the program.  */
  351.  
  352. void
  353. usysdep_exit (fsuccess)
  354.      boolean fsuccess;
  355. {
  356.   exit (fsuccess ? EXIT_SUCCESS : EXIT_FAILURE);
  357. }
  358.  
  359. /* This is called when a non-standard configuration file is used, to
  360.    make sure the program doesn't hand out privileged file access.
  361.    This means that to test non-standard configuration files, you
  362.    should be logged in as uucp.  This is called before
  363.    usysdep_initialize.  It ensures that someone can't simply use an
  364.    alternate configuration file to steal UUCP transfers from other
  365.    systems.  This will still permit people to set up their own
  366.    configuration file and pretend to be whatever system they choose.
  367.    The only real security is to use a high level of protection on the
  368.    modem ports.  */
  369.  
  370. /*ARGSUSED*/
  371. boolean fsysdep_other_config (z)
  372.      const char *z;
  373. {
  374.   (void) setuid (getuid ());
  375.   (void) setgid (getgid ());
  376.   return TRUE;
  377. }
  378.  
  379. /* Get the node name to use if it was not specified in the configuration
  380.    file.  */
  381.  
  382. const char *
  383. zsysdep_localname ()
  384. {
  385.   return zSlocalname;
  386. }
  387.  
  388. /* Get the login name.  We actually get the login name in
  389.    usysdep_initialize, because after that we may switch away from the
  390.    real uid.  */
  391.  
  392. const char *
  393. zsysdep_login_name ()
  394. {
  395.   if (zSlogin == NULL)
  396.     ulog (LOG_FATAL, "Can't get login name");
  397.   return zSlogin;
  398. }
  399.