static int outfile_opendef(char *filename, char *type) { scamper_file_t *sf; int flags; mode_t mode; char sf_mode; int fd; flags = O_WRONLY | O_TRUNC | O_CREAT; sf_mode = 'w'; #ifndef _WIN32 mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; #else mode = _S_IREAD | _S_IWRITE; #endif if(strcmp(filename, "-") == 0) { fd = STDOUT_FILENO; } else { #if defined(WITHOUT_PRIVSEP) fd = open(filename, flags, mode); #else fd = scamper_privsep_open_file(filename, flags, mode); #endif } if(fd == -1) { return -1; } if((sf = scamper_file_openfd(fd, filename, sf_mode, type)) == NULL) { close(fd); return -1; } if((outfile_def = outfile_alloc(filename, sf)) == NULL) { scamper_file_close(sf); return -1; } return 0; }
int scamper_debug_open(const char *file) { mode_t mode; int flags, fd; #if defined(WITHOUT_PRIVSEP) && !defined(_WIN32) uid_t uid = getuid(); #endif #ifndef _WIN32 mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; #else mode = _S_IREAD | _S_IWRITE; #endif if(scamper_option_debugfileappend() == 0) flags = O_WRONLY | O_CREAT | O_TRUNC; else flags = O_WRONLY | O_CREAT | O_APPEND; #ifndef WITHOUT_PRIVSEP fd = scamper_privsep_open_file(file, flags, mode); #else fd = open(file, flags, mode); #endif if(fd == -1) { printerror(errno, strerror, __func__, "could not open debugfile %s", file); return -1; } if((debugfile = fdopen(fd, "a")) == NULL) { printerror(errno, strerror, __func__, "could not fdopen debugfile %s", file); return -1; } #if defined(WITHOUT_PRIVSEP) && !defined(_WIN32) if(uid != geteuid() && fchown(fd, uid, -1) != 0) { printerror(errno, strerror, __func__, "could not fchown"); } #endif return 0; }
scamper_outfile_t *scamper_outfile_open(char *name, char *file, char *mo) { scamper_outfile_t *sof; scamper_file_t *sf; int flags; mode_t mode; char sf_mode; int fd; #if defined(WITHOUT_PRIVSEP) && !defined(_WIN32) uid_t uid; #endif if(name == NULL || file == NULL || mo == NULL) { return NULL; } if((sof = scamper_outfiles_get(name)) != NULL) { return NULL; } if(strcasecmp(mo, "append") == 0) { flags = O_RDWR | O_APPEND | O_CREAT; sf_mode = 'a'; } else if(strcasecmp(mo, "truncate") == 0) { flags = O_WRONLY | O_TRUNC | O_CREAT; sf_mode = 'w'; } else { return NULL; } #ifndef _WIN32 mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; #else mode = _S_IREAD | _S_IWRITE; #endif #if defined(WITHOUT_PRIVSEP) fd = open(file, flags, mode); #else fd = scamper_privsep_open_file(file, flags, mode); #endif /* make sure the fd is valid, otherwise bail */ if(fd == -1) { return NULL; } #if defined(WITHOUT_PRIVSEP) && !defined(_WIN32) if((uid = getuid()) != geteuid() && fchown(fd, uid, -1) != 0) { printerror(errno, strerror, __func__, "could not fchown"); } #endif if((sf = scamper_file_openfd(fd, file, sf_mode, "warts")) == NULL) { close(fd); return NULL; } if((sof = outfile_alloc(name, sf)) == NULL) { scamper_file_close(sf); return NULL; } return sof; }