WvStreams
wvconfemu.h
00001 /* -*- Mode: C++ -*-
00002  * Worldvisions Weaver Software:
00003  *   Copyright (C) 1997-2004 Net Integration Technologies, Inc.
00004  *
00005  * Basic WvConf emulation layer for UniConf.
00006  */
00007 #ifndef __WVCONFEMU_H
00008 #define __WVCONFEMU_H
00009 
00010 #include "uniconfroot.h"
00011 #include "wvstringtable.h"
00012 #include "wvsorter.h"
00013 
00014 #define WvConf WvConfEmu
00015 #define WvConfigSection WvConfigSectionEmu
00016 #define WvConfigSectionList WvConfigSectionListEmu
00017 #define WvConfigEntry WvConfigEntryEmu
00018 #define WvConfigEntryList WvConfigEntryListEmu
00019 
00020 
00021 class WvConfEmu;
00022 class WvConfigEntryEmu;
00023 class WvConfigSectionEmu;
00024 class WvAuthDaemon;
00025 class WvAuthDaemonSvc;
00026 
00027 typedef WvConfEmu WvConfigSectionListEmu;
00028 typedef WvConfigSectionEmu WvConfigEntryListEmu;
00029 
00030 
00031 class WvConfigEntryEmu
00032 {
00033 public:
00034     const WvString name;
00035     WvString value;
00036     WvConfigEntryEmu(WvStringParm _name, WvStringParm _value):
00037         name(_name), value(_value)
00038     {}
00039 };
00040 
00041 
00042 DeclareWvDict(WvConfigEntryEmu, WvString, name);
00043 
00044 
00045 class WvConfigSectionEmu
00046 {
00047 private:
00048     const UniConf uniconf;
00049     WvConfigEntryEmuDict entries;
00050     WvStringTable &values;
00051 public:
00052     const WvString name;
00053     WvConfigSectionEmu(const UniConf& _uniconf, WvStringParm _name,
00054                        WvStringTable *_values):
00055         uniconf(_uniconf), entries(42), values(*_values), name(_name)
00056     {}
00057     WvConfigEntryEmu *operator[] (WvStringParm s);
00058     const char *get(WvStringParm entry, const char *def_val = NULL);
00059     void set(WvStringParm entry, WvStringParm value);
00060     void quick_set(WvStringParm entry, WvStringParm value);
00061 
00062     bool isempty() const;
00063 
00064     class Iter;
00065     friend class Iter;
00066 
00067     typedef WvSorter<WvConfigEntryEmu, WvConfigSectionEmu, Iter> Sorter;
00068 };
00069 
00070 
00071 DeclareWvDict(WvConfigSectionEmu, WvString, name);
00072 
00073 
00074 class WvConfigSectionEmu::Iter
00075 {
00076 private:
00077     WvConfigSectionEmu& sect;
00078     UniConf::RecursiveIter iter;
00079     WvLink link;
00080     WvConfigEntryEmu* entry;
00081 public:
00082     Iter(WvConfigSectionEmu& _sect):
00083         sect(_sect), iter(_sect.uniconf), link(NULL, false), entry(NULL)
00084     { 
00085         assert(&_sect);
00086     }
00087     ~Iter();
00088     void rewind();
00089     WvLink *next();
00090     WvLink *cur();
00091     WvConfigEntryEmu* ptr() const;
00092     void* vptr() const;
00093     WvIterStuff(WvConfigEntryEmu);
00094 };
00095 
00096 
00097 // parameters are: userdata, section, entry, oldval, newval
00098 typedef wv::function<void(void*, WvStringParm, WvStringParm, WvStringParm, WvStringParm)> WvConfCallback;
00099 
00100 
00101 class WvConfEmu
00102 {
00103 private:
00104     struct CallbackInfo
00105     {
00106         WvConfCallback callback;
00107         void* userdata;
00108         WvString section;
00109         WvString key;
00110         void* cookie;
00111         CallbackInfo(WvConfCallback _callback, void* _userdata,
00112                      WvStringParm _section, WvStringParm _key,
00113                      void* _cookie):
00114             callback(_callback), userdata(_userdata), section(_section),
00115             key(_key), cookie(_cookie)
00116         {}
00117     };
00118 
00119     WvConfigSectionEmuDict sections;
00120     bool hold;
00121     bool dirty;
00122     WvList<CallbackInfo> callbacks;
00123     WvStringTable values;
00124 
00125     void notify(const UniConf &_uni, const UniConfKey &_key);
00126 public:
00127     const UniConf uniconf;
00128 
00129     WvConfEmu(const UniConf &_uniconf);
00130     ~WvConfEmu();
00131     void zap();
00132     bool isclean() const;
00133     bool isok() const;
00134     void load_file(WvStringParm filename);
00135     void save(WvStringParm filename, int _create_mode = 0666);
00136     void save();
00137     void flush();
00138 
00139     WvConfigSectionEmu *operator[] (WvStringParm sect);
00140 
00141     void add_callback(WvConfCallback callback, void *userdata,
00142                       WvStringParm section, WvStringParm key, void *cookie);
00143     void del_callback(WvStringParm section, WvStringParm key, void *cookie);
00144 
00145     void add_setbool(bool *b, WvStringParm _section, WvStringParm _key);
00146     void del_setbool(bool *b, WvStringParm _section, WvStringParm _key);
00147 
00148     // The addname callback will add the key "ent" in "sect" to the "list" 
00149     // whenever "ent" changes. If ent is empty, add any key in the sect to
00150     // the list when one is added/deleted/changed
00151     void add_addname(WvStringList *list, WvStringParm sect, WvStringParm ent);
00152     void del_addname(WvStringList *list, WvStringParm sect, WvStringParm ent);
00153 
00154     WvString getraw(WvString wvconfstr, int &parse_error);
00155     int getint(WvStringParm section, WvStringParm entry, int def_val);
00156     const char *get(WvStringParm section, WvStringParm entry,
00157                     const char *def_val = NULL);
00158     int fuzzy_getint(WvStringList &sect, WvStringParm entry,
00159                   int def_val);
00160     const char *fuzzy_get(WvStringList &sect, WvStringParm entry,
00161                           const char *def_val = NULL);
00162 
00163     void setraw(WvString wvconfstr, const char *&value, int &parse_error);
00164     void setint(WvStringParm section, WvStringParm entry, int value);
00165     void set(WvStringParm section, WvStringParm entry,
00166              const char *value);
00167 
00168     void maybesetint(WvStringParm section, WvStringParm entry,
00169                      int value);
00170     void maybeset(WvStringParm section, WvStringParm entry,
00171                   const char *value);
00172 
00173     void delete_section(WvStringParm section);
00174 
00175     // Gets a user's password and decrypts it.  This isn't defined in wvconf.cc.
00176     WvString get_passwd(WvStringParm sect, WvStringParm user);
00177     WvString get_passwd(WvStringParm user)
00178         { return get_passwd("Users", user); }
00179     WvString get_passwd2(WvString pwenc);
00180 
00181     // Check the password passed in.  This isn't defined in wvconf.cc
00182     // We use this function to check passwords since we may not know what
00183     // the password actually is!
00184     // If s is not null and has continue_select enabled, check_passwd will
00185     // pause if the password is incorrect (the pause length depends on how
00186     // many password failures have occurred recently).
00187     bool check_passwd(WvStringParm sect, WvStringParm user,
00188                       WvStringParm passwd, WvStream *s);
00189     bool check_passwd(WvStringParm user, WvStringParm passwd, WvStream *s)
00190     {
00191         return check_passwd("Users", user, passwd, s);
00192     }
00193 
00194     // Check if the user exists.  This isn't defined in wvconf.cc
00195     bool user_exists(WvStringParm sect, WvStringParm user);
00196     bool user_exists(WvStringParm user)
00197     {
00198         return user_exists("Users", user);
00199     }
00200 
00201     // Encrypts and sets a user's password.  This isn't defined in wvconf.cc.
00202     void set_passwd(WvStringParm sect, WvStringParm user, WvStringParm passwd);
00203     void set_passwd(WvStringParm user, WvStringParm passwd)
00204         { set_passwd("Users", user, passwd); }
00205     WvString set_passwd2(WvStringParm passwd);
00206 
00207     // Converts all passwords to unencrypted format.  Not defined in wvconf.cc.
00208     void convert_to_old_pw();
00209 
00210     static int check_for_bool_string(const char *s);
00211 
00212     class Iter;
00213     friend class Iter;
00214     
00215 private:
00216 /* The following is an ugly hack, but since WvConf is being
00217  * deprecated, we don't care.
00218  * 
00219  * It seems that check_passwd() and user_exists() need to talk to a
00220  * WvAuthDaemon.  However, making them virtual functions would break since
00221  * everyone else has to implement them.  So we'll its pointer and accessors
00222  * here.
00223  */
00224 private:
00225     WvAuthDaemon *wvauthd;      // Authentication Daemon
00226 public:
00227     friend class WvAuthDaemonSvc;
00228 };
00229 
00230 
00231 class WvConfEmu::Iter
00232 {
00233     WvConfEmu& conf;
00234     UniConf::Iter iter;
00235     WvLink link;
00236 public:
00237     Iter(WvConfEmu& _conf):
00238         conf(_conf), iter(conf.uniconf), link(NULL, false)
00239     {}
00240     void rewind();
00241     WvLink *next();
00242     WvConfigSectionEmu* ptr() const;
00243     WvIterStuff(WvConfigSectionEmu);
00244 };
00245 
00246 #endif // __WVCONFEMU_H