1 /* Copyright (C) 1997 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Mark Kettenis <kettenis@phys.uva.nl>, 1997.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include <bits/libc-lock.h>
29 /* Get the declaration of the parser function. */
31 #define STRUCTURE passwd
33 #include <nss/nss_files/files-parse.c>
35 /* Locks the static variables in this file. */
36 __libc_lock_define_initialized (static, lock
);
38 static void *context
= NULL
;
40 static enum nss_status
41 internal_setpwent (void)
45 if (hesiod_init (&context
) == -1)
46 return NSS_STATUS_UNAVAIL
;
49 return NSS_STATUS_SUCCESS
;
53 _nss_hesiod_setpwent (void)
55 enum nss_status status
;
57 __libc_lock_lock (lock
);
59 status
= internal_setpwent ();
61 __libc_lock_unlock (lock
);
67 _nss_hesiod_endpwent (void)
69 __libc_lock_lock (lock
);
77 __libc_lock_unlock (lock
);
79 return NSS_STATUS_SUCCESS
;
82 static enum nss_status
83 lookup (const char *name
, const char *type
, struct passwd
*pwd
,
84 char *buffer
, size_t buflen
, int *errnop
)
86 enum nss_status status
;
87 struct parser_data
*data
= (void *) buffer
;
93 status
= internal_setpwent ();
94 if (status
!= NSS_STATUS_SUCCESS
)
97 list
= hesiod_resolve (context
, name
, type
);
99 return errno
== ENOENT
? NSS_STATUS_NOTFOUND
: NSS_STATUS_UNAVAIL
;
101 linebuflen
= buffer
+ buflen
- data
->linebuffer
;
102 len
= strlen (*list
) + 1;
103 if (linebuflen
< len
)
105 hesiod_free_list (context
, list
);
107 return NSS_STATUS_TRYAGAIN
;
110 memcpy (data
->linebuffer
, *list
, len
);
111 hesiod_free_list (context
, list
);
113 parse_res
= _nss_files_parse_pwent (buffer
, pwd
, data
, buflen
, errnop
);
115 return parse_res
== -1 ? NSS_STATUS_TRYAGAIN
: NSS_STATUS_NOTFOUND
;
117 return NSS_STATUS_SUCCESS
;
121 _nss_hesiod_getpwnam_r (const char *name
, struct passwd
*pwd
,
122 char *buffer
, size_t buflen
, int *errnop
)
124 enum nss_status status
;
126 __libc_lock_lock (lock
);
128 status
= lookup (name
, "passwd", pwd
, buffer
, buflen
, errnop
);
130 __libc_lock_unlock (lock
);
136 _nss_hesiod_getpwuid_r (uid_t uid
, struct passwd
*pwd
,
137 char *buffer
, size_t buflen
, int *errnop
)
139 enum nss_status status
= NSS_STATUS_UNAVAIL
;
140 char uidstr
[21]; /* We will probably never have a gid_t with more
143 snprintf (uidstr
, sizeof uidstr
, "%d", uid
);
145 __libc_lock_lock (lock
);
147 status
= lookup (uidstr
, "uid", pwd
, buffer
, buflen
, errnop
);
149 __libc_lock_unlock (lock
);