Back to index

glibc  2.9
nisplus-ethers.c
Go to the documentation of this file.
00001 /* Copyright (C) 1997,1998,2000-2003,2005,2006,2007
00002    Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Thorsten Kukuk <kukuk@suse.de>, 1997.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <atomic.h>
00022 #include <ctype.h>
00023 #include <errno.h>
00024 #include <inttypes.h>
00025 #include <netdb.h>
00026 #include <nss.h>
00027 #include <string.h>
00028 #include <netinet/ether.h>
00029 #include <netinet/if_ether.h>
00030 #include <rpcsvc/nis.h>
00031 #include <bits/libc-lock.h>
00032 
00033 #include "nss-nisplus.h"
00034 
00035 __libc_lock_define_initialized (static, lock)
00036 
00037 static nis_result *result;
00038 static nis_name tablename_val;
00039 static u_long tablename_len;
00040 
00041 
00042 #define NISENTRYVAL(idx, col, res) \
00043         (NIS_RES_OBJECT (res)[idx].zo_data.objdata_u.en_data.en_cols.en_cols_val[col].ec_value.ec_value_val)
00044 
00045 #define NISENTRYLEN(idx, col, res) \
00046         (NIS_RES_OBJECT (res)[idx].zo_data.objdata_u.en_data.en_cols.en_cols_val[col].ec_value.ec_value_len)
00047 
00048 static int
00049 _nss_nisplus_parse_etherent (nis_result *result, struct etherent *ether,
00050                           char *buffer, size_t buflen, int *errnop)
00051 {
00052   char *p = buffer;
00053   size_t room_left = buflen;
00054 
00055   if (result == NULL)
00056     return 0;
00057 
00058   if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
00059       || NIS_RES_NUMOBJ (result) != 1
00060       || __type_of (NIS_RES_OBJECT (result)) != NIS_ENTRY_OBJ
00061       || strcmp (NIS_RES_OBJECT (result)->EN_data.en_type,
00062                "ethers_tbl") != 0
00063       || NIS_RES_OBJECT (result)->EN_data.en_cols.en_cols_len < 2)
00064     return 0;
00065 
00066   /* Generate the ether entry format and use the normal parser */
00067   if (NISENTRYLEN (0, 0, result) + 1 > room_left)
00068     {
00069       *errnop = ERANGE;
00070       return -1;
00071     }
00072   char *cp = __stpncpy (p, NISENTRYVAL (0, 0, result),
00073                      NISENTRYLEN (0, 0, result));
00074   *cp = '\0';
00075   room_left -= NISENTRYLEN (0, 0, result) + 1;
00076   ether->e_name = p;
00077 
00078   struct ether_addr *ea = ether_aton (NISENTRYVAL (0, 1, result));
00079   if (ea == NULL)
00080     {
00081       *errnop = EINVAL;
00082       return -2;
00083     }
00084 
00085   ether->e_addr = *ea;
00086 
00087   return 1;
00088 }
00089 
00090 static enum nss_status
00091 _nss_create_tablename (int *errnop)
00092 {
00093   if (tablename_val == NULL)
00094     {
00095       const char *local_dir = nis_local_directory ();
00096       size_t local_dir_len = strlen (local_dir);
00097       static const char prefix[] = "ethers.org_dir.";
00098 
00099       char *p = malloc (sizeof (prefix) + local_dir_len);
00100       if (p == NULL)
00101        {
00102          *errnop = errno;
00103          return NSS_STATUS_TRYAGAIN;
00104        }
00105 
00106       memcpy (__stpcpy (p, prefix), local_dir, local_dir_len + 1);
00107 
00108       tablename_len = sizeof (prefix) - 1 + local_dir_len;
00109 
00110       atomic_write_barrier ();
00111 
00112       tablename_val = p;
00113     }
00114   return NSS_STATUS_SUCCESS;
00115 }
00116 
00117 
00118 enum nss_status
00119 _nss_nisplus_setetherent (int stayopen)
00120 {
00121   enum nss_status status;
00122   int err;
00123 
00124   status = NSS_STATUS_SUCCESS;
00125 
00126   __libc_lock_lock (lock);
00127 
00128   if (result != NULL)
00129     {
00130       nis_freeresult (result);
00131       result = NULL;
00132     }
00133 
00134   if (_nss_create_tablename (&err) != NSS_STATUS_SUCCESS)
00135     status = NSS_STATUS_UNAVAIL;
00136 
00137   __libc_lock_unlock (lock);
00138 
00139   return NSS_STATUS_SUCCESS;
00140 }
00141 
00142 enum nss_status
00143 _nss_nisplus_endetherent (void)
00144 {
00145   __libc_lock_lock (lock);
00146 
00147   if (result != NULL)
00148     {
00149       nis_freeresult (result);
00150       result = NULL;
00151     }
00152 
00153   __libc_lock_unlock (lock);
00154 
00155   return NSS_STATUS_SUCCESS;
00156 }
00157 
00158 static enum nss_status
00159 internal_nisplus_getetherent_r (struct etherent *ether, char *buffer,
00160                             size_t buflen, int *errnop)
00161 {
00162   if (tablename_val == NULL)
00163     {
00164       enum nss_status status = _nss_create_tablename (errnop);
00165 
00166       if (status != NSS_STATUS_SUCCESS)
00167        return status;
00168     }
00169 
00170   /* Get the next entry until we found a correct one. */
00171   int parse_res;
00172   do
00173     {
00174       nis_result *saved_result;
00175 
00176       if (result == NULL)
00177        {
00178          saved_result = NULL;
00179          result = nis_first_entry (tablename_val);
00180          if (result == NULL)
00181            {
00182              *errnop = errno;
00183              return NSS_STATUS_TRYAGAIN;
00184            }
00185          if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
00186            return niserr2nss (result->status);
00187        }
00188       else
00189        {
00190          saved_result = result;
00191          result = nis_next_entry (tablename_val, &result->cookie);
00192          if (result == NULL)
00193            {
00194              *errnop = errno;
00195              return NSS_STATUS_TRYAGAIN;
00196            }
00197          if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
00198            {
00199              nis_freeresult (saved_result);
00200              return niserr2nss (result->status);
00201            }
00202        }
00203 
00204       parse_res = _nss_nisplus_parse_etherent (result, ether, buffer,
00205                                           buflen, errnop);
00206       if (parse_res == -1)
00207        {
00208          nis_freeresult (result);
00209          result = saved_result;
00210          return NSS_STATUS_TRYAGAIN;
00211        }
00212 
00213       if (saved_result != NULL)
00214        nis_freeresult (saved_result);
00215 
00216     }
00217   while (!parse_res);
00218 
00219   return NSS_STATUS_SUCCESS;
00220 }
00221 
00222 enum nss_status
00223 _nss_nisplus_getetherent_r (struct etherent *result, char *buffer,
00224                          size_t buflen, int *errnop)
00225 {
00226   int status;
00227 
00228   __libc_lock_lock (lock);
00229 
00230   status = internal_nisplus_getetherent_r (result, buffer, buflen, errnop);
00231 
00232   __libc_lock_unlock (lock);
00233 
00234   return status;
00235 }
00236 
00237 enum nss_status
00238 _nss_nisplus_gethostton_r (const char *name, struct etherent *eth,
00239                         char *buffer, size_t buflen, int *errnop)
00240 {
00241   if (tablename_val == NULL)
00242     {
00243       enum nss_status status = _nss_create_tablename (errnop);
00244 
00245       if (status != NSS_STATUS_SUCCESS)
00246        return status;
00247     }
00248 
00249   if (name == NULL)
00250     {
00251       *errnop = EINVAL;
00252       return NSS_STATUS_UNAVAIL;
00253     }
00254 
00255   char buf[strlen (name) + 9 + tablename_len];
00256   int olderr = errno;
00257 
00258   snprintf (buf, sizeof (buf), "[name=%s],%s", name, tablename_val);
00259 
00260   nis_result *result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS | USE_DGRAM,
00261                              NULL, NULL);
00262 
00263   if (result == NULL)
00264     {
00265       *errnop = ENOMEM;
00266       return NSS_STATUS_TRYAGAIN;
00267     }
00268 
00269   if (__builtin_expect (niserr2nss (result->status) != NSS_STATUS_SUCCESS, 0))
00270     {
00271       enum nss_status status = niserr2nss (result->status);
00272       nis_freeresult (result);
00273       return status;
00274     }
00275 
00276   int parse_res = _nss_nisplus_parse_etherent (result, eth, buffer,
00277                                           buflen, errnop);
00278 
00279   /* We do not need the lookup result anymore.  */
00280   nis_freeresult (result);
00281 
00282   if (__builtin_expect (parse_res < 1, 0))
00283     {
00284       __set_errno (olderr);
00285 
00286       if (parse_res == -1)
00287        return NSS_STATUS_TRYAGAIN;
00288 
00289       return NSS_STATUS_NOTFOUND;
00290     }
00291 
00292   return NSS_STATUS_SUCCESS;
00293 }
00294 
00295 enum nss_status
00296 _nss_nisplus_getntohost_r (const struct ether_addr *addr, struct etherent *eth,
00297                         char *buffer, size_t buflen, int *errnop)
00298 {
00299   if (tablename_val == NULL)
00300     {
00301       __libc_lock_lock (lock);
00302 
00303       enum nss_status status = _nss_create_tablename (errnop);
00304 
00305       __libc_lock_unlock (lock);
00306 
00307       if (status != NSS_STATUS_SUCCESS)
00308        return status;
00309     }
00310 
00311   if (addr == NULL)
00312     {
00313       *errnop = EINVAL;
00314       return NSS_STATUS_UNAVAIL;
00315     }
00316 
00317   char buf[26 + tablename_len];
00318 
00319   snprintf (buf, sizeof (buf),
00320            "[addr=%" PRIx8 ":%" PRIx8 ":%" PRIx8 ":%" PRIx8 ":%" PRIx8
00321            ":%" PRIx8 "],%s",
00322            addr->ether_addr_octet[0], addr->ether_addr_octet[1],
00323            addr->ether_addr_octet[2], addr->ether_addr_octet[3],
00324            addr->ether_addr_octet[4], addr->ether_addr_octet[5],
00325            tablename_val);
00326 
00327   nis_result *result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS | USE_DGRAM,
00328                              NULL, NULL);
00329 
00330   if (result == NULL)
00331     {
00332       *errnop = ENOMEM;
00333       return NSS_STATUS_TRYAGAIN;
00334     }
00335 
00336   if (__builtin_expect (niserr2nss (result->status) != NSS_STATUS_SUCCESS, 0))
00337     {
00338       enum nss_status status = niserr2nss (result->status);
00339       nis_freeresult (result);
00340       return status;
00341     }
00342 
00343   int parse_res = _nss_nisplus_parse_etherent (result, eth, buffer,
00344                                           buflen, errnop);
00345 
00346   /* We do not need the lookup result anymore.  */
00347   nis_freeresult (result);
00348 
00349   if (__builtin_expect (parse_res < 1, 0))
00350     {
00351       if (parse_res == -1)
00352        return NSS_STATUS_TRYAGAIN;
00353 
00354       return NSS_STATUS_NOTFOUND;
00355     }
00356 
00357   return NSS_STATUS_SUCCESS;
00358 }