elf: Add all malloc tunable to unsecvars
[glibc.git] / hesiod / nss_hesiod / hesiod-service.c
blob00a2ee64b68d2da3c5137680a5a829853f5e5218
1 /* Copyright (C) 1997-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
18 #include <errno.h>
19 #include <hesiod.h>
20 #include <netdb.h>
21 #include <netinet/in.h>
22 #include <nss.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
27 NSS_DECLARE_MODULE_FUNCTIONS (hesiod)
29 /* Hesiod uses a format for service entries that differs from the
30 traditional format. We therefore declare our own parser. */
32 #define ENTNAME servent
34 struct servent_data {};
36 #define TRAILING_LIST_MEMBER s_aliases
37 #define TRAILING_LIST_SEPARATOR_P isspace
38 #include <nss/nss_files/files-parse.c>
39 #define ISSC_OR_SPACE(c) ((c) == ';' || isspace (c))
40 LINE_PARSER
41 ("#",
42 STRING_FIELD (result->s_name, ISSC_OR_SPACE, 1);
43 STRING_FIELD (result->s_proto, ISSC_OR_SPACE, 1);
44 INT_FIELD (result->s_port, ISSC_OR_SPACE, 10, 0, htons);
47 enum nss_status
48 _nss_hesiod_setservent (int stayopen)
50 return NSS_STATUS_SUCCESS;
53 enum nss_status
54 _nss_hesiod_endservent (void)
56 return NSS_STATUS_SUCCESS;
59 static enum nss_status
60 lookup (const char *name, const char *type, const char *protocol,
61 struct servent *serv, char *buffer, size_t buflen, int *errnop)
63 struct parser_data *data = (void *) buffer;
64 size_t linebuflen;
65 void *context;
66 char **list, **item;
67 int parse_res;
68 int found;
69 int olderr = errno;
71 if (hesiod_init (&context) < 0)
72 return NSS_STATUS_UNAVAIL;
74 list = hesiod_resolve (context, name, type);
75 if (list == NULL)
77 int err = errno;
78 hesiod_end (context);
79 __set_errno (olderr);
80 return err == ENOENT ? NSS_STATUS_NOTFOUND : NSS_STATUS_UNAVAIL;
83 linebuflen = buffer + buflen - data->linebuffer;
85 item = list;
86 found = 0;
89 size_t len = strlen (*item) + 1;
91 if (linebuflen < len)
93 hesiod_free_list (context, list);
94 hesiod_end (context);
95 *errnop = ERANGE;
96 return NSS_STATUS_TRYAGAIN;
99 memcpy (data->linebuffer, *item, len);
101 parse_res = parse_line (buffer, serv, data, buflen, errnop);
102 if (parse_res == -1)
104 hesiod_free_list (context, list);
105 hesiod_end (context);
106 return NSS_STATUS_TRYAGAIN;
109 if (parse_res > 0)
110 found = protocol == NULL || strcasecmp (serv->s_proto, protocol) == 0;
112 ++item;
114 while (*item != NULL && !found);
116 hesiod_free_list (context, list);
117 hesiod_end (context);
119 if (found == 0)
121 __set_errno (olderr);
122 return NSS_STATUS_NOTFOUND;
125 return NSS_STATUS_SUCCESS;
128 enum nss_status
129 _nss_hesiod_getservbyname_r (const char *name, const char *protocol,
130 struct servent *serv,
131 char *buffer, size_t buflen, int *errnop)
133 return lookup (name, "service", protocol, serv, buffer, buflen, errnop);
136 enum nss_status
137 _nss_hesiod_getservbyport_r (const int port, const char *protocol,
138 struct servent *serv,
139 char *buffer, size_t buflen, int *errnop)
141 char portstr[6]; /* Port numbers are restricted to 16 bits. */
143 snprintf (portstr, sizeof portstr, "%d", ntohs (port));
145 return lookup (portstr, "port", protocol, serv, buffer, buflen, errnop);