Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

LDAPOBJECT Class Reference

#include <ldapconf_defs.h>

Inheritance diagram for LDAPOBJECT:

ARRAY_OBJ List of all members.

Public Methods

 LDAPOBJECT (const char *profile_name)
 LDAPOBJECT (void)
int add (void)
int at_add (const char *name, const char *val)
int at_add (const char *name, int val)
int at_del (const char *name)
const char* at_get (const char *name)
const char* at_get (const char *name, int k)
int at_getall (const char *name, SSTRINGS&lst)
const char* at_getname (int num)
const char* at_getval (int num)
int at_list (SSTRINGS&lst)
int at_set (const char *name, const char *val)
int at_set (const char *name, int val)
int command (const char *command, SSTRINGS&resmsg)
int del (void)
int dn_set (const char *val)
int export_confdb (CONFDB *c_data)
int export_ldif (const char *path)
int get_free_gid (void)
int get_free_uid (void)
int import_confdb (CONFDB *c_data)
void init (void)
int ldif_splitline (const char *ldif, char *name, char *val)
int load_profile (const char *profile_name)
int locate (const char *key, SSTRINGS&lst)
SSTRING* locateassign (const char *key)
SSTRING* locateassign (const char *key, int k)
int modify (void)
int oc_add (const char *val)
int oc_del (const char *name)
const char* oc_getval (int num)
int oc_list (SSTRINGS&lst)
int reset (void)
int reset_data (void)
int search (void)
int search_list (SSTRINGS&lst)
int search_list_val (SSTRINGS&lst, const char *key)
int search_list_vals (SSTRINGS&lst, const char *key, const char *at_name[])
int set_password (const char *username, const char *password)
 ~LDAPOBJECT (void)

Public Attributes

LDAPPROFILE bind
CONFDB* form
CONFDB* c_profile
SSTRING profile
SSTRING bindname
SSTRING formname
SSTRING dn_prefix
SSTRING group_prefix
SSTRING primary_key
SSTRING crypt_hash
SSTRING dn
SSTRING host
SSTRING port
SSTRING binddn
SSTRING bindpw
SSTRING base
SSTRING scope
SSTRING filter
SSTRING attr
SSTRING command_line
SSTRINGS atlist
SSTRINGS oclist
char ldif_format
char* ldapconf_entry
int last_uid

Constructor & Destructor Documentation

PUBLIC LDAPOBJECT::LDAPOBJECT ( const char * profile_name )
 

Create and initialize a new object. Load named directory profile.

Definition at line 56 of file ldap_object.cc.

00057 {
00058     D(debugf(4,">>>LDAPOBJECT::CREATE(%s)",profile_name));
00059 
00060     init();
00061     load_profile(profile_name);
00062 }

PUBLIC LDAPOBJECT::LDAPOBJECT ( void )
 

Create and initialize a new object.

Definition at line 43 of file ldap_object.cc.

00044 {
00045     D(debugf(4,">>>LDAPOBJECT::CREATE:"));
00046     
00047     init();
00048 }

PUBLIC LDAPOBJECT::~LDAPOBJECT ( void )
 

Destroy object and free allocations.

Definition at line 87 of file ldap_object.cc.

00088 {
00089     free (ldapconf_entry);
00090     if (this->form != NULL) delete this->form;
00091 
00092     D(debugf(4,"<<<LDAPOBJECT::DESTROY:"));
00093 }


Member Function Documentation

PUBLIC int LDAPOBJECT::add ( void )
 

LDAPOBJECT.add

Add object to directory.

Definition at line 219 of file ldap_object.cc.

Referenced by LDAPDB::create(), ldap_form_adduser(), ldap_formclient(), ldap_migrate_users(), and USERINFO_COMNG::save().

00220 {
00221     D(debugf(4,"-->LDAPOBJECT::ADD"));
00222     int ret=0;
00223     SSTRINGS resmsg;
00224 
00225     export_ldif(ldapconf_entry);
00226 
00227     command_line.setfrom(" -a"); // Add
00228     command_line.appendf(" -f %s",ldapconf_entry); // Entry
00229 
00230     /* Run command*/
00231     ret = command ("ldapmodify",resmsg);
00232     
00233     /* Remove tmp entry file */
00234     unlink(ldapconf_entry);
00235 
00236     D(debugf(4,"<--LDAPOBJECT::ADD"));
00237     return ret;
00238 }

PUBLIC int LDAPOBJECT::at_add ( const char * name,
const char * val )
 

LDAPOBJECT.at_add

Add attribute STR value. Should always add a new attr value, even if an attr with same name already exist, to support multivalue fields. (Use at_set to replace attributes.)

Definition at line 833 of file ldap_object.cc.

Referenced by at_set(), LDAPDB::create(), ldap_form_adduser(), ldap_groupadmin(), and search().

00834 {
00835     D(debugf(4,"-->LDAPOBJECT::at_add:str (%s,%s)",name,val));
00836     int ret=0;
00837 
00838     if (strlen(val) == 0) {
00839         D(debugf(4,"<--LDAPOBJECT::at_add:str NULL value - not saved"));
00840         return ret;
00841     }
00842     
00843     // Check for non-ascii char, if attr is not a password field. 
00844     // \bug : total rewrite when we find out how to handle this problem
00845     if (strncasecmp(name,"userpassword",ATTR_VAL_MAX)){ 
00846         str_conv_ascii(val);
00847     }
00848     
00849     SSTRING *it = NULL;
00850     if (it == NULL){
00851         it = new SSTRING("");
00852         atlist.add (it);
00853     }
00854     it->setfrom (name);
00855     // lowering attribute name only, not the value
00856     it->to_lower();
00857 
00858     //! \bug : temporary support for base64 passwords, just to see if it helps.
00859     if (!it->cmp("userpassword"))
00860         it->append(":");
00861     
00862     it->appendf(": %s",val);
00863 
00864     D(debugf(4,"<--LDAPOBJECT::at_add:str (%s)",it->get()));
00865     return ret;
00866 }

PUBLIC int LDAPOBJECT::at_add ( const char * name,
int val )
 

LDAPOBJECT.at_add

Add attribute NUM value. Will not replace existing values.

Definition at line 874 of file ldap_object.cc.

00875 {
00876     D(debugf(4,"-->LDAPOBJECT::at_add:num (%s,%i)",name,val));
00877     int ret=0;
00878 
00879     SSTRING *it = locateassign(name);
00880     if (it == NULL){
00881         it = new SSTRING("");
00882         atlist.add (it);
00883     }
00884 
00885     it->setfrom (name);
00886 
00887     // lowering attribute name only, not the value
00888     it->to_lower();
00889     it->appendf(": %d",val);
00890 
00891     D(debugf(4,"<--LDAPOBJECT::at_set:num (%s)",it->get()));
00892     return ret;
00893 }

PUBLIC int LDAPOBJECT::at_del ( const char * name )
 

LDAPOBJECT.at_del

Delete all instances of the named attribute.

Definition at line 900 of file ldap_object.cc.

Referenced by export_ldif(), and ldap_groupadmin().

00901 {
00902     D(debugf(4,"-->LDAPOBJECT::at_del  (%s)",name));
00903     int ret=0;
00904 
00905     while (1){
00906         SSTRING *it = locateassign (name);
00907         if (it == NULL) break;
00908         ret = atlist.remove_del(it);
00909         D(debugf(3,":at_del: Deleting (%s)",name));
00910     }
00911     
00912     D(debugf(4,"<--LDAPOBJECT::at_del  (%s)",name));
00913     return ret;
00914 }

PUBLIC const char * LDAPOBJECT::at_get ( const char * name )
 

LDAPOBJECT.at_get

Locate the value of the named attribute. Return NULL if the name is not defined. Eventual quote surrounding the value are removed.

Definition at line 620 of file ldap_object.cc.

Referenced by export_confdb(), ldap_form_adduser(), ldap_form_search(), ldap_formclient(), ldap_useredit(), and ldapsearch().

00621 {
00622     return LDAPOBJECT::at_get(name,1);
00623 }

PUBLIC const char * LDAPOBJECT::at_get ( const char * name,
int k )
 

LDAPOBJECT.at_get

Locate the value of the k named attribute. Return NULL if the name is not defined. Eventual quote surrounding the value are removed.

Definition at line 632 of file ldap_object.cc.

00633 {
00634     char tmp[1000];
00635     const char *ret = NULL;
00636     SSTRING *it = locateassign(name,k);
00637 
00638     if (it != NULL){
00639         const char *pt = it->strchr(':');
00640         if (pt != NULL){
00641             ret = pt+2;
00642             if (ret[0] == '"'){
00643                 ret++;
00644                 strncpy (tmp,ret,sizeof(tmp));
00645                 strip_end (tmp);
00646                 int len = strlen(tmp)-1;
00647                 if (len >= 0 && tmp[len] == '"') tmp[len] = '\0';
00648                 ret = tmp;
00649             }
00650         }
00651     }
00652 
00653     D(debugf(6,"LDAPOBJECT::at_get %s: %s\n",name,ret));
00654     return ret;
00655 }

PUBLIC int LDAPOBJECT::at_getall ( const char * name,
SSTRINGS & lst )
 

LDAPOBJECT.at_getall

Copy multiple attribute values from the named attribute to the list. Return number of values found.

Definition at line 663 of file ldap_object.cc.

Referenced by ldap_groupadmin().

00664 {
00665     int num = 0;
00666 
00667     num = locate(name,lst);
00668 
00669     D(debugf(6,"LDAPOBJECT::at_getall name=%s num=$d \n",name,num));
00670     return num;
00671 }

PUBLIC const char * LDAPOBJECT::at_getname ( int num )
 

LDAPOBJECT.at_getname

Return the name of attribute n. Return NULL if not found.

Definition at line 679 of file ldap_object.cc.

00680 {
00681     char name[ATTR_NAME_MAX];
00682     const char *ret = NULL;
00683 
00684     SSTRING *it = atlist.getitem(num);
00685 
00686     if (it != NULL){        
00687         SSTRINGS tb;
00688         str_splitline(it->get(),':',tb); // 2.0
00689         strncpy(name,tb.getitem(0)->get(),ATTR_NAME_MAX);
00690         strip_end(name);
00691         ret = name;
00692     }
00693 
00694     D(debugf(6,"LDAPOBJECT::at_getname %d=%s\n",num,name));
00695     return ret;
00696 }

PUBLIC const char * LDAPOBJECT::at_getval ( int num )
 

LDAPOBJECT.at_getval

Return the value of attribute n. Return NULL if not found.

Definition at line 704 of file ldap_object.cc.

00705 {
00706     char val[ATTR_VAL_MAX];
00707     const char *ret = NULL;
00708 
00709     SSTRING *it = atlist.getitem(num);
00710 
00711     if (it != NULL){        
00712         SSTRINGS tb;
00713         str_splitline(it->get(),':',tb); // 2.0
00714         strncpy(val,tb.getitem(1)->get(),ATTR_VAL_MAX);
00715         strip_end(val);
00716         ret = val;
00717     }
00718 
00719     D(debugf(6,"LDAPOBJECT::at_getval %d=%s\n",num,val));
00720     return ret;
00721 }

PUBLIC int LDAPOBJECT::at_list ( SSTRINGS & lst )
 

LDAPOBJECT.at_list Copy all attribute names to the list. Return number of attributes.

Definition at line 595 of file ldap_object.cc.

Referenced by export_confdb(), and ldapsearch().

00596 {
00597     int n = atlist.getnb();
00598     D(debugf(4,"at_list number of attr=%d\n",n));
00599 
00600     for (int i=0; i<n; i++){
00601         SSTRINGS tb;
00602         if (SSTRING *it = atlist.getitem(i)){
00603             str_splitline(it->get(),':',tb);
00604             if (tb.getitem(0)) {
00605                 lst.add (new SSTRING( tb.getitem(0)->get()) );
00606                 D(debugf(6,"LDAPOBJECT::at_list found attr %s\n",tb.getitem(0)->get()));
00607             }
00608         }
00609     }
00610     return n;
00611 }

PUBLIC int LDAPOBJECT::at_set ( const char * name,
const char * val )
 

LDAPOBJECT.at_set

Update or add an attribute STR value. It will replace any existing value.

Definition at line 729 of file ldap_object.cc.

Referenced by import_confdb(), ldap_form_adduser(), ldap_formclient(), ldap_migrate_users(), ldap_passwd_tool(), ldap_useredit(), and USERINFO_COMNG::save().

00730 {
00731     D(debugf(4,"-->LDAPOBJECT::at_set:str (%s,%s)",name,val));
00732     int ret=0;
00733     
00734     // Don't save empty attributes.
00735     // \bug  - sometimes it should be saved as empty in directroy, sometimes not - 
00736     // we need something to handle both situations  
00737     if (strlen(val) == 0) {
00738         D(debugf(4,"<--LDAPOBJECT::at_set:str NULL value - not saved"));
00739         return 0;
00740     }
00741 
00742     // we must remove extra numbers at the end 
00743     // to manage multi attribute values 
00744     // ie we want to add mail not mail3 or mail5
00745     // The number must be removed before we can search 
00746     // for other dublicates
00747 
00748     char * basename=strdup(name);
00749     char * p=basename+strlen(basename)-1;   
00750     bool multivalue = false;
00751     D(debugf(4,"p=(%c)",*p));
00752     
00753     while (isdigit(*p)) {
00754         D(debugf(4,"at_set multiattribute value %c",*p));
00755         *p = '\0';
00756         p--;
00757         multivalue=true;
00758     }
00759     
00760     // Use at_add instead of at_set if this is multivalue and current index >= 2
00761     if (multivalue) {
00762         at_add(basename,val);
00763         return ret;
00764     }
00765 
00766     D(debugf(4,"str=(%s) p=(%c)",basename,*p));
00767     // Check for non-ascii char, if attr is not a password field. 
00768     // \bug : total rewrite when we find out how to handle this problem
00769     if (strncasecmp(name,"userpassword",ATTR_VAL_MAX)){ 
00770         str_conv_ascii(val);
00771     }
00772 
00773     // Got our real name, now search for duplicates 
00774     SSTRING *it = locateassign(basename);
00775     if (it == NULL){
00776         it = new SSTRING("");
00777         atlist.add (it);
00778     }
00779 
00780     it->setfrom (basename);
00781 
00782     // lowering attribute name only, not the value
00783     // we have to lower because comparing the values with strncasecmp 
00784     // is not enough as the attribute will be indexed by using its name 
00785     // so we would miss it sometimes
00786     it->to_lower();
00787 
00788     // \bug : temporary support for base64 passwords, just to see if it helps.
00789     if (!it->cmp("userpassword"))
00790         it->append(":");
00791 
00792     it->appendf(": %s",val);
00793     it->strip_end(); // Make sure it is clean
00794 
00795     D(debugf(4,"<--LDAPOBJECT::at_set:str (%s)",it->get()));
00796     return ret; 
00797 }

PUBLIC int LDAPOBJECT::at_set ( const char * name,
int val )
 

LDAPOBJECT.at_set

Update or add an attribute NUM value. Will replace existing value.

Definition at line 805 of file ldap_object.cc.

00806 {
00807     D(debugf(4,"-->LDAPOBJECT::at_set:num (%s,%i)",name,val));
00808     int ret=0;
00809 
00810     SSTRING *it = locateassign(name);
00811     if (it == NULL){
00812         it = new SSTRING("");
00813         atlist.add (it);
00814     }
00815     it->setfrom (name);
00816     // lowering attribute name only, not the value
00817     it->to_lower();
00818 
00819     it->appendf(": %d",val);    
00820 
00821     D(debugf(4,"<--LDAPOBJECT::at_set:num (%s)",it->get()));
00822     return ret;
00823 }

PUBLIC int LDAPOBJECT::command ( const char * command,
SSTRINGS & resmsg )
 

LDABOBJECT.command

Build commandline from standard, common, and custom settings. Run the command, and return the results.

Definition at line 148 of file ldap_object.cc.

Referenced by add(), del(), LDAPDB::export_ldif(), LDAPDB::import_ldif(), modify(), search(), search_list(), search_list_val(), search_list_vals(), and set_password().

00149 {
00150     D(debugf(4,"-->LDAPOBJECT::ldap_command: %s",command));
00151     SSTRING parameters;
00152     const char *args;
00153     int ret;    
00154     
00155     /* Directory config */
00156     if (!binddn.is_empty())
00157         parameters.appendf(" -D %s",binddn.get());
00158     if (!bindpw.is_empty()) 
00159         parameters.appendf(" -w %s",bindpw.get());
00160     if (!host.is_empty()) 
00161         parameters.appendf(" -h %s",host.get());
00162     if (!port.is_empty()) 
00163         parameters.appendf(" -p %s",port.get());
00164     
00165     /* Standard args */
00166     parameters.appendf(" %s",mode_openldap_params.get());
00167 
00168     if (!strcmp(command,"ldapsearch")) {
00169         parameters.append(" -LLL"); // Removes comment and version indication (OpenLdap 2.x)
00170         if (!base.is_empty())
00171             parameters.appendf(" -b %s",base.get());
00172     }
00173 
00174     /* OpenLDAP version depended parameters */
00175     if (!mode_openldap_version.cmp("2")) { 
00176         // This is for 2.x
00177         if (command != "ldappasswd"){
00178             // Add LDAP protocol version for this directory
00179             if (this->c_profile->getval("profile","protocol")){
00180                 // User defined
00181                 parameters.appendf(" -P %s",this->c_profile->getval("profile","protocol",mode_ldap_protocol.get()));
00182             }
00183             else{
00184                 // Use default
00185                 parameters.appendf(" -P %s",mode_ldap_protocol.get());
00186             }
00187         }
00188         // Add option to disable SASL with OL-2.x
00189         parameters.append(" -x");
00190     }
00191     
00192     /* Custom args */
00193     if (!command_line.is_empty())
00194         parameters.appendf(" %s",command_line.get());
00195     
00196     /* Filter */
00197     if (!filter.is_empty() && command == "ldapsearch")
00198         parameters.appendf(" %s",filter.get());
00199 
00200     /* Attr */
00201     if (!attr.is_empty() && command == "ldapsearch" )
00202         parameters.appendf(" %s",attr.get());
00203 
00204     args = parameters.get();
00205         
00206 
00207     /* Run command*/
00208     ret = sys_command (command,args,resmsg);
00209 
00210     D(debugf(4,"<--LDAPOBJECT::ldap_command:"));
00211     return ret;
00212 }

PUBLIC int LDAPOBJECT::del ( void )
 

LDAPOBJECT.del Delete object from database

Definition at line 319 of file ldap_object.cc.

Referenced by USERINFO_COMNG::deluser(), ldap_form_edituser(), and ldap_formclient().

00320 {
00321     D(debugf(4,"---LDAPOBJECT::DEL"));
00322     int ret=0;  
00323 
00324     SSTRINGS resmsg;
00325 
00326     command_line.setfrom(dn.get());
00327 
00328     /* Run command*/
00329     ret = command ("ldapdelete",resmsg);
00330 
00331     return ret;
00332 }

PUBLIC int LDAPOBJECT::dn_set ( const char * val )
 

LDAPOBJECT.dn_set

Set dn.

Definition at line 574 of file ldap_object.cc.

00575 {   
00576     int ret=0;
00577 
00578     dn.setfromf("dn: %s",val);
00579     
00580     D(debugf(4,"---LDAPOBJECT::dn_set (%s)",val));
00581     return ret;
00582 }

PUBLIC int LDAPOBJECT::export_confdb ( CONFDB * c_data )
 

LDAPOBJECT.export_confdb

Export data to CONFDB object.

Definition at line 1215 of file ldap_object.cc.

Referenced by ldap_form_edituser().

01216 {
01217     int ret = 0;
01218     const char *prefix;
01219     const char *attr;
01220     SSTRING attrk;
01221     const char *val;
01222 
01223     SSTRINGS at_names;
01224 
01225     prefix = at_get(primary_key.get());
01226     int n = at_list(at_names); // Get all attribute names
01227 
01228     D(debugf(4,"LDAPOBJECT::export_confdb prefix=%s attrcount=%d\n",prefix,n));
01229 
01230     for (int i=0; i<n; i++){
01231         if (at_names.getitem(i)==NULL) continue;  //uncorrect result , let's try the next one
01232         attr = at_names.getitem(i)->get(); // Get attribute key
01233         const char * save_attr=attr;
01234         int k=0;
01235         c_data->removeall(prefix,attr);   // Reset item
01236         while (k++<1000) { // avoid infinite loop (should not happen !) 
01237             attr=save_attr;
01238             val = at_get(attr,k);                  // Get k  attribute value
01239             if (!val) break ; //exit the loop if no more value for this attribute
01240             if (k>1) { // multivalue attribute
01241                 // trick :
01242                 // we will use mail2 mail3   instead of mail
01243                 attrk.setfrom(attr);
01244                 attrk.appendf("%d",k);
01245                 attr=attrk.get(); // will be restored with save_attr
01246             }
01247             c_data->add(prefix,attr,val);     // Add item
01248             D(debugf(6,"LDAPOBJECT::export k=%d %s.%s=%s\n",k,prefix,attr,val));
01249         }
01250     }
01251     return ret;
01252 }

PUBLIC int LDAPOBJECT::export_ldif ( const char * path )
 

LDAPOBJECT.export_ldif Write object data to file in ldif format

Definition at line 273 of file ldap_object.cc.

Referenced by add(), and modify().

00274 {
00275     int ret=0;
00276 
00277     /* Remove creator/modifier stamps */
00278     at_del("modifytimestamp");
00279     at_del("modifiersname");
00280     at_del("createtimestamp");
00281     at_del("creatorsname");
00282 
00283     // Remove duplicates
00284     // \bug : Doing this will bypass the problem with duplicates of some entries (eg mail2),
00285     // but it will also hide the cause of the real problem - why/where do the duplicates come from?
00286     // it does not work , I think the problem is the file modify just add things but never tell openldap to remove attributes so they become duplicates !
00287     atlist.remove_dups();
00288     oclist.remove_dups();
00289 
00290     /* Build dataentry */
00291     FILE *fout = fopen (path,"w");
00292     if (fout != NULL){
00293         fprintf (fout,"%s\n",dn.get());
00294         D(debugf(4,"LDIF: %s",dn.get()));
00295 //write objectclass before attributes
00296 
00297         for (int i=0; i<oclist.getnb(); i++){
00298             fprintf (fout,"%s\n",oclist.getitem(i)->get());
00299             D(debugf(4,"LDIF: %s",oclist.getitem(i)->get()));
00300         }
00301 
00302         for (int i=0; i<atlist.getnb(); i++){
00303             fprintf (fout,"%s\n",atlist.getitem(i)->get());
00304             D(debugf(4,"LDIF: %s",atlist.getitem(i)->get()));
00305         }
00306 
00307         fclose (fout);
00308     }
00309 
00310     return ret;
00311 }

PUBLIC int LDAPOBJECT::get_free_gid ( void )
 

Return next free gid to be used by a new object

Definition at line 1312 of file ldap_object.cc.

01313 {
01314     int gid = 999;
01315 
01316     return gid;
01317 }

PUBLIC int LDAPOBJECT::get_free_uid ( void )
 

Return free uid to available for used by new object

Definition at line 1302 of file ldap_object.cc.

01303 {
01304     int uid = 999;
01305 
01306     return uid;
01307 }

PUBLIC int LDAPOBJECT::import_confdb ( CONFDB * c_data )
 

LDAPOBJECT.import_confdb

Import data from CONFDB object.

Definition at line 1259 of file ldap_object.cc.

Referenced by ldap_form_edituser(), and USERINFO_COMNG::save().

01260 {
01261     D(debugf(4,"-->LDAPOBJECT::import_confdb"));
01262     int ret = 0;    
01263     const char *bkey;
01264     const char *key;
01265     const char *val;
01266 
01267     int n = c_data->getnb();
01268 
01269     // Reset/replace all existing attributes before import.
01270     atlist.remove_all();
01271 
01272     // Get all keys from confdb and register their data inside ldapobject 
01273     for (int i=0; i<n; i++){
01274         SSTRINGS tb;
01275 
01276         // Get the complet key
01277         const char *fullkey = c_data->getitem(i)->key.get(); // Get full key
01278 
01279         // Split key in minor and major
01280         str_splitline(fullkey,'.',tb);
01281         bkey = tb.getitem(0)->get();
01282         key = tb.getitem(1)->get();
01283         if (key == NULL)
01284             key = bkey;
01285         
01286         // Get the value
01287         val = c_data->getitem(i)->val.get(); 
01288 
01289         // Add attribute
01290         at_set(key,val); 
01291 
01292         D(debugf(4,":import_confdb: %s.%s=%s",bkey,key,val));
01293     }
01294 
01295     D(debugf(4,"<--LDAPOBJECT::import_confdb:"));
01296     return ret;
01297 }

PUBLIC void LDAPOBJECT::init ( void )
 

Definition at line 69 of file ldap_object.cc.

Referenced by LDAPOBJECT().

00070 {
00071     D(debugf(4,"---LDAPOBJECT::init"));
00072 
00073     // We assemble a unique name for the temporary ldif file
00074     static int instance=0;
00075     char buf[PATH_MAX];
00076     snprintf (buf,PATH_MAX-1,"/var/run/ldapconf.entry.%d.%d",getpid(),instance++);
00077     ldapconf_entry = strdup(buf);
00078     this->form = NULL;
00079     reset();
00080 }

PUBLIC int LDAPOBJECT::ldif_splitline ( const char * ldif,
char * name,
char * val )
 

LDAPOBJECT.ldif_splitline

Split ldif formatted stringline into attribute and value components.

Definition at line 1093 of file ldap_object.cc.

Referenced by ldap_userlist(), search(), search_list_val(), and search_list_vals().

01094 { 
01095     char *pt;
01096 
01097     pt = strchr(ldif,':');
01098 
01099     if (pt != NULL) {
01100         // Extract name
01101         strncpy(name,ldif,ATTR_NAME_MAX);
01102         pt = strchr(name,':');
01103         *pt = '\0';
01104         strip_end(name);
01105         
01106         // Extract value
01107         pt = strchr(ldif,':');
01108         pt +=2;
01109         strncpy(val,pt,ATTR_VAL_MAX);
01110         strip_end(val);
01111         
01112         return 0;
01113     }
01114     else return -1;
01115 }

PUBLIC int LDAPOBJECT::load_profile ( const char * profile_name )
 

Load LDAP configuration from CONFDB profile object

Definition at line 1129 of file ldap_object.cc.

Referenced by LDAPOBJECT(), LDAPDB::create(), LDAPDB::export_ldif(), LDAPDB::import_ldif(), and ldap_groupadmin().

01130 {
01131     D(debugf(4,"LDAPOBJECT::load_profile(%s)",profile_name));
01132 
01133     int ret = -1;
01134     
01135     CONFDB c_ldapconf(f_ldapconf);
01136 
01137     char fpath[PATH_MAX]; 
01138     /* Load profile */
01139     sprintf(fpath,"%s/%s",PROFILE_DIR,profile_name);
01140     CONFIG_FILE f_profile (fpath,help_ldap
01141                            ,CONFIGF_MANAGED|CONFIGF_OPTIONAL
01142                            ,"root","root",0600
01143                            ,subsys_ldap);   
01144     c_profile = new CONFDB (f_profile);
01145 
01146     this->bindname = c_profile->getval ("profile","bind","formclient");
01147     this->formname = c_profile->getval ("profile","form","formclient");
01148     this->dn_prefix = c_profile->getval ("profile","prefix","ou=People");
01149     this->group_prefix = c_profile->getval ("profile","groupprefix","ou=Group");
01150     this->primary_key = c_profile->getval ("profile","primarykey","uid");
01151     this->crypt_hash = c_profile->getval ("profile","crypt_hash","");
01152 
01153     /* Load binding */
01154     sprintf (fpath,"%s/%s",DBBIND_DIR,this->bindname.get());
01155     CONFIG_FILE f_bind (fpath,help_ldap
01156                         ,CONFIGF_MANAGED|CONFIGF_OPTIONAL
01157                         ,"root","root",0600
01158                         ,subsys_ldap);
01159     CONFDB c_bind (f_bind); 
01160 
01161     /* Set default binding */
01162     this->host = c_bind.getval("ldap","host");
01163     this->base = c_bind.getval("ldap","base");
01164     this->binddn = c_bind.getval("ldap","binddn");
01165     this->bindpw = c_bind.getval("ldap","bindpw");
01166 
01167     /* Load form */
01168     sprintf (fpath,"%s/%s",DBFORM_DIR,this->formname.get());
01169     CONFIG_FILE f_form (fpath,help_ldap
01170                         ,CONFIGF_MANAGED|CONFIGF_OPTIONAL
01171                         ,"root","root",0600
01172                         ,subsys_ldap);  
01173 
01174     if (this->form != NULL) delete this->form;
01175     this->form = new CONFDB(f_form);
01176 
01177     ret = 0;
01178     return ret;
01179 }

PUBLIC int LDAPOBJECT::locate ( const char * key,
SSTRINGS & lst )
 

LDAPOBJECT.locate

Locate all attributes with the given name. Return number found.

Definition at line 1068 of file ldap_object.cc.

Referenced by at_getall().

01069 {
01070     int lenkey = strlen(key);
01071     int ret = 0;
01072     int n = atlist.getnb();
01073     for (int i=0; i<n; i++){
01074         SSTRING *it = atlist.getitem (i);
01075         const char *pt = it->get();
01076         pt = str_skip(pt);
01077         if (strncasecmp(pt,key,lenkey)==0 && pt[lenkey] == ':'){
01078             // Strip the key
01079             pt += strlen(key)+1;
01080             pt = str_skip (pt);
01081             lst.add(new SSTRING (pt));
01082             ret++;
01083         }
01084     }
01085     return ret;
01086 }

PUBLIC SSTRING * LDAPOBJECT::locateassign ( const char * key )
 

LDAPOBJECT.locateassign

Lookup attribute and return in "raw" SSTRING format (name: value) Return NULL if not found.

Definition at line 1025 of file ldap_object.cc.

Referenced by at_add(), at_del(), at_get(), and at_set().

01026 {
01027     return LDAPOBJECT::locateassign (key,1);
01028 }

PUBLIC SSTRING * LDAPOBJECT::locateassign ( const char * key,
int k )
 

LDAPOBJECT.locateassign

Lookup attribute number k and return in "raw" SSTRING format (name: value) Return NULL if not found.

k is >=1 and is the number of the attribute we are looking for 1 means the first 3 means the third.... usefull for example if many mail attributes for the same person

Definition at line 1041 of file ldap_object.cc.

01042 {
01043     int lenkey = strlen(key);
01044     int k2=0;
01045     SSTRING  *ret = NULL;
01046     int n = atlist.getnb();
01047     for (int i=0; i<n; i++){
01048         SSTRING *it = atlist.getitem (i);
01049         const char *pt = it->get();
01050         pt = str_skip(pt);
01051         if (strncasecmp(pt,key,lenkey)==0 && pt[lenkey] == ':'){ // 2.0
01052             k2++;
01053             if (k==k2) {    // k th attribute of value key, this is the good one 
01054                 ret = it;
01055                 break;
01056             }
01057         }
01058     }
01059     return ret;
01060 }

PUBLIC int LDAPOBJECT::modify ( void )
 

LDAPOBJECT.modify Update object to database

Definition at line 246 of file ldap_object.cc.

Referenced by ldap_form_edituser(), ldap_formclient(), ldap_groupadmin(), ldap_passwd_tool(), ldap_useredit(), ldapedit(), and USERINFO_COMNG::save().

00247 {
00248     D(debugf(4,"-->LDAPOBJECT::MODIFY"));
00249     int ret=0;
00250     SSTRINGS resmsg;
00251 
00252     export_ldif(ldapconf_entry);
00253 
00254     command_line.setfrom(" -r");
00255     command_line.appendf(" -f %s",ldapconf_entry);
00256 
00257     /* Run command*/
00258     ret = command ("ldapmodify",resmsg); 
00259     
00260     /* Remove tmp entry file */
00261     unlink(ldapconf_entry);
00262 
00263     D(debugf(4,"<--LDAPOBJECT::MODIFY"));
00264     return ret;
00265 }

PUBLIC int LDAPOBJECT::oc_add ( const char * val )
 

LDAPOBJECT.oc_add

Add named objectclass.

Definition at line 952 of file ldap_object.cc.

Referenced by LDAPDB::create(), ldap_form_adduser(), ldap_formclient(), ldap_migrate_users(), USERINFO_COMNG::save(), and search().

00953 {
00954     int ret=0;
00955     char buf[ATTR_VAL_MAX];
00956 
00957     snprintf(buf,sizeof(buf)-1,"objectClass: %s",val);
00958     oclist.add(new SSTRING (buf));
00959 
00960     // No objectclass need multivalues, 
00961     // so make sure there is only one of each
00962     oclist.remove_dups();
00963 
00964     D(debugf(4,"LDAPOBJECT::oc_add (%s)",buf));
00965     return ret;
00966 }

PUBLIC int LDAPOBJECT::oc_del ( const char * name )
 

LDAPOBJECT.oc_del

Delete named objectclass.

Definition at line 973 of file ldap_object.cc.

00974 {
00975     int ret=0;
00976 
00977     // Locate named class and remove it if found
00978     int n = oclist.lookup(name);
00979     if (n)
00980         ret = oclist.remove(oclist.getitem(n));
00981     else
00982         ret = -1;
00983 
00984     D(debugf(4,"---LDAPOBJECT::oc_del (%s)", name));
00985     return ret;
00986 }

PUBLIC const char * LDAPOBJECT::oc_getval ( int num )
 

LDAPOBJECT.oc_getval

Return the name of objectclass n Return NULL if not found.

Definition at line 994 of file ldap_object.cc.

00995 {
00996     SSTRINGS tb;
00997     const char *val = NULL;
00998 
00999     SSTRING *it = oclist.getitem(num);  
01000 
01001     if (it){        
01002         str_splitline(it->get(),':',tb);
01003         val = tb.getitem(1)->get();
01004     }
01005 
01006     // Remove frontspace
01007     str_skip(val);
01008 
01009     D(debugf(6,"LDAPOBJECT::oc_getval %s\n",val));
01010     return val;
01011 }

PUBLIC int LDAPOBJECT::oc_list ( SSTRINGS & lst )
 

LDAPOBJECT.oc_list

Copy objectclass names to the list. Return number of items found.

Definition at line 928 of file ldap_object.cc.

Referenced by ldapsearch().

00929 {
00930     int n =oclist.getnb();
00931 
00932     for (int i=0; i<n; i++){ 
00933         
00934         SSTRING *it = oclist.getitem(i);
00935         SSTRINGS tb;
00936         const char *name = NULL;
00937 
00938         str_splitline(it->get(),':',tb);
00939         name =  tb.getitem(1)->get();
00940         lst.add ( new SSTRING(name) );
00941         D(debugf(6,"LDAPOBJECT::oc_list %s",name));
00942     }   
00943 
00944     return n;
00945 }

PUBLIC int LDAPOBJECT::reset ( void )
 

LDAPOBJECT.reset Empty the local object

Definition at line 107 of file ldap_object.cc.

Referenced by init().

00108 {
00109     D(debugf(4,"---LDAPOBJECT::reset:"));
00110     reset_data();
00111 
00112     filter = "";
00113     attr = "";
00114     base = "";
00115     binddn = "";
00116     bindpw = "";
00117     host = "";
00118     port = "";
00119 
00120     ldif_format = 0;
00121 
00122     return 0;
00123 }

PUBLIC int LDAPOBJECT::reset_data ( void )
 

LDAPOBJECT.reset_data Empty all attributes and objectclasses

Definition at line 131 of file ldap_object.cc.

Referenced by LDAPDB::create(), ldap_migrate_users(), reset(), search(), search_list(), search_list_val(), and search_list_vals().

00132 {
00133     D(debugf(4,"---LDAPOBJECT::reset_data:"));
00134 
00135     atlist.remove_all();
00136     oclist.remove_all();
00137 //  command_line.setfrom("");
00138 
00139     return 0;
00140 }

PUBLIC int LDAPOBJECT::search ( void )
 

LDAPOBJECT.search Search uniq object

Definition at line 340 of file ldap_object.cc.

Referenced by ldap_form_adduser(), ldap_form_edituser(), ldap_form_search(), ldap_formclient(), ldap_groupadmin(), ldap_useredit(), ldapedit(), ldapsearch(), and USERINFO_COMNG::save().

00341 {
00342     D(debugf(4,"-->LDAPOBJECT::SEARCH"));
00343     SSTRINGS data;
00344     char name[ATTR_NAME_MAX];
00345     char val[ATTR_VAL_MAX];
00346 
00347     D(debugf(4,"search filter: %s\n",filter.get()));
00348     reset_data();
00349 
00350     /* Run ldapsearch command*/
00351     command("ldapsearch",data);
00352 
00353     /* Split the objects */
00354 
00355     int c = 1;  /* Line counter */ 
00356     int d;      /* Number of lines */
00357     int idx = 0;    /* Object index number */   
00358     int i = 1; /* Object internal count */
00359     
00360     d = data.getnb();
00361     c = 0;
00362     c++; /* Skip command message */
00363 
00364     while (c < d ) {
00365         SSTRING *s = data.getitem(c);
00366         s->strip_end();
00367 
00368         const char *a = str_skip(s->get());
00369 
00370         if (s->is_empty()) { // Empty line is object seperator
00371             i=0;
00372         } else if (s->strstr("dn:") != 0) { // DN line found
00373             dn.setfrom(s->get());
00374             idx++;
00375             i=1;
00376             D(debugf(6,"search line=%d found %s\n",dn.get()));
00377         } else if (ldif_splitline(a,name,val)==0) {
00378             if (strncasecmp(name,"objectClass",11) == 0) {
00379                 oc_add(val);
00380             }
00381             else{
00382                 at_add(name,val);
00383             }
00384         } else { // Should not happen
00385             D(debugf(4,"search Warning - undecoded ldif line %s\n",s->get()));
00386         }
00387         c++;
00388     }
00389     return idx;
00390 }

PUBLIC int LDAPOBJECT::search_list ( SSTRINGS & lst )
 

LDAPOBJECT.search_list Search and return resultlist

Definition at line 398 of file ldap_object.cc.

Referenced by ldapsearch().

00399 {
00400     D(debugf(4,"-->LDAPOBJECT::SEARCH_LIST"));
00401     int ret=0;
00402     SSTRINGS data;  
00403 
00404     reset_data();
00405 
00406     attr.setfrom("dn");
00407 
00408     ret = command ("ldapsearch",data);
00409 
00410     int c = 0;  /* Line counter */ 
00411     int d = 0;      /* Number of lines */
00412     int idx = 0;    /* Object index number */   
00413     int i = 0; /* Object internal count */
00414 
00415     d = data.getnb();
00416     
00417     c++; // Skip the first line, its the ldapsearch commandline
00418 
00419     while (c < d ) {
00420         SSTRING *s = data.getitem(c);
00421         s->strip_end();
00422 
00423         const char *a = str_skip(s->get());
00424 
00425         if (s->is_empty()) { // Empty line is object seperator
00426             i=0;
00427         } else if (s->strstr("dn:") != NULL) { // DN line found
00428             lst.add(new SSTRING(a));
00429             idx++;
00430             i=1;
00431         } // Skip the rest of this object
00432         c++;
00433     }
00434     return idx;
00435 }

PUBLIC int LDAPOBJECT::search_list_val ( SSTRINGS & lst,
const char * key )
 

LDAPOBJECT.search_list_val Search and return resultlist, where entries are the values of the key attribute only.

Definition at line 443 of file ldap_object.cc.

Referenced by ldap_form_search(), ldap_formclient(), ldap_get_free_uidnumber(), and ldap_groupadmin().

00444 {
00445     D(debugf(4,"-->LDAPOBJECT::SEARCH_LIST_VAL (%s)",key));
00446     int ret=0;
00447     SSTRINGS data;
00448     char name[ATTR_NAME_MAX];
00449     char val[ATTR_VAL_MAX];
00450 
00451     reset_data();
00452 
00453     attr.setfrom(key);
00454 
00455     ret = command ("ldapsearch",data);
00456 
00457     int c = 0;  /* Line counter */ 
00458     int d = 0;      /* Number of lines */
00459     int idx = 0;    /* Object index number */   
00460     int i = 0; /* Object internal count */
00461 
00462     d = data.getnb();
00463 
00464     c++; // Skip the first line, its the ldapsearch commandline
00465 
00466     while (c < d ) {
00467         SSTRING *s = data.getitem(c);
00468         s->strip_end();
00469 
00470         const char *a = str_skip(s->get());
00471 
00472         if (s->is_empty()) { // Empty line is object seperator
00473             i=0;
00474         } else if (s->strstr("dn:") != NULL) { // DN line found
00475             idx++;
00476             i=1;
00477         } else if (i==1 && ldif_splitline(a,name,val)==0) {
00478             lst.add(new SSTRING(val));
00479             i++;
00480         }
00481         c++;
00482     }
00483     return idx;
00484 }

PUBLIC int LDAPOBJECT::search_list_vals ( SSTRINGS & lst,
const char * key,
const char * at_name[] )
 

LDAPOBJECT.search_list_vals Search and return resultlist, where entries are the values of the specified attributes. The key attribute must always be specified.

Definition at line 493 of file ldap_object.cc.

Referenced by ldap_userlist().

00494 {
00495     D(debugf(4,"-->LDAPOBJECT::SEARCH_LIST_VALS (%s)(%s)",key,at_name));
00496     int ret=0;
00497     SSTRINGS data;  
00498     char name[ATTR_NAME_MAX];
00499     char val[ATTR_VAL_MAX];
00500 
00501     reset_data();
00502 
00503     attr.setfrom(key);
00504 
00505     int atc = 0;
00506     while (at_name[atc] != NULL){
00507         attr.appendf(" %s",at_name[atc]);
00508         atc++;
00509     } 
00510     
00511     ret = command ("ldapsearch",data);
00512 
00513     int c = 0;  /* Line counter */ 
00514     int d = 0;      /* Number of lines */
00515     int idx = 0;    /* Object index number */   
00516     int i = 0; /* Object internal count */
00517 
00518     d = data.getnb();
00519 
00520     c++; // Skip the first line, its the ldapsearch commandline
00521 
00522     while (c < d ) {
00523         SSTRING *s = data.getitem(c);
00524         s->strip_end();
00525 
00526         const char *a = str_skip(s->get());
00527 
00528         if (s->is_empty()) { // Empty line is object seperator
00529             i=0;
00530         } else if (s->strstr("dn:") != NULL) { // DN line found
00531             idx++;
00532             i=1;
00533         } else if (i==1 && ldif_splitline(a,name,val)==0) {
00534             lst.add(new SSTRING(a));
00535             i++;
00536         } else if (ldif_splitline(a,name,val)==0) {
00537             lst.getitem(idx-1)->appendf("\t%s",a);
00538             i++;
00539         }
00540 
00541         c++;
00542     }
00543     return idx;
00544 }

PUBLIC int LDAPOBJECT::set_password ( const char * username,
const char * password )
 

Set new password for the named user. Return error message if update failed. (normally caused by denied access)

Definition at line 1188 of file ldap_object.cc.

Referenced by ldap_form_password(), and ldap_pwsync().

01189 {
01190     int ret=0;
01191     SSTRINGS resmsg;
01192 
01193     command_line.setfrom("");
01194     if (!mode_openldap_version.cmp("2")) { // OpenLDAP 2.x
01195         command_line.appendf(" -s %s",password); //2.0 1.0= -e
01196         command_line.appendf(" uid=%s,%s,%s",username,dn_prefix.get(),base.get());
01197     }
01198     else { // OpenLDAP 1.x
01199         if (!crypt_hash.is_empty()) 
01200             command_line.appendf(" -H %s",crypt_hash.get());
01201         command_line.appendf(" -e %s",password);
01202         command_line.appendf(" -t uid=%s,%s,%s",username,dn_prefix.get(),base.get());
01203     }
01204 
01205     ret = command ("ldappasswd",resmsg); 
01206     
01207     return ret;
01208 }


Member Data Documentation

LDAPPROFILE LDAPOBJECT::bind
 

Definition at line 156 of file ldapconf_defs.h.

CONFDB * LDAPOBJECT::form
 

Definition at line 157 of file ldapconf_defs.h.

CONFDB * LDAPOBJECT::c_profile
 

Definition at line 158 of file ldapconf_defs.h.

SSTRING LDAPOBJECT::profile
 

Definition at line 159 of file ldapconf_defs.h.

SSTRING LDAPOBJECT::bindname
 

Definition at line 160 of file ldapconf_defs.h.

SSTRING LDAPOBJECT::formname
 

Definition at line 161 of file ldapconf_defs.h.

SSTRING LDAPOBJECT::dn_prefix
 

Definition at line 162 of file ldapconf_defs.h.

SSTRING LDAPOBJECT::group_prefix
 

Definition at line 163 of file ldapconf_defs.h.

SSTRING LDAPOBJECT::primary_key
 

Definition at line 164 of file ldapconf_defs.h.

SSTRING LDAPOBJECT::crypt_hash
 

Definition at line 165 of file ldapconf_defs.h.

SSTRING LDAPOBJECT::dn
 

Definition at line 166 of file ldapconf_defs.h.

SSTRING LDAPOBJECT::host
 

Definition at line 167 of file ldapconf_defs.h.

SSTRING LDAPOBJECT::port
 

Definition at line 168 of file ldapconf_defs.h.

SSTRING LDAPOBJECT::binddn
 

Definition at line 169 of file ldapconf_defs.h.

SSTRING LDAPOBJECT::bindpw
 

Definition at line 170 of file ldapconf_defs.h.

SSTRING LDAPOBJECT::base
 

Definition at line 171 of file ldapconf_defs.h.

SSTRING LDAPOBJECT::scope
 

Definition at line 172 of file ldapconf_defs.h.

SSTRING LDAPOBJECT::filter
 

Definition at line 173 of file ldapconf_defs.h.

SSTRING LDAPOBJECT::attr
 

Definition at line 174 of file ldapconf_defs.h.

SSTRING LDAPOBJECT::command_line
 

Definition at line 175 of file ldapconf_defs.h.

SSTRINGS LDAPOBJECT::atlist
 

Definition at line 176 of file ldapconf_defs.h.

SSTRINGS LDAPOBJECT::oclist
 

Definition at line 177 of file ldapconf_defs.h.

char LDAPOBJECT::ldif_format
 

Definition at line 178 of file ldapconf_defs.h.

char * LDAPOBJECT::ldapconf_entry
 

Definition at line 179 of file ldapconf_defs.h.

int LDAPOBJECT::last_uid
 

Definition at line 180 of file ldapconf_defs.h.


The documentation for this class was generated from the following files:
Generated at Mon Jan 22 08:35:18 2001 for ldapconf by doxygen1.2.1 written by Dimitri van Heesch, © 1997-2000