Diff for /libaitcfg/src/aitcfg.c between versions 1.7 and 1.9

version 1.7, 2012/07/25 15:24:20 version 1.9, 2013/05/30 09:12:27
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, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
         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;
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 174  cfgClearConfig(cfg_root_t * __restrict cfg) Line 186  cfgClearConfig(cfg_root_t * __restrict cfg)
                 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);
                io_free(av);                e_free(av);
         }          }
         cfg->rbh_root = NULL;          cfg->rbh_root = NULL;
         CFG_RC_UNLOCK(cfg);          CFG_RC_UNLOCK(cfg);
Line 220  cfgCreateConfig(const char *csConfigName, cfg_root_t * Line 232  cfgCreateConfig(const char *csConfigName, cfg_root_t *
         }          }
                   
         ret = cfgWriteConfig(f, cfg, whitespace);          ret = cfgWriteConfig(f, cfg, whitespace);
   
           fclose(f);
           return ret;
   }
   
   /*
    * cfgInitPasswd() - Init password root
    *
    * @pwd = Password root
    * return: -1 error or 0 ok
    */
   int
   cfgInitPasswd(pwd_root_t * __restrict pwd)
   {
           if (!pwd)
                   return -1;
   
           pthread_mutex_init(&pwd->pwd_mtx, NULL);
   
           SLIST_INIT(pwd);
           RB_INIT(pwd);
           return 0;
   }
   
   /*
    * 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;
           int ret;
   
           if (!pwdName || !pwd) {
                   cfg_SetErr(EINVAL, "Invalid parameter(s)");
                   return -1;
           } else
                   cfgInitPasswd(pwd);
   
           f = fopen(pwdName, "r");
           if (!f) {
                   LOGERR;
                   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 = cfgWritePasswd(f, pwd);
   
         fclose(f);          fclose(f);
         return ret;          return ret;

Removed from v.1.7  
changed lines
  Added in v.1.9


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