WvStreams
wvconfemu.h
1 /* -*- Mode: C++ -*-
2  * Worldvisions Weaver Software:
3  * Copyright (C) 1997-2004 Net Integration Technologies, Inc.
4  *
5  * Basic WvConf emulation layer for UniConf.
6  */
7 #ifndef __WVCONFEMU_H
8 #define __WVCONFEMU_H
9 
10 #include "uniconfroot.h"
11 #include "wvstringtable.h"
12 #include "wvsorter.h"
13 
14 #define WvConf WvConfEmu
15 #define WvConfigSection WvConfigSectionEmu
16 #define WvConfigSectionList WvConfigSectionListEmu
17 #define WvConfigEntry WvConfigEntryEmu
18 #define WvConfigEntryList WvConfigEntryListEmu
19 
20 
21 class WvConfEmu;
22 class WvConfigEntryEmu;
23 class WvConfigSectionEmu;
24 class WvAuthDaemon;
25 class WvAuthDaemonSvc;
26 
29 
30 
32 {
33 public:
34  const WvString name;
35  WvString value;
36  WvConfigEntryEmu(WvStringParm _name, WvStringParm _value):
37  name(_name), value(_value)
38  {}
39 };
40 
41 
42 DeclareWvDict(WvConfigEntryEmu, WvString, name);
43 
44 
46 {
47 private:
48  const UniConf uniconf;
49  WvConfigEntryEmuDict entries;
50  WvStringTable &values;
51 public:
52  const WvString name;
53  WvConfigSectionEmu(const UniConf& _uniconf, WvStringParm _name,
54  WvStringTable *_values):
55  uniconf(_uniconf), entries(42), values(*_values), name(_name)
56  {}
57  WvConfigEntryEmu *operator[] (WvStringParm s);
58  const char *get(WvStringParm entry, const char *def_val = NULL);
59  void set(WvStringParm entry, WvStringParm value);
60  void quick_set(WvStringParm entry, WvStringParm value);
61 
62  bool isempty() const;
63 
64  class Iter;
65  friend class Iter;
66 
68 };
69 
70 
71 DeclareWvDict(WvConfigSectionEmu, WvString, name);
72 
73 
75 {
76 private:
77  WvConfigSectionEmu& sect;
79  WvLink link;
80  WvConfigEntryEmu* entry;
81 public:
82  Iter(WvConfigSectionEmu& _sect):
83  sect(_sect), iter(_sect.uniconf), link(NULL, false), entry(NULL)
84  {
85  assert(&_sect);
86  }
87  ~Iter();
88  void rewind();
89  WvLink *next();
90  WvLink *cur();
91  WvConfigEntryEmu* ptr() const;
92  void* vptr() const;
93  WvIterStuff(WvConfigEntryEmu);
94 };
95 
96 
97 // parameters are: userdata, section, entry, oldval, newval
98 typedef wv::function<void(void*, WvStringParm, WvStringParm, WvStringParm, WvStringParm)> WvConfCallback;
99 
100 
102 {
103 private:
104  struct CallbackInfo
105  {
106  WvConfCallback callback;
107  void* userdata;
108  WvString section;
109  WvString key;
110  void* cookie;
111  CallbackInfo(WvConfCallback _callback, void* _userdata,
112  WvStringParm _section, WvStringParm _key,
113  void* _cookie):
114  callback(_callback), userdata(_userdata), section(_section),
115  key(_key), cookie(_cookie)
116  {}
117  };
118 
119  WvConfigSectionEmuDict sections;
120  bool hold;
121  bool dirty;
122  WvList<CallbackInfo> callbacks;
123  WvStringTable values;
124 
125  void notify(const UniConf &_uni, const UniConfKey &_key);
126 public:
127  const UniConf uniconf;
128 
129  WvConfEmu(const UniConf &_uniconf);
130  ~WvConfEmu();
131  void zap();
132  bool isclean() const;
133  bool isok() const;
134  void load_file(WvStringParm filename);
135  void save(WvStringParm filename, int _create_mode = 0666);
136  void save();
137  void flush();
138 
139  WvConfigSectionEmu *operator[] (WvStringParm sect);
140 
141  void add_callback(WvConfCallback callback, void *userdata,
142  WvStringParm section, WvStringParm key, void *cookie);
143  void del_callback(WvStringParm section, WvStringParm key, void *cookie);
144 
145  void add_setbool(bool *b, WvStringParm _section, WvStringParm _key);
146  void del_setbool(bool *b, WvStringParm _section, WvStringParm _key);
147 
148  // The addname callback will add the key "ent" in "sect" to the "list"
149  // whenever "ent" changes. If ent is empty, add any key in the sect to
150  // the list when one is added/deleted/changed
151  void add_addname(WvStringList *list, WvStringParm sect, WvStringParm ent);
152  void del_addname(WvStringList *list, WvStringParm sect, WvStringParm ent);
153 
154  WvString getraw(WvString wvconfstr, int &parse_error);
155  int getint(WvStringParm section, WvStringParm entry, int def_val);
156  const char *get(WvStringParm section, WvStringParm entry,
157  const char *def_val = NULL);
158  int fuzzy_getint(WvStringList &sect, WvStringParm entry,
159  int def_val);
160  const char *fuzzy_get(WvStringList &sect, WvStringParm entry,
161  const char *def_val = NULL);
162 
163  void setraw(WvString wvconfstr, const char *&value, int &parse_error);
164  void setint(WvStringParm section, WvStringParm entry, int value);
165  void set(WvStringParm section, WvStringParm entry,
166  const char *value);
167 
168  void maybesetint(WvStringParm section, WvStringParm entry,
169  int value);
170  void maybeset(WvStringParm section, WvStringParm entry,
171  const char *value);
172 
173  void delete_section(WvStringParm section);
174 
175  // Gets a user's password and decrypts it. This isn't defined in wvconf.cc.
176  WvString get_passwd(WvStringParm sect, WvStringParm user);
177  WvString get_passwd(WvStringParm user)
178  { return get_passwd("Users", user); }
179  WvString get_passwd2(WvString pwenc);
180 
181  // Check the password passed in. This isn't defined in wvconf.cc
182  // We use this function to check passwords since we may not know what
183  // the password actually is!
184  // If s is not null and has continue_select enabled, check_passwd will
185  // pause if the password is incorrect (the pause length depends on how
186  // many password failures have occurred recently).
187  bool check_passwd(WvStringParm sect, WvStringParm user,
188  WvStringParm passwd, WvStream *s);
189  bool check_passwd(WvStringParm user, WvStringParm passwd, WvStream *s)
190  {
191  return check_passwd("Users", user, passwd, s);
192  }
193 
194  // Check if the user exists. This isn't defined in wvconf.cc
195  bool user_exists(WvStringParm sect, WvStringParm user);
196  bool user_exists(WvStringParm user)
197  {
198  return user_exists("Users", user);
199  }
200 
201  // Encrypts and sets a user's password. This isn't defined in wvconf.cc.
202  void set_passwd(WvStringParm sect, WvStringParm user, WvStringParm passwd);
203  void set_passwd(WvStringParm user, WvStringParm passwd)
204  { set_passwd("Users", user, passwd); }
205  WvString set_passwd2(WvStringParm passwd);
206 
207  // Converts all passwords to unencrypted format. Not defined in wvconf.cc.
208  void convert_to_old_pw();
209 
210  static int check_for_bool_string(const char *s);
211 
212  class Iter;
213  friend class Iter;
214 
215 private:
216 /* The following is an ugly hack, but since WvConf is being
217  * deprecated, we don't care.
218  *
219  * It seems that check_passwd() and user_exists() need to talk to a
220  * WvAuthDaemon. However, making them virtual functions would break since
221  * everyone else has to implement them. So we'll its pointer and accessors
222  * here.
223  */
224 private:
225  WvAuthDaemon *wvauthd; // Authentication Daemon
226 public:
227  friend class WvAuthDaemonSvc;
228 };
229 
230 
232 {
233  WvConfEmu& conf;
234  UniConf::Iter iter;
235  WvLink link;
236 public:
237  Iter(WvConfEmu& _conf):
238  conf(_conf), iter(conf.uniconf), link(NULL, false)
239  {}
240  void rewind();
241  WvLink *next();
242  WvConfigSectionEmu* ptr() const;
243  WvIterStuff(WvConfigSectionEmu);
244 };
245 
246 #endif // __WVCONFEMU_H
WvConfigSectionEmu
Definition: wvconfemu.h:45
WvConfEmu::Iter
Definition: wvconfemu.h:231
UniConf
UniConf instances function as handles to subtrees of a UniConf tree and expose a high-level interface...
Definition: uniconf.h:50
WvStringTable
Definition: wvstringtable.h:17
WvString
WvString is an implementation of a simple and efficient printable-string class.
Definition: wvstring.h:329
UniConfKey
Represents a UniConf key which is a path in a hierarchy structured much like the traditional Unix fil...
Definition: uniconfkey.h:38
WvConfEmu
Definition: wvconfemu.h:101
WvStream
Unified support for streams, that is, sequences of bytes that may or may not be ready for read/write ...
Definition: wvstream.h:24
WvConfigSectionEmu::Iter
Definition: wvconfemu.h:74
WvConfigEntryEmu
Definition: wvconfemu.h:31
WvSorter
Definition: wvsorter.h:56
WvStringList
This is a WvList of WvStrings, and is a really handy way to parse strings.
Definition: wvstringlist.h:27
UniConf::Iter
This iterator walks through all immediate children of a UniConf node.
Definition: uniconf.h:435
WvList< CallbackInfo >
UniConf::RecursiveIter
This iterator performs depth-first traversal of a subtree.
Definition: uniconf.h:466