/*************************************************************************/ /* 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. **************************************************************************/ /* PROFILES This interface is intended to provide a generic high-level profile system for linuxconf modules. A profile is defined to be a set of parameter fields for configuring a service or application. The PROFILE object will currently use a CONFDB object to access the parameters for a single configuration. The PROFILES object will be used to access and manage a set of profiles, this may be useful when the user have to change the active configuration frequently, or run several instances of the same service. The goal is to also provide some generic dialogs for managing the PROFILES, like creation/deletion/selection and configuration. At the moment the PROFILES class work somewhat, while the PROFILE class is more like a placeholder for the CONFDB data. Both will be improved to provide a better interface and internal structure, so don't depend too much on the current interface yet. TODO: When/if the PROFILES interface is completed and proven to be useful for a generic profile system, it will probably be taken out of ldapconf and be converted to a generic libmodule, to let it be used by other linuxconf modules. So to keep it generic, we must try to avoid adding any ldap specific code inside this class interface. ***************************************************************************/ #include #include #include #include #include #include "ldapconf_defs.h" #include "profiles.h" #include /* PROFILE */ static const char subsys_profiles[]="profiles"; static LINUXCONF_SUBSYS subb (subsys_profiles ,P_MSG_U(M_PROFILESSUB,"Profiles")); static HELP_FILE help_profiles ("ldapconf","profiles"); PUBLIC PROFILE::PROFILE() { this->init(); } PUBLIC PROFILE::~PROFILE() { this->reset(); } // Make sure the pointers is initilazed to NULL. // FIXME - should be better designed PUBLIC void PROFILE::init(){ D(debugf(6,"PROFILE::init \n")); this->data = NULL; this->file = NULL; } // Free alloceted objects // FIXME - should be better designed PUBLIC void PROFILE::reset(){ D(debugf(6,"PROFILE::reset \n")); if (this->file) { delete(file); } /* if (this->data) { delete(data); } */ this->init(); } /* setfile - Create new profile data object connected to config file. */ PUBLIC int PROFILE::setfile(const char *path) { D(debugf(6,"PROFILE::setfile path=%s \n",path)); int ret = 0; this->reset(); file = new CONFIG_FILE (path, help_profiles ,CONFIGF_MANAGED|CONFIGF_OPTIONAL ,"root","root",0600 ,subsys_profiles); data = new CONFDB(*file); return ret; } /* PROFILES list copy add del choose dia_list */ PUBLIC PROFILES::PROFILES() { } PUBLIC PROFILES::~PROFILES() { this->reset(); } /* Inititziliser setting dirpath for these profiles. FIXME: Should set more initial parameters. */ PUBLIC PROFILES::PROFILES(const char *path) { this->path.setfrom(path); } PUBLIC PROFILES::PROFILES(const char *path,const char *id) { this->path.setfrom(path); this->id.setfrom(id); } PUBLIC void PROFILES::reset(){ this->profile.reset(); } /* Return list of all profiles for this service. Filename/shortname only, so it may be used for picklist etc. FIXME: Should filter out profiles the user dont have access to (can't make it work for some reason) */ PUBLIC int PROFILES::get_list(SSTRINGS &lst) { dir_getfiltered (path.get(),lst,".OLD .rpmorig .rpmsave .rpmnew ~"); // Ignore the specified extensions lst.sort(); return lst.getnb(); } /* Add a profile FIXME: This is just a start. */ PUBLIC int PROFILES::add(const char *id) { int ret = setpath(id); return ret; } /* Add and copy profile */ PUBLIC int PROFILES::add(CONFDB *db) { int ret; char name[PATH_MAX]; if (dialog_inputbox(MSG_U(T_NEWPROFILE,"New profile") ,MSG_U(I_NEWPROFILE,"Enter name for new profile") ,help_profiles,name)==MENU_ACCEPT){ ret = setpath(name); /* if (this->profile.file->exist) // Check if profilename is used ret = -1; else { */ for (int n = 0 ; n < db->getnb(); n++) { // Copy profile CONFOBJ record(*db->getitem(n)); char buf[256]; sprintf(buf,"%s %s",record.key.get(),record.val.get()); this->profile.data->addline(buf); } this->profile.data->save(); xconf_notice(MSG_U(N_PROFILE_CREATED,"Profile created")); ret = 1; } else { xconf_error(MSG_U(E_PROFILE_EXIST,"Profile not created")); ret = 0; } return ret; } /* Delete profile */ PUBLIC int PROFILES::del(){ int ret = 0; if (xconf_delok()){ if (this->profile.file->unlink() != 0) { xconf_error(MSG_U(E_DELPROFILE,"Error deleting profile")); }else{ xconf_notice(MSG_U(N_DELPROFILE,"Profile deleted")); ret = 1; } } return ret; } /* setpath - set profile directory path before opening profile */ PUBLIC int PROFILES::setpath(const char *id) { int ret = 0; char f_path[PATH_MAX]; snprintf(f_path,sizeof(f_path)-1,"%s/%s",this->path.get(),id); ret = this->profile.setfile(f_path); profile.id.setfrom(id); return ret; } /* use - will set the current profile to 'id'. */ PUBLIC int PROFILES::use(const char *id) { D(debugf(6,"PROFILES::use id=%s \n",id)); int ret = setpath(id); return ret; } /* get - Return data object for current profile. */ PUBLIC CONFDB *PROFILES::get(const char *id) { CONFDB *ret; this->setpath(id); ret = profile.data; return ret; } /* dia_list - Add profile list to dialog object */ PUBLIC int PROFILES::dia_list(DIALOG &dia,SSTRINGS &tb) { int n = get_list(tb); for (int i=0; iget())) // { dia.set_menuitem (i,tb.getitem(i)->get(),""); // } } return n; } /* dia_select - dialog to select a profile, will return id of selected. */ PUBLIC int PROFILES::dia_select() { int ret = -1; DIALOG_LISTE dia; SSTRINGS tb; dia.newf_head ("",id.get()); int nof = 0; while (1){ dia_list(dia,tb); MENU_STATUS code = dia.editmenu (MSG_U(T_PROFILES_SELECT,"Profiles") ,MSG_U(I_PROFILES_SELECT,"Select profile to use") ,help_profiles ,nof ,0); if (code == MENU_QUIT || code == MENU_ESCAPE){ break; }else if (nof >=0 && nof < tb.getnb()){ profile.id.setfrom(tb.getitem(nof)->get()); this->use(profile.id.get()); ret = nof; break; } } return ret; } /* dia_admin - dialog to select or add a profile. */ void profiles_dia_admin(_F_profiles_dia_admin &c, PROFILES *profs) { glocal SSTRINGS tb; glocal _F_profiles_dia_admin *c = &c; glocal PROFILES *p = profs; (MSG_U(T_PROFILES_ADMIN,"Profiles") ,MSG_U(I_PROFILES_ADMIN,"Select profile to edit, or add a new") ,help_profiles); newf_head (glocal.p->id.get()); addwhat (MSG_U(U_ADDPROF,"a new profile")); sortable(); sortpolicy ("a"); glocal.tb.remove_all(); int n = glocal.p->get_list(glocal.tb); for (int i=0; iget(),""); } glocal.c->editone (glocal.tb.getitem(no)->get()); char name[100]; if (dialog_inputbox(MSG_U(T_PROFILES_ADMIN_ADD,"New profile") ,MSG_U(I_PROFILES_ADMIN_ADD,"Enter name of the new profile") ,help_profiles,name)==MENU_ACCEPT){ glocal.p->add(name); glocal.c->editone (name); } } /* setup_priv - Create privileges for all profiles */ PUBLIC void PROFILES::setup_priv(PRIVILEGES &priv_tb,const char *section) { D(debugf(6,"PROFILES::set_privi section = %s \n",section)); SSTRINGS profiles_tb; priv_tb.remove_all(); int n = this->get_list(profiles_tb); for (int i=0; iget(); char priv_id[PATH_MAX]; sprintf (priv_id,"profile_%s",id); priv_tb.add (new PRIVILEGE (priv_id,id,section)); } } /* lookup_priv - return privilege of profile 'id' */ static PRIVILEGE *lookup_priv(const char *id) { char priv_id[PATH_MAX]; sprintf (priv_id,"profile_%s",id); D(debugf(4,"lookup_priv priv_id = %s \n",priv_id)); return privilege_lookup (priv_id); } /* check_perm - check if user has permission. FIXME - Doesn't work as intended yet. */ int check_perm(const char *id) { D(debugf(6,"PROFILES::perm_check id = %s \n", id)); PRIVILEGE *priv = lookup_priv(id); int i; i = perm_checkpriv(priv); D(debugf(4,"check_perm ret = %i \n",i)); return perm_access(priv,"Sorry"); }