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 
16 WV_LINK(UniPermGen);
17 
18 
19 
20 UniPermGen::UniPermGen(IUniConfGen *_gen)
21  : UniFilterGen(_gen)
22 {
23 }
24 
25 
26 UniPermGen::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 
35 void UniPermGen::setowner(const UniConfKey &path, WvStringParm owner)
36 {
37  inner()->set(WvString("%s/owner", path), owner);
38 }
39 
40 
41 WvString 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 
51 void UniPermGen::setgroup(const UniConfKey &path, WvStringParm group)
52 {
53  inner()->set(WvString("%s/group", path), group);
54 }
55 
56 
57 WvString 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 
66 void 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 
74 bool 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 
96 bool 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 
119 void UniPermGen::chmod(const UniConfKey &path,
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 
141 void 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 
151 WvString 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 
164 WvString 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 }
IUniConfGen::set
virtual void set(const UniConfKey &key, WvStringParm value)=0
Stores a string value for a key into the registry.
UniPermGen::setowner
void setowner(const UniConfKey &path, WvStringParm owner)
get and set the owner for a path
Definition: unipermgen.cc:35
IUniConfGen::get
virtual WvString get(const UniConfKey &key)=0
Fetches a string value for a key from the registry.
UniPermGen::setgroup
void setgroup(const UniConfKey &path, WvStringParm group)
get and set the group for a path
Definition: unipermgen.cc:51
wvtclstring.h
UniConfKey::removelast
UniConfKey removelast(int n=1) const
Returns the path formed by removing the last n segments of this path.
Definition: uniconfkey.h:346
UniConfGen::str2int
virtual int str2int(WvStringParm s, int defvalue) const
Converts a string to an integer.
Definition: uniconfgen.cc:126
UniFilterGen::get
virtual WvString get(const UniConfKey &key)
Fetches a string value for a key from the registry.
Definition: unifiltergen.cc:76
UniPermGen::chmod
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
WvString
WvString is an implementation of a simple and efficient printable-string class.
Definition: wvstring.h:329
IUniConfGen
An abstract data container that backs a UniConf tree.
Definition: uniconfgen.h:39
group
Definition: argp-parse.c:204
UniPermGen
UniPermGen wraps a tree encoding Unix-style permissions, and provides an API for setting and checking...
Definition: unipermgen.h:26
UniConfKey
Represents a UniConf key which is a path in a hierarchy structured much like the traditional Unix fil...
Definition: uniconfkey.h:38
UniConfKey::isempty
bool isempty() const
Returns true if this path has zero segments (also known as root).
Definition: uniconfkey.h:264
UniFilterGen::inner
IUniConfGen * inner() const
Returns the inner generator.
Definition: unifiltergen.h:33
UniFilterGen
A UniConfGen that delegates all requests to an inner generator.
Definition: unifiltergen.h:17