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

profiles.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 /*  PROFILES
00013   
00014     This interface is intended to provide a generic high-level profile 
00015     system for linuxconf modules.
00016 
00017     A profile is defined to be a set of parameter fields for configuring 
00018     a service or application.
00019  
00020     The PROFILE object will currently use a CONFDB object to access the
00021     parameters for a single configuration.
00022 
00023     The PROFILES object will be used to access and manage a set of 
00024     profiles, this may be useful when the user have to change the
00025     active configuration frequently, or run several instances of the same 
00026     service.
00027 
00028     The goal is to also provide some generic dialogs for managing the 
00029     PROFILES, like creation/deletion/selection and configuration. 
00030 
00031     At the moment the PROFILES class work somewhat, while the PROFILE 
00032     class is more like a placeholder for the CONFDB data. Both will be 
00033     improved to provide a better interface and internal structure, so 
00034     don't depend too much on the current interface yet.
00035 
00036     TODO: When/if the PROFILES interface is completed and proven to be 
00037     useful for a generic profile system, it will probably be taken out 
00038     of ldapconf and be converted to a generic libmodule, to let it be 
00039     used by other linuxconf modules. So to keep it generic, we must try 
00040     to avoid adding any ldap specific code inside this class interface.
00041 
00042 ***************************************************************************/
00043 
00044 #include <stdlib.h>
00045 #include <confdb.h>
00046 #include <translat.h>
00047 #include <dialog.h>
00048 #include <userconf.h>
00049 #include "ldapconf_defs.h"
00050 #include "profiles.h" 
00051 
00052 /* 
00053 
00054    PROFILE
00055 
00056 */
00057 
00058 static const char subsys_profiles[]="profiles";
00059 
00060 static LINUXCONF_SUBSYS subb (subsys_profiles
00061                               ,P_MSG_U(M_PROFILESSUB,"Profiles"));
00062 
00063 static HELP_FILE help_profiles ("ldapconf","profiles");
00064 
00065 PUBLIC PROFILE::PROFILE()
00066 {
00067     this->init();
00068 }
00069 
00070 PUBLIC PROFILE::~PROFILE()
00071 {
00072     this->reset();
00073 }
00074 
00075 // Make sure the pointers is initilazed to NULL.
00076 // FIXME - should be better designed
00077 
00078 PUBLIC void PROFILE::init(){
00079     D(debugf(6,"PROFILE::init \n"));
00080     this->data = NULL;
00081     this->file = NULL;
00082 }
00083 
00084 // Free alloceted objects 
00085 // FIXME - should be better designed
00086 
00087 PUBLIC void PROFILE::reset(){
00088     D(debugf(6,"PROFILE::reset \n"));
00089     if (this->file) {
00090         delete(file);
00091     }
00092 /*
00093   if (this->data) {
00094   delete(data);
00095   }
00096 */
00097     this->init();
00098 }
00099 
00100 /*
00101   
00102   setfile - Create new profile data object connected to config file.
00103 
00104  */
00105 
00106 PUBLIC int PROFILE::setfile(const char *path)
00107 {
00108     D(debugf(6,"PROFILE::setfile path=%s \n",path));    
00109     int ret = 0;
00110     this->reset();
00111     file = new CONFIG_FILE (path,
00112                             help_profiles
00113                             ,CONFIGF_MANAGED|CONFIGF_OPTIONAL
00114                             ,"root","root",0600
00115                             ,subsys_profiles);  
00116     data = new CONFDB(*file);
00117     return ret;
00118 }
00119     
00120 /*
00121 
00122   PROFILES
00123 
00124   list
00125   copy
00126   add
00127   del
00128   choose
00129   dia_list
00130 
00131  */
00132 
00133 PUBLIC PROFILES::PROFILES()
00134 {
00135 }
00136 
00137 PUBLIC PROFILES::~PROFILES()
00138 {
00139     this->reset();
00140 }
00141 
00142 /*
00143 
00144   Inititziliser setting dirpath for these profiles.
00145   FIXME: Should set more initial parameters.
00146 
00147 */
00148 
00149 PUBLIC PROFILES::PROFILES(const char *path)
00150 {
00151     this->path.setfrom(path);
00152 }
00153 
00154 PUBLIC PROFILES::PROFILES(const char *path,const char *id)
00155 {
00156     this->path.setfrom(path);
00157     this->id.setfrom(id);
00158 }
00159 
00160 PUBLIC void PROFILES::reset(){
00161     this->profile.reset();
00162 }
00163 
00164 /*
00165 
00166   Return list of all profiles for this service.
00167   Filename/shortname only, so it may be used for picklist etc.
00168   FIXME: Should filter out profiles the user dont have access to (can't make it work for some reason)
00169 
00170 */
00171 
00172 PUBLIC int PROFILES::get_list(SSTRINGS &lst)
00173 {
00174     dir_getfiltered (path.get(),lst,".OLD .rpmorig .rpmsave .rpmnew ~"); // Ignore the specified extensions
00175     lst.sort();
00176     return lst.getnb();
00177 }
00178 
00179 /*
00180 
00181   Add a profile
00182   FIXME: This is just a start.
00183 
00184 */
00185 
00186 PUBLIC int PROFILES::add(const char *id)
00187 {
00188     int ret = setpath(id);
00189     return ret;
00190 }
00191 
00192 /*
00193  
00194    Add and copy profile
00195 
00196 */
00197 
00198 PUBLIC int PROFILES::add(CONFDB *db)
00199 {
00200     int ret;
00201     char name[PATH_MAX];
00202     if (dialog_inputbox(MSG_U(T_NEWPROFILE,"New profile")
00203                         ,MSG_U(I_NEWPROFILE,"Enter name for new profile")
00204                         ,help_profiles,name)==MENU_ACCEPT){     
00205         ret = setpath(name);
00206 /*  
00207     if (this->profile.file->exist) // Check if profilename is used
00208     ret = -1;
00209     else {
00210 */
00211         for (int n = 0 ; n < db->getnb(); n++) { // Copy profile
00212             CONFOBJ record(*db->getitem(n));
00213             char buf[256];
00214             sprintf(buf,"%s %s",record.key.get(),record.val.get());
00215             this->profile.data->addline(buf); 
00216         }
00217         this->profile.data->save();
00218 
00219         xconf_notice(MSG_U(N_PROFILE_CREATED,"Profile created"));
00220         ret = 1;
00221     }
00222     else {
00223         xconf_error(MSG_U(E_PROFILE_EXIST,"Profile not created"));              
00224         ret = 0;
00225     }
00226 
00227     return ret;
00228 }
00229 
00230 /*
00231   
00232   Delete profile
00233 
00234 */
00235 
00236 PUBLIC int PROFILES::del(){
00237     int ret = 0;
00238     if (xconf_delok()){
00239         if (this->profile.file->unlink() != 0) { 
00240             xconf_error(MSG_U(E_DELPROFILE,"Error deleting profile"));
00241         }else{
00242             xconf_notice(MSG_U(N_DELPROFILE,"Profile deleted"));
00243             ret = 1;
00244         }
00245     }
00246     return ret;
00247 }
00248     
00249 /*
00250 
00251   setpath - set profile directory path before opening profile
00252   
00253 */
00254 
00255 PUBLIC int PROFILES::setpath(const char *id)
00256 {
00257     int ret = 0;
00258 
00259     char f_path[PATH_MAX];
00260     snprintf(f_path,sizeof(f_path)-1,"%s/%s",this->path.get(),id);
00261     ret = this->profile.setfile(f_path);
00262     profile.id.setfrom(id);
00263 
00264     return ret;
00265 }
00266 
00267 /*
00268 
00269   use - will set the current profile to 'id'. 
00270 
00271 */
00272 
00273 PUBLIC int PROFILES::use(const char *id)
00274 {
00275     D(debugf(6,"PROFILES::use id=%s \n",id));
00276     int ret = setpath(id);
00277     return ret;
00278 }
00279 
00280 /*
00281 
00282   get - Return data object for current profile.
00283 
00284 */
00285 
00286 PUBLIC CONFDB *PROFILES::get(const char *id)
00287 {
00288     CONFDB *ret;
00289     
00290     this->setpath(id);      
00291     ret = profile.data;
00292 
00293     return ret;
00294 }
00295 
00296 /*
00297 
00298   dia_list - Add profile list to dialog object
00299 
00300 */
00301 
00302 PUBLIC int PROFILES::dia_list(DIALOG &dia,SSTRINGS &tb)
00303 {
00304     int n = get_list(tb);
00305     for (int i=0; i<n; i++){
00306 //      if (check_perm(tb.getitem(i)->get()))
00307 //      {
00308         dia.set_menuitem (i,tb.getitem(i)->get(),"");
00309 //      }
00310     }
00311     return n;
00312 }
00313 
00314 /*
00315 
00316   dia_select - dialog to select a profile, will return id of selected.
00317 
00318 */
00319 
00320 PUBLIC int PROFILES::dia_select()
00321 {
00322     int ret = -1;
00323     DIALOG_LISTE dia;
00324     SSTRINGS tb;
00325     dia.newf_head ("",id.get());
00326     int nof = 0;
00327     
00328     while (1){
00329         dia_list(dia,tb);   
00330 
00331         MENU_STATUS code = dia.editmenu (MSG_U(T_PROFILES_SELECT,"Profiles")
00332                                          ,MSG_U(I_PROFILES_SELECT,"Select profile to use")
00333                                          ,help_profiles
00334                                          ,nof
00335                                          ,0);
00336         if (code == MENU_QUIT || code == MENU_ESCAPE){
00337             break;      
00338         }else if (nof >=0 && nof < tb.getnb()){
00339             profile.id.setfrom(tb.getitem(nof)->get());
00340             this->use(profile.id.get());
00341             ret = nof;
00342             break;
00343         }
00344     }
00345     return ret;
00346 }
00347 
00348 /*
00349 
00350   dia_admin - dialog to select or add a profile.
00351 
00352  */
00353 
00354 
00355 PUBLIC int PROFILES::dia_admin()
00356 {
00357     D(debugf(6,"PROFILES::dia_admin \n"));
00358 
00359     int ret = -1;
00360     DIALOG_LISTE dia;
00361     SSTRINGS tb;
00362     dia.newf_head ("",id.get());
00363     dia_list(dia,tb);
00364     int nof = 0;
00365     
00366     while (1){
00367         MENU_STATUS code = dia.editmenu (MSG_U(T_PROFILES_ADMIN,"Profiles")
00368                                          ,MSG_U(I_PROFILES_ADMIN,"Select profile to edit, or add a new")
00369                                          ,help_profiles
00370                                          ,nof
00371                                          ,MENUBUT_ADD);
00372         if (code == MENU_QUIT || code == MENU_ESCAPE){
00373             ret = -1;
00374             break;      
00375         } else if (code == MENU_ADD ){
00376             char name[100];
00377             if (dialog_inputbox(MSG_U(T_PROFILES_ADMIN_ADD,"New profile")
00378                                 ,MSG_U(I_PROFILES_ADMIN_ADD,"Enter name of the new profile")
00379                                 ,help_profiles,name)==MENU_ACCEPT){
00380                 this->add(name);
00381                 profile.id.setfrom(name);
00382                 this->use(name);
00383                 ret = 0;
00384                 break;
00385             }
00386         }else if (nof >=0 && nof < tb.getnb()){
00387             profile.id.setfrom(tb.getitem(nof)->get());
00388             this->use(profile.id.get());
00389             ret = nof;
00390             break;
00391         }
00392     }
00393     return ret;
00394 }
00395 
00396 /*
00397 
00398   setup_priv - Create privileges for all profiles
00399 
00400 */
00401 
00402 PUBLIC void PROFILES::setup_priv(PRIVILEGES &priv_tb,const char *section)
00403 {
00404     D(debugf(6,"PROFILES::set_privi section = %s \n",section));
00405     SSTRINGS profiles_tb;
00406 
00407     priv_tb.remove_all();
00408     
00409     int n = this->get_list(profiles_tb);
00410     for (int i=0; i<n; i++){
00411         const char *id = profiles_tb.getitem(i)->get();
00412         char priv_id[PATH_MAX];
00413         sprintf (priv_id,"profile_%s",id);
00414         priv_tb.add (new PRIVILEGE (priv_id,id,section));
00415     }
00416 }
00417 
00418 /*
00419 
00420   lookup_priv - return privilege of profile 'id'
00421 
00422 */
00423 
00424 static PRIVILEGE *lookup_priv(const char *id)
00425 {  
00426     char priv_id[PATH_MAX];
00427     sprintf (priv_id,"profile_%s",id);
00428     D(debugf(4,"lookup_priv  priv_id = %s \n",priv_id));
00429     return privilege_lookup (priv_id);
00430 }
00431 
00432 /*
00433 
00434   check_perm - check if user has permission.
00435   FIXME - Doesn't work as intended yet.
00436 
00437  */
00438 
00439 int check_perm(const char *id)
00440 {   
00441     D(debugf(6,"PROFILES::perm_check id = %s  \n", id));
00442     PRIVILEGE *priv = lookup_priv(id);
00443     int i;
00444     i = perm_checkpriv(priv);
00445     D(debugf(4,"check_perm  ret = %i \n",i));
00446     return perm_access(priv,"Sorry");
00447 }

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