Back to index

openldap  2.4.31
config.cpp
Go to the documentation of this file.
00001 /* config.cpp - ndb backend configuration file routine */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00004  *
00005  * Copyright 2008-2012 The OpenLDAP Foundation.
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted only as authorized by the OpenLDAP
00010  * Public License.
00011  *
00012  * A copy of this license is available in the file LICENSE in the
00013  * top-level directory of the distribution or, alternatively, at
00014  * <http://www.OpenLDAP.org/license.html>.
00015  */
00016 /* ACKNOWLEDGEMENTS:
00017  * This work was initially developed by Howard Chu for inclusion
00018  * in OpenLDAP Software. This work was sponsored by MySQL.
00019  */
00020 
00021 #include "portable.h"
00022 #include "lutil.h"
00023 
00024 #include "back-ndb.h"
00025 
00026 #include "config.h"
00027 
00028 extern "C" {
00029        static ConfigDriver ndb_cf_gen;
00030 };
00031 
00032 enum {
00033        NDB_ATLEN = 1,
00034        NDB_ATSET,
00035        NDB_INDEX,
00036        NDB_ATBLOB
00037 };
00038 
00039 static ConfigTable ndbcfg[] = {
00040        { "dbhost", "hostname", 2, 2, 0, ARG_STRING|ARG_OFFSET,
00041               (void *)offsetof(struct ndb_info, ni_hostname),
00042               "( OLcfgDbAt:6.1 NAME 'olcDbHost' "
00043                      "DESC 'Hostname of SQL server' "
00044                      "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
00045        { "dbname", "name", 2, 2, 0, ARG_STRING|ARG_OFFSET,
00046               (void *)offsetof(struct ndb_info, ni_dbname),
00047               "( OLcfgDbAt:6.2 NAME 'olcDbName' "
00048                      "DESC 'Name of SQL database' "
00049                      "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
00050        { "dbuser", "username", 2, 2, 0, ARG_STRING|ARG_OFFSET,
00051               (void *)offsetof(struct ndb_info, ni_username),
00052               "( OLcfgDbAt:6.3 NAME 'olcDbUser' "
00053                      "DESC 'Username for SQL session' "
00054                      "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
00055        { "dbpass", "password", 2, 2, 0, ARG_STRING|ARG_OFFSET,
00056               (void *)offsetof(struct ndb_info, ni_password),
00057               "( OLcfgDbAt:6.4 NAME 'olcDbPass' "
00058                      "DESC 'Password for SQL session' "
00059                      "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
00060        { "dbport", "port", 2, 2, 0, ARG_UINT|ARG_OFFSET,
00061               (void *)offsetof(struct ndb_info, ni_port),
00062               "( OLcfgDbAt:6.5 NAME 'olcDbPort' "
00063                      "DESC 'Port number of SQL server' "
00064                      "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
00065        { "dbsocket", "path", 2, 2, 0, ARG_STRING|ARG_OFFSET,
00066               (void *)offsetof(struct ndb_info, ni_socket),
00067               "( OLcfgDbAt:6.6 NAME 'olcDbSocket' "
00068                      "DESC 'Local socket path of SQL server' "
00069                      "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
00070        { "dbflag", "flag", 2, 2, 0, ARG_LONG|ARG_OFFSET,
00071               (void *)offsetof(struct ndb_info, ni_clflag),
00072               "( OLcfgDbAt:6.7 NAME 'olcDbFlag' "
00073                      "DESC 'Flags for SQL session' "
00074                      "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
00075        { "dbconnect", "hostname", 2, 2, 0, ARG_STRING|ARG_OFFSET,
00076               (void *)offsetof(struct ndb_info, ni_connectstr),
00077               "( OLcfgDbAt:6.8 NAME 'olcDbConnect' "
00078                      "DESC 'Hostname of NDB server' "
00079                      "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
00080        { "dbconnections", "number", 2, 2, 0, ARG_INT|ARG_OFFSET,
00081               (void *)offsetof(struct ndb_info, ni_nconns),
00082               "( OLcfgDbAt:6.9 NAME 'olcDbConnections' "
00083                      "DESC 'Number of cluster connections to open' "
00084                      "SYNTAX OMsInteger SINGLE-VALUE )", NULL, NULL },
00085        { "attrlen", "attr> <len", 3, 3, 0, ARG_MAGIC|NDB_ATLEN,
00086               (void *)ndb_cf_gen,
00087               "( OLcfgDbAt:6.10 NAME 'olcNdbAttrLen' "
00088                      "DESC 'Column length of a specific attribute' "
00089                      "EQUALITY caseIgnoreMatch "
00090                      "SYNTAX OMsDirectoryString )", NULL, NULL },
00091        { "attrset", "set> <attrs", 3, 3, 0, ARG_MAGIC|NDB_ATSET,
00092               (void *)ndb_cf_gen,
00093               "( OLcfgDbAt:6.11 NAME 'olcNdbAttrSet' "
00094                      "DESC 'Set of common attributes' "
00095                      "EQUALITY caseIgnoreMatch "
00096                      "SYNTAX OMsDirectoryString )", NULL, NULL },
00097        { "index", "attr", 2, 2, 0, ARG_MAGIC|NDB_INDEX,
00098               (void *)ndb_cf_gen, "( OLcfgDbAt:0.2 NAME 'olcDbIndex' "
00099               "DESC 'Attribute to index' "
00100               "EQUALITY caseIgnoreMatch "
00101               "SYNTAX OMsDirectoryString )", NULL, NULL },
00102        { "attrblob", "attr", 2, 2, 0, ARG_MAGIC|NDB_ATBLOB,
00103               (void *)ndb_cf_gen, "( OLcfgDbAt:6.12 NAME 'olcNdbAttrBlob' "
00104               "DESC 'Attribute to treat as a BLOB' "
00105               "EQUALITY caseIgnoreMatch "
00106               "SYNTAX OMsDirectoryString )", NULL, NULL },
00107        { "directory", "dir", 2, 2, 0, ARG_IGNORED,
00108               NULL, "( OLcfgDbAt:0.1 NAME 'olcDbDirectory' "
00109                      "DESC 'Dummy keyword' "
00110                      "SYNTAX OMsDirectoryString SINGLE-VALUE )", NULL, NULL },
00111        { NULL, NULL, 0, 0, 0, ARG_IGNORED,
00112               NULL, NULL, NULL, NULL }
00113 };
00114 
00115 static ConfigOCs ndbocs[] = {
00116        {
00117               "( OLcfgDbOc:6.2 "
00118               "NAME 'olcNdbConfig' "
00119               "DESC 'NDB backend configuration' "
00120               "SUP olcDatabaseConfig "
00121               "MUST ( olcDbHost $ olcDbName $ olcDbConnect ) "
00122               "MAY ( olcDbUser $ olcDbPass $ olcDbPort $ olcDbSocket $ "
00123               "olcDbFlag $ olcDbConnections $ olcNdbAttrLen $ "
00124               "olcDbIndex $ olcNdbAttrSet $ olcNdbAttrBlob ) )",
00125                      Cft_Database, ndbcfg },
00126        { NULL, Cft_Abstract, NULL }
00127 };
00128 
00129 static int
00130 ndb_cf_gen( ConfigArgs *c )
00131 {
00132        struct ndb_info *ni = (struct ndb_info *)c->be->be_private;
00133        int i, rc;
00134        NdbAttrInfo *ai;
00135        NdbOcInfo *oci;
00136        ListNode *ln, **l2;
00137        struct berval bv, *bva;
00138 
00139        if ( c->op == SLAP_CONFIG_EMIT ) {
00140               char buf[BUFSIZ];
00141               rc = 0;
00142               bv.bv_val = buf;
00143               switch( c->type ) {
00144               case NDB_ATLEN:
00145                      if ( ni->ni_attrlens ) {
00146                             for ( ln = ni->ni_attrlens; ln; ln=ln->ln_next ) {
00147                                    ai = (NdbAttrInfo *)ln->ln_data;
00148                                    bv.bv_len = snprintf( buf, sizeof(buf),
00149                                           "%s %d", ai->na_name.bv_val,
00150                                                  ai->na_len );
00151                                    value_add_one( &c->rvalue_vals, &bv );
00152                             }
00153                      } else {
00154                             rc = 1;
00155                      }
00156                      break;
00157 
00158               case NDB_ATSET:
00159                      if ( ni->ni_attrsets ) {
00160                             char *ptr, *end = buf+sizeof(buf);
00161                             for ( ln = ni->ni_attrsets; ln; ln=ln->ln_next ) {
00162                                    oci = (NdbOcInfo *)ln->ln_data;
00163                                    ptr = lutil_strcopy( buf, oci->no_name.bv_val );
00164                                    *ptr++ = ' ';
00165                                    for ( i=0; i<oci->no_nattrs; i++ ) {
00166                                           if ( end - ptr < oci->no_attrs[i]->na_name.bv_len+1 )
00167                                                  break;
00168                                           if ( i )
00169                                                  *ptr++ = ',';
00170                                           ptr = lutil_strcopy(ptr,
00171                                                  oci->no_attrs[i]->na_name.bv_val );
00172                                    }
00173                                    bv.bv_len = ptr - buf;
00174                                    value_add_one( &c->rvalue_vals, &bv );
00175                             }
00176                      } else {
00177                             rc = 1;
00178                      }
00179                      break;
00180 
00181               case NDB_INDEX:
00182                      if ( ni->ni_attridxs ) {
00183                             for ( ln = ni->ni_attridxs; ln; ln=ln->ln_next ) {
00184                                    ai = (NdbAttrInfo *)ln->ln_data;
00185                                    value_add_one( &c->rvalue_vals, &ai->na_name );
00186                             }
00187                      } else {
00188                             rc = 1;
00189                      }
00190                      break;
00191 
00192               case NDB_ATBLOB:
00193                      if ( ni->ni_attrblobs ) {
00194                             for ( ln = ni->ni_attrblobs; ln; ln=ln->ln_next ) {
00195                                    ai = (NdbAttrInfo *)ln->ln_data;
00196                                    value_add_one( &c->rvalue_vals, &ai->na_name );
00197                             }
00198                      } else {
00199                             rc = 1;
00200                      }
00201                      break;
00202 
00203               }
00204               return rc;
00205        } else if ( c->op == LDAP_MOD_DELETE ) { /* FIXME */
00206               rc = 0;
00207               switch( c->type ) {
00208               case NDB_INDEX:
00209                      if ( c->valx == -1 ) {
00210 
00211                             /* delete all */
00212 
00213                      } else {
00214 
00215                      }
00216                      break;
00217               }
00218               return rc;
00219        }
00220 
00221        switch( c->type ) {
00222        case NDB_ATLEN:
00223               ber_str2bv( c->argv[1], 0, 0, &bv );
00224               ai = ndb_ai_get( ni, &bv );
00225               if ( !ai ) {
00226                      snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s: invalid attr %s",
00227                             c->log, c->argv[1] );
00228                      Debug( LDAP_DEBUG_ANY, "%s\n", c->cr_msg, 0, 0 );
00229                      return -1;
00230               }
00231               for ( ln = ni->ni_attrlens; ln; ln = ln->ln_next ) {
00232                      if ( ln->ln_data == (void *)ai ) {
00233                             snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s: attr len already set for %s",
00234                                    c->log, c->argv[1] );
00235                             Debug( LDAP_DEBUG_ANY, "%s\n", c->cr_msg, 0, 0 );
00236                             return -1;
00237                      }
00238               }
00239               ai->na_len = atoi( c->argv[2] );
00240               ai->na_flag |= NDB_INFO_ATLEN;
00241               ln = (ListNode *)ch_malloc( sizeof(ListNode));
00242               ln->ln_data = ai;
00243               ln->ln_next = NULL;
00244               for ( l2 = &ni->ni_attrlens; *l2; l2 = &(*l2)->ln_next );
00245               *l2 = ln;
00246               break;
00247               
00248        case NDB_INDEX:
00249               ber_str2bv( c->argv[1], 0, 0, &bv );
00250               ai = ndb_ai_get( ni, &bv );
00251               if ( !ai ) {
00252                      snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s: invalid attr %s",
00253                             c->log, c->argv[1] );
00254                      Debug( LDAP_DEBUG_ANY, "%s\n", c->cr_msg, 0, 0 );
00255                      return -1;
00256               }
00257               for ( ln = ni->ni_attridxs; ln; ln = ln->ln_next ) {
00258                      if ( ln->ln_data == (void *)ai ) {
00259                             snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s: attr index already set for %s",
00260                                    c->log, c->argv[1] );
00261                             Debug( LDAP_DEBUG_ANY, "%s\n", c->cr_msg, 0, 0 );
00262                             return -1;
00263                      }
00264               }
00265               ai->na_flag |= NDB_INFO_INDEX;
00266               ln = (ListNode *)ch_malloc( sizeof(ListNode));
00267               ln->ln_data = ai;
00268               ln->ln_next = NULL;
00269               for ( l2 = &ni->ni_attridxs; *l2; l2 = &(*l2)->ln_next );
00270               *l2 = ln;
00271               break;
00272 
00273        case NDB_ATSET:
00274               ber_str2bv( c->argv[1], 0, 0, &bv );
00275               bva = ndb_str2bvarray( c->argv[2], strlen( c->argv[2] ), ',', NULL );
00276               rc = ndb_aset_get( ni, &bv, bva, &oci );
00277               ber_bvarray_free( bva );
00278               if ( rc ) {
00279                      if ( rc == LDAP_ALREADY_EXISTS ) {
00280                             snprintf( c->cr_msg, sizeof( c->cr_msg ),
00281                                    "%s: attrset %s already defined",
00282                                    c->log, c->argv[1] );
00283                      } else {
00284                             snprintf( c->cr_msg, sizeof( c->cr_msg ),
00285                                    "%s: invalid attrset %s (%d)",
00286                                    c->log, c->argv[1], rc );
00287                      }
00288                      Debug( LDAP_DEBUG_ANY, "%s\n", c->cr_msg, 0, 0 );
00289                      return -1;
00290               }
00291               ln = (ListNode *)ch_malloc( sizeof(ListNode));
00292               ln->ln_data = oci;
00293               ln->ln_next = NULL;
00294               for ( l2 = &ni->ni_attrsets; *l2; l2 = &(*l2)->ln_next );
00295               *l2 = ln;
00296               break;
00297 
00298        case NDB_ATBLOB:
00299               ber_str2bv( c->argv[1], 0, 0, &bv );
00300               ai = ndb_ai_get( ni, &bv );
00301               if ( !ai ) {
00302                      snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s: invalid attr %s",
00303                             c->log, c->argv[1] );
00304                      Debug( LDAP_DEBUG_ANY, "%s\n", c->cr_msg, 0, 0 );
00305                      return -1;
00306               }
00307               for ( ln = ni->ni_attrblobs; ln; ln = ln->ln_next ) {
00308                      if ( ln->ln_data == (void *)ai ) {
00309                             snprintf( c->cr_msg, sizeof( c->cr_msg ), "%s: attr blob already set for %s",
00310                                    c->log, c->argv[1] );
00311                             Debug( LDAP_DEBUG_ANY, "%s\n", c->cr_msg, 0, 0 );
00312                             return -1;
00313                      }
00314               }
00315               ai->na_flag |= NDB_INFO_ATBLOB;
00316               ln = (ListNode *)ch_malloc( sizeof(ListNode));
00317               ln->ln_data = ai;
00318               ln->ln_next = NULL;
00319               for ( l2 = &ni->ni_attrblobs; *l2; l2 = &(*l2)->ln_next );
00320               *l2 = ln;
00321               break;
00322 
00323        }
00324        return 0;
00325 }
00326 
00327 extern "C"
00328 int ndb_back_init_cf( BackendInfo *bi )
00329 {
00330        bi->bi_cf_ocs = ndbocs;
00331 
00332        return config_register_schema( ndbcfg, ndbocs );
00333 }