Back to index

glibc  2.9
nis-alias.c
Go to the documentation of this file.
00001 /* Copyright (C) 1996-2002, 2003, 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>, 1996.
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 <nss.h>
00021 #include <ctype.h>
00022 #include <errno.h>
00023 #include <string.h>
00024 #include <aliases.h>
00025 #include <bits/libc-lock.h>
00026 #include <rpcsvc/yp.h>
00027 #include <rpcsvc/ypclnt.h>
00028 
00029 #include "nss-nis.h"
00030 
00031 __libc_lock_define_initialized (static, lock)
00032 
00033 static bool_t new_start = 1;
00034 static char *oldkey;
00035 static int oldkeylen;
00036 
00037 static int
00038 _nss_nis_parse_aliasent (const char *key, char *alias, struct aliasent *result,
00039                       char *buffer, size_t buflen, int *errnop)
00040 {
00041   char *first_unused = buffer + strlen (alias) + 1;
00042   size_t room_left =
00043     buflen - (buflen % __alignof__ (char *)) - strlen (alias) - 2;
00044   char *line;
00045   char *cp;
00046 
00047   result->alias_members_len = 0;
00048   *first_unused = '\0';
00049   first_unused++;
00050   strcpy (first_unused, key);
00051 
00052   if (first_unused[room_left - 1] != '\0')
00053     {
00054       /* The line is too long for our buffer.  */
00055     no_more_room:
00056       *errnop = ERANGE;
00057       return -1;
00058     }
00059 
00060   result->alias_name = first_unused;
00061 
00062   /* Terminate the line for any case.  */
00063   cp = strpbrk (alias, "#\n");
00064   if (cp != NULL)
00065     *cp = '\0';
00066 
00067   first_unused += strlen (result->alias_name) + 1;
00068   /* Adjust the pointer so it is aligned for
00069      storing pointers.  */
00070   first_unused += __alignof__ (char *) - 1;
00071   first_unused -= ((first_unused - (char *) 0) % __alignof__ (char *));
00072   result->alias_members = (char **) first_unused;
00073 
00074   line = alias;
00075 
00076   while (*line != '\0')
00077     {
00078       /* Skip leading blanks.  */
00079       while (isspace (*line))
00080        line++;
00081 
00082       if (*line == '\0')
00083        break;
00084 
00085       if (room_left < sizeof (char *))
00086          goto no_more_room;
00087       room_left -= sizeof (char *);
00088       result->alias_members[result->alias_members_len] = line;
00089 
00090       while (*line != '\0' && *line != ',')
00091        line++;
00092 
00093       if (line != result->alias_members[result->alias_members_len])
00094        {
00095          *line = '\0';
00096          line++;
00097          result->alias_members_len++;
00098        }
00099     }
00100   return result->alias_members_len == 0 ? 0 : 1;
00101 }
00102 
00103 enum nss_status
00104 _nss_nis_setaliasent (void)
00105 {
00106   __libc_lock_lock (lock);
00107 
00108   new_start = 1;
00109   if (oldkey != NULL)
00110     {
00111       free (oldkey);
00112       oldkey = NULL;
00113       oldkeylen = 0;
00114     }
00115 
00116   __libc_lock_unlock (lock);
00117 
00118   return NSS_STATUS_SUCCESS;
00119 }
00120 /* The 'endaliasent' function is identical.  */
00121 strong_alias (_nss_nis_setaliasent, _nss_nis_endaliasent)
00122 
00123 static enum nss_status
00124 internal_nis_getaliasent_r (struct aliasent *alias, char *buffer,
00125                          size_t buflen, int *errnop)
00126 {
00127   char *domain;
00128 
00129   if (__builtin_expect (yp_get_default_domain (&domain), 0))
00130     return NSS_STATUS_UNAVAIL;
00131 
00132   alias->alias_local = 0;
00133 
00134   /* Get the next entry until we found a correct one. */
00135   int parse_res;
00136   do
00137     {
00138       char *result;
00139       int len;
00140       char *outkey;
00141       int keylen;
00142       int yperr;
00143 
00144       if (new_start)
00145         yperr = yp_first (domain, "mail.aliases", &outkey, &keylen, &result,
00146                        &len);
00147       else
00148         yperr = yp_next (domain, "mail.aliases", oldkey, oldkeylen, &outkey,
00149                       &keylen, &result, &len);
00150 
00151       if (__builtin_expect (yperr != YPERR_SUCCESS, 0))
00152        {
00153          enum nss_status retval = yperr2nss (yperr);
00154 
00155          if (retval == NSS_STATUS_TRYAGAIN)
00156             *errnop = errno;
00157           return retval;
00158         }
00159 
00160       if (__builtin_expect ((size_t) (len + 1) > buflen, 0))
00161         {
00162          free (result);
00163           *errnop = ERANGE;
00164           return NSS_STATUS_TRYAGAIN;
00165         }
00166       char *p = strncpy (buffer, result, len);
00167       buffer[len] = '\0';
00168       while (isspace (*p))
00169         ++p;
00170       free (result);
00171 
00172       parse_res = _nss_nis_parse_aliasent (outkey, p, alias, buffer,
00173                                       buflen, errnop);
00174       if (__builtin_expect (parse_res == -1, 0))
00175        {
00176          free (outkey);
00177          *errnop = ERANGE;
00178          return NSS_STATUS_TRYAGAIN;
00179        }
00180 
00181       free (oldkey);
00182       oldkey = outkey;
00183       oldkeylen = keylen;
00184       new_start = 0;
00185     }
00186   while (!parse_res);
00187 
00188   return NSS_STATUS_SUCCESS;
00189 }
00190 
00191 enum nss_status
00192 _nss_nis_getaliasent_r (struct aliasent *alias, char *buffer, size_t buflen,
00193                      int *errnop)
00194 {
00195   enum nss_status status;
00196 
00197   __libc_lock_lock (lock);
00198 
00199   status = internal_nis_getaliasent_r (alias, buffer, buflen, errnop);
00200 
00201   __libc_lock_unlock (lock);
00202 
00203   return status;
00204 }
00205 
00206 enum nss_status
00207 _nss_nis_getaliasbyname_r (const char *name, struct aliasent *alias,
00208                         char *buffer, size_t buflen, int *errnop)
00209 {
00210   if (name == NULL)
00211     {
00212       *errnop = EINVAL;
00213       return NSS_STATUS_UNAVAIL;
00214     }
00215 
00216   size_t namlen = strlen (name);
00217   char name2[namlen + 1];
00218 
00219   char *domain;
00220   if (__builtin_expect (yp_get_default_domain (&domain), 0))
00221     return NSS_STATUS_UNAVAIL;
00222 
00223   /* Convert name to lowercase.  */
00224   size_t i;
00225   for (i = 0; i < namlen; ++i)
00226     name2[i] = _tolower (name[i]);
00227   name2[i] = '\0';
00228 
00229   char *result;
00230   int len;
00231   int yperr = yp_match (domain, "mail.aliases", name2, namlen, &result, &len);
00232 
00233   if (__builtin_expect (yperr != YPERR_SUCCESS, 0))
00234     {
00235       enum nss_status retval = yperr2nss (yperr);
00236 
00237       if (retval == NSS_STATUS_TRYAGAIN)
00238        *errnop = errno;
00239       return retval;
00240     }
00241 
00242   if (__builtin_expect ((size_t) (len + 1) > buflen, 0))
00243     {
00244       free (result);
00245       *errnop = ERANGE;
00246       return NSS_STATUS_TRYAGAIN;
00247     }
00248 
00249   char *p = strncpy (buffer, result, len);
00250   buffer[len] = '\0';
00251   while (isspace (*p))
00252     ++p;
00253   free (result);
00254 
00255   alias->alias_local = 0;
00256   int parse_res = _nss_nis_parse_aliasent (name, p, alias, buffer, buflen,
00257                                       errnop);
00258   if (__builtin_expect (parse_res < 1, 0))
00259     {
00260       if (parse_res == -1)
00261        return NSS_STATUS_TRYAGAIN;
00262       else
00263        return NSS_STATUS_NOTFOUND;
00264     }
00265 
00266   return NSS_STATUS_SUCCESS;
00267 }