]> git.ipfire.org Git - thirdparty/glibc.git/blob - hesiod/nss_hesiod/hesiod-service.c
Update.
[thirdparty/glibc.git] / hesiod / nss_hesiod / hesiod-service.c
1 /* Copyright (C) 1997, 2000, 2002 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
19
20 #include <errno.h>
21 #include <hesiod.h>
22 #include <netdb.h>
23 #include <netinet/in.h>
24 #include <nss.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "nss_hesiod.h"
30
31 /* Hesiod uses a format for service entries that differs from the
32 traditional format. We therefore declare our own parser. */
33
34 #define ENTNAME servent
35
36 struct servent_data {};
37
38 #define TRAILING_LIST_MEMBER s_aliases
39 #define TRAILING_LIST_SEPARATOR_P isspace
40 #include <nss/nss_files/files-parse.c>
41 #define ISSC_OR_SPACE(c) ((c) == ';' || isspace (c))
42 LINE_PARSER
43 ("#",
44 STRING_FIELD (result->s_name, ISSC_OR_SPACE, 1);
45 STRING_FIELD (result->s_proto, ISSC_OR_SPACE, 1);
46 INT_FIELD (result->s_port, ISSC_OR_SPACE, 10, 0, htons);
47 )
48
49 enum nss_status
50 _nss_hesiod_setservent (int stayopen)
51 {
52 return NSS_STATUS_SUCCESS;
53 }
54
55 enum nss_status
56 _nss_hesiod_endservent (void)
57 {
58 return NSS_STATUS_SUCCESS;
59 }
60
61 static enum nss_status
62 lookup (const char *name, const char *type, const char *protocol,
63 struct servent *serv, char *buffer, size_t buflen, int *errnop)
64 {
65 struct parser_data *data = (void *) buffer;
66 size_t linebuflen;
67 void *context;
68 char **list, **item;
69 int parse_res;
70 int found;
71 int olderr = errno;
72
73 context = _nss_hesiod_init ();
74 if (context == NULL)
75 return NSS_STATUS_UNAVAIL;
76
77 list = hesiod_resolve (context, name, type);
78 if (list == NULL)
79 {
80 int err = errno;
81 hesiod_end (context);
82 __set_errno (olderr);
83 return err == ENOENT ? NSS_STATUS_NOTFOUND : NSS_STATUS_UNAVAIL;
84 }
85
86 linebuflen = buffer + buflen - data->linebuffer;
87
88 item = list;
89 found = 0;
90 do
91 {
92 size_t len = strlen (*item) + 1;
93
94 if (linebuflen < len)
95 {
96 hesiod_free_list (context, list);
97 hesiod_end (context);
98 *errnop = ERANGE;
99 return NSS_STATUS_TRYAGAIN;
100 }
101
102 memcpy (data->linebuffer, *item, len);
103
104 parse_res = parse_line (buffer, serv, data, buflen, errnop);
105 if (parse_res == -1)
106 {
107 hesiod_free_list (context, list);
108 hesiod_end (context);
109 return NSS_STATUS_TRYAGAIN;
110 }
111
112 if (parse_res > 0)
113 found = protocol == NULL || strcasecmp (serv->s_proto, protocol) == 0;
114
115 ++item;
116 }
117 while (*item != NULL && !found);
118
119 hesiod_free_list (context, list);
120 hesiod_end (context);
121
122 if (found == 0)
123 {
124 __set_errno (olderr);
125 return NSS_STATUS_NOTFOUND;
126 }
127
128 return NSS_STATUS_SUCCESS;
129 }
130
131 enum nss_status
132 _nss_hesiod_getservbyname_r (const char *name, const char *protocol,
133 struct servent *serv,
134 char *buffer, size_t buflen, int *errnop)
135 {
136 return lookup (name, "service", protocol, serv, buffer, buflen, errnop);
137 }
138
139 enum nss_status
140 _nss_hesiod_getservbyport_r (const int port, const char *protocol,
141 struct servent *serv,
142 char *buffer, size_t buflen, int *errnop)
143 {
144 char portstr[6]; /* Port numbers are restricted to 16 bits. */
145
146 snprintf (portstr, sizeof portstr, "%d", ntohs (port));
147
148 return lookup (portstr, "port", protocol, serv, buffer, buflen, errnop);
149 }