Back to index

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