Diff for /libaitcfg/src/aitcfg.c between versions 1.4.4.2 and 1.16

version 1.4.4.2, 2012/04/03 09:21:06 version 1.16, 2023/01/23 23:27:26
Line 12  terms: Line 12  terms:
 All of the documentation and software included in the ELWIX and AITNET  All of the documentation and software included in the ELWIX and AITNET
 Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>  Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
   
Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012Copyright 2004 - 2023
         by Michael Pounov <misho@elwix.org>.  All rights reserved.          by Michael Pounov <misho@elwix.org>.  All rights reserved.
   
 Redistribution and use in source and binary forms, with or without  Redistribution and use in source and binary forms, with or without
Line 44  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF TH Line 44  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF TH
 SUCH DAMAGE.  SUCH DAMAGE.
 */  */
 #include "global.h"  #include "global.h"
 #include "aitcfg.h"  
   
   
 #pragma GCC visibility push(hidden)  #pragma GCC visibility push(hidden)
Line 52  SUCH DAMAGE. Line 51  SUCH DAMAGE.
 int cfg_Errno;  int cfg_Errno;
 char cfg_Error[STRSIZ];  char cfg_Error[STRSIZ];
   
inline intint
 cfg_Write(FILE *f, char *fmt, ...)
 {
         int ret = 0;
         va_list lst;
 
         va_start(lst, fmt);
         ret = vfprintf(f, fmt, lst);
         va_end(lst);
 
         return ret;
 }
 
 int
 cfg_tree_cmp(struct tagCfg *a, struct tagCfg *b)  cfg_tree_cmp(struct tagCfg *a, struct tagCfg *b)
 {  {
         int ret;          int ret;
   
         assert(a && b);          assert(a && b);
   
        ret = ((AIT_KEY(&a->cfg_sec) << 16) | AIT_KEY(&a->cfg_attr)) -         ret = ((AIT_KEY(&a->cfg_sec) << 15) | AIT_KEY(&a->cfg_attr)) - 
                ((AIT_KEY(&b->cfg_sec) << 16) | AIT_KEY(&b->cfg_attr));                ((AIT_KEY(&b->cfg_sec) << 15) | AIT_KEY(&b->cfg_attr));
   
         if (ret < 0)          if (ret < 0)
                 return -1;                  return -1;
Line 76  RB_GENERATE(tagRC, tagCfg, cfg_node, cfg_tree_cmp); Line 88  RB_GENERATE(tagRC, tagCfg, cfg_node, cfg_tree_cmp);
   
   
 // cfg_GetErrno() Get error code of last operation  // cfg_GetErrno() Get error code of last operation
inline intint
 cfg_GetErrno()  cfg_GetErrno()
 {  {
         return cfg_Errno;          return cfg_Errno;
 }  }
   
 // cfg_GetError() Get error text of last operation  // cfg_GetError() Get error text of last operation
inline const char *const char *
 cfg_GetError()  cfg_GetError()
 {  {
         return cfg_Error;          return cfg_Error;
 }  }
   
 // cfg_SetErr() Set error to variables for internal use!!!  // cfg_SetErr() Set error to variables for internal use!!!
inline voidvoid
 cfg_SetErr(int eno, char *estr, ...)  cfg_SetErr(int eno, char *estr, ...)
 {  {
         va_list lst;          va_list lst;
Line 106  cfg_SetErr(int eno, char *estr, ...) Line 118  cfg_SetErr(int eno, char *estr, ...)
 /*  /*
  * cfgInitConfig() - Init config root   * cfgInitConfig() - Init config root
  *   *
 * @cfg = Config root * return: NULL error or !=NULL allocated config root
 * return: -1 error or 0 ok 
  */   */
intcfg_root_t *
cfgInitConfig(cfg_root_t * __restrict cfg)cfgInitConfig()
 {  {
        if (!cfg)        cfg_root_t *cfg = NULL;
                return -1; 
   
#ifdef HAVE_LIBPTHREAD        cfg = e_malloc(sizeof(cfg_root_t));
         if (!cfg) {
                 cfg_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
                 return NULL;
         } else
                 memset(cfg, 0, sizeof(cfg_root_t));
 
         pthread_mutex_init(&cfg->rc_mtx, NULL);          pthread_mutex_init(&cfg->rc_mtx, NULL);
#endif
        SLIST_INIT(cfg);        TAILQ_INIT(cfg);
         RB_INIT(cfg);          RB_INIT(cfg);
        return 0;        return cfg;
 }  }
   
 /*  /*
    * cfgEndConfig() - Free resources & config root
    *
    * @pcfg = Config root
    * return: none
    */
   void
   cfgEndConfig(cfg_root_t **pcfg)
   {
           if (pcfg && *pcfg) {
                   cfgClearConfig(*pcfg);
                   pthread_mutex_destroy(&(*pcfg)->rc_mtx);
                   e_free(*pcfg);
                   *pcfg = NULL;
           }
   }
   
   /*
  * cfgLoadConfig() - Load config from file   * cfgLoadConfig() - Load config from file
  *   *
  * @cfgName = Config filename   * @cfgName = Config filename
Line 139  cfgLoadConfig(const char *cfgName, cfg_root_t * __rest Line 172  cfgLoadConfig(const char *cfgName, cfg_root_t * __rest
         if (!cfgName || !cfg) {          if (!cfgName || !cfg) {
                 cfg_SetErr(EINVAL, "Invalid parameter(s)");                  cfg_SetErr(EINVAL, "Invalid parameter(s)");
                 return -1;                  return -1;
        } else        } else {
                cfgInitConfig(cfg);                memset(cfg, 0, sizeof(cfg_root_t));
   
                   pthread_mutex_init(&cfg->rc_mtx, NULL);
   
                   TAILQ_INIT(cfg);
                   RB_INIT(cfg);
           }
   
         f = fopen(cfgName, "r");          f = fopen(cfgName, "r");
         if (!f) {          if (!f) {
                 LOGERR;                  LOGERR;
Line 155  cfgLoadConfig(const char *cfgName, cfg_root_t * __rest Line 194  cfgLoadConfig(const char *cfgName, cfg_root_t * __rest
 }  }
   
 /*  /*
 * cfgUnloadConfig() - Unload config from memory and free resources * cfgClearConfig() - Clear config and free resources
  *   *
  * @cfg = Config root   * @cfg = Config root
  * return: none   * return: none
  */   */
 void  void
cfgUnloadConfig(cfg_root_t * __restrict cfg)cfgClearConfig(cfg_root_t * __restrict cfg)
 {  {
         struct tagCfg *av;          struct tagCfg *av;
   
Line 169  cfgUnloadConfig(cfg_root_t * __restrict cfg) Line 208  cfgUnloadConfig(cfg_root_t * __restrict cfg)
                 return;                  return;
   
         CFG_RC_LOCK(cfg);          CFG_RC_LOCK(cfg);
        while ((av = SLIST_FIRST(cfg))) {        while ((av = TAILQ_FIRST(cfg))) {
                SLIST_REMOVE_HEAD(cfg, cfg_next);                TAILQ_REMOVE(cfg, av, cfg_next);
   
                 AIT_FREE_VAL(&av->cfg_val);                  AIT_FREE_VAL(&av->cfg_val);
                 AIT_FREE_VAL(&av->cfg_attr);                  AIT_FREE_VAL(&av->cfg_attr);
                 AIT_FREE_VAL(&av->cfg_sec);                  AIT_FREE_VAL(&av->cfg_sec);
                free(av);                e_free(av);
         }          }
         cfg->rbh_root = NULL;          cfg->rbh_root = NULL;
         CFG_RC_UNLOCK(cfg);          CFG_RC_UNLOCK(cfg);
   }
   
#ifdef HAVE_LIBPTHREAD/*
  * cfgUnloadConfig() - Unload config from memory and destroy resources
  *
  * @cfg = Config root
  * return: none
  */
 void
 cfgUnloadConfig(cfg_root_t * __restrict cfg)
 {
         if (!cfg)
                 return;
 
         cfgClearConfig(cfg);
         pthread_mutex_destroy(&cfg->rc_mtx);          pthread_mutex_destroy(&cfg->rc_mtx);
 #endif  
 }  }
   
 #if 0  
 /*  /*
 * CreateConfig() Create config file from memory * cfgCreateConfig() - Create config file from memory
  *
  * @csConfigName = New config filename   * @csConfigName = New config filename
 * @cfg = Head list element * @cfg = Config root
 * return: 0 ok; -1 error:: can`t save new config * @whitespace = Additional whitespace characters to file
*/ * return: -1 error or 0 ok
int CreateConfig(const char *csConfigName, sl_config * __restrict cfg) */
 int
 cfgCreateConfig(const char *csConfigName, cfg_root_t * __restrict cfg, int whitespace)
 {  {
         FILE *f;          FILE *f;
         int ret;          int ret;
Line 200  int CreateConfig(const char *csConfigName, sl_config * Line 253  int CreateConfig(const char *csConfigName, sl_config *
         if (!csConfigName || !cfg)          if (!csConfigName || !cfg)
                 return -1;                  return -1;
   
        f = fopen(csConfigName, "wt");        f = fopen(csConfigName, "w");
         if (!f) {          if (!f) {
                 LOGERR;                  LOGERR;
                 return -1;                  return -1;
         }          }
                   
        ret ^= ret;        ret = cfgWriteConfigRaw(f, cfg, whitespace);
        ret = WriteConfig(f, cfg); 
   
         fclose(f);          fclose(f);
         return ret;          return ret;
 }  }
   
 /*  /*
 * cfg_CreateConfig() Create config file from memory without whitespaces! * cfgInitPasswd() - Init password root
 * @csConfigName = New config filename *
 * @cfg = Head list element * return: NULL error or !=NULL allocated password root
 * return: 0 ok; -1 error:: can`t save new config */
*/pwd_root_t *
int cfg_CreateConfig(const char *csConfigName, sl_config * __restrict cfg)cfgInitPasswd()
 {  {
           pwd_root_t *pwd = NULL;
   
           pwd = e_malloc(sizeof(pwd_root_t));
           if (!pwd) {
                   cfg_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
                   return NULL;
           } else
                   memset(pwd, 0, sizeof(pwd_root_t));
   
           pthread_mutex_init(&pwd->pwd_mtx, NULL);
   
           SLIST_INIT(pwd);
           RB_INIT(pwd);
           return 0;
   }
   
   /*
    * cfgEndPasswd() - Free resources & password root
    *
    * @ppwd = Password root
    * return: none
    */
   void
   cfgEndPasswd(pwd_root_t **ppwd)
   {
           if (ppwd && *ppwd) {
                   cfgClearPasswd(*ppwd);
                   pthread_mutex_destroy(&(*ppwd)->pwd_mtx);
                   e_free(*ppwd);
                   *ppwd = NULL;
           }
   }
   
   /*
    * cfgLoadPasswd() - Load passwords from file
    *
    * @pwdName = Passwords filename
    * @pwd = Password root
    * return: -1 error or 0 ok
    */
   int
   cfgLoadPasswd(const char *pwdName, pwd_root_t * __restrict pwd)
   {
         FILE *f;          FILE *f;
         int ret;          int ret;
   
        if (!csConfigName || !cfg)        if (!pwdName || !pwd) {
                 cfg_SetErr(EINVAL, "Invalid parameter(s)");
                 return -1;                  return -1;
           } else {
                   memset(pwd, 0, sizeof(pwd_root_t));
   
        f = fopen(csConfigName, "wt");                pthread_mutex_init(&pwd->pwd_mtx, NULL);
 
                 SLIST_INIT(pwd);
                 RB_INIT(pwd);
         }
 
         f = fopen(pwdName, "r");
         if (!f) {          if (!f) {
                 LOGERR;                  LOGERR;
                 return -1;                  return -1;
         }          }
   
           ret = cfgReadPasswd(f, pwd);
   
           fclose(f);
           return ret;
   }
   
   /*
    * cfgClearPasswd() - Clear passwords and free resources
    *
    * @cfg = Password root
    * return: none
    */
   void
   cfgClearPasswd(pwd_root_t * __restrict pwd)
   {
           struct tagUser *p;
   
           if (!pwd)
                   return;
   
           PWD_LOCK(pwd);
           while ((p = SLIST_FIRST(pwd))) {
                   SLIST_REMOVE_HEAD(pwd, usr_next);
   
                   AIT_FREE_VAL(&p->usr_name);
                   AIT_FREE_VAL(&p->usr_pass);
                   AIT_FREE_VAL(&p->usr_uid);
                   AIT_FREE_VAL(&p->usr_gid);
                   AIT_FREE_VAL(&p->usr_class);
                   AIT_FREE_VAL(&p->usr_change);
                   AIT_FREE_VAL(&p->usr_expire);
                   AIT_FREE_VAL(&p->usr_realm);
                   AIT_FREE_VAL(&p->usr_home);
                   AIT_FREE_VAL(&p->usr_shell);
                   e_free(p);
           }
           pwd->rbh_root = NULL;
           PWD_UNLOCK(pwd);
   }
   
   /*
    * cfgUnloadPasswd() - Unload passwords from memory and destroy resources
    *
    * @pwd = Password root
    * return: none
    */
   void
   cfgUnloadPasswd(pwd_root_t * __restrict pwd)
   {
           if (!pwd)
                   return;
   
           cfgClearPasswd(pwd);
           pthread_mutex_destroy(&pwd->pwd_mtx);
   }
   
   /*
    * cfgCreatePasswd() - Create password file from memory
    *
    * @pwdName = New password filename
    * @pwd = Password root
    * return: -1 error or 0 ok
    */
   int
   cfgCreatePasswd(const char *pwdName, pwd_root_t * __restrict pwd)
   {
           FILE *f;
           int ret;
   
           if (!pwdName || !pwd)
                   return -1;
   
           f = fopen(pwdName, "w");
           if (!f) {
                   LOGERR;
                   return -1;
           }
                   
        ret ^= ret;        ret = cfgWritePasswd(f, pwd);
        ret = cfg_WriteConfig(f, cfg); 
   
         fclose(f);          fclose(f);
         return ret;          return ret;
 }  }
 #endif  

Removed from v.1.4.4.2  
changed lines
  Added in v.1.16


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>