Back to index

glibc  2.9
nis_local_names.c
Go to the documentation of this file.
00001 /* Copyright (c) 1997, 1998, 2004, 2006 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <errno.h>
00021 #include <string.h>
00022 #include <unistd.h>
00023 #include <libintl.h>
00024 #include <rpcsvc/nis.h>
00025 
00026 nis_name
00027 nis_local_group (void)
00028 {
00029   static char __nisgroup[NIS_MAXNAMELEN + 1];
00030 
00031   char *cptr;
00032   if (__nisgroup[0] == '\0'
00033       && (cptr = getenv ("NIS_GROUP")) != NULL
00034       && strlen (cptr) < NIS_MAXNAMELEN)
00035     {
00036       char *cp = stpcpy (__nisgroup, cptr);
00037 
00038       if (cp[-1] != '.')
00039        {
00040          cptr = nis_local_directory ();
00041          if ((cp - __nisgroup) + strlen (cptr) + 1 < NIS_MAXNAMELEN)
00042            {
00043              *cp++ = '.';
00044              strcpy (cp, cptr);
00045            }
00046          else
00047            __nisgroup[0] = '\0';
00048        }
00049     }
00050 
00051   return __nisgroup;
00052 }
00053 libnsl_hidden_def (nis_local_group)
00054 
00055 nis_name
00056 nis_local_directory (void)
00057 {
00058   static char __nisdomainname[NIS_MAXNAMELEN + 1];
00059 
00060   if (__nisdomainname[0] == '\0')
00061     {
00062       if (getdomainname (__nisdomainname, NIS_MAXNAMELEN) < 0)
00063        __nisdomainname[0] = '\0';
00064       else
00065        {
00066          char *cp = rawmemchr (__nisdomainname, '\0');
00067 
00068          /* Missing trailing dot? */
00069          if (cp[-1] != '.')
00070            {
00071              *cp++ = '.';
00072              *cp = '\0';
00073            }
00074        }
00075     }
00076 
00077   return __nisdomainname;
00078 }
00079 libnsl_hidden_def (nis_local_directory)
00080 
00081 nis_name
00082 nis_local_principal (void)
00083 {
00084   static char __principal[NIS_MAXNAMELEN + 1];
00085 
00086   if (__principal[0] == '\0')
00087     {
00088       char buf[NIS_MAXNAMELEN + 1];
00089       nis_result *res;
00090       uid_t uid = geteuid ();
00091 
00092       if (uid != 0)
00093        {
00094          int len = snprintf (buf, NIS_MAXNAMELEN - 1,
00095                            "[auth_name=%d,auth_type=LOCAL],cred.org_dir.%s",
00096                            uid, nis_local_directory ());
00097 
00098          if (len >= NIS_MAXNAMELEN - 1)
00099            nobody:
00100            /* XXX The buffer is too small.  Can this happen???  */
00101            return strcpy (__principal, "nobody");
00102 
00103          if (buf[len - 1] != '.')
00104            {
00105              buf[len++] = '.';
00106              buf[len] = '\0';
00107            }
00108 
00109          res = nis_list (buf, USE_DGRAM + NO_AUTHINFO + FOLLOW_LINKS +
00110                        FOLLOW_PATH, NULL, NULL);
00111 
00112          if (res == NULL)
00113            goto nobody;
00114 
00115          if (NIS_RES_STATUS (res) == NIS_SUCCESS)
00116            {
00117              if (res->objects.objects_len > 1)
00118               {
00119                 /* More than one principal with same uid?  something
00120                    wrong with cred table.  Should be unique.  Warn user
00121                    and continue.  */
00122                 printf (_("\
00123 LOCAL entry for UID %d in directory %s not unique\n"),
00124                        uid, nis_local_directory ());
00125               }
00126              strcpy (__principal, ENTRY_VAL (res->objects.objects_val, 0));
00127              nis_freeresult (res);
00128              return __principal;
00129            }
00130          else
00131            {
00132              nis_freeresult (res);
00133              goto nobody;
00134            }
00135        }
00136       else
00137        return strcpy (__principal, nis_local_host ());
00138 
00139       /* Should be never reached */
00140       goto nobody;
00141     }
00142   return __principal;
00143 }
00144 libnsl_hidden_def (nis_local_principal)
00145 
00146 nis_name
00147 nis_local_host (void)
00148 {
00149   static char __nishostname[NIS_MAXNAMELEN + 1];
00150 
00151   if (__nishostname[0] == '\0')
00152     {
00153       if (gethostname (__nishostname, NIS_MAXNAMELEN) < 0)
00154        __nishostname[0] = '\0';
00155       else
00156        {
00157          char *cp = rawmemchr (__nishostname, '\0');
00158          int len = cp - __nishostname;
00159 
00160          /* Hostname already fully qualified? */
00161          if (cp[-1] == '.')
00162            return __nishostname;
00163 
00164          if (len + strlen (nis_local_directory ()) + 1 > NIS_MAXNAMELEN)
00165            {
00166              __nishostname[0] = '\0';
00167              return __nishostname;
00168            }
00169 
00170          *cp++ = '.';
00171          strncpy (cp, nis_local_directory (), NIS_MAXNAMELEN - len -1);
00172          __nishostname[NIS_MAXNAMELEN] = '\0';
00173        }
00174     }
00175 
00176   return __nishostname;
00177 }
00178 libnsl_hidden_def (nis_local_host)