LCOV - code coverage report
Current view: top level - server - utils.c (source / functions) Hit Total Coverage
Test: id:002084,src:002034,op:havoc,rep:2.lcov_info_final Lines: 12 75 16.0 %
Date: 2015-05-30 Functions: 2 7 28.6 %
Branches: 8 58 13.8 %

           Branch data     Line data    Source code
       1                 :            : /**
       2                 :            :  * @file    utils.c
       3                 :            :  *
       4                 :            :  * @brief   General/Generic functions for the fwknop server.
       5                 :            :  *
       6                 :            :  *  Fwknop is developed primarily by the people listed in the file 'AUTHORS'.
       7                 :            :  *  Copyright (C) 2009-2014 fwknop developers and contributors. For a full
       8                 :            :  *  list of contributors, see the file 'CREDITS'.
       9                 :            :  *
      10                 :            :  *  License (GNU General Public License):
      11                 :            :  *
      12                 :            :  *  This program is free software; you can redistribute it and/or
      13                 :            :  *  modify it under the terms of the GNU General Public License
      14                 :            :  *  as published by the Free Software Foundation; either version 2
      15                 :            :  *  of the License, or (at your option) any later version.
      16                 :            :  *
      17                 :            :  *  This program is distributed in the hope that it will be useful,
      18                 :            :  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
      19                 :            :  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      20                 :            :  *  GNU General Public License for more details.
      21                 :            :  *
      22                 :            :  *  You should have received a copy of the GNU General Public License
      23                 :            :  *  along with this program; if not, write to the Free Software
      24                 :            :  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
      25                 :            :  *  USA
      26                 :            :  */
      27                 :            : 
      28                 :            : #include "fwknopd_common.h"
      29                 :            : #include "utils.h"
      30                 :            : #include "log_msg.h"
      31                 :            : #include "replay_cache.h"
      32                 :            : #include "config_init.h"
      33                 :            : #include "fw_util.h"
      34                 :            : 
      35                 :            : #include <stdarg.h>
      36                 :            : 
      37                 :            : #define ASCII_LEN 16
      38                 :            : 
      39                 :            : /* Generic hex dump function.
      40                 :            : */
      41                 :            : void
      42                 :          0 : hex_dump(const unsigned char *data, const int size)
      43                 :            : {
      44                 :          0 :     int ln=0, i=0, j=0;
      45                 :          0 :     char ascii_str[ASCII_LEN+1] = {0};
      46                 :            : 
      47         [ #  # ]:          0 :     for(i=0; i<size; i++)
      48                 :            :     {
      49         [ #  # ]:          0 :         if((i % ASCII_LEN) == 0)
      50                 :            :         {
      51                 :            :             printf(" %s\n  0x%.4x:  ", ascii_str, i);
      52                 :            :             memset(ascii_str, 0x0, ASCII_LEN-1);
      53                 :          0 :             j = 0;
      54                 :            :         }
      55                 :            : 
      56                 :          0 :         printf("%.2x ", data[i]);
      57                 :            : 
      58         [ #  # ]:          0 :         ascii_str[j++] = (data[i] < 0x20 || data[i] > 0x7e) ? '.' : data[i];
      59                 :            : 
      60         [ #  # ]:          0 :         if(j == 8)
      61                 :            :             printf(" ");
      62                 :            :     }
      63                 :            : 
      64                 :            :     /* Remainder...
      65                 :            :     */
      66                 :          0 :     ln = strlen(ascii_str);
      67         [ #  # ]:          0 :     if(ln > 0)
      68                 :            :     {
      69         [ #  # ]:          0 :         for(i=0; i < ASCII_LEN-ln; i++)
      70                 :            :             printf("   ");
      71         [ #  # ]:          0 :         if(ln < 8)
      72                 :            :             printf(" ");
      73                 :            : 
      74                 :            :         printf(" %s\n\n", ascii_str);
      75                 :            :     }
      76                 :          0 :     return;
      77                 :            : }
      78                 :            : 
      79                 :            : /* Basic directory checks (stat() and whether the path is actually
      80                 :            :  * a directory).
      81                 :            : */
      82                 :            : int
      83                 :          0 : is_valid_dir(const char *path)
      84                 :            : {
      85                 :            : #if HAVE_STAT
      86                 :            :     struct stat st;
      87                 :            : 
      88                 :            :     /* If we are unable to stat the given dir, then return with error.
      89                 :            :     */
      90         [ #  # ]:          0 :     if(stat(path, &st) != 0)
      91                 :            :     {
      92                 :          0 :         log_msg(LOG_ERR, "[-] unable to stat() directory: %s: %s",
      93                 :          0 :             path, strerror(errno));
      94                 :          0 :         return(0);
      95                 :            :     }
      96                 :            : 
      97         [ #  # ]:          0 :     if(!S_ISDIR(st.st_mode))
      98                 :            :         return(0);
      99                 :            : #endif /* HAVE_STAT */
     100                 :            : 
     101                 :          0 :     return(1);
     102                 :            : }
     103                 :            : 
     104                 :            : int
     105                 :       5577 : verify_file_perms_ownership(const char *file)
     106                 :            : {
     107                 :            : #if HAVE_STAT
     108                 :            :     struct stat st;
     109                 :            : 
     110                 :            :     /* Every file that fwknopd deals with should be owned
     111                 :            :      * by the user and permissions set to 600 (user read/write)
     112                 :            :     */
     113         [ +  - ]:       5577 :     if((stat(file, &st)) == 0)
     114                 :            :     {
     115                 :            :         /* Make sure it is a regular file
     116                 :            :         */
     117         [ -  + ]:       5577 :         if(S_ISREG(st.st_mode) != 1 && S_ISLNK(st.st_mode) != 1)
     118                 :            :         {
     119                 :          0 :             log_msg(LOG_WARNING,
     120                 :            :                 "[-] file: %s is not a regular file or symbolic link.",
     121                 :            :                 file
     122                 :            :             );
     123                 :          0 :             return 0;
     124                 :            :         }
     125                 :            : 
     126         [ +  + ]:       5577 :         if((st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO)) != (S_IRUSR|S_IWUSR))
     127                 :            :         {
     128                 :       2161 :             log_msg(LOG_WARNING,
     129                 :            :                 "[-] file: %s permissions should only be user read/write (0600, -rw-------)",
     130                 :            :                 file
     131                 :            :             );
     132                 :            :             /* when we start in enforcing this instead of just warning
     133                 :            :              * the user
     134                 :            :             res = 0;
     135                 :            :             */
     136                 :            :         }
     137                 :            : 
     138         [ -  + ]:       5577 :         if(st.st_uid != getuid())
     139                 :            :         {
     140                 :          0 :             log_msg(LOG_WARNING, "[-] file: %s not owned by current effective user id",
     141                 :            :                 file);
     142                 :            :             /* when we start in enforcing this instead of just warning
     143                 :            :              * the user
     144                 :            :             res = 0;
     145                 :            :             */
     146                 :            :         }
     147                 :            :     }
     148                 :            :     else
     149                 :            :     {
     150                 :            :         /* if the path doesn't exist, just return, but otherwise something
     151                 :            :          * went wrong
     152                 :            :         */
     153         [ #  # ]:          0 :         if(errno != ENOENT)
     154                 :            :         {
     155                 :          0 :             log_msg(LOG_ERR, "[-] stat() against file: %s returned: %s",
     156                 :            :                 file, strerror(errno));
     157                 :          0 :             return 0;
     158                 :            :         }
     159                 :            :     }
     160                 :            : 
     161                 :            : #endif
     162                 :            : 
     163                 :            :     return 1;
     164                 :            : }
     165                 :            : 
     166                 :            : static int
     167                 :          0 : add_argv(char **argv_new, int *argc_new,
     168                 :            :         const char *new_arg, const fko_srv_options_t * const opts)
     169                 :            : {
     170                 :          0 :     int buf_size = 0;
     171                 :            : 
     172         [ #  # ]:          0 :     if(opts->verbose > 3)
     173                 :          0 :         log_msg(LOG_INFO, "[+] add_argv() + arg: %s", new_arg);
     174                 :            : 
     175                 :          0 :     buf_size = strlen(new_arg) + 1;
     176                 :          0 :     argv_new[*argc_new] = calloc(1, buf_size);
     177                 :            : 
     178         [ #  # ]:          0 :     if(argv_new[*argc_new] == NULL)
     179                 :            :     {
     180                 :          0 :         log_msg(LOG_INFO, "[*] Memory allocation error.");
     181                 :            :         return 0;
     182                 :            :     }
     183                 :          0 :     strlcpy(argv_new[*argc_new], new_arg, buf_size);
     184                 :            : 
     185                 :          0 :     *argc_new += 1;
     186                 :            : 
     187         [ #  # ]:          0 :     if(*argc_new >= MAX_CMDLINE_ARGS-1)
     188                 :            :     {
     189                 :          0 :         log_msg(LOG_ERR, "[*] max command line args exceeded.");
     190                 :            :         return 0;
     191                 :            :     }
     192                 :            : 
     193                 :          0 :     argv_new[*argc_new] = NULL;
     194                 :            : 
     195                 :            :     return 1;
     196                 :            : }
     197                 :            : 
     198                 :            : int
     199                 :          0 : strtoargv(const char * const args_str, char **argv_new, int *argc_new,
     200                 :            :         const fko_srv_options_t * const opts)
     201                 :            : {
     202                 :          0 :     int       current_arg_ctr = 0, i;
     203                 :          0 :     char      arg_tmp[MAX_LINE_LEN] = {0};
     204                 :            : 
     205         [ #  # ]:          0 :     for (i=0; i < (int)strlen(args_str); i++)
     206                 :            :     {
     207         [ #  # ]:          0 :         if (!isspace(args_str[i]))
     208                 :            :         {
     209                 :          0 :             arg_tmp[current_arg_ctr] = args_str[i];
     210                 :          0 :             current_arg_ctr++;
     211                 :            :         }
     212                 :            :         else
     213                 :            :         {
     214         [ #  # ]:          0 :             if(current_arg_ctr > 0)
     215                 :            :             {
     216                 :          0 :                 arg_tmp[current_arg_ctr] = '\0';
     217         [ #  # ]:          0 :                 if (add_argv(argv_new, argc_new, arg_tmp, opts) != 1)
     218                 :            :                 {
     219                 :          0 :                     free_argv(argv_new, argc_new);
     220                 :          0 :                     return 0;
     221                 :            :                 }
     222                 :            :                 current_arg_ctr = 0;
     223                 :            :             }
     224                 :            :         }
     225                 :            :     }
     226                 :            : 
     227                 :            :     /* pick up the last argument in the string
     228                 :            :     */
     229         [ #  # ]:          0 :     if(current_arg_ctr > 0)
     230                 :            :     {
     231                 :          0 :         arg_tmp[current_arg_ctr] = '\0';
     232         [ #  # ]:          0 :         if (add_argv(argv_new, argc_new, arg_tmp, opts) != 1)
     233                 :            :         {
     234                 :          0 :             free_argv(argv_new, argc_new);
     235                 :          0 :             return 0;
     236                 :            :         }
     237                 :            :     }
     238                 :            :     return 1;
     239                 :            : }
     240                 :            : 
     241                 :            : void
     242                 :          0 : free_argv(char **argv_new, int *argc_new)
     243                 :            : {
     244                 :            :     int i;
     245                 :            : 
     246 [ #  # ][ #  # ]:          0 :     if(argv_new == NULL || *argv_new == NULL)
     247                 :            :         return;
     248                 :            : 
     249         [ #  # ]:          0 :     for (i=0; i < *argc_new; i++)
     250                 :            :     {
     251         [ #  # ]:          0 :         if(argv_new[i] == NULL)
     252                 :            :             break;
     253                 :            :         else
     254                 :          0 :             free(argv_new[i]);
     255                 :            :     }
     256                 :            :     return;
     257                 :            : }
     258                 :            : 
     259                 :            : void
     260                 :       1814 : clean_exit(fko_srv_options_t *opts, unsigned int fw_cleanup_flag, unsigned int exit_status)
     261                 :            : {
     262                 :            : #if HAVE_LIBFIU
     263                 :            :     if(opts->config[CONF_FAULT_INJECTION_TAG] != NULL)
     264                 :            :     {
     265                 :            :         fiu_disable(opts->config[CONF_FAULT_INJECTION_TAG]);
     266                 :            :     }
     267                 :            : #endif
     268                 :            : 
     269 [ +  + ][ -  + ]:       1814 :     if(!opts->test && (fw_cleanup_flag == FW_CLEANUP))
     270                 :          0 :         fw_cleanup(opts);
     271                 :            : 
     272                 :            : #if USE_FILE_CACHE
     273                 :       1814 :     free_replay_list(opts);
     274                 :            : #endif
     275                 :            : 
     276                 :       1814 :     free_logging();
     277                 :       1814 :     free_configs(opts);
     278                 :       1814 :     exit(exit_status);
     279                 :            : }
     280                 :            : 
     281                 :            : /***EOF***/

Generated by: LCOV version 1.10