Make WvStreams compile with gcc 4.4.
[wvstreams.git] / uniconf / unipermgen.cc
blob50743253c3ec799628cc49fa9870b74908235dfc
1 /*
2 * Worldvisions Weaver Software:
3 * Copyright (C) 2002 Net Integration Technologies, Inc.
4 *
5 * UniPermGen is a UniConfGen for holding Unix-style permissions. See
6 * unipermgen.h.
7 */
8 #include "unipermgen.h"
9 #include "unidefgen.h"
10 #include "wvmoniker.h"
11 #include "wvstringlist.h"
12 #include "wvtclstring.h"
13 #include "wvstream.h"
14 #include "wvlinkerhack.h"
16 WV_LINK(UniPermGen);
20 UniPermGen::UniPermGen(IUniConfGen *_gen)
21 : UniFilterGen(_gen)
26 UniPermGen::UniPermGen(WvStringParm moniker)
27 : UniFilterGen(NULL)
29 IUniConfGen *gen = wvcreate<IUniConfGen>(moniker);
30 assert(gen && "Moniker doesn't get us a generator!");
31 setinner(gen);
35 void UniPermGen::setowner(const UniConfKey &path, WvStringParm owner)
37 inner()->set(WvString("%s/owner", path), owner);
41 WvString UniPermGen::getowner(const UniConfKey &path)
43 WvString owner = inner()->get(WvString("%s/owner", path));
45 if (!owner && !path.isempty())
46 owner = getowner(path.removelast());
47 return owner;
51 void UniPermGen::setgroup(const UniConfKey &path, WvStringParm group)
53 inner()->set(WvString("%s/group", path), group);
57 WvString UniPermGen::getgroup(const UniConfKey &path)
59 WvString group = inner()->get(WvString("%s/group", path));
60 if (!group && !path.isempty())
61 group = getgroup(path.removelast());
62 return group;
66 void UniPermGen::setperm(const UniConfKey &path, Level level,
67 Type type, bool val)
69 inner()->set(WvString("%s/%s-%s", path, level2str(level),
70 type2str(type)), val);
74 bool UniPermGen::getperm(const UniConfKey &path, const Credentials &cred,
75 Type type)
77 WvString owner = getowner(path);
78 WvString group = getgroup(path);
80 Level level;
81 if (!!owner && cred.user == owner) level = USER;
82 else if (!!group && cred.groups[group]) level = GROUP;
83 else level = WORLD;
85 bool perm = getoneperm(path, level, type);
86 // wverr->print("getperm(%s/%s, %s/%s, %s,%s-%s) = %s\n",
87 // cred.user, cred.groups.count(), owner, group,
88 // path, level2str(level), type2str(type), perm);
89 return perm;
93 /// retrieve an individual permission. If there's no explicit permission
94 /// of that type set for that object, proceed up the tree so that children
95 /// can inherit permissions from their parents.
96 bool UniPermGen::getoneperm(const UniConfKey &path, Level level, Type type)
98 int val = str2int(inner()->get(WvString("%s/%s-%s", path, level2str(level),
99 type2str(type))), -1);
100 if (val == -1)
102 if (path.isempty())
104 // nothing found: use default
105 switch (type)
107 case READ: return false;
108 case WRITE: return false;
109 case EXEC: return false;
112 else
113 return getoneperm(path.removelast(), level, type);
115 return val;
119 void UniPermGen::chmod(const UniConfKey &path,
120 unsigned int user, unsigned int group,
121 unsigned int world)
123 static const int r = 4;
124 static const int w = 2;
125 static const int x = 1;
127 setperm(path, USER, READ, (user & r));
128 setperm(path, USER, WRITE, (user & w));
129 setperm(path, USER, EXEC, (user & x));
131 setperm(path, GROUP, READ, (group & r));
132 setperm(path, GROUP, WRITE, (group & w));
133 setperm(path, GROUP, EXEC, (group & x));
135 setperm(path, WORLD, READ, (world & r));
136 setperm(path, WORLD, WRITE, (world & w));
137 setperm(path, WORLD, EXEC, (world & x));
141 void UniPermGen::chmod(const UniConfKey &path, unsigned int mode)
143 unsigned int user = (mode & 0700) >> 6;
144 unsigned int group = (mode & 0070) >> 3;
145 unsigned int world = (mode & 0007);
147 chmod(path, user, group, world);
151 WvString UniPermGen::level2str(Level level)
153 switch (level)
155 case USER: return "user";
156 case GROUP: return "group";
157 case WORLD: return "world";
159 assert(false && "Something in the Level enum wasn't covered");
160 return WvString::null;
164 WvString UniPermGen::type2str(Type type)
166 switch (type)
168 case READ: return "read";
169 case WRITE: return "write";
170 case EXEC: return "exec";
172 assert(false && "Something in the Type enum wasn't covered");
173 return WvString::null;