| 1 | /* Copyright (C) 1997-2016 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | Contributed by Mark Kettenis <kettenis@phys.uva.nl>, 1997. |
| 4 | |
| 5 | The GNU C Library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 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 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; if not, see |
| 17 | <http://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #include <errno.h> |
| 20 | #include <hesiod.h> |
| 21 | #include <pwd.h> |
| 22 | #include <nss.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | |
| 27 | #include "nss_hesiod.h" |
| 28 | |
| 29 | /* Get the declaration of the parser function. */ |
| 30 | #define ENTNAME pwent |
| 31 | #define STRUCTURE passwd |
| 32 | #define EXTERN_PARSER |
| 33 | #include <nss/nss_files/files-parse.c> |
| 34 | |
| 35 | enum nss_status |
| 36 | _nss_hesiod_setpwent (int stayopen) |
| 37 | { |
| 38 | return NSS_STATUS_SUCCESS; |
| 39 | } |
| 40 | |
| 41 | enum nss_status |
| 42 | _nss_hesiod_endpwent (void) |
| 43 | { |
| 44 | return NSS_STATUS_SUCCESS; |
| 45 | } |
| 46 | |
| 47 | static enum nss_status |
| 48 | lookup (const char *name, const char *type, struct passwd *pwd, |
| 49 | char *buffer, size_t buflen, int *errnop) |
| 50 | { |
| 51 | struct parser_data *data = (void *) buffer; |
| 52 | size_t linebuflen; |
| 53 | void *context; |
| 54 | char **list; |
| 55 | int parse_res; |
| 56 | size_t len; |
| 57 | int olderr = errno; |
| 58 | |
| 59 | context = _nss_hesiod_init (); |
| 60 | if (context == NULL) |
| 61 | return NSS_STATUS_UNAVAIL; |
| 62 | |
| 63 | list = hesiod_resolve (context, name, type); |
| 64 | if (list == NULL) |
| 65 | { |
| 66 | int err = errno; |
| 67 | hesiod_end (context); |
| 68 | __set_errno (olderr); |
| 69 | return err == ENOENT ? NSS_STATUS_NOTFOUND : NSS_STATUS_UNAVAIL; |
| 70 | } |
| 71 | |
| 72 | linebuflen = buffer + buflen - data->linebuffer; |
| 73 | len = strlen (*list) + 1; |
| 74 | if (linebuflen < len) |
| 75 | { |
| 76 | hesiod_free_list (context, list); |
| 77 | hesiod_end (context); |
| 78 | *errnop = ERANGE; |
| 79 | return NSS_STATUS_TRYAGAIN; |
| 80 | } |
| 81 | |
| 82 | memcpy (data->linebuffer, *list, len); |
| 83 | hesiod_free_list (context, list); |
| 84 | hesiod_end (context); |
| 85 | |
| 86 | parse_res = _nss_files_parse_pwent (buffer, pwd, data, buflen, errnop); |
| 87 | if (parse_res < 1) |
| 88 | { |
| 89 | __set_errno (olderr); |
| 90 | return parse_res == -1 ? NSS_STATUS_TRYAGAIN : NSS_STATUS_NOTFOUND; |
| 91 | } |
| 92 | |
| 93 | return NSS_STATUS_SUCCESS; |
| 94 | } |
| 95 | |
| 96 | enum nss_status |
| 97 | _nss_hesiod_getpwnam_r (const char *name, struct passwd *pwd, |
| 98 | char *buffer, size_t buflen, int *errnop) |
| 99 | { |
| 100 | return lookup (name, "passwd" , pwd, buffer, buflen, errnop); |
| 101 | } |
| 102 | |
| 103 | enum nss_status |
| 104 | _nss_hesiod_getpwuid_r (uid_t uid, struct passwd *pwd, |
| 105 | char *buffer, size_t buflen, int *errnop) |
| 106 | { |
| 107 | char uidstr[21]; /* We will probably never have a gid_t with more |
| 108 | than 64 bits. */ |
| 109 | |
| 110 | snprintf (uidstr, sizeof uidstr, "%d" , uid); |
| 111 | |
| 112 | return lookup (uidstr, "uid" , pwd, buffer, buflen, errnop); |
| 113 | } |
| 114 | |