1 /* Copyright (C) 1997-2005 Luke Howard.
2 This file is part of the nss_ldap library.
3 Contributed by Luke Howard, <lukeh@padl.com>, 1997.
4 (The author maintains a non-exclusive licence to distribute this file
5 under their own conditions.)
6
7 The nss_ldap library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
11
12 The nss_ldap library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
16
17 You should have received a copy of the GNU Library General Public
18 License along with the nss_ldap library; see the file COPYING.LIB. If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
21 */
22
23 /*
24 * Support DNS SRV records. I look up the SRV record for
25 * _ldap._tcp.gnu.org.
26 * and build the DN DC=gnu,DC=org.
27 * Thanks to Assar & co for resolve.[ch].
28 */
29
30 static char rcsId[] =
31 "$Id: dnsconfig.c,v 2.45 2007/10/01 14:25:30 lukeh Exp $";
32
33 #include "config.h"
34
35 #include <stdio.h>
36 #include <string.h>
37 #include <stdlib.h>
38 #include <sys/param.h>
39 #include <netdb.h>
40 #include <syslog.h>
41 #include <netinet/in.h>
42 #include <arpa/nameser.h>
43 #include <resolv.h>
44 #include <string.h>
45
46 #ifdef HAVE_LBER_H
47 #include <lber.h>
48 #endif
49 #ifdef HAVE_LDAP_H
50 #include <ldap.h>
51 #endif
52
53 #ifndef HAVE_SNPRINTF
54 #include "snprintf.h"
55 #endif
56
57 #include "ldap-nss.h"
58 #include "util.h"
59 #include "resolve.h"
60 #include "dnsconfig.h"
61
62
63 /* map gnu.org into DC=gnu,DC=org */
64 NSS_STATUS
_nss_ldap_getdnsdn(char * src_domain,char ** rval,char ** buffer,size_t * buflen)65 _nss_ldap_getdnsdn (char *src_domain,
66 char **rval, char **buffer, size_t * buflen)
67 {
68 char *p;
69 int len = 0;
70 #ifdef HAVE_STRTOK_R
71 char *st = NULL;
72 #endif
73 char *bptr;
74 char *domain, *domain_copy;
75
76 /* we need to take a copy of domain, because strtok() modifies
77 * it in place. Bad.
78 */
79 domain_copy = strdup (src_domain);
80 if (domain_copy == NULL)
81 {
82 return NSS_TRYAGAIN;
83 }
84
85 domain = domain_copy;
86
87 bptr = *rval = *buffer;
88 **rval = '\0';
89
90 #ifndef HAVE_STRTOK_R
91 while ((p = strtok (domain, ".")))
92 #else
93 while ((p = strtok_r (domain, ".", &st)))
94 #endif
95 {
96 len = strlen (p);
97
98 if (*buflen < (size_t) (len + DC_ATTR_AVA_LEN + 1 /* D C = [,|\0] */ ))
99 {
100 free (domain_copy);
101 return NSS_TRYAGAIN;
102 }
103
104 if (domain == NULL)
105 {
106 strcpy (bptr, ",");
107 bptr++;
108 }
109 else
110 {
111 domain = NULL;
112 }
113
114 strcpy (bptr, DC_ATTR_AVA);
115 bptr += DC_ATTR_AVA_LEN;
116
117 strcpy (bptr, p);
118 bptr += len; /* don't include comma */
119 *buffer += len + DC_ATTR_AVA_LEN + 1;
120 *buflen -= len + DC_ATTR_AVA_LEN + 1;
121 }
122
123 if (bptr != NULL)
124 {
125 (*rval)[bptr - *rval] = '\0';
126 }
127
128 free (domain_copy);
129
130 return NSS_SUCCESS;
131 }
132
133 NSS_STATUS
_nss_ldap_mergeconfigfromdns(ldap_config_t * result,char ** buffer,size_t * buflen)134 _nss_ldap_mergeconfigfromdns (ldap_config_t * result,
135 char **buffer, size_t *buflen)
136 {
137 NSS_STATUS stat = NSS_SUCCESS;
138 struct dns_reply *r;
139 struct resource_record *rr;
140 char domain[MAXHOSTNAMELEN + 1];
141 char *pDomain;
142 char uribuf[NSS_BUFSIZ];
143
144 if ((_res.options & RES_INIT) == 0 && res_init () == -1)
145 {
146 return NSS_UNAVAIL;
147 }
148
149 snprintf (domain, sizeof (domain), "_ldap._tcp.%s.",
150 result->ldc_srv_domain ? result->ldc_srv_domain : _res.defdname);
151 pDomain = domain;
152
153 r = dns_lookup (pDomain, "srv");
154 if (r == NULL)
155 {
156 return NSS_NOTFOUND;
157 }
158
159 /* XXX sort by priority */
160 for (rr = r->head; rr != NULL; rr = rr->next)
161 {
162 if (rr->type == T_SRV)
163 {
164 snprintf (uribuf, sizeof(uribuf), "ldap%s://%s:%d",
165 (rr->u.srv->port == LDAPS_PORT) ? "s" : "",
166 rr->u.srv->target,
167 rr->u.srv->port);
168
169 stat = _nss_ldap_add_uri (result, uribuf, buffer, buflen);
170 if (stat != NSS_SUCCESS)
171 {
172 break;
173 }
174 }
175 }
176
177 dns_free_data (r);
178 stat = NSS_SUCCESS;
179
180 if (result->ldc_base == NULL)
181 {
182 stat = _nss_ldap_getdnsdn (_res.defdname, &result->ldc_base,
183 buffer, buflen);
184 }
185
186 return stat;
187 }
188
189