kern_prot.cc

00001 /* 
00002    EyeDB Object Database Management System
00003    Copyright (C) 1994-2008 SYSRA
00004    
00005    EyeDB is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009    
00010    EyeDB is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014    
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with this library; if not, write to the Free Software
00017    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA 
00018 */
00019 
00020 /*
00021    Author: Eric Viara <viara@sysra.com>
00022 */
00023 
00024 #include <eyedbconfig.h>
00025 
00026 #include "kern_p.h"
00027 
00028 namespace eyedbsm {
00029 
00030 const Protection p_all = { ReadAll, WriteAll },
00031   p_none = { ReadNone, WriteNone };
00032 
00033 Status
00034 ESM_protectionDelete(DbHandle const *dbh, Oid const *const oid)
00035 {
00036   DbDescription *vd = dbh->vd;
00037   DbHeader _dbh(vd->dbs_addr);
00038   DbHeader *h = &_dbh;
00039   Status se;
00040   Oid *oid_array, *o;
00041   int i, j;
00042   unsigned int size;
00043 
00044 #undef PR
00045 #define PR "protectionDelete: "
00046   if (!(vd->flags & VOLRW))
00047     return statusMake(WRITE_FORBIDDEN, PR WF, dbh->dbfile);
00048 
00049   Oid prot_lock_oid, prot_list_oid, prot_uid_oid;
00050   x2h_protoids(&prot_lock_oid, &prot_list_oid, &prot_uid_oid, h);
00051   if ( !(se = ESM_objectSizeGet(dbh, &size, LockS, &prot_list_oid, OPDefault)) )
00052     {
00053       oid_array = (Oid *)m_malloc(size);
00054       if ( !(se = ESM_objectRead(dbh, 0, 0, oid_array, LockS, 0, 0,
00055                                 &prot_list_oid, OPDefault)) )
00056         {
00057           Oid xoid;
00058           h2x_oid(&xoid, oid);
00059           for (o = oid_array, i = 0; i < vd->nprot_list; o++, i++)
00060             if (!memcmp(o, &xoid, sizeof(Oid)))
00061               {
00062                 for (j = i; j < vd->nprot_list-1; j++, o++)
00063                   *o = *(o+1);
00064 
00065                 if ( !( se = ESM_objectDelete(dbh, &prot_list_oid,
00066                                              OPDefault)) )
00067                   {
00068                     if (--vd->nprot_list > 0)
00069                       ESM_objectCreate(dbh, oid_array, size-sizeof(Oid),
00070                                       DefaultDspid, &prot_list_oid, OPDefault);
00071                     else
00072                       prot_list_oid.setUnique(0);
00073                     se = protectionRunTimeUpdate(dbh);
00074                   }
00075                 free(oid_array);
00076                 h2x_protoids(&prot_lock_oid, &prot_list_oid, &prot_uid_oid, h);
00077                 return Success;
00078               }
00079           se = statusMake(PROTECTION_NOT_FOUND, PR "protection not found: '%s'", getOidString(oid));
00080         }
00081       free(oid_array);
00082     }
00083 
00084   return se;
00085 }
00086 
00087 Status
00088 ESM_protectionCreate(DbHandle const *dbh,
00089                     ProtectionDescription const *desc,
00090                     Oid *oid)
00091 {
00092   DbDescription *vd = dbh->vd;
00093   DbHeader _dbh(vd->dbs_addr);
00094   DbHeader *h = &_dbh;
00095   int i, j;
00096   unsigned int size = protectionDescriptionInternalSize(vd->nprot_uid);
00097   DbProtectionDescription *u;
00098   ProtectionDescriptionInternal *dbi, **l;
00099   ProtectionAtom const *v;
00100   Status se;
00101   Oid roid;
00102   
00103 #undef PR
00104 #define PR "protectionCreate: "
00105   if (!(vd->flags & VOLRW))
00106     return statusMake(WRITE_FORBIDDEN, PR WF, dbh->dbfile);
00107 
00108   if (strlen(desc->name) >= sizeof(desc->name))
00109     return statusMake(PROT_NAME_TOO_LONG, PR "protection name is too long: '%s', maximum allowed is `%d'", desc->name, sizeof(desc->name));
00110 
00111   for (l = vd->vol_desc_list, i = 0; i < vd->nprot_list; l++, i++)
00112     if (!strcmp((*l)->name, desc->name))
00113       return statusMake(PROTECTION_DUPLICATE_NAME, PR "protection already exists: '%s'", desc->name);
00114 
00115   Oid prot_lock_oid, prot_list_oid, prot_uid_oid;
00116   x2h_protoids(&prot_lock_oid, &prot_list_oid, &prot_uid_oid, h);
00117   if (se = ESM_objectLock(dbh, &prot_lock_oid,
00118                          (TransactionOP)(OWRITE | LOCKX),
00119                          NULL, NULL))
00120     return se;
00121 
00122   dbi = (ProtectionDescriptionInternal *)m_calloc(size, 1);
00123 
00124   for (v = desc->desc, i = 0; i < desc->nprot; i++, v++)
00125     {
00126       if ( (j = uidIndGet(dbh, v->uid)) == INVALID )
00127         {
00128           free(dbi);
00129           return statusMake(PROTECTION_INVALID_UID, PR "uid is not valid `%d'", v->uid);
00130         }
00131       else
00132         dbi->prot[j] = v->prot;
00133     }
00134 
00135   strcpy(dbi->name, desc->name);
00136   dbi->nprot = desc->nprot;
00137 
00138   if ( !(se = ESM_objectCreate(dbh, dbi, size, DefaultDspid, &roid, OPDefault)) )
00139     {
00140       if ( !(se = ESM_objectCreate(dbh, &roid, sizeof(Oid), DefaultDspid, oid, OPDefault)) )
00141         {
00142           Oid *o;
00143 
00144           se = ESM_objectSizeGet(dbh, &size, LockS, &prot_list_oid, OPDefault);
00145           if (!se)
00146             {
00147               o = (Oid *)m_malloc(size + sizeof(Oid));
00148               if ( (se = ESM_objectRead(dbh, 0, 0, o,
00149                                        LockS, 0, 0,
00150                                        &prot_list_oid, OPDefault)) /*||
00151                                                              (se = ESM_objectDelete(dbh, &h->prot_list_oid, OPDefault)) */)
00152                 {
00153                   free(o);
00154                   return se;
00155                 }
00156             }
00157           else
00158             {
00159               size = 0;
00160               o = (Oid *)m_malloc(sizeof(Oid));
00161             }
00162 
00163 #ifdef ESM_DBG
00164           printf("PROTLIST %d\n", vd->nprot_list);
00165 #endif
00166           o[vd->nprot_list] = *oid;
00167 
00168 #ifdef ESM_DBG
00169           printf("Create %d (%d %d)\n", size + sizeof(Oid), roid.ns,
00170                  roid.unique);
00171 #endif
00172 
00173           if (!prot_list_oid.getUnique())
00174             se = ESM_objectCreate(dbh, o, size + sizeof(Oid), DefaultDspid,
00175                                  &prot_list_oid, OPDefault);
00176           else
00177             {
00178               se = ESM_objectSizeModify(dbh, size + sizeof(Oid),
00179                                        True, &prot_list_oid,
00180                                        OPDefault);
00181               se = ESM_objectWrite(dbh, 0, size + sizeof(Oid), o,
00182                                   &prot_list_oid, OPDefault);
00183             }
00184 
00185           free(o);
00186           vd->nprot_list++;
00187           if (!se)
00188             h2x_protoids(&prot_lock_oid, &prot_list_oid, &prot_uid_oid, h);
00189           return (!se ? protectionRunTimeUpdate(dbh) : se);
00190         }
00191     }
00192 
00193   return se;
00194 }
00195 
00196 Status
00197 ESM_protectionModify(DbHandle const *dbh,
00198                     ProtectionDescription const *desc,
00199                     Oid const *oid)
00200 {
00201   DbDescription *vd = dbh->vd;
00202   //  DbHeader *h = vd->dbs_addr;
00203   int i, j, size = protectionDescriptionInternalSize(vd->nprot_uid);
00204   DbProtectionDescription *u;
00205   ProtectionDescriptionInternal *dbi, **l;
00206   ProtectionAtom const *v;
00207   Status se;
00208   Oid roid, *o;
00209   
00210 #undef PR
00211 #define PR "protectionModify: "
00212   if (!(vd->flags & VOLRW))
00213     return statusMake(WRITE_FORBIDDEN, PR WF, dbh->dbfile);
00214 
00215   if (strlen(desc->name) >= sizeof(desc->name))
00216     return statusMake(PROT_NAME_TOO_LONG, PR "protection name is too long: '%s', maximum allowed is `%d'", desc->name, sizeof(desc->name));
00217 
00218   for (l = vd->vol_desc_list, o = vd->vol_prot_list_oid,
00219          i = 0; i < vd->nprot_list; l++, i++, o++)
00220     if (!memcmp(oid, o, sizeof(Oid)))
00221       {
00222         dbi = (ProtectionDescriptionInternal *)m_calloc(size, 1);
00223 
00224         for (v = desc->desc, i = 0; i < desc->nprot; i++, v++)
00225           {
00226             if ( (j = uidIndGet(dbh, v->uid)) == INVALID )
00227               {
00228                 free(dbi);
00229                 return statusMake_s(PROTECTION_INVALID_UID);
00230               }
00231             else
00232               dbi->prot[j] = v->prot;
00233           }
00234 
00235         strcpy(dbi->name, desc->name);
00236         dbi->nprot = desc->nprot;
00237 
00238         if (!(se = ESM_objectRead(dbh, 0, 0, &roid, LockS, 0, 0, oid, OPDefault)) &&
00239             !(se = ESM_objectDelete(dbh, &roid, OPDefault)) &&
00240             !(se = ESM_objectCreate(dbh, dbi, size, DefaultDspid, &roid, OPDefault)))
00241           se = ESM_objectWrite(dbh, 0, 0, &roid, oid, OPDefault);
00242 
00243         free(dbi);
00244         return (!se ? protectionRunTimeUpdate(dbh) : se);
00245       }
00246 
00247   return statusMake_s(PROTECTION_NOT_FOUND);
00248 }
00249 
00250 Status
00251 protectionGet_realize(DbHandle const *dbh,
00252                          ProtectionDescriptionInternal *src,
00253                          ProtectionDescription **desc)
00254 {
00255   int nprot_uid = dbh->vd->nprot_uid;
00256   ProtectionDescription *dest;
00257   int j;
00258 
00259   dest = *desc = (ProtectionDescription *)
00260     m_calloc(protectionDescriptionSize(nprot_uid), 1);
00261 
00262   strcpy(dest->name, src->name);
00263   dest->nprot = nprot_uid;
00264 
00265   for (j = 0; j < nprot_uid; j++)
00266     {
00267       dest->desc[j].uid = indUidGet(dbh, j);
00268       dest->desc[j].prot = src->prot[j];
00269     }
00270   return Success;
00271 }
00272 
00273 Status
00274 ESM_protectionGetByName(DbHandle const *dbh,
00275                        char const *name, ProtectionDescription **desc,
00276                        Oid *oid)
00277 {
00278   DbDescription *vd = dbh->vd;
00279   //DbHeader *h = vd->dbs_addr;
00280   ProtectionDescriptionInternal **l;
00281   int i;
00282   Oid *o;
00283 
00284   for (l = vd->vol_desc_list, o = vd->vol_prot_list_oid,
00285          i = 0; i < vd->nprot_list; l++, i++, o++)
00286     if (!strcmp((*l)->name, name))
00287       {
00288         *oid = *o;
00289         return protectionGet_realize(dbh, *l, desc);
00290       }
00291 
00292   return statusMake_s(PROTECTION_NOT_FOUND);
00293 }
00294 
00295 Status
00296 ESM_protectionListGet(DbHandle const *dbh,
00297                      Oid **oid, ProtectionDescription ***desc,
00298                       unsigned int *nprot)
00299 {
00300   DbDescription *vd = dbh->vd;
00301   //DbHeader *h = vd->dbs_addr;
00302   ProtectionDescriptionInternal **l;
00303   Oid *o;
00304   int i;
00305 
00306   *oid = (Oid *)m_calloc(sizeof(Oid) * vd->nprot_list, 1);
00307   *desc = (ProtectionDescription **)
00308     m_calloc(sizeof(ProtectionDescription *) * vd->nprot_list, 1);
00309   *nprot = vd->nprot_list;
00310 
00311   for (l = vd->vol_desc_list, o = vd->vol_prot_list_oid,
00312          i = 0; i < vd->nprot_list; l++, i++, o++)
00313     {
00314       (*oid)[i] = *o;
00315       protectionGet_realize(dbh, *l, &((*desc)[i]));
00316     }
00317 
00318   return Success;
00319 }
00320 
00321 Status
00322 ESM_protectionGetByOid(DbHandle const *dbh,
00323                       Oid const *oid,
00324                       ProtectionDescription **desc)
00325 {
00326   DbDescription *vd = dbh->vd;
00327   //DbHeader *h = vd->dbs_addr;
00328   ProtectionDescriptionInternal **l;
00329   int i;
00330   Oid *o;
00331 
00332   for (l = vd->vol_desc_list, o = vd->vol_prot_list_oid,
00333          i = 0; i < vd->nprot_list; l++, i++, o++)
00334     if (oidloc_cmp(oidLocGet(dbh, o), oidLocGet(dbh, oid)) &&
00335         o->getUnique() == oid->getUnique())
00336       return protectionGet_realize(dbh, *l, desc);
00337 
00338   return statusMake_s(PROTECTION_NOT_FOUND);
00339 }
00340 
00341  Status
00342 dbProtectionCheck(DbHandle const *dbh, int flag)
00343 {
00344   DbHeader _dbh(DBSADDR(dbh));
00345   DbHeader *h = &_dbh;
00346   DbDescription *vd = dbh->vd;
00347   //DbHeader *h = vd->dbs_addr;
00348   Status se;
00349   int uid = getUid(0), i;
00350   DbProtectionDescription *u;
00351 
00352   if (vd->suser)
00353     return Success;
00354 
00355   vd->uid = uid;
00356   vd->uid_ind = uidIndGet(dbh, uid);
00357 
00358   if (vd->uid_ind == INVALID)
00359     {
00360       if (x2h_32(_dbh.__guest_uid()) == INVALID_UID)
00361         return statusMake_s(DATABASE_ACCESS_DENIED);
00362       else
00363         {
00364           uid = x2h_32(_dbh.__guest_uid());
00365           vd->uid = uid;
00366           vd->uid_ind = uidIndGet(dbh, uid);
00367         }
00368     }
00369 
00370   for (u = vd->vol_uid, i = 0; i < vd->nprot_uid; i++, u++)
00371     if (u->uid == uid)
00372       {
00373         if (((flag & VOLREAD) && u->prot.r == ReadAll) ||
00374             ((flag & VOLRW) && u->prot.r == ReadAll &&
00375              u->prot.w == WriteAll))
00376           return Success;
00377         else
00378           break;
00379       }
00380 
00381   return statusMake_s(DATABASE_ACCESS_DENIED);
00382 }
00383 
00384 Status
00385 protectionRunTimeUpdate(DbHandle const *dbh)
00386 {
00387   DbDescription *vd = dbh->vd;
00388   DbHeader _dbh(DBSADDR(dbh));
00389   DbHeader *h = &_dbh;
00390   Status se;
00391   unsigned int size;
00392   
00393   Oid prot_lock_oid, prot_list_oid, prot_uid_oid;
00394   x2h_protoids(&prot_lock_oid, &prot_list_oid, &prot_uid_oid, h);
00395 
00396   if (!(se = ESM_objectSizeGet(dbh, &size, LockS, &prot_list_oid, OPDefault)))
00397     {
00398       Oid *o;
00399       ProtectionDescriptionInternal **v;
00400       int i;
00401       Oid roid;
00402       Oid *vol_prot_list_oid;
00403       ProtectionDescriptionInternal **vol_desc_list;
00404 
00405       vol_prot_list_oid = (Oid *)m_malloc(size);
00406       if (se = ESM_objectRead(dbh, 0, 0, vol_prot_list_oid,
00407                              LockS, 0, 0, &prot_list_oid, OPDefault))
00408         return se;
00409         
00410       /*
00411         printf("SIZE %d, size/sizeof(Oid) %d, nprot_list %d\n",
00412         size, size/sizeof(Oid), vd->nprot_list);
00413         */
00414 
00415       vd->nprot_list = size/sizeof(Oid);
00416 
00417       vol_desc_list = (ProtectionDescriptionInternal **)
00418         m_malloc(sizeof(ProtectionDescriptionInternal *) *
00419                  vd->nprot_list);
00420         
00421       for (o = vol_prot_list_oid, v = vol_desc_list,
00422              i = 0; i < vd->nprot_list; o++, v++, i++)
00423         {
00424           if (!(se = ESM_objectRead(dbh, 0, 0, &roid, LockS, 0, 0, o, OPDefault)))
00425             {
00426               *v = (ProtectionDescriptionInternal *)
00427                 m_calloc(protectionDescriptionInternalSize(vd->nprot_uid), 1);
00428               if (se = ESM_objectRead(dbh, 0, 0, *v, LockS, 0, 0, &roid, OPDefault))
00429                 errorInit();
00430               /*
00431                 else
00432                 printf("HAVE READ protection %s\n", (*v)->name);*/
00433             }
00434           else
00435             return se;
00436         }
00437       
00438       if (vd->vol_prot_list_oid)
00439         free(vd->vol_prot_list_oid);
00440       if (vd->vol_desc_list)
00441         free(vd->vol_desc_list);
00442       
00443       vd->vol_prot_list_oid = vol_prot_list_oid;
00444       vd->vol_desc_list = vol_desc_list;
00445       
00446 #ifdef DICO_ALG
00447       qsort(vd->vol_prot_list_oid, vd->nprot_list, sizeof(Oid),
00448             oid_cmp);
00449 #endif
00450     }
00451   //  h2x_protoids(&prot_lock_oid, &prot_list_oid, &prot_uid_oid, h);
00452   return Success;
00453 }
00454               
00455 Status
00456 dbProtectionRunTimeUpdate(DbHandle const *dbh)
00457 {
00458   DbDescription *vd = dbh->vd;
00459   DbHeader _dbh(DBSADDR(dbh));
00460   DbHeader *h = &_dbh;
00461   Status se;
00462   unsigned int size;
00463   
00464   Oid prot_lock_oid, prot_list_oid, prot_uid_oid;
00465   x2h_protoids(&prot_lock_oid, &prot_list_oid, &prot_uid_oid, h);
00466 
00467   if (!(se = ESM_objectSizeGet(dbh, &size, LockS, &prot_uid_oid,
00468                               OPDefault))) {
00469     if (!size) {
00470       free(vd->vol_uid);
00471       vd->nprot_uid = 0;
00472       vd->vol_uid = 0;
00473       return Success;
00474     }
00475 
00476     free(vd->vol_uid);
00477     vd->vol_uid = (DbProtectionDescription *)m_malloc(size);
00478     
00479     vd->nprot_uid = size/sizeof(Oid);
00480     
00481     ESM_objectRead(dbh, 0, 0, vd->vol_uid, LockS, 0, 0, &prot_uid_oid,
00482                   OPDefault);
00483 
00484 #ifdef ESM_DBG
00485     printf("dbProtectionRunTimeUpdate'nprot_uid' %d\n", vd->nprot_uid);
00486     for (int i = 0; i < vd->nprot_uid; i++)
00487       printf("%d: %d %d %d\n", i, vd->vol_uid[i].uid,
00488              vd->vol_uid[i].prot.r, vd->vol_uid[i].prot.w);
00489 #endif
00490   }
00491 
00492   return se;
00493 }
00494               
00495 Status
00496 ESM_protectionsRunTimeUpdate(DbHandle const *dbh)
00497 {
00498   Status s = dbProtectionRunTimeUpdate(dbh);
00499   if (s) return s;
00500   return protectionRunTimeUpdate(dbh);
00501 }
00502 
00503 Status
00504 ESM_dbProtectionGet(DbHandle const *dbh,
00505                     DbProtectionDescription **desc, unsigned int *nprot)
00506 {
00507   DbDescription *vd = dbh->vd;
00508   //DbHeader *h = vd->dbs_addr;
00509   unsigned int size = sizeof(DbProtectionDescription) * vd->nprot_uid;
00510   
00511   *nprot = vd->nprot_uid;
00512   *desc = (DbProtectionDescription *)m_malloc(size);
00513 
00514   memcpy(*desc, vd->vol_uid, size);
00515   return Success;
00516 }
00517 
00518 Status
00519 ESM_dbProtectionAdd(DbHandle const *dbh,
00520                    DbProtectionDescription const *desc, int nprot)
00521 {
00522   DbDescription *vd = dbh->vd;
00523   DbHeader _dbh(DBSADDR(dbh));
00524   DbHeader *h = &_dbh;
00525   Status se;
00526   DbProtectionDescription const *u, *v;
00527   int i, j, n;
00528 
00529   /*
00530     printf("ESM_dbProtectionAdd(Prot %s, %s)\n",
00531     getOidString(&h->prot_uid_oid),
00532     getOidString(&h->prot_list_oid));
00533     */
00534 
00535   if (!(vd->flags & VOLRW))
00536     return statusMake_s(WRITE_FORBIDDEN);
00537 
00538   for (u = desc, i = 0; i < nprot; i++, u++)
00539     for (v = u+1, j = i+1; j < nprot; j++, v++)
00540       if (v->uid == u->uid)
00541         return Success;
00542   
00543   Oid prot_lock_oid, prot_list_oid, prot_uid_oid;
00544   x2h_protoids(&prot_lock_oid, &prot_list_oid, &prot_uid_oid, h);
00545 
00546   if (se = ESM_objectLock(dbh, &prot_lock_oid,
00547                          (TransactionOP)(OWRITE | LOCKX),
00548                          NULL, NULL))
00549     return se;
00550 
00551   if (!prot_uid_oid.getUnique())
00552     {
00553       unsigned int size = sizeof(DbProtectionDescription) * nprot;
00554       if ( !(se = ESM_objectCreate(dbh, desc, size, DefaultDspid,
00555                                   &prot_uid_oid, OPDefault)) )
00556         {
00557           vd->nprot_uid = nprot;
00558           free(vd->vol_uid);
00559           if (!size)
00560             vd->vol_uid = 0;
00561           else
00562             vd->vol_uid = (DbProtectionDescription *)m_malloc(size);
00563           memcpy(vd->vol_uid, desc, size);
00564 #ifdef BUG_2
00565           protectionRunTimeUpdate(dbh);
00566 #endif
00567         }
00568 
00569       h2x_protoids(&prot_lock_oid, &prot_list_oid, &prot_uid_oid, h);
00570 #ifndef BUG_2
00571       protectionRunTimeUpdate(dbh);
00572 #endif
00573       return se;
00574     }
00575   else
00576     {
00577       unsigned int size = sizeof(DbProtectionDescription) * (vd->nprot_uid + nprot);
00578       DbProtectionDescription *vol_uid =
00579         (DbProtectionDescription *)m_malloc(size), *v;
00580 
00581       memcpy(vol_uid, vd->vol_uid, sizeof(DbProtectionDescription) *
00582              vd->nprot_uid);
00583 
00584       n = vd->nprot_uid;
00585 
00586       for (u = desc, i = 0; i < nprot; u++, i++)
00587         {
00588           for (v = vol_uid, j = 0; j < vd->nprot_uid; v++, j++)
00589             if (v->uid == u->uid)
00590               {
00591                 *v = *u;
00592                 break;
00593               }
00594 
00595           if (j == vd->nprot_uid)
00596             vol_uid[n++] = *u;
00597         }
00598 
00599       if (!(se = ESM_objectSizeModify(dbh, size, True, &prot_uid_oid, OPDefault)))
00600         {
00601           if (!(se = ESM_objectWrite(dbh, 0, size, vol_uid, &prot_uid_oid,
00602                                     OPDefault)))
00603             {
00604               //4/10/05
00605               //free(vd->vol_uid);
00606               vd->nprot_uid = n;
00607               free(vd->vol_uid);
00608               vd->vol_uid = vol_uid;
00609             }
00610         }
00611 
00612       protectionRunTimeUpdate(dbh);
00613       return se;
00614     }
00615 }
00616 
00617 Status
00618 ESM_dbProtectionDelete(DbHandle const *dbh,
00619                       int const *uid, int nuid)
00620 {
00621   return statusMake_s(ERROR);
00622 }
00623 
00624 Status
00625 protectionInit(DbHandle const *dbh)
00626 {
00627   Status status;
00628 
00629   DbHeader _dbh(DBSADDR(dbh));
00630   DbHeader *h = &_dbh;
00631   Oid prot_lock_oid, prot_list_oid, prot_uid_oid;
00632   x2h_protoids(&prot_lock_oid, &prot_list_oid, &prot_uid_oid, h);
00633 
00634   status = ESM_objectCreate(dbh, ObjectZero, 0, DefaultDspid,
00635                            &prot_uid_oid, OPDefault);
00636   if (status)
00637     return status;
00638 
00639   status = ESM_objectCreate(dbh, ObjectZero, 0, DefaultDspid,
00640                            &prot_list_oid, OPDefault);
00641   if (status)
00642     return status;
00643 
00644   status = ESM_objectCreate(dbh, ObjectZero, 0, DefaultDspid,
00645                            &prot_lock_oid, OPDefault);
00646   if (!status)
00647     h2x_protoids(&prot_lock_oid, &prot_list_oid, &prot_uid_oid, h);
00648   return status;
00649 }
00650 
00651 const Protection *
00652 protGet(DbHandle const *dbh, Oid const *oid, int uid)
00653 {
00654   DbDescription *vd = dbh->vd;
00655   if (vd->suser || !uid || !oid->getUnique()) /* adds !uid 20/08/97 */
00656     return &p_all;
00657   else
00658     {
00659       Oid *o;
00660       ProtectionDescriptionInternal **v;
00661       ProtectionAtom *u;
00662       //DbHeader *h = vd->dbs_addr;
00663 
00664       /*      printf("protGet(%s)\n", getOidString(oid)); */
00665       if (ESM_protectionsMustUpdate(dbh))
00666         {
00667           printf("MUST update protections\n");
00668           dbProtectionRunTimeUpdate(dbh);
00669           protectionRunTimeUpdate(dbh);
00670         }
00671 
00672 #ifdef DICO_ALG
00673       if (o = (Oid *)bsearch(oid, vd->vol_prot_list_oid, vd->nprot_list,
00674                                 sizeof(Oid), oid_cmp))
00675         return &vd->vol_desc_list[((int)o-(int)vd->vol_prot_list_oid) / 
00676                                  sizeof(Oid)]->prot[j];
00677       return &p_none;
00678 #else
00679       if (vd->vol_prot_list_oid && vd->vol_desc_list)
00680         {
00681           int i, j = uidIndGet(dbh, INVALID);
00682           int cont = 0;
00683           if (j == INVALID)
00684             return &p_none;
00685 
00686           for ( ; cont < 2; cont++)
00687             {
00688               for (o = vd->vol_prot_list_oid, v = vd->vol_desc_list,
00689                      i = 0; i < vd->nprot_list; o++, v++, i++)
00690                 if (oidloc_cmp(oidLocGet(dbh, o), 
00691                                   oidLocGet(dbh, oid)) &&
00692                     o->getUnique() == oid->getUnique())
00693                   return &(*v)->prot[j];
00694 
00695               printf("TRIES TO update protections\n");
00696               dbProtectionRunTimeUpdate(dbh);
00697               protectionRunTimeUpdate(dbh);
00698             }
00699           
00700           return &p_none;
00701         }
00702       else
00703         return &p_all;
00704 #endif
00705     }
00706 }
00707 
00708 Status
00709 ESM_guestUidSet(DbHandle *dbh, int uid)
00710 {
00711   if (!(dbh->vd->flags & VOLRW))
00712     return statusMake_s(WRITE_FORBIDDEN);
00713   else if (uid != INVALID_UID && uidIndGet(dbh, uid) == INVALID)
00714     return statusMake_s(PROTECTION_INVALID_UID);
00715   else
00716     {
00717       DbHeader(DBSADDR(dbh)).__guest_uid() = h2x_32(uid);
00718       return Success;
00719     }
00720 }
00721 
00722 Status
00723 ESM_guestUidGet(DbHandle const *dbh, int *uid)
00724 {
00725   *uid = x2h_32(DbHeader(DBSADDR(dbh)).__guest_uid());
00726   return Success;
00727 }
00728 
00729 static int dbg_uid;
00730 
00731 void
00732 dbg_setuid(int uid)
00733 {
00734   dbg_uid = uid;
00735 }
00736 
00737 int
00738 getUid(DbHandle const *dbh)
00739 {
00740   if (dbh)
00741     return dbh->vd->uid;
00742   else
00743     return (dbg_uid ? dbg_uid : getuid());
00744 }
00745 
00746 int
00747 uidIndGet(DbHandle const *dbh, int uid)
00748 {
00749   if (uid == INVALID)
00750     return dbh->vd->uid_ind;
00751   else
00752     {
00753       DbDescription *vd = dbh->vd;
00754       //DbHeader *h = vd->dbs_addr;
00755 
00756       DbProtectionDescription *u;
00757       int i;
00758 
00759       for (u = vd->vol_uid, i = 0; i < vd->nprot_uid; i++, u++)
00760         if (u->uid == uid)
00761           return i;
00762 
00763       return INVALID;
00764     }
00765 }
00766 
00767 int
00768 indUidGet(DbHandle const *dbh, int ind)
00769 {
00770   DbDescription *vd = dbh->vd;
00771   //DbHeader *h = vd->dbs_addr;
00772 
00773   return vd->vol_uid[ind].uid;
00774 }
00775 
00776 Status
00777 ESM_dbSetuid(DbHandle *dbh, int uid)
00778 {
00779   if (dbh->vd->rsuser)
00780     {
00781       int uid_ind;
00782       if ( (uid_ind = uidIndGet(dbh, uid)) == INVALID )
00783         return statusMake_s(DATABASE_ACCESS_DENIED);
00784       else
00785         {
00786           dbh->vd->uid     = uid;
00787           dbh->vd->uid_ind = uid_ind;
00788           dbh->vd->suser   = False;
00789           return Success;
00790         }
00791     }
00792   else
00793     return statusMake_s(NO_SETUID_PRIVILEGE);
00794 }
00795 
00796 Status
00797 ESM_suserUnset(DbHandle *dbh)
00798 {
00799   dbh->vd->suser = False;
00800   return Success;
00801 }
00802 
00803 unsigned int
00804 ESM_xidGet(DbHandle *dbh)
00805 {
00806   return dbh->vd->xid;
00807 }
00808 
00809 void
00810 ESM_uidSet(DbHandle *dbh, int uid)
00811 {
00812   dbh->vd->uid = uid;
00813   dbh->vd->uid_ind = uidIndGet(dbh, uid);
00814 }
00815 
00816 int
00817 ESM_uidGet(DbHandle *dbh)
00818 {
00819   return dbh->vd->uid;
00820 }
00821 
00822 }

Generated on Mon Dec 22 18:15:57 2008 for eyedb by  doxygen 1.5.3