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/>. */
21 #include <netinet/in.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))
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
);
48 _nss_hesiod_setservent (int stayopen
)
50 return NSS_STATUS_SUCCESS
;
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
;
71 if (hesiod_init (&context
) < 0)
72 return NSS_STATUS_UNAVAIL
;
74 list
= hesiod_resolve (context
, name
, type
);
80 return err
== ENOENT
? NSS_STATUS_NOTFOUND
: NSS_STATUS_UNAVAIL
;
83 linebuflen
= buffer
+ buflen
- data
->linebuffer
;
89 size_t len
= strlen (*item
) + 1;
93 hesiod_free_list (context
, list
);
96 return NSS_STATUS_TRYAGAIN
;
99 memcpy (data
->linebuffer
, *item
, len
);
101 parse_res
= parse_line (buffer
, serv
, data
, buflen
, errnop
);
104 hesiod_free_list (context
, list
);
105 hesiod_end (context
);
106 return NSS_STATUS_TRYAGAIN
;
110 found
= protocol
== NULL
|| strcasecmp (serv
->s_proto
, protocol
) == 0;
114 while (*item
!= NULL
&& !found
);
116 hesiod_free_list (context
, list
);
117 hesiod_end (context
);
121 __set_errno (olderr
);
122 return NSS_STATUS_NOTFOUND
;
125 return NSS_STATUS_SUCCESS
;
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
);
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
);