⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 zone2ldap.c

📁 package of develop dns
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * Copyright (C) 2001 Jeff McNeil <jeff@snapcase.g-rock.net> * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. *  * Change Log * * Tue May  1 19:19:54 EDT 2001 - Jeff McNeil * Update to objectClass code, and add_to_rr_list function * (I need to rename that) to support the dNSZone schema, * ditched dNSDomain2 schema support. Version 0.3-ALPHA */#include <errno.h>#include <string.h>#include <stdlib.h>#include <unistd.h>#include <getopt.h>#include <isc/buffer.h>#include <isc/mem.h>#include <isc/print.h>#include <isc/result.h>#include <dns/db.h>#include <dns/dbiterator.h>#include <dns/fixedname.h>#include <dns/name.h>#include <dns/rdata.h>#include <dns/rdataset.h>#include <dns/rdatasetiter.h>#include <dns/result.h>#include <dns/rdatatype.h>#include <ldap.h>#define DNS_OBJECT 6#define DNS_TOP	   2#define VERSION    "0.4-ALPHA"#define NO_SPEC 0 #define WI_SPEC  1/* Global Zone Pointer */char *gbl_zone = NULL;typedef struct LDAP_INFO{  char *dn;  LDAPMod **attrs;  struct LDAP_INFO *next;  int attrcnt;}ldap_info;/* usage Info */void usage ();/* Add to the ldap dit */void add_ldap_values (ldap_info * ldinfo);/* Init an ldap connection */void init_ldap_conn ();/* Ldap error checking */void ldap_result_check (char *msg, char *dn, int err);/* Put a hostname into a char ** array */char **hostname_to_dn_list (char *hostname, char *zone, unsigned int flags);/* Find out how many items are in a char ** array */int get_attr_list_size (char **tmp);/* Get a DN */char *build_dn_from_dc_list (char **dc_list, unsigned int ttl, int flag);/* Add to RR list */void add_to_rr_list (char *dn, char *name, char *type, char *data,		     unsigned int ttl, unsigned int flags);/* Error checking */void isc_result_check (isc_result_t res, char *errorstr);/* Generate LDIF Format files */void generate_ldap (dns_name_t * dnsname, dns_rdata_t * rdata,		    unsigned int ttl);/* head pointer to the list */ldap_info *ldap_info_base = NULL;char *argzone, *ldapbase, *binddn, *bindpw = NULL;char *ldapsystem = "localhost";static char *objectClasses[] =  { "top", "dNSZone", NULL };static char *topObjectClasses[] = { "top", NULL };LDAP *conn;unsigned int debug = 0;#ifdef DEBUGdebug = 1;#endifintmain (int *argc, char **argv){  isc_mem_t *isc_ctx = NULL;  isc_result_t result;  char *basedn;  ldap_info *tmp;  LDAPMod *base_attrs[2];  LDAPMod base;  isc_buffer_t buff;  char *zonefile;  char fullbasedn[1024];  char *ctmp;  dns_fixedname_t fixedzone, fixedname;  dns_rdataset_t rdataset;  char **dc_list;  dns_rdata_t rdata = DNS_RDATA_INIT;  dns_rdatasetiter_t *riter;  dns_name_t *zone, *name;  dns_db_t *db = NULL;  dns_dbiterator_t *dbit = NULL;  dns_dbnode_t *node;  extern char *optarg;  extern int optind, opterr, optopt;  int create_base = 0;  int topt;  if ((int) argc < 2)    {      usage ();      exit (-1);    }  while ((topt = getopt ((int) argc, argv, "D:w:b:z:f:h:?dcv")) != -1)    {      switch (topt)	{	case 'v':		printf("%s\n", VERSION);		exit(0);	case 'c':	  create_base++;	  break;	case 'd':	  debug++;	  break;	case 'D':	  binddn = strdup (optarg);	  break;	case 'w':	  bindpw = strdup (optarg);	  break;	case 'b':	  ldapbase = strdup (optarg);	  break;	case 'z':	  argzone = strdup (optarg);	  // We wipe argzone all to hell when we parse it for the DN */	  gbl_zone = strdup(argzone);	  break;	case 'f':	  zonefile = strdup (optarg);	  break;	case 'h':	  ldapsystem = strdup (optarg);	  break;	case '?':	default:	  usage ();	  exit (0);	}    }  if ((argzone == NULL) || (zonefile == NULL))    {      usage ();      exit (-1);    }  if (debug)    printf ("Initializing ISC Routines, parsing zone file\n");  result = isc_mem_create (0, 0, &isc_ctx);  isc_result_check (result, "isc_mem_create");  isc_buffer_init (&buff, argzone, strlen (argzone));  isc_buffer_add (&buff, strlen (argzone));  dns_fixedname_init (&fixedzone);  zone = dns_fixedname_name (&fixedzone);  result = dns_name_fromtext (zone, &buff, dns_rootname, ISC_FALSE, NULL);  isc_result_check (result, "dns_name_fromtext");  result =    dns_db_create (isc_ctx, "rbt", zone, dns_dbtype_zone, dns_rdataclass_in,		   0, NULL, &db);  isc_result_check (result, "dns_db_create");  result = dns_db_load (db, zonefile);  isc_result_check (result, "Check Zone Syntax: dns_db_load");  result = dns_db_createiterator (db, ISC_FALSE, &dbit);  isc_result_check (result, "dns_db_createiterator");  result = dns_dbiterator_first (dbit);  isc_result_check (result, "dns_dbiterator_first");  dns_fixedname_init (&fixedname);  name = dns_fixedname_name (&fixedname);  dns_rdataset_init (&rdataset);  dns_rdata_init (&rdata);  while (result == ISC_R_SUCCESS)    {      node = NULL;      result = dns_dbiterator_current (dbit, &node, name);      if (result == ISC_R_NOMORE)	break;      isc_result_check (result, "dns_dbiterator_current");      riter = NULL;      result = dns_db_allrdatasets (db, node, NULL, 0, &riter);      isc_result_check (result, "dns_db_allrdatasets");      result = dns_rdatasetiter_first (riter);      //isc_result_check(result, "dns_rdatasetiter_first");      while (result == ISC_R_SUCCESS)	{	  dns_rdatasetiter_current (riter, &rdataset);	  result = dns_rdataset_first (&rdataset);	  isc_result_check (result, "dns_rdatasetiter_current");	  while (result == ISC_R_SUCCESS)	    {	      dns_rdataset_current (&rdataset, &rdata);	      generate_ldap (name, &rdata, rdataset.ttl);	      dns_rdata_reset (&rdata);	      result = dns_rdataset_next (&rdataset);	    }	  dns_rdataset_disassociate (&rdataset);	  result = dns_rdatasetiter_next (riter);	}      dns_rdatasetiter_destroy (&riter);      result = dns_dbiterator_next (dbit);    }  /* Initialize the LDAP Connection */  if (debug)    printf ("Initializing LDAP Connection to %s as %s\n", ldapsystem, binddn);  init_ldap_conn ();  if (create_base)    {      if (debug)	printf ("Creating base zone DN %s\n", argzone);      dc_list = hostname_to_dn_list (argzone, argzone, DNS_TOP);      basedn = build_dn_from_dc_list (dc_list, 0, NO_SPEC);      for (ctmp = &basedn[strlen (basedn)]; ctmp >= &basedn[0]; ctmp--)	{	  if ((*ctmp == ',') || (ctmp == &basedn[0]))	    {	      base.mod_op = LDAP_MOD_ADD;	      base.mod_type = "objectClass";	      base.mod_values = topObjectClasses;	      base_attrs[0] = &base;	      base_attrs[1] = NULL;	      if (ldapbase)		{		  if (ctmp != &basedn[0])		    sprintf (fullbasedn, "%s,%s", ctmp + 1, ldapbase);		  else		    sprintf (fullbasedn, "%s,%s", ctmp, ldapbase);		}	      else		{		  if (ctmp != &basedn[0])		    sprintf (fullbasedn, "%s", ctmp + 1);		  else		    sprintf (fullbasedn, "%s", ctmp);		}	      result = ldap_add_s (conn, fullbasedn, base_attrs);	      ldap_result_check ("intial ldap_add_s", fullbasedn, result);	    }	}    }  else    {      if (debug)	printf ("Skipping zone base dn creation for %s\n", argzone);    }  for (tmp = ldap_info_base; tmp != NULL; tmp = tmp->next)    {      if (debug)	printf ("Adding DN: %s\n", tmp->dn);      add_ldap_values (tmp);    }if (debug)	printf("Operation Complete.\n");  return 0;}/* Check the status of an isc_result_t after any isc routines. * I should probably rename this function, as not to cause any * confusion with the isc* routines. Will exit on error. */voidisc_result_check (isc_result_t res, char *errorstr){  if (res != ISC_R_SUCCESS)    {      fprintf (stderr, " %s: %s\n", errorstr, isc_result_totext (res));      exit (-1);    }}/* Takes DNS information, in bind data structure format, and adds textual * zone information to the LDAP run queue. */voidgenerate_ldap (dns_name_t * dnsname, dns_rdata_t * rdata, unsigned int ttl){  unsigned char name[DNS_NAME_MAXTEXT + 1];  unsigned int len;  unsigned char type[20];  unsigned char data[2048];  char **dc_list;  char *dn;  isc_buffer_t buff;  isc_result_t result;  isc_buffer_init (&buff, name, sizeof (name));  result = dns_name_totext (dnsname, ISC_TRUE, &buff);  isc_result_check (result, "dns_name_totext");  name[isc_buffer_usedlength (&buff)] = 0;  isc_buffer_init (&buff, type, sizeof (type));  result = dns_rdatatype_totext (rdata->type, &buff);  isc_result_check (result, "dns_rdatatype_totext");  type[isc_buffer_usedlength (&buff)] = 0;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -