1 | /* Copyright (C) 1997-2021 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 | <https://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <errno.h> |
20 | #include <hesiod.h> |
21 | #include <netdb.h> |
22 | #include <netinet/in.h> |
23 | #include <nss.h> |
24 | #include <stdio.h> |
25 | #include <stdlib.h> |
26 | #include <string.h> |
27 | |
28 | NSS_DECLARE_MODULE_FUNCTIONS (hesiod) |
29 | |
30 | /* Declare a parser for Hesiod protocol entries. Although the format |
31 | of the entries is identical to those in /etc/protocols, here is no |
32 | predefined parser for us to use. */ |
33 | |
34 | #define ENTNAME protoent |
35 | |
36 | struct protoent_data {}; |
37 | |
38 | #define TRAILING_LIST_MEMBER p_aliases |
39 | #define TRAILING_LIST_SEPARATOR_P isspace |
40 | #include <nss/nss_files/files-parse.c> |
41 | LINE_PARSER |
42 | ("#" , |
43 | STRING_FIELD (result->p_name, isspace, 1); |
44 | INT_FIELD (result->p_proto, isspace, 1, 10,); |
45 | ) |
46 | |
47 | enum nss_status |
48 | _nss_hesiod_setprotoent (int stayopen) |
49 | { |
50 | return NSS_STATUS_SUCCESS; |
51 | } |
52 | |
53 | enum nss_status |
54 | _nss_hesiod_endprotoent (void) |
55 | { |
56 | return NSS_STATUS_SUCCESS; |
57 | } |
58 | |
59 | static enum nss_status |
60 | lookup (const char *name, const char *type, struct protoent *proto, |
61 | char *buffer, size_t buflen, int *errnop) |
62 | { |
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; |
70 | |
71 | if (hesiod_init (&context) < 0) |
72 | return NSS_STATUS_UNAVAIL; |
73 | |
74 | list = hesiod_resolve (context, name, type); |
75 | if (list == NULL) |
76 | { |
77 | int err = errno; |
78 | hesiod_end (context); |
79 | __set_errno (olderr); |
80 | return err == ENOENT ? NSS_STATUS_NOTFOUND : NSS_STATUS_UNAVAIL; |
81 | } |
82 | |
83 | linebuflen = buffer + buflen - data->linebuffer; |
84 | |
85 | item = list; |
86 | found = 0; |
87 | do |
88 | { |
89 | size_t len = strlen (*item) + 1; |
90 | |
91 | if (linebuflen < len) |
92 | { |
93 | hesiod_free_list (context, list); |
94 | hesiod_end (context); |
95 | *errnop = ERANGE; |
96 | return NSS_STATUS_TRYAGAIN; |
97 | } |
98 | |
99 | memcpy (data->linebuffer, *item, len); |
100 | |
101 | parse_res = parse_line (buffer, proto, data, buflen, errnop); |
102 | if (parse_res == -1) |
103 | { |
104 | hesiod_free_list (context, list); |
105 | hesiod_end (context); |
106 | return NSS_STATUS_TRYAGAIN; |
107 | } |
108 | |
109 | if (parse_res > 0) |
110 | found = 1; |
111 | |
112 | ++item; |
113 | } |
114 | while (*item != NULL && !found); |
115 | |
116 | hesiod_free_list (context, list); |
117 | hesiod_end (context); |
118 | |
119 | if (found == 0) |
120 | { |
121 | __set_errno (olderr); |
122 | return NSS_STATUS_NOTFOUND; |
123 | } |
124 | |
125 | return NSS_STATUS_SUCCESS; |
126 | } |
127 | |
128 | enum nss_status |
129 | _nss_hesiod_getprotobyname_r (const char *name, struct protoent *proto, |
130 | char *buffer, size_t buflen, int *errnop) |
131 | { |
132 | return lookup (name, "protocol" , proto, buffer, buflen, errnop); |
133 | } |
134 | |
135 | enum nss_status |
136 | _nss_hesiod_getprotobynumber_r (const int protocol, struct protoent *proto, |
137 | char *buffer, size_t buflen, int *errnop) |
138 | { |
139 | char protostr[21]; |
140 | |
141 | snprintf (protostr, sizeof protostr, "%d" , protocol); |
142 | |
143 | return lookup (protostr, "protonum" , proto, buffer, buflen, errnop); |
144 | } |
145 | |