WvStreams
wvconf.h
00001 /* -*- Mode: C++ -*-
00002  * Worldvisions Weaver Software:
00003  *   Copyright (C) 1997-2002 Net Integration Technologies, Inc.
00004  *
00005  * Definition of the WvConfigFile, WvConfigSection, and WvConfigEntry classes, 
00006  * which are used to read and write entries from a Windows-INI-style file.
00007  *
00008  * Created:     Sept 12 1997            D. Coombs
00009  *
00010  */
00011 
00012 #ifndef __WVCONF_H
00013 #define __WVCONF_H
00014 
00015 #include "strutils.h"
00016 #include "wvlinklist.h"
00017 #include "wvlog.h"
00018 #include "wvstringlist.h"
00019 #include "wvtr1.h"
00020 
00021 
00022 #ifdef __WVCONFEMU_H
00023 #warning "disabling wvconfemu transparent emulation"
00024 #undef WvConf
00025 #undef WvConfigSection
00026 #undef WvConfigSectionList
00027 #undef WvConfigEntry
00028 #undef WvConfigEntryList
00029 #endif
00030 
00031 
00032 class WvConf;
00033 
00034 
00035 class WvConfigEntry
00036 {
00037 public:
00038     WvConfigEntry();
00039     WvConfigEntry(WvStringParm _name, WvStringParm _value);
00040     ~WvConfigEntry();
00041     
00042     void set(WvStringParm _value)
00043         { value = _value; }
00044     
00045     WvString name;
00046     WvString value;
00047 };
00048 
00049 
00050 DeclareWvList(WvConfigEntry);
00051 
00052 
00053 class WvConfigSection : public WvConfigEntryList
00054 {
00055 public:
00056     WvConfigSection(WvStringParm name);
00057     ~WvConfigSection();
00058     
00059     WvConfigEntry *operator[] (WvStringParm s);
00060 
00061     const char *get(WvStringParm entry, const char *def_val = NULL);
00062     void set(WvStringParm entry, WvStringParm value);
00063     void set(WvConfigEntry *e, WvStringParm value);
00064     
00065     // add an entry to the end of the section, _assuming_ no duplicates exist
00066     void quick_set(WvStringParm entry, WvStringParm value);
00067 
00068     void dump(WvStream &fp);
00069 
00070     WvString name;
00071 };
00072 
00073 
00074 // parameters are: userdata, section, entry, oldval, newval
00075 typedef wv::function<void(void*, WvStringParm, WvStringParm, WvStringParm, WvStringParm)> WvConfCallback;
00076 
00077 
00078 class WvConfCallbackInfo
00079 {
00080 public:
00081     WvConfCallback callback;
00082     void *userdata, *cookie;
00083     const WvString section, entry;
00084     
00085     WvConfCallbackInfo(WvConfCallback _callback, void *_userdata,
00086                        WvStringParm _section, WvStringParm _entry,
00087                        void *_cookie)
00088         : callback(_callback), section(_section), entry(_entry)
00089         { userdata = _userdata; cookie = _cookie; }
00090 };
00091 
00092 
00093 DeclareWvList(WvConfCallbackInfo);
00094 DeclareWvList(WvConfigSection);
00095 
00096 
00097 class WvAuthDaemon;
00098 class WvAuthDaemonSvc;
00099 
00104 class WvConf : public WvConfigSectionList
00105 {
00106 public:
00107     WvConf(WvStringParm _filename, int _create_mode = 0666);
00108     ~WvConf();
00109     
00110     bool isok() const
00111         { return !error; }
00112     bool isclean() const
00113         { return isok() && !dirty; }
00114     void save(WvStringParm filename);
00115     void save();
00116     void flush();
00117 
00118     WvConfigSection *operator[] (WvStringParm s);
00119 
00120     static int check_for_bool_string(const char *s);
00121     int parse_wvconf_request(char *request, char *&section, char *&entry,
00122                              char *&value);
00123 
00124     int getint(WvStringParm section, WvStringParm entry, int def_val);
00125     
00126     const char *get(WvStringParm section, WvStringParm entry,
00127                     const char *def_val = NULL);
00128     WvString getraw(WvString wvconfstr, int &parse_error);
00129 
00130     int fuzzy_getint(WvStringList &sect, WvStringParm entry,
00131                   int def_val);
00132     const char *fuzzy_get(WvStringList &sect, WvStringParm entry,
00133                           const char *def_val = NULL);
00134 
00135     int fuzzy_getint(WvStringList &sect, WvStringList &entry,
00136                   int def_val);
00137     const char *fuzzy_get(WvStringList & sect, WvStringList & ent,
00138                           const char *def_val = NULL);
00139 
00140     void setint(WvStringParm section, WvStringParm entry, int value);
00141     void set(WvStringParm section, WvStringParm entry,
00142              const char *value);
00143     void setraw(WvString wvconfstr, const char *&value, int &parse_error);
00144     
00145     void maybesetint(WvStringParm section, WvStringParm entry,
00146                      int value);
00147     void maybeset(WvStringParm section, WvStringParm entry,
00148                   const char *value);
00149 
00150     void delete_section(WvStringParm section);
00151 
00152     // section and entry may be blank -- that means _all_ sections/entries!
00153     // the 'cookie' is a random value that must be unique between all
00154     // registered callbacks on a particular key.  (Hint: maybe you should
00155     // use your 'this' pointer.)
00156     void add_callback(WvConfCallback callback, void *userdata,
00157                       WvStringParm section, WvStringParm entry, void *cookie);
00158     void del_callback(WvStringParm section, WvStringParm entry, void *cookie);
00159     void run_callbacks(WvStringParm section, WvStringParm entry,
00160                        WvStringParm oldvalue, WvStringParm newvalue);
00161     void run_all_callbacks();
00162     
00163     // generic callback function for setting a bool to "true" when changed
00164     void setbool(void *userdata,
00165                  WvStringParm section, WvStringParm entry,
00166                  WvStringParm oldval, WvStringParm newval);
00167 
00168     // generic callback for adding an entry name to name list when changed
00169     void addname(void *userdata,
00170                  WvStringParm section, WvStringParm entry,
00171                  WvStringParm oldval, WvStringParm newval);
00172 
00173     // generic callback to create a file with a one-line backup string
00174     void addfile(void *userdata,
00175                  WvStringParm section, WvStringParm entry,
00176                  WvStringParm oldval, WvStringParm newval);
00177 
00178     void add_addfile(WvString *filename, WvStringParm sect, WvStringParm ent)
00179         { add_callback(wv::bind(&WvConf::addfile, this, _1, _2, _3, _4, _5),
00180                        filename, sect, ent, new int); }
00181 
00182     void add_addname(WvStringList *list, WvStringParm sect, WvStringParm ent)
00183         { add_callback(wv::bind(&WvConf::addname, this, _1, _2, _3, _4, _5),
00184                        list, sect, ent, list); }
00185     void del_addname(WvStringList *list, WvStringParm sect, WvStringParm ent)
00186         { del_callback(sect, ent, list); }
00187     
00188     void add_setbool(bool *b, WvStringParm section, WvStringParm entry)
00189         { add_callback(wv::bind(&WvConf::setbool, this, _1, _2, _3, _4, _5),
00190                        b, section, entry, b); }
00191     void del_setbool(bool *b, WvStringParm section, WvStringParm entry)
00192         { del_callback(section, entry, b); }
00193                     
00194     void load_file() // append the contents of the real config file
00195         { load_file(filename); }
00196     void load_file(WvStringParm filename); // append any config file
00197 
00198     // Gets a user's password and decrypts it.  This isn't defined in wvconf.cc.
00199     WvString get_passwd(WvStringParm sect, WvStringParm user);
00200     WvString get_passwd(WvStringParm user)
00201         { return get_passwd("Users", user); }
00202     WvString get_passwd2(WvString pwenc);
00203 
00204     // Check the password passed in.  This isn't defined in wvconf.cc
00205     // We use this function to check passwords since we may not know what
00206     // the password actually is!
00207     bool check_passwd(WvStringParm sect, WvStringParm user,
00208                       WvStringParm passwd);
00209     bool check_passwd(WvStringParm user, WvStringParm passwd)
00210     {
00211         return check_passwd("Users", user, passwd);
00212     }
00213 
00214     // Check if the user exists.  This isn't defined in wvconf.cc
00215     bool user_exists(WvStringParm sect, WvStringParm user);
00216     bool user_exists(WvStringParm user)
00217     {
00218         return user_exists("Users", user);
00219     }
00220 
00221     // Encrypts and sets a user's password.  This isn't defined in wvconf.cc.
00222     void set_passwd(WvStringParm sect, WvStringParm user, WvStringParm passwd);
00223     void set_passwd(WvStringParm user, WvStringParm passwd)
00224         { set_passwd("Users", user, passwd); }
00225     WvString set_passwd2(WvStringParm passwd);
00226 
00227     // Converts all passwords to unencrypted format.  Not defined in wvconf.cc.
00228     void convert_to_old_pw();
00229 
00230     // needed by wvfast_user_import
00231     void setdirty()
00232     { dirty = true; }
00233 
00234 private:
00235     bool dirty;                 // true if changed since last flush()
00236     bool error;                 // true if something has gone wrong
00237     bool loaded_once;           // true if load_file succeeded at least once
00238     int create_mode;            // if we must create config file
00239 
00240     WvString filename;
00241     WvLog log;
00242 
00243     WvConfigSection globalsection;
00244     WvConfCallbackInfoList callbacks;
00245 
00246     char *parse_section(char *s);
00247     char *parse_value(char *s);
00248 
00249 /* The following is an ugly hack, but since WvConf is being
00250  * deprecated, we don't care.
00251  * 
00252  * It seems that check_passwd() and user_exists() need to talk to a
00253  * WvAuthDaemon.  However, making them virtual functions would break since
00254  * everyone else has to implement them.  So we'll its pointer and accessors
00255  * here.
00256  */
00257 private:
00258     WvAuthDaemon *wvauthd;      // Authentication Daemon
00259 public:
00260     friend class WvAuthDaemonSvc;
00261 };
00262 
00263 
00264 #endif // __WVCONF_H