00001
00002 00003 00004 00005 00006 00007 00008 00009 00010 00011
00012 00013 00014 00015 00016 00017 00018 00019 00020 00021 00022 00023 00024 00025 00026 00027 00028 00029 00030 00031 00032 00033 00034 00035 00036 00037 00038 00039 00040 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 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
00076
00077
00078 PUBLIC void PROFILE::init(){
00079 D(debugf(6,"PROFILE::init \n"));
00080 this->data = NULL;
00081 this->file = NULL;
00082 }
00083
00084
00085
00086
00087 PUBLIC void PROFILE::reset(){
00088 D(debugf(6,"PROFILE::reset \n"));
00089 if (this->file) {
00090 delete(file);
00091 }
00092 00093 00094 00095 00096
00097 this->init();
00098 }
00099
00100 00101 00102 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 00123 00124 00125 00126 00127 00128 00129 00130 00131
00132
00133 PUBLIC PROFILES::PROFILES()
00134 {
00135 }
00136
00137 PUBLIC PROFILES::~PROFILES()
00138 {
00139 this->reset();
00140 }
00141
00142 00143 00144 00145 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 00167 00168 00169 00170
00171
00172 PUBLIC int PROFILES::get_list(SSTRINGS &lst)
00173 {
00174 dir_getfiltered (path.get(),lst,".OLD .rpmorig .rpmsave .rpmnew ~");
00175 lst.sort();
00176 return lst.getnb();
00177 }
00178
00179 00180 00181 00182 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 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 00208 00209 00210
00211 for (int n = 0 ; n < db->getnb(); n++) {
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 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 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 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 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 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
00307
00308 dia.set_menuitem (i,tb.getitem(i)->get(),"");
00309
00310 }
00311 return n;
00312 }
00313
00314 00315 00316 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 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 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 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 00435 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 }