Back to index

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