(root)/
glibc-2.38/
nis/
nis_local_names.c
       1  /* Copyright (c) 1997-2023 Free Software Foundation, Inc.
       2     This file is part of the GNU C Library.
       3  
       4     The GNU C Library is free software; you can redistribute it and/or
       5     modify it under the terms of the GNU Lesser General Public
       6     License as published by the Free Software Foundation; either
       7     version 2.1 of the License, or (at your option) any later version.
       8  
       9     The GNU C Library is distributed in the hope that it will be useful,
      10     but WITHOUT ANY WARRANTY; without even the implied warranty of
      11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12     Lesser General Public License for more details.
      13  
      14     You should have received a copy of the GNU Lesser General Public
      15     License along with the GNU C Library; if not, see
      16     <https://www.gnu.org/licenses/>.  */
      17  
      18  #include <errno.h>
      19  #include <string.h>
      20  #include <unistd.h>
      21  #include <libintl.h>
      22  #include <rpcsvc/nis.h>
      23  #include <shlib-compat.h>
      24  
      25  nis_name
      26  nis_local_group (void)
      27  {
      28    static char __nisgroup[NIS_MAXNAMELEN + 1];
      29  
      30    char *cptr;
      31    if (__nisgroup[0] == '\0'
      32        && (cptr = getenv ("NIS_GROUP")) != NULL
      33        && cptr[0] != '\0'
      34        && strlen (cptr) < NIS_MAXNAMELEN)
      35      {
      36        char *cp = stpcpy (__nisgroup, cptr);
      37  
      38        if (cp[-1] != '.')
      39  	{
      40  	  cptr = nis_local_directory ();
      41  	  if ((cp - __nisgroup) + strlen (cptr) + 1 < NIS_MAXNAMELEN)
      42  	    {
      43  	      *cp++ = '.';
      44  	      strcpy (cp, cptr);
      45  	    }
      46  	  else
      47  	    __nisgroup[0] = '\0';
      48  	}
      49      }
      50  
      51    return __nisgroup;
      52  }
      53  libnsl_hidden_nolink_def (nis_local_group, GLIBC_2_1)
      54  
      55  nis_name
      56  nis_local_directory (void)
      57  {
      58    static char __nisdomainname[NIS_MAXNAMELEN + 1];
      59  
      60    if (__nisdomainname[0] == '\0')
      61      {
      62        if (getdomainname (__nisdomainname, NIS_MAXNAMELEN) < 0)
      63  	__nisdomainname[0] = '\0';
      64        else
      65  	{
      66  	  char *cp = strchr (__nisdomainname, '\0');
      67  
      68  	  /* Missing trailing dot? */
      69  	  if (cp[-1] != '.')
      70  	    {
      71  	      *cp++ = '.';
      72  	      *cp = '\0';
      73  	    }
      74  	}
      75      }
      76  
      77    return __nisdomainname;
      78  }
      79  libnsl_hidden_nolink_def (nis_local_directory, GLIBC_2_1)
      80  
      81  nis_name
      82  nis_local_principal (void)
      83  {
      84    static char __principal[NIS_MAXNAMELEN + 1];
      85  
      86    if (__principal[0] == '\0')
      87      {
      88        char buf[NIS_MAXNAMELEN + 1];
      89        nis_result *res;
      90        uid_t uid = geteuid ();
      91  
      92        if (uid != 0)
      93  	{
      94  	  int len = snprintf (buf, NIS_MAXNAMELEN - 1,
      95  			      "[auth_name=%d,auth_type=LOCAL],cred.org_dir.%s",
      96  			      uid, nis_local_directory ());
      97  
      98  	  if (len >= NIS_MAXNAMELEN - 1)
      99  	    nobody:
     100  	    /* XXX The buffer is too small.  Can this happen???  */
     101  	    return strcpy (__principal, "nobody");
     102  
     103  	  if (buf[len - 1] != '.')
     104  	    {
     105  	      buf[len++] = '.';
     106  	      buf[len] = '\0';
     107  	    }
     108  
     109  	  res = nis_list (buf, USE_DGRAM + NO_AUTHINFO + FOLLOW_LINKS
     110  			  + FOLLOW_PATH, NULL, NULL);
     111  
     112  	  if (res == NULL)
     113  	    goto nobody;
     114  
     115  	  if (NIS_RES_STATUS (res) == NIS_SUCCESS)
     116  	    {
     117  	      if (res->objects.objects_len > 1)
     118  		{
     119  		  /* More than one principal with same uid?  something
     120  		     wrong with cred table.  Should be unique.  Warn user
     121  		     and continue.  */
     122  		  printf (_("\
     123  LOCAL entry for UID %d in directory %s not unique\n"),
     124  			  uid, nis_local_directory ());
     125  		}
     126  	      strcpy (__principal, ENTRY_VAL (res->objects.objects_val, 0));
     127  	      nis_freeresult (res);
     128  	      return __principal;
     129  	    }
     130  	  else
     131  	    {
     132  	      nis_freeresult (res);
     133  	      goto nobody;
     134  	    }
     135  	}
     136        else
     137  	return strcpy (__principal, nis_local_host ());
     138  
     139        /* Should be never reached */
     140        goto nobody;
     141      }
     142    return __principal;
     143  }
     144  libnsl_hidden_nolink_def (nis_local_principal, GLIBC_2_1)
     145  
     146  nis_name
     147  nis_local_host (void)
     148  {
     149    static char __nishostname[NIS_MAXNAMELEN + 1];
     150  
     151    if (__nishostname[0] == '\0')
     152      {
     153        if (gethostname (__nishostname, NIS_MAXNAMELEN) < 0)
     154  	__nishostname[0] = '\0';
     155        else
     156  	{
     157  	  char *cp = strchr (__nishostname, '\0');
     158  	  int len = cp - __nishostname;
     159  
     160  	  /* Hostname already fully qualified? */
     161  	  if (cp[-1] == '.')
     162  	    return __nishostname;
     163  
     164  	  nis_name local_directory = nis_local_directory ();
     165  	  size_t local_directory_len = strlen (local_directory);
     166  	  if (len + 1 + local_directory_len > NIS_MAXNAMELEN)
     167  	    {
     168  	      __nishostname[0] = '\0';
     169  	      return __nishostname;
     170  	    }
     171  
     172  	  /* We have enough space in __nishostname with length of
     173  	     (NIS_MAXNAMELEN + 1) for
     174  	     hostname + '.' + directory-name + '\0'.  */
     175  	  *cp++ = '.';
     176  	  memcpy (cp, local_directory, local_directory_len + 1);
     177  	}
     178      }
     179  
     180    return __nishostname;
     181  }
     182  libnsl_hidden_nolink_def (nis_local_host, GLIBC_2_1)