Back to index

glibc  2.9
nisplus-network.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@vt.uni-paderborn.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 <netdb.h>
00025 #include <nss.h>
00026 #include <stdint.h>
00027 #include <string.h>
00028 #include <arpa/inet.h>
00029 #include <rpcsvc/nis.h>
00030 #include <bits/libc-lock.h>
00031 
00032 #include "nss-nisplus.h"
00033 
00034 __libc_lock_define_initialized (static, lock)
00035 
00036 static nis_result *result;
00037 static nis_name tablename_val;
00038 static u_long tablename_len;
00039 
00040 #define NISENTRYVAL(idx, col, res) \
00041         (NIS_RES_OBJECT (res)[idx].EN_data.en_cols.en_cols_val[col].ec_value.ec_value_val)
00042 
00043 #define NISENTRYLEN(idx, col, res) \
00044         (NIS_RES_OBJECT (res)[idx].EN_data.en_cols.en_cols_val[col].ec_value.ec_value_len)
00045 
00046 
00047 static int
00048 _nss_nisplus_parse_netent (nis_result *result, struct netent *network,
00049                         char *buffer, size_t buflen, int *errnop)
00050 {
00051   char *first_unused = buffer;
00052   size_t room_left = buflen;
00053 
00054   if (result == NULL)
00055     return 0;
00056 
00057   if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
00058       || __type_of (NIS_RES_OBJECT (result)) != NIS_ENTRY_OBJ
00059       || strcmp (NIS_RES_OBJECT (result)[0].EN_data.en_type,
00060                "networks_tbl") != 0
00061       || NIS_RES_OBJECT (result)[0].EN_data.en_cols.en_cols_len < 3)
00062     return 0;
00063 
00064   if (NISENTRYLEN (0, 0, result) >= room_left)
00065     {
00066       /* The line is too long for our buffer.  */
00067     no_more_room:
00068       *errnop = ERANGE;
00069       return -1;
00070     }
00071 
00072   strncpy (first_unused, NISENTRYVAL (0, 0, result),
00073            NISENTRYLEN (0, 0, result));
00074   first_unused[NISENTRYLEN (0, 0, result)] = '\0';
00075   network->n_name = first_unused;
00076   size_t len = strlen (first_unused) + 1;
00077   room_left -= len;
00078   first_unused += len;
00079 
00080   network->n_addrtype = 0;
00081   network->n_net = inet_network (NISENTRYVAL (0, 2, result));
00082 
00083   /* XXX Rewrite at some point to allocate the array first and then
00084      copy the strings.  It wasteful to first concatenate the strings
00085      to just split them again later.  */
00086   char *line = first_unused;
00087   for (unsigned int i = 0; i < NIS_RES_NUMOBJ (result); ++i)
00088     {
00089       if (strcmp (NISENTRYVAL (i, 1, result), network->n_name) != 0)
00090         {
00091           if (NISENTRYLEN (i, 1, result) + 2 > room_left)
00092            goto no_more_room;
00093 
00094          *first_unused++ = ' ';
00095           first_unused = __stpncpy (first_unused, NISENTRYVAL (i, 1, result),
00096                                 NISENTRYLEN (i, 1, result));
00097           room_left -= (NISENTRYLEN (i, 1, result) + 1);
00098         }
00099     }
00100   *first_unused++ = '\0';
00101 
00102   /* Adjust the pointer so it is aligned for
00103      storing pointers.  */
00104   size_t adjust = ((__alignof__ (char *)
00105                   - (first_unused - (char *) 0) % __alignof__ (char *))
00106                  % __alignof__ (char *));
00107   if (room_left < adjust + sizeof (char *))
00108     goto no_more_room;
00109   first_unused += adjust;
00110   room_left -= adjust;
00111   network->n_aliases = (char **) first_unused;
00112 
00113   /* For the terminating NULL pointer.  */
00114   room_left -= sizeof (char *);
00115 
00116   unsigned int i = 0;
00117   while (*line != '\0')
00118     {
00119       /* Skip leading blanks.  */
00120       while (isspace (*line))
00121         ++line;
00122 
00123       if (*line == '\0')
00124         break;
00125 
00126       if (room_left < sizeof (char *))
00127        goto no_more_room;
00128 
00129       room_left -= sizeof (char *);
00130       network->n_aliases[i++] = line;
00131 
00132       while (*line != '\0' && *line != ' ')
00133         ++line;
00134 
00135       if (*line == ' ')
00136        *line++ = '\0';
00137     }
00138   network->n_aliases[i] = NULL;
00139 
00140   return 1;
00141 }
00142 
00143 
00144 static enum nss_status
00145 _nss_create_tablename (int *errnop)
00146 {
00147   if (tablename_val == NULL)
00148     {
00149       const char *local_dir = nis_local_directory ();
00150       size_t local_dir_len = strlen (local_dir);
00151       static const char prefix[] = "networks.org_dir.";
00152 
00153       char *p = malloc (sizeof (prefix) + local_dir_len);
00154       if (p == NULL)
00155        {
00156          *errnop = errno;
00157          return NSS_STATUS_TRYAGAIN;
00158        }
00159 
00160       memcpy (__stpcpy (p, prefix), local_dir, local_dir_len + 1);
00161 
00162       tablename_len = sizeof (prefix) - 1 + local_dir_len;
00163 
00164       atomic_write_barrier ();
00165 
00166       tablename_val = p;
00167     }
00168 
00169   return NSS_STATUS_SUCCESS;
00170 }
00171 
00172 enum nss_status
00173 _nss_nisplus_setnetent (int stayopen)
00174 {
00175   enum nss_status status = NSS_STATUS_SUCCESS;
00176 
00177   __libc_lock_lock (lock);
00178 
00179   if (result != NULL)
00180     {
00181       nis_freeresult (result);
00182       result = NULL;
00183     }
00184 
00185   if (tablename_val == NULL)
00186     {
00187       int err;
00188       status = _nss_create_tablename (&err);
00189     }
00190 
00191   __libc_lock_unlock (lock);
00192 
00193   return status;
00194 }
00195 
00196 enum nss_status
00197 _nss_nisplus_endnetent (void)
00198 {
00199   __libc_lock_lock (lock);
00200 
00201   if (result != NULL)
00202     {
00203       nis_freeresult (result);
00204       result = NULL;
00205     }
00206 
00207   __libc_lock_unlock (lock);
00208 
00209   return NSS_STATUS_SUCCESS;
00210 }
00211 
00212 static enum nss_status
00213 internal_nisplus_getnetent_r (struct netent *network, char *buffer,
00214                             size_t buflen, int *errnop, int *herrnop)
00215 {
00216   int parse_res;
00217 
00218   /* Get the next entry until we found a correct one. */
00219   do
00220     {
00221       nis_result *saved_res;
00222 
00223       if (result == NULL)
00224        {
00225          saved_res = NULL;
00226 
00227          if (tablename_val == NULL)
00228            {
00229              enum nss_status status = _nss_create_tablename (errnop);
00230 
00231              if (status != NSS_STATUS_SUCCESS)
00232               return status;
00233            }
00234 
00235          result = nis_first_entry (tablename_val);
00236          if (result == NULL)
00237            {
00238              *errnop = errno;
00239              return NSS_STATUS_TRYAGAIN;
00240            }
00241          if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
00242            {
00243              int retval = niserr2nss (result->status);
00244              nis_freeresult (result);
00245              result = NULL;
00246              if (retval == NSS_STATUS_TRYAGAIN)
00247               {
00248                 *herrnop = NETDB_INTERNAL;
00249                 *errnop = errno;
00250                 return retval;
00251               }
00252              else
00253               return retval;
00254            }
00255        }
00256       else
00257        {
00258          saved_res = result;
00259          result = nis_next_entry (tablename_val, &result->cookie);
00260          if (result == NULL)
00261            {
00262              *errnop = errno;
00263              return NSS_STATUS_TRYAGAIN;
00264            }
00265          if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
00266            {
00267              int retval = niserr2nss (result->status);
00268              nis_freeresult (result);
00269              result = saved_res;
00270              if (retval == NSS_STATUS_TRYAGAIN)
00271               {
00272                 *herrnop = NETDB_INTERNAL;
00273                 *errnop = errno;
00274               }
00275              return retval;
00276            }
00277        }
00278 
00279       parse_res = _nss_nisplus_parse_netent (result, network, buffer,
00280                                         buflen, errnop);
00281       if (parse_res == -1)
00282         {
00283           *herrnop = NETDB_INTERNAL;
00284           return NSS_STATUS_TRYAGAIN;
00285         }
00286 
00287     }
00288   while (!parse_res);
00289 
00290   return NSS_STATUS_SUCCESS;
00291 }
00292 
00293 enum nss_status
00294 _nss_nisplus_getnetent_r (struct netent *result, char *buffer,
00295                         size_t buflen, int *errnop, int *herrnop)
00296 {
00297   int status;
00298 
00299   __libc_lock_lock (lock);
00300 
00301   status = internal_nisplus_getnetent_r (result, buffer, buflen, errnop,
00302                                     herrnop);
00303 
00304   __libc_lock_unlock (lock);
00305 
00306   return status;
00307 }
00308 
00309 enum nss_status
00310 _nss_nisplus_getnetbyname_r (const char *name, struct netent *network,
00311                            char *buffer, size_t buflen, int *errnop,
00312                           int *herrnop)
00313 {
00314   int parse_res, retval;
00315 
00316   if (tablename_val == NULL)
00317     {
00318       __libc_lock_lock (lock);
00319 
00320       enum nss_status status = _nss_create_tablename (errnop);
00321 
00322       __libc_lock_unlock (lock);
00323 
00324       if (status != NSS_STATUS_SUCCESS)
00325        return status;
00326     }
00327 
00328   if (name == NULL)
00329     {
00330       *errnop = EINVAL;
00331       *herrnop = NETDB_INTERNAL;
00332       return NSS_STATUS_UNAVAIL;
00333     }
00334 
00335   nis_result *result;
00336   char buf[strlen (name) + 10 + tablename_len];
00337   int olderr = errno;
00338 
00339   /* Search at first in the alias list, and use the correct name
00340      for the next search */
00341   snprintf (buf, sizeof (buf), "[name=%s],%s", name, tablename_val);
00342   result = nis_list (buf, FOLLOW_LINKS | FOLLOW_PATH | USE_DGRAM, NULL, NULL);
00343 
00344   if (result != NULL)
00345     {
00346       char *bufptr = buf;
00347 
00348       /* If we do not find it, try it as original name. But if the
00349         database is correct, we should find it in the first case, too */
00350       if ((result->status != NIS_SUCCESS
00351           && result->status != NIS_S_SUCCESS)
00352          || __type_of (result->objects.objects_val) != NIS_ENTRY_OBJ
00353          || strcmp (result->objects.objects_val[0].EN_data.en_type,
00354                    "networks_tbl") != 0
00355          || (result->objects.objects_val[0].EN_data.en_cols.en_cols_len
00356              < 3))
00357        snprintf (buf, sizeof (buf), "[cname=%s],%s", name, tablename_val);
00358       else
00359        {
00360          /* We need to allocate a new buffer since there is no
00361             guarantee the returned name has a length limit.  */
00362          const char *entryval = NISENTRYVAL (0, 0, result);
00363          size_t buflen = strlen (entryval) + 10 + tablename_len;
00364          bufptr = alloca (buflen);
00365          snprintf (bufptr, buflen, "[cname=%s],%s",
00366                   entryval, tablename_val);
00367        }
00368 
00369       nis_freeresult (result);
00370       result = nis_list (bufptr, FOLLOW_LINKS | FOLLOW_PATH | USE_DGRAM,
00371                       NULL, NULL);
00372     }
00373 
00374   if (result == NULL)
00375     {
00376       __set_errno (ENOMEM);
00377       return NSS_STATUS_TRYAGAIN;
00378     }
00379 
00380   retval = niserr2nss (result->status);
00381   if (__builtin_expect (retval != NSS_STATUS_SUCCESS, 0))
00382     {
00383       if (retval == NSS_STATUS_TRYAGAIN)
00384        {
00385          *errnop = errno;
00386          *herrnop = NETDB_INTERNAL;
00387        }
00388       else
00389        __set_errno (olderr);
00390       nis_freeresult (result);
00391       return retval;
00392     }
00393 
00394   parse_res = _nss_nisplus_parse_netent (result, network, buffer, buflen,
00395                                     errnop);
00396 
00397   nis_freeresult (result);
00398 
00399   if (parse_res > 0)
00400     return NSS_STATUS_SUCCESS;
00401 
00402   *herrnop = NETDB_INTERNAL;
00403   if (parse_res == -1)
00404     {
00405       *errnop = ERANGE;
00406       return NSS_STATUS_TRYAGAIN;
00407     }
00408 
00409   __set_errno (olderr);
00410   return NSS_STATUS_NOTFOUND;
00411 }
00412 
00413 /* XXX type is ignored, SUN's NIS+ table doesn't support it */
00414 enum nss_status
00415 _nss_nisplus_getnetbyaddr_r (uint32_t addr, const int type,
00416                           struct netent *network, char *buffer,
00417                           size_t buflen, int *errnop, int *herrnop)
00418 {
00419   if (tablename_val == NULL)
00420     {
00421       __libc_lock_lock (lock);
00422 
00423       enum nss_status status = _nss_create_tablename (errnop);
00424 
00425       __libc_lock_unlock (lock);
00426 
00427       if (status != NSS_STATUS_SUCCESS)
00428        return status;
00429     }
00430 
00431   {
00432     char buf[27 + tablename_len];
00433     char buf2[18];
00434     int olderr = errno;
00435 
00436     struct in_addr in = inet_makeaddr (addr, 0);
00437     strcpy (buf2, inet_ntoa (in));
00438     size_t b2len = strlen (buf2);
00439 
00440     while (1)
00441       {
00442        snprintf (buf, sizeof (buf), "[addr=%s],%s", buf2, tablename_val);
00443        nis_result *result = nis_list (buf, EXPAND_NAME | USE_DGRAM,
00444                                    NULL, NULL);
00445 
00446        if (result == NULL)
00447          {
00448            __set_errno (ENOMEM);
00449            return NSS_STATUS_TRYAGAIN;
00450          }
00451        enum nss_status retval = niserr2nss (result->status);
00452        if (__builtin_expect (retval != NSS_STATUS_SUCCESS, 0))
00453          {
00454            if (b2len > 2 && buf2[b2len - 2] == '.' && buf2[b2len - 1] == '0')
00455              {
00456               /* Try again, but with trailing dot(s)
00457                  removed (one by one) */
00458               buf2[b2len - 2] = '\0';
00459               b2len -= 2;
00460               nis_freeresult (result);
00461               continue;
00462              }
00463 
00464            if (retval == NSS_STATUS_TRYAGAIN)
00465              {
00466               *errnop = errno;
00467               *herrnop = NETDB_INTERNAL;
00468              }
00469            else
00470              __set_errno (olderr);
00471            nis_freeresult (result);
00472            return retval;
00473          }
00474 
00475        int parse_res = _nss_nisplus_parse_netent (result, network, buffer,
00476                                              buflen, errnop);
00477 
00478        nis_freeresult (result);
00479 
00480        if (parse_res > 0)
00481          return NSS_STATUS_SUCCESS;
00482 
00483        *herrnop = NETDB_INTERNAL;
00484        if (parse_res == -1)
00485          {
00486            *errnop = ERANGE;
00487            return NSS_STATUS_TRYAGAIN;
00488          }
00489        else
00490          {
00491            __set_errno (olderr);
00492            return NSS_STATUS_NOTFOUND;
00493          }
00494       }
00495   }
00496 }