Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

unipermgen.cc

Go to the documentation of this file.
00001 /*
00002  * Worldvisions Weaver Software:
00003  *   Copyright (C) 2002 Net Integration Technologies, Inc.
00004  * 
00005  * UniPermGen is a UniConfGen for holding Unix-style permissions
00006  *
00007  */
00008 
00009 #include "unipermgen.h"
00010 #include "unidefgen.h"
00011 
00012 #include "wvmoniker.h"
00013 #include "wvstringlist.h"
00014 #include "wvtclstring.h"
00015 
00016 
00017 UniPermGen::UniPermGen(UniConfGen *_gen) :
00018     UniFilterGen(new UniDefGen(_gen))
00019 {
00020 }
00021 
00022 
00023 UniPermGen::UniPermGen(WvStringParm moniker) :
00024     UniFilterGen(NULL)
00025 {
00026     UniConfGen *gen = wvcreate<UniConfGen>(moniker);
00027     assert(gen && "Moniker doesn't get us a generator!");
00028     setinner(new UniDefGen(gen));
00029 }
00030 
00031 
00032 void UniPermGen::setowner(const UniConfKey &path, WvStringParm owner)
00033 {
00034     inner()->set(WvString("%s/owner", path), owner);
00035 }
00036 
00037 
00038 WvString UniPermGen::getowner(const UniConfKey &path)
00039 {
00040     WvString owner = inner()->get(WvString("%s/owner", path));
00041     if (owner.isnull() && !path.isempty())
00042         owner = getowner(path.removelast());
00043     return owner;
00044 }
00045 
00046 
00047 void UniPermGen::setgroup(const UniConfKey &path, WvStringParm group)
00048 {
00049     inner()->set(WvString("%s/group", path), group);
00050 }
00051 
00052 
00053 WvString UniPermGen::getgroup(const UniConfKey &path)
00054 {
00055     WvString group = inner()->get(WvString("%s/group", path));
00056     if (group.isnull() && !path.isempty())
00057         group = getgroup(path.removelast());
00058     return group;
00059 }
00060 
00061 
00062 void UniPermGen::setperm(const UniConfKey &path, Level level, Type type, bool val)
00063 {
00064     inner()->set(WvString("%s/%s-%s", path, level2str(level), type2str(type)), val);
00065 }
00066 
00067 
00068 bool UniPermGen::getperm(const UniConfKey &path, const Credentials &cred, Type type)
00069 {
00070     WvString owner = getowner(path);
00071     WvString group = getgroup(path);
00072 
00073     Level level;
00074     if (cred.user == owner) level = USER;
00075     else if (cred.groups[group]) level = GROUP;
00076     else level = WORLD;
00077 
00078     bool perm = getoneperm(path, level, type);
00079     return perm;
00080 }
00081 
00082 
00083 bool UniPermGen::getoneperm(const UniConfKey &path, Level level, Type type)
00084 {
00085     int val = str2int(inner()->get(WvString("%s/%s-%s", path, level2str(level),
00086                 type2str(type))), -1);
00087     if (val == -1)
00088     {
00089         if (path.isempty())
00090         {
00091             switch (type)
00092             {
00093                 case READ: return true;
00094                 case WRITE: return false;
00095                 case EXEC: return true; 
00096                 default: assert(false && "Something in the Type enum wasn't covered");
00097             }
00098         }
00099         else
00100             return getoneperm(path.removelast(), level, type);
00101     }
00102     return val;
00103 }
00104 
00105 
00106 void UniPermGen::chmod(const UniConfKey &path, unsigned int user, unsigned int group,
00107         unsigned int world)
00108 {
00109     static const int r = 4;
00110     static const int w = 2;
00111     static const int x = 1;
00112 
00113     setperm(path, USER, READ, (user & r));
00114     setperm(path, USER, WRITE, (user & w));
00115     setperm(path, USER, EXEC, (user & x));
00116 
00117     setperm(path, GROUP, READ, (group & r));
00118     setperm(path, GROUP, WRITE, (group & w));
00119     setperm(path, GROUP, EXEC, (group & x));
00120 
00121     setperm(path, WORLD, READ, (world & r));
00122     setperm(path, WORLD, WRITE, (world & w));
00123     setperm(path, WORLD, EXEC, (world & x));
00124 }
00125 
00126 
00127 void UniPermGen::chmod(const UniConfKey &path, unsigned int mode)
00128 {
00129     unsigned int user =  (mode & 0700) >> 6;
00130     unsigned int group = (mode & 0070) >> 3;
00131     unsigned int world = (mode & 0007);
00132     
00133     chmod(path, user, group, world);
00134 }
00135 
00136 
00137 WvString UniPermGen::level2str(Level level)
00138 {
00139     switch (level)
00140     {
00141     case USER: return "user";
00142     case GROUP: return "group";
00143     case WORLD: return "world";
00144     }
00145     assert(false && "Something in the Level enum wasn't covered");
00146     return WvString::null;
00147 }
00148 
00149 
00150 WvString UniPermGen::type2str(Type type)
00151 {
00152     switch (type)
00153     {
00154     case READ: return "read";
00155     case WRITE: return "write";
00156     case EXEC: return "exec";
00157     }
00158     assert(false && "Something in the Type enum wasn't covered");
00159     return WvString::null;
00160 }

Generated on Wed Dec 15 15:08:10 2004 for WvStreams by  doxygen 1.3.9.1