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

ldapclient.cc

Go to the documentation of this file.
00001 /*************************************************************************/
00002 /*  LDAPCONF - Linuxconf module for LDAP operation.
00003     
00004     Copyright (C) 1999,2000,2001 Stein Vråle <stein@terminator.net>
00005 
00006     This program is distributed in the hope that it will be useful,
00007     but WITHOUT ANY WARRANTY; without even the implied warranty of
00008     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
00009     GNU General Public License for more details.
00010     
00011 **************************************************************************/
00012 /*  LDAPCLIENT.cc
00013 
00014     FIXME: Most of the dialogs in this file are now more or less obsolete.
00015     The dialogs was made during the earlier stages of ldapconf, and are 
00016     now mostly replaced by the much better "form/profile" system. So most of
00017     these dialogs should be removed, or even better, be rewritten using the
00018     new form/profile support. But it can't be removed to easily yet, since
00019     many of the messages are in use in other files, and probably also some 
00020     menus. It may also be useful for others to see the different approaches
00021     for making ldap dialogs, so the best solution would be to rewrite it.
00022 
00023 **************************************************************************/
00024 
00025 #include "ldapconf_defs.h"
00026 #include <confdb.h>
00027 #include "fields.h"
00028 
00029 /*
00030     LDAPsearch
00031 */
00032 
00033 void ldapsearch()
00034 {
00035     
00036     LDAPOBJECT lc; /* LDAP client object */
00037     
00038 /* default */
00039     
00040     int ret = 0;
00041     
00042 /* Draw dialog */
00043     
00044     DIALOG dia;
00045     dia.newf_str (MSG_U(F_API_FILTER,"Filter"),lc.filter);
00046     dia.newf_str (MSG_U(F_API_ATTR,"Show attributes"),lc.attr); 
00047     dia.newf_chk (MSG_U(F_LDIF_FORMAT,"LDIF format"),lc.ldif_format,"");    
00048     dia.newf_title ("",MSG_U(I_LDAPBIND,"Binding"));
00049     dia.newf_str (MSG_R(F_API_BASE),lc.base);
00050     dia.newf_str (MSG_R(F_API_HOST),lc.host);
00051     dia.newf_str (MSG_R(F_API_PORT),lc.port);
00052     dia.newf_str (MSG_R(F_API_DN),lc.binddn);
00053     dia.newf_str (MSG_R(F_API_PW),lc.bindpw);   
00054     dia.newf_title ("",MSG_U(I_LDAPOPTIONS,"Results"));
00055     dia.newf_str ("DN",lc.dn);
00056     dia.setbutinfo (MENU_USR1,MSG_U(B_SEARCH,"Search")
00057                     ,MSG_U(X_SEARCH,"Search"));
00058     SSTRINGS key_list,at_vals,oc_vals;
00059     int n = lc.at_list(key_list);
00060     dia.newf_title ("","Attributes");
00061     for (int i=0; i<n; i++){
00062         const char *key = key_list.getitem(i)->get();
00063         at_vals.add( new SSTRING (lc.at_get(key)));
00064         dia.newf_str (key_list.getitem(i)->get(),*at_vals.getitem(i));
00065     }
00066 
00067     n = lc.oc_list(oc_vals);
00068     dia.newf_title ("","Objectclass");
00069     for (int i=0; i<n; i++){
00070         dia.newf_str ("objectclass",*oc_vals.getitem(i));
00071     }   
00072     int nof = 0;
00073     while (1) {
00074         MENU_STATUS code = dia.edit (MSG_U(T_API_OBJECT,"LDAP database client")
00075                                      ,MSG_U(I_API_OBJECT
00076                                             ,"This is a small database client for testing of your LDAP system.\n It will use the settings from the LDAP client config by default.\n You may specify other settings here if needed.")                
00077                                      ,help_ldap
00078                                      ,nof
00079                                      ,MENUBUT_USR1|MENUBUT_CANCEL);
00080 /* Exit */
00081         if (code == MENU_CANCEL || code == MENU_ESCAPE){
00082             break;
00083         }
00084 /* Save */
00085         else if (code == MENU_ACCEPT){
00086         }
00087 /* Search */
00088         else if (code == MENU_USR1){
00089             dia.save();
00090         
00091             SSTRINGS dnlist;
00092             ret = lc.search_list(dnlist);
00093         
00094             SSTRING choice;
00095             ldaplist(dnlist,choice);
00096             lc.filter.setfrom(choice.get());
00097             ret = lc.search();
00098             dia.reload();
00099             // ldapedit()
00100         }
00101     }
00102 }
00103 
00104 /*
00105 
00106   LDAPedit
00107 
00108 */
00109 void ldapedit()
00110 {
00111     int ret = 0;
00112     int nof = 0;
00113     SSTRING uid="";
00114     LDAPOBJECT lc; /* LDAP client object */
00115     
00116 /* Draw dialog */
00117     DIALOG *dia;
00118     while (1) {
00119         dia = new DIALOG;
00120         dia->newf_str (MSG_U(F_EDIT_USER,"Username"),uid);      
00121         dia->newf_title ("",MSG_R(I_LDAPBIND));
00122         dia->newf_str (MSG_R(F_API_BASE),lc.base);
00123         dia->newf_str (MSG_R(F_API_HOST),lc.host);
00124         dia->newf_str (MSG_R(F_API_DN),lc.binddn);
00125         dia->newf_str (MSG_R(F_API_PW),lc.bindpw);
00126         dia->newf_title ("",MSG_R(I_LDAPOPTIONS));
00127         dia->newf_str ("DN",lc.dn);
00128         dia->newf_title ("","Attributes");
00129         for (int i=0; i<lc.atlist.getnb(); i++){
00130             dia->newf_str ("",*lc.atlist.getitem(i));
00131         }
00132         dia->newf_title ("","Objectclass");
00133         for (int i=0; i<lc.oclist.getnb(); i++){
00134             dia->newf_str ("",*lc.oclist.getitem(i));
00135         }
00136 /* Wait for dialog */
00137         dia->setbutinfo (MENU_USR1,MSG_R(B_SEARCH)
00138                          ,MSG_R(X_SEARCH));
00139         dia->setbutinfo (MENU_USR2,MSG_R(B_MOD)
00140                          ,MSG_R(X_MOD));
00141     
00142         MENU_STATUS code = dia->edit (MSG_U(T_LDAP_EDIT,"LDAP editor")
00143                                       ,MSG_U(I_LDAP_EDIT
00144                                              ,"This is a small database LDAP editor")               
00145                                       ,help_ldapuser
00146                                       ,nof
00147                                       ,MENUBUT_USR1|MENUBUT_USR2|MENUBUT_CANCEL);
00148     
00149         if (code == MENU_CANCEL || code == MENU_ESCAPE){ /* Exit */
00150             delete dia;
00151             break;
00152         } else if (code == MENU_ACCEPT){ /* Save */
00153             dia->save();
00154         } else if (code == MENU_USR1){ /* Search */
00155             dia->save();
00156         
00157             char buf[100];
00158             sprintf(buf,"uid=%s",uid.get());
00159             lc.filter.setfrom(buf);
00160             ret = lc.search();
00161         } else if (code == MENU_USR2){
00162             dia->save();
00163             ret = lc.modify();
00164         }
00165         delete dia;
00166     }
00167 }
00168 
00169 /*
00170 
00171   LDAPCLIENT useredit
00172 
00173 */
00174 void ldap_useredit()
00175 {
00176     LDAPOBJECT lc; /* LDAP client object */
00177     int ret = 0;
00178     int nof = 0;
00179     SSTRING uid="";
00180     SSTRING base="";
00181     SSTRING host="";
00182     SSTRING passwd="";
00183     SSTRING cn="";
00184     SSTRING givenname="";
00185     SSTRING sn="";
00186     SSTRING o="";
00187     SSTRING ou="";
00188     SSTRING mail="";
00189     SSTRING phone="";
00190     SSTRING cellphone="";
00191     SSTRING homephone="";
00192     SSTRING homeurl="";
00193     SSTRING telephonenumber="";
00194     
00195 /* Draw dialog */
00196     DIALOG *dia;
00197     while (1) {
00198         if (lc.at_get("mail") != NULL) { mail.setfrom(lc.at_get("mail")); }
00199         if (lc.at_get("givenname") != NULL) { givenname.setfrom(lc.at_get("givenname")); }
00200         if (lc.at_get("sn") != NULL) { sn.setfrom(lc.at_get("sn")); }
00201         if (lc.at_get("phone") != NULL) { phone.setfrom(lc.at_get("phone")); }
00202         if (lc.at_get("cellphone") != NULL) { cellphone.setfrom(lc.at_get("cellphone")); }
00203         if (lc.at_get("homephone") != NULL) { homephone.setfrom(lc.at_get("homephone")); }
00204         if (lc.at_get("homeurl") != NULL) {homeurl.setfrom(lc.at_get("homeurl")); }
00205         if (lc.at_get("telephonenumber") != NULL) { telephonenumber.setfrom(lc.at_get("telephonenumber")); }
00206         if (lc.at_get("o") != NULL) { o.setfrom(lc.at_get("o")); }
00207         if (lc.at_get("ou") != NULL) {ou.setfrom(lc.at_get("ou")); }
00208     
00209         dia = new DIALOG;
00210         dia->newf_str (MSG_U(F_USERNAME,"Account"),uid);
00211         dia->newf_title(MSG_U(F_RESULT,"Data"),1,"","");
00212         dia->newf_str(MSG_U(F_GIVENNAME,"Firstname"),givenname);
00213         dia->newf_str(MSG_U(F_SN,"Lastname"),sn);
00214         dia->newf_str(MSG_U(F_MAIL,"Email"),mail);
00215         dia->newf_str(MSG_U(F_TELEPHONE,"Telephone"),telephonenumber);
00216         dia->newf_str(MSG_U(F_PHONE,"Work phone"),phone);
00217         dia->newf_str(MSG_U(F_HOMEPHONE,"Home phone"),homephone);
00218         dia->newf_str(MSG_U(F_CELLPHONE,"Cellular phone"),cellphone);
00219         dia->newf_str(MSG_U(F_HOMEURL,"Homepage URL"),homeurl);
00220         dia->newf_str(MSG_U(F_O,"Organization"),o);
00221         dia->newf_title(MSG_U(F_BINDING,"Binding"),1,"","");    
00222         dia->newf_pass (MSG_U(F_PASSWD,"Password"),lc.bindpw);
00223         dia->newf_str (MSG_U(F_SERVER,"Server"),lc.host);
00224         dia->newf_str (MSG_U(F_BASE,"Base"),lc.base);
00225         /* dia->newf_str(MSG_U(F_OU,"Department"),ou); */
00226     
00227         /*
00228           for (int i=0; i<lc.attributes.getnb(); i++){
00229           dia->newf_str ("",*lc.attributes.getitem(i));
00230           }
00231           dia->newf_title ("","Objectclass");
00232           for (int i=0; i<lc.objectclasses.getnb(); i++){
00233           dia->newf_str ("",*lc.objectclasses.getitem(i));
00234           }
00235         */
00236 /* Wait for dialog */
00237         dia->setbutinfo (MENU_USR1,MSG_R(B_SEARCH)
00238                          ,MSG_R(X_SEARCH));
00239         dia->setbutinfo (MENU_USR2,MSG_U(B_MOD,"Modify")
00240                          ,MSG_U(X_MOD,"Modify"));
00241     
00242         MENU_STATUS code = dia->edit (MSG_U(T_USEREDIT,"LDAP userinfo")
00243                                       ,MSG_U(I_USEREDIT
00244                                              ,"Use this dialog to update your personal info")               
00245                                       ,help_ldapuser
00246                                       ,nof
00247                                       ,MENUBUT_USR1|MENUBUT_USR2|MENUBUT_CANCEL);
00248     
00249         if (code == MENU_CANCEL || code == MENU_ESCAPE){ /* Exit */
00250             delete dia;
00251             break;
00252         } else if (code == MENU_ACCEPT){ /* Save */
00253             dia->save();
00254         } else if (code == MENU_USR1){ /* Search */
00255             dia->save();
00256         
00257             char buf[100];
00258             sprintf(buf,"uid=%s",uid.get());
00259             lc.filter.setfrom(buf);
00260             ret = lc.search();
00261         } else if (code == MENU_USR2){
00262             dia->save();
00263             lc.binddn=lc.dn;
00264             lc.at_set("mail",mail.get());
00265             lc.at_set("givenname",givenname.get());
00266             lc.at_set("sn",sn.get());
00267             lc.at_set("phone",phone.get());
00268             lc.at_set("homephone",homephone.get());
00269             lc.at_set("cellphone",cellphone.get());
00270             lc.at_set("homeurl",homeurl.get());
00271             lc.at_set("telephonenumber",telephonenumber.get());
00272             lc.at_set("o",o.get());
00273             lc.at_set("ou",ou.get());
00274             ret = lc.modify();
00275         }
00276         delete dia;
00277     }
00278 }
00279 
00280 /*
00281 
00282   LDAPCLIENT password
00283 
00284 */
00285 void ldap_passwd_tool()
00286 {
00287     LDAPOBJECT lc; /* LDAP client object */
00288     int ret = 0;
00289     int nof = 0;
00290     SSTRING uid="";
00291     SSTRING base="";
00292     SSTRING host="";
00293     SSTRING passwd="";
00294     SSTRING passwd2="";
00295     
00296 /* Draw dialog */
00297     DIALOG *dia;
00298     dia = new DIALOG;
00299     dia->newf_title("",MSG_U(F_PASSWORD_BINDING,"Binding"));
00300     dia->newf_str (MSG_U(F_PASSWORD_DN,"Username"),uid);
00301     dia->newf_pass (MSG_U(F_PASSWORD_PW1,"Password"),passwd);
00302     dia->newf_pass (MSG_U(F_PASSWORD_PW2,"Repeat"),passwd2);
00303     dia->newf_str (MSG_U(F_PASSWORD_SERVER,"Server"),host);
00304     dia->newf_str (MSG_U(F_PASSWORD_BASE,"Base"),base);
00305     
00306 /* Wait for dialog */
00307     MENU_STATUS code = dia->edit (MSG_U(T_PASSWORD,"LDAP passwd")
00308                                   ,MSG_U(I_PASSWORD
00309                                          ,"Use this dialog to update your password")                
00310                                   ,help_ldapuser
00311                                   ,nof
00312                                   ,MENUBUT_CANCEL);
00313     
00314     if (code == MENU_CANCEL || code == MENU_ESCAPE){ /* Exit */
00315         delete dia;
00316         return;
00317     } else if (code == MENU_ACCEPT){ /* Save */
00318         dia->save();
00319         lc.binddn=lc.dn;
00320         lc.at_set("UserPasswd",passwd.get());
00321         ret = lc.modify();
00322     }
00323     delete dia;
00324     
00325 }
00326 
00327 /*
00328 
00329   LDAPsearch
00330 
00331 */
00332 int ldaplist(SSTRINGS&lst,SSTRING&choice)
00333 {
00334     DIALOG_LISTE *dia = NULL;
00335     int nof = 0;
00336     while (1){
00337         if (dia == NULL){
00338             dia = new DIALOG_LISTE;
00339             int n = lst.getnb();
00340             dia->newf_head ("",MSG_U(F_DNLIST,"Search result"));
00341             for (int i=0; i<n; i++){
00342                 dia->new_menuitem (lst.getitem(i)->get(),"");
00343             }
00344         }
00345         MENU_STATUS code = dia->editmenu (MSG_U(T_DNLIST,"Select")
00346                                           ,MSG_U(I_DNLIST,"Select one")
00347                                           ,help_ldapuser
00348                                           ,nof
00349                                           ,0);
00350         if (code == MENU_QUIT || code == MENU_ESCAPE){
00351             break;
00352         }
00353         else if (nof >=0 && nof < lst.getnb()){
00354             choice = lst.getitem(nof)->get();
00355             break;
00356         }
00357     }
00358     delete dia;
00359     return nof;
00360 }
00361 
00362 /*
00363 
00364   LDAPCLIENT formclient
00365 
00366 */
00367 
00368 void ldap_formclient(const char *profile_name)
00369 {
00370     SSTRING form_key;
00371 
00372     int nof = 0;
00373 
00374     CONFDB c_ldapconf(f_ldapconf);
00375 
00376     char fpath[PATH_MAX]; 
00377     /* Load profile */
00378 
00379     sprintf(fpath,"%s/%s",PROFILE_DIR,profile_name);
00380     CONFIG_FILE f_profile (fpath,help_ldap
00381                            ,CONFIGF_MANAGED|CONFIGF_OPTIONAL
00382                            ,"root","root",0600
00383                            ,subsys_ldap);   
00384     CONFDB c_profile (f_profile);
00385 
00386     SSTRING bind_name = c_profile.getval ("profile","bind","formclient");
00387     SSTRING form_name = c_profile.getval ("profile","form","formclient");
00388     SSTRING dn_prefix = c_profile.getval ("profile","prefix","ou=People");
00389     SSTRING primary_key = c_profile.getval ("profile","primarykey","uid");
00390     // char user_access = c_profile.getvalnum ("profile","access",0);
00391 
00392     /* Load binding */
00393 
00394     sprintf (fpath,"%s/%s",DBBIND_DIR,bind_name.get());
00395     CONFIG_FILE f_bind (fpath,help_ldap
00396                         ,CONFIGF_MANAGED|CONFIGF_OPTIONAL
00397                         ,"root","root",0600
00398                         ,subsys_ldap);  
00399     CONFDB c_bind (f_bind);
00400     
00401     /* Load form */
00402 
00403     sprintf (fpath,"%s/%s",DBFORM_DIR,form_name.get());
00404     CONFIG_FILE f_form (fpath,help_ldap
00405                         ,CONFIGF_MANAGED|CONFIGF_OPTIONAL
00406                         ,"root","root",0600
00407                         ,subsys_ldap);  
00408     CONFDB c_form (f_form); 
00409 
00410     /* Set default binding */
00411     SSTRING bindhost = c_bind.getval("ldap","host");
00412     SSTRING bindbase =  c_bind.getval("ldap","base");
00413     SSTRING binddn = c_bind.getval("ldap","binddn");
00414     SSTRING bindpw;
00415 //  bindpw = c_bind.getval("ldap","bindpw");   
00416 
00417     DIALOG dia;
00418     dia.newf_title ("","Profile");
00419     dia.newf_title ("",profile_name);
00420 
00421     /* Form key */
00422     dia.newf_str (MSG_R(I_FORM_KEY),form_key);
00423     /* Form data */
00424     dia.newf_title (MSG_R(I_FORM_DATA),1,"",MSG_R(I_FORM_DATA));
00425     dia.newf_title ("",form_name.get());
00426     FIELD_DEFS defs(&c_form);
00427     defs.read();
00428     defs.setupdia(dia);
00429     /* Binding */
00430     dia.newf_title(MSG_R(F_BINDING),1,"",MSG_R(F_BINDING));
00431     dia.newf_title ("",bind_name.get());
00432     dia.newf_str (MSG_R(F_API_BASE),bindbase);
00433     dia.newf_str (MSG_R(F_API_HOST),bindhost);
00434     dia.newf_str (MSG_R(F_API_DN),binddn);
00435     dia.newf_pass (MSG_R(F_API_PW),bindpw);
00436     
00437     /* Extra buttons */
00438     dia.setbutinfo (MENU_USR1,MSG_R(B_SEARCH),MSG_R(X_SEARCH));
00439     dia.setbutinfo (MENU_USR2,MSG_R(B_MOD),MSG_R(X_MOD));
00440 
00441     LDAPOBJECT ldap;
00442     while (1) {
00443         //defs.loadval (ldap,form_key.get());
00444         dia.reload();
00445 
00446         MENU_STATUS code = dia.edit (MSG_R(T_FORM_CLIENT)
00447                                      ,MSG_R(I_FORM_CLIENT)              
00448                                      ,help_formclient
00449                                      ,nof
00450                                      ,MENUBUT_USR1|MENUBUT_USR2|MENUBUT_CANCEL|MENUBUT_ADD|MENUBUT_DEL);
00451    
00452         // Save dialog entries
00453         dia.save();
00454 
00455         /* Update ldap binding */
00456         ldap.host.setfrom(bindhost);
00457         ldap.base.setfrom(bindbase);
00458         ldap.binddn.setfrom(binddn);
00459         if ( bindpw.is_empty() ) { // Use password from profile if not specified by user
00460             ldap.bindpw.setfrom(c_bind.getval("ldap","bindpw")); }
00461         else { 
00462             ldap.bindpw.setfrom(bindpw); }
00463 
00464         /* Refresh dialog */
00465         if (code == MENU_CANCEL || code == MENU_ESCAPE){ /* Exit */
00466             break;
00467         } else if (code == MENU_USR1 || code == MENU_ACCEPT){ /* Search */
00468             SSTRINGS dnlist;
00469             ldap.filter.setfromf("uid=%s",form_key.get());
00470             int n = ldap.search_list_val(dnlist,"uid");
00471             if (n > 1) { // Multiple results
00472                 SSTRING choice;
00473                 ldaplist(dnlist,choice);
00474                 ldap.base.setfromf("%s,%s",dn_prefix.get(),bindbase.get());
00475                 ldap.filter.setfromf("uid=%s",choice.get());
00476                 ldap.search();
00477             }
00478             ldap.search();
00479             form_key.setfrom(ldap.at_get("uid"));
00480         } else if (code == MENU_ADD){ /* Add new entry */
00481             char name[100];
00482             if (dialog_inputbox(MSG_U(T_NEWENTRY,"New entry")
00483                                 ,MSG_U(I_NEWENTRY
00484                                        ,"Enter the value for the key attribute")
00485                                 ,help_formclient,name)==MENU_ACCEPT){
00486                 /* Check if this entry exist */
00487                 form_key.setfrom(name);
00488                 ldap.filter.setfromf("%s=%s",primary_key.get(),name);
00489                 int n = ldap.search();
00490                 if ( n < 1) { /* Add new entry */
00491                     ldap.dn.setfromf("%s=%s,%s,%s",primary_key.get(),name,dn_prefix.get(),ldap.base.get());
00492                     ldap.at_set("uid",name);
00493                     ldap.at_set("cn",name); // For Netscape search
00494                     ldap.oc_add("top");
00495 //                  ldap.oc_add("person");
00496                     ldap.oc_add("account");
00497                     ldap.oc_add("posixAccount");
00498                     ldap.add();
00499                 }
00500             } else { 
00501                 break; 
00502             }
00503         } else if (code == MENU_USR2){ /* Modify */
00504             /* Check if this entry exist */
00505             ldap.filter.setfromf("%s=%s",primary_key.get(),form_key.get());
00506             int n = ldap.search();
00507             if ( n == 1) {
00508                 //defs.saveval (ldap,form_key.get());
00509                 ldap.modify();
00510             }
00511         } else if (code == MENU_DEL){ /* Delete */
00512             ldap.del();
00513         }
00514     }
00515 }
00516 
00517 
00518 
00519 
00520 
00521 
00522     

Generated at Mon Jan 22 08:35:11 2001 for ldapconf by doxygen1.2.1 written by Dimitri van Heesch, © 1997-2000