WvStreams
unipermgen.cc
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"
15
16WV_LINK(UniPermGen);
17
18
19
20UniPermGen::UniPermGen(IUniConfGen *_gen)
21 : UniFilterGen(_gen)
22{
23}
24
25
26UniPermGen::UniPermGen(WvStringParm moniker)
27 : UniFilterGen(NULL)
28{
29 IUniConfGen *gen = wvcreate<IUniConfGen>(moniker);
30 assert(gen && "Moniker doesn't get us a generator!");
31 setinner(gen);
32}
33
34
36{
37 inner()->set(WvString("%s/owner", path), owner);
38}
39
40
41WvString UniPermGen::getowner(const UniConfKey &path)
42{
43 WvString owner = inner()->get(WvString("%s/owner", path));
44
45 if (!owner && !path.isempty())
46 owner = getowner(path.removelast());
47 return owner;
48}
49
50
52{
53 inner()->set(WvString("%s/group", path), group);
54}
55
56
57WvString UniPermGen::getgroup(const UniConfKey &path)
58{
59 WvString group = inner()->get(WvString("%s/group", path));
60 if (!group && !path.isempty())
61 group = getgroup(path.removelast());
62 return group;
63}
64
65
66void UniPermGen::setperm(const UniConfKey &path, Level level,
67 Type type, bool val)
68{
69 inner()->set(WvString("%s/%s-%s", path, level2str(level),
70 type2str(type)), val);
71}
72
73
74bool UniPermGen::getperm(const UniConfKey &path, const Credentials &cred,
75 Type type)
76{
77 WvString owner = getowner(path);
78 WvString group = getgroup(path);
79
80 Level level;
81 if (!!owner && cred.user == owner) level = USER;
82 else if (!!group && cred.groups[group]) level = GROUP;
83 else level = WORLD;
84
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;
90}
91
92
96bool UniPermGen::getoneperm(const UniConfKey &path, Level level, Type type)
97{
98 int val = str2int(inner()->get(WvString("%s/%s-%s", path, level2str(level),
99 type2str(type))), -1);
100 if (val == -1)
101 {
102 if (path.isempty())
103 {
104 // nothing found: use default
105 switch (type)
106 {
107 case READ: return false;
108 case WRITE: return false;
109 case EXEC: return false;
110 }
111 }
112 else
113 return getoneperm(path.removelast(), level, type);
114 }
115 return val;
116}
117
118
120 unsigned int user, unsigned int group,
121 unsigned int world)
122{
123 static const int r = 4;
124 static const int w = 2;
125 static const int x = 1;
126
127 setperm(path, USER, READ, (user & r));
128 setperm(path, USER, WRITE, (user & w));
129 setperm(path, USER, EXEC, (user & x));
130
131 setperm(path, GROUP, READ, (group & r));
132 setperm(path, GROUP, WRITE, (group & w));
133 setperm(path, GROUP, EXEC, (group & x));
134
135 setperm(path, WORLD, READ, (world & r));
136 setperm(path, WORLD, WRITE, (world & w));
137 setperm(path, WORLD, EXEC, (world & x));
138}
139
140
141void UniPermGen::chmod(const UniConfKey &path, unsigned int mode)
142{
143 unsigned int user = (mode & 0700) >> 6;
144 unsigned int group = (mode & 0070) >> 3;
145 unsigned int world = (mode & 0007);
146
147 chmod(path, user, group, world);
148}
149
150
151WvString UniPermGen::level2str(Level level)
152{
153 switch (level)
154 {
155 case USER: return "user";
156 case GROUP: return "group";
157 case WORLD: return "world";
158 }
159 assert(false && "Something in the Level enum wasn't covered");
160 return WvString::null;
161}
162
163
164WvString UniPermGen::type2str(Type type)
165{
166 switch (type)
167 {
168 case READ: return "read";
169 case WRITE: return "write";
170 case EXEC: return "exec";
171 }
172 assert(false && "Something in the Type enum wasn't covered");
173 return WvString::null;
174}
An abstract data container that backs a UniConf tree.
Definition: uniconfgen.h:40
virtual WvString get(const UniConfKey &key)=0
Fetches a string value for a key from the registry.
virtual void set(const UniConfKey &key, WvStringParm value)=0
Stores a string value for a key into the registry.
virtual int str2int(WvStringParm s, int defvalue) const
Converts a string to an integer.
Definition: uniconfgen.cc:126
Represents a UniConf key which is a path in a hierarchy structured much like the traditional Unix fil...
Definition: uniconfkey.h:39
UniConfKey removelast(int n=1) const
Returns the path formed by removing the last n segments of this path.
Definition: uniconfkey.h:346
bool isempty() const
Returns true if this path has zero segments (also known as root).
Definition: uniconfkey.h:264
A UniConfGen that delegates all requests to an inner generator.
Definition: unifiltergen.h:18
virtual WvString get(const UniConfKey &key)
Fetches a string value for a key from the registry.
Definition: unifiltergen.cc:76
IUniConfGen * inner() const
Returns the inner generator.
Definition: unifiltergen.h:33
UniPermGen wraps a tree encoding Unix-style permissions, and provides an API for setting and checking...
Definition: unipermgen.h:27
void setowner(const UniConfKey &path, WvStringParm owner)
get and set the owner for a path
Definition: unipermgen.cc:35
void chmod(const UniConfKey &path, unsigned int owner, unsigned int group, unsigned int world)
Set permissions for path using Unix style chmod (with the second form, be sure to use octal)
Definition: unipermgen.cc:119
void setgroup(const UniConfKey &path, WvStringParm group)
get and set the group for a path
Definition: unipermgen.cc:51
A WvFastString acts exactly like a WvString, but can take (const char *) strings without needing to a...
Definition: wvstring.h:94
WvString is an implementation of a simple and efficient printable-string class.
Definition: wvstring.h:330
Functions to handle "tcl-style" strings and lists.