Ticket #89: fopen-use-statok.diff

File fopen-use-statok.diff, 8.3 kB (added by matthijs, 17 months ago)

The patch, against r240

  • libvuurmuur/src/io.c

    old new  
    2222#include "vuurmuur.h" 
    2323 
    2424 
    25 // 
     25/*  vuurmuur_fopen 
     26 
     27    A wrapper around fopen which can be used to open config files. This 
     28    function performs additionals checks on the file, appropriate for 
     29    configuration files (such as checking the owner, the permissions, etc.) 
     30 
     31    This wrapper only works on a regular file and only when it already exists 
     32    (even when opening for writing!). 
     33 
     34    The path and mode parameters are identical to the fopen(3) libc function. 
     35*/ 
    2636FILE * 
    27 vuurmuur_fopen(const char *path, const char *mode) 
     37vuurmuur_fopen(const int debuglvl, const char *path, const char *mode) 
    2838{ 
    2939    FILE        *fp=NULL; 
    30     struct stat stat_buf; 
    31     int         statted=0;  // can 'path' be stat-ed? 0: no, 1: yes 
    3240 
    33     // check if we can lstat the file. If not, we assume file doens't exist. 
    34     if(lstat(path, &stat_buf) == -1) 
    35         statted = 0 ; 
    36     else 
    37         statted = 1; 
     41    // Stat the file 
     42    if (!stat_ok(debuglvl, path, STATOK_WANT_FILE, STATOK_VERBOSE)) 
     43        // File not OK? Don't open it. stat_ok will have printed an error message already. 
     44        return NULL; 
    3845 
    39     // now look at the results 
    40     if(statted && S_ISLNK(stat_buf.st_mode) == 1) 
    41     { 
    42         (void)vrprint.error(-1, "Error", "opening '%s': For security reasons Vuurmuur will not allow following symbolic-links.", path); 
    43     } 
    44     else if(statted && (stat_buf.st_mode & S_IWGRP || stat_buf.st_mode & S_IWOTH)) 
     46    // now open the file, this should not fail because if we get here it exists and is readable, 
     47    // but we check to be sure. 
     48    if(!(fp=fopen(path, mode))) 
    4549    { 
    46         (void)vrprint.error(-1, "Error", "opening '%s': For security reasons Vuurmuur will not open files that are writable by 'group' or 'other'. Check the file content & permissions.", path); 
     50        (void)vrprint.error(-1, "Error", "opening '%s' failed: %s (in: vuurmuur_fopen).", path, strerror(errno)); 
     51        return NULL; 
    4752    } 
    48     else if(statted && (stat_buf.st_uid != 0 || stat_buf.st_gid != 0)) 
    49     { 
    50         (void)vrprint.error(-1, "Error", "opening '%s': For security reasons Vuurmuur will not open files that are not owned by root.", path); 
    51     } 
    52     else 
    53     { 
    54         // check if group and others can read the file. If so, fix the permissions. 
    55         if(statted && (stat_buf.st_mode & S_IRGRP || stat_buf.st_mode & S_IROTH)) 
    56         { 
    57             (void)vrprint.info("Info", "'%s' is readable by 'group' and 'other'. This is not recommended. Fixing.", path); 
    58             if(chmod(path, 0600) == -1) 
    59             { 
    60                 (void)vrprint.error(-1, "Error", "failed to repair file permissions for file '%s': %s.", path, strerror(errno)); 
    61                 return(NULL); 
    62             } 
    63         } 
    64         // check if group and others can execute the file. If so, fix the permissions. 
    65         if(statted && (stat_buf.st_mode & S_IXGRP || stat_buf.st_mode & S_IXOTH)) 
    66         { 
    67             (void)vrprint.info("Info", "'%s' is executable by 'group' and 'other'. This is not recommended. Fixing.", path); 
    68             if(chmod(path, 0600) == -1) 
    69             { 
    70                 (void)vrprint.error(-1, "Error", "failed to repair file permissions for file '%s': %s.", path, strerror(errno)); 
    71                 return(NULL); 
    72             } 
    73         } 
    7453 
    75         // now open the file, this should not fail because if we get here it exists and is readable, 
    76         // but we check to be sure. 
    77         if(!(fp=fopen(path, mode))) 
    78         { 
    79             (void)vrprint.error(-1, "Error", "opening '%s' failed: %s (in: vuurmuur_fopen).", path, strerror(errno)); 
    80         } 
    81         else 
    82         { 
    83             // return our succes 
    84             return(fp); 
    85         } 
    86     } 
    87  
    88     // if we get here, there was an error 
    89     return(NULL); 
     54    // return our succes 
     55    return(fp); 
    9056} 
    9157 
    9258 
     
    340306    Returns the pointer to the file, or NULL if failed. 
    341307*/ 
    342308FILE * 
    343 rules_file_open(const char *path, const char *mode, int caller) 
     309rules_file_open(const int debuglvl, const char *path, const char *mode, int caller) 
    344310{ 
    345311    FILE    *lock_fp = NULL, 
    346312            *fp = NULL; 
     
    431397        free(lock_path); 
    432398    } 
    433399 
    434     fp = vuurmuur_fopen(path, mode); 
     400    fp = vuurmuur_fopen(debuglvl, path, mode); 
    435401    return(fp); 
    436402} 
    437403 
  • libvuurmuur/plugins/textdir/textdir_ask.c

    old new  
    9696    /* now open and read the file, but only if it is not already open */ 
    9797    if(ptr->file == NULL) 
    9898    { 
    99         if(!(ptr->file = vuurmuur_fopen(file_location, "r"))) 
     99        if(!(ptr->file = vuurmuur_fopen(debuglvl, file_location, "r"))) 
    100100        { 
    101101            (void)vrprint.error(-1, "Error", "Unable to open file '%s'.", file_location); 
    102102 
  • libvuurmuur/plugins/textdir/textdir_tell.c

    old new  
    8585    /* 
    8686        first open the file for reading 
    8787    */ 
    88     if(!(fp = vuurmuur_fopen(file_location, "r"))) 
     88    if(!(fp = vuurmuur_fopen(debuglvl, file_location, "r"))) 
    8989    { 
    9090        (void)vrprint.error(-1, "Error", "unable to open file '%s' for reading: %s.", file_location, strerror(errno)); 
    9191 
     
    321321    /* 
    322322        now open the file for writing 
    323323    */ 
    324     if(!(fp = vuurmuur_fopen(file_location, "w+"))) 
     324    if(!(fp = vuurmuur_fopen(debuglvl, file_location, "w+"))) 
    325325    { 
    326326        (void)vrprint.error(-1, "Error", "unable to open file '%s' for writing: %s (in: %s).", file_location, strerror(errno), __FUNC__); 
    327327         
  • libvuurmuur/src/config.c

    old new  
    15581558    if(!question || !file_location || size == 0) 
    15591559        return(-1); 
    15601560 
    1561     if(!(fp = vuurmuur_fopen(file_location,"r"))) 
     1561    if(!(fp = vuurmuur_fopen(debuglvl, file_location,"r"))) 
    15621562    { 
    15631563        (void)vrprint.error(-1, "Error", "unable to open configfile '%s': %s (in: ask_configfile).", file_location, strerror(errno)); 
    15641564        return(-1); 
  • libvuurmuur/src/rules.c

    old new  
    13711371    } 
    13721372 
    13731373    /* open the rulesfile */ 
    1374     if(!(fp = rules_file_open(rulesfile_location, "w+", 0))) 
     1374    if(!(fp = rules_file_open(debuglvl, rulesfile_location, "w+", 0))) 
    13751375    { 
    13761376        (void)vrprint.error(-1, "Error", "opening rulesfile '%s' failed: %s (in: %s).", 
    13771377                rulesfile_location, strerror(errno), __FUNC__); 
  • libvuurmuur/src/vuurmuur.h

    old new  
    14011401/* 
    14021402    io.c 
    14031403*/ 
    1404 FILE *vuurmuur_fopen(const char *path, const char *mode); 
     1404FILE *vuurmuur_fopen(const int debuglvl, const char *path, const char *mode); 
    14051405DIR *vuurmuur_opendir(const int, const char *); 
    14061406int stat_ok(const int, const char *, char, char); 
    14071407int check_pidfile(char *pidfile_location); 
    14081408int create_pidfile(char *pidfile_location, int shm_id); 
    14091409int remove_pidfile(char *pidfile_location); 
    1410 FILE * rules_file_open(const char *path, const char *mode, int caller); 
     1410FILE * rules_file_open(const int debuglvl, const char *path, const char *mode, int caller); 
    14111411int rules_file_close(FILE *file, const char *path); 
    14121412int pipe_command(const int, struct vuurmuur_config *, char *, char); 
    14131413int libvuurmuur_exec_command(const int, struct vuurmuur_config *, char *, char **, char *);