ISC DHCP  4.3.3-P1
A reference DHCPv4 and DHCPv6 implementation
ldap.c
Go to the documentation of this file.
1 /* ldap.c
2 
3  Routines for reading the configuration from LDAP */
4 
5 /*
6  * Copyright (c) 2010,2015 by Internet Systems Consortium, Inc. ("ISC")
7  * Copyright (c) 2003-2006 Ntelos, Inc.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in the
18  * documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of The Internet Software Consortium nor the names
20  * of its contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
24  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
25  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27  * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
28  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
31  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
32  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
34  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  * This LDAP module was written by Brian Masney <masneyb@ntelos.net>. Its
38  * development was sponsored by Ntelos, Inc. (www.ntelos.com).
39  */
40 
41 #include "dhcpd.h"
42 #if defined(LDAP_CONFIGURATION)
43 #include <signal.h>
44 #include <errno.h>
45 #include <ctype.h>
46 #include <netdb.h>
47 #include <net/if.h>
48 #if defined(HAVE_IFADDRS_H)
49 #include <ifaddrs.h>
50 #endif
51 #include <string.h>
52 
53 #if defined(LDAP_CASA_AUTH)
54 #include "ldap_casa.h"
55 #endif
56 
57 #if defined(LDAP_USE_GSSAPI)
58 #include <sasl/sasl.h>
59 #include "ldap_krb_helper.h"
60 #endif
61 
62 static LDAP * ld = NULL;
63 static char *ldap_server = NULL,
64  *ldap_username = NULL,
65  *ldap_password = NULL,
66  *ldap_base_dn = NULL,
67  *ldap_dhcp_server_cn = NULL,
68  *ldap_debug_file = NULL;
69 static int ldap_port = LDAP_PORT,
70  ldap_method = LDAP_METHOD_DYNAMIC,
71  ldap_referrals = -1,
72  ldap_debug_fd = -1,
73  ldap_enable_retry = -1,
74  ldap_init_retry = -1;
75 #if defined (LDAP_USE_SSL)
76 static int ldap_use_ssl = -1, /* try TLS if possible */
77  ldap_tls_reqcert = -1,
78  ldap_tls_crlcheck = -1;
79 static char *ldap_tls_ca_file = NULL,
80  *ldap_tls_ca_dir = NULL,
81  *ldap_tls_cert = NULL,
82  *ldap_tls_key = NULL,
83  *ldap_tls_ciphers = NULL,
84  *ldap_tls_randfile = NULL;
85 #endif
86 
87 #if defined (LDAP_USE_GSSAPI)
88 static char *ldap_gssapi_keytab = NULL,
89  *ldap_gssapi_principal = NULL;
90 
91 struct ldap_sasl_instance {
92  char *sasl_mech;
93  char *sasl_realm;
94  char *sasl_authz_id;
95  char *sasl_authc_id;
96  char *sasl_password;
97 };
98 
99 static struct ldap_sasl_instance *ldap_sasl_inst = NULL;
100 
101 static int
102 _ldap_sasl_interact(LDAP *ld, unsigned flags, void *defaults, void *sin) ;
103 #endif
104 
105 static struct ldap_config_stack *ldap_stack = NULL;
106 
107 typedef struct ldap_dn_node {
108  struct ldap_dn_node *next;
109  size_t refs;
110  char *dn;
111 } ldap_dn_node;
112 
113 static ldap_dn_node *ldap_service_dn_head = NULL;
114 static ldap_dn_node *ldap_service_dn_tail = NULL;
115 
116 static int ldap_read_function (struct parse *cfile);
117 
118 static struct parse *
119 x_parser_init(const char *name)
120 {
121  struct parse *cfile;
122  isc_result_t res;
123  char *inbuf;
124 
125  inbuf = dmalloc (LDAP_BUFFER_SIZE, MDL);
126  if (inbuf == NULL)
127  return NULL;
128 
129  cfile = (struct parse *) NULL;
130  res = new_parse (&cfile, -1, inbuf, LDAP_BUFFER_SIZE, name, 0);
131  if (res != ISC_R_SUCCESS)
132  {
133  dfree(inbuf, MDL);
134  return NULL;
135  }
136  /* the buffer is still empty */
137  cfile->bufsiz = LDAP_BUFFER_SIZE;
138  cfile->buflen = cfile->bufix = 0;
139  /* attach ldap read function */
140  cfile->read_function = ldap_read_function;
141  return cfile;
142 }
143 
144 static isc_result_t
145 x_parser_free(struct parse **cfile)
146 {
147  if (cfile && *cfile)
148  {
149  if ((*cfile)->inbuf)
150  dfree((*cfile)->inbuf, MDL);
151  (*cfile)->inbuf = NULL;
152  (*cfile)->bufsiz = 0;
153  return end_parse(cfile);
154  }
155  return ISC_R_SUCCESS;
156 }
157 
158 static int
159 x_parser_resize(struct parse *cfile, size_t len)
160 {
161  size_t size;
162  char * temp;
163 
164  /* grow by len rounded up at LDAP_BUFFER_SIZE */
165  size = cfile->bufsiz + (len | (LDAP_BUFFER_SIZE-1)) + 1;
166 
167  /* realloc would be better, but there isn't any */
168  if ((temp = dmalloc (size, MDL)) != NULL)
169  {
170 #if defined (DEBUG_LDAP)
171  log_info ("Reallocated %s buffer from %zu to %zu",
172  cfile->tlname, cfile->bufsiz, size);
173 #endif
174  memcpy(temp, cfile->inbuf, cfile->bufsiz);
175  dfree(cfile->inbuf, MDL);
176  cfile->inbuf = temp;
177  cfile->bufsiz = size;
178  return 1;
179  }
180 
181  /*
182  * Hmm... what is worser, consider it as fatal error and
183  * bail out completely or discard config data in hope it
184  * is "only" an option in dynamic host lookup?
185  */
186  log_error("Unable to reallocated %s buffer from %zu to %zu",
187  cfile->tlname, cfile->bufsiz, size);
188  return 0;
189 }
190 
191 static char *
192 x_parser_strcat(struct parse *cfile, const char *str)
193 {
194  size_t cur = strlen(cfile->inbuf);
195  size_t len = strlen(str);
196  size_t cnt;
197 
198  if (cur + len >= cfile->bufsiz && !x_parser_resize(cfile, len))
199  return NULL;
200 
201  cnt = cfile->bufsiz > cur ? cfile->bufsiz - cur - 1 : 0;
202  return strncat(cfile->inbuf, str, cnt);
203 }
204 
205 static inline void
206 x_parser_reset(struct parse *cfile)
207 {
208  cfile->inbuf[0] = '\0';
209  cfile->bufix = cfile->buflen = 0;
210 }
211 
212 static inline size_t
213 x_parser_length(struct parse *cfile)
214 {
215  cfile->buflen = strlen(cfile->inbuf);
216  return cfile->buflen;
217 }
218 
219 static char *
220 x_strxform(char *dst, const char *src, size_t dst_size,
221  int (*xform)(int))
222 {
223  if(dst && src && dst_size)
224  {
225  size_t len, pos;
226 
227  len = strlen(src);
228  for(pos=0; pos < len && pos + 1 < dst_size; pos++)
229  dst[pos] = xform((int)src[pos]);
230  dst[pos] = '\0';
231 
232  return dst;
233  }
234  return NULL;
235 }
236 
237 static int
238 get_host_entry(char *fqdnname, size_t fqdnname_size,
239  char *hostaddr, size_t hostaddr_size)
240 {
241 #if defined(MAXHOSTNAMELEN)
242  char hname[MAXHOSTNAMELEN+1];
243 #else
244  char hname[65];
245 #endif
246  struct hostent *hp;
247 
248  if (NULL == fqdnname || 1 >= fqdnname_size)
249  return -1;
250 
251  memset(hname, 0, sizeof(hname));
252  if (gethostname(hname, sizeof(hname)-1))
253  return -1;
254 
255  if (NULL == (hp = gethostbyname(hname)))
256  return -1;
257 
258  strncpy(fqdnname, hp->h_name, fqdnname_size-1);
259  fqdnname[fqdnname_size-1] = '\0';
260 
261  if (hostaddr != NULL)
262  {
263  if (hp->h_addr != NULL)
264  {
265  struct in_addr *aptr = (struct in_addr *)hp->h_addr;
266 #if defined(HAVE_INET_NTOP)
267  if (hostaddr_size >= INET_ADDRSTRLEN &&
268  inet_ntop(AF_INET, aptr, hostaddr, hostaddr_size) != NULL)
269  {
270  return 0;
271  }
272 #else
273  char *astr = inet_ntoa(*aptr);
274  size_t alen = strlen(astr);
275  if (astr && alen > 0 && hostaddr_size > alen)
276  {
277  strncpy(hostaddr, astr, hostaddr_size-1);
278  hostaddr[hostaddr_size-1] = '\0';
279  return 0;
280  }
281 #endif
282  }
283  return -1;
284  }
285  return 0;
286 }
287 
288 #if defined(HAVE_IFADDRS_H)
289 static int
290 is_iface_address(struct ifaddrs *addrs, struct in_addr *addr)
291 {
292  struct ifaddrs *ia;
293  struct sockaddr_in *sa;
294  int num = 0;
295 
296  if(addrs == NULL || addr == NULL)
297  return -1;
298 
299  for (ia = addrs; ia != NULL; ia = ia->ifa_next)
300  {
301  ++num;
302  if (ia->ifa_addr && (ia->ifa_flags & IFF_UP) &&
303  ia->ifa_addr->sa_family == AF_INET)
304  {
305  sa = (struct sockaddr_in *)(ia->ifa_addr);
306  if (addr->s_addr == sa->sin_addr.s_addr)
307  return num;
308  }
309  }
310  return 0;
311 }
312 
313 static int
314 get_host_address(const char *hostname, char *hostaddr, size_t hostaddr_size, struct ifaddrs *addrs)
315 {
316  if (hostname && *hostname && hostaddr && hostaddr_size)
317  {
318  struct in_addr addr;
319 
320 #if defined(HAVE_INET_PTON)
321  if (inet_pton(AF_INET, hostname, &addr) == 1)
322 #else
323  if (inet_aton(hostname, &addr) != 0)
324 #endif
325  {
326  /* it is already IP address string */
327  if(strlen(hostname) < hostaddr_size)
328  {
329  strncpy(hostaddr, hostname, hostaddr_size-1);
330  hostaddr[hostaddr_size-1] = '\0';
331 
332  if (addrs != NULL && is_iface_address (addrs, &addr) > 0)
333  return 1;
334  else
335  return 0;
336  }
337  }
338  else
339  {
340  struct hostent *hp;
341  if ((hp = gethostbyname(hostname)) != NULL && hp->h_addr != NULL)
342  {
343  struct in_addr *aptr = (struct in_addr *)hp->h_addr;
344  int mret = 0;
345 
346  if (addrs != NULL)
347  {
348  char **h;
349  for (h=hp->h_addr_list; *h; h++)
350  {
351  struct in_addr *haddr = (struct in_addr *)*h;
352  if (is_iface_address (addrs, haddr) > 0)
353  {
354  aptr = haddr;
355  mret = 1;
356  }
357  }
358  }
359 
360 #if defined(HAVE_INET_NTOP)
361  if (hostaddr_size >= INET_ADDRSTRLEN &&
362  inet_ntop(AF_INET, aptr, hostaddr, hostaddr_size) != NULL)
363  {
364  return mret;
365  }
366 #else
367  char *astr = inet_ntoa(*aptr);
368  size_t alen = strlen(astr);
369  if (astr && alen > 0 && alen < hostaddr_size)
370  {
371  strncpy(hostaddr, astr, hostaddr_size-1);
372  hostaddr[hostaddr_size-1] = '\0';
373  return mret;
374  }
375 #endif
376  }
377  }
378  }
379  return -1;
380 }
381 #endif /* HAVE_IFADDRS_H */
382 
383 static void
384 ldap_parse_class (struct ldap_config_stack *item, struct parse *cfile)
385 {
386  struct berval **tempbv;
387 
388  if ((tempbv = ldap_get_values_len (ld, item->ldent, "cn")) == NULL ||
389  tempbv[0] == NULL)
390  {
391  if (tempbv != NULL)
392  ldap_value_free_len (tempbv);
393 
394  return;
395  }
396 
397  x_parser_strcat (cfile, "class \"");
398  x_parser_strcat (cfile, tempbv[0]->bv_val);
399  x_parser_strcat (cfile, "\" {\n");
400 
401  item->close_brace = 1;
402  ldap_value_free_len (tempbv);
403 }
404 
405 static int
406 is_hex_string(const char *str)
407 {
408  int colon = 1;
409  int xdigit = 0;
410  size_t i;
411 
412  if (!str)
413  return 0;
414 
415  if (*str == '-')
416  str++;
417 
418  for (i=0; str[i]; ++i)
419  {
420  if (str[i] == ':')
421  {
422  xdigit = 0;
423  if(++colon > 1)
424  return 0;
425  }
426  else if(isxdigit((unsigned char)str[i]))
427  {
428  colon = 0;
429  if (++xdigit > 2)
430  return 0;
431  }
432  else
433  return 0;
434  }
435  return i > 0 && !colon;
436 }
437 
438 static void
439 ldap_parse_subclass (struct ldap_config_stack *item, struct parse *cfile)
440 {
441  struct berval **tempbv, **classdata;
442  char *tmp;
443 
444  if ((tempbv = ldap_get_values_len (ld, item->ldent, "cn")) == NULL ||
445  tempbv[0] == NULL)
446  {
447  if (tempbv != NULL)
448  ldap_value_free_len (tempbv);
449 
450  return;
451  }
452 
453  if ((classdata = ldap_get_values_len (ld, item->ldent,
454  "dhcpClassData")) == NULL ||
455  classdata[0] == NULL)
456  {
457  if (classdata != NULL)
458  ldap_value_free_len (classdata);
459  ldap_value_free_len (tempbv);
460 
461  return;
462  }
463 
464  x_parser_strcat (cfile, "subclass \"");
465  x_parser_strcat (cfile, classdata[0]->bv_val);
466  if (is_hex_string(tempbv[0]->bv_val))
467  {
468  x_parser_strcat (cfile, "\" ");
469  x_parser_strcat (cfile, tempbv[0]->bv_val);
470  x_parser_strcat (cfile, " {\n");
471  }
472  else
473  {
474  tmp = quotify_string(tempbv[0]->bv_val, MDL);
475  x_parser_strcat (cfile, "\" \"");
476  x_parser_strcat (cfile, tmp);
477  x_parser_strcat (cfile, "\" {\n");
478  dfree(tmp, MDL);
479  }
480 
481  item->close_brace = 1;
482  ldap_value_free_len (tempbv);
483  ldap_value_free_len (classdata);
484 }
485 
486 
487 static void
488 ldap_parse_host (struct ldap_config_stack *item, struct parse *cfile)
489 {
490  struct berval **tempbv, **hwaddr;
491 
492  if ((tempbv = ldap_get_values_len (ld, item->ldent, "cn")) == NULL ||
493  tempbv[0] == NULL)
494  {
495  if (tempbv != NULL)
496  ldap_value_free_len (tempbv);
497 
498  return;
499  }
500 
501  hwaddr = ldap_get_values_len (ld, item->ldent, "dhcpHWAddress");
502 
503  x_parser_strcat (cfile, "host ");
504  x_parser_strcat (cfile, tempbv[0]->bv_val);
505  x_parser_strcat (cfile, " {\n");
506 
507  if (hwaddr != NULL)
508  {
509  if (hwaddr[0] != NULL)
510  {
511  x_parser_strcat (cfile, "hardware ");
512  x_parser_strcat (cfile, hwaddr[0]->bv_val);
513  x_parser_strcat (cfile, ";\n");
514  }
515  ldap_value_free_len (hwaddr);
516  }
517 
518  item->close_brace = 1;
519  ldap_value_free_len (tempbv);
520 }
521 
522 
523 static void
524 ldap_parse_shared_network (struct ldap_config_stack *item, struct parse *cfile)
525 {
526  struct berval **tempbv;
527 
528  if ((tempbv = ldap_get_values_len (ld, item->ldent, "cn")) == NULL ||
529  tempbv[0] == NULL)
530  {
531  if (tempbv != NULL)
532  ldap_value_free_len (tempbv);
533 
534  return;
535  }
536 
537  x_parser_strcat (cfile, "shared-network \"");
538  x_parser_strcat (cfile, tempbv[0]->bv_val);
539  x_parser_strcat (cfile, "\" {\n");
540 
541  item->close_brace = 1;
542  ldap_value_free_len (tempbv);
543 }
544 
545 
546 static void
547 parse_netmask (int netmask, char *netmaskbuf)
548 {
549  unsigned long nm;
550  int i;
551 
552  nm = 0;
553  for (i=1; i <= netmask; i++)
554  {
555  nm |= 1 << (32 - i);
556  }
557 
558  sprintf (netmaskbuf, "%d.%d.%d.%d", (int) (nm >> 24) & 0xff,
559  (int) (nm >> 16) & 0xff,
560  (int) (nm >> 8) & 0xff,
561  (int) nm & 0xff);
562 }
563 
564 
565 static void
566 ldap_parse_subnet (struct ldap_config_stack *item, struct parse *cfile)
567 {
568  struct berval **tempbv, **netmaskstr;
569  char netmaskbuf[sizeof("255.255.255.255")];
570  int i;
571 
572  if ((tempbv = ldap_get_values_len (ld, item->ldent, "cn")) == NULL ||
573  tempbv[0] == NULL)
574  {
575  if (tempbv != NULL)
576  ldap_value_free_len (tempbv);
577 
578  return;
579  }
580 
581  if ((netmaskstr = ldap_get_values_len (ld, item->ldent,
582  "dhcpNetmask")) == NULL ||
583  netmaskstr[0] == NULL)
584  {
585  if (netmaskstr != NULL)
586  ldap_value_free_len (netmaskstr);
587  ldap_value_free_len (tempbv);
588 
589  return;
590  }
591 
592  x_parser_strcat (cfile, "subnet ");
593  x_parser_strcat (cfile, tempbv[0]->bv_val);
594 
595  x_parser_strcat (cfile, " netmask ");
596  parse_netmask (strtol (netmaskstr[0]->bv_val, NULL, 10), netmaskbuf);
597  x_parser_strcat (cfile, netmaskbuf);
598 
599  x_parser_strcat (cfile, " {\n");
600 
601  ldap_value_free_len (tempbv);
602  ldap_value_free_len (netmaskstr);
603 
604  if ((tempbv = ldap_get_values_len (ld, item->ldent, "dhcpRange")) != NULL)
605  {
606  for (i=0; tempbv[i] != NULL; i++)
607  {
608  x_parser_strcat (cfile, "range");
609  x_parser_strcat (cfile, " ");
610  x_parser_strcat (cfile, tempbv[i]->bv_val);
611  x_parser_strcat (cfile, ";\n");
612  }
613  ldap_value_free_len (tempbv);
614  }
615 
616  item->close_brace = 1;
617 }
618 
619 static void
620 ldap_parse_subnet6 (struct ldap_config_stack *item, struct parse *cfile)
621 {
622  struct berval **tempbv;
623  int i;
624 
625  if ((tempbv = ldap_get_values_len (ld, item->ldent, "cn")) == NULL ||
626  tempbv[0] == NULL)
627  {
628  if (tempbv != NULL)
629  ldap_value_free_len (tempbv);
630 
631  return;
632  }
633 
634  x_parser_strcat (cfile, "subnet6 ");
635  x_parser_strcat (cfile, tempbv[0]->bv_val);
636 
637  x_parser_strcat (cfile, " {\n");
638 
639  ldap_value_free_len (tempbv);
640 
641  if ((tempbv = ldap_get_values_len (ld, item->ldent, "dhcpRange")) != NULL)
642  {
643  for (i=0; tempbv[i] != NULL; i++)
644  {
645  x_parser_strcat (cfile, "range6");
646  x_parser_strcat (cfile, " ");
647  x_parser_strcat (cfile, tempbv[i]->bv_val);
648  x_parser_strcat (cfile, ";\n");
649  }
650  ldap_value_free_len (tempbv);
651  }
652 
653  if ((tempbv = ldap_get_values_len (ld, item->ldent, "dhcpPermitList")) != NULL)
654  {
655  for (i=0; tempbv[i] != NULL; i++)
656  {
657  x_parser_strcat (cfile, tempbv[i]->bv_val);
658  x_parser_strcat (cfile, ";\n");
659  }
660  ldap_value_free_len (tempbv);
661  }
662 
663  item->close_brace = 1;
664 }
665 
666 static void
667 ldap_parse_pool (struct ldap_config_stack *item, struct parse *cfile)
668 {
669  struct berval **tempbv;
670  int i;
671 
672  x_parser_strcat (cfile, "pool {\n");
673 
674  if ((tempbv = ldap_get_values_len (ld, item->ldent, "dhcpRange")) != NULL)
675  {
676  x_parser_strcat (cfile, "range");
677  for (i=0; tempbv[i] != NULL; i++)
678  {
679  x_parser_strcat (cfile, " ");
680  x_parser_strcat (cfile, tempbv[i]->bv_val);
681  }
682  x_parser_strcat (cfile, ";\n");
683  ldap_value_free_len (tempbv);
684  }
685 
686  if ((tempbv = ldap_get_values_len (ld, item->ldent, "dhcpPermitList")) != NULL)
687  {
688  for (i=0; tempbv[i] != NULL; i++)
689  {
690  x_parser_strcat (cfile, tempbv[i]->bv_val);
691  x_parser_strcat (cfile, ";\n");
692  }
693  ldap_value_free_len (tempbv);
694  }
695 
696  item->close_brace = 1;
697 }
698 
699 static void
700 ldap_parse_pool6 (struct ldap_config_stack *item, struct parse *cfile)
701 {
702  struct berval **tempbv;
703  int i;
704 
705  x_parser_strcat (cfile, "pool {\n");
706 
707  if ((tempbv = ldap_get_values_len (ld, item->ldent, "dhcpRange")) != NULL)
708  {
709  x_parser_strcat (cfile, "range6");
710  for (i=0; tempbv[i] != NULL; i++)
711  {
712  x_parser_strcat (cfile, " ");
713  x_parser_strcat (cfile, tempbv[i]->bv_val);
714  }
715  x_parser_strcat (cfile, ";\n");
716  ldap_value_free_len (tempbv);
717  }
718 
719  if ((tempbv = ldap_get_values_len (ld, item->ldent, "dhcpPermitList")) != NULL)
720  {
721  for (i=0; tempbv[i] != NULL; i++)
722  {
723  x_parser_strcat(cfile, tempbv[i]->bv_val);
724  x_parser_strcat (cfile, ";\n");
725  }
726  ldap_value_free_len (tempbv);
727  }
728 
729  item->close_brace = 1;
730 }
731 
732 static void
733 ldap_parse_group (struct ldap_config_stack *item, struct parse *cfile)
734 {
735  x_parser_strcat (cfile, "group {\n");
736  item->close_brace = 1;
737 }
738 
739 
740 static void
741 ldap_parse_key (struct ldap_config_stack *item, struct parse *cfile)
742 {
743  struct berval **tempbv;
744 
745  if ((tempbv = ldap_get_values_len (ld, item->ldent, "cn")) != NULL)
746  {
747  x_parser_strcat (cfile, "key ");
748  x_parser_strcat (cfile, tempbv[0]->bv_val);
749  x_parser_strcat (cfile, " {\n");
750  ldap_value_free_len (tempbv);
751  }
752 
753  if ((tempbv = ldap_get_values_len (ld, item->ldent, "dhcpKeyAlgorithm")) != NULL)
754  {
755  x_parser_strcat (cfile, "algorithm ");
756  x_parser_strcat (cfile, tempbv[0]->bv_val);
757  x_parser_strcat (cfile, ";\n");
758  ldap_value_free_len (tempbv);
759  }
760 
761  if ((tempbv = ldap_get_values_len (ld, item->ldent, "dhcpKeySecret")) != NULL)
762  {
763  x_parser_strcat (cfile, "secret ");
764  x_parser_strcat (cfile, tempbv[0]->bv_val);
765  x_parser_strcat (cfile, ";\n");
766  ldap_value_free_len (tempbv);
767  }
768 
769  item->close_brace = 1;
770 }
771 
772 
773 static void
774 ldap_parse_zone (struct ldap_config_stack *item, struct parse *cfile)
775 {
776  char *cnFindStart, *cnFindEnd;
777  struct berval **tempbv;
778  char *keyCn;
779  size_t len;
780 
781  if ((tempbv = ldap_get_values_len (ld, item->ldent, "cn")) != NULL)
782  {
783  x_parser_strcat (cfile, "zone ");
784  x_parser_strcat (cfile, tempbv[0]->bv_val);
785  x_parser_strcat (cfile, " {\n");
786  ldap_value_free_len (tempbv);
787  }
788 
789  if ((tempbv = ldap_get_values_len (ld, item->ldent, "dhcpDnsZoneServer")) != NULL)
790  {
791  x_parser_strcat (cfile, "primary ");
792  x_parser_strcat (cfile, tempbv[0]->bv_val);
793 
794  x_parser_strcat (cfile, ";\n");
795  ldap_value_free_len (tempbv);
796  }
797 
798  if ((tempbv = ldap_get_values_len (ld, item->ldent, "dhcpKeyDN")) != NULL)
799  {
800  cnFindStart = strchr(tempbv[0]->bv_val,'=');
801  if (cnFindStart != NULL)
802  cnFindEnd = strchr(++cnFindStart,',');
803  else
804  cnFindEnd = NULL;
805 
806  if (cnFindEnd != NULL && cnFindEnd > cnFindStart)
807  {
808  len = cnFindEnd - cnFindStart;
809  keyCn = dmalloc (len + 1, MDL);
810  }
811  else
812  {
813  len = 0;
814  keyCn = NULL;
815  }
816 
817  if (keyCn != NULL)
818  {
819  strncpy (keyCn, cnFindStart, len);
820  keyCn[len] = '\0';
821 
822  x_parser_strcat (cfile, "key ");
823  x_parser_strcat (cfile, keyCn);
824  x_parser_strcat (cfile, ";\n");
825 
826  dfree (keyCn, MDL);
827  }
828 
829  ldap_value_free_len (tempbv);
830  }
831 
832  item->close_brace = 1;
833 }
834 
835 #if defined(HAVE_IFADDRS_H)
836 static void
837 ldap_parse_failover (struct ldap_config_stack *item, struct parse *cfile)
838 {
839  struct berval **tempbv, **peername;
840  struct ifaddrs *addrs = NULL;
841  char srvaddr[2][64] = {"\0", "\0"};
842  int primary, split = 0, match;
843 
844  if ((peername = ldap_get_values_len (ld, item->ldent, "cn")) == NULL ||
845  peername[0] == NULL)
846  {
847  if (peername != NULL)
848  ldap_value_free_len (peername);
849 
850  // ldap with disabled schema checks? fail to avoid syntax error.
851  log_error("Unable to find mandatory failover peering name attribute");
852  return;
853  }
854 
855  /* Get all interface addresses */
856  getifaddrs(&addrs);
857 
858  /*
859  ** when dhcpFailOverPrimaryServer or dhcpFailOverSecondaryServer
860  ** matches one of our IP address, the following valiables are set:
861  ** - primary is 1 when we are primary or 0 when we are secondary
862  ** - srvaddr[0] contains ip address of the primary
863  ** - srvaddr[1] contains ip address of the secondary
864  */
865  primary = -1;
866  if ((tempbv = ldap_get_values_len (ld, item->ldent, "dhcpFailOverPrimaryServer")) != NULL &&
867  tempbv[0] != NULL)
868  {
869  match = get_host_address (tempbv[0]->bv_val, srvaddr[0], sizeof(srvaddr[0]), addrs);
870  if (match >= 0)
871  {
872  /* we are the primary */
873  if (match > 0)
874  primary = 1;
875  }
876  else
877  {
878  log_info("Can't resolve address of the primary failover '%s' server %s",
879  peername[0]->bv_val, tempbv[0]->bv_val);
880  ldap_value_free_len (tempbv);
881  ldap_value_free_len (peername);
882  if (addrs)
883  freeifaddrs(addrs);
884  return;
885  }
886  }
887  if (tempbv != NULL)
888  ldap_value_free_len (tempbv);
889 
890  if ((tempbv = ldap_get_values_len (ld, item->ldent, "dhcpFailOverSecondaryServer")) != NULL &&
891  tempbv[0] != NULL)
892  {
893  match = get_host_address (tempbv[0]->bv_val, srvaddr[1], sizeof(srvaddr[1]), addrs);
894  if (match >= 0)
895  {
896  if (match > 0)
897  {
898  if (primary == 1)
899  {
900  log_info("Both, primary and secondary failover '%s' server"
901  " attributes match our local address", peername[0]->bv_val);
902  ldap_value_free_len (tempbv);
903  ldap_value_free_len (peername);
904  if (addrs)
905  freeifaddrs(addrs);
906  return;
907  }
908 
909  /* we are the secondary */
910  primary = 0;
911  }
912  }
913  else
914  {
915  log_info("Can't resolve address of the secondary failover '%s' server %s",
916  peername[0]->bv_val, tempbv[0]->bv_val);
917  ldap_value_free_len (tempbv);
918  ldap_value_free_len (peername);
919  if (addrs)
920  freeifaddrs(addrs);
921  return;
922  }
923  }
924  if (tempbv != NULL)
925  ldap_value_free_len (tempbv);
926 
927 
928  if (primary == -1 || srvaddr[0] == '\0' || srvaddr[1] == '\0')
929  {
930  log_error("Could not decide if the server type is primary"
931  " or secondary for failover peering '%s'.", peername[0]->bv_val);
932  ldap_value_free_len (peername);
933  if (addrs)
934  freeifaddrs(addrs);
935  return;
936  }
937 
938  x_parser_strcat (cfile, "failover peer \"");
939  x_parser_strcat (cfile, peername[0]->bv_val);
940  x_parser_strcat (cfile, "\" {\n");
941 
942  if (primary)
943  x_parser_strcat (cfile, "primary;\n");
944  else
945  x_parser_strcat (cfile, "secondary;\n");
946 
947  x_parser_strcat (cfile, "address ");
948  if (primary)
949  x_parser_strcat (cfile, srvaddr[0]);
950  else
951  x_parser_strcat (cfile, srvaddr[1]);
952  x_parser_strcat (cfile, ";\n");
953 
954  x_parser_strcat (cfile, "peer address ");
955  if (primary)
956  x_parser_strcat (cfile, srvaddr[1]);
957  else
958  x_parser_strcat (cfile, srvaddr[0]);
959  x_parser_strcat (cfile, ";\n");
960 
961  if ((tempbv = ldap_get_values_len (ld, item->ldent, "dhcpFailOverPrimaryPort")) != NULL &&
962  tempbv[0] != NULL)
963  {
964  if (primary)
965  x_parser_strcat (cfile, "port ");
966  else
967  x_parser_strcat (cfile, "peer port ");
968  x_parser_strcat (cfile, tempbv[0]->bv_val);
969  x_parser_strcat (cfile, ";\n");
970  }
971  if (tempbv != NULL)
972  ldap_value_free_len (tempbv);
973 
974  if ((tempbv = ldap_get_values_len (ld, item->ldent, "dhcpFailOverSecondaryPort")) != NULL &&
975  tempbv[0] != NULL)
976  {
977  if (primary)
978  x_parser_strcat (cfile, "peer port ");
979  else
980  x_parser_strcat (cfile, "port ");
981  x_parser_strcat (cfile, tempbv[0]->bv_val);
982  x_parser_strcat (cfile, ";\n");
983  }
984  if (tempbv != NULL)
985  ldap_value_free_len (tempbv);
986 
987  if ((tempbv = ldap_get_values_len (ld, item->ldent, "dhcpFailOverResponseDelay")) != NULL &&
988  tempbv[0] != NULL)
989  {
990  x_parser_strcat (cfile, "max-response-delay ");
991  x_parser_strcat (cfile, tempbv[0]->bv_val);
992  x_parser_strcat (cfile, ";\n");
993  }
994  if (tempbv != NULL)
995  ldap_value_free_len (tempbv);
996 
997  if ((tempbv = ldap_get_values_len (ld, item->ldent, "dhcpFailOverUnackedUpdates")) != NULL &&
998  tempbv[0] != NULL)
999  {
1000  x_parser_strcat (cfile, "max-unacked-updates ");
1001  x_parser_strcat (cfile, tempbv[0]->bv_val);
1002  x_parser_strcat (cfile, ";\n");
1003  }
1004  if (tempbv != NULL)
1005  ldap_value_free_len (tempbv);
1006 
1007  if ((tempbv = ldap_get_values_len (ld, item->ldent, "dhcpFailOverLoadBalanceTime")) != NULL &&
1008  tempbv[0] != NULL)
1009  {
1010  x_parser_strcat (cfile, "load balance max seconds ");
1011  x_parser_strcat (cfile, tempbv[0]->bv_val);
1012  x_parser_strcat (cfile, ";\n");
1013  }
1014  if (tempbv != NULL)
1015  ldap_value_free_len (tempbv);
1016 
1017  tempbv = NULL;
1018  if (primary &&
1019  (tempbv = ldap_get_values_len (ld, item->ldent, "dhcpMaxClientLeadTime")) != NULL &&
1020  tempbv[0] != NULL)
1021  {
1022  x_parser_strcat (cfile, "mclt ");
1023  x_parser_strcat (cfile, tempbv[0]->bv_val);
1024  x_parser_strcat (cfile, ";\n");
1025  }
1026  if (tempbv != NULL)
1027  ldap_value_free_len (tempbv);
1028 
1029  tempbv = NULL;
1030  if (primary &&
1031  (tempbv = ldap_get_values_len (ld, item->ldent, "dhcpFailOverSplit")) != NULL &&
1032  tempbv[0] != NULL)
1033  {
1034  x_parser_strcat (cfile, "split ");
1035  x_parser_strcat (cfile, tempbv[0]->bv_val);
1036  x_parser_strcat (cfile, ";\n");
1037  split = 1;
1038  }
1039  if (tempbv != NULL)
1040  ldap_value_free_len (tempbv);
1041 
1042  tempbv = NULL;
1043  if (primary && !split &&
1044  (tempbv = ldap_get_values_len (ld, item->ldent, "dhcpFailOverHashBucketAssignment")) != NULL &&
1045  tempbv[0] != NULL)
1046  {
1047  x_parser_strcat (cfile, "hba ");
1048  x_parser_strcat (cfile, tempbv[0]->bv_val);
1049  x_parser_strcat (cfile, ";\n");
1050  }
1051  if (tempbv != NULL)
1052  ldap_value_free_len (tempbv);
1053 
1054  item->close_brace = 1;
1055 }
1056 #endif /* HAVE_IFADDRS_H */
1057 
1058 static void
1059 add_to_config_stack (LDAPMessage * res, LDAPMessage * ent)
1060 {
1061  struct ldap_config_stack *ns;
1062 
1063  ns = dmalloc (sizeof (*ns), MDL);
1064  ns->res = res;
1065  ns->ldent = ent;
1066  ns->close_brace = 0;
1067  ns->processed = 0;
1068  ns->next = ldap_stack;
1069  ldap_stack = ns;
1070 }
1071 
1072 static void
1073 ldap_stop()
1074 {
1075  struct sigaction old, new;
1076 
1077  if (ld == NULL)
1078  return;
1079 
1080  /*
1081  ** ldap_unbind after a LDAP_SERVER_DOWN result
1082  ** causes a SIGPIPE and dhcpd gets terminated,
1083  ** since it doesn't handle it...
1084  */
1085 
1086  new.sa_flags = 0;
1087  new.sa_handler = SIG_IGN;
1088  sigemptyset (&new.sa_mask);
1089  sigaction (SIGPIPE, &new, &old);
1090 
1091  ldap_unbind_ext_s (ld, NULL, NULL);
1092  ld = NULL;
1093 
1094  sigaction (SIGPIPE, &old, &new);
1095 }
1096 
1097 
1098 static char *
1099 _do_lookup_dhcp_string_option (struct option_state *options, int option_name)
1100 {
1101  struct option_cache *oc;
1102  struct data_string db;
1103  char *ret;
1104 
1105  memset (&db, 0, sizeof (db));
1106  oc = lookup_option (&server_universe, options, option_name);
1107  if (oc &&
1108  evaluate_option_cache (&db, (struct packet*) NULL,
1109  (struct lease *) NULL,
1110  (struct client_state *) NULL, options,
1111  (struct option_state *) NULL,
1112  &global_scope, oc, MDL) &&
1113  db.data != NULL && *db.data != '\0')
1114 
1115  {
1116  ret = dmalloc (db.len + 1, MDL);
1117  if (ret == NULL)
1118  log_fatal ("no memory for ldap option %d value", option_name);
1119 
1120  memcpy (ret, db.data, db.len);
1121  ret[db.len] = 0;
1122  data_string_forget (&db, MDL);
1123  }
1124  else
1125  ret = NULL;
1126 
1127  return (ret);
1128 }
1129 
1130 
1131 static int
1132 _do_lookup_dhcp_int_option (struct option_state *options, int option_name)
1133 {
1134  struct option_cache *oc;
1135  struct data_string db;
1136  int ret;
1137 
1138  memset (&db, 0, sizeof (db));
1139  oc = lookup_option (&server_universe, options, option_name);
1140  if (oc &&
1141  evaluate_option_cache (&db, (struct packet*) NULL,
1142  (struct lease *) NULL,
1143  (struct client_state *) NULL, options,
1144  (struct option_state *) NULL,
1145  &global_scope, oc, MDL) &&
1146  db.data != NULL && *db.data != '\0')
1147  {
1148  ret = strtol ((const char *) db.data, NULL, 10);
1149  data_string_forget (&db, MDL);
1150  }
1151  else
1152  ret = 0;
1153 
1154  return (ret);
1155 }
1156 
1157 
1158 static int
1159 _do_lookup_dhcp_enum_option (struct option_state *options, int option_name)
1160 {
1161  struct option_cache *oc;
1162  struct data_string db;
1163  int ret = -1;
1164 
1165  memset (&db, 0, sizeof (db));
1166  oc = lookup_option (&server_universe, options, option_name);
1167  if (oc &&
1168  evaluate_option_cache (&db, (struct packet*) NULL,
1169  (struct lease *) NULL,
1170  (struct client_state *) NULL, options,
1171  (struct option_state *) NULL,
1172  &global_scope, oc, MDL) &&
1173  db.data != NULL && *db.data != '\0')
1174  {
1175  if (db.len == 1)
1176  ret = db.data [0];
1177  else
1178  log_fatal ("invalid option name %d", option_name);
1179 
1180  data_string_forget (&db, MDL);
1181  }
1182  else
1183  ret = 0;
1184 
1185  return (ret);
1186 }
1187 
1188 int
1189 ldap_rebind_cb (LDAP *ld, LDAP_CONST char *url, ber_tag_t request, ber_int_t msgid, void *parms)
1190 {
1191  int ret;
1192  LDAPURLDesc *ldapurl = NULL;
1193  char *who = NULL;
1194  struct berval creds;
1195 
1196  log_info("LDAP rebind to '%s'", url);
1197  if ((ret = ldap_url_parse(url, &ldapurl)) != LDAP_SUCCESS)
1198  {
1199  log_error ("Error: Can not parse ldap rebind url '%s': %s",
1200  url, ldap_err2string(ret));
1201  return ret;
1202  }
1203 
1204 
1205 #if defined (LDAP_USE_SSL)
1206  if (strcasecmp(ldapurl->lud_scheme, "ldaps") == 0)
1207  {
1208  int opt = LDAP_OPT_X_TLS_HARD;
1209  if ((ret = ldap_set_option (ld, LDAP_OPT_X_TLS, &opt)) != LDAP_SUCCESS)
1210  {
1211  log_error ("Error: Cannot init LDAPS session to %s:%d: %s",
1212  ldapurl->lud_host, ldapurl->lud_port, ldap_err2string (ret));
1213  ldap_free_urldesc(ldapurl);
1214  return ret;
1215  }
1216  else
1217  {
1218  log_info ("LDAPS session successfully enabled to %s", ldap_server);
1219  }
1220  }
1221  else
1222  if (strcasecmp(ldapurl->lud_scheme, "ldap") == 0 &&
1223  ldap_use_ssl != LDAP_SSL_OFF)
1224  {
1225  if ((ret = ldap_start_tls_s (ld, NULL, NULL)) != LDAP_SUCCESS)
1226  {
1227  log_error ("Error: Cannot start TLS session to %s:%d: %s",
1228  ldapurl->lud_host, ldapurl->lud_port, ldap_err2string (ret));
1229  ldap_free_urldesc(ldapurl);
1230  return ret;
1231  }
1232  else
1233  {
1234  log_info ("TLS session successfully started to %s:%d",
1235  ldapurl->lud_host, ldapurl->lud_port);
1236  }
1237  }
1238 #endif
1239 
1240 #if defined(LDAP_USE_GSSAPI)
1241  if (ldap_gssapi_principal != NULL) {
1242  krb5_get_tgt(ldap_gssapi_principal, ldap_gssapi_keytab);
1243  if ((ret = ldap_sasl_interactive_bind_s(ld, NULL, ldap_sasl_inst->sasl_mech,
1244  NULL, NULL, LDAP_SASL_AUTOMATIC,
1245  _ldap_sasl_interact, ldap_sasl_inst)
1246  ) != LDAP_SUCCESS)
1247  {
1248  log_error ("Error: Cannot SASL bind to ldap server %s:%d: %s",
1249  ldap_server, ldap_port, ldap_err2string (ret));
1250  char *msg=NULL;
1251  ldap_get_option( ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, (void*)&msg);
1252  log_error ("\tAdditional info: %s", msg);
1253  ldap_memfree(msg);
1254  ldap_stop();
1255  }
1256 
1257  ldap_free_urldesc(ldapurl);
1258  return ret;
1259  }
1260 #endif
1261 
1262  if (ldap_username != NULL && *ldap_username != '\0' && ldap_password != NULL)
1263  {
1264  who = ldap_username;
1265  creds.bv_val = strdup(ldap_password);
1266  if (creds.bv_val == NULL)
1267  log_fatal ("Error: Unable to allocate memory to duplicate ldap_password");
1268 
1269  creds.bv_len = strlen(ldap_password);
1270 
1271  if ((ret = ldap_sasl_bind_s (ld, who, LDAP_SASL_SIMPLE, &creds,
1272  NULL, NULL, NULL)) != LDAP_SUCCESS)
1273  {
1274  log_error ("Error: Cannot login into ldap server %s:%d: %s",
1275  ldapurl->lud_host, ldapurl->lud_port, ldap_err2string (ret));
1276  }
1277 
1278  if (creds.bv_val)
1279  free(creds.bv_val);
1280  }
1281 
1282  ldap_free_urldesc(ldapurl);
1283  return ret;
1284 }
1285 
1286 static int
1287 _do_ldap_retry(int ret, const char *server, int port)
1288 {
1289  static int inform = 1;
1290 
1291  if (ldap_enable_retry > 0 && ret == LDAP_SERVER_DOWN && ldap_init_retry > 0)
1292  {
1293  if (inform || (ldap_init_retry % 10) == 0)
1294  {
1295  inform = 0;
1296  log_info ("Can't contact LDAP server %s:%d: retrying for %d sec",
1297  server, port, ldap_init_retry);
1298  }
1299  sleep(1);
1300  return ldap_init_retry--;
1301  }
1302  return 0;
1303 }
1304 
1305 static struct berval *
1306 _do_ldap_str2esc_filter_bv(const char *str, ber_len_t len, struct berval *bv_o)
1307 {
1308  struct berval bv_i;
1309 
1310  if (!str || !bv_o || (ber_str2bv(str, len, 0, &bv_i) == NULL) ||
1311  (ldap_bv2escaped_filter_value(&bv_i, bv_o) != 0))
1312  return NULL;
1313  return bv_o;
1314 }
1315 
1316 static void
1317 ldap_start (void)
1318 {
1319  struct option_state *options;
1320  int ret, version;
1321  char *uri = NULL;
1322  struct berval creds;
1323 #if defined(LDAP_USE_GSSAPI)
1324  char *gssapi_realm = NULL;
1325  char *gssapi_user = NULL;
1326  char *running = NULL;
1327  const char *gssapi_delim = "@";
1328 #endif
1329 
1330  if (ld != NULL)
1331  return;
1332 
1333  if (ldap_server == NULL)
1334  {
1335  options = NULL;
1336  option_state_allocate (&options, MDL);
1337 
1338  execute_statements_in_scope (NULL, NULL, NULL, NULL, NULL,
1339  options, &global_scope, root_group,
1340  NULL, NULL);
1341 
1342  ldap_server = _do_lookup_dhcp_string_option (options, SV_LDAP_SERVER);
1343  ldap_dhcp_server_cn = _do_lookup_dhcp_string_option (options,
1344  SV_LDAP_DHCP_SERVER_CN);
1345  ldap_port = _do_lookup_dhcp_int_option (options, SV_LDAP_PORT);
1346  ldap_base_dn = _do_lookup_dhcp_string_option (options, SV_LDAP_BASE_DN);
1347  ldap_method = _do_lookup_dhcp_enum_option (options, SV_LDAP_METHOD);
1348  ldap_debug_file = _do_lookup_dhcp_string_option (options,
1349  SV_LDAP_DEBUG_FILE);
1350  ldap_referrals = _do_lookup_dhcp_enum_option (options, SV_LDAP_REFERRALS);
1351  ldap_init_retry = _do_lookup_dhcp_int_option (options, SV_LDAP_INIT_RETRY);
1352 
1353 #if defined (LDAP_USE_SSL)
1354  ldap_use_ssl = _do_lookup_dhcp_enum_option (options, SV_LDAP_SSL);
1355  if( ldap_use_ssl != LDAP_SSL_OFF)
1356  {
1357  ldap_tls_reqcert = _do_lookup_dhcp_enum_option (options, SV_LDAP_TLS_REQCERT);
1358  ldap_tls_ca_file = _do_lookup_dhcp_string_option (options, SV_LDAP_TLS_CA_FILE);
1359  ldap_tls_ca_dir = _do_lookup_dhcp_string_option (options, SV_LDAP_TLS_CA_DIR);
1360  ldap_tls_cert = _do_lookup_dhcp_string_option (options, SV_LDAP_TLS_CERT);
1361  ldap_tls_key = _do_lookup_dhcp_string_option (options, SV_LDAP_TLS_KEY);
1362  ldap_tls_crlcheck = _do_lookup_dhcp_enum_option (options, SV_LDAP_TLS_CRLCHECK);
1363  ldap_tls_ciphers = _do_lookup_dhcp_string_option (options, SV_LDAP_TLS_CIPHERS);
1364  ldap_tls_randfile = _do_lookup_dhcp_string_option (options, SV_LDAP_TLS_RANDFILE);
1365  }
1366 #endif
1367 
1368 #if defined (LDAP_USE_GSSAPI)
1369  ldap_gssapi_principal = _do_lookup_dhcp_string_option (options,
1370  SV_LDAP_GSSAPI_PRINCIPAL);
1371 
1372  if (ldap_gssapi_principal == NULL) {
1373  log_error("ldap_gssapi_principal is not set,"
1374  "GSSAPI Authentication for LDAP will not be used");
1375  } else {
1376  ldap_gssapi_keytab = _do_lookup_dhcp_string_option (options,
1377  SV_LDAP_GSSAPI_KEYTAB);
1378  if (ldap_gssapi_keytab == NULL) {
1379  log_fatal("ldap_gssapi_keytab must be specified");
1380  }
1381 
1382  running = strdup(ldap_gssapi_principal);
1383  if (running == NULL)
1384  log_fatal("Could not allocate memory to duplicate gssapi principal");
1385 
1386  gssapi_user = strtok(running, gssapi_delim);
1387  if (!gssapi_user || strlen(gssapi_user) == 0) {
1388  log_fatal ("GSSAPI principal must specify user: user@realm");
1389  }
1390 
1391  gssapi_realm = strtok(NULL, gssapi_delim);
1392  if (!gssapi_realm || strlen(gssapi_realm) == 0) {
1393  log_fatal ("GSSAPI principal must specify realm: user@realm");
1394  }
1395 
1396  ldap_sasl_inst = malloc(sizeof(struct ldap_sasl_instance));
1397  if (ldap_sasl_inst == NULL)
1398  log_fatal("Could not allocate memory for sasl instance! Can not run!");
1399 
1400  ldap_sasl_inst->sasl_mech = ber_strdup("GSSAPI");
1401  if (ldap_sasl_inst->sasl_mech == NULL)
1402  log_fatal("Could not allocate memory to duplicate gssapi mechanism");
1403 
1404  ldap_sasl_inst->sasl_realm = ber_strdup(gssapi_realm);
1405  if (ldap_sasl_inst->sasl_realm == NULL)
1406  log_fatal("Could not allocate memory to duplicate gssapi realm");
1407 
1408  ldap_sasl_inst->sasl_authz_id = ber_strdup(gssapi_user);
1409  if (ldap_sasl_inst->sasl_authz_id == NULL)
1410  log_fatal("Could not allocate memory to duplicate gssapi user");
1411 
1412  ldap_sasl_inst->sasl_authc_id = NULL;
1413  ldap_sasl_inst->sasl_password = NULL; //"" before
1414  free(running);
1415  }
1416 #endif
1417 
1418 #if defined (LDAP_CASA_AUTH)
1419  if (!load_uname_pwd_from_miCASA(&ldap_username,&ldap_password))
1420  {
1421 #if defined (DEBUG_LDAP)
1422  log_info ("Authentication credential taken from file");
1423 #endif
1424 #endif
1425 
1426  ldap_username = _do_lookup_dhcp_string_option (options, SV_LDAP_USERNAME);
1427  ldap_password = _do_lookup_dhcp_string_option (options, SV_LDAP_PASSWORD);
1428 
1429 #if defined (LDAP_CASA_AUTH)
1430  }
1431 #endif
1432 
1433  option_state_dereference (&options, MDL);
1434  }
1435 
1436  if (ldap_server == NULL || ldap_base_dn == NULL)
1437  {
1438  log_info ("Not searching LDAP since ldap-server, ldap-port and ldap-base-dn were not specified in the config file");
1439  ldap_method = LDAP_METHOD_STATIC;
1440  return;
1441  }
1442 
1443  if (ldap_debug_file != NULL && ldap_debug_fd == -1)
1444  {
1445  if ((ldap_debug_fd = open (ldap_debug_file, O_CREAT | O_TRUNC | O_WRONLY | O_CLOEXEC,
1446  S_IRUSR | S_IWUSR)) < 0)
1447  log_error ("Error opening debug LDAP log file %s: %s", ldap_debug_file,
1448  strerror (errno));
1449  }
1450 
1451 #if defined (DEBUG_LDAP)
1452  log_info ("Connecting to LDAP server %s:%d", ldap_server, ldap_port);
1453 #endif
1454 
1455 #if defined (LDAP_USE_SSL)
1456  if (ldap_use_ssl == -1)
1457  {
1458  /*
1459  ** There was no "ldap-ssl" option in dhcpd.conf (also not "off").
1460  ** Let's try, if we can use an anonymous TLS session without to
1461  ** verify the server certificate -- if not continue without TLS.
1462  */
1463  int opt = LDAP_OPT_X_TLS_ALLOW;
1464  if ((ret = ldap_set_option (NULL, LDAP_OPT_X_TLS_REQUIRE_CERT,
1465  &opt)) != LDAP_SUCCESS)
1466  {
1467  log_error ("Warning: Cannot set LDAP TLS require cert option to 'allow': %s",
1468  ldap_err2string (ret));
1469  }
1470  }
1471 
1472  if (ldap_use_ssl != LDAP_SSL_OFF)
1473  {
1474  if (ldap_tls_reqcert != -1)
1475  {
1476  if ((ret = ldap_set_option (NULL, LDAP_OPT_X_TLS_REQUIRE_CERT,
1477  &ldap_tls_reqcert)) != LDAP_SUCCESS)
1478  {
1479  log_error ("Cannot set LDAP TLS require cert option: %s",
1480  ldap_err2string (ret));
1481  }
1482  }
1483 
1484  if( ldap_tls_ca_file != NULL)
1485  {
1486  if ((ret = ldap_set_option (NULL, LDAP_OPT_X_TLS_CACERTFILE,
1487  ldap_tls_ca_file)) != LDAP_SUCCESS)
1488  {
1489  log_error ("Cannot set LDAP TLS CA certificate file %s: %s",
1490  ldap_tls_ca_file, ldap_err2string (ret));
1491  }
1492  }
1493  if( ldap_tls_ca_dir != NULL)
1494  {
1495  if ((ret = ldap_set_option (NULL, LDAP_OPT_X_TLS_CACERTDIR,
1496  ldap_tls_ca_dir)) != LDAP_SUCCESS)
1497  {
1498  log_error ("Cannot set LDAP TLS CA certificate dir %s: %s",
1499  ldap_tls_ca_dir, ldap_err2string (ret));
1500  }
1501  }
1502  if( ldap_tls_cert != NULL)
1503  {
1504  if ((ret = ldap_set_option (NULL, LDAP_OPT_X_TLS_CERTFILE,
1505  ldap_tls_cert)) != LDAP_SUCCESS)
1506  {
1507  log_error ("Cannot set LDAP TLS client certificate file %s: %s",
1508  ldap_tls_cert, ldap_err2string (ret));
1509  }
1510  }
1511  if( ldap_tls_key != NULL)
1512  {
1513  if ((ret = ldap_set_option (NULL, LDAP_OPT_X_TLS_KEYFILE,
1514  ldap_tls_key)) != LDAP_SUCCESS)
1515  {
1516  log_error ("Cannot set LDAP TLS certificate key file %s: %s",
1517  ldap_tls_key, ldap_err2string (ret));
1518  }
1519  }
1520  if( ldap_tls_crlcheck != -1)
1521  {
1522  int opt = ldap_tls_crlcheck;
1523  if ((ret = ldap_set_option (NULL, LDAP_OPT_X_TLS_CRLCHECK,
1524  &opt)) != LDAP_SUCCESS)
1525  {
1526  log_error ("Cannot set LDAP TLS crl check option: %s",
1527  ldap_err2string (ret));
1528  }
1529  }
1530  if( ldap_tls_ciphers != NULL)
1531  {
1532  if ((ret = ldap_set_option (NULL, LDAP_OPT_X_TLS_CIPHER_SUITE,
1533  ldap_tls_ciphers)) != LDAP_SUCCESS)
1534  {
1535  log_error ("Cannot set LDAP TLS cipher suite %s: %s",
1536  ldap_tls_ciphers, ldap_err2string (ret));
1537  }
1538  }
1539  if( ldap_tls_randfile != NULL)
1540  {
1541  if ((ret = ldap_set_option (NULL, LDAP_OPT_X_TLS_RANDOM_FILE,
1542  ldap_tls_randfile)) != LDAP_SUCCESS)
1543  {
1544  log_error ("Cannot set LDAP TLS random file %s: %s",
1545  ldap_tls_randfile, ldap_err2string (ret));
1546  }
1547  }
1548  }
1549 #endif
1550 
1551  /* enough for 'ldap://+ + hostname + ':' + port number */
1552  uri = malloc(strlen(ldap_server) + 16);
1553  if (uri == NULL)
1554  {
1555  log_error ("Cannot build ldap init URI %s:%d", ldap_server, ldap_port);
1556  return;
1557  }
1558 
1559  sprintf(uri, "ldap://%s:%d", ldap_server, ldap_port);
1560  ldap_initialize(&ld, uri);
1561 
1562  if (ld == NULL)
1563  {
1564  log_error ("Cannot init ldap session to %s:%d", ldap_server, ldap_port);
1565  return;
1566  }
1567 
1568  free(uri);
1569 
1570  version = LDAP_VERSION3;
1571  if ((ret = ldap_set_option (ld, LDAP_OPT_PROTOCOL_VERSION, &version)) != LDAP_OPT_SUCCESS)
1572  {
1573  log_error ("Cannot set LDAP version to %d: %s", version,
1574  ldap_err2string (ret));
1575  }
1576 
1577  if (ldap_referrals != -1)
1578  {
1579  if ((ret = ldap_set_option (ld, LDAP_OPT_REFERRALS, ldap_referrals ?
1580  LDAP_OPT_ON : LDAP_OPT_OFF)) != LDAP_OPT_SUCCESS)
1581  {
1582  log_error ("Cannot %s LDAP referrals option: %s",
1583  (ldap_referrals ? "enable" : "disable"),
1584  ldap_err2string (ret));
1585  }
1586  }
1587 
1588  if ((ret = ldap_set_rebind_proc(ld, ldap_rebind_cb, NULL)) != LDAP_SUCCESS)
1589  {
1590  log_error ("Warning: Cannot set ldap rebind procedure: %s",
1591  ldap_err2string (ret));
1592  }
1593 
1594 #if defined (LDAP_USE_SSL)
1595  if (ldap_use_ssl == LDAP_SSL_LDAPS ||
1596  (ldap_use_ssl == LDAP_SSL_ON && ldap_port == LDAPS_PORT))
1597  {
1598  int opt = LDAP_OPT_X_TLS_HARD;
1599  if ((ret = ldap_set_option (ld, LDAP_OPT_X_TLS, &opt)) != LDAP_SUCCESS)
1600  {
1601  log_error ("Error: Cannot init LDAPS session to %s:%d: %s",
1602  ldap_server, ldap_port, ldap_err2string (ret));
1603  ldap_stop();
1604  return;
1605  }
1606  else
1607  {
1608  log_info ("LDAPS session successfully enabled to %s:%d",
1609  ldap_server, ldap_port);
1610  }
1611  }
1612  else if (ldap_use_ssl != LDAP_SSL_OFF)
1613  {
1614  do
1615  {
1616  ret = ldap_start_tls_s (ld, NULL, NULL);
1617  }
1618  while(_do_ldap_retry(ret, ldap_server, ldap_port) > 0);
1619 
1620  if (ret != LDAP_SUCCESS)
1621  {
1622  log_error ("Error: Cannot start TLS session to %s:%d: %s",
1623  ldap_server, ldap_port, ldap_err2string (ret));
1624  ldap_stop();
1625  return;
1626  }
1627  else
1628  {
1629  log_info ("TLS session successfully started to %s:%d",
1630  ldap_server, ldap_port);
1631  }
1632  }
1633 #endif
1634 
1635 #if defined(LDAP_USE_GSSAPI)
1636  if (ldap_gssapi_principal != NULL) {
1637  krb5_get_tgt(ldap_gssapi_principal, ldap_gssapi_keytab);
1638  if ((ret = ldap_sasl_interactive_bind_s(ld, NULL, ldap_sasl_inst->sasl_mech,
1639  NULL, NULL, LDAP_SASL_AUTOMATIC,
1640  _ldap_sasl_interact, ldap_sasl_inst)
1641  ) != LDAP_SUCCESS)
1642  {
1643  log_error ("Error: Cannot SASL bind to ldap server %s:%d: %s",
1644  ldap_server, ldap_port, ldap_err2string (ret));
1645  char *msg=NULL;
1646  ldap_get_option( ld, LDAP_OPT_DIAGNOSTIC_MESSAGE, (void*)&msg);
1647  log_error ("\tAdditional info: %s", msg);
1648  ldap_memfree(msg);
1649  ldap_stop();
1650  return;
1651  }
1652  } else
1653 #endif
1654 
1655  if (ldap_username != NULL && *ldap_username != '\0' && ldap_password != NULL)
1656  {
1657  creds.bv_val = strdup(ldap_password);
1658  if (creds.bv_val == NULL)
1659  log_fatal ("Error: Unable to allocate memory to duplicate ldap_password");
1660 
1661  creds.bv_len = strlen(ldap_password);
1662 
1663  do
1664  {
1665  ret = ldap_sasl_bind_s (ld, ldap_username, LDAP_SASL_SIMPLE,
1666  &creds, NULL, NULL, NULL);
1667  }
1668  while(_do_ldap_retry(ret, ldap_server, ldap_port) > 0);
1669  free(creds.bv_val);
1670 
1671  if (ret != LDAP_SUCCESS)
1672  {
1673  log_error ("Error: Cannot login into ldap server %s:%d: %s",
1674  ldap_server, ldap_port, ldap_err2string (ret));
1675  ldap_stop();
1676  return;
1677  }
1678  }
1679 
1680 #if defined (DEBUG_LDAP)
1681  log_info ("Successfully logged into LDAP server %s", ldap_server);
1682 #endif
1683 }
1684 
1685 
1686 static void
1687 parse_external_dns (LDAPMessage * ent)
1688 {
1689  char *search[] = {"dhcpOptionsDN", "dhcpSharedNetworkDN", "dhcpSubnetDN",
1690  "dhcpGroupDN", "dhcpHostDN", "dhcpClassesDN",
1691  "dhcpPoolDN", "dhcpZoneDN", "dhcpFailOverPeerDN", NULL};
1692 
1693  /* TODO: dhcpKeyDN can't be added. It is referenced in dhcpDnsZone to
1694  retrive the key name (cn). Adding keyDN will reflect adding a key
1695  declaration inside the zone configuration.
1696 
1697  dhcpSubClassesDN cant be added. It is also similar to the above.
1698  Needs schema change.
1699  */
1700  LDAPMessage * newres, * newent;
1701  struct berval **tempbv;
1702  int i, j, ret;
1703 #if defined (DEBUG_LDAP)
1704  char *dn;
1705 
1706  dn = ldap_get_dn (ld, ent);
1707  if (dn != NULL)
1708  {
1709  log_info ("Parsing external DNs for '%s'", dn);
1710  ldap_memfree (dn);
1711  }
1712 #endif
1713 
1714  if (ld == NULL)
1715  ldap_start ();
1716  if (ld == NULL)
1717  return;
1718 
1719  for (i=0; search[i] != NULL; i++)
1720  {
1721  if ((tempbv = ldap_get_values_len (ld, ent, search[i])) == NULL)
1722  continue;
1723 
1724  for (j=0; tempbv[j] != NULL; j++)
1725  {
1726  if (*tempbv[j]->bv_val == '\0')
1727  continue;
1728 
1729  if ((ret = ldap_search_ext_s(ld, tempbv[j]->bv_val, LDAP_SCOPE_BASE,
1730  "objectClass=*", NULL, 0, NULL,
1731  NULL, NULL, 0, &newres)) != LDAP_SUCCESS)
1732  {
1733  ldap_value_free_len (tempbv);
1734  ldap_stop();
1735  return;
1736  }
1737 
1738 #if defined (DEBUG_LDAP)
1739  log_info ("Adding contents of subtree '%s' to config stack from '%s' reference", tempbv[j]->bv_val, search[i]);
1740 #endif
1741  for (newent = ldap_first_entry (ld, newres);
1742  newent != NULL;
1743  newent = ldap_next_entry (ld, newent))
1744  {
1745 #if defined (DEBUG_LDAP)
1746  dn = ldap_get_dn (ld, newent);
1747  if (dn != NULL)
1748  {
1749  log_info ("Adding LDAP result set starting with '%s' to config stack", dn);
1750  ldap_memfree (dn);
1751  }
1752 #endif
1753 
1754  add_to_config_stack (newres, newent);
1755  /* don't free newres here */
1756  }
1757  }
1758 
1759  ldap_value_free_len (tempbv);
1760  }
1761 }
1762 
1763 
1764 static void
1765 free_stack_entry (struct ldap_config_stack *item)
1766 {
1767  struct ldap_config_stack *look_ahead_pointer = item;
1768  int may_free_msg = 1;
1769 
1770  while (look_ahead_pointer->next != NULL)
1771  {
1772  look_ahead_pointer = look_ahead_pointer->next;
1773  if (look_ahead_pointer->res == item->res)
1774  {
1775  may_free_msg = 0;
1776  break;
1777  }
1778  }
1779 
1780  if (may_free_msg)
1781  ldap_msgfree (item->res);
1782 
1783  dfree (item, MDL);
1784 }
1785 
1786 
1787 static void
1788 next_ldap_entry (struct parse *cfile)
1789 {
1790  struct ldap_config_stack *temp_stack;
1791 
1792  if (ldap_stack != NULL && ldap_stack->close_brace)
1793  {
1794  x_parser_strcat (cfile, "}\n");
1795  ldap_stack->close_brace = 0;
1796  }
1797 
1798  while (ldap_stack != NULL &&
1799  (ldap_stack->ldent == NULL || ( ldap_stack->processed &&
1800  (ldap_stack->ldent = ldap_next_entry (ld, ldap_stack->ldent)) == NULL)))
1801  {
1802  if (ldap_stack->close_brace)
1803  {
1804  x_parser_strcat (cfile, "}\n");
1805  ldap_stack->close_brace = 0;
1806  }
1807 
1808  temp_stack = ldap_stack;
1809  ldap_stack = ldap_stack->next;
1810  free_stack_entry (temp_stack);
1811  }
1812 
1813  if (ldap_stack != NULL && ldap_stack->close_brace)
1814  {
1815  x_parser_strcat (cfile, "}\n");
1816  ldap_stack->close_brace = 0;
1817  }
1818 }
1819 
1820 
1821 static char
1822 check_statement_end (const char *statement)
1823 {
1824  char *ptr;
1825 
1826  if (statement == NULL || *statement == '\0')
1827  return ('\0');
1828 
1829  /*
1830  ** check if it ends with "}", e.g.:
1831  ** "zone my.domain. { ... }"
1832  ** optionally followed by spaces
1833  */
1834  ptr = strrchr (statement, '}');
1835  if (ptr != NULL)
1836  {
1837  /* skip following white-spaces */
1838  for (++ptr; isspace ((int)*ptr); ptr++);
1839 
1840  /* check if we reached the end */
1841  if (*ptr == '\0')
1842  return ('}'); /* yes, block end */
1843  else
1844  return (*ptr);
1845  }
1846 
1847  /*
1848  ** this should not happen, but...
1849  ** check if it ends with ";", e.g.:
1850  ** "authoritative;"
1851  ** optionally followed by spaces
1852  */
1853  ptr = strrchr (statement, ';');
1854  if (ptr != NULL)
1855  {
1856  /* skip following white-spaces */
1857  for (++ptr; isspace ((int)*ptr); ptr++);
1858 
1859  /* check if we reached the end */
1860  if (*ptr == '\0')
1861  return (';'); /* ends with a ; */
1862  else
1863  return (*ptr);
1864  }
1865 
1866  return ('\0');
1867 }
1868 
1869 
1870 static isc_result_t
1871 ldap_parse_entry_options (LDAPMessage *ent, struct parse *cfile,
1872  int *lease_limit)
1873 {
1874  struct berval **tempbv;
1875  int i;
1876 
1877  if (ent == NULL || cfile == NULL)
1878  return (ISC_R_FAILURE);
1879 
1880  if ((tempbv = ldap_get_values_len (ld, ent, "dhcpStatements")) != NULL)
1881  {
1882  for (i=0; tempbv[i] != NULL; i++)
1883  {
1884  if (lease_limit != NULL &&
1885  strncasecmp ("lease limit ", tempbv[i]->bv_val, 12) == 0)
1886  {
1887  *lease_limit = (int) strtol ((tempbv[i]->bv_val) + 12, NULL, 10);
1888  continue;
1889  }
1890 
1891  x_parser_strcat (cfile, tempbv[i]->bv_val);
1892 
1893  switch((int) check_statement_end (tempbv[i]->bv_val))
1894  {
1895  case '}':
1896  case ';':
1897  x_parser_strcat (cfile, "\n");
1898  break;
1899  default:
1900  x_parser_strcat (cfile, ";\n");
1901  break;
1902  }
1903  }
1904  ldap_value_free_len (tempbv);
1905  }
1906 
1907  if ((tempbv = ldap_get_values_len (ld, ent, "dhcpOption")) != NULL)
1908  {
1909  for (i=0; tempbv[i] != NULL; i++)
1910  {
1911  x_parser_strcat (cfile, "option ");
1912  x_parser_strcat (cfile, tempbv[i]->bv_val);
1913  switch ((int) check_statement_end (tempbv[i]->bv_val))
1914  {
1915  case ';':
1916  x_parser_strcat (cfile, "\n");
1917  break;
1918  default:
1919  x_parser_strcat (cfile, ";\n");
1920  break;
1921  }
1922  }
1923  ldap_value_free_len (tempbv);
1924  }
1925 
1926  return (ISC_R_SUCCESS);
1927 }
1928 
1929 
1930 static void
1931 ldap_generate_config_string (struct parse *cfile)
1932 {
1933  struct berval **objectClass;
1934  char *dn;
1935  struct ldap_config_stack *entry;
1936  LDAPMessage * ent, * res, *entfirst, *resfirst;
1937  int i, ignore, found;
1938  int ret, parsedn = 1;
1939  size_t len = cfile->buflen;
1940 
1941  if (ld == NULL)
1942  ldap_start ();
1943  if (ld == NULL)
1944  return;
1945 
1946  entry = ldap_stack;
1947  if ((objectClass = ldap_get_values_len (ld, entry->ldent,
1948  "objectClass")) == NULL)
1949  return;
1950 
1951  entry->processed = 1;
1952  ignore = 0;
1953  found = 1;
1954  for (i=0; objectClass[i] != NULL; i++)
1955  {
1956  if (strcasecmp (objectClass[i]->bv_val, "dhcpSharedNetwork") == 0)
1957  ldap_parse_shared_network (entry, cfile);
1958  else if (strcasecmp (objectClass[i]->bv_val, "dhcpClass") == 0)
1959  ldap_parse_class (entry, cfile);
1960  else if (strcasecmp (objectClass[i]->bv_val, "dhcpSubnet") == 0)
1961  ldap_parse_subnet (entry, cfile);
1962  else if (strcasecmp (objectClass[i]->bv_val, "dhcpSubnet6") == 0)
1963  ldap_parse_subnet6 (entry, cfile);
1964  else if (strcasecmp (objectClass[i]->bv_val, "dhcpPool") == 0)
1965  ldap_parse_pool (entry, cfile);
1966  else if (strcasecmp (objectClass[i]->bv_val, "dhcpPool6") == 0)
1967  ldap_parse_pool6 (entry, cfile);
1968  else if (strcasecmp (objectClass[i]->bv_val, "dhcpGroup") == 0)
1969  ldap_parse_group (entry, cfile);
1970  else if (strcasecmp (objectClass[i]->bv_val, "dhcpTSigKey") == 0)
1971  ldap_parse_key (entry, cfile);
1972  else if (strcasecmp (objectClass[i]->bv_val, "dhcpDnsZone") == 0)
1973  ldap_parse_zone (entry, cfile);
1974 #if defined(HAVE_IFADDRS_H)
1975  else if (strcasecmp (objectClass[i]->bv_val, "dhcpFailOverPeer") == 0)
1976  ldap_parse_failover (entry, cfile);
1977 #endif
1978  else if (strcasecmp (objectClass[i]->bv_val, "dhcpHost") == 0)
1979  {
1980  if (ldap_method == LDAP_METHOD_STATIC)
1981  ldap_parse_host (entry, cfile);
1982  else
1983  {
1984  ignore = 1;
1985  break;
1986  }
1987  }
1988  else if (strcasecmp (objectClass[i]->bv_val, "dhcpSubClass") == 0)
1989  {
1990  if (ldap_method == LDAP_METHOD_STATIC)
1991  ldap_parse_subclass (entry, cfile);
1992  else
1993  {
1994  ignore = 1;
1995  break;
1996  }
1997  }
1998  else
1999  found = 0;
2000 
2001  if (found && x_parser_length(cfile) <= len)
2002  {
2003  ignore = 1;
2004  break;
2005  }
2006  }
2007 
2008  ldap_value_free_len (objectClass);
2009 
2010  if (ignore)
2011  {
2012  next_ldap_entry (cfile);
2013  return;
2014  }
2015 
2016  ldap_parse_entry_options(entry->ldent, cfile, NULL);
2017 
2018  dn = ldap_get_dn (ld, entry->ldent);
2019  if (dn == NULL)
2020  {
2021  ldap_stop();
2022  return;
2023  }
2024 #if defined(DEBUG_LDAP)
2025  log_info ("Found LDAP entry '%s'", dn);
2026 #endif
2027 
2028  if ((ret = ldap_search_ext_s (ld, dn, LDAP_SCOPE_ONELEVEL,
2029  "(!(|(|(objectClass=dhcpTSigKey)(objectClass=dhcpClass)) (objectClass=dhcpFailOverPeer)))",
2030  NULL, 0, NULL, NULL,
2031  NULL, 0, &res)) != LDAP_SUCCESS)
2032  {
2033  ldap_memfree (dn);
2034 
2035  ldap_stop();
2036  return;
2037  }
2038 
2039  if ((ret = ldap_search_ext_s (ld, dn, LDAP_SCOPE_ONELEVEL,
2040  "(|(|(objectClass=dhcpTSigKey)(objectClass=dhcpClass)) (objectClass=dhcpFailOverPeer))",
2041  NULL, 0, NULL, NULL,
2042  NULL, 0, &resfirst)) != LDAP_SUCCESS)
2043  {
2044  ldap_memfree (dn);
2045  ldap_msgfree (res);
2046 
2047  ldap_stop();
2048  return;
2049  }
2050 
2051  ldap_memfree (dn);
2052 
2053  ent = ldap_first_entry(ld, res);
2054  entfirst = ldap_first_entry(ld, resfirst);
2055 
2056  if (ent == NULL && entfirst == NULL)
2057  {
2058  parse_external_dns (entry->ldent);
2059  next_ldap_entry (cfile);
2060  }
2061 
2062  if (ent != NULL)
2063  {
2064  add_to_config_stack (res, ent);
2065  parse_external_dns (entry->ldent);
2066  parsedn = 0;
2067  }
2068  else
2069  ldap_msgfree (res);
2070 
2071  if (entfirst != NULL)
2072  {
2073  add_to_config_stack (resfirst, entfirst);
2074  if(parsedn)
2075  parse_external_dns (entry->ldent);
2076 
2077  }
2078  else
2079  ldap_msgfree (resfirst);
2080 }
2081 
2082 
2083 static void
2084 ldap_close_debug_fd()
2085 {
2086  if (ldap_debug_fd != -1)
2087  {
2088  close (ldap_debug_fd);
2089  ldap_debug_fd = -1;
2090  }
2091 }
2092 
2093 
2094 static void
2095 ldap_write_debug (const void *buff, size_t size)
2096 {
2097  if (ldap_debug_fd != -1)
2098  {
2099  if (write (ldap_debug_fd, buff, size) < 0)
2100  {
2101  log_error ("Error writing to LDAP debug file %s: %s."
2102  " Disabling log file.", ldap_debug_file,
2103  strerror (errno));
2104  ldap_close_debug_fd();
2105  }
2106  }
2107 }
2108 
2109 static int
2110 ldap_read_function (struct parse *cfile)
2111 {
2112  size_t len;
2113 
2114  /* append when in saved state */
2115  if (cfile->saved_state == NULL)
2116  {
2117  cfile->inbuf[0] = '\0';
2118  cfile->bufix = 0;
2119  cfile->buflen = 0;
2120  }
2121  len = cfile->buflen;
2122 
2123  while (ldap_stack != NULL && x_parser_length(cfile) <= len)
2124  ldap_generate_config_string (cfile);
2125 
2126  if (x_parser_length(cfile) <= len && ldap_stack == NULL)
2127  return (EOF);
2128 
2129  if (cfile->buflen > len)
2130  ldap_write_debug (cfile->inbuf + len, cfile->buflen - len);
2131 #if defined (DEBUG_LDAP)
2132  log_info ("Sending config portion '%s'", cfile->inbuf + len);
2133 #endif
2134 
2135  return (cfile->inbuf[cfile->bufix++]);
2136 }
2137 
2138 
2139 static char *
2140 ldap_get_host_name (LDAPMessage * ent)
2141 {
2142  struct berval **name;
2143  char *ret;
2144 
2145  ret = NULL;
2146  if ((name = ldap_get_values_len (ld, ent, "cn")) == NULL || name[0] == NULL)
2147  {
2148  if (name != NULL)
2149  ldap_value_free_len (name);
2150 
2151 #if defined (DEBUG_LDAP)
2152  ret = ldap_get_dn (ld, ent);
2153  if (ret != NULL)
2154  {
2155  log_info ("Cannot get cn attribute for LDAP entry %s", ret);
2156  ldap_memfree(ret);
2157  }
2158 #endif
2159  return (NULL);
2160  }
2161 
2162  ret = dmalloc (strlen (name[0]->bv_val) + 1, MDL);
2163  strcpy (ret, name[0]->bv_val);
2164  ldap_value_free_len (name);
2165 
2166  return (ret);
2167 }
2168 
2169 
2170 isc_result_t
2171 ldap_read_config (void)
2172 {
2173  LDAPMessage * ldres, * hostres, * ent, * hostent;
2174  char hfilter[1024], sfilter[1024], fqdn[257];
2175  char *hostdn;
2176  ldap_dn_node *curr = NULL;
2177  struct parse *cfile;
2178  struct utsname unme;
2179  isc_result_t res;
2180  size_t length;
2181  int ret, cnt;
2182  struct berval **tempbv = NULL;
2183  struct berval bv_o[2];
2184 
2185  cfile = x_parser_init("LDAP");
2186  if (cfile == NULL)
2187  return (ISC_R_NOMEMORY);
2188 
2189  ldap_enable_retry = 1;
2190  if (ld == NULL)
2191  ldap_start ();
2192  ldap_enable_retry = 0;
2193 
2194  if (ld == NULL)
2195  {
2196  x_parser_free(&cfile);
2197  return (ldap_server == NULL ? ISC_R_SUCCESS : ISC_R_FAILURE);
2198  }
2199 
2200  uname (&unme);
2201  if (ldap_dhcp_server_cn != NULL)
2202  {
2203  if (_do_ldap_str2esc_filter_bv(ldap_dhcp_server_cn, 0, &bv_o[0]) == NULL)
2204  {
2205  log_error ("Cannot escape ldap filter value %s: %m", ldap_dhcp_server_cn);
2206  x_parser_free(&cfile);
2207  return (ISC_R_FAILURE);
2208  }
2209 
2210  snprintf (hfilter, sizeof (hfilter),
2211  "(&(objectClass=dhcpServer)(cn=%s))", bv_o[0].bv_val);
2212 
2213  ber_memfree(bv_o[0].bv_val);
2214  }
2215  else
2216  {
2217  if (_do_ldap_str2esc_filter_bv(unme.nodename, 0, &bv_o[0]) == NULL)
2218  {
2219  log_error ("Cannot escape ldap filter value %s: %m", unme.nodename);
2220  x_parser_free(&cfile);
2221  return (ISC_R_FAILURE);
2222  }
2223 
2224  *fqdn ='\0';
2225  if(0 == get_host_entry(fqdn, sizeof(fqdn), NULL, 0))
2226  {
2227  if (_do_ldap_str2esc_filter_bv(fqdn, 0, &bv_o[1]) == NULL)
2228  {
2229  log_error ("Cannot escape ldap filter value %s: %m", fqdn);
2230  ber_memfree(bv_o[0].bv_val);
2231  x_parser_free(&cfile);
2232  return (ISC_R_FAILURE);
2233  }
2234  }
2235 
2236  // If we have fqdn and it isn't the same as nodename, use it in filter
2237  // otherwise just use nodename
2238  if ((*fqdn) && (strcmp(unme.nodename, fqdn))) {
2239  snprintf (hfilter, sizeof (hfilter),
2240  "(&(objectClass=dhcpServer)(|(cn=%s)(cn=%s)))",
2241  bv_o[0].bv_val, bv_o[1].bv_val);
2242 
2243  ber_memfree(bv_o[1].bv_val);
2244  }
2245  else
2246  {
2247  snprintf (hfilter, sizeof (hfilter),
2248  "(&(objectClass=dhcpServer)(cn=%s))",
2249  bv_o[0].bv_val);
2250  }
2251 
2252  ber_memfree(bv_o[0].bv_val);
2253  }
2254 
2255  ldap_enable_retry = 1;
2256  do
2257  {
2258  hostres = NULL;
2259  ret = ldap_search_ext_s (ld, ldap_base_dn, LDAP_SCOPE_SUBTREE,
2260  hfilter, NULL, 0, NULL, NULL, NULL, 0,
2261  &hostres);
2262  }
2263  while(_do_ldap_retry(ret, ldap_server, ldap_port) > 0);
2264  ldap_enable_retry = 0;
2265 
2266  if(ret != LDAP_SUCCESS)
2267  {
2268  log_error ("Cannot find host LDAP entry %s %s",
2269  ((ldap_dhcp_server_cn == NULL)?(unme.nodename):(ldap_dhcp_server_cn)), hfilter);
2270  if(NULL != hostres)
2271  ldap_msgfree (hostres);
2272  ldap_stop();
2273  x_parser_free(&cfile);
2274  return (ISC_R_FAILURE);
2275  }
2276 
2277  if ((hostent = ldap_first_entry (ld, hostres)) == NULL)
2278  {
2279  log_error ("Error: Cannot find LDAP entry matching %s", hfilter);
2280  ldap_msgfree (hostres);
2281  ldap_stop();
2282  x_parser_free(&cfile);
2283  return (ISC_R_FAILURE);
2284  }
2285 
2286  hostdn = ldap_get_dn (ld, hostent);
2287 #if defined(DEBUG_LDAP)
2288  if (hostdn != NULL)
2289  log_info ("Found dhcpServer LDAP entry '%s'", hostdn);
2290 #endif
2291 
2292  if (hostdn == NULL ||
2293  (tempbv = ldap_get_values_len (ld, hostent, "dhcpServiceDN")) == NULL ||
2294  tempbv[0] == NULL)
2295  {
2296  log_error ("Error: No dhcp service is associated with the server %s %s",
2297  (hostdn ? "dn" : "name"), (hostdn ? hostdn :
2298  (ldap_dhcp_server_cn ? ldap_dhcp_server_cn : unme.nodename)));
2299 
2300  if (tempbv != NULL)
2301  ldap_value_free_len (tempbv);
2302 
2303  if (hostdn)
2304  ldap_memfree (hostdn);
2305  ldap_msgfree (hostres);
2306  ldap_stop();
2307  x_parser_free(&cfile);
2308  return (ISC_R_FAILURE);
2309  }
2310 
2311 #if defined(DEBUG_LDAP)
2312  log_info ("LDAP: Parsing dhcpServer options '%s' ...", hostdn);
2313 #endif
2314 
2315  res = ldap_parse_entry_options(hostent, cfile, NULL);
2316  if (res != ISC_R_SUCCESS)
2317  {
2318  ldap_value_free_len (tempbv);
2319  ldap_msgfree (hostres);
2320  ldap_memfree (hostdn);
2321  ldap_stop();
2322  x_parser_free(&cfile);
2323  return res;
2324  }
2325 
2326  if (x_parser_length(cfile) > 0)
2327  {
2328  ldap_write_debug(cfile->inbuf, cfile->buflen);
2329 
2330  res = conf_file_subparse (cfile, root_group, ROOT_GROUP);
2331  if (res != ISC_R_SUCCESS)
2332  {
2333  log_error ("LDAP: cannot parse dhcpServer entry '%s'", hostdn);
2334  ldap_value_free_len (tempbv);
2335  ldap_msgfree (hostres);
2336  ldap_memfree (hostdn);
2337  ldap_stop();
2338  x_parser_free(&cfile);
2339  return res;
2340  }
2341  x_parser_reset(cfile);
2342  }
2343  ldap_msgfree (hostres);
2344 
2345  res = ISC_R_SUCCESS;
2346  for (cnt=0; tempbv[cnt] != NULL; cnt++)
2347  {
2348 
2349  if (_do_ldap_str2esc_filter_bv(hostdn, 0, &bv_o[0]) == NULL)
2350  {
2351  log_error ("Cannot escape ldap filter value %s: %m", hostdn);
2352  res = ISC_R_FAILURE;
2353  break;
2354  }
2355 
2356  snprintf(sfilter, sizeof(sfilter), "(&(objectClass=dhcpService)"
2357  "(|(|(dhcpPrimaryDN=%s)(dhcpSecondaryDN=%s))(dhcpServerDN=%s)))",
2358  bv_o[0].bv_val, bv_o[0].bv_val, bv_o[0].bv_val);
2359 
2360  ber_memfree(bv_o[0].bv_val);
2361 
2362  ldres = NULL;
2363  if ((ret = ldap_search_ext_s (ld, tempbv[cnt]->bv_val, LDAP_SCOPE_BASE,
2364  sfilter, NULL, 0, NULL, NULL, NULL,
2365  0, &ldres)) != LDAP_SUCCESS)
2366  {
2367  log_error ("Error searching for dhcpServiceDN '%s': %s. Please update the LDAP entry '%s'",
2368  tempbv[cnt]->bv_val, ldap_err2string (ret), hostdn);
2369  if(NULL != ldres)
2370  ldap_msgfree(ldres);
2371  res = ISC_R_FAILURE;
2372  break;
2373  }
2374 
2375  if ((ent = ldap_first_entry (ld, ldres)) == NULL)
2376  {
2377  log_error ("Error: Cannot find dhcpService DN '%s' with server reference. Please update the LDAP server entry '%s'",
2378  tempbv[cnt]->bv_val, hostdn);
2379 
2380  ldap_msgfree(ldres);
2381  res = ISC_R_FAILURE;
2382  break;
2383  }
2384 
2385  /*
2386  ** FIXME: how to free the remembered dn's on exit?
2387  ** This should be OK if dmalloc registers the
2388  ** memory it allocated and frees it on exit..
2389  */
2390 
2391  curr = dmalloc (sizeof (*curr), MDL);
2392  if (curr != NULL)
2393  {
2394  length = strlen (tempbv[cnt]->bv_val);
2395  curr->dn = dmalloc (length + 1, MDL);
2396  if (curr->dn == NULL)
2397  {
2398  dfree (curr, MDL);
2399  curr = NULL;
2400  }
2401  else
2402  strcpy (curr->dn, tempbv[cnt]->bv_val);
2403  }
2404 
2405  if (curr != NULL)
2406  {
2407  curr->refs++;
2408 
2409  /* append to service-dn list */
2410  if (ldap_service_dn_tail != NULL)
2411  ldap_service_dn_tail->next = curr;
2412  else
2413  ldap_service_dn_head = curr;
2414 
2415  ldap_service_dn_tail = curr;
2416  }
2417  else
2418  log_fatal ("no memory to remember ldap service dn");
2419 
2420 #if defined (DEBUG_LDAP)
2421  log_info ("LDAP: Parsing dhcpService DN '%s' ...", tempbv[cnt]->bv_val);
2422 #endif
2423  add_to_config_stack (ldres, ent);
2424  res = conf_file_subparse (cfile, root_group, ROOT_GROUP);
2425  if (res != ISC_R_SUCCESS)
2426  {
2427  log_error ("LDAP: cannot parse dhcpService entry '%s'", tempbv[cnt]->bv_val);
2428  break;
2429  }
2430  }
2431 
2432  x_parser_free(&cfile);
2433  ldap_close_debug_fd();
2434 
2435  ldap_memfree (hostdn);
2436  ldap_value_free_len (tempbv);
2437 
2438  if (res != ISC_R_SUCCESS)
2439  {
2440  struct ldap_config_stack *temp_stack;
2441 
2442  while ((curr = ldap_service_dn_head) != NULL)
2443  {
2444  ldap_service_dn_head = curr->next;
2445  dfree (curr->dn, MDL);
2446  dfree (curr, MDL);
2447  }
2448 
2449  ldap_service_dn_tail = NULL;
2450 
2451  while ((temp_stack = ldap_stack) != NULL)
2452  {
2453  ldap_stack = temp_stack->next;
2454  free_stack_entry (temp_stack);
2455  }
2456 
2457  ldap_stop();
2458  }
2459 
2460  /* Unbind from ldap immediately after reading config in static mode. */
2461  if (ldap_method == LDAP_METHOD_STATIC)
2462  ldap_stop();
2463 
2464  return (res);
2465 }
2466 
2467 
2468 /* This function will parse the dhcpOption and dhcpStatements field in the LDAP
2469  entry if it exists. Right now, type will be either HOST_DECL or CLASS_DECL.
2470  If we are parsing a HOST_DECL, this always returns 0. If we are parsing a
2471  CLASS_DECL, this will return what the current lease limit is in LDAP. If
2472  there is no lease limit specified, we return 0 */
2473 
2474 static int
2475 ldap_parse_options (LDAPMessage * ent, struct group *group,
2476  int type, struct host_decl *host,
2477  struct class **class)
2478 {
2479  int declaration, lease_limit;
2480  enum dhcp_token token;
2481  struct parse *cfile;
2482  isc_result_t res;
2483  const char *val;
2484 
2485  lease_limit = 0;
2486  cfile = x_parser_init(type == HOST_DECL ? "LDAP-HOST" : "LDAP-SUBCLASS");
2487  if (cfile == NULL)
2488  return (lease_limit);
2489 
2490  /* This block of code will try to find the parent of the host, and
2491  if it is a group object, fetch the options and apply to the host. */
2492  if (type == HOST_DECL)
2493  {
2494  char *hostdn, *basedn, *temp1, *temp2, filter[1024];
2495  LDAPMessage *groupdn, *entry;
2496  int ret;
2497 
2498  hostdn = ldap_get_dn (ld, ent);
2499  if( hostdn != NULL)
2500  {
2501  basedn = NULL;
2502 
2503  temp1 = strchr (hostdn, '=');
2504  if (temp1 != NULL)
2505  temp1 = strchr (++temp1, '=');
2506  if (temp1 != NULL)
2507  temp2 = strchr (++temp1, ',');
2508  else
2509  temp2 = NULL;
2510 
2511  if (temp2 != NULL)
2512  {
2513  struct berval bv_o;
2514 
2515  if (_do_ldap_str2esc_filter_bv(temp1, (temp2 - temp1), &bv_o) == NULL)
2516  {
2517  log_error ("Cannot escape ldap filter value %.*s: %m",
2518  (int)(temp2 - temp1), temp1);
2519  filter[0] = '\0';
2520  }
2521  else
2522  {
2523  snprintf (filter, sizeof(filter),
2524  "(&(cn=%s)(objectClass=dhcpGroup))",
2525  bv_o.bv_val);
2526 
2527  ber_memfree(bv_o.bv_val);
2528  }
2529 
2530  basedn = strchr (temp1, ',');
2531  if (basedn != NULL)
2532  ++basedn;
2533  }
2534 
2535  if (basedn != NULL && *basedn != '\0' && filter[0] != '\0')
2536  {
2537  ret = ldap_search_ext_s (ld, basedn, LDAP_SCOPE_SUBTREE, filter,
2538  NULL, 0, NULL, NULL, NULL, 0, &groupdn);
2539  if (ret == LDAP_SUCCESS)
2540  {
2541  if ((entry = ldap_first_entry (ld, groupdn)) != NULL)
2542  {
2543  res = ldap_parse_entry_options (entry, cfile, &lease_limit);
2544  if (res != ISC_R_SUCCESS)
2545  {
2546  /* reset option buffer discarding any results */
2547  x_parser_reset(cfile);
2548  lease_limit = 0;
2549  }
2550  }
2551  ldap_msgfree( groupdn);
2552  }
2553  }
2554  ldap_memfree( hostdn);
2555  }
2556  }
2557 
2558  res = ldap_parse_entry_options (ent, cfile, &lease_limit);
2559  if (res != ISC_R_SUCCESS)
2560  {
2561  x_parser_free(&cfile);
2562  return (lease_limit);
2563  }
2564 
2565  if (x_parser_length(cfile) == 0)
2566  {
2567  x_parser_free(&cfile);
2568  return (lease_limit);
2569  }
2570 
2571  declaration = 0;
2572  do
2573  {
2574  token = peek_token (&val, NULL, cfile);
2575  if (token == END_OF_FILE)
2576  break;
2577  declaration = parse_statement (cfile, group, type, host, declaration);
2578  } while (1);
2579 
2580  x_parser_free(&cfile);
2581 
2582  return (lease_limit);
2583 }
2584 
2585 
2586 
2587 int
2588 find_haddr_in_ldap (struct host_decl **hp, int htype, unsigned hlen,
2589  const unsigned char *haddr, const char *file, int line)
2590 {
2591  char buf[128], *type_str;
2592  LDAPMessage * res, *ent;
2593  struct host_decl * host;
2594  isc_result_t status;
2595  ldap_dn_node *curr;
2596  char up_hwaddr[20];
2597  char lo_hwaddr[20];
2598  int ret;
2599  struct berval bv_o[2];
2600 
2601  *hp = NULL;
2602 
2603 
2604  if (ldap_method == LDAP_METHOD_STATIC)
2605  return (0);
2606 
2607  if (ld == NULL)
2608  ldap_start ();
2609  if (ld == NULL)
2610  return (0);
2611 
2612  switch (htype)
2613  {
2614  case HTYPE_ETHER:
2615  type_str = "ethernet";
2616  break;
2617  case HTYPE_IEEE802:
2618  type_str = "token-ring";
2619  break;
2620  case HTYPE_FDDI:
2621  type_str = "fddi";
2622  break;
2623  default:
2624  log_info ("Ignoring unknown type %d", htype);
2625  return (0);
2626  }
2627 
2628  /*
2629  ** FIXME: It is not guaranteed, that the dhcpHWAddress attribute
2630  ** contains _exactly_ "type addr" with one space between!
2631  */
2632  snprintf(lo_hwaddr, sizeof(lo_hwaddr), "%s",
2633  print_hw_addr (htype, hlen, haddr));
2634  x_strxform(up_hwaddr, lo_hwaddr, sizeof(up_hwaddr), toupper);
2635 
2636  if (_do_ldap_str2esc_filter_bv(lo_hwaddr, 0, &bv_o[0]) == NULL)
2637  {
2638  log_error ("Cannot escape ldap filter value %s: %m", lo_hwaddr);
2639  return (0);
2640  }
2641  if (_do_ldap_str2esc_filter_bv(up_hwaddr, 0, &bv_o[1]) == NULL)
2642  {
2643  log_error ("Cannot escape ldap filter value %s: %m", up_hwaddr);
2644  ber_memfree(bv_o[0].bv_val);
2645  return (0);
2646  }
2647 
2648  snprintf (buf, sizeof (buf),
2649  "(&(objectClass=dhcpHost)(|(dhcpHWAddress=%s %s)(dhcpHWAddress=%s %s)))",
2650  type_str, bv_o[0].bv_val, type_str, bv_o[1].bv_val);
2651 
2652  ber_memfree(bv_o[0].bv_val);
2653  ber_memfree(bv_o[1].bv_val);
2654 
2655  res = ent = NULL;
2656  for (curr = ldap_service_dn_head;
2657  curr != NULL && *curr->dn != '\0';
2658  curr = curr->next)
2659  {
2660 #if defined (DEBUG_LDAP)
2661  log_info ("Searching for %s in LDAP tree %s", buf, curr->dn);
2662 #endif
2663  ret = ldap_search_ext_s (ld, curr->dn, LDAP_SCOPE_SUBTREE, buf, NULL, 0,
2664  NULL, NULL, NULL, 0, &res);
2665 
2666  if(ret == LDAP_SERVER_DOWN)
2667  {
2668  log_info ("LDAP server was down, trying to reconnect...");
2669 
2670  ldap_stop();
2671  ldap_start();
2672  if(ld == NULL)
2673  {
2674  log_info ("LDAP reconnect failed - try again later...");
2675  return (0);
2676  }
2677 
2678  ret = ldap_search_ext_s (ld, curr->dn, LDAP_SCOPE_SUBTREE, buf, NULL,
2679  0, NULL, NULL, NULL, 0, &res);
2680  }
2681 
2682  if (ret == LDAP_SUCCESS)
2683  {
2684  ent = ldap_first_entry (ld, res);
2685 #if defined (DEBUG_LDAP)
2686  if (ent == NULL) {
2687  log_info ("No host entry for %s in LDAP tree %s",
2688  buf, curr->dn);
2689  }
2690 #endif
2691  while (ent != NULL) {
2692 #if defined (DEBUG_LDAP)
2693  char *dn = ldap_get_dn (ld, ent);
2694  if (dn != NULL)
2695  {
2696  log_info ("Found dhcpHWAddress LDAP entry %s", dn);
2697  ldap_memfree(dn);
2698  }
2699 #endif
2700 
2701  host = (struct host_decl *)0;
2702  status = host_allocate (&host, MDL);
2703  if (status != ISC_R_SUCCESS)
2704  {
2705  log_fatal ("can't allocate host decl struct: %s",
2706  isc_result_totext (status));
2707  ldap_msgfree (res);
2708  return (0);
2709  }
2710 
2711  host->name = ldap_get_host_name (ent);
2712  if (host->name == NULL)
2713  {
2714  host_dereference (&host, MDL);
2715  ldap_msgfree (res);
2716  return (0);
2717  }
2718 
2719  if (!clone_group (&host->group, root_group, MDL))
2720  {
2721  log_fatal ("can't clone group for host %s", host->name);
2722  host_dereference (&host, MDL);
2723  ldap_msgfree (res);
2724  return (0);
2725  }
2726 
2727  ldap_parse_options (ent, host->group, HOST_DECL, host, NULL);
2728 
2729  host->n_ipaddr = *hp;
2730  *hp = host;
2731  ent = ldap_next_entry (ld, ent);
2732  }
2733  if(res)
2734  {
2735  ldap_msgfree (res);
2736  res = NULL;
2737  }
2738  return (*hp != NULL);
2739  }
2740  else
2741  {
2742  if(res)
2743  {
2744  ldap_msgfree (res);
2745  res = NULL;
2746  }
2747 
2748  if (ret != LDAP_NO_SUCH_OBJECT && ret != LDAP_SUCCESS)
2749  {
2750  log_error ("Cannot search for %s in LDAP tree %s: %s", buf,
2751  curr->dn, ldap_err2string (ret));
2752  ldap_stop();
2753  return (0);
2754  }
2755 #if defined (DEBUG_LDAP)
2756  else
2757  {
2758  log_info ("ldap_search_ext_s returned %s when searching for %s in %s",
2759  ldap_err2string (ret), buf, curr->dn);
2760  }
2761 #endif
2762  }
2763  }
2764 
2765  return (0);
2766 }
2767 
2768 
2769 int
2770 find_subclass_in_ldap (struct class *class, struct class **newclass,
2771  struct data_string *data)
2772 {
2773  LDAPMessage * res, * ent;
2774  int ret, lease_limit;
2775  isc_result_t status;
2776  ldap_dn_node *curr;
2777  char buf[2048];
2778  struct berval bv_class;
2779  struct berval bv_cdata;
2780  char *hex_1;
2781 
2782  if (ldap_method == LDAP_METHOD_STATIC)
2783  return (0);
2784 
2785  if (ld == NULL)
2786  ldap_start ();
2787  if (ld == NULL)
2788  return (0);
2789 
2790  hex_1 = print_hex_1 (data->len, data->data, 1024);
2791  if (*hex_1 == '"')
2792  {
2793  /* result is a quotted not hex string: ldap escape the original string */
2794  if (_do_ldap_str2esc_filter_bv((const char*)data->data, data->len, &bv_cdata) == NULL)
2795  {
2796  log_error ("Cannot escape ldap filter value %s: %m", hex_1);
2797  return (0);
2798  }
2799  hex_1 = NULL;
2800  }
2801  if (_do_ldap_str2esc_filter_bv(class->name, strlen (class->name), &bv_class) == NULL)
2802  {
2803  log_error ("Cannot escape ldap filter value %s: %m", class->name);
2804  if (hex_1 == NULL)
2805  ber_memfree(bv_cdata.bv_val);
2806  return (0);
2807  }
2808 
2809  snprintf (buf, sizeof (buf),
2810  "(&(objectClass=dhcpSubClass)(cn=%s)(dhcpClassData=%s))",
2811  (hex_1 == NULL ? bv_cdata.bv_val : hex_1), bv_class.bv_val);
2812 
2813  if (hex_1 == NULL)
2814  ber_memfree(bv_cdata.bv_val);
2815  ber_memfree(bv_class.bv_val);
2816 
2817 #if defined (DEBUG_LDAP)
2818  log_info ("Searching LDAP for %s", buf);
2819 #endif
2820 
2821  res = ent = NULL;
2822  for (curr = ldap_service_dn_head;
2823  curr != NULL && *curr->dn != '\0';
2824  curr = curr->next)
2825  {
2826 #if defined (DEBUG_LDAP)
2827  log_info ("Searching for %s in LDAP tree %s", buf, curr->dn);
2828 #endif
2829  ret = ldap_search_ext_s (ld, curr->dn, LDAP_SCOPE_SUBTREE, buf, NULL, 0,
2830  NULL, NULL, NULL, 0, &res);
2831 
2832  if(ret == LDAP_SERVER_DOWN)
2833  {
2834  log_info ("LDAP server was down, trying to reconnect...");
2835 
2836  ldap_stop();
2837  ldap_start();
2838 
2839  if(ld == NULL)
2840  {
2841  log_info ("LDAP reconnect failed - try again later...");
2842  return (0);
2843  }
2844 
2845  ret = ldap_search_ext_s (ld, curr->dn, LDAP_SCOPE_SUBTREE, buf,
2846  NULL, 0, NULL, NULL, NULL, 0, &res);
2847  }
2848 
2849  if (ret == LDAP_SUCCESS)
2850  {
2851  if( (ent = ldap_first_entry (ld, res)) != NULL)
2852  break; /* search OK and have entry */
2853 
2854 #if defined (DEBUG_LDAP)
2855  log_info ("No subclass entry for %s in LDAP tree %s",
2856  buf, curr->dn);
2857 #endif
2858  if(res)
2859  {
2860  ldap_msgfree (res);
2861  res = NULL;
2862  }
2863  }
2864  else
2865  {
2866  if(res)
2867  {
2868  ldap_msgfree (res);
2869  res = NULL;
2870  }
2871 
2872  if (ret != LDAP_NO_SUCH_OBJECT && ret != LDAP_SUCCESS)
2873  {
2874  log_error ("Cannot search for %s in LDAP tree %s: %s", buf,
2875  curr->dn, ldap_err2string (ret));
2876  ldap_stop();
2877  return (0);
2878  }
2879 #if defined (DEBUG_LDAP)
2880  else
2881  {
2882  log_info ("ldap_search_ext_s returned %s when searching for %s in %s",
2883  ldap_err2string (ret), buf, curr->dn);
2884  }
2885 #endif
2886  }
2887  }
2888 
2889  if (res && ent)
2890  {
2891 #if defined (DEBUG_LDAP)
2892  char *dn = ldap_get_dn (ld, ent);
2893  if (dn != NULL)
2894  {
2895  log_info ("Found subclass LDAP entry %s", dn);
2896  ldap_memfree(dn);
2897  }
2898 #endif
2899 
2900  status = class_allocate (newclass, MDL);
2901  if (status != ISC_R_SUCCESS)
2902  {
2903  log_error ("Cannot allocate memory for a new class");
2904  ldap_msgfree (res);
2905  return (0);
2906  }
2907 
2908  group_reference (&(*newclass)->group, class->group, MDL);
2909  class_reference (&(*newclass)->superclass, class, MDL);
2910  lease_limit = ldap_parse_options (ent, (*newclass)->group,
2911  CLASS_DECL, NULL, newclass);
2912  if (lease_limit == 0)
2913  (*newclass)->lease_limit = class->lease_limit;
2914  else
2915  class->lease_limit = lease_limit;
2916 
2917  if ((*newclass)->lease_limit)
2918  {
2919  (*newclass)->billed_leases =
2920  dmalloc ((*newclass)->lease_limit * sizeof (struct lease *), MDL);
2921  if (!(*newclass)->billed_leases)
2922  {
2923  log_error ("no memory for billing");
2924  class_dereference (newclass, MDL);
2925  ldap_msgfree (res);
2926  return (0);
2927  }
2928  memset ((*newclass)->billed_leases, 0,
2929  ((*newclass)->lease_limit * sizeof (struct lease *)));
2930  }
2931 
2932  data_string_copy (&(*newclass)->hash_string, data, MDL);
2933 
2934  ldap_msgfree (res);
2935  return (1);
2936  }
2937 
2938  if(res) ldap_msgfree (res);
2939  return (0);
2940 }
2941 
2942 int find_client_in_ldap (struct host_decl **hp, struct packet *packet,
2943  struct option_state *state, const char *file, int line)
2944 {
2945  LDAPMessage * res, * ent;
2946  ldap_dn_node *curr;
2947  struct host_decl * host;
2948  isc_result_t status;
2949  struct data_string client_id;
2950  char buf[1024], buf1[1024];
2951  int ret;
2952 
2953  if (ldap_method == LDAP_METHOD_STATIC)
2954  return (0);
2955 
2956  if (ld == NULL)
2957  ldap_start ();
2958  if (ld == NULL)
2959  return (0);
2960 
2961  memset(&client_id, 0, sizeof(client_id));
2962  if (get_client_id(packet, &client_id) != ISC_R_SUCCESS)
2963  return (0);
2964  snprintf(buf, sizeof(buf),
2965  "(&(objectClass=dhcpHost)(dhcpClientId=%s))",
2966  print_hw_addr(0, client_id.len, client_id.data));
2967 
2968  /* log_info ("Searching LDAP for %s (%s)", buf, packet->interface->shared_network->name); */
2969 
2970  res = ent = NULL;
2971  for (curr = ldap_service_dn_head;
2972  curr != NULL && *curr->dn != '\0';
2973  curr = curr->next)
2974  {
2975  snprintf(buf1, sizeof(buf1), "cn=%s,%s", packet->interface->shared_network->name, curr->dn);
2976 #if defined (DEBUG_LDAP)
2977  log_info ("Searching for %s in LDAP tree %s", buf, buf1);
2978 #endif
2979  ret = ldap_search_ext_s (ld, buf1, LDAP_SCOPE_SUBTREE, buf, NULL, 0,
2980  NULL, NULL, NULL, 0, &res);
2981 
2982  if(ret == LDAP_SERVER_DOWN)
2983  {
2984  log_info ("LDAP server was down, trying to reconnect...");
2985 
2986  ldap_stop();
2987  ldap_start();
2988 
2989  if(ld == NULL)
2990  {
2991  log_info ("LDAP reconnect failed - try again later...");
2992  return (0);
2993  }
2994 
2995  ret = ldap_search_ext_s (ld, buf1, LDAP_SCOPE_SUBTREE, buf,
2996  NULL, 0, NULL, NULL, NULL, 0, &res);
2997  }
2998 
2999  if (ret == LDAP_SUCCESS)
3000  {
3001  if( (ent = ldap_first_entry (ld, res)) != NULL) {
3002  log_info ("found entry in search %s", buf1);
3003  break; /* search OK and have entry */
3004  }
3005 
3006 #if defined (DEBUG_LDAP)
3007  log_info ("No subclass entry for %s in LDAP tree %s", buf, curr->dn);
3008 #endif
3009  if(res)
3010  {
3011  ldap_msgfree (res);
3012  res = NULL;
3013  }
3014  }
3015  else
3016  {
3017  if(res)
3018  {
3019  ldap_msgfree (res);
3020  res = NULL;
3021  }
3022 
3023  if (ret != LDAP_NO_SUCH_OBJECT && ret != LDAP_SUCCESS)
3024  {
3025  log_error ("Cannot search for %s in LDAP tree %s: %s", buf,
3026  curr->dn, ldap_err2string (ret));
3027  ldap_stop();
3028  return (0);
3029  }
3030  else
3031  {
3032  log_info ("did not find: %s", buf);
3033  }
3034  }
3035  }
3036 
3037  if (res && ent)
3038  {
3039 #if defined (DEBUG_LDAP)
3040  log_info ("ldap_get_dn %s", curr->dn);
3041  char *dn = ldap_get_dn (ld, ent);
3042  if (dn != NULL)
3043  {
3044  log_info ("Found subclass LDAP entry %s", dn);
3045  ldap_memfree(dn);
3046  } else {
3047  log_info ("DN is null %s", dn);
3048  }
3049 #endif
3050 
3051  host = (struct host_decl *)0;
3052  status = host_allocate (&host, MDL);
3053  if (status != ISC_R_SUCCESS)
3054  {
3055  log_fatal ("can't allocate host decl struct: %s",
3056  isc_result_totext (status));
3057  ldap_msgfree (res);
3058  return (0);
3059  }
3060 
3061  host->name = ldap_get_host_name (ent);
3062  if (host->name == NULL)
3063  {
3064  host_dereference (&host, MDL);
3065  ldap_msgfree (res);
3066  return (0);
3067  }
3068  /* log_info ("Host name %s", host->name); */
3069 
3070  if (!clone_group (&host->group, root_group, MDL))
3071  {
3072  log_fatal ("can't clone group for host %s", host->name);
3073  host_dereference (&host, MDL);
3074  ldap_msgfree (res);
3075  return (0);
3076  }
3077 
3078  ldap_parse_options (ent, host->group, HOST_DECL, host, NULL);
3079 
3080  *hp = host;
3081  ldap_msgfree (res);
3082  return (1);
3083  }
3084  else
3085  {
3086  log_info ("did not find clientid: %s", buf);
3087  }
3088 
3089  if(res) ldap_msgfree (res);
3090  return (0);
3091 
3092 }
3093 
3094 #if defined(LDAP_USE_GSSAPI)
3095 static int
3096 _ldap_sasl_interact(LDAP *ld, unsigned flags, void *defaults, void *sin)
3097 {
3098  sasl_interact_t *in;
3099  struct ldap_sasl_instance *ldap_inst = defaults;
3100  int ret = LDAP_OTHER;
3101  size_t size;
3102 
3103  if (ld == NULL || sin == NULL)
3104  return LDAP_PARAM_ERROR;
3105 
3106  log_info("doing interactive bind");
3107  for (in = sin; in != NULL && in->id != SASL_CB_LIST_END; in++) {
3108  switch (in->id) {
3109  case SASL_CB_USER:
3110  log_info("got request for SASL_CB_USER %s", ldap_inst->sasl_authz_id);
3111  size = strlen(ldap_inst->sasl_authz_id);
3112  in->result = ldap_inst->sasl_authz_id;
3113  in->len = size;
3114  ret = LDAP_SUCCESS;
3115  break;
3116  case SASL_CB_GETREALM:
3117  log_info("got request for SASL_CB_GETREALM %s", ldap_inst->sasl_realm);
3118  size = strlen(ldap_inst->sasl_realm);
3119  in->result = ldap_inst->sasl_realm;
3120  in->len = size;
3121  ret = LDAP_SUCCESS;
3122  break;
3123  case SASL_CB_AUTHNAME:
3124  log_info("got request for SASL_CB_AUTHNAME %s", ldap_inst->sasl_authc_id);
3125  size = strlen(ldap_inst->sasl_authc_id);
3126  in->result = ldap_inst->sasl_authc_id;
3127  in->len = size;
3128  ret = LDAP_SUCCESS;
3129  break;
3130  case SASL_CB_PASS:
3131  log_info("got request for SASL_CB_PASS %s", ldap_inst->sasl_password);
3132  size = strlen(ldap_inst->sasl_password);
3133  in->result = ldap_inst->sasl_password;
3134  in->len = size;
3135  ret = LDAP_SUCCESS;
3136  break;
3137  default:
3138  goto cleanup;
3139  }
3140  }
3141  return ret;
3142 
3143 cleanup:
3144  in->result = NULL;
3145  in->len = 0;
3146  return LDAP_OTHER;
3147 }
3148 #endif /* LDAP_USE_GSSAPI */
3149 
3150 
3151 #endif
const char int line
Definition: dhcpd.h:3676
struct binding_scope * global_scope
Definition: tree.c:39
Definition: dhcpd.h:550
unsigned len
Definition: tree.h:80
struct shared_network * shared_network
Definition: dhcpd.h:1327
isc_result_t end_parse(struct parse **cfile)
Definition: conflex.c:103
void * dmalloc(unsigned, const char *, int)
Definition: alloc.c:56
struct universe server_universe
Definition: stables.c:175
size_t buflen
Definition: dhcpd.h:329
#define MDL
Definition: omapip.h:568
#define print_hex_1(len, data, limit)
Definition: dhcpd.h:2533
int group_reference(struct group **ptr, struct group *bp, const char *file, int line)
Definition: alloc.c:178
void data_string_forget(struct data_string *data, const char *file, int line)
Definition: alloc.c:1340
struct group * root_group
Definition: memory.c:31
int log_error(const char *,...) __attribute__((__format__(__printf__
const char * tlname
Definition: dhcpd.h:294
enum dhcp_token peek_token(const char **rval, unsigned *rlen, struct parse *cfile)
Definition: conflex.c:443
struct parse * saved_state
Definition: dhcpd.h:332
#define HAVE_INET_NTOP
Definition: config.h:60
Definition: dhcpd.h:288
char * name
Definition: dhcpd.h:1062
void log_fatal(const char *,...) __attribute__((__format__(__printf__
#define HTYPE_ETHER
Definition: dhcp.h:76
void execute_statements_in_scope(struct binding_value **result, struct packet *packet, struct lease *lease, struct client_state *client_state, struct option_state *in_options, struct option_state *out_options, struct binding_scope **scope, struct group *group, struct group *limiting_group, struct on_star *on_star)
Definition: execute.c:563
int option_state_allocate(struct option_state **ptr, const char *file, int line)
Definition: alloc.c:847
int evaluate_option_cache(struct data_string *result, struct packet *packet, struct lease *lease, struct client_state *client_state, struct option_state *in_options, struct option_state *cfg_options, struct binding_scope **scope, struct option_cache *oc, const char *file, int line)
Definition: tree.c:2688
struct interface_info * interface
Definition: dhcpd.h:427
Definition: dhcpd.h:405
char * name
Definition: dhcpd.h:934
void dfree(void *, const char *, int)
Definition: alloc.c:131
struct host_decl * n_ipaddr
Definition: dhcpd.h:932
struct option_cache * lookup_option(struct universe *universe, struct option_state *options, unsigned code)
Definition: options.c:2348
int int log_info(const char *,...) __attribute__((__format__(__printf__
#define CLASS_DECL
Definition: dhcpd.h:680
void cleanup(void)
dhcp_token
Definition: dhctoken.h:35
isc_result_t get_client_id(struct packet *, struct data_string *)
#define ROOT_GROUP
Definition: dhcpd.h:676
#define HTYPE_FDDI
Definition: dhcp.h:78
int parse_statement(struct parse *, struct group *, int, struct host_decl *, int)
Definition: confpars.c:347
int option_state_dereference(struct option_state **ptr, const char *file, int line)
Definition: alloc.c:912
Definition: dhcpd.h:918
size_t bufsiz
Definition: dhcpd.h:330
const char int
Definition: omapip.h:443
size_t bufix
Definition: dhcpd.h:329
#define HOST_DECL
Definition: dhcpd.h:677
#define HTYPE_IEEE802
Definition: dhcp.h:77
int flags
Definition: dhcpd.h:947
const char * file
Definition: dhcpd.h:3676
char * name
Definition: dhcpd.h:1016
char * inbuf
Definition: dhcpd.h:328
Definition: dhcpd.h:1058
const unsigned char * data
Definition: tree.h:79
isc_result_t conf_file_subparse(struct parse *, struct group *, int)
Definition: confpars.c:235
void data_string_copy(struct data_string *dest, const struct data_string *src, const char *file, int line)
Definition: alloc.c:1324
int clone_group(struct group **gp, struct group *group, const char *file, int line)
Definition: memory.c:130
isc_result_t new_parse(struct parse **cfile, int file, char *inbuf, unsigned buflen, const char *name, int eolp)
Definition: conflex.c:41
struct group * group
Definition: dhcpd.h:944
struct group * group
Definition: dhcpd.h:1085