Back to index

glibc  2.9
nis-netgrp.c
Go to the documentation of this file.
00001 /* Copyright (C) 1996,1997,1999,2000,2002,2003,2004,2005,2006
00002    Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Thorsten Kukuk <kukuk@suse.de>, 1996.
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 <assert.h>
00022 #include <ctype.h>
00023 #include <errno.h>
00024 #include <malloc.h>
00025 #include <netdb.h>
00026 #include <nss.h>
00027 #include <stdio.h>
00028 #include <stdlib.h>
00029 #include <string.h>
00030 #include <netgroup.h>
00031 #include <rpcsvc/yp.h>
00032 #include <rpcsvc/ypclnt.h>
00033 
00034 #include "nss-nis.h"
00035 
00036 extern enum nss_status
00037 _nss_netgroup_parseline (char **cursor, struct __netgrent *netgrp,
00038                       char *buffer, size_t buflen, int *errnop);
00039 
00040 
00041 static void
00042 internal_nis_endnetgrent (struct __netgrent *netgrp)
00043 {
00044   free (netgrp->data);
00045   netgrp->data = NULL;
00046   netgrp->data_size = 0;
00047   netgrp->cursor = NULL;
00048 }
00049 
00050 
00051 enum nss_status
00052 _nss_nis_setnetgrent (const char *group, struct __netgrent *netgrp)
00053 {
00054   int len;
00055   enum nss_status status;
00056 
00057   status = NSS_STATUS_SUCCESS;
00058 
00059   if (__builtin_expect (group == NULL || group[0] == '\0', 0))
00060     return NSS_STATUS_UNAVAIL;
00061 
00062   char *domain;
00063   if (__builtin_expect (yp_get_default_domain (&domain), 0))
00064     return NSS_STATUS_UNAVAIL;
00065 
00066   status = yperr2nss (yp_match (domain, "netgroup", group, strlen (group),
00067                             &netgrp->data, &len));
00068   if (__builtin_expect (status == NSS_STATUS_SUCCESS, 1))
00069     {
00070       /* Our implementation of yp_match already allocates a buffer
00071         which is one byte larger than the value in LEN specifies
00072         and the last byte is filled with NUL.  So we can simply
00073         use that buffer.  */
00074       assert (len >= 0);
00075       assert (malloc_usable_size (netgrp->data) >= len + 1);
00076       assert (netgrp->data[len] == '\0');
00077 
00078       netgrp->data_size = len;
00079       netgrp->cursor = netgrp->data;
00080     }
00081 
00082   return status;
00083 }
00084 
00085 
00086 enum nss_status
00087 _nss_nis_endnetgrent (struct __netgrent *netgrp)
00088 {
00089   internal_nis_endnetgrent (netgrp);
00090 
00091   return NSS_STATUS_SUCCESS;
00092 }
00093 
00094 
00095 enum nss_status
00096 _nss_nis_getnetgrent_r (struct __netgrent *result, char *buffer, size_t buflen,
00097                      int *errnop)
00098 {
00099   return _nss_netgroup_parseline (&result->cursor, result, buffer, buflen,
00100                               errnop);
00101 }