| 1 | /* |
| 2 | * AIDE (Advanced Intrusion Detection Environment) |
| 3 | * |
| 4 | * Copyright (C) 1999-2003, 2005-2006, 2010-2011, 2013, 2019-2023 Rami Lehti, |
| 5 | * Pablo Virolainen, Richard van den Berg, Hannes von Haugwitz |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License as |
| 9 | * published by the Free Software Foundation; either version 2 of the |
| 10 | * License, or (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | */ |
| 21 | |
| 22 | #include "config.h" |
| 23 | #include <string.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdbool.h> |
| 26 | #include <stdlib.h> |
| 27 | #ifdef HAVE_FCNTL |
| 28 | #include <fcntl.h> |
| 29 | #endif |
| 30 | #include <unistd.h> |
| 31 | #include <errno.h> |
| 32 | #ifdef WITH_ZLIB |
| 33 | #include <zlib.h> |
| 34 | #endif |
| 35 | #include "log.h" |
| 36 | #include "util.h" |
| 37 | #include "errorcodes.h" |
| 38 | #ifdef WITH_CURL |
| 39 | #include "fopen.h" |
| 40 | #endif |
| 41 | #include "be.h" |
| 42 | #include "url.h" |
| 43 | |
| 44 | /*for locale support*/ |
| 45 | #include "locale-aide.h" |
| 46 | /*for locale support*/ |
| 47 | |
| 48 | |
| 49 | void* be_init(url_t* u, bool readonly, bool iszipped, bool append, int linenumber, char* filename, char* linebuf, bool *created) { |
| 50 | FILE* fh=NULL; |
| 51 | long a=0; |
| 52 | char* err=NULL; |
| 53 | int fd; |
| 54 | #if HAVE_FCNTL && HAVE_FTRUNCATE |
| 55 | struct flock fl; |
| 56 | #endif |
| 57 | |
| 58 | switch (u->type) { |
| 59 | case url_file : { |
| 60 | u->value = expand_tilde(u->value); |
| 61 | log_msg(LOG_LEVEL_DEBUG, "open (%s, gzip: %s, append: %s) file '%s'", readonly?"read-only":"read/write", btoa(iszipped), btoa(append), u->value); |
| 62 | bool new_file = false; |
| 63 | if (readonly) { |
| 64 | fd = open(u->value, O_RDONLY, 0); |
| 65 | } else { |
| 66 | int flag_truncate; |
| 67 | #if HAVE_FCNTL && HAVE_FTRUNCATE |
| 68 | flag_truncate = 0; |
| 69 | #else |
| 70 | flag_truncate = O_TRUNC; |
| 71 | #endif |
| 72 | fd = open(u->value, O_CREAT|O_EXCL|O_RDWR, 0666); |
| 73 | new_file = fd >= 0; |
| 74 | if (fd == -1 && errno == EEXIST) { |
| 75 | fd = open(u->value, O_RDWR|(append?O_APPEND:flag_truncate), 0666); |
| 76 | } |
| 77 | } |
| 78 | if(fd==-1) { |
| 79 | LOG_CONFIG_FORMAT_LINE(LOG_LEVEL_ERROR, "open (%s) failed for file '%s': %s", readonly?"read-only":"read/write", u->value, strerror(errno)); |
| 80 | return NULL; |
| 81 | } else { |
| 82 | if (created) { *created = new_file; } |
| 83 | log_msg(LOG_LEVEL_DEBUG, "%s file '%s' with fd=%i",new_file?"created":"opened existing", u->value,fd); |
| 84 | } |
| 85 | #if HAVE_FCNTL && HAVE_FTRUNCATE |
| 86 | if(!readonly) { |
| 87 | if (strcmp(u->value, "/dev/null")) { |
| 88 | fl.l_type = F_WRLCK; |
| 89 | fl.l_whence = SEEK_SET; |
| 90 | fl.l_start = 0; |
| 91 | fl.l_len = 0; |
| 92 | log_msg(LOG_LEVEL_DEBUG, "try to get lock for file '%s'", u->value); |
| 93 | if (fcntl(fd, F_SETLK, &fl) == -1) { |
| 94 | log_msg(LOG_LEVEL_ERROR, "cannot get lock for file '%s': %s", u->value, strerror(errno)); |
| 95 | exit(LOCK_ERROR); |
| 96 | } else { |
| 97 | log_msg(LOG_LEVEL_DEBUG, "successfully got lock for file '%s'", u->value); |
| 98 | } |
| 99 | if (!append) { |
| 100 | if(ftruncate(fd,0)==-1) { |
| 101 | log_msg(LOG_LEVEL_ERROR,_("ftruncate failed for file %s: %s"),u->value, strerror(errno)); |
| 102 | return NULL; |
| 103 | } else { |
| 104 | log_msg(LOG_LEVEL_DEBUG, "successfully truncated file '%s' to size 0", u->value); |
| 105 | } |
| 106 | } |
| 107 | } else { |
| 108 | log_msg(LOG_LEVEL_DEBUG, "skip lock for '/dev/null'"); |
| 109 | } |
| 110 | } |
| 111 | #endif |
| 112 | #ifdef WITH_ZLIB |
| 113 | if(iszipped && !readonly){ |
| 114 | gzFile gzfh = gzdopen(fd,"wb9"); |
| 115 | if(gzfh==NULL){ |
| 116 | log_msg(LOG_LEVEL_ERROR, _("gzdopen (%s) failed for file %s"), readonly?"read-only":"read/write", u->value); |
| 117 | } |
| 118 | return gzfh; |
| 119 | } |
| 120 | else{ |
| 121 | #endif |
| 122 | fh=fdopen(fd,readonly?"r":"w+"); |
| 123 | if(fh==NULL){ |
| 124 | log_msg(LOG_LEVEL_ERROR, _("fdopen (%s) failed for file '%s': %s"), readonly?"read-only":"read/write", u->value, strerror(errno)); |
| 125 | } |
| 126 | return fh; |
| 127 | #ifdef WITH_ZLIB |
| 128 | } |
| 129 | #endif |
| 130 | } |
| 131 | case url_stdout : { |
| 132 | #ifdef WITH_ZLIB |
| 133 | if(iszipped){ |
| 134 | return gzdopen(fileno(stdout),"wb"); |
| 135 | } |
| 136 | else{ |
| 137 | #endif |
| 138 | return stdout; |
| 139 | #ifdef WITH_ZLIB |
| 140 | } |
| 141 | #endif |
| 142 | } |
| 143 | case url_stdin : { |
| 144 | #ifdef WITH_ZLIB |
| 145 | if(iszipped){ |
| 146 | return gzdopen(fileno(stdin),"r"); |
| 147 | } |
| 148 | else{ |
| 149 | #endif |
| 150 | return stdin; |
| 151 | #ifdef WITH_ZLIB |
| 152 | } |
| 153 | #endif |
| 154 | } |
| 155 | case url_stderr : { |
| 156 | #ifdef WITH_ZLIB |
| 157 | if(iszipped){ |
| 158 | return gzdopen(fileno(stderr),"wb"); |
| 159 | } |
| 160 | else{ |
| 161 | #endif |
| 162 | return stderr; |
| 163 | #ifdef WITH_ZLIB |
| 164 | } |
| 165 | #endif |
| 166 | } |
| 167 | case url_fd : { |
| 168 | a=strtol(u->value,&err,10); |
| 169 | if(*err!='\0'||errno==ERANGE){ |
| 170 | log_msg(LOG_LEVEL_ERROR,"illegal file descriptor value:%s",u->value); |
| 171 | } |
| 172 | #ifdef WITH_ZLIB |
| 173 | if(iszipped && !readonly){ |
| 174 | gzFile gzfh = gzdopen(a,"w"); |
| 175 | if(gzfh==NULL){ |
| 176 | log_msg(LOG_LEVEL_ERROR,"couldn't reopen file descriptor %li",a); |
| 177 | } |
| 178 | return gzfh; |
| 179 | } |
| 180 | else{ |
| 181 | #endif |
| 182 | fh=fdopen(a,readonly?"r":"w"); |
| 183 | if(fh==NULL){ |
| 184 | log_msg(LOG_LEVEL_ERROR,"couldn't reopen file descriptor %li",a); |
| 185 | } |
| 186 | return fh; |
| 187 | #ifdef WITH_ZLIB |
| 188 | } |
| 189 | #endif |
| 190 | } |
| 191 | #ifdef WITH_CURL |
| 192 | case url_http: |
| 193 | case url_https: |
| 194 | case url_ftp: |
| 195 | { |
| 196 | log_msg(LOG_LEVEL_DEBUG,_("opening curl '%s' for %s"),u->value,readonly?"r":"w+"); |
| 197 | if (iszipped) { |
| 198 | return NULL; |
| 199 | } |
| 200 | return url_fopen(u->value,readonly?"r":"w+"); |
| 201 | } |
| 202 | #endif /* WITH CURL */ |
| 203 | default:{ |
| 204 | log_msg(LOG_LEVEL_ERROR, "unsupported backend: %i", u->type); |
| 205 | return NULL; |
| 206 | } |
| 207 | } |
| 208 | /* Not reached */ |
| 209 | return NULL; |
| 210 | |
| 211 | } |