Ticket #89: fopen-use-statok.diff
| File fopen-use-statok.diff, 8.3 kB (added by matthijs, 17 months ago) |
|---|
-
libvuurmuur/src/io.c
old new 22 22 #include "vuurmuur.h" 23 23 24 24 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 */ 26 36 FILE * 27 vuurmuur_fopen(const char *path, const char *mode)37 vuurmuur_fopen(const int debuglvl, const char *path, const char *mode) 28 38 { 29 39 FILE *fp=NULL; 30 struct stat stat_buf;31 int statted=0; // can 'path' be stat-ed? 0: no, 1: yes32 40 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; 38 45 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))) 45 49 { 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; 47 52 } 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 else53 {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 }74 53 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); 90 56 } 91 57 92 58 … … 340 306 Returns the pointer to the file, or NULL if failed. 341 307 */ 342 308 FILE * 343 rules_file_open(const char *path, const char *mode, int caller)309 rules_file_open(const int debuglvl, const char *path, const char *mode, int caller) 344 310 { 345 311 FILE *lock_fp = NULL, 346 312 *fp = NULL; … … 431 397 free(lock_path); 432 398 } 433 399 434 fp = vuurmuur_fopen( path, mode);400 fp = vuurmuur_fopen(debuglvl, path, mode); 435 401 return(fp); 436 402 } 437 403 -
libvuurmuur/plugins/textdir/textdir_ask.c
old new 96 96 /* now open and read the file, but only if it is not already open */ 97 97 if(ptr->file == NULL) 98 98 { 99 if(!(ptr->file = vuurmuur_fopen( file_location, "r")))99 if(!(ptr->file = vuurmuur_fopen(debuglvl, file_location, "r"))) 100 100 { 101 101 (void)vrprint.error(-1, "Error", "Unable to open file '%s'.", file_location); 102 102 -
libvuurmuur/plugins/textdir/textdir_tell.c
old new 85 85 /* 86 86 first open the file for reading 87 87 */ 88 if(!(fp = vuurmuur_fopen( file_location, "r")))88 if(!(fp = vuurmuur_fopen(debuglvl, file_location, "r"))) 89 89 { 90 90 (void)vrprint.error(-1, "Error", "unable to open file '%s' for reading: %s.", file_location, strerror(errno)); 91 91 … … 321 321 /* 322 322 now open the file for writing 323 323 */ 324 if(!(fp = vuurmuur_fopen( file_location, "w+")))324 if(!(fp = vuurmuur_fopen(debuglvl, file_location, "w+"))) 325 325 { 326 326 (void)vrprint.error(-1, "Error", "unable to open file '%s' for writing: %s (in: %s).", file_location, strerror(errno), __FUNC__); 327 327 -
libvuurmuur/src/config.c
old new 1558 1558 if(!question || !file_location || size == 0) 1559 1559 return(-1); 1560 1560 1561 if(!(fp = vuurmuur_fopen( file_location,"r")))1561 if(!(fp = vuurmuur_fopen(debuglvl, file_location,"r"))) 1562 1562 { 1563 1563 (void)vrprint.error(-1, "Error", "unable to open configfile '%s': %s (in: ask_configfile).", file_location, strerror(errno)); 1564 1564 return(-1); -
libvuurmuur/src/rules.c
old new 1371 1371 } 1372 1372 1373 1373 /* open the rulesfile */ 1374 if(!(fp = rules_file_open( rulesfile_location, "w+", 0)))1374 if(!(fp = rules_file_open(debuglvl, rulesfile_location, "w+", 0))) 1375 1375 { 1376 1376 (void)vrprint.error(-1, "Error", "opening rulesfile '%s' failed: %s (in: %s).", 1377 1377 rulesfile_location, strerror(errno), __FUNC__); -
libvuurmuur/src/vuurmuur.h
old new 1401 1401 /* 1402 1402 io.c 1403 1403 */ 1404 FILE *vuurmuur_fopen(const char *path, const char *mode);1404 FILE *vuurmuur_fopen(const int debuglvl, const char *path, const char *mode); 1405 1405 DIR *vuurmuur_opendir(const int, const char *); 1406 1406 int stat_ok(const int, const char *, char, char); 1407 1407 int check_pidfile(char *pidfile_location); 1408 1408 int create_pidfile(char *pidfile_location, int shm_id); 1409 1409 int remove_pidfile(char *pidfile_location); 1410 FILE * rules_file_open(const char *path, const char *mode, int caller);1410 FILE * rules_file_open(const int debuglvl, const char *path, const char *mode, int caller); 1411 1411 int rules_file_close(FILE *file, const char *path); 1412 1412 int pipe_command(const int, struct vuurmuur_config *, char *, char); 1413 1413 int libvuurmuur_exec_command(const int, struct vuurmuur_config *, char *, char **, char *);
