/*************************************************************************/ /* LDAPCONF - Linuxconf module for LDAP operation. Copyright (C) 1999,2000,2001 Stein Vråle This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the GNU General Public License for more details. **************************************************************************/ /* LDAPCLIENT.cc FIXME: Most of the dialogs in this file are now more or less obsolete. The dialogs was made during the earlier stages of ldapconf, and are now mostly replaced by the much better "form/profile" system. So most of these dialogs should be removed, or even better, be rewritten using the new form/profile support. But it can't be removed to easily yet, since many of the messages are in use in other files, and probably also some menus. It may also be useful for others to see the different approaches for making ldap dialogs, so the best solution would be to rewrite it. **************************************************************************/ #include "ldapconf_defs.h" #include #include "fields.h" /* LDAPsearch */ void ldapsearch() { LDAPOBJECT lc; /* LDAP client object */ /* default */ int ret = 0; /* Draw dialog */ DIALOG dia; dia.newf_str (MSG_U(F_API_FILTER,"Filter"),lc.filter); dia.newf_str (MSG_U(F_API_ATTR,"Show attributes"),lc.attr); dia.newf_chk (MSG_U(F_LDIF_FORMAT,"LDIF format"),lc.ldif_format,""); dia.newf_title ("",MSG_U(I_LDAPBIND,"Binding")); dia.newf_str (MSG_R(F_API_BASE),lc.base); dia.newf_str (MSG_R(F_API_HOST),lc.host); dia.newf_str (MSG_R(F_API_PORT),lc.port); dia.newf_str (MSG_R(F_API_DN),lc.binddn); dia.newf_str (MSG_R(F_API_PW),lc.bindpw); dia.newf_title ("",MSG_U(I_LDAPOPTIONS,"Results")); dia.newf_str ("DN",lc.dn); dia.setbutinfo (MENU_USR1,MSG_U(B_SEARCH,"Search") ,MSG_U(X_SEARCH,"Search")); SSTRINGS key_list,at_vals,oc_vals; int n = lc.at_list(key_list); dia.newf_title ("","Attributes"); for (int i=0; iget(); at_vals.add( new SSTRING (lc.at_get(key))); dia.newf_str (key_list.getitem(i)->get(),*at_vals.getitem(i)); } n = lc.oc_list(oc_vals); dia.newf_title ("","Objectclass"); for (int i=0; inewf_str (MSG_U(F_EDIT_USER,"Username"),uid); dia->newf_title ("",MSG_R(I_LDAPBIND)); dia->newf_str (MSG_R(F_API_BASE),lc.base); dia->newf_str (MSG_R(F_API_HOST),lc.host); dia->newf_str (MSG_R(F_API_DN),lc.binddn); dia->newf_str (MSG_R(F_API_PW),lc.bindpw); dia->newf_title ("",MSG_R(I_LDAPOPTIONS)); dia->newf_str ("DN",lc.dn); dia->newf_title ("","Attributes"); for (int i=0; inewf_str ("",*lc.atlist.getitem(i)); } dia->newf_title ("","Objectclass"); for (int i=0; inewf_str ("",*lc.oclist.getitem(i)); } /* Wait for dialog */ dia->setbutinfo (MENU_USR1,MSG_R(B_SEARCH) ,MSG_R(X_SEARCH)); dia->setbutinfo (MENU_USR2,MSG_R(B_MOD) ,MSG_R(X_MOD)); MENU_STATUS code = dia->edit (MSG_U(T_LDAP_EDIT,"LDAP editor") ,MSG_U(I_LDAP_EDIT ,"This is a small database LDAP editor") ,help_ldapuser ,nof ,MENUBUT_USR1|MENUBUT_USR2|MENUBUT_CANCEL); if (code == MENU_CANCEL || code == MENU_ESCAPE){ /* Exit */ delete dia; break; } else if (code == MENU_ACCEPT){ /* Save */ dia->save(); } else if (code == MENU_USR1){ /* Search */ dia->save(); char buf[100]; sprintf(buf,"uid=%s",uid.get()); lc.filter.setfrom(buf); ret = lc.search(); } else if (code == MENU_USR2){ dia->save(); ret = lc.modify(); } delete dia; } } /* LDAPCLIENT useredit */ void ldap_useredit() { LDAPOBJECT lc; /* LDAP client object */ int ret = 0; int nof = 0; SSTRING uid=""; SSTRING base=""; SSTRING host=""; SSTRING passwd=""; SSTRING cn=""; SSTRING givenname=""; SSTRING sn=""; SSTRING o=""; SSTRING ou=""; SSTRING mail=""; SSTRING phone=""; SSTRING cellphone=""; SSTRING homephone=""; SSTRING homeurl=""; SSTRING telephonenumber=""; /* Draw dialog */ DIALOG *dia; while (1) { if (lc.at_get("mail") != NULL) { mail.setfrom(lc.at_get("mail")); } if (lc.at_get("givenname") != NULL) { givenname.setfrom(lc.at_get("givenname")); } if (lc.at_get("sn") != NULL) { sn.setfrom(lc.at_get("sn")); } if (lc.at_get("phone") != NULL) { phone.setfrom(lc.at_get("phone")); } if (lc.at_get("cellphone") != NULL) { cellphone.setfrom(lc.at_get("cellphone")); } if (lc.at_get("homephone") != NULL) { homephone.setfrom(lc.at_get("homephone")); } if (lc.at_get("homeurl") != NULL) {homeurl.setfrom(lc.at_get("homeurl")); } if (lc.at_get("telephonenumber") != NULL) { telephonenumber.setfrom(lc.at_get("telephonenumber")); } if (lc.at_get("o") != NULL) { o.setfrom(lc.at_get("o")); } if (lc.at_get("ou") != NULL) {ou.setfrom(lc.at_get("ou")); } dia = new DIALOG; dia->newf_str (MSG_U(F_USERNAME,"Account"),uid); dia->newf_title(MSG_U(F_RESULT,"Data"),1,"",""); dia->newf_str(MSG_U(F_GIVENNAME,"Firstname"),givenname); dia->newf_str(MSG_U(F_SN,"Lastname"),sn); dia->newf_str(MSG_U(F_MAIL,"Email"),mail); dia->newf_str(MSG_U(F_TELEPHONE,"Telephone"),telephonenumber); dia->newf_str(MSG_U(F_PHONE,"Work phone"),phone); dia->newf_str(MSG_U(F_HOMEPHONE,"Home phone"),homephone); dia->newf_str(MSG_U(F_CELLPHONE,"Cellular phone"),cellphone); dia->newf_str(MSG_U(F_HOMEURL,"Homepage URL"),homeurl); dia->newf_str(MSG_U(F_O,"Organization"),o); dia->newf_title(MSG_U(F_BINDING,"Binding"),1,"",""); dia->newf_pass (MSG_U(F_PASSWD,"Password"),lc.bindpw); dia->newf_str (MSG_U(F_SERVER,"Server"),lc.host); dia->newf_str (MSG_U(F_BASE,"Base"),lc.base); /* dia->newf_str(MSG_U(F_OU,"Department"),ou); */ /* for (int i=0; inewf_str ("",*lc.attributes.getitem(i)); } dia->newf_title ("","Objectclass"); for (int i=0; inewf_str ("",*lc.objectclasses.getitem(i)); } */ /* Wait for dialog */ dia->setbutinfo (MENU_USR1,MSG_R(B_SEARCH) ,MSG_R(X_SEARCH)); dia->setbutinfo (MENU_USR2,MSG_U(B_MOD,"Modify") ,MSG_U(X_MOD,"Modify")); MENU_STATUS code = dia->edit (MSG_U(T_USEREDIT,"LDAP userinfo") ,MSG_U(I_USEREDIT ,"Use this dialog to update your personal info") ,help_ldapuser ,nof ,MENUBUT_USR1|MENUBUT_USR2|MENUBUT_CANCEL); if (code == MENU_CANCEL || code == MENU_ESCAPE){ /* Exit */ delete dia; break; } else if (code == MENU_ACCEPT){ /* Save */ dia->save(); } else if (code == MENU_USR1){ /* Search */ dia->save(); char buf[100]; sprintf(buf,"uid=%s",uid.get()); lc.filter.setfrom(buf); ret = lc.search(); } else if (code == MENU_USR2){ dia->save(); lc.binddn=lc.dn; lc.at_set("mail",mail.get()); lc.at_set("givenname",givenname.get()); lc.at_set("sn",sn.get()); lc.at_set("phone",phone.get()); lc.at_set("homephone",homephone.get()); lc.at_set("cellphone",cellphone.get()); lc.at_set("homeurl",homeurl.get()); lc.at_set("telephonenumber",telephonenumber.get()); lc.at_set("o",o.get()); lc.at_set("ou",ou.get()); ret = lc.modify(); } delete dia; } } /* LDAPCLIENT password */ void ldap_passwd_tool() { LDAPOBJECT lc; /* LDAP client object */ int ret = 0; int nof = 0; SSTRING uid=""; SSTRING base=""; SSTRING host=""; SSTRING passwd=""; SSTRING passwd2=""; /* Draw dialog */ DIALOG *dia; dia = new DIALOG; dia->newf_title("",MSG_U(F_PASSWORD_BINDING,"Binding")); dia->newf_str (MSG_U(F_PASSWORD_DN,"Username"),uid); dia->newf_pass (MSG_U(F_PASSWORD_PW1,"Password"),passwd); dia->newf_pass (MSG_U(F_PASSWORD_PW2,"Repeat"),passwd2); dia->newf_str (MSG_U(F_PASSWORD_SERVER,"Server"),host); dia->newf_str (MSG_U(F_PASSWORD_BASE,"Base"),base); /* Wait for dialog */ MENU_STATUS code = dia->edit (MSG_U(T_PASSWORD,"LDAP passwd") ,MSG_U(I_PASSWORD ,"Use this dialog to update your password") ,help_ldapuser ,nof ,MENUBUT_CANCEL); if (code == MENU_CANCEL || code == MENU_ESCAPE){ /* Exit */ delete dia; return; } else if (code == MENU_ACCEPT){ /* Save */ dia->save(); lc.binddn=lc.dn; lc.at_set("UserPasswd",passwd.get()); ret = lc.modify(); } delete dia; } /* LDAPCLIENT formclient */ void ldap_formclient(const char *profile_name) { SSTRING form_key; int nof = 0; CONFDB c_ldapconf(f_ldapconf); char fpath[PATH_MAX]; /* Load profile */ sprintf(fpath,"%s/%s",PROFILE_DIR,profile_name); CONFIG_FILE f_profile (fpath,help_ldap ,CONFIGF_MANAGED|CONFIGF_OPTIONAL ,"root","root",0600 ,subsys_ldap); CONFDB c_profile (f_profile); SSTRING bind_name = c_profile.getval ("profile","bind","formclient"); SSTRING form_name = c_profile.getval ("profile","form","formclient"); SSTRING dn_prefix = c_profile.getval ("profile","prefix","ou=People"); SSTRING primary_key = c_profile.getval ("profile","primarykey","uid"); // char user_access = c_profile.getvalnum ("profile","access",0); /* Load binding */ sprintf (fpath,"%s/%s",DBBIND_DIR,bind_name.get()); CONFIG_FILE f_bind (fpath,help_ldap ,CONFIGF_MANAGED|CONFIGF_OPTIONAL ,"root","root",0600 ,subsys_ldap); CONFDB c_bind (f_bind); /* Load form */ sprintf (fpath,"%s/%s",DBFORM_DIR,form_name.get()); CONFIG_FILE f_form (fpath,help_ldap ,CONFIGF_MANAGED|CONFIGF_OPTIONAL ,"root","root",0600 ,subsys_ldap); CONFDB c_form (f_form); /* Set default binding */ SSTRING bindhost = c_bind.getval("ldap","host"); SSTRING bindbase = c_bind.getval("ldap","base"); SSTRING binddn = c_bind.getval("ldap","binddn"); SSTRING bindpw; // bindpw = c_bind.getval("ldap","bindpw"); DIALOG dia; dia.newf_title ("","Profile"); dia.newf_title ("",profile_name); /* Form key */ dia.newf_str (MSG_R(I_FORM_KEY),form_key); /* Form data */ dia.newf_title (MSG_R(I_FORM_DATA),1,"",MSG_R(I_FORM_DATA)); dia.newf_title ("",form_name.get()); FIELD_DEFS defs(&c_form); defs.read(); defs.setupdia(dia); /* Binding */ dia.newf_title(MSG_R(F_BINDING),1,"",MSG_R(F_BINDING)); dia.newf_title ("",bind_name.get()); dia.newf_str (MSG_R(F_API_BASE),bindbase); dia.newf_str (MSG_R(F_API_HOST),bindhost); dia.newf_str (MSG_R(F_API_DN),binddn); dia.newf_pass (MSG_R(F_API_PW),bindpw); /* Extra buttons */ dia.setbutinfo (MENU_USR1,MSG_R(B_SEARCH),MSG_R(X_SEARCH)); dia.setbutinfo (MENU_USR2,MSG_R(B_MOD),MSG_R(X_MOD)); LDAPOBJECT ldap; while (1) { //defs.loadval (ldap,form_key.get()); dia.reload(); MENU_STATUS code = dia.edit (MSG_R(T_FORM_CLIENT) ,MSG_R(I_FORM_CLIENT) ,help_formclient ,nof ,MENUBUT_USR1|MENUBUT_USR2|MENUBUT_CANCEL|MENUBUT_ADD|MENUBUT_DEL); // Save dialog entries dia.save(); /* Update ldap binding */ ldap.host.setfrom(bindhost); ldap.base.setfrom(bindbase); ldap.binddn.setfrom(binddn); if ( bindpw.is_empty() ) { // Use password from profile if not specified by user ldap.bindpw.setfrom(c_bind.getval("ldap","bindpw")); } else { ldap.bindpw.setfrom(bindpw); } /* Refresh dialog */ if (code == MENU_CANCEL || code == MENU_ESCAPE){ /* Exit */ break; } else if (code == MENU_USR1 || code == MENU_ACCEPT){ /* Search */ SSTRINGS dnlist; ldap.filter.setfromf("uid=%s",form_key.get()); int n = ldap.search_list_val(dnlist,"uid"); if (n > 1) { // Multiple results SSTRING choice; ldaplist(dnlist,choice); ldap.base.setfromf("%s,%s",dn_prefix.get(),bindbase.get()); ldap.filter.setfromf("uid=%s",choice.get()); ldap.search(); } ldap.search(); form_key.setfrom(ldap.at_get("uid")); } else if (code == MENU_ADD){ /* Add new entry */ char name[100]; if (dialog_inputbox(MSG_U(T_NEWENTRY,"New entry") ,MSG_U(I_NEWENTRY ,"Enter the value for the key attribute") ,help_formclient,name)==MENU_ACCEPT){ /* Check if this entry exist */ form_key.setfrom(name); ldap.filter.setfromf("%s=%s",primary_key.get(),name); int n = ldap.search(); if ( n < 1) { /* Add new entry */ ldap.dn.setfromf("%s=%s,%s,%s",primary_key.get(),name,dn_prefix.get(),ldap.base.get()); ldap.at_set("uid",name); ldap.at_set("cn",name); // For Netscape search ldap.oc_add("top"); // ldap.oc_add("person"); ldap.oc_add("account"); ldap.oc_add("posixAccount"); ldap.add(); } } else { break; } } else if (code == MENU_USR2){ /* Modify */ /* Check if this entry exist */ ldap.filter.setfromf("%s=%s",primary_key.get(),form_key.get()); int n = ldap.search(); if ( n == 1) { //defs.saveval (ldap,form_key.get()); ldap.modify(); } } else if (code == MENU_DEL){ /* Delete */ ldap.del(); } } }