Blame nis/nis_local_names.c

Packit 6c4009
/* Copyright (c) 1997-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <libintl.h>
Packit 6c4009
#include <rpcsvc/nis.h>
Packit 6c4009
#include <shlib-compat.h>
Packit 6c4009
Packit 6c4009
nis_name
Packit 6c4009
nis_local_group (void)
Packit 6c4009
{
Packit 6c4009
  static char __nisgroup[NIS_MAXNAMELEN + 1];
Packit 6c4009
Packit 6c4009
  char *cptr;
Packit 6c4009
  if (__nisgroup[0] == '\0'
Packit 6c4009
      && (cptr = getenv ("NIS_GROUP")) != NULL
Packit 6c4009
      && strlen (cptr) < NIS_MAXNAMELEN)
Packit 6c4009
    {
Packit 6c4009
      char *cp = stpcpy (__nisgroup, cptr);
Packit 6c4009
Packit 6c4009
      if (cp[-1] != '.')
Packit 6c4009
	{
Packit 6c4009
	  cptr = nis_local_directory ();
Packit 6c4009
	  if ((cp - __nisgroup) + strlen (cptr) + 1 < NIS_MAXNAMELEN)
Packit 6c4009
	    {
Packit 6c4009
	      *cp++ = '.';
Packit 6c4009
	      strcpy (cp, cptr);
Packit 6c4009
	    }
Packit 6c4009
	  else
Packit 6c4009
	    __nisgroup[0] = '\0';
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return __nisgroup;
Packit 6c4009
}
Packit 6c4009
libnsl_hidden_nolink_def (nis_local_group, GLIBC_2_1)
Packit 6c4009
Packit 6c4009
nis_name
Packit 6c4009
nis_local_directory (void)
Packit 6c4009
{
Packit 6c4009
  static char __nisdomainname[NIS_MAXNAMELEN + 1];
Packit 6c4009
Packit 6c4009
  if (__nisdomainname[0] == '\0')
Packit 6c4009
    {
Packit 6c4009
      if (getdomainname (__nisdomainname, NIS_MAXNAMELEN) < 0)
Packit 6c4009
	__nisdomainname[0] = '\0';
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  char *cp = rawmemchr (__nisdomainname, '\0');
Packit 6c4009
Packit 6c4009
	  /* Missing trailing dot? */
Packit 6c4009
	  if (cp[-1] != '.')
Packit 6c4009
	    {
Packit 6c4009
	      *cp++ = '.';
Packit 6c4009
	      *cp = '\0';
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return __nisdomainname;
Packit 6c4009
}
Packit 6c4009
libnsl_hidden_nolink_def (nis_local_directory, GLIBC_2_1)
Packit 6c4009
Packit 6c4009
nis_name
Packit 6c4009
nis_local_principal (void)
Packit 6c4009
{
Packit 6c4009
  static char __principal[NIS_MAXNAMELEN + 1];
Packit 6c4009
Packit 6c4009
  if (__principal[0] == '\0')
Packit 6c4009
    {
Packit 6c4009
      char buf[NIS_MAXNAMELEN + 1];
Packit 6c4009
      nis_result *res;
Packit 6c4009
      uid_t uid = geteuid ();
Packit 6c4009
Packit 6c4009
      if (uid != 0)
Packit 6c4009
	{
Packit 6c4009
	  int len = snprintf (buf, NIS_MAXNAMELEN - 1,
Packit 6c4009
			      "[auth_name=%d,auth_type=LOCAL],cred.org_dir.%s",
Packit 6c4009
			      uid, nis_local_directory ());
Packit 6c4009
Packit 6c4009
	  if (len >= NIS_MAXNAMELEN - 1)
Packit 6c4009
	    nobody:
Packit 6c4009
	    /* XXX The buffer is too small.  Can this happen???  */
Packit 6c4009
	    return strcpy (__principal, "nobody");
Packit 6c4009
Packit 6c4009
	  if (buf[len - 1] != '.')
Packit 6c4009
	    {
Packit 6c4009
	      buf[len++] = '.';
Packit 6c4009
	      buf[len] = '\0';
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  res = nis_list (buf, USE_DGRAM + NO_AUTHINFO + FOLLOW_LINKS +
Packit 6c4009
			  FOLLOW_PATH, NULL, NULL);
Packit 6c4009
Packit 6c4009
	  if (res == NULL)
Packit 6c4009
	    goto nobody;
Packit 6c4009
Packit 6c4009
	  if (NIS_RES_STATUS (res) == NIS_SUCCESS)
Packit 6c4009
	    {
Packit 6c4009
	      if (res->objects.objects_len > 1)
Packit 6c4009
		{
Packit 6c4009
		  /* More than one principal with same uid?  something
Packit 6c4009
		     wrong with cred table.  Should be unique.  Warn user
Packit 6c4009
		     and continue.  */
Packit 6c4009
		  printf (_("\
Packit 6c4009
LOCAL entry for UID %d in directory %s not unique\n"),
Packit 6c4009
			  uid, nis_local_directory ());
Packit 6c4009
		}
Packit 6c4009
	      strcpy (__principal, ENTRY_VAL (res->objects.objects_val, 0));
Packit 6c4009
	      nis_freeresult (res);
Packit 6c4009
	      return __principal;
Packit 6c4009
	    }
Packit 6c4009
	  else
Packit 6c4009
	    {
Packit 6c4009
	      nis_freeresult (res);
Packit 6c4009
	      goto nobody;
Packit 6c4009
	    }
Packit 6c4009
	}
Packit 6c4009
      else
Packit 6c4009
	return strcpy (__principal, nis_local_host ());
Packit 6c4009
Packit 6c4009
      /* Should be never reached */
Packit 6c4009
      goto nobody;
Packit 6c4009
    }
Packit 6c4009
  return __principal;
Packit 6c4009
}
Packit 6c4009
libnsl_hidden_nolink_def (nis_local_principal, GLIBC_2_1)
Packit 6c4009
Packit 6c4009
nis_name
Packit 6c4009
nis_local_host (void)
Packit 6c4009
{
Packit 6c4009
  static char __nishostname[NIS_MAXNAMELEN + 1];
Packit 6c4009
Packit 6c4009
  if (__nishostname[0] == '\0')
Packit 6c4009
    {
Packit 6c4009
      if (gethostname (__nishostname, NIS_MAXNAMELEN) < 0)
Packit 6c4009
	__nishostname[0] = '\0';
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  char *cp = rawmemchr (__nishostname, '\0');
Packit 6c4009
	  int len = cp - __nishostname;
Packit 6c4009
Packit 6c4009
	  /* Hostname already fully qualified? */
Packit 6c4009
	  if (cp[-1] == '.')
Packit 6c4009
	    return __nishostname;
Packit 6c4009
Packit 6c4009
	  if (len + strlen (nis_local_directory ()) + 1 > NIS_MAXNAMELEN)
Packit 6c4009
	    {
Packit 6c4009
	      __nishostname[0] = '\0';
Packit 6c4009
	      return __nishostname;
Packit 6c4009
	    }
Packit 6c4009
Packit 6c4009
	  *cp++ = '.';
Packit 6c4009
	  strncpy (cp, nis_local_directory (), NIS_MAXNAMELEN - len -1);
Packit 6c4009
	  __nishostname[NIS_MAXNAMELEN] = '\0';
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return __nishostname;
Packit 6c4009
}
Packit 6c4009
libnsl_hidden_nolink_def (nis_local_host, GLIBC_2_1)