Back to index

glibc  2.9
hesiod-pwd.c
Go to the documentation of this file.
00001 /* Copyright (C) 1997, 2000, 2002 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Mark Kettenis <kettenis@phys.uva.nl>, 1997.
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 <errno.h>
00021 #include <hesiod.h>
00022 #include <pwd.h>
00023 #include <nss.h>
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 #include <string.h>
00027 
00028 #include "nss_hesiod.h"
00029 
00030 /* Get the declaration of the parser function.  */
00031 #define ENTNAME pwent
00032 #define STRUCTURE passwd
00033 #define EXTERN_PARSER
00034 #include <nss/nss_files/files-parse.c>
00035 
00036 enum nss_status
00037 _nss_hesiod_setpwent (int stayopen)
00038 {
00039   return NSS_STATUS_SUCCESS;
00040 }
00041 
00042 enum nss_status
00043 _nss_hesiod_endpwent (void)
00044 {
00045   return NSS_STATUS_SUCCESS;
00046 }
00047 
00048 static enum nss_status
00049 lookup (const char *name, const char *type, struct passwd *pwd,
00050        char *buffer, size_t buflen, int *errnop)
00051 {
00052   struct parser_data *data = (void *) buffer;
00053   size_t linebuflen;
00054   void *context;
00055   char **list;
00056   int parse_res;
00057   size_t len;
00058   int olderr = errno;
00059 
00060   context = _nss_hesiod_init ();
00061   if (context == NULL)
00062     return NSS_STATUS_UNAVAIL;
00063 
00064   list = hesiod_resolve (context, name, type);
00065   if (list == NULL)
00066     {
00067       int err = errno;
00068       hesiod_end (context);
00069       __set_errno (olderr);
00070       return err == ENOENT ? NSS_STATUS_NOTFOUND : NSS_STATUS_UNAVAIL;
00071     }
00072 
00073   linebuflen = buffer + buflen - data->linebuffer;
00074   len = strlen (*list) + 1;
00075   if (linebuflen < len)
00076     {
00077       hesiod_free_list (context, list);
00078       hesiod_end (context);
00079       *errnop = ERANGE;
00080       return NSS_STATUS_TRYAGAIN;
00081     }
00082 
00083   memcpy (data->linebuffer, *list, len);
00084   hesiod_free_list (context, list);
00085   hesiod_end (context);
00086 
00087   parse_res = _nss_files_parse_pwent (buffer, pwd, data, buflen, errnop);
00088   if (parse_res < 1)
00089     {
00090       __set_errno (olderr);
00091       return parse_res == -1 ? NSS_STATUS_TRYAGAIN : NSS_STATUS_NOTFOUND;
00092     }
00093 
00094   return NSS_STATUS_SUCCESS;
00095 }
00096 
00097 enum nss_status
00098 _nss_hesiod_getpwnam_r (const char *name, struct passwd *pwd,
00099                      char *buffer, size_t buflen, int *errnop)
00100 {
00101   return lookup (name, "passwd", pwd, buffer, buflen, errnop);
00102 }
00103 
00104 enum nss_status
00105 _nss_hesiod_getpwuid_r (uid_t uid, struct passwd *pwd,
00106                      char *buffer, size_t buflen, int *errnop)
00107 {
00108   char uidstr[21];   /* We will probably never have a gid_t with more
00109                         than 64 bits.  */
00110 
00111   snprintf (uidstr, sizeof uidstr, "%d", uid);
00112 
00113   return lookup (uidstr, "uid", pwd, buffer, buflen, errnop);
00114 }